blob: ad78eaabf231a2fd8c79e00c0d1cfcee687088ee [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"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050075
Marius Vlad78984ee2019-06-11 00:05:08 +030076/**
77 * \defgroup head Head
78 * \defgroup output Output
79 * \defgroup compositor Compositor
80 */
81
Pekka Paalanen0513a952014-05-21 16:17:27 +030082#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
83
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020084static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030085weston_output_update_matrix(struct weston_output *output);
86
87static void
Alexander Larsson0b135062013-05-28 16:23:36 +020088weston_output_transform_scale_init(struct weston_output *output,
89 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020090
Rob Bradford27b17932013-06-26 18:08:46 +010091static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050092weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010093
Pekka Paalanendcac3512017-12-08 14:13:34 +020094static char *
95weston_output_create_heads_string(struct weston_output *output);
96
Pekka Paalanen6528c032017-03-28 15:27:10 +030097/** Send wl_output events for mode and scale changes
98 *
99 * \param head Send on all resources bound to this head.
100 * \param mode_changed If true, send the current mode.
101 * \param scale_changed If true, send the current scale.
102 */
103static void
104weston_mode_switch_send_events(struct weston_head *head,
105 bool mode_changed, bool scale_changed)
106{
107 struct weston_output *output = head->output;
108 struct wl_resource *resource;
109 int version;
110
111 wl_resource_for_each(resource, &head->resource_list) {
112 if (mode_changed) {
113 wl_output_send_mode(resource,
114 output->current_mode->flags,
115 output->current_mode->width,
116 output->current_mode->height,
117 output->current_mode->refresh);
118 }
119
120 version = wl_resource_get_version(resource);
121 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
122 wl_output_send_scale(resource, output->current_scale);
123
124 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
125 wl_output_send_done(resource);
126 }
Roman Gilge97391c2019-03-29 13:01:06 +0100127 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
128 zxdg_output_v1_send_logical_position(resource,
129 output->x,
130 output->y);
131 zxdg_output_v1_send_logical_size(resource,
132 output->width,
133 output->height);
134 zxdg_output_v1_send_done(resource);
135 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300136}
137
138static void
139weston_mode_switch_finish(struct weston_output *output,
140 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800141{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200142 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300143 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200144 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200145
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200146 pixman_region32_init(&old_output_region);
147 pixman_region32_copy(&old_output_region, &output->region);
148
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200149 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200150 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200151
152 pixman_region32_init(&output->previous_damage);
153 pixman_region32_init_rect(&output->region, output->x, output->y,
154 output->width, output->height);
155
156 weston_output_update_matrix(output);
157
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200158 /* If a pointer falls outside the outputs new geometry, move it to its
159 * lower-right corner */
160 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500161 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200162 int32_t x, y;
163
164 if (!pointer)
165 continue;
166
167 x = wl_fixed_to_int(pointer->x);
168 y = wl_fixed_to_int(pointer->y);
169
170 if (!pixman_region32_contains_point(&old_output_region,
171 x, y, NULL) ||
172 pixman_region32_contains_point(&output->region,
173 x, y, NULL))
174 continue;
175
176 if (x >= output->x + output->width)
177 x = output->x + output->width - 1;
178 if (y >= output->y + output->height)
179 y = output->y + output->height - 1;
180
181 pointer->x = wl_fixed_from_int(x);
182 pointer->y = wl_fixed_from_int(y);
183 }
184
185 pixman_region32_fini(&old_output_region);
186
Derek Foremandd4cd332014-11-10 10:29:59 -0600187 if (!mode_changed && !scale_changed)
188 return;
189
Hardening57388e42013-09-18 23:56:36 +0200190 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300191 wl_list_for_each(head, &output->head_list, output_link)
192 weston_mode_switch_send_events(head,
193 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600194}
195
David Fort0de859e2016-05-27 23:22:57 +0200196static void
197weston_compositor_reflow_outputs(struct weston_compositor *compositor,
198 struct weston_output *resized_output, int delta_width);
199
Marius Vlad55d87362019-06-11 01:15:35 +0300200/**
201 * \ingroup output
202 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600203WL_EXPORT int
204weston_output_mode_set_native(struct weston_output *output,
205 struct weston_mode *mode,
206 int32_t scale)
207{
208 int ret;
209 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200210 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600211
212 if (!output->switch_mode)
213 return -1;
214
215 if (!output->original_mode) {
216 mode_changed = 1;
217 ret = output->switch_mode(output, mode);
218 if (ret < 0)
219 return ret;
220 if (output->current_scale != scale) {
221 scale_changed = 1;
222 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200223 }
224 }
225
David Fort0de859e2016-05-27 23:22:57 +0200226 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600227 output->native_mode = mode;
228 output->native_scale = scale;
229
230 weston_mode_switch_finish(output, mode_changed, scale_changed);
231
David Fort0de859e2016-05-27 23:22:57 +0200232 if (mode_changed || scale_changed) {
233 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
234
235 wl_signal_emit(&output->compositor->output_resized_signal, output);
236 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600237 return 0;
238}
239
Marius Vlad55d87362019-06-11 01:15:35 +0300240/**
241 * \ingroup output
242 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600243WL_EXPORT int
244weston_output_mode_switch_to_native(struct weston_output *output)
245{
246 int ret;
247 int mode_changed = 0, scale_changed = 0;
248
249 if (!output->switch_mode)
250 return -1;
251
252 if (!output->original_mode) {
253 weston_log("already in the native mode\n");
254 return -1;
255 }
256 /* the non fullscreen clients haven't seen a mode set since we
257 * switched into a temporary, so we need to notify them if the
258 * mode at that time is different from the native mode now.
259 */
260 mode_changed = (output->original_mode != output->native_mode);
261 scale_changed = (output->original_scale != output->native_scale);
262
263 ret = output->switch_mode(output, output->native_mode);
264 if (ret < 0)
265 return ret;
266
267 output->current_scale = output->native_scale;
268
269 output->original_mode = NULL;
270 output->original_scale = 0;
271
272 weston_mode_switch_finish(output, mode_changed, scale_changed);
273
274 return 0;
275}
276
Marius Vlad55d87362019-06-11 01:15:35 +0300277/**
278 * \ingroup output
279 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600280WL_EXPORT int
281weston_output_mode_switch_to_temporary(struct weston_output *output,
282 struct weston_mode *mode,
283 int32_t scale)
284{
285 int ret;
286
287 if (!output->switch_mode)
288 return -1;
289
290 /* original_mode is the last mode non full screen clients have seen,
291 * so we shouldn't change it if we already have one set.
292 */
293 if (!output->original_mode) {
294 output->original_mode = output->native_mode;
295 output->original_scale = output->native_scale;
296 }
297 ret = output->switch_mode(output, mode);
298 if (ret < 0)
299 return ret;
300
301 output->current_scale = scale;
302
303 weston_mode_switch_finish(output, 0, 0);
304
305 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800306}
307
Benjamin Franzke06286262011-05-06 19:12:33 +0200308static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300309region_init_infinite(pixman_region32_t *region)
310{
311 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
312 UINT32_MAX, UINT32_MAX);
313}
314
Pekka Paalanene67858b2013-04-25 13:57:42 +0300315static struct weston_subsurface *
316weston_surface_to_subsurface(struct weston_surface *surface);
317
Jason Ekstranda7af7042013-10-12 22:38:11 -0500318WL_EXPORT struct weston_view *
319weston_view_create(struct weston_surface *surface)
320{
321 struct weston_view *view;
322
Bryce Harringtonde16d892014-11-20 22:21:57 -0800323 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500324 if (view == NULL)
325 return NULL;
326
327 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000328 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500329
Jason Ekstranda7af7042013-10-12 22:38:11 -0500330 /* Assign to surface */
331 wl_list_insert(&surface->views, &view->surface_link);
332
333 wl_signal_init(&view->destroy_signal);
334 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300335 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500336
Jason Ekstranda7af7042013-10-12 22:38:11 -0500337 pixman_region32_init(&view->clip);
338
339 view->alpha = 1.0;
340 pixman_region32_init(&view->transform.opaque);
341
342 wl_list_init(&view->geometry.transformation_list);
343 wl_list_insert(&view->geometry.transformation_list,
344 &view->transform.position.link);
345 weston_matrix_init(&view->transform.position.matrix);
346 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200347 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500348 pixman_region32_init(&view->transform.boundingbox);
349 view->transform.dirty = 1;
350
Jason Ekstranda7af7042013-10-12 22:38:11 -0500351 return view;
352}
353
Jason Ekstrand108865d2014-06-26 10:04:49 -0700354struct weston_frame_callback {
355 struct wl_resource *resource;
356 struct wl_list link;
357};
358
Pekka Paalanen133e4392014-09-23 22:08:46 -0400359struct weston_presentation_feedback {
360 struct wl_resource *resource;
361
362 /* XXX: could use just wl_resource_get_link() instead */
363 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200364
365 /* The per-surface feedback flags */
366 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400367};
368
369static void
370weston_presentation_feedback_discard(
371 struct weston_presentation_feedback *feedback)
372{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200373 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400374 wl_resource_destroy(feedback->resource);
375}
376
377static void
378weston_presentation_feedback_discard_list(struct wl_list *list)
379{
380 struct weston_presentation_feedback *feedback, *tmp;
381
382 wl_list_for_each_safe(feedback, tmp, list, link)
383 weston_presentation_feedback_discard(feedback);
384}
385
386static void
387weston_presentation_feedback_present(
388 struct weston_presentation_feedback *feedback,
389 struct weston_output *output,
390 uint32_t refresh_nsec,
391 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200392 uint64_t seq,
393 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400394{
395 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300396 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400397 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200398 uint32_t tv_sec_hi;
399 uint32_t tv_sec_lo;
400 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300401 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400402
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300403 wl_list_for_each(head, &output->head_list, output_link) {
404 wl_resource_for_each(o, &head->resource_list) {
405 if (wl_resource_get_client(o) != client)
406 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400407
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300408 wp_presentation_feedback_send_sync_output(feedback->resource, o);
409 done = true;
410 }
411
412 /* For clone mode, send it for just one wl_output global,
413 * they are all equivalent anyway.
414 */
415 if (done)
416 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400417 }
418
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200419 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200420 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200421 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200422 refresh_nsec,
423 seq >> 32, seq & 0xffffffff,
424 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400425 wl_resource_destroy(feedback->resource);
426}
427
428static void
429weston_presentation_feedback_present_list(struct wl_list *list,
430 struct weston_output *output,
431 uint32_t refresh_nsec,
432 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200433 uint64_t seq,
434 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400435{
436 struct weston_presentation_feedback *feedback, *tmp;
437
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200438 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200439 wl_list_empty(list));
440
Pekka Paalanen133e4392014-09-23 22:08:46 -0400441 wl_list_for_each_safe(feedback, tmp, list, link)
442 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200443 refresh_nsec, ts, seq,
444 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400445}
446
Jason Ekstrand7b982072014-05-20 14:33:03 -0500447static void
448surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
449{
450 struct weston_surface_state *state =
451 container_of(listener, struct weston_surface_state,
452 buffer_destroy_listener);
453
454 state->buffer = NULL;
455}
456
457static void
458weston_surface_state_init(struct weston_surface_state *state)
459{
460 state->newly_attached = 0;
461 state->buffer = NULL;
462 state->buffer_destroy_listener.notify =
463 surface_state_handle_buffer_destroy;
464 state->sx = 0;
465 state->sy = 0;
466
Derek Foreman152254b2015-11-26 14:17:48 -0600467 pixman_region32_init(&state->damage_surface);
468 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500469 pixman_region32_init(&state->opaque);
470 region_init_infinite(&state->input);
471
472 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400473 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500474
475 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
476 state->buffer_viewport.buffer.scale = 1;
477 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
478 state->buffer_viewport.surface.width = -1;
479 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300480
481 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530482
483 state->desired_protection = WESTON_HDCP_DISABLE;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500484}
485
486static void
487weston_surface_state_fini(struct weston_surface_state *state)
488{
489 struct weston_frame_callback *cb, *next;
490
491 wl_list_for_each_safe(cb, next,
492 &state->frame_callback_list, link)
493 wl_resource_destroy(cb->resource);
494
Pekka Paalanen133e4392014-09-23 22:08:46 -0400495 weston_presentation_feedback_discard_list(&state->feedback_list);
496
Jason Ekstrand7b982072014-05-20 14:33:03 -0500497 pixman_region32_fini(&state->input);
498 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600499 pixman_region32_fini(&state->damage_surface);
500 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500501
502 if (state->buffer)
503 wl_list_remove(&state->buffer_destroy_listener.link);
504 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300505
506 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300507 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500508}
509
510static void
511weston_surface_state_set_buffer(struct weston_surface_state *state,
512 struct weston_buffer *buffer)
513{
514 if (state->buffer == buffer)
515 return;
516
517 if (state->buffer)
518 wl_list_remove(&state->buffer_destroy_listener.link);
519 state->buffer = buffer;
520 if (state->buffer)
521 wl_signal_add(&state->buffer->destroy_signal,
522 &state->buffer_destroy_listener);
523}
524
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500525WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500526weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500527{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500528 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400529
Bryce Harringtonde16d892014-11-20 22:21:57 -0800530 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400531 if (surface == NULL)
532 return NULL;
533
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500534 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800535 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500536
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500537 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200538 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400539
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200540 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
541 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200542 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
543 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500544
545 weston_surface_state_init(&surface->pending);
546
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400547 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500548 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300549 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400550
Jason Ekstranda7af7042013-10-12 22:38:11 -0500551 wl_list_init(&surface->views);
552
553 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400554 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500555
Pekka Paalanene67858b2013-04-25 13:57:42 +0300556 wl_list_init(&surface->subsurface_list);
557 wl_list_init(&surface->subsurface_list_pending);
558
Jason Ekstrand1e059042014-10-16 10:55:19 -0500559 weston_matrix_init(&surface->buffer_to_surface_matrix);
560 weston_matrix_init(&surface->surface_to_buffer_matrix);
561
Jonas Ådahld3414f22016-07-22 17:56:31 +0800562 wl_list_init(&surface->pointer_constraints);
563
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300564 surface->acquire_fence_fd = -1;
565
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530566 surface->desired_protection = WESTON_HDCP_DISABLE;
567
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400568 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500569}
570
Alex Wu8811bf92012-02-28 18:07:54 +0800571WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500572weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200573 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500574{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100575 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200576 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500577}
578
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400579WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500580weston_view_to_global_float(struct weston_view *view,
581 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200582{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500583 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200584 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
585
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200587
588 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200589 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700590 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200591 v.f[3]);
592 *x = 0;
593 *y = 0;
594 return;
595 }
596
597 *x = v.f[0] / v.f[3];
598 *y = v.f[1] / v.f[3];
599 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500600 *x = sx + view->geometry.x;
601 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200602 }
603}
604
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500605WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200606weston_transformed_coord(int width, int height,
607 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200608 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200609 float sx, float sy, float *bx, float *by)
610{
611 switch (transform) {
612 case WL_OUTPUT_TRANSFORM_NORMAL:
613 default:
614 *bx = sx;
615 *by = sy;
616 break;
617 case WL_OUTPUT_TRANSFORM_FLIPPED:
618 *bx = width - sx;
619 *by = sy;
620 break;
621 case WL_OUTPUT_TRANSFORM_90:
622 *bx = height - sy;
623 *by = sx;
624 break;
625 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
626 *bx = height - sy;
627 *by = width - sx;
628 break;
629 case WL_OUTPUT_TRANSFORM_180:
630 *bx = width - sx;
631 *by = height - sy;
632 break;
633 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
634 *bx = sx;
635 *by = height - sy;
636 break;
637 case WL_OUTPUT_TRANSFORM_270:
638 *bx = sy;
639 *by = width - sx;
640 break;
641 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
642 *bx = sy;
643 *by = sx;
644 break;
645 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200646
647 *bx *= scale;
648 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200649}
650
651WL_EXPORT pixman_box32_t
652weston_transformed_rect(int width, int height,
653 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200654 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200655 pixman_box32_t rect)
656{
657 float x1, x2, y1, y2;
658
659 pixman_box32_t ret;
660
Alexander Larsson4ea95522013-05-22 14:41:37 +0200661 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200662 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200663 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200664 rect.x2, rect.y2, &x2, &y2);
665
666 if (x1 <= x2) {
667 ret.x1 = x1;
668 ret.x2 = x2;
669 } else {
670 ret.x1 = x2;
671 ret.x2 = x1;
672 }
673
674 if (y1 <= y2) {
675 ret.y1 = y1;
676 ret.y2 = y2;
677 } else {
678 ret.y1 = y2;
679 ret.y2 = y1;
680 }
681
682 return ret;
683}
684
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600685/** Transform a region by a matrix, restricted to axis-aligned transformations
686 *
687 * Warning: This function does not work for projective, affine, or matrices
688 * that encode arbitrary rotations. Only 90-degree step rotations are
689 * supported.
690 */
691WL_EXPORT void
692weston_matrix_transform_region(pixman_region32_t *dest,
693 struct weston_matrix *matrix,
694 pixman_region32_t *src)
695{
696 pixman_box32_t *src_rects, *dest_rects;
697 int nrects, i;
698
699 src_rects = pixman_region32_rectangles(src, &nrects);
700 dest_rects = malloc(nrects * sizeof(*dest_rects));
701 if (!dest_rects)
702 return;
703
704 for (i = 0; i < nrects; i++) {
705 struct weston_vector vec1 = {{
706 src_rects[i].x1, src_rects[i].y1, 0, 1
707 }};
708 weston_matrix_transform(matrix, &vec1);
709 vec1.f[0] /= vec1.f[3];
710 vec1.f[1] /= vec1.f[3];
711
712 struct weston_vector vec2 = {{
713 src_rects[i].x2, src_rects[i].y2, 0, 1
714 }};
715 weston_matrix_transform(matrix, &vec2);
716 vec2.f[0] /= vec2.f[3];
717 vec2.f[1] /= vec2.f[3];
718
719 if (vec1.f[0] < vec2.f[0]) {
720 dest_rects[i].x1 = floor(vec1.f[0]);
721 dest_rects[i].x2 = ceil(vec2.f[0]);
722 } else {
723 dest_rects[i].x1 = floor(vec2.f[0]);
724 dest_rects[i].x2 = ceil(vec1.f[0]);
725 }
726
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600727 if (vec1.f[1] < vec2.f[1]) {
728 dest_rects[i].y1 = floor(vec1.f[1]);
729 dest_rects[i].y2 = ceil(vec2.f[1]);
730 } else {
731 dest_rects[i].y1 = floor(vec2.f[1]);
732 dest_rects[i].y2 = ceil(vec1.f[1]);
733 }
734 }
735
736 pixman_region32_clear(dest);
737 pixman_region32_init_rects(dest, dest_rects, nrects);
738 free(dest_rects);
739}
740
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200741WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500742weston_transformed_region(int width, int height,
743 enum wl_output_transform transform,
744 int32_t scale,
745 pixman_region32_t *src, pixman_region32_t *dest)
746{
747 pixman_box32_t *src_rects, *dest_rects;
748 int nrects, i;
749
750 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
751 if (src != dest)
752 pixman_region32_copy(dest, src);
753 return;
754 }
755
756 src_rects = pixman_region32_rectangles(src, &nrects);
757 dest_rects = malloc(nrects * sizeof(*dest_rects));
758 if (!dest_rects)
759 return;
760
761 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
762 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
763 } else {
764 for (i = 0; i < nrects; i++) {
765 switch (transform) {
766 default:
767 case WL_OUTPUT_TRANSFORM_NORMAL:
768 dest_rects[i].x1 = src_rects[i].x1;
769 dest_rects[i].y1 = src_rects[i].y1;
770 dest_rects[i].x2 = src_rects[i].x2;
771 dest_rects[i].y2 = src_rects[i].y2;
772 break;
773 case WL_OUTPUT_TRANSFORM_90:
774 dest_rects[i].x1 = height - src_rects[i].y2;
775 dest_rects[i].y1 = src_rects[i].x1;
776 dest_rects[i].x2 = height - src_rects[i].y1;
777 dest_rects[i].y2 = src_rects[i].x2;
778 break;
779 case WL_OUTPUT_TRANSFORM_180:
780 dest_rects[i].x1 = width - src_rects[i].x2;
781 dest_rects[i].y1 = height - src_rects[i].y2;
782 dest_rects[i].x2 = width - src_rects[i].x1;
783 dest_rects[i].y2 = height - src_rects[i].y1;
784 break;
785 case WL_OUTPUT_TRANSFORM_270:
786 dest_rects[i].x1 = src_rects[i].y1;
787 dest_rects[i].y1 = width - src_rects[i].x2;
788 dest_rects[i].x2 = src_rects[i].y2;
789 dest_rects[i].y2 = width - src_rects[i].x1;
790 break;
791 case WL_OUTPUT_TRANSFORM_FLIPPED:
792 dest_rects[i].x1 = width - src_rects[i].x2;
793 dest_rects[i].y1 = src_rects[i].y1;
794 dest_rects[i].x2 = width - src_rects[i].x1;
795 dest_rects[i].y2 = src_rects[i].y2;
796 break;
797 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
798 dest_rects[i].x1 = height - src_rects[i].y2;
799 dest_rects[i].y1 = width - src_rects[i].x2;
800 dest_rects[i].x2 = height - src_rects[i].y1;
801 dest_rects[i].y2 = width - src_rects[i].x1;
802 break;
803 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
804 dest_rects[i].x1 = src_rects[i].x1;
805 dest_rects[i].y1 = height - src_rects[i].y2;
806 dest_rects[i].x2 = src_rects[i].x2;
807 dest_rects[i].y2 = height - src_rects[i].y1;
808 break;
809 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
810 dest_rects[i].x1 = src_rects[i].y1;
811 dest_rects[i].y1 = src_rects[i].x1;
812 dest_rects[i].x2 = src_rects[i].y2;
813 dest_rects[i].y2 = src_rects[i].x2;
814 break;
815 }
816 }
817 }
818
819 if (scale != 1) {
820 for (i = 0; i < nrects; i++) {
821 dest_rects[i].x1 *= scale;
822 dest_rects[i].x2 *= scale;
823 dest_rects[i].y1 *= scale;
824 dest_rects[i].y2 *= scale;
825 }
826 }
827
828 pixman_region32_clear(dest);
829 pixman_region32_init_rects(dest, dest_rects, nrects);
830 free(dest_rects);
831}
832
Jonny Lamb74130762013-11-26 18:19:46 +0100833static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300834viewport_surface_to_buffer(struct weston_surface *surface,
835 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100836{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200837 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200838 double src_width, src_height;
839 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200840
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200841 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
842 if (vp->surface.width == -1) {
843 *bx = sx;
844 *by = sy;
845 return;
846 }
Jonny Lamb74130762013-11-26 18:19:46 +0100847
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200848 src_x = 0.0;
849 src_y = 0.0;
850 src_width = surface->width_from_buffer;
851 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100852 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200853 src_x = wl_fixed_to_double(vp->buffer.src_x);
854 src_y = wl_fixed_to_double(vp->buffer.src_y);
855 src_width = wl_fixed_to_double(vp->buffer.src_width);
856 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100857 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200858
859 *bx = sx * src_width / surface->width + src_x;
860 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100861}
862
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500863WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200864weston_surface_to_buffer_float(struct weston_surface *surface,
865 float sx, float sy, float *bx, float *by)
866{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200867 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
868
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300869 /* first transform coordinates if the viewport is set */
870 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100871
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500872 weston_transformed_coord(surface->width_from_buffer,
873 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200874 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100875 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200876}
877
Derek Foreman99739672015-12-03 16:38:11 -0600878/** Transform a rectangle from surface coordinates to buffer coordinates
879 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300880 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600881 * from.
882 * \param rect The rectangle to transform.
883 * \return The transformed rectangle.
884 *
885 * Viewport and buffer transformations can only do translation, scaling,
886 * and rotations in 90-degree steps. Therefore the only loss in the
887 * conversion is coordinate rounding.
888 *
889 * However, some coordinate rounding takes place as an intermediate
890 * step before the buffer scale factor is applied, so the rectangle
891 * boundary may not be exactly as expected.
892 *
893 * This is OK for damage tracking since a little extra coverage is
894 * not a problem.
895 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200896WL_EXPORT pixman_box32_t
897weston_surface_to_buffer_rect(struct weston_surface *surface,
898 pixman_box32_t rect)
899{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200900 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100901 float xf, yf;
902
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300903 /* first transform box coordinates if the viewport is set */
904 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100905 rect.x1 = floorf(xf);
906 rect.y1 = floorf(yf);
907
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300908 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600909 rect.x2 = ceilf(xf);
910 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100911
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500912 return weston_transformed_rect(surface->width_from_buffer,
913 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200914 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200915 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200916}
917
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200918/** Transform a region from surface coordinates to buffer coordinates
919 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300920 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200921 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300922 * \param[in] surface_region The region in surface coordinates.
923 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200924 *
925 * Buffer_region must be init'd, but will be completely overwritten.
926 *
927 * Viewport and buffer transformations can only do translation, scaling,
928 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600929 * conversion is from the coordinate rounding that takes place in
930 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300931 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200932 */
933WL_EXPORT void
934weston_surface_to_buffer_region(struct weston_surface *surface,
935 pixman_region32_t *surface_region,
936 pixman_region32_t *buffer_region)
937{
938 pixman_box32_t *src_rects, *dest_rects;
939 int nrects, i;
940
941 src_rects = pixman_region32_rectangles(surface_region, &nrects);
942 dest_rects = malloc(nrects * sizeof(*dest_rects));
943 if (!dest_rects)
944 return;
945
946 for (i = 0; i < nrects; i++) {
947 dest_rects[i] = weston_surface_to_buffer_rect(surface,
948 src_rects[i]);
949 }
950
951 pixman_region32_fini(buffer_region);
952 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
953 free(dest_rects);
954}
955
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200956WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400958 struct weston_plane *plane)
959{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500960 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400961 return;
962
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963 weston_view_damage_below(view);
964 view->plane = plane;
965 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400966}
967
Pekka Paalanen51723d52015-02-17 13:10:01 +0200968/** Inflict damage on the plane where the view is visible.
969 *
970 * \param view The view that causes the damage.
971 *
972 * If the view is currently on a plane (including the primary plane),
973 * take the view's boundingbox, subtract all the opaque views that cover it,
974 * and add the remaining region as damage to the plane. This corresponds
975 * to the damage inflicted to the plane if this view disappeared.
976 *
977 * A repaint is scheduled for this view.
978 *
979 * The region of all opaque views covering this view is stored in
980 * weston_view::clip and updated by view_accumulate_damage() during
981 * weston_output_repaint(). Specifically, that region matches the
982 * scenegraph as it was last painted.
983 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400984WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500985weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200986{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500987 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200988
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500989 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200990 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800992 if (view->plane)
993 pixman_region32_union(&view->plane->damage,
994 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500995 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800996 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200997}
998
Pekka Paalanen01e00682017-03-24 16:21:06 +0200999/** Send wl_surface.enter/leave events
1000 *
1001 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001002 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001003 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001004 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001005 *
1006 * Send the enter/leave events for all protocol objects bound to the given
1007 * output by the client owning the surface.
1008 */
1009static void
1010weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001011 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001012 bool enter,
1013 bool leave)
1014{
1015 struct wl_resource *wloutput;
1016 struct wl_client *client;
1017
1018 assert(enter != leave);
1019
1020 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001021 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001022 if (wl_resource_get_client(wloutput) != client)
1023 continue;
1024
1025 if (enter)
1026 wl_surface_send_enter(surface->resource, wloutput);
1027 if (leave)
1028 wl_surface_send_leave(surface->resource, wloutput);
1029 }
1030}
1031
Bryce Harrington3f650b82015-12-23 11:01:58 -08001032/**
1033 * \param es The surface
1034 * \param mask The new set of outputs for the surface
1035 *
1036 * Sets the surface's set of outputs to the ones specified by
1037 * the new output mask provided. Identifies the outputs that
1038 * have changed, the posts enter and leave events for these
1039 * outputs as appropriate.
1040 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001041static void
1042weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1043{
1044 uint32_t different = es->output_mask ^ mask;
1045 uint32_t entered = mask & different;
1046 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001047 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001048 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001049 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001050
1051 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001052 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001053 return;
1054 if (different == 0)
1055 return;
1056
1057 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001058 output_bit = 1u << output->id;
1059 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001060 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001061
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001062 wl_list_for_each(head, &output->head_list, output_link) {
1063 weston_surface_send_enter_leave(es, head,
1064 output_bit & entered,
1065 output_bit & left);
1066 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001067 }
1068}
1069
Semi Malinene7a52fb2018-04-26 11:08:10 +02001070static void
1071notify_view_output_destroy(struct wl_listener *listener, void *data)
1072{
1073 struct weston_view *view =
1074 container_of(listener,
1075 struct weston_view, output_destroy_listener);
1076
1077 view->output = NULL;
1078 view->output_destroy_listener.notify = NULL;
1079}
1080
1081/** Set the primary output of the view
1082 *
1083 * \param view The view whose primary output to set
1084 * \param output The new primary output for the view
1085 *
1086 * Set \a output to be the primary output of the \a view.
1087 *
1088 * Notice that the assignment may be temporary; the primary output could be
1089 * automatically changed. Hence, one cannot rely on the value persisting.
1090 *
1091 * Passing NULL as /a output will set the primary output to NULL.
1092 */
1093WL_EXPORT void
1094weston_view_set_output(struct weston_view *view, struct weston_output *output)
1095{
1096 if (view->output_destroy_listener.notify) {
1097 wl_list_remove(&view->output_destroy_listener.link);
1098 view->output_destroy_listener.notify = NULL;
1099 }
1100 view->output = output;
1101 if (output) {
1102 view->output_destroy_listener.notify =
1103 notify_view_output_destroy;
1104 wl_signal_add(&output->destroy_signal,
1105 &view->output_destroy_listener);
1106 }
1107}
1108
Bryce Harrington3f650b82015-12-23 11:01:58 -08001109/** Recalculate which output(s) the surface has views displayed on
1110 *
1111 * \param es The surface to remap to outputs
1112 *
1113 * Finds the output that is showing the largest amount of one
1114 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001115 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001116 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001117 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001118 * in the output_mask for the surface.
1119 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001120static void
1121weston_surface_assign_output(struct weston_surface *es)
1122{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123 struct weston_output *new_output;
1124 struct weston_view *view;
1125 pixman_region32_t region;
1126 uint32_t max, area, mask;
1127 pixman_box32_t *e;
1128
1129 new_output = NULL;
1130 max = 0;
1131 mask = 0;
1132 pixman_region32_init(&region);
1133 wl_list_for_each(view, &es->views, surface_link) {
1134 if (!view->output)
1135 continue;
1136
1137 pixman_region32_intersect(&region, &view->transform.boundingbox,
1138 &view->output->region);
1139
1140 e = pixman_region32_extents(&region);
1141 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1142
1143 mask |= view->output_mask;
1144
1145 if (area >= max) {
1146 new_output = view->output;
1147 max = area;
1148 }
1149 }
1150 pixman_region32_fini(&region);
1151
1152 es->output = new_output;
1153 weston_surface_update_output_mask(es, mask);
1154}
1155
Bryce Harrington3f650b82015-12-23 11:01:58 -08001156/** Recalculate which output(s) the view is displayed on
1157 *
1158 * \param ev The view to remap to outputs
1159 *
1160 * Identifies the set of outputs that the view is visible on,
1161 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001162 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001163 *
1164 * Also does the same for the view's surface. See
1165 * weston_surface_assign_output().
1166 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167static void
1168weston_view_assign_output(struct weston_view *ev)
1169{
1170 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001171 struct weston_output *output, *new_output;
1172 pixman_region32_t region;
1173 uint32_t max, area, mask;
1174 pixman_box32_t *e;
1175
1176 new_output = NULL;
1177 max = 0;
1178 mask = 0;
1179 pixman_region32_init(&region);
1180 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001181 if (output->destroying)
1182 continue;
1183
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001185 &output->region);
1186
1187 e = pixman_region32_extents(&region);
1188 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1189
1190 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001191 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001192
1193 if (area >= max) {
1194 new_output = output;
1195 max = area;
1196 }
1197 }
1198 pixman_region32_fini(&region);
1199
Semi Malinene7a52fb2018-04-26 11:08:10 +02001200 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001201 ev->output_mask = mask;
1202
1203 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001204}
1205
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001206static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001207weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1208 int from_x, int from_y, int *to_x, int *to_y)
1209{
1210 float x, y;
1211
1212 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1213 weston_view_from_global_float(to, x, y, &x, &y);
1214
1215 *to_x = round(x);
1216 *to_y = round(y);
1217}
1218
1219static void
1220weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1221{
1222 pixman_box32_t *a;
1223 pixman_box32_t b;
1224
1225 a = pixman_region32_extents(&from->geometry.scissor);
1226
1227 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1228 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1229
1230 pixman_region32_fini(&to->geometry.scissor);
1231 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1232}
1233
1234static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001235view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001236 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001237{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001238 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1239 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001240 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001241 { inbox->x1, inbox->y1 },
1242 { inbox->x1, inbox->y2 },
1243 { inbox->x2, inbox->y1 },
1244 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001245 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001246 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001247 int i;
1248
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001249 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001250 /* avoid rounding empty bbox to 1x1 */
1251 pixman_region32_init(bbox);
1252 return;
1253 }
1254
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001255 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001256 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001258 if (x < min_x)
1259 min_x = x;
1260 if (x > max_x)
1261 max_x = x;
1262 if (y < min_y)
1263 min_y = y;
1264 if (y > max_y)
1265 max_y = y;
1266 }
1267
Pekka Paalanen219b9822012-02-08 15:38:37 +02001268 int_x = floorf(min_x);
1269 int_y = floorf(min_y);
1270 pixman_region32_init_rect(bbox, int_x, int_y,
1271 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001272}
1273
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001274static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001276{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001278
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001279 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 view->geometry.x = roundf(view->geometry.x);
1281 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001282
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001283 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1285 view->transform.position.matrix.d[12] = view->geometry.x;
1286 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001287
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001289
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 view->transform.inverse = view->transform.position.matrix;
1291 view->transform.inverse.d[12] = -view->geometry.x;
1292 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001293
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001295 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001296 view->surface->width,
1297 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001298 if (view->geometry.scissor_enabled)
1299 pixman_region32_intersect(&view->transform.boundingbox,
1300 &view->transform.boundingbox,
1301 &view->geometry.scissor);
1302
1303 pixman_region32_translate(&view->transform.boundingbox,
1304 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001305
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 if (view->alpha == 1.0) {
1307 pixman_region32_copy(&view->transform.opaque,
1308 &view->surface->opaque);
1309 pixman_region32_translate(&view->transform.opaque,
1310 view->geometry.x,
1311 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001312 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001313}
1314
1315static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001317{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 struct weston_view *parent = view->geometry.parent;
1319 struct weston_matrix *matrix = &view->transform.matrix;
1320 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001321 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001322 pixman_region32_t surfregion;
1323 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001324
Jason Ekstranda7af7042013-10-12 22:38:11 -05001325 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001326
1327 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1329 view->transform.position.matrix.d[12] = view->geometry.x;
1330 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001331
1332 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001334 weston_matrix_multiply(matrix, &tform->matrix);
1335
Pekka Paalanen483243f2013-03-08 14:56:50 +02001336 if (parent)
1337 weston_matrix_multiply(matrix, &parent->transform.matrix);
1338
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001339 if (weston_matrix_invert(inverse, matrix) < 0) {
1340 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 weston_log("error: weston_view %p"
1342 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001343 return -1;
1344 }
1345
Giulio Camuffo148c1992016-09-04 18:50:46 +03001346 if (view->alpha == 1.0 &&
1347 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1348 pixman_region32_copy(&view->transform.opaque,
1349 &view->surface->opaque);
1350 pixman_region32_translate(&view->transform.opaque,
1351 matrix->d[12],
1352 matrix->d[13]);
1353 }
1354
Pekka Paalanen380adf52015-02-16 14:39:11 +02001355 pixman_region32_init_rect(&surfregion, 0, 0,
1356 view->surface->width, view->surface->height);
1357 if (view->geometry.scissor_enabled)
1358 pixman_region32_intersect(&surfregion, &surfregion,
1359 &view->geometry.scissor);
1360 surfbox = pixman_region32_extents(&surfregion);
1361
1362 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1363 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001364
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001365 return 0;
1366}
1367
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001368static struct weston_layer *
1369get_view_layer(struct weston_view *view)
1370{
1371 if (view->parent_view)
1372 return get_view_layer(view->parent_view);
1373 return view->layer_link.layer;
1374}
1375
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001376WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001378{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001380 struct weston_layer *layer;
1381 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001382
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001384 return;
1385
Pekka Paalanen483243f2013-03-08 14:56:50 +02001386 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001388
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001390
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001392
Jason Ekstranda7af7042013-10-12 22:38:11 -05001393 pixman_region32_fini(&view->transform.boundingbox);
1394 pixman_region32_fini(&view->transform.opaque);
1395 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001396
Pekka Paalanencd403622012-01-25 13:37:39 +02001397 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 if (view->geometry.transformation_list.next ==
1399 &view->transform.position.link &&
1400 view->geometry.transformation_list.prev ==
1401 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001402 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001404 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405 if (weston_view_update_transform_enable(view) < 0)
1406 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001407 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001408
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001409 layer = get_view_layer(view);
1410 if (layer) {
1411 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001412 pixman_region32_intersect(&view->transform.boundingbox,
1413 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001414 pixman_region32_intersect(&view->transform.opaque,
1415 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001416 pixman_region32_fini(&mask);
1417 }
1418
Pekka Paalanen380adf52015-02-16 14:39:11 +02001419 if (parent) {
1420 if (parent->geometry.scissor_enabled) {
1421 view->geometry.scissor_enabled = true;
1422 weston_view_transfer_scissor(parent, view);
1423 } else {
1424 view->geometry.scissor_enabled = false;
1425 }
1426 }
1427
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001429
Jason Ekstranda7af7042013-10-12 22:38:11 -05001430 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 wl_signal_emit(&view->surface->compositor->transform_signal,
1433 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001434}
1435
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001436WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001438{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001440
1441 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 * The invariant: if view->geometry.dirty, then all views
1443 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001444 * Corollary: if not parent->geometry.dirty, then all ancestors
1445 * are not dirty.
1446 */
1447
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001449 return;
1450
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001452
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001454 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001456}
1457
1458WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459weston_view_to_global_fixed(struct weston_view *view,
1460 wl_fixed_t vx, wl_fixed_t vy,
1461 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001462{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001463 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001464
Jason Ekstranda7af7042013-10-12 22:38:11 -05001465 weston_view_to_global_float(view,
1466 wl_fixed_to_double(vx),
1467 wl_fixed_to_double(vy),
1468 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001469 *x = wl_fixed_from_double(xf);
1470 *y = wl_fixed_from_double(yf);
1471}
1472
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001473WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474weston_view_from_global_float(struct weston_view *view,
1475 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001476{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001478 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1479
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001481
1482 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001483 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001484 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001485 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486 *vx = 0;
1487 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001488 return;
1489 }
1490
Jason Ekstranda7af7042013-10-12 22:38:11 -05001491 *vx = v.f[0] / v.f[3];
1492 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001493 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001494 *vx = x - view->geometry.x;
1495 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001496 }
1497}
1498
1499WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001500weston_view_from_global_fixed(struct weston_view *view,
1501 wl_fixed_t x, wl_fixed_t y,
1502 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001503{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001504 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001505
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506 weston_view_from_global_float(view,
1507 wl_fixed_to_double(x),
1508 wl_fixed_to_double(y),
1509 &vxf, &vyf);
1510 *vx = wl_fixed_from_double(vxf);
1511 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001512}
1513
1514WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515weston_view_from_global(struct weston_view *view,
1516 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001517{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001519
Jason Ekstranda7af7042013-10-12 22:38:11 -05001520 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1521 *vx = floorf(vxf);
1522 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001523}
1524
Bryce Harrington3f650b82015-12-23 11:01:58 -08001525/**
1526 * \param surface The surface to be repainted
1527 *
1528 * Marks the output(s) that the surface is shown on as needing to be
1529 * repainted. See weston_output_schedule_repaint().
1530 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001531WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001532weston_surface_schedule_repaint(struct weston_surface *surface)
1533{
1534 struct weston_output *output;
1535
1536 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001537 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001538 weston_output_schedule_repaint(output);
1539}
1540
Bryce Harrington3f650b82015-12-23 11:01:58 -08001541/**
1542 * \param view The view to be repainted
1543 *
1544 * Marks the output(s) that the view is shown on as needing to be
1545 * repainted. See weston_output_schedule_repaint().
1546 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001547WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548weston_view_schedule_repaint(struct weston_view *view)
1549{
1550 struct weston_output *output;
1551
1552 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001553 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001554 weston_output_schedule_repaint(output);
1555}
1556
Pekka Paalanene508ce62015-02-19 13:59:55 +02001557/**
1558 * XXX: This function does it the wrong way.
1559 * surface->damage is the damage from the client, and causes
1560 * surface_flush_damage() to copy pixels. No window management action can
1561 * cause damage to the client-provided content, warranting re-upload!
1562 *
1563 * Instead of surface->damage, this function should record the damage
1564 * with all the views for this surface to avoid extraneous texture
1565 * uploads.
1566 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001567WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001568weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001569{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001570 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001571 0, 0, surface->width,
1572 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001573
Kristian Høgsberg98238702012-08-03 16:29:12 -04001574 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001575}
1576
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001577WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001579{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001580 if (view->geometry.x == x && view->geometry.y == y)
1581 return;
1582
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583 view->geometry.x = x;
1584 view->geometry.y = y;
1585 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001586}
1587
Pekka Paalanen483243f2013-03-08 14:56:50 +02001588static void
1589transform_parent_handle_parent_destroy(struct wl_listener *listener,
1590 void *data)
1591{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592 struct weston_view *view =
1593 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001594 geometry.parent_destroy_listener);
1595
Jason Ekstranda7af7042013-10-12 22:38:11 -05001596 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001597}
1598
1599WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001601 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001602{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603 if (view->geometry.parent) {
1604 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1605 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001606
1607 if (!parent)
1608 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001609 }
1610
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001612
Jason Ekstranda7af7042013-10-12 22:38:11 -05001613 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001614 transform_parent_handle_parent_destroy;
1615 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001616 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001617 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001618 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001619 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001620 }
1621
Jason Ekstranda7af7042013-10-12 22:38:11 -05001622 weston_view_geometry_dirty(view);
1623}
1624
Pekka Paalanen380adf52015-02-16 14:39:11 +02001625/** Set a clip mask rectangle on a view
1626 *
1627 * \param view The view to set the clip mask on.
1628 * \param x Top-left corner X coordinate of the clip rectangle.
1629 * \param y Top-left corner Y coordinate of the clip rectangle.
1630 * \param width Width of the clip rectangle, non-negative.
1631 * \param height Height of the clip rectangle, non-negative.
1632 *
1633 * A shell may set a clip mask rectangle on a view. Everything outside
1634 * the rectangle is cut away for input and output purposes: it is
1635 * not drawn and cannot be hit by hit-test based input like pointer
1636 * motion or touch-downs. Everything inside the rectangle will behave
1637 * normally. Clients are unaware of clipping.
1638 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001639 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001640 * mask rectangle does not affect the view position, the view is positioned
1641 * as it would be without a clip. The clip also does not change
1642 * weston_surface::width,height.
1643 *
1644 * The clip mask rectangle is part of transformation inheritance
1645 * (weston_view_set_transform_parent()). A clip set in the root of the
1646 * transformation inheritance tree will affect all views in the tree.
1647 * A clip can be set only on the root view. Attempting to set a clip
1648 * on view that has a transformation parent will fail. Assigning a parent
1649 * to a view that has a clip set will cause the clip to be forgotten.
1650 *
1651 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1652 * on the additional transformations in the child views. These transformations
1653 * may not rotate the coordinate axes, i.e., only translation and scaling
1654 * are allowed. Violating this restriction causes the clipping to malfunction.
1655 * Furthermore, using scaling may cause rounding errors in child clipping.
1656 *
1657 * The clip mask rectangle is not automatically adjusted based on
1658 * wl_surface.attach dx and dy arguments.
1659 *
1660 * A clip mask rectangle can be set only if the compositor capability
1661 * WESTON_CAP_VIEW_CLIP_MASK is present.
1662 *
1663 * This function sets the clip mask rectangle and schedules a repaint for
1664 * the view.
1665 */
1666WL_EXPORT void
1667weston_view_set_mask(struct weston_view *view,
1668 int x, int y, int width, int height)
1669{
1670 struct weston_compositor *compositor = view->surface->compositor;
1671
1672 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1673 weston_log("%s not allowed without capability!\n", __func__);
1674 return;
1675 }
1676
1677 if (view->geometry.parent) {
1678 weston_log("view %p has a parent, clip forbidden!\n", view);
1679 return;
1680 }
1681
1682 if (width < 0 || height < 0) {
1683 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1684 x, y, width, height);
1685 return;
1686 }
1687
1688 pixman_region32_fini(&view->geometry.scissor);
1689 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1690 view->geometry.scissor_enabled = true;
1691 weston_view_geometry_dirty(view);
1692 weston_view_schedule_repaint(view);
1693}
1694
1695/** Remove the clip mask from a view
1696 *
1697 * \param view The view to remove the clip mask from.
1698 *
1699 * Removed the clip mask rectangle and schedules a repaint.
1700 *
1701 * \sa weston_view_set_mask
1702 */
1703WL_EXPORT void
1704weston_view_set_mask_infinite(struct weston_view *view)
1705{
1706 view->geometry.scissor_enabled = false;
1707 weston_view_geometry_dirty(view);
1708 weston_view_schedule_repaint(view);
1709}
1710
Armin Krezović0da12b82016-06-30 06:04:33 +02001711/* Check if view should be displayed
1712 *
1713 * The indicator is set manually when assigning
1714 * a view to a surface.
1715 *
1716 * This needs reworking. See the thread starting at:
1717 *
1718 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1719 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001720WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001721weston_view_is_mapped(struct weston_view *view)
1722{
Armin Krezović0da12b82016-06-30 06:04:33 +02001723 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001724}
1725
Philipp Zabel70decd52018-09-03 20:11:15 +02001726/* Check if view is opaque in specified region
1727 *
1728 * \param view The view to check for opacity.
1729 * \param region The region to check for opacity, in view coordinates.
1730 *
1731 * Returns true if the view is opaque in the specified region, because view
1732 * alpha is 1.0 and either the opaque region set by the client contains the
1733 * specified region, or the buffer pixel format or solid color is opaque.
1734 */
1735WL_EXPORT bool
1736weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1737{
1738 pixman_region32_t r;
1739 bool ret = false;
1740
1741 if (ev->alpha < 1.0)
1742 return false;
1743
1744 if (ev->surface->is_opaque)
1745 return true;
1746
1747 if (ev->transform.dirty) {
1748 weston_log("%s: transform dirty", __func__);
1749 return false;
1750 }
1751
1752 pixman_region32_init(&r);
1753 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1754
1755 if (!pixman_region32_not_empty(&r))
1756 ret = true;
1757
1758 pixman_region32_fini(&r);
1759
1760 return ret;
1761}
1762
Armin Krezović0da12b82016-06-30 06:04:33 +02001763/* Check if a surface has a view assigned to it
1764 *
1765 * The indicator is set manually when mapping
1766 * a surface and creating a view for it.
1767 *
1768 * This needs to go. See the thread starting at:
1769 *
1770 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1771 *
1772 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001773WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001774weston_surface_is_mapped(struct weston_surface *surface)
1775{
Armin Krezović0da12b82016-06-30 06:04:33 +02001776 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001777}
1778
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001779static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001780surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001781{
1782 struct weston_view *view;
1783
1784 if (surface->width == width && surface->height == height)
1785 return;
1786
1787 surface->width = width;
1788 surface->height = height;
1789
1790 wl_list_for_each(view, &surface->views, surface_link)
1791 weston_view_geometry_dirty(view);
1792}
1793
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001794WL_EXPORT void
1795weston_surface_set_size(struct weston_surface *surface,
1796 int32_t width, int32_t height)
1797{
1798 assert(!surface->resource);
1799 surface_set_size(surface, width, height);
1800}
1801
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001802static int
1803fixed_round_up_to_int(wl_fixed_t f)
1804{
1805 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1806}
1807
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001808static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001809convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1810 int32_t width, int32_t height,
1811 uint32_t transform,
1812 int32_t scale)
1813{
1814 assert(scale > 0);
1815
1816 switch (transform) {
1817 case WL_OUTPUT_TRANSFORM_NORMAL:
1818 case WL_OUTPUT_TRANSFORM_180:
1819 case WL_OUTPUT_TRANSFORM_FLIPPED:
1820 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1821 *width_out = width / scale;
1822 *height_out = height / scale;
1823 break;
1824 case WL_OUTPUT_TRANSFORM_90:
1825 case WL_OUTPUT_TRANSFORM_270:
1826 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1827 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1828 *width_out = height / scale;
1829 *height_out = width / scale;
1830 break;
1831 default:
1832 assert(0 && "invalid transform");
1833 }
1834}
1835
1836static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001837weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001838{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001839 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001840
1841 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001842 surface->width_from_buffer = 0;
1843 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001844 return;
1845 }
1846
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001847 convert_size_by_transform_scale(&surface->width_from_buffer,
1848 &surface->height_from_buffer,
1849 surface->buffer_ref.buffer->width,
1850 surface->buffer_ref.buffer->height,
1851 vp->buffer.transform,
1852 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001853}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001854
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001855static void
1856weston_surface_update_size(struct weston_surface *surface)
1857{
1858 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1859 int32_t width, height;
1860
1861 width = surface->width_from_buffer;
1862 height = surface->height_from_buffer;
1863
1864 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001865 surface_set_size(surface,
1866 vp->surface.width, vp->surface.height);
1867 return;
1868 }
1869
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001870 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001871 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1872 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1873
1874 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001875 return;
1876 }
1877
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001878 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001879}
1880
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001881/** weston_compositor_get_time
1882 * \ingroup compositor
1883 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001884WL_EXPORT void
1885weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001886{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001887 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001888}
1889
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001890/** weston_compositor_pick_view
1891 * \ingroup compositor
1892 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001893WL_EXPORT struct weston_view *
1894weston_compositor_pick_view(struct weston_compositor *compositor,
1895 wl_fixed_t x, wl_fixed_t y,
1896 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001897{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001898 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001899 wl_fixed_t view_x, view_y;
1900 int view_ix, view_iy;
1901 int ix = wl_fixed_to_int(x);
1902 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001903
Jason Ekstranda7af7042013-10-12 22:38:11 -05001904 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001905 if (!pixman_region32_contains_point(
1906 &view->transform.boundingbox, ix, iy, NULL))
1907 continue;
1908
1909 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1910 view_ix = wl_fixed_to_int(view_x);
1911 view_iy = wl_fixed_to_int(view_y);
1912
1913 if (!pixman_region32_contains_point(&view->surface->input,
1914 view_ix, view_iy, NULL))
1915 continue;
1916
Pekka Paalanen380adf52015-02-16 14:39:11 +02001917 if (view->geometry.scissor_enabled &&
1918 !pixman_region32_contains_point(&view->geometry.scissor,
1919 view_ix, view_iy, NULL))
1920 continue;
1921
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001922 *vx = view_x;
1923 *vy = view_y;
1924 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001925 }
1926
Derek Foremanf9318d12015-05-11 15:40:11 -05001927 *vx = wl_fixed_from_int(-1000000);
1928 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001929 return NULL;
1930}
1931
1932static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001933weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001934{
Daniel Stone37816df2012-05-16 18:45:18 +01001935 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001936
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001937 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001938 return;
1939
Daniel Stone37816df2012-05-16 18:45:18 +01001940 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001941 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001942}
1943
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001944WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001945weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001946{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001947 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001948
Jason Ekstranda7af7042013-10-12 22:38:11 -05001949 if (!weston_view_is_mapped(view))
1950 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001951
Jason Ekstranda7af7042013-10-12 22:38:11 -05001952 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02001953 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08001954 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001955 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001956 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001957 wl_list_remove(&view->link);
1958 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001959 view->output_mask = 0;
1960 weston_surface_assign_output(view->surface);
1961
1962 if (weston_surface_is_mapped(view->surface))
1963 return;
1964
1965 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001966 struct weston_touch *touch = weston_seat_get_touch(seat);
1967 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1968 struct weston_keyboard *keyboard =
1969 weston_seat_get_keyboard(seat);
1970
1971 if (keyboard && keyboard->focus == view->surface)
1972 weston_keyboard_set_focus(keyboard, NULL);
1973 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001974 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001975 if (touch && touch->focus == view)
1976 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001977 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001978}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001979
Jason Ekstranda7af7042013-10-12 22:38:11 -05001980WL_EXPORT void
1981weston_surface_unmap(struct weston_surface *surface)
1982{
1983 struct weston_view *view;
1984
Armin Krezovićf8486c32016-06-30 06:04:28 +02001985 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001986 wl_list_for_each(view, &surface->views, surface_link)
1987 weston_view_unmap(view);
1988 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001989}
1990
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001991static void
1992weston_surface_reset_pending_buffer(struct weston_surface *surface)
1993{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001994 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001995 surface->pending.sx = 0;
1996 surface->pending.sy = 0;
1997 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001998 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001999}
2000
Jason Ekstranda7af7042013-10-12 22:38:11 -05002001WL_EXPORT void
2002weston_view_destroy(struct weston_view *view)
2003{
2004 wl_signal_emit(&view->destroy_signal, view);
2005
2006 assert(wl_list_empty(&view->geometry.child_list));
2007
2008 if (weston_view_is_mapped(view)) {
2009 weston_view_unmap(view);
2010 weston_compositor_build_view_list(view->surface->compositor);
2011 }
2012
2013 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002014 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002015
2016 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002017 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002019 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002020
2021 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002022 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023
Jason Ekstranda7af7042013-10-12 22:38:11 -05002024 wl_list_remove(&view->surface_link);
2025
2026 free(view);
2027}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002028
2029WL_EXPORT void
2030weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002031{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002032 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002033 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002034 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002035
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002036 if (--surface->ref_count > 0)
2037 return;
2038
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002039 assert(surface->resource == NULL);
2040
Pekka Paalanenca790762015-04-17 14:23:38 +03002041 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002042
Pekka Paalanene67858b2013-04-25 13:57:42 +03002043 assert(wl_list_empty(&surface->subsurface_list_pending));
2044 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002045
Jason Ekstranda7af7042013-10-12 22:38:11 -05002046 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2047 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002048
Jason Ekstrand7b982072014-05-20 14:33:03 -05002049 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002050
Pekka Paalanende685b82012-12-04 15:58:12 +02002051 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002052 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002053
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002054 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002055 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002056 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002057
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002058 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002059 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002060
Pekka Paalanen133e4392014-09-23 22:08:46 -04002061 weston_presentation_feedback_discard_list(&surface->feedback_list);
2062
Jonas Ådahld3414f22016-07-22 17:56:31 +08002063 wl_list_for_each_safe(constraint, next_constraint,
2064 &surface->pointer_constraints,
2065 link)
2066 weston_pointer_constraint_destroy(constraint);
2067
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002068 fd_clear(&surface->acquire_fence_fd);
2069
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002070 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002071}
2072
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002073static void
2074destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002075{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002076 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002077
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002078 assert(surface);
2079
Giulio Camuffo0d379742013-11-15 22:06:15 +01002080 /* Set the resource to NULL, since we don't want to leave a
2081 * dangling pointer if the surface was refcounted and survives
2082 * the weston_surface_destroy() call. */
2083 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002084
2085 if (surface->viewport_resource)
2086 wl_resource_set_user_data(surface->viewport_resource, NULL);
2087
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002088 if (surface->synchronization_resource) {
2089 wl_resource_set_user_data(surface->synchronization_resource,
2090 NULL);
2091 }
2092
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002093 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002094}
2095
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002096static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002097weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2098{
2099 struct weston_buffer *buffer =
2100 container_of(listener, struct weston_buffer, destroy_listener);
2101
2102 wl_signal_emit(&buffer->destroy_signal, buffer);
2103 free(buffer);
2104}
2105
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002106WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002107weston_buffer_from_resource(struct wl_resource *resource)
2108{
2109 struct weston_buffer *buffer;
2110 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002111
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002112 listener = wl_resource_get_destroy_listener(resource,
2113 weston_buffer_destroy_handler);
2114
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002115 if (listener)
2116 return container_of(listener, struct weston_buffer,
2117 destroy_listener);
2118
2119 buffer = zalloc(sizeof *buffer);
2120 if (buffer == NULL)
2121 return NULL;
2122
2123 buffer->resource = resource;
2124 wl_signal_init(&buffer->destroy_signal);
2125 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002126 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002127 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002128
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002129 return buffer;
2130}
2131
2132static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002133weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2134 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002135{
Pekka Paalanende685b82012-12-04 15:58:12 +02002136 struct weston_buffer_reference *ref =
2137 container_of(listener, struct weston_buffer_reference,
2138 destroy_listener);
2139
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002140 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002141 ref->buffer = NULL;
2142}
2143
2144WL_EXPORT void
2145weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002146 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002147{
2148 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002149 ref->buffer->busy_count--;
2150 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002151 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002152 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002153 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002154 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002155 }
2156
Pekka Paalanende685b82012-12-04 15:58:12 +02002157 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002158 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002159 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002160 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002161 }
2162
Pekka Paalanende685b82012-12-04 15:58:12 +02002163 ref->buffer = buffer;
2164 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2165}
2166
2167static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002168weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2169 void *data)
2170{
2171 struct weston_buffer_release_reference *ref =
2172 container_of(listener, struct weston_buffer_release_reference,
2173 destroy_listener);
2174
2175 assert((struct wl_resource *)data == ref->buffer_release->resource);
2176 ref->buffer_release = NULL;
2177}
2178
2179static void
2180weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2181{
2182 struct wl_resource *resource = buffer_release->resource;
2183 int release_fence_fd = buffer_release->fence_fd;
2184
2185 if (release_fence_fd >= 0) {
2186 zwp_linux_buffer_release_v1_send_fenced_release(
2187 resource, release_fence_fd);
2188 } else {
2189 zwp_linux_buffer_release_v1_send_immediate_release(
2190 resource);
2191 }
2192
2193 wl_resource_destroy(resource);
2194}
2195
2196WL_EXPORT void
2197weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2198 struct weston_buffer_release *buffer_release)
2199{
2200 if (buffer_release == ref->buffer_release)
2201 return;
2202
2203 if (ref->buffer_release) {
2204 ref->buffer_release->ref_count--;
2205 wl_list_remove(&ref->destroy_listener.link);
2206 if (ref->buffer_release->ref_count == 0)
2207 weston_buffer_release_destroy(ref->buffer_release);
2208 }
2209
2210 if (buffer_release) {
2211 buffer_release->ref_count++;
2212 wl_resource_add_destroy_listener(buffer_release->resource,
2213 &ref->destroy_listener);
2214 }
2215
2216 ref->buffer_release = buffer_release;
2217 ref->destroy_listener.notify =
2218 weston_buffer_release_reference_handle_destroy;
2219}
2220
2221WL_EXPORT void
2222weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2223 struct weston_buffer_release_reference *src)
2224{
2225 weston_buffer_release_reference(dest, src->buffer_release);
2226 weston_buffer_release_reference(src, NULL);
2227}
2228
2229static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002230weston_surface_attach(struct weston_surface *surface,
2231 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002232{
2233 weston_buffer_reference(&surface->buffer_ref, buffer);
2234
Pekka Paalanena6421c42012-12-04 15:58:10 +02002235 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002236 if (weston_surface_is_mapped(surface))
2237 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002238 }
2239
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002240 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002241
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002242 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002243 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002244}
2245
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002246/** weston_compositor_damage_all
2247 * \ingroup compositor
2248 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002249WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002250weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002251{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002252 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002253
2254 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002255 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002256}
2257
Marius Vlad55d87362019-06-11 01:15:35 +03002258/**
2259 * \ingroup output
2260 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002261WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002262weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002263{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002264 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002265
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002266 pixman_region32_union(&compositor->primary_plane.damage,
2267 &compositor->primary_plane.damage,
2268 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002269 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002270}
2271
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002272static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002273surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002274{
Pekka Paalanende685b82012-12-04 15:58:12 +02002275 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002276 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002277 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002278
Pekka Paalanenb5026542014-11-12 15:09:24 +02002279 if (weston_timeline_enabled_ &&
2280 pixman_region32_not_empty(&surface->damage))
2281 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2282 TLP_OUTPUT(surface->output), TLP_END);
2283
Jason Ekstrandef540082014-06-26 10:37:36 -07002284 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002285}
2286
2287static void
2288view_accumulate_damage(struct weston_view *view,
2289 pixman_region32_t *opaque)
2290{
2291 pixman_region32_t damage;
2292
2293 pixman_region32_init(&damage);
2294 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002295 pixman_box32_t *extents;
2296
Jason Ekstranda7af7042013-10-12 22:38:11 -05002297 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002298 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002299 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002300 pixman_region32_copy(&damage, &view->surface->damage);
2301 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002302 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002303 }
2304
Pekka Paalanen380adf52015-02-16 14:39:11 +02002305 pixman_region32_intersect(&damage, &damage,
2306 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002307 pixman_region32_subtract(&damage, &damage, opaque);
2308 pixman_region32_union(&view->plane->damage,
2309 &view->plane->damage, &damage);
2310 pixman_region32_fini(&damage);
2311 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002312 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002313}
2314
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002315static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002316compositor_accumulate_damage(struct weston_compositor *ec)
2317{
2318 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002319 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002320 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002321
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002322 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002323
2324 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002325 pixman_region32_copy(&plane->clip, &clip);
2326
2327 pixman_region32_init(&opaque);
2328
Jason Ekstranda7af7042013-10-12 22:38:11 -05002329 wl_list_for_each(ev, &ec->view_list, link) {
2330 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002331 continue;
2332
Jason Ekstranda7af7042013-10-12 22:38:11 -05002333 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002334 }
2335
2336 pixman_region32_union(&clip, &clip, &opaque);
2337 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002338 }
2339
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002340 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002341
Jason Ekstranda7af7042013-10-12 22:38:11 -05002342 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002343 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002344
2345 wl_list_for_each(ev, &ec->view_list, link) {
2346 if (ev->surface->touched)
2347 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002348 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002349
2350 surface_flush_damage(ev->surface);
2351
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002352 /* Both the renderer and the backend have seen the buffer
2353 * by now. If renderer needs the buffer, it has its own
2354 * reference set. If the backend wants to keep the buffer
2355 * around for migrating the surface into a non-primary plane
2356 * later, keep_buffer is true. Otherwise, drop the core
2357 * reference now, and allow early buffer release. This enables
2358 * clients to use single-buffering.
2359 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002360 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002361 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002362 weston_buffer_release_reference(
2363 &ev->surface->buffer_release_ref, NULL);
2364 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002365 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002366}
2367
2368static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002369surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002370{
2371 struct weston_subsurface *sub;
2372
Pekka Paalanene67858b2013-04-25 13:57:42 +03002373 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002374 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002375 continue;
2376
Jason Ekstranda7af7042013-10-12 22:38:11 -05002377 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2378 wl_list_init(&sub->surface->views);
2379
2380 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002381 }
2382}
2383
2384static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002385surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002386{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002387 struct weston_subsurface *sub;
2388 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002389
Jason Ekstranda7af7042013-10-12 22:38:11 -05002390 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2391 if (sub->surface == surface)
2392 continue;
2393
George Kiagiadakised04d382014-06-13 18:10:26 +02002394 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2395 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002396 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002397 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002398
2399 surface_free_unused_subsurface_views(sub->surface);
2400 }
2401}
2402
2403static void
2404view_list_add_subsurface_view(struct weston_compositor *compositor,
2405 struct weston_subsurface *sub,
2406 struct weston_view *parent)
2407{
2408 struct weston_subsurface *child;
2409 struct weston_view *view = NULL, *iv;
2410
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002411 if (!weston_surface_is_mapped(sub->surface))
2412 return;
2413
Jason Ekstranda7af7042013-10-12 22:38:11 -05002414 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2415 if (iv->geometry.parent == parent) {
2416 view = iv;
2417 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002418 }
2419 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002420
2421 if (view) {
2422 /* Put it back in the surface's list of views */
2423 wl_list_remove(&view->surface_link);
2424 wl_list_insert(&sub->surface->views, &view->surface_link);
2425 } else {
2426 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002427 weston_view_set_position(view,
2428 sub->position.x,
2429 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002430 weston_view_set_transform_parent(view, parent);
2431 }
2432
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002433 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002434 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002435 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002436
Pekka Paalanenb188e912013-11-19 14:03:35 +02002437 if (wl_list_empty(&sub->surface->subsurface_list)) {
2438 wl_list_insert(compositor->view_list.prev, &view->link);
2439 return;
2440 }
2441
2442 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2443 if (child->surface == sub->surface)
2444 wl_list_insert(compositor->view_list.prev, &view->link);
2445 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002446 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002447 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002448}
2449
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002450/* This recursively adds the sub-surfaces for a view, relying on the
2451 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2452 * change first happens to the sub-surface list, and then automatically
2453 * propagates here. See weston_surface_damage_subsurfaces() for how the
2454 * sub-surfaces receive damage when the client changes the state.
2455 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002456static void
2457view_list_add(struct weston_compositor *compositor,
2458 struct weston_view *view)
2459{
2460 struct weston_subsurface *sub;
2461
2462 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002463
Pekka Paalanenb188e912013-11-19 14:03:35 +02002464 if (wl_list_empty(&view->surface->subsurface_list)) {
2465 wl_list_insert(compositor->view_list.prev, &view->link);
2466 return;
2467 }
2468
2469 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2470 if (sub->surface == view->surface)
2471 wl_list_insert(compositor->view_list.prev, &view->link);
2472 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002473 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002474 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002475}
2476
2477static void
2478weston_compositor_build_view_list(struct weston_compositor *compositor)
2479{
2480 struct weston_view *view;
2481 struct weston_layer *layer;
2482
2483 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002484 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002485 surface_stash_subsurface_views(view->surface);
2486
2487 wl_list_init(&compositor->view_list);
2488 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002489 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002490 view_list_add(compositor, view);
2491 }
2492 }
2493
2494 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002495 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002496 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002497}
2498
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002499static void
2500weston_output_take_feedback_list(struct weston_output *output,
2501 struct weston_surface *surface)
2502{
2503 struct weston_view *view;
2504 struct weston_presentation_feedback *feedback;
2505 uint32_t flags = 0xffffffff;
2506
2507 if (wl_list_empty(&surface->feedback_list))
2508 return;
2509
2510 /* All views must have the flag for the flag to survive. */
2511 wl_list_for_each(view, &surface->views, surface_link) {
2512 /* ignore views that are not on this output at all */
2513 if (view->output_mask & (1u << output->id))
2514 flags &= view->psf_flags;
2515 }
2516
2517 wl_list_for_each(feedback, &surface->feedback_list, link)
2518 feedback->psf_flags = flags;
2519
2520 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2521 wl_list_init(&surface->feedback_list);
2522}
2523
David Herrmann1edf44c2013-10-22 17:11:26 +02002524static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002525weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002526{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002527 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002528 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002529 struct weston_animation *animation, *next;
2530 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002531 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002532 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002533 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002534 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302535 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002536
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002537 if (output->destroying)
2538 return 0;
2539
Pekka Paalanenb5026542014-11-12 15:09:24 +02002540 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2541
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002542 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002543 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002544
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302545 /* Find the highest protection desired for an output */
2546 wl_list_for_each(ev, &ec->view_list, link) {
2547 if (ev->surface->output_mask & (1u << output->id)) {
2548 /*
2549 * The desired_protection of the output should be the
2550 * maximum of the desired_protection of the surfaces,
2551 * that are displayed on that output, to avoid
2552 * reducing the protection for existing surfaces.
2553 */
2554 if (ev->surface->desired_protection > highest_requested)
2555 highest_requested =
2556 ev->surface->desired_protection;
2557 }
2558 }
2559
2560 output->desired_protection = highest_requested;
2561
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002562 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002563 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002564 } else {
2565 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002566 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002567 ev->psf_flags = 0;
2568 }
2569 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002570
Pekka Paalanene67858b2013-04-25 13:57:42 +03002571 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002572 wl_list_for_each(ev, &ec->view_list, link) {
2573 /* Note: This operation is safe to do multiple times on the
2574 * same surface.
2575 */
2576 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002577 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002578 &ev->surface->frame_callback_list);
2579 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002580
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002581 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002582 }
2583 }
2584
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002585 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002586
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002587 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002588 pixman_region32_intersect(&output_damage,
2589 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002590 pixman_region32_subtract(&output_damage,
2591 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002592
Scott Moreauccbf29d2012-02-22 14:21:41 -07002593 if (output->dirty)
2594 weston_output_update_matrix(output);
2595
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002596 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002597
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002598 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002599
Daniel Stone09a97e22017-03-01 11:34:06 +00002600 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002601 if (r == 0)
2602 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002603
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002604 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002605
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002606 frame_time_msec = timespec_to_msec(&output->frame_time);
2607
Jonas Ådahldb773762012-06-13 00:01:21 +02002608 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002609 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002610 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002611 }
2612
Scott Moreaud64cf212012-06-08 19:40:54 -06002613 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002614 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002615 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002616 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002617
Pekka Paalanenb5026542014-11-12 15:09:24 +02002618 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2619
David Herrmann1edf44c2013-10-22 17:11:26 +02002620 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002621}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002622
Pekka Paalanen82919792014-05-21 13:51:49 +03002623static void
2624weston_output_schedule_repaint_reset(struct weston_output *output)
2625{
Daniel Stone05df8c12017-03-03 16:59:42 +00002626 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002627 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002628}
2629
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002630static int
2631weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2632 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002633{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002634 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002635 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002636 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002637
Daniel Stone6847b852017-03-01 11:34:08 +00002638 /* We're not ready yet; come back to make a decision later. */
2639 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002640 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002641
2642 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2643 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002644 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002645
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002646 /* If we're sleeping, drop the repaint machinery entirely; we will
2647 * explicitly repaint all outputs when we come back. */
2648 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2649 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2650 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002651
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002652 /* We don't actually need to repaint this output; drop it from
2653 * repaint until something causes damage. */
2654 if (!output->repaint_needed)
2655 goto err;
2656
2657 /* If repaint fails, we aren't going to get weston_output_finish_frame
2658 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002659 * something schedules a successful repaint later. As repainting may
2660 * take some time, re-read our clock as a courtesy to the next
2661 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002662 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002663 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002664 if (ret != 0)
2665 goto err;
2666
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002667 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002668 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002669
2670err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002671 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002672 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002673}
2674
2675static void
2676output_repaint_timer_arm(struct weston_compositor *compositor)
2677{
2678 struct weston_output *output;
2679 bool any_should_repaint = false;
2680 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002681 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002682
2683 weston_compositor_read_presentation_clock(compositor, &now);
2684
2685 wl_list_for_each(output, &compositor->output_list, link) {
2686 int64_t msec_to_this;
2687
2688 if (output->repaint_status != REPAINT_SCHEDULED)
2689 continue;
2690
2691 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2692 &now);
2693 if (!any_should_repaint || msec_to_this < msec_to_next)
2694 msec_to_next = msec_to_this;
2695
2696 any_should_repaint = true;
2697 }
2698
2699 if (!any_should_repaint)
2700 return;
2701
2702 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2703 * This is a workaround to allow coalescing multiple output repaints
2704 * particularly from weston_output_finish_frame()
2705 * into the same call, which would not happen if we called
2706 * output_repaint_timer_handler() directly.
2707 */
2708 if (msec_to_next < 1)
2709 msec_to_next = 1;
2710
2711 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2712}
2713
2714static int
2715output_repaint_timer_handler(void *data)
2716{
2717 struct weston_compositor *compositor = data;
2718 struct weston_output *output;
2719 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002720 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002721 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002722
2723 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002724
2725 if (compositor->backend->repaint_begin)
2726 repaint_data = compositor->backend->repaint_begin(compositor);
2727
2728 wl_list_for_each(output, &compositor->output_list, link) {
2729 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2730 if (ret)
2731 break;
2732 }
2733
2734 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002735 if (compositor->backend->repaint_flush)
2736 compositor->backend->repaint_flush(compositor,
2737 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002738 } else {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002739 wl_list_for_each(output, &compositor->output_list, link) {
2740 if (output->repainted)
2741 weston_output_schedule_repaint_reset(output);
2742 }
2743
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002744 if (compositor->backend->repaint_cancel)
2745 compositor->backend->repaint_cancel(compositor,
2746 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002747 }
Daniel Stone6847b852017-03-01 11:34:08 +00002748
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002749 wl_list_for_each(output, &compositor->output_list, link)
2750 output->repainted = false;
2751
Daniel Stone6847b852017-03-01 11:34:08 +00002752 output_repaint_timer_arm(compositor);
2753
Pekka Paalanen0513a952014-05-21 16:17:27 +03002754 return 0;
2755}
2756
Marius Vlad55d87362019-06-11 01:15:35 +03002757/**
2758 * \ingroup output
2759 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002760WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002761weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002762 const struct timespec *stamp,
2763 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002764{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002765 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002766 int32_t refresh_nsec;
2767 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002768 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002769
Pekka Paalanenb5026542014-11-12 15:09:24 +02002770
Daniel Stone05df8c12017-03-03 16:59:42 +00002771 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002772 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2773
Daniel Stone6847b852017-03-01 11:34:08 +00002774 weston_compositor_read_presentation_clock(compositor, &now);
2775
Daniel Stone3615ce12017-03-01 11:34:05 +00002776 /* If we haven't been supplied any timestamp at all, we don't have a
2777 * timebase to work against, so any delay just wastes time. Push a
2778 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002779 if (!stamp) {
2780 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002781 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002782 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002783
Marius Vladdf9278a2018-03-06 18:56:23 +02002784 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2785 TLP_VBLANK(stamp), TLP_END);
2786
Pekka Paalanend7894d02015-07-03 15:08:53 +03002787 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002788 weston_presentation_feedback_present_list(&output->feedback_list,
2789 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002790 output->msc,
2791 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002792
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002793 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002794
Daniel Stone6847b852017-03-01 11:34:08 +00002795 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2796 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2797 -compositor->repaint_msec);
2798 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002799
2800 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002801 static bool warned;
2802
2803 if (!warned)
2804 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002805 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002806 warned = true;
2807
Daniel Stone6847b852017-03-01 11:34:08 +00002808 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002809 }
2810
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002811 /* Called from restart_repaint_loop and restart happens already after
2812 * the deadline given by repaint_msec? In that case we delay until
2813 * the deadline of the next frame, to give clients a more predictable
2814 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002815 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2816 msec_rel < 0) {
2817 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2818 timespec_add_nsec(&output->next_repaint,
2819 &output->next_repaint,
2820 refresh_nsec);
2821 }
2822 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002823
Daniel Stone3615ce12017-03-01 11:34:05 +00002824out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002825 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002826 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002827}
2828
2829static void
2830idle_repaint(void *data)
2831{
2832 struct weston_output *output = data;
2833
Daniel Stone05df8c12017-03-03 16:59:42 +00002834 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2835 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002836 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002837 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002838}
2839
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002840WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002841weston_layer_entry_insert(struct weston_layer_entry *list,
2842 struct weston_layer_entry *entry)
2843{
2844 wl_list_insert(&list->link, &entry->link);
2845 entry->layer = list->layer;
2846}
2847
2848WL_EXPORT void
2849weston_layer_entry_remove(struct weston_layer_entry *entry)
2850{
2851 wl_list_remove(&entry->link);
2852 wl_list_init(&entry->link);
2853 entry->layer = NULL;
2854}
2855
Quentin Glidic82681572016-12-17 13:40:51 +01002856
2857/** Initialize the weston_layer struct.
2858 *
2859 * \param compositor The compositor instance
2860 * \param layer The layer to initialize
2861 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002862WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002863weston_layer_init(struct weston_layer *layer,
2864 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002865{
Quentin Glidic82681572016-12-17 13:40:51 +01002866 layer->compositor = compositor;
2867 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002868 wl_list_init(&layer->view_list.link);
2869 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002870 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002871}
2872
2873/** Sets the position of the layer in the layer list. The layer will be placed
2874 * below any layer with the same position value, if any.
2875 * This function is safe to call if the layer is already on the list, but the
2876 * layer may be moved below other layers at the same position, if any.
2877 *
2878 * \param layer The layer to modify
2879 * \param position The position the layer will be placed at
2880 */
2881WL_EXPORT void
2882weston_layer_set_position(struct weston_layer *layer,
2883 enum weston_layer_position position)
2884{
2885 struct weston_layer *below;
2886
2887 wl_list_remove(&layer->link);
2888
2889 /* layer_list is ordered from top to bottom, the last layer being the
2890 * background with the smallest position value */
2891
2892 layer->position = position;
2893 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2894 if (below->position >= layer->position) {
2895 wl_list_insert(&below->link, &layer->link);
2896 return;
2897 }
2898 }
2899 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2900}
2901
2902/** Hide a layer by taking it off the layer list.
2903 * This function is safe to call if the layer is not on the list.
2904 *
2905 * \param layer The layer to hide
2906 */
2907WL_EXPORT void
2908weston_layer_unset_position(struct weston_layer *layer)
2909{
2910 wl_list_remove(&layer->link);
2911 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002912}
2913
2914WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002915weston_layer_set_mask(struct weston_layer *layer,
2916 int x, int y, int width, int height)
2917{
2918 struct weston_view *view;
2919
2920 layer->mask.x1 = x;
2921 layer->mask.x2 = x + width;
2922 layer->mask.y1 = y;
2923 layer->mask.y2 = y + height;
2924
2925 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2926 weston_view_geometry_dirty(view);
2927 }
2928}
2929
2930WL_EXPORT void
2931weston_layer_set_mask_infinite(struct weston_layer *layer)
2932{
2933 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2934 UINT32_MAX, UINT32_MAX);
2935}
2936
Daniel Stone3b775632018-07-20 08:38:25 +01002937WL_EXPORT bool
2938weston_layer_mask_is_infinite(struct weston_layer *layer)
2939{
2940 return layer->mask.x1 == INT32_MIN &&
2941 layer->mask.y1 == INT32_MIN &&
2942 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
2943 layer->mask.y2 == INT32_MIN + UINT32_MAX;
2944}
2945
Marius Vlad55d87362019-06-11 01:15:35 +03002946/**
2947 * \ingroup output
2948 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002949WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002950weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002951{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002952 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002953 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002954
Bryce Harrington08976ac2016-08-30 12:05:16 -07002955 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2956 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002957 return;
2958
Pekka Paalanenb5026542014-11-12 15:09:24 +02002959 if (!output->repaint_needed)
2960 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2961
Kristian Høgsbergef044142011-06-21 15:02:12 -04002962 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002963 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002964
2965 /* If we already have a repaint scheduled for our idle handler,
2966 * no need to set it again. If the repaint has been called but
2967 * not finished, then weston_output_finish_frame() will notice
2968 * that a repaint is needed and schedule one. */
2969 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002970 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002971
Daniel Stone05df8c12017-03-03 16:59:42 +00002972 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002973 assert(!output->idle_repaint_source);
2974 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2975 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002976 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002977}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002978
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002979/** weston_compositor_schedule_repaint
2980 * \ingroup compositor
2981 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002982WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002983weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2984{
2985 struct weston_output *output;
2986
2987 wl_list_for_each(output, &compositor->output_list, link)
2988 weston_output_schedule_repaint(output);
2989}
2990
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002991static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002992surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002993{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002994 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002995}
2996
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002997static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002998surface_attach(struct wl_client *client,
2999 struct wl_resource *resource,
3000 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3001{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003002 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003003 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003004
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003005 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003006 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003007 if (buffer == NULL) {
3008 wl_client_post_no_memory(client);
3009 return;
3010 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003011 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003012
Pekka Paalanende685b82012-12-04 15:58:12 +02003013 /* Attach, attach, without commit in between does not send
3014 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003015 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003016
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003017 surface->pending.sx = sx;
3018 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003019 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003020}
3021
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003022static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003023surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003024 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003025 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003026{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003027 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003028
Derek Foreman57e92ed2015-11-17 14:11:35 -06003029 if (width <= 0 || height <= 0)
3030 return;
3031
Derek Foreman152254b2015-11-26 14:17:48 -06003032 pixman_region32_union_rect(&surface->pending.damage_surface,
3033 &surface->pending.damage_surface,
3034 x, y, width, height);
3035}
3036
3037static void
3038surface_damage_buffer(struct wl_client *client,
3039 struct wl_resource *resource,
3040 int32_t x, int32_t y, int32_t width, int32_t height)
3041{
3042 struct weston_surface *surface = wl_resource_get_user_data(resource);
3043
3044 if (width <= 0 || height <= 0)
3045 return;
3046
3047 pixman_region32_union_rect(&surface->pending.damage_buffer,
3048 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003049 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003050}
3051
Kristian Høgsberg33418202011-08-16 23:01:28 -04003052static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003053destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003054{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003055 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003056
3057 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003058 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003059}
3060
3061static void
3062surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003063 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003064{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003065 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003066 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003067
3068 cb = malloc(sizeof *cb);
3069 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003070 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003071 return;
3072 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003073
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003074 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3075 callback);
3076 if (cb->resource == NULL) {
3077 free(cb);
3078 wl_resource_post_no_memory(resource);
3079 return;
3080 }
3081
Jason Ekstranda85118c2013-06-27 20:17:02 -05003082 wl_resource_set_implementation(cb->resource, NULL, cb,
3083 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003084
Pekka Paalanenbc106382012-10-10 12:49:31 +03003085 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003086}
3087
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003088static void
3089surface_set_opaque_region(struct wl_client *client,
3090 struct wl_resource *resource,
3091 struct wl_resource *region_resource)
3092{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003093 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003094 struct weston_region *region;
3095
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003096 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003097 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003098 pixman_region32_copy(&surface->pending.opaque,
3099 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003100 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003101 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003102 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003103}
3104
3105static void
3106surface_set_input_region(struct wl_client *client,
3107 struct wl_resource *resource,
3108 struct wl_resource *region_resource)
3109{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003110 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003111 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003112
3113 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003114 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003115 pixman_region32_copy(&surface->pending.input,
3116 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003117 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003118 pixman_region32_fini(&surface->pending.input);
3119 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003120 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003121}
3122
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003123/* Cause damage to this sub-surface and all its children.
3124 *
3125 * This is useful when there are state changes that need an implicit
3126 * damage, e.g. a z-order change.
3127 */
3128static void
3129weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3130{
3131 struct weston_subsurface *child;
3132
3133 weston_surface_damage(sub->surface);
3134 sub->reordered = false;
3135
3136 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3137 if (child != sub)
3138 weston_surface_damage_subsurfaces(child);
3139}
3140
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003141static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003142weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003143{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003144 struct weston_subsurface *sub;
3145
3146 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3147 parent_link_pending) {
3148 wl_list_remove(&sub->parent_link);
3149 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003150
3151 if (sub->reordered)
3152 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003153 }
3154}
3155
3156static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003157weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003158 struct weston_matrix *matrix)
3159{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003160 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003161 double src_width, src_height, dest_width, dest_height;
3162
3163 weston_matrix_init(matrix);
3164
3165 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3166 src_width = surface->width_from_buffer;
3167 src_height = surface->height_from_buffer;
3168 } else {
3169 src_width = wl_fixed_to_double(vp->buffer.src_width);
3170 src_height = wl_fixed_to_double(vp->buffer.src_height);
3171 }
3172
3173 if (vp->surface.width == -1) {
3174 dest_width = src_width;
3175 dest_height = src_height;
3176 } else {
3177 dest_width = vp->surface.width;
3178 dest_height = vp->surface.height;
3179 }
3180
3181 if (src_width != dest_width || src_height != dest_height)
3182 weston_matrix_scale(matrix,
3183 src_width / dest_width,
3184 src_height / dest_height, 1);
3185
3186 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3187 weston_matrix_translate(matrix,
3188 wl_fixed_to_double(vp->buffer.src_x),
3189 wl_fixed_to_double(vp->buffer.src_y),
3190 0);
3191
3192 switch (vp->buffer.transform) {
3193 case WL_OUTPUT_TRANSFORM_FLIPPED:
3194 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3195 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3196 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3197 weston_matrix_scale(matrix, -1, 1, 1);
3198 weston_matrix_translate(matrix,
3199 surface->width_from_buffer, 0, 0);
3200 break;
3201 }
3202
3203 switch (vp->buffer.transform) {
3204 default:
3205 case WL_OUTPUT_TRANSFORM_NORMAL:
3206 case WL_OUTPUT_TRANSFORM_FLIPPED:
3207 break;
3208 case WL_OUTPUT_TRANSFORM_90:
3209 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3210 weston_matrix_rotate_xy(matrix, 0, 1);
3211 weston_matrix_translate(matrix,
3212 surface->height_from_buffer, 0, 0);
3213 break;
3214 case WL_OUTPUT_TRANSFORM_180:
3215 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3216 weston_matrix_rotate_xy(matrix, -1, 0);
3217 weston_matrix_translate(matrix,
3218 surface->width_from_buffer,
3219 surface->height_from_buffer, 0);
3220 break;
3221 case WL_OUTPUT_TRANSFORM_270:
3222 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3223 weston_matrix_rotate_xy(matrix, 0, -1);
3224 weston_matrix_translate(matrix,
3225 0, surface->width_from_buffer, 0);
3226 break;
3227 }
3228
3229 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3230}
3231
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003232/**
3233 * Compute a + b > c while being safe to overflows.
3234 */
3235static bool
3236fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3237{
3238 return (int64_t)a + (int64_t)b > (int64_t)c;
3239}
3240
3241static bool
3242weston_surface_is_pending_viewport_source_valid(
3243 const struct weston_surface *surface)
3244{
3245 const struct weston_surface_state *pend = &surface->pending;
3246 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3247 int width_from_buffer = 0;
3248 int height_from_buffer = 0;
3249 wl_fixed_t w;
3250 wl_fixed_t h;
3251
3252 /* If viewport source rect is not set, it is always ok. */
3253 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3254 return true;
3255
3256 if (pend->newly_attached) {
3257 if (pend->buffer) {
3258 convert_size_by_transform_scale(&width_from_buffer,
3259 &height_from_buffer,
3260 pend->buffer->width,
3261 pend->buffer->height,
3262 vp->buffer.transform,
3263 vp->buffer.scale);
3264 } else {
3265 /* No buffer: viewport is irrelevant. */
3266 return true;
3267 }
3268 } else {
3269 width_from_buffer = surface->width_from_buffer;
3270 height_from_buffer = surface->height_from_buffer;
3271 }
3272
3273 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3274 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3275
3276 /* No buffer: viewport is irrelevant. */
3277 if (width_from_buffer == 0 || height_from_buffer == 0)
3278 return true;
3279
3280 /* overflow checks for wl_fixed_from_int() */
3281 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3282 return false;
3283 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3284 return false;
3285
3286 w = wl_fixed_from_int(width_from_buffer);
3287 h = wl_fixed_from_int(height_from_buffer);
3288
3289 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3290 return false;
3291 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3292 return false;
3293
3294 return true;
3295}
3296
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003297static bool
3298fixed_is_integer(wl_fixed_t v)
3299{
3300 return (v & 0xff) == 0;
3301}
3302
3303static bool
3304weston_surface_is_pending_viewport_dst_size_int(
3305 const struct weston_surface *surface)
3306{
3307 const struct weston_buffer_viewport *vp =
3308 &surface->pending.buffer_viewport;
3309
3310 if (vp->surface.width != -1) {
3311 assert(vp->surface.width > 0 && vp->surface.height > 0);
3312 return true;
3313 }
3314
3315 return fixed_is_integer(vp->buffer.src_width) &&
3316 fixed_is_integer(vp->buffer.src_height);
3317}
3318
Derek Foreman152254b2015-11-26 14:17:48 -06003319/* Translate pending damage in buffer co-ordinates to surface
3320 * co-ordinates and union it with a pixman_region32_t.
3321 * This should only be called after the buffer is attached.
3322 */
3323static void
3324apply_damage_buffer(pixman_region32_t *dest,
3325 struct weston_surface *surface,
3326 struct weston_surface_state *state)
3327{
3328 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3329
3330 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3331 * translated into surface co-ordinates and unioned with
3332 * any other surface damage.
3333 * None of this makes sense if there is no buffer though.
3334 */
3335 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3336 pixman_region32_t buffer_damage;
3337
3338 pixman_region32_intersect_rect(&state->damage_buffer,
3339 &state->damage_buffer,
3340 0, 0, buffer->width,
3341 buffer->height);
3342 pixman_region32_init(&buffer_damage);
3343 weston_matrix_transform_region(&buffer_damage,
3344 &surface->buffer_to_surface_matrix,
3345 &state->damage_buffer);
3346 pixman_region32_union(dest, dest, &buffer_damage);
3347 pixman_region32_fini(&buffer_damage);
3348 }
3349 /* We should clear this on commit even if there was no buffer */
3350 pixman_region32_clear(&state->damage_buffer);
3351}
3352
Jason Ekstrand1e059042014-10-16 10:55:19 -05003353static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303354weston_surface_set_desired_protection(struct weston_surface *surface,
3355 enum weston_hdcp_protection protection)
3356{
3357 if (surface->desired_protection == protection)
3358 return;
3359 surface->desired_protection = protection;
3360 weston_surface_damage(surface);
3361}
3362
3363static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003364weston_surface_commit_state(struct weston_surface *surface,
3365 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003366{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003367 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003368 pixman_region32_t opaque;
3369
Alexander Larsson4ea95522013-05-22 14:41:37 +02003370 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003371 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003372 /* wp_viewport.set_source */
3373 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003374 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003375
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003376 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003377 if (state->newly_attached) {
3378 /* zwp_surface_synchronization_v1.set_acquire_fence */
3379 fd_move(&surface->acquire_fence_fd,
3380 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003381 /* zwp_surface_synchronization_v1.get_release */
3382 weston_buffer_release_move(&surface->buffer_release_ref,
3383 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003384 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003385 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003386 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003387 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003388 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003389
Jason Ekstrand1e059042014-10-16 10:55:19 -05003390 weston_surface_build_buffer_matrix(surface,
3391 &surface->surface_to_buffer_matrix);
3392 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3393 &surface->surface_to_buffer_matrix);
3394
Jason Ekstrand7b982072014-05-20 14:33:03 -05003395 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003396 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003397 if (surface->committed)
3398 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003399 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003400
Jason Ekstrand7b982072014-05-20 14:33:03 -05003401 state->sx = 0;
3402 state->sy = 0;
3403 state->newly_attached = 0;
3404 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003405
Derek Foreman152254b2015-11-26 14:17:48 -06003406 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003407 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003408 (pixman_region32_not_empty(&state->damage_surface) ||
3409 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003410 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003411
Pekka Paalanen8e159182012-10-10 12:49:25 +03003412 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003413 &state->damage_surface);
3414
3415 apply_damage_buffer(&surface->damage, surface, state);
3416
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003417 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003418 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003419 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003420
3421 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003422 pixman_region32_init(&opaque);
3423 pixman_region32_intersect_rect(&opaque, &state->opaque,
3424 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003425
3426 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3427 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003428 wl_list_for_each(view, &surface->views, surface_link)
3429 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003430 }
3431
3432 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003433
3434 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003435 pixman_region32_intersect_rect(&surface->input, &state->input,
3436 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003437
Pekka Paalanenbc106382012-10-10 12:49:31 +03003438 /* wl_surface.frame */
3439 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003440 &state->frame_callback_list);
3441 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003442
3443 /* XXX:
3444 * What should happen with a feedback request, if there
3445 * is no wl_buffer attached for this commit?
3446 */
3447
3448 /* presentation.feedback */
3449 wl_list_insert_list(&surface->feedback_list,
3450 &state->feedback_list);
3451 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003452
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303453 /* weston_protected_surface.set_type */
3454 weston_surface_set_desired_protection(surface, state->desired_protection);
3455
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003456 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003457}
3458
3459static void
3460weston_surface_commit(struct weston_surface *surface)
3461{
3462 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003463
Pekka Paalanene67858b2013-04-25 13:57:42 +03003464 weston_surface_commit_subsurface_order(surface);
3465
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003466 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003467}
3468
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003469static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003470weston_subsurface_commit(struct weston_subsurface *sub);
3471
3472static void
3473weston_subsurface_parent_commit(struct weston_subsurface *sub,
3474 int parent_is_synchronized);
3475
3476static void
3477surface_commit(struct wl_client *client, struct wl_resource *resource)
3478{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003479 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003480 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3481
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003482 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3483 assert(surface->viewport_resource);
3484
3485 wl_resource_post_error(surface->viewport_resource,
3486 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3487 "wl_surface@%d has viewport source outside buffer",
3488 wl_resource_get_id(resource));
3489 return;
3490 }
3491
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003492 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3493 assert(surface->viewport_resource);
3494
3495 wl_resource_post_error(surface->viewport_resource,
3496 WP_VIEWPORT_ERROR_BAD_SIZE,
3497 "wl_surface@%d viewport dst size not integer",
3498 wl_resource_get_id(resource));
3499 return;
3500 }
3501
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003502 if (surface->pending.acquire_fence_fd >= 0) {
3503 assert(surface->synchronization_resource);
3504
3505 if (!surface->pending.buffer) {
3506 fd_clear(&surface->pending.acquire_fence_fd);
3507 wl_resource_post_error(surface->synchronization_resource,
3508 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3509 "wl_surface@%"PRIu32" no buffer for synchronization",
3510 wl_resource_get_id(resource));
3511 return;
3512 }
3513
3514 /* We support fences for both wp_linux_dmabuf and opaque EGL
3515 * buffers, as mandated by minor version 2 of the
3516 * zwp_linux_explicit_synchronization_v1 protocol. Since
3517 * renderers that support fences currently only support these
3518 * two buffer types plus SHM buffers, we can just check for the
3519 * SHM buffer case here.
3520 */
3521 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3522 fd_clear(&surface->pending.acquire_fence_fd);
3523 wl_resource_post_error(surface->synchronization_resource,
3524 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3525 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3526 wl_resource_get_id(resource));
3527 return;
3528 }
3529 }
3530
Alexandros Frantzis67629672018-10-19 12:14:11 +03003531 if (surface->pending.buffer_release_ref.buffer_release &&
3532 !surface->pending.buffer) {
3533 assert(surface->synchronization_resource);
3534
3535 wl_resource_post_error(surface->synchronization_resource,
3536 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3537 "wl_surface@%"PRIu32" no buffer for synchronization",
3538 wl_resource_get_id(resource));
3539 return;
3540 }
3541
Pekka Paalanene67858b2013-04-25 13:57:42 +03003542 if (sub) {
3543 weston_subsurface_commit(sub);
3544 return;
3545 }
3546
3547 weston_surface_commit(surface);
3548
3549 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3550 if (sub->surface != surface)
3551 weston_subsurface_parent_commit(sub, 0);
3552 }
3553}
3554
3555static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003556surface_set_buffer_transform(struct wl_client *client,
3557 struct wl_resource *resource, int transform)
3558{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003559 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003560
Jonny Lamba55f1392014-05-30 12:07:15 +02003561 /* if wl_output.transform grows more members this will need to be updated. */
3562 if (transform < 0 ||
3563 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3564 wl_resource_post_error(resource,
3565 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3566 "buffer transform must be a valid transform "
3567 "('%d' specified)", transform);
3568 return;
3569 }
3570
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003571 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003572 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003573}
3574
Alexander Larsson4ea95522013-05-22 14:41:37 +02003575static void
3576surface_set_buffer_scale(struct wl_client *client,
3577 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003578 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003579{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003580 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003581
Jonny Lamba55f1392014-05-30 12:07:15 +02003582 if (scale < 1) {
3583 wl_resource_post_error(resource,
3584 WL_SURFACE_ERROR_INVALID_SCALE,
3585 "buffer scale must be at least one "
3586 "('%d' specified)", scale);
3587 return;
3588 }
3589
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003590 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003591 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003592}
3593
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003594static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003595 surface_destroy,
3596 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003597 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003598 surface_frame,
3599 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003600 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003601 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003602 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003603 surface_set_buffer_scale,
3604 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003605};
3606
3607static void
3608compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003609 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003610{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003611 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003612 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003613
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003614 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003615 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003616 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003617 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003618 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003619
Jason Ekstranda85118c2013-06-27 20:17:02 -05003620 surface->resource =
3621 wl_resource_create(client, &wl_surface_interface,
3622 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003623 if (surface->resource == NULL) {
3624 weston_surface_destroy(surface);
3625 wl_resource_post_no_memory(resource);
3626 return;
3627 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003628 wl_resource_set_implementation(surface->resource, &surface_interface,
3629 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003630
3631 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003632}
3633
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003634static void
3635destroy_region(struct wl_resource *resource)
3636{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003637 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003638
3639 pixman_region32_fini(&region->region);
3640 free(region);
3641}
3642
3643static void
3644region_destroy(struct wl_client *client, struct wl_resource *resource)
3645{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003646 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003647}
3648
3649static void
3650region_add(struct wl_client *client, struct wl_resource *resource,
3651 int32_t x, int32_t y, int32_t width, int32_t height)
3652{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003653 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003654
3655 pixman_region32_union_rect(&region->region, &region->region,
3656 x, y, width, height);
3657}
3658
3659static void
3660region_subtract(struct wl_client *client, struct wl_resource *resource,
3661 int32_t x, int32_t y, int32_t width, int32_t height)
3662{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003663 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003664 pixman_region32_t rect;
3665
3666 pixman_region32_init_rect(&rect, x, y, width, height);
3667 pixman_region32_subtract(&region->region, &region->region, &rect);
3668 pixman_region32_fini(&rect);
3669}
3670
3671static const struct wl_region_interface region_interface = {
3672 region_destroy,
3673 region_add,
3674 region_subtract
3675};
3676
3677static void
3678compositor_create_region(struct wl_client *client,
3679 struct wl_resource *resource, uint32_t id)
3680{
3681 struct weston_region *region;
3682
3683 region = malloc(sizeof *region);
3684 if (region == NULL) {
3685 wl_resource_post_no_memory(resource);
3686 return;
3687 }
3688
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003689 pixman_region32_init(&region->region);
3690
Jason Ekstranda85118c2013-06-27 20:17:02 -05003691 region->resource =
3692 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003693 if (region->resource == NULL) {
3694 free(region);
3695 wl_resource_post_no_memory(resource);
3696 return;
3697 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003698 wl_resource_set_implementation(region->resource, &region_interface,
3699 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003700}
3701
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003702static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003703 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003704 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003705};
3706
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003707static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003708weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3709{
3710 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003711
Jason Ekstrand7b982072014-05-20 14:33:03 -05003712 weston_surface_commit_state(surface, &sub->cached);
3713 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003714
3715 weston_surface_commit_subsurface_order(surface);
3716
3717 weston_surface_schedule_repaint(surface);
3718
Jason Ekstrand7b982072014-05-20 14:33:03 -05003719 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003720}
3721
3722static void
3723weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3724{
3725 struct weston_surface *surface = sub->surface;
3726
3727 /*
3728 * If this commit would cause the surface to move by the
3729 * attach(dx, dy) parameters, the old damage region must be
3730 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003731 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003732 */
Derek Foreman152254b2015-11-26 14:17:48 -06003733 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003734 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003735 pixman_region32_union(&sub->cached.damage_surface,
3736 &sub->cached.damage_surface,
3737 &surface->pending.damage_surface);
3738 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003739
3740 if (surface->pending.newly_attached) {
3741 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003742 weston_surface_state_set_buffer(&sub->cached,
3743 surface->pending.buffer);
3744 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003745 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003746 weston_presentation_feedback_discard_list(
3747 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003748 /* zwp_surface_synchronization_v1.set_acquire_fence */
3749 fd_move(&sub->cached.acquire_fence_fd,
3750 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003751 /* zwp_surface_synchronization_v1.get_release */
3752 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3753 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003754 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303755 sub->cached.desired_protection = surface->pending.desired_protection;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003756 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003757 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003758 sub->cached.sx += surface->pending.sx;
3759 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003760
Derek Foreman152254b2015-11-26 14:17:48 -06003761 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3762
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003763 sub->cached.buffer_viewport.changed |=
3764 surface->pending.buffer_viewport.changed;
3765 sub->cached.buffer_viewport.buffer =
3766 surface->pending.buffer_viewport.buffer;
3767 sub->cached.buffer_viewport.surface =
3768 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003769
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003770 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003771
3772 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3773
3774 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3775
3776 wl_list_insert_list(&sub->cached.frame_callback_list,
3777 &surface->pending.frame_callback_list);
3778 wl_list_init(&surface->pending.frame_callback_list);
3779
Pekka Paalanen133e4392014-09-23 22:08:46 -04003780 wl_list_insert_list(&sub->cached.feedback_list,
3781 &surface->pending.feedback_list);
3782 wl_list_init(&surface->pending.feedback_list);
3783
Jason Ekstrand7b982072014-05-20 14:33:03 -05003784 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003785}
3786
Derek Foreman280e7dd2014-10-03 13:13:42 -05003787static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003788weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3789{
3790 while (sub) {
3791 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003792 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003793
3794 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003795 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003796
3797 sub = weston_surface_to_subsurface(sub->parent);
3798 }
3799
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003800 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003801}
3802
3803static void
3804weston_subsurface_commit(struct weston_subsurface *sub)
3805{
3806 struct weston_surface *surface = sub->surface;
3807 struct weston_subsurface *tmp;
3808
3809 /* Recursive check for effectively synchronized. */
3810 if (weston_subsurface_is_synchronized(sub)) {
3811 weston_subsurface_commit_to_cache(sub);
3812 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003813 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003814 /* flush accumulated state from cache */
3815 weston_subsurface_commit_to_cache(sub);
3816 weston_subsurface_commit_from_cache(sub);
3817 } else {
3818 weston_surface_commit(surface);
3819 }
3820
3821 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3822 if (tmp->surface != surface)
3823 weston_subsurface_parent_commit(tmp, 0);
3824 }
3825 }
3826}
3827
3828static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003829weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003830{
3831 struct weston_surface *surface = sub->surface;
3832 struct weston_subsurface *tmp;
3833
Pekka Paalanene67858b2013-04-25 13:57:42 +03003834 /* From now on, commit_from_cache the whole sub-tree, regardless of
3835 * the synchronized mode of each child. This sub-surface or some
3836 * of its ancestors were synchronized, so we are synchronized
3837 * all the way down.
3838 */
3839
Jason Ekstrand7b982072014-05-20 14:33:03 -05003840 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003841 weston_subsurface_commit_from_cache(sub);
3842
3843 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3844 if (tmp->surface != surface)
3845 weston_subsurface_parent_commit(tmp, 1);
3846 }
3847}
3848
3849static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003850weston_subsurface_parent_commit(struct weston_subsurface *sub,
3851 int parent_is_synchronized)
3852{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003853 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003854 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003855 wl_list_for_each(view, &sub->surface->views, surface_link)
3856 weston_view_set_position(view,
3857 sub->position.x,
3858 sub->position.y);
3859
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003860 sub->position.set = 0;
3861 }
3862
3863 if (parent_is_synchronized || sub->synchronized)
3864 weston_subsurface_synchronized_commit(sub);
3865}
3866
Pekka Paalanen8274d902014-08-06 19:36:51 +03003867static int
3868subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3869{
3870 return snprintf(buf, len, "sub-surface");
3871}
3872
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003873static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003874subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003875{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003876 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003877
Jason Ekstranda7af7042013-10-12 22:38:11 -05003878 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003879 weston_view_set_position(view,
3880 view->geometry.x + dx,
3881 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003882
3883 /* No need to check parent mappedness, because if parent is not
3884 * mapped, parent is not in a visible layer, so this sub-surface
3885 * will not be drawn either.
3886 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003887
Pekka Paalanene67858b2013-04-25 13:57:42 +03003888 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003889 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003890
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003891 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003892 * because that would call it also for the parent surface,
3893 * which might not be mapped yet. That would lead to
3894 * inconsistent state, where the window could never be
3895 * mapped.
3896 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003897 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003898 * weston_surface_is_mapped() return true, so that when the
3899 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003900 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003901 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003902 }
3903}
3904
3905static struct weston_subsurface *
3906weston_surface_to_subsurface(struct weston_surface *surface)
3907{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003908 if (surface->committed == subsurface_committed)
3909 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003910
3911 return NULL;
3912}
3913
Pekka Paalanen01388e22013-04-25 13:57:44 +03003914WL_EXPORT struct weston_surface *
3915weston_surface_get_main_surface(struct weston_surface *surface)
3916{
3917 struct weston_subsurface *sub;
3918
3919 while (surface && (sub = weston_surface_to_subsurface(surface)))
3920 surface = sub->parent;
3921
3922 return surface;
3923}
3924
Pekka Paalanen50b67472014-10-01 15:02:41 +03003925WL_EXPORT int
3926weston_surface_set_role(struct weston_surface *surface,
3927 const char *role_name,
3928 struct wl_resource *error_resource,
3929 uint32_t error_code)
3930{
3931 assert(role_name);
3932
3933 if (surface->role_name == NULL ||
3934 surface->role_name == role_name ||
3935 strcmp(surface->role_name, role_name) == 0) {
3936 surface->role_name = role_name;
3937
3938 return 0;
3939 }
3940
3941 wl_resource_post_error(error_resource, error_code,
3942 "Cannot assign role %s to wl_surface@%d,"
3943 " already has role %s\n",
3944 role_name,
3945 wl_resource_get_id(surface->resource),
3946 surface->role_name);
3947 return -1;
3948}
3949
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003950WL_EXPORT const char *
3951weston_surface_get_role(struct weston_surface *surface)
3952{
3953 return surface->role_name;
3954}
3955
Pekka Paalanen8274d902014-08-06 19:36:51 +03003956WL_EXPORT void
3957weston_surface_set_label_func(struct weston_surface *surface,
3958 int (*desc)(struct weston_surface *,
3959 char *, size_t))
3960{
3961 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003962 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003963}
3964
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003965/** Get the size of surface contents
3966 *
3967 * \param surface The surface to query.
3968 * \param width Returns the width of raw contents.
3969 * \param height Returns the height of raw contents.
3970 *
3971 * Retrieves the raw surface content size in pixels for the given surface.
3972 * This is the whole content size in buffer pixels. If the surface
3973 * has no content or the renderer does not implement this feature,
3974 * zeroes are returned.
3975 *
3976 * This function is used to determine the buffer size needed for
3977 * a weston_surface_copy_content() call.
3978 */
3979WL_EXPORT void
3980weston_surface_get_content_size(struct weston_surface *surface,
3981 int *width, int *height)
3982{
3983 struct weston_renderer *rer = surface->compositor->renderer;
3984
3985 if (!rer->surface_get_content_size) {
3986 *width = 0;
3987 *height = 0;
3988 return;
3989 }
3990
3991 rer->surface_get_content_size(surface, width, height);
3992}
3993
Quentin Glidic248dd102016-08-12 10:41:34 +02003994/** Get the bounding box of a surface and its subsurfaces
3995 *
3996 * \param surface The surface to query.
3997 * \return The bounding box relative to the surface origin.
3998 *
3999 */
4000WL_EXPORT struct weston_geometry
4001weston_surface_get_bounding_box(struct weston_surface *surface)
4002{
4003 pixman_region32_t region;
4004 pixman_box32_t *box;
4005 struct weston_subsurface *subsurface;
4006
4007 pixman_region32_init_rect(&region,
4008 0, 0,
4009 surface->width, surface->height);
4010
4011 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4012 pixman_region32_union_rect(&region, &region,
4013 subsurface->position.x,
4014 subsurface->position.y,
4015 subsurface->surface->width,
4016 subsurface->surface->height);
4017
4018 box = pixman_region32_extents(&region);
4019 struct weston_geometry geometry = {
4020 .x = box->x1,
4021 .y = box->y1,
4022 .width = box->x2 - box->x1,
4023 .height = box->y2 - box->y1,
4024 };
4025
4026 pixman_region32_fini(&region);
4027
4028 return geometry;
4029}
4030
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004031/** Copy surface contents to system memory.
4032 *
4033 * \param surface The surface to copy from.
4034 * \param target Pointer to the target memory buffer.
4035 * \param size Size of the target buffer in bytes.
4036 * \param src_x X location on contents to copy from.
4037 * \param src_y Y location on contents to copy from.
4038 * \param width Width in pixels of the area to copy.
4039 * \param height Height in pixels of the area to copy.
4040 * \return 0 for success, -1 for failure.
4041 *
4042 * Surface contents are maintained by the renderer. They can be in a
4043 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4044 * else.
4045 *
4046 * Surface contents are copied into memory pointed to by target,
4047 * which has size bytes of space available. The target memory
4048 * may be larger than needed, but being smaller returns an error.
4049 * The extra bytes in target may or may not be written; their content is
4050 * unspecified. Size must be large enough to hold the image.
4051 *
4052 * The image in the target memory will be arranged in rows from
4053 * top to bottom, and pixels on a row from left to right. The pixel
4054 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4055 * width * 4.
4056 *
4057 * Parameters src_x and src_y define the upper-left corner in buffer
4058 * coordinates (pixels) to copy from. Parameters width and height
4059 * define the size of the area to copy in pixels.
4060 *
4061 * The rectangle defined by src_x, src_y, width, height must fit in
4062 * the surface contents. Otherwise an error is returned.
4063 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004064 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004065 * needed target buffer size and rectangle limits.
4066 *
4067 * CURRENT IMPLEMENTATION RESTRICTIONS:
4068 * - the machine must be little-endian due to Pixman formats.
4069 *
4070 * NOTE: Pixman formats are premultiplied.
4071 */
4072WL_EXPORT int
4073weston_surface_copy_content(struct weston_surface *surface,
4074 void *target, size_t size,
4075 int src_x, int src_y,
4076 int width, int height)
4077{
4078 struct weston_renderer *rer = surface->compositor->renderer;
4079 int cw, ch;
4080 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4081
4082 if (!rer->surface_copy_content)
4083 return -1;
4084
4085 weston_surface_get_content_size(surface, &cw, &ch);
4086
4087 if (src_x < 0 || src_y < 0)
4088 return -1;
4089
4090 if (width <= 0 || height <= 0)
4091 return -1;
4092
4093 if (src_x + width > cw || src_y + height > ch)
4094 return -1;
4095
4096 if (width * bytespp * height > size)
4097 return -1;
4098
4099 return rer->surface_copy_content(surface, target, size,
4100 src_x, src_y, width, height);
4101}
4102
Pekka Paalanene67858b2013-04-25 13:57:42 +03004103static void
4104subsurface_set_position(struct wl_client *client,
4105 struct wl_resource *resource, int32_t x, int32_t y)
4106{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004107 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004108
4109 if (!sub)
4110 return;
4111
4112 sub->position.x = x;
4113 sub->position.y = y;
4114 sub->position.set = 1;
4115}
4116
4117static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004118subsurface_find_sibling(struct weston_subsurface *sub,
4119 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004120{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004121 struct weston_surface *parent = sub->parent;
4122 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004123
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004124 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4125 if (sibling->surface == surface && sibling != sub)
4126 return sibling;
4127 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004128
4129 return NULL;
4130}
4131
4132static struct weston_subsurface *
4133subsurface_sibling_check(struct weston_subsurface *sub,
4134 struct weston_surface *surface,
4135 const char *request)
4136{
4137 struct weston_subsurface *sibling;
4138
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004139 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004140 if (!sibling) {
4141 wl_resource_post_error(sub->resource,
4142 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4143 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004144 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004145 return NULL;
4146 }
4147
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004148 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004149
4150 return sibling;
4151}
4152
4153static void
4154subsurface_place_above(struct wl_client *client,
4155 struct wl_resource *resource,
4156 struct wl_resource *sibling_resource)
4157{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004158 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004159 struct weston_surface *surface =
4160 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004161 struct weston_subsurface *sibling;
4162
4163 if (!sub)
4164 return;
4165
4166 sibling = subsurface_sibling_check(sub, surface, "place_above");
4167 if (!sibling)
4168 return;
4169
4170 wl_list_remove(&sub->parent_link_pending);
4171 wl_list_insert(sibling->parent_link_pending.prev,
4172 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004173
4174 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004175}
4176
4177static void
4178subsurface_place_below(struct wl_client *client,
4179 struct wl_resource *resource,
4180 struct wl_resource *sibling_resource)
4181{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004182 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004183 struct weston_surface *surface =
4184 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004185 struct weston_subsurface *sibling;
4186
4187 if (!sub)
4188 return;
4189
4190 sibling = subsurface_sibling_check(sub, surface, "place_below");
4191 if (!sibling)
4192 return;
4193
4194 wl_list_remove(&sub->parent_link_pending);
4195 wl_list_insert(&sibling->parent_link_pending,
4196 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004197
4198 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004199}
4200
4201static void
4202subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4203{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004204 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004205
4206 if (sub)
4207 sub->synchronized = 1;
4208}
4209
4210static void
4211subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4212{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004213 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004214
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004215 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004216 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004217
4218 /* If sub became effectively desynchronized, flush. */
4219 if (!weston_subsurface_is_synchronized(sub))
4220 weston_subsurface_synchronized_commit(sub);
4221 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004222}
4223
4224static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004225weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4226{
4227 wl_list_remove(&sub->parent_link);
4228 wl_list_remove(&sub->parent_link_pending);
4229 wl_list_remove(&sub->parent_destroy_listener.link);
4230 sub->parent = NULL;
4231}
4232
4233static void
4234weston_subsurface_destroy(struct weston_subsurface *sub);
4235
4236static void
4237subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4238{
4239 struct weston_subsurface *sub =
4240 container_of(listener, struct weston_subsurface,
4241 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004242 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004243
4244 /* The protocol object (wl_resource) is left inert. */
4245 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004246 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004247
4248 weston_subsurface_destroy(sub);
4249}
4250
4251static void
4252subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4253{
4254 struct weston_subsurface *sub =
4255 container_of(listener, struct weston_subsurface,
4256 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004257 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004258 assert(sub->surface != sub->parent);
4259
4260 if (weston_surface_is_mapped(sub->surface))
4261 weston_surface_unmap(sub->surface);
4262
4263 weston_subsurface_unlink_parent(sub);
4264}
4265
4266static void
4267subsurface_resource_destroy(struct wl_resource *resource)
4268{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004269 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004270
4271 if (sub)
4272 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004273}
4274
4275static void
4276subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4277{
4278 wl_resource_destroy(resource);
4279}
4280
4281static void
4282weston_subsurface_link_parent(struct weston_subsurface *sub,
4283 struct weston_surface *parent)
4284{
4285 sub->parent = parent;
4286 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004287 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004288 &sub->parent_destroy_listener);
4289
4290 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4291 wl_list_insert(&parent->subsurface_list_pending,
4292 &sub->parent_link_pending);
4293}
4294
4295static void
4296weston_subsurface_link_surface(struct weston_subsurface *sub,
4297 struct weston_surface *surface)
4298{
4299 sub->surface = surface;
4300 sub->surface_destroy_listener.notify =
4301 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004302 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004303 &sub->surface_destroy_listener);
4304}
4305
4306static void
4307weston_subsurface_destroy(struct weston_subsurface *sub)
4308{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004309 struct weston_view *view, *next;
4310
Pekka Paalanene67858b2013-04-25 13:57:42 +03004311 assert(sub->surface);
4312
4313 if (sub->resource) {
4314 assert(weston_surface_to_subsurface(sub->surface) == sub);
4315 assert(sub->parent_destroy_listener.notify ==
4316 subsurface_handle_parent_destroy);
4317
George Kiagiadakised04d382014-06-13 18:10:26 +02004318 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4319 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004320 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004321 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004322
Pekka Paalanene67858b2013-04-25 13:57:42 +03004323 if (sub->parent)
4324 weston_subsurface_unlink_parent(sub);
4325
Jason Ekstrand7b982072014-05-20 14:33:03 -05004326 weston_surface_state_fini(&sub->cached);
4327 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004328
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004329 sub->surface->committed = NULL;
4330 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004331 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004332 } else {
4333 /* the dummy weston_subsurface for the parent itself */
4334 assert(sub->parent_destroy_listener.notify == NULL);
4335 wl_list_remove(&sub->parent_link);
4336 wl_list_remove(&sub->parent_link_pending);
4337 }
4338
4339 wl_list_remove(&sub->surface_destroy_listener.link);
4340 free(sub);
4341}
4342
4343static const struct wl_subsurface_interface subsurface_implementation = {
4344 subsurface_destroy,
4345 subsurface_set_position,
4346 subsurface_place_above,
4347 subsurface_place_below,
4348 subsurface_set_sync,
4349 subsurface_set_desync
4350};
4351
4352static struct weston_subsurface *
4353weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4354 struct weston_surface *parent)
4355{
4356 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004357 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004358
Bryce Harringtonde16d892014-11-20 22:21:57 -08004359 sub = zalloc(sizeof *sub);
4360 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004361 return NULL;
4362
Jason Ekstranda7af7042013-10-12 22:38:11 -05004363 wl_list_init(&sub->unused_views);
4364
Jason Ekstranda85118c2013-06-27 20:17:02 -05004365 sub->resource =
4366 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004367 if (!sub->resource) {
4368 free(sub);
4369 return NULL;
4370 }
4371
Jason Ekstranda85118c2013-06-27 20:17:02 -05004372 wl_resource_set_implementation(sub->resource,
4373 &subsurface_implementation,
4374 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004375 weston_subsurface_link_surface(sub, surface);
4376 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004377 weston_surface_state_init(&sub->cached);
4378 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004379 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004380
4381 return sub;
4382}
4383
4384/* Create a dummy subsurface for having the parent itself in its
4385 * sub-surface lists. Makes stacking order manipulation easy.
4386 */
4387static struct weston_subsurface *
4388weston_subsurface_create_for_parent(struct weston_surface *parent)
4389{
4390 struct weston_subsurface *sub;
4391
Bryce Harringtonde16d892014-11-20 22:21:57 -08004392 sub = zalloc(sizeof *sub);
4393 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004394 return NULL;
4395
4396 weston_subsurface_link_surface(sub, parent);
4397 sub->parent = parent;
4398 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4399 wl_list_insert(&parent->subsurface_list_pending,
4400 &sub->parent_link_pending);
4401
4402 return sub;
4403}
4404
4405static void
4406subcompositor_get_subsurface(struct wl_client *client,
4407 struct wl_resource *resource,
4408 uint32_t id,
4409 struct wl_resource *surface_resource,
4410 struct wl_resource *parent_resource)
4411{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004412 struct weston_surface *surface =
4413 wl_resource_get_user_data(surface_resource);
4414 struct weston_surface *parent =
4415 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004416 struct weston_subsurface *sub;
4417 static const char where[] = "get_subsurface: wl_subsurface@";
4418
4419 if (surface == parent) {
4420 wl_resource_post_error(resource,
4421 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4422 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004423 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004424 return;
4425 }
4426
4427 if (weston_surface_to_subsurface(surface)) {
4428 wl_resource_post_error(resource,
4429 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4430 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004431 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004432 return;
4433 }
4434
Pekka Paalanen50b67472014-10-01 15:02:41 +03004435 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4436 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004437 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004438
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004439 if (weston_surface_get_main_surface(parent) == surface) {
4440 wl_resource_post_error(resource,
4441 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4442 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004443 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004444 return;
4445 }
4446
Pekka Paalanene67858b2013-04-25 13:57:42 +03004447 /* make sure the parent is in its own list */
4448 if (wl_list_empty(&parent->subsurface_list)) {
4449 if (!weston_subsurface_create_for_parent(parent)) {
4450 wl_resource_post_no_memory(resource);
4451 return;
4452 }
4453 }
4454
4455 sub = weston_subsurface_create(id, surface, parent);
4456 if (!sub) {
4457 wl_resource_post_no_memory(resource);
4458 return;
4459 }
4460
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004461 surface->committed = subsurface_committed;
4462 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004463 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004464}
4465
4466static void
4467subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4468{
4469 wl_resource_destroy(resource);
4470}
4471
4472static const struct wl_subcompositor_interface subcompositor_interface = {
4473 subcompositor_destroy,
4474 subcompositor_get_subsurface
4475};
4476
4477static void
4478bind_subcompositor(struct wl_client *client,
4479 void *data, uint32_t version, uint32_t id)
4480{
4481 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004482 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004483
Jason Ekstranda85118c2013-06-27 20:17:02 -05004484 resource =
4485 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004486 if (resource == NULL) {
4487 wl_client_post_no_memory(client);
4488 return;
4489 }
4490 wl_resource_set_implementation(resource, &subcompositor_interface,
4491 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004492}
4493
Bryce Harrington0795ece2016-08-30 12:04:26 -07004494/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004495 *
4496 * \param compositor The compositor instance
4497 * \param state The DPMS state the outputs will be set to
4498 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004499static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004500weston_compositor_dpms(struct weston_compositor *compositor,
4501 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004502{
4503 struct weston_output *output;
4504
Bryce Harrington08976ac2016-08-30 12:05:16 -07004505 wl_list_for_each(output, &compositor->output_list, link)
4506 if (output->set_dpms)
4507 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004508}
4509
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004510/** Restores the compositor to active status
4511 *
4512 * \param compositor The compositor instance
4513 *
4514 * If the compositor was in a sleeping mode, all outputs are powered
4515 * back on via DPMS. Otherwise if the compositor was inactive
4516 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4517 * signal will fire.
4518 *
4519 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004520 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004521 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004522WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004523weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004524{
Neil Roberts8b62e202013-09-30 13:14:47 +01004525 uint32_t old_state = compositor->state;
4526
4527 /* The state needs to be changed before emitting the wake
4528 * signal because that may try to schedule a repaint which
4529 * will not work if the compositor is still sleeping */
4530 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4531
4532 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004533 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004534 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004535 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004536 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004537 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004538 /* fall through */
4539 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004540 wl_event_source_timer_update(compositor->idle_source,
4541 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004542 }
4543}
4544
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004545/** Turns off rendering and frame events for the compositor.
4546 *
4547 * \param compositor The compositor instance
4548 *
4549 * This is used for example to prevent further rendering while the
4550 * compositor is shutting down.
4551 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004552 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004553 *
4554 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004555 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004556WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004557weston_compositor_offscreen(struct weston_compositor *compositor)
4558{
4559 switch (compositor->state) {
4560 case WESTON_COMPOSITOR_OFFSCREEN:
4561 return;
4562 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004563 default:
4564 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4565 wl_event_source_timer_update(compositor->idle_source, 0);
4566 }
4567}
4568
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004569/** Powers down all attached output devices
4570 *
4571 * \param compositor The compositor instance
4572 *
4573 * Causes rendering to the outputs to cease, and no frame events to be
4574 * sent. Only powers down the outputs if the compositor is not already
4575 * in sleep mode.
4576 *
4577 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004578 *
4579 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004580 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004581WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004582weston_compositor_sleep(struct weston_compositor *compositor)
4583{
4584 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4585 return;
4586
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004587 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004588 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4589 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4590}
4591
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004592/** Sets compositor to idle mode
4593 *
4594 * \param data The compositor instance
4595 *
4596 * This is called when the idle timer fires. Once the compositor is in
4597 * idle mode it requires a wake action (e.g. via
4598 * weston_compositor_wake()) to restore it. The compositor's
4599 * idle_signal will be triggered when the idle event occurs.
4600 *
4601 * Idleness can be inhibited by setting the compositor's idle_inhibit
4602 * property.
4603 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004604static int
4605idle_handler(void *data)
4606{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004607 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004608
4609 if (compositor->idle_inhibit)
4610 return 1;
4611
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004612 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004613 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004614
4615 return 1;
4616}
4617
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004618WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004619weston_plane_init(struct weston_plane *plane,
4620 struct weston_compositor *ec,
4621 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004622{
4623 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004624 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004625 plane->x = x;
4626 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004627 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004628
4629 /* Init the link so that the call to wl_list_remove() when releasing
4630 * the plane without ever stacking doesn't lead to a crash */
4631 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004632}
4633
4634WL_EXPORT void
4635weston_plane_release(struct weston_plane *plane)
4636{
Xiong Zhang97116532013-10-23 13:58:31 +08004637 struct weston_view *view;
4638
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004639 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004640 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004641
Xiong Zhang97116532013-10-23 13:58:31 +08004642 wl_list_for_each(view, &plane->compositor->view_list, link) {
4643 if (view->plane == plane)
4644 view->plane = NULL;
4645 }
4646
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004647 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004648}
4649
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004650/** weston_compositor_stack_plane
4651 * \ingroup compositor
4652 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004653WL_EXPORT void
4654weston_compositor_stack_plane(struct weston_compositor *ec,
4655 struct weston_plane *plane,
4656 struct weston_plane *above)
4657{
4658 if (above)
4659 wl_list_insert(above->link.prev, &plane->link);
4660 else
4661 wl_list_insert(&ec->plane_list, &plane->link);
4662}
4663
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004664static void
4665output_release(struct wl_client *client, struct wl_resource *resource)
4666{
4667 wl_resource_destroy(resource);
4668}
4669
4670static const struct wl_output_interface output_interface = {
4671 output_release,
4672};
4673
4674
Casey Dahlin9074db52012-04-19 22:50:09 -04004675static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004676{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004677 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004678}
4679
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004680static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004681bind_output(struct wl_client *client,
4682 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004683{
Pekka Paalanen05347622017-03-27 12:24:34 +03004684 struct weston_head *head = data;
4685 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004686 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004687 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004688
Jason Ekstranda85118c2013-06-27 20:17:02 -05004689 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004690 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004691 if (resource == NULL) {
4692 wl_client_post_no_memory(client);
4693 return;
4694 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004695
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004696 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004697 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004698 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004699
Pekka Paalanen05347622017-03-27 12:24:34 +03004700 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004701 wl_output_send_geometry(resource,
4702 output->x,
4703 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004704 head->mm_width,
4705 head->mm_height,
4706 head->subpixel,
4707 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004708 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004709 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004710 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004711 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004712
4713 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004714 wl_output_send_mode(resource,
4715 mode->flags,
4716 mode->width,
4717 mode->height,
4718 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004719 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004720
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004721 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004722 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004723}
4724
Pekka Paalanendcac3512017-12-08 14:13:34 +02004725static void
4726weston_head_add_global(struct weston_head *head)
4727{
4728 head->global = wl_global_create(head->compositor->wl_display,
4729 &wl_output_interface, 3,
4730 head, bind_output);
4731}
4732
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004733/** Remove the global wl_output protocol object
4734 *
4735 * \param head The head whose global to remove.
4736 *
4737 * Also orphans the wl_resources for this head (wl_output).
4738 */
4739static void
4740weston_head_remove_global(struct weston_head *head)
4741{
4742 struct wl_resource *resource, *tmp;
4743
4744 if (head->global)
4745 wl_global_destroy(head->global);
4746 head->global = NULL;
4747
4748 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4749 unbind_resource(resource);
4750 wl_resource_set_destructor(resource, NULL);
4751 wl_resource_set_user_data(resource, NULL);
4752 }
Roman Gilge97391c2019-03-29 13:01:06 +01004753
4754 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
4755 /* It's sufficient to unset the destructor, then the list elements
4756 * won't be accessed.
4757 */
4758 wl_resource_set_destructor(resource, NULL);
4759 }
4760 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004761}
4762
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004763/** Get the backing object of wl_output
4764 *
4765 * \param resource A wl_output protocol object.
4766 * \return The backing object (user data) of a wl_resource representing a
4767 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004768 *
4769 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004770 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004771WL_EXPORT struct weston_head *
4772weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004773{
4774 assert(wl_resource_instance_of(resource, &wl_output_interface,
4775 &output_interface));
4776
4777 return wl_resource_get_user_data(resource);
4778}
4779
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004780/** Initialize a pre-allocated weston_head
4781 *
4782 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004783 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004784 *
4785 * The head will be safe to attach, detach and release.
4786 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004787 * The name is used in logs, and can be used by compositors as a configuration
4788 * identifier.
4789 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004790 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004791 * \internal
4792 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004793WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004794weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004795{
4796 /* Add some (in)sane defaults which can be used
4797 * for checking if an output was properly configured
4798 */
4799 memset(head, 0, sizeof *head);
4800
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004801 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004802 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004803 wl_list_init(&head->output_link);
4804 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01004805 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004806 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05304807 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004808}
4809
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004810/** Send output heads changed signal
4811 *
4812 * \param output The output that changed.
4813 *
4814 * Notify that the enabled output gained and/or lost heads, or that the
4815 * associated heads may have changed their connection status. This does not
4816 * include cases where the output becomes enabled or disabled. The registered
4817 * callbacks are called after the change has successfully happened.
4818 *
4819 * If connection status change causes the compositor to attach or detach a head
4820 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03004821 *
4822 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004823 */
4824static void
4825weston_output_emit_heads_changed(struct weston_output *output)
4826{
4827 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4828 output);
4829}
4830
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004831/** Idle task for emitting heads_changed_signal */
4832static void
4833weston_compositor_call_heads_changed(void *data)
4834{
4835 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004836 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004837
4838 compositor->heads_changed_source = NULL;
4839
4840 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004841
4842 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4843 if (head->output && head->output->enabled)
4844 weston_output_emit_heads_changed(head->output);
4845 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004846}
4847
4848/** Schedule a call on idle to heads_changed callback
4849 *
4850 * \param compositor The Compositor.
4851 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004852 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004853 * \internal
4854 */
4855static void
4856weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4857{
4858 struct wl_event_loop *loop;
4859
4860 if (compositor->heads_changed_source)
4861 return;
4862
4863 loop = wl_display_get_event_loop(compositor->wl_display);
4864 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4865 weston_compositor_call_heads_changed, compositor);
4866}
4867
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004868/** Register a new head
4869 *
4870 * \param compositor The compositor.
4871 * \param head The head to register, must not be already registered.
4872 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004873 * This signals the core that a new head has become available, leading to
4874 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004875 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004876 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004877 * \internal
4878 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004879WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004880weston_compositor_add_head(struct weston_compositor *compositor,
4881 struct weston_head *head)
4882{
4883 assert(wl_list_empty(&head->compositor_link));
4884 assert(head->name);
4885
4886 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4887 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004888 weston_compositor_schedule_heads_changed(compositor);
4889}
4890
4891/** Adds a listener to be called when heads change
4892 *
4893 * \param compositor The compositor.
4894 * \param listener The listener to add.
4895 *
Marius Vlada2dace22019-06-12 16:05:44 +03004896 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004897 *
4898 * The listener function will be called after heads are added or their
4899 * connection status has changed. Several changes may be accumulated into a
4900 * single call. The user is expected to iterate over the existing heads and
4901 * check their statuses to find out what changed.
4902 *
4903 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4904 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004905 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004906 */
4907WL_EXPORT void
4908weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4909 struct wl_listener *listener)
4910{
4911 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004912}
4913
4914/** Iterate over available heads
4915 *
4916 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03004917 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004918 * \return The next available head in the list.
4919 *
4920 * Returns all available heads, regardless of being connected or enabled.
4921 *
4922 * You can iterate over all heads as follows:
4923 * \code
4924 * struct weston_head *head = NULL;
4925 *
4926 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4927 * ...
4928 * }
4929 * \endcode
4930 *
4931 * If you cause \c iter to be removed from the list, you cannot use it to
4932 * continue iterating. Removing any other item is safe.
4933 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004934 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004935 */
4936WL_EXPORT struct weston_head *
4937weston_compositor_iterate_heads(struct weston_compositor *compositor,
4938 struct weston_head *iter)
4939{
4940 struct wl_list *list = &compositor->head_list;
4941 struct wl_list *node;
4942
4943 assert(compositor);
4944 assert(!iter || iter->compositor == compositor);
4945
4946 if (iter)
4947 node = iter->compositor_link.next;
4948 else
4949 node = list->next;
4950
4951 assert(node);
4952 assert(!iter || node != &iter->compositor_link);
4953
4954 if (node == list)
4955 return NULL;
4956
4957 return container_of(node, struct weston_head, compositor_link);
4958}
4959
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004960/** Iterate over attached heads
4961 *
4962 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03004963 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004964 * \return The next attached head in the list.
4965 *
4966 * Returns all heads currently attached to the output.
4967 *
4968 * You can iterate over all heads as follows:
4969 * \code
4970 * struct weston_head *head = NULL;
4971 *
4972 * while ((head = weston_output_iterate_heads(output, head))) {
4973 * ...
4974 * }
4975 * \endcode
4976 *
4977 * If you cause \c iter to be removed from the list, you cannot use it to
4978 * continue iterating. Removing any other item is safe.
4979 *
Marius Vlad55d87362019-06-11 01:15:35 +03004980 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004981 */
4982WL_EXPORT struct weston_head *
4983weston_output_iterate_heads(struct weston_output *output,
4984 struct weston_head *iter)
4985{
4986 struct wl_list *list = &output->head_list;
4987 struct wl_list *node;
4988
4989 assert(output);
4990 assert(!iter || iter->output == output);
4991
4992 if (iter)
4993 node = iter->output_link.next;
4994 else
4995 node = list->next;
4996
4997 assert(node);
4998 assert(!iter || node != &iter->output_link);
4999
5000 if (node == list)
5001 return NULL;
5002
5003 return container_of(node, struct weston_head, output_link);
5004}
5005
Pekka Paalanendcac3512017-12-08 14:13:34 +02005006/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005007 *
5008 * \param output The output to attach to.
5009 * \param head The head that is not yet attached.
5010 * \return 0 on success, -1 on failure.
5011 *
5012 * Attaches the given head to the output. All heads of an output are clones
5013 * and share the resolution and timings.
5014 *
5015 * Cloning heads this way uses less resources than creating an output for
5016 * each head, but is not always possible due to environment, driver and hardware
5017 * limitations.
5018 *
5019 * On failure, the head remains unattached. Success of this function does not
5020 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005021 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005022 *
Marius Vlad55d87362019-06-11 01:15:35 +03005023 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005024 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005025WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005026weston_output_attach_head(struct weston_output *output,
5027 struct weston_head *head)
5028{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005029 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005030
5031 if (!wl_list_empty(&head->output_link))
5032 return -1;
5033
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005034 if (output->attach_head) {
5035 if (output->attach_head(output, head) < 0)
5036 return -1;
5037 } else if (!wl_list_empty(&output->head_list)) {
5038 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005039 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005040 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005041
5042 head->output = output;
5043 wl_list_insert(output->head_list.prev, &head->output_link);
5044
Pekka Paalanendcac3512017-12-08 14:13:34 +02005045 if (output->enabled) {
5046 weston_head_add_global(head);
5047
5048 head_names = weston_output_create_heads_string(output);
5049 weston_log("Output '%s' updated to have head(s) %s\n",
5050 output->name, head_names);
5051 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005052
5053 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005054 }
5055
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005056 return 0;
5057}
5058
5059/** Detach a head from its output
5060 *
5061 * \param head The head to detach.
5062 *
5063 * It is safe to detach a non-attached head.
5064 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005065 * If the head is attached to an enabled output and the output will be left
5066 * with no heads, the output will be disabled.
5067 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005068 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005069 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005070 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005071WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005072weston_head_detach(struct weston_head *head)
5073{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005074 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005075 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005076
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005077 wl_list_remove(&head->output_link);
5078 wl_list_init(&head->output_link);
5079 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005080
5081 if (!output)
5082 return;
5083
5084 if (output->detach_head)
5085 output->detach_head(output, head);
5086
5087 if (output->enabled) {
5088 weston_head_remove_global(head);
5089
Pekka Paalanena0106992017-12-08 16:11:17 +02005090 if (wl_list_empty(&output->head_list)) {
5091 weston_log("Output '%s' no heads left, disabling.\n",
5092 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005093 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005094 } else {
5095 head_names = weston_output_create_heads_string(output);
5096 weston_log("Output '%s' updated to have head(s) %s\n",
5097 output->name, head_names);
5098 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005099
5100 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005101 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005102 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005103}
5104
5105/** Destroy a head
5106 *
5107 * \param head The head to be released.
5108 *
5109 * Destroys the head. The caller is responsible for freeing the memory pointed
5110 * to by \c head.
5111 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005112 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005113 * \internal
5114 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005115WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005116weston_head_release(struct weston_head *head)
5117{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005118 wl_signal_emit(&head->destroy_signal, head);
5119
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005120 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005121
5122 free(head->make);
5123 free(head->model);
5124 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005125 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005126
5127 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005128}
5129
Pekka Paalanene19970f2017-08-28 14:11:02 +03005130static void
5131weston_head_set_device_changed(struct weston_head *head)
5132{
5133 head->device_changed = true;
5134
5135 if (head->compositor)
5136 weston_compositor_schedule_heads_changed(head->compositor);
5137}
5138
5139/** String equal comparison with NULLs being equal */
5140static bool
5141str_null_eq(const char *a, const char *b)
5142{
5143 if (!a && !b)
5144 return true;
5145
5146 if (!!a != !!b)
5147 return false;
5148
5149 return strcmp(a, b) == 0;
5150}
5151
Pekka Paalanen01f60212017-03-24 15:39:24 +02005152/** Store monitor make, model and serial number
5153 *
5154 * \param head The head to modify.
5155 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5156 * any string, or NULL for none.
5157 * \param model The monitor model or name, or a made-up string, or NULL for
5158 * none.
5159 * \param serialno The monitor serial number, a made-up string, or NULL for
5160 * none.
5161 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005162 * This may set the device_changed flag.
5163 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005164 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005165 * \internal
5166 */
5167WL_EXPORT void
5168weston_head_set_monitor_strings(struct weston_head *head,
5169 const char *make,
5170 const char *model,
5171 const char *serialno)
5172{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005173 if (str_null_eq(head->make, make) &&
5174 str_null_eq(head->model, model) &&
5175 str_null_eq(head->serial_number, serialno))
5176 return;
5177
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005178 free(head->make);
5179 free(head->model);
5180 free(head->serial_number);
5181
5182 head->make = make ? strdup(make) : NULL;
5183 head->model = model ? strdup(model) : NULL;
5184 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005185
5186 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005187}
5188
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005189/** Store display non-desktop status
5190 *
5191 * \param head The head to modify.
5192 * \param non_desktop Whether the head connects to a non-desktop display.
5193 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005194 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005195 * \internal
5196 */
5197WL_EXPORT void
5198weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5199{
5200 if (head->non_desktop == non_desktop)
5201 return;
5202
5203 head->non_desktop = non_desktop;
5204
5205 weston_head_set_device_changed(head);
5206}
5207
Pekka Paalanen01f60212017-03-24 15:39:24 +02005208/** Store physical image size
5209 *
5210 * \param head The head to modify.
5211 * \param mm_width Image area width in millimeters.
5212 * \param mm_height Image area height in millimeters.
5213 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005214 * This may set the device_changed flag.
5215 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005216 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005217 * \internal
5218 */
5219WL_EXPORT void
5220weston_head_set_physical_size(struct weston_head *head,
5221 int32_t mm_width, int32_t mm_height)
5222{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005223 if (head->mm_width == mm_width &&
5224 head->mm_height == mm_height)
5225 return;
5226
Pekka Paalanen01f60212017-03-24 15:39:24 +02005227 head->mm_width = mm_width;
5228 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005229
5230 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005231}
5232
5233/** Store monitor sub-pixel layout
5234 *
5235 * \param head The head to modify.
5236 * \param sp Sub-pixel layout. The possible values are:
5237 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5238 * - WL_OUTPUT_SUBPIXEL_NONE,
5239 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5240 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5241 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5242 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5243 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005244 * This may set the device_changed flag.
5245 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005246 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005247 * \internal
5248 */
5249WL_EXPORT void
5250weston_head_set_subpixel(struct weston_head *head,
5251 enum wl_output_subpixel sp)
5252{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005253 if (head->subpixel == sp)
5254 return;
5255
Pekka Paalanen01f60212017-03-24 15:39:24 +02005256 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005257
5258 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005259}
5260
5261/** Mark the monitor as internal
5262 *
5263 * This is used for embedded screens, like laptop panels.
5264 *
5265 * \param head The head to mark as internal.
5266 *
5267 * By default a head is external. The type is often inferred from the physical
5268 * connector type.
5269 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005270 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005271 * \internal
5272 */
5273WL_EXPORT void
5274weston_head_set_internal(struct weston_head *head)
5275{
5276 head->connection_internal = true;
5277}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005278
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005279/** Store connector status
5280 *
5281 * \param head The head to modify.
5282 * \param connected Whether the head is connected.
5283 *
5284 * Connectors are created as disconnected. This function can be used to
5285 * set the connector status.
5286 *
5287 * The status should be set to true when a physical connector is connected to
5288 * a video sink device like a monitor and to false when the connector is
5289 * disconnected. For nested backends, the connection status should reflect the
5290 * connection to the parent display server.
5291 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005292 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005293 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005294 *
5295 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005296 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005297 * \internal
5298 */
5299WL_EXPORT void
5300weston_head_set_connection_status(struct weston_head *head, bool connected)
5301{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005302 if (head->connected == connected)
5303 return;
5304
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005305 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005306
Pekka Paalanene19970f2017-08-28 14:11:02 +03005307 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005308}
5309
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305310static void
5311weston_output_compute_protection(struct weston_output *output)
5312{
5313 struct weston_head *head;
5314 enum weston_hdcp_protection op_protection;
5315 bool op_protection_valid = false;
5316
5317 wl_list_for_each(head, &output->head_list, output_link) {
5318 if (!op_protection_valid) {
5319 op_protection = head->current_protection;
5320 op_protection_valid = true;
5321 }
5322 if (head->current_protection < op_protection)
5323 op_protection = head->current_protection;
5324 }
5325
5326 if (!op_protection_valid)
5327 op_protection = WESTON_HDCP_DISABLE;
5328
5329 if (output->current_protection != op_protection)
5330 output->current_protection = op_protection;
5331}
5332
5333WL_EXPORT void
5334weston_head_set_content_protection_status(struct weston_head *head,
5335 enum weston_hdcp_protection status)
5336{
5337 head->current_protection = status;
5338 if (head->output)
5339 weston_output_compute_protection(head->output);
5340}
5341
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005342/** Is the head currently connected?
5343 *
5344 * \param head The head to query.
5345 * \return Connection status.
5346 *
5347 * Returns true if the head is physically connected to a monitor, or in
5348 * case of a nested backend returns true when there is a connection to the
5349 * parent display server.
5350 *
5351 * This is independent from the head being enabled.
5352 *
5353 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005354 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005355 */
5356WL_EXPORT bool
5357weston_head_is_connected(struct weston_head *head)
5358{
5359 return head->connected;
5360}
5361
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005362/** Is the head currently enabled?
5363 *
5364 * \param head The head to query.
5365 * \return Video status.
5366 *
5367 * Returns true if the head is currently transmitting a video stream.
5368 *
5369 * This is independent of the head being connected.
5370 *
5371 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005372 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005373 */
5374WL_EXPORT bool
5375weston_head_is_enabled(struct weston_head *head)
5376{
5377 if (!head->output)
5378 return false;
5379
5380 return head->output->enabled;
5381}
5382
Pekka Paalanene19970f2017-08-28 14:11:02 +03005383/** Has the device information changed?
5384 *
5385 * \param head The head to query.
5386 * \return True if the device information has changed since last reset.
5387 *
5388 * The information about the connected display device, e.g. a monitor, may
5389 * change without being disconnected in between. Changing information
5390 * causes a call to the heads_changed hook.
5391 *
5392 * The information includes make, model, serial number, physical size,
5393 * and sub-pixel type. The connection status is also included.
5394 *
5395 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005396 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005397 */
5398WL_EXPORT bool
5399weston_head_is_device_changed(struct weston_head *head)
5400{
5401 return head->device_changed;
5402}
5403
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005404/** Does the head represent a non-desktop display?
5405 *
5406 * \param head The head to query.
5407 * \return True if the device is a non-desktop display.
5408 *
5409 * Non-desktop heads are not attached to outputs by default.
5410 * This stops weston from extending the desktop onto head mounted displays.
5411 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005412 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005413 */
5414WL_EXPORT bool
5415weston_head_is_non_desktop(struct weston_head *head)
5416{
5417 return head->non_desktop;
5418}
5419
Pekka Paalanene19970f2017-08-28 14:11:02 +03005420/** Acknowledge device information change
5421 *
5422 * \param head The head to acknowledge.
5423 *
5424 * Clears the device changed flag on this head. When a compositor has processed
5425 * device information, it should call this to be able to notice further
5426 * changes.
5427 *
5428 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005429 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005430 */
5431WL_EXPORT void
5432weston_head_reset_device_changed(struct weston_head *head)
5433{
5434 head->device_changed = false;
5435}
5436
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005437/** Get the name of a head
5438 *
5439 * \param head The head to query.
5440 * \return The head's name, not NULL.
5441 *
5442 * The name depends on the backend. The DRM backend uses connector names,
5443 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005444 *
5445 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005446 */
5447WL_EXPORT const char *
5448weston_head_get_name(struct weston_head *head)
5449{
5450 return head->name;
5451}
5452
5453/** Get the output the head is attached to
5454 *
5455 * \param head The head to query.
5456 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005457 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005458 */
5459WL_EXPORT struct weston_output *
5460weston_head_get_output(struct weston_head *head)
5461{
5462 return head->output;
5463}
5464
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005465/** Add destroy callback for a head
5466 *
5467 * \param head The head to watch for.
5468 * \param listener The listener to add. The \c notify member must be set.
5469 *
5470 * Heads may get destroyed for various reasons by the backends. If a head is
5471 * attached to an output, the compositor should listen for head destruction
5472 * and reconfigure or destroy the output if necessary.
5473 *
5474 * The destroy callbacks will be called on weston_head destruction before any
5475 * automatic detaching from an associated weston_output and before any
5476 * weston_head information is lost.
5477 *
5478 * The \c data argument to the notify callback is the weston_head being
5479 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005480 *
5481 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005482 */
5483WL_EXPORT void
5484weston_head_add_destroy_listener(struct weston_head *head,
5485 struct wl_listener *listener)
5486{
5487 wl_signal_add(&head->destroy_signal, listener);
5488}
5489
5490/** Look up destroy listener for a head
5491 *
5492 * \param head The head to query.
5493 * \param notify The notify function used used for the added destroy listener.
5494 * \return The listener, or NULL if not found.
5495 *
5496 * This looks up the previously added destroy listener struct based on the
5497 * notify function it has. The listener can be used to access user data
5498 * through \c container_of().
5499 *
5500 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005501 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005502 */
5503WL_EXPORT struct wl_listener *
5504weston_head_get_destroy_listener(struct weston_head *head,
5505 wl_notify_func_t notify)
5506{
5507 return wl_signal_get(&head->destroy_signal, notify);
5508}
5509
David Fort0de859e2016-05-27 23:22:57 +02005510/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005511static void
David Fort0de859e2016-05-27 23:22:57 +02005512weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5513 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005514{
5515 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005516 bool start_resizing = false;
5517
5518 if (!delta_width)
5519 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005520
5521 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005522 if (output == resized_output) {
5523 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005524 continue;
5525 }
5526
David Fort0de859e2016-05-27 23:22:57 +02005527 if (start_resizing) {
5528 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005529 output->dirty = 1;
5530 }
5531 }
5532}
5533
Pekka Paalanend72bad22017-03-29 17:01:41 +03005534static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005535weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005536{
Scott Moreau850ca422012-05-21 15:21:25 -06005537 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005538
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005539 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005540 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005541
Scott Moreauccbf29d2012-02-22 14:21:41 -07005542 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005543 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005544 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005545 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005546 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005547 weston_matrix_scale(&output->matrix, magnification,
5548 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005549 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005550
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005551 switch (output->transform) {
5552 case WL_OUTPUT_TRANSFORM_FLIPPED:
5553 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5554 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5555 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5556 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5557 weston_matrix_scale(&output->matrix, -1, 1, 1);
5558 break;
5559 }
5560
5561 switch (output->transform) {
5562 default:
5563 case WL_OUTPUT_TRANSFORM_NORMAL:
5564 case WL_OUTPUT_TRANSFORM_FLIPPED:
5565 break;
5566 case WL_OUTPUT_TRANSFORM_90:
5567 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5568 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5569 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5570 break;
5571 case WL_OUTPUT_TRANSFORM_180:
5572 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5573 weston_matrix_translate(&output->matrix,
5574 -output->width, -output->height, 0);
5575 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5576 break;
5577 case WL_OUTPUT_TRANSFORM_270:
5578 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5579 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5580 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5581 break;
5582 }
5583
5584 if (output->current_scale != 1)
5585 weston_matrix_scale(&output->matrix,
5586 output->current_scale,
5587 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005588
Scott Moreauccbf29d2012-02-22 14:21:41 -07005589 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005590
5591 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005592}
5593
Scott Moreau1bad5db2012-08-18 01:04:05 -06005594static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005595weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005596{
5597 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005598 output->native_scale = scale;
5599 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005600
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005601 convert_size_by_transform_scale(&output->width, &output->height,
5602 output->current_mode->width,
5603 output->current_mode->height,
5604 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005605}
5606
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005607static void
5608weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005609{
5610 output->x = x;
5611 output->y = y;
5612
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005613 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005614 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005615
5616 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005617 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005618 output->width,
5619 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005620}
5621
Marius Vlad55d87362019-06-11 01:15:35 +03005622/**
5623 * \ingroup output
5624 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005625WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005626weston_output_move(struct weston_output *output, int x, int y)
5627{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005628 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005629 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005630 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005631
5632 output->move_x = x - output->x;
5633 output->move_y = y - output->y;
5634
5635 if (output->move_x == 0 && output->move_y == 0)
5636 return;
5637
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005638 weston_output_init_geometry(output, x, y);
5639
5640 output->dirty = 1;
5641
5642 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005643 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005644
5645 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005646 wl_list_for_each(head, &output->head_list, output_link) {
5647 wl_resource_for_each(resource, &head->resource_list) {
5648 wl_output_send_geometry(resource,
5649 output->x,
5650 output->y,
5651 head->mm_width,
5652 head->mm_height,
5653 head->subpixel,
5654 head->make,
5655 head->model,
5656 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005657
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005658 ver = wl_resource_get_version(resource);
5659 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5660 wl_output_send_done(resource);
5661 }
Roman Gilge97391c2019-03-29 13:01:06 +01005662
5663 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5664 zxdg_output_v1_send_logical_position(resource,
5665 output->x,
5666 output->y);
5667 zxdg_output_v1_send_done(resource);
5668 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005669 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005670}
5671
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005672/** Signal that a pending output is taken into use.
5673 *
5674 * Removes the output from the pending list and adds it to the compositor's
5675 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005676 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005677 * The output gets an internal ID assigned, and the wl_output global is
5678 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005679 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005680 * \param compositor The compositor instance.
5681 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005682 *
5683 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005684 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005685 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005686static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005687weston_compositor_add_output(struct weston_compositor *compositor,
5688 struct weston_output *output)
5689{
Armin Krezoviće5403842016-08-05 15:28:29 +02005690 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005691 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005692
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005693 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005694
5695 /* Verify we haven't reached the limit of 32 available output IDs */
5696 assert(ffs(~compositor->output_id_pool) > 0);
5697
5698 /* Invert the output id pool and look for the lowest numbered
5699 * switch (the least significant bit). Take that bit's position
5700 * as our ID, and mark it used in the compositor's output_id_pool.
5701 */
5702 output->id = ffs(~compositor->output_id_pool) - 1;
5703 compositor->output_id_pool |= 1u << output->id;
5704
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005705 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005706 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005707 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005708
Pekka Paalanendcac3512017-12-08 14:13:34 +02005709 wl_list_for_each(head, &output->head_list, output_link)
5710 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005711
Giulio Camuffob1147152015-05-06 21:41:57 +03005712 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005713
5714 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5715 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005716}
5717
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005718/** Transform device coordinates into global coordinates
5719 *
Marius Vlada2dace22019-06-12 16:05:44 +03005720 * \param output the weston_output object
5721 * \param[in] device_x X coordinate in device units.
5722 * \param[in] device_y Y coordinate in device units.
5723 * \param[out] x X coordinate in the global space.
5724 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005725 *
Marius Vlada2dace22019-06-12 16:05:44 +03005726 * Transforms coordinates from the device coordinate space (physical pixel
5727 * units) to the global coordinate space (logical pixel units). This takes
5728 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005729 *
Marius Vlad55d87362019-06-11 01:15:35 +03005730 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005731 * \internal
5732 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005733WL_EXPORT void
5734weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005735 double device_x, double device_y,
5736 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005737{
Derek Foreman0f679412014-10-02 13:41:17 -05005738 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005739 device_x,
5740 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005741 0.0,
5742 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005743
Derek Foreman67a18b92015-03-24 11:36:14 -05005744 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005745
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005746 *x = p.f[0] / p.f[3];
5747 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005748}
5749
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005750/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005751 *
5752 * \param output The weston_output object that is being removed.
5753 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005754 * The following happens:
5755 *
5756 * - The output assignments of all views in the current scenegraph are
5757 * recomputed.
5758 *
5759 * - Presentation feedback is discarded.
5760 *
5761 * - Compositor is notified that outputs were changed and
5762 * applies the necessary changes to re-layout outputs.
5763 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005764 * - The output is put back in the pending outputs list.
5765 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005766 * - Signal is emitted to notify all users of the weston_output
5767 * object that the output is being destroyed.
5768 *
5769 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005770 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005771 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005772 * - The output's internal ID is released.
5773 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005774 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005775 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005776 */
5777static void
5778weston_compositor_remove_output(struct weston_output *output)
5779{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005780 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005781 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005782 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005783
5784 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005785 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005786
Pekka Paalanenbccda712017-03-29 16:16:04 +03005787 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005788 if (view->output_mask & (1u << output->id))
5789 weston_view_assign_output(view);
5790 }
5791
5792 weston_presentation_feedback_discard_list(&output->feedback_list);
5793
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005794 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005795
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005796 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005797 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005798 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005799
Pekka Paalanenbccda712017-03-29 16:16:04 +03005800 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005801 wl_signal_emit(&output->destroy_signal, output);
5802
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005803 wl_list_for_each(head, &output->head_list, output_link)
5804 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005805
5806 compositor->output_id_pool &= ~(1u << output->id);
5807 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005808}
5809
5810/** Sets the output scale for a given output.
5811 *
5812 * \param output The weston_output object that the scale is set for.
5813 * \param scale Scale factor for the given output.
5814 *
5815 * It only supports setting scale for an output that
5816 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005817 *
Marius Vlad55d87362019-06-11 01:15:35 +03005818 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005819 */
5820WL_EXPORT void
5821weston_output_set_scale(struct weston_output *output,
5822 int32_t scale)
5823{
5824 /* We can only set scale on a disabled output */
5825 assert(!output->enabled);
5826
5827 /* We only want to set scale once */
5828 assert(!output->scale);
5829
5830 output->scale = scale;
5831}
5832
5833/** Sets the output transform for a given output.
5834 *
5835 * \param output The weston_output object that the transform is set for.
5836 * \param transform Transform value for the given output.
5837 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005838 * Refer to wl_output::transform section located at
5839 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5840 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005841 *
Marius Vlad55d87362019-06-11 01:15:35 +03005842 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005843 */
5844WL_EXPORT void
5845weston_output_set_transform(struct weston_output *output,
5846 uint32_t transform)
5847{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005848 struct weston_pointer_motion_event ev;
5849 struct wl_resource *resource;
5850 struct weston_seat *seat;
5851 pixman_region32_t old_region;
5852 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005853 struct weston_head *head;
5854 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005855
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005856 if (!output->enabled && output->transform == UINT32_MAX) {
5857 output->transform = transform;
5858 return;
5859 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005860
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005861 weston_output_transform_scale_init(output, transform, output->scale);
5862
5863 pixman_region32_init(&old_region);
5864 pixman_region32_copy(&old_region, &output->region);
5865
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005866 weston_output_init_geometry(output, output->x, output->y);
5867
5868 output->dirty = 1;
5869
5870 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005871 wl_list_for_each(head, &output->head_list, output_link) {
5872 wl_resource_for_each(resource, &head->resource_list) {
5873 wl_output_send_geometry(resource,
5874 output->x,
5875 output->y,
5876 head->mm_width,
5877 head->mm_height,
5878 head->subpixel,
5879 head->make,
5880 head->model,
5881 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005882
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005883 ver = wl_resource_get_version(resource);
5884 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5885 wl_output_send_done(resource);
5886 }
Roman Gilge97391c2019-03-29 13:01:06 +01005887 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5888 zxdg_output_v1_send_logical_position(resource,
5889 output->x,
5890 output->y);
5891 zxdg_output_v1_send_logical_size(resource,
5892 output->width,
5893 output->height);
5894 zxdg_output_v1_send_done(resource);
5895 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005896 }
5897
5898 /* we must ensure that pointers are inside output, otherwise they disappear */
5899 mid_x = output->x + output->width / 2;
5900 mid_y = output->y + output->height / 2;
5901
5902 ev.mask = WESTON_POINTER_MOTION_ABS;
5903 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5904 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5905
5906 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5907 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5908
5909 if (pointer && pixman_region32_contains_point(&old_region,
5910 wl_fixed_to_int(pointer->x),
5911 wl_fixed_to_int(pointer->y),
5912 NULL))
5913 weston_pointer_move(pointer, &ev);
5914 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005915}
5916
5917/** Initializes a weston_output object with enough data so
5918 ** an output can be configured.
5919 *
5920 * \param output The weston_output object to initialize
5921 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005922 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005923 *
5924 * Sets initial values for fields that are expected to be
5925 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005926 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005927 * The name is used in logs, and can be used by compositors as a configuration
5928 * identifier.
5929 *
Marius Vlad55d87362019-06-11 01:15:35 +03005930 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005931 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005932 */
5933WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005934weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005935 struct weston_compositor *compositor,
5936 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005937{
5938 output->compositor = compositor;
5939 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005940 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005941 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005942 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005943 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05305944 output->desired_protection = WESTON_HDCP_DISABLE;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005945
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005946 wl_list_init(&output->head_list);
5947
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005948 /* Add some (in)sane defaults which can be used
5949 * for checking if an output was properly configured
5950 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005951 output->scale = 0;
5952 /* Can't use -1 on uint32_t and 0 is valid enum value */
5953 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005954
5955 pixman_region32_init(&output->previous_damage);
5956 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005957 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005958}
5959
5960/** Adds weston_output object to pending output list.
5961 *
5962 * \param output The weston_output object to add
5963 * \param compositor The compositor instance.
5964 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005965 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005966 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005967 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005968 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005969 */
5970WL_EXPORT void
5971weston_compositor_add_pending_output(struct weston_output *output,
5972 struct weston_compositor *compositor)
5973{
Pekka Paalanene952a012017-03-29 17:14:00 +03005974 assert(output->disable);
5975 assert(output->enable);
5976
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005977 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005978 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005979}
5980
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005981/** Create a string with the attached heads' names.
5982 *
5983 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03005984 *
5985 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005986 */
5987static char *
5988weston_output_create_heads_string(struct weston_output *output)
5989{
5990 FILE *fp;
5991 char *str = NULL;
5992 size_t size = 0;
5993 struct weston_head *head;
5994 const char *sep = "";
5995
5996 fp = open_memstream(&str, &size);
5997 if (!fp)
5998 return NULL;
5999
6000 wl_list_for_each(head, &output->head_list, output_link) {
6001 fprintf(fp, "%s%s", sep, head->name);
6002 sep = ", ";
6003 }
6004 fclose(fp);
6005
6006 return str;
6007}
6008
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006009/** Constructs a weston_output object that can be used by the compositor.
6010 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006011 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006012 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006013 *
6014 * Output coordinates are calculated and each new output is by default
6015 * assigned to the right of previous one.
6016 *
6017 * Sets up the transformation, zoom, and geometry of the output using
6018 * the properties that need to be configured by the compositor.
6019 *
6020 * Establishes a repaint timer for the output with the relevant display
6021 * object's event loop. See output_repaint_timer_handler().
6022 *
6023 * The output is assigned an ID. Weston can support up to 32 distinct
6024 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6025 * is referred to and used to find the first available ID number, and
6026 * then this ID is marked as used in output_id_pool.
6027 *
6028 * The output is also assigned a Wayland global with the wl_output
6029 * external interface.
6030 *
6031 * Backend specific function is called to set up the output output.
6032 *
6033 * Output is added to the compositor's output list
6034 *
6035 * If the backend specific function fails, the weston_output object
6036 * is returned to a state it was before calling this function and
6037 * is added to the compositor's pending_output_list in case it needs
6038 * to be reconfigured or just so it can be destroyed at shutdown.
6039 *
6040 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006041 *
6042 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006043 */
6044WL_EXPORT int
6045weston_output_enable(struct weston_output *output)
6046{
Armin Krezović782f5df2016-09-30 14:11:11 +02006047 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006048 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006049 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006050 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006051 int x = 0, y = 0;
6052
Pekka Paalanencc201e42017-03-30 15:11:25 +03006053 if (output->enabled) {
6054 weston_log("Error: attempt to enable an enabled output '%s'\n",
6055 output->name);
6056 return -1;
6057 }
6058
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006059 if (wl_list_empty(&output->head_list)) {
6060 weston_log("Error: cannot enable output '%s' without heads.\n",
6061 output->name);
6062 return -1;
6063 }
6064
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006065 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6066 weston_log("Error: no video mode for output '%s'.\n",
6067 output->name);
6068 return -1;
6069 }
6070
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006071 wl_list_for_each(head, &output->head_list, output_link) {
6072 assert(head->make);
6073 assert(head->model);
6074 }
6075
Armin Krezović782f5df2016-09-30 14:11:11 +02006076 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006077 struct weston_output, link);
6078
Armin Krezović782f5df2016-09-30 14:11:11 +02006079 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006080 x = iterator->x + iterator->width;
6081
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006082 /* Make sure the scale is set up */
6083 assert(output->scale);
6084
6085 /* Make sure we have a transform set */
6086 assert(output->transform != UINT32_MAX);
6087
Armin Krezović782f5df2016-09-30 14:11:11 +02006088 output->x = x;
6089 output->y = y;
6090 output->dirty = 1;
6091 output->original_scale = output->scale;
6092
6093 weston_output_transform_scale_init(output, output->transform, output->scale);
6094 weston_output_init_zoom(output);
6095
6096 weston_output_init_geometry(output, x, y);
6097 weston_output_damage(output);
6098
6099 wl_signal_init(&output->frame_signal);
6100 wl_signal_init(&output->destroy_signal);
6101 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006102 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006103
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006104 /* Enable the output (set up the crtc or create a
6105 * window representing the output, set up the
6106 * renderer, etc)
6107 */
6108 if (output->enable(output) < 0) {
6109 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006110 return -1;
6111 }
6112
6113 weston_compositor_add_output(output->compositor, output);
6114
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006115 head_names = weston_output_create_heads_string(output);
6116 weston_log("Output '%s' enabled with head(s) %s\n",
6117 output->name, head_names);
6118 free(head_names);
6119
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006120 return 0;
6121}
6122
6123/** Converts a weston_output object to a pending output state, so it
6124 ** can be configured again or destroyed.
6125 *
6126 * \param output The weston_output object that needs to be disabled.
6127 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006128 * Calls a backend specific function to disable an output, in case
6129 * such function exists.
6130 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006131 * The backend specific disable function may choose to postpone the disabling
6132 * by returning a negative value, in which case this function returns early.
6133 * In that case the backend will guarantee the output will be disabled soon
6134 * by the backend calling this function again. One must not attempt to re-enable
6135 * the output until that happens.
6136 *
6137 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006138 * from weston's output_list (see weston_compositor_remove_output())
6139 * and is returned to a state it was before weston_output_enable()
6140 * was ran (see weston_output_enable_undo()).
6141 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006142 * See weston_output_init() for more information on the
6143 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006144 *
6145 * If the output has never been enabled yet, this function can still be
6146 * called to ensure that the output is actually turned off rather than left
6147 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006148 *
6149 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006150 */
6151WL_EXPORT void
6152weston_output_disable(struct weston_output *output)
6153{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006154 /* Should we rename this? */
6155 output->destroying = 1;
6156
Pekka Paalanenc65df642017-03-29 15:45:46 +03006157 /* Disable is called unconditionally also for not-enabled outputs,
6158 * because at compositor start-up, if there is an output that is
6159 * already on but the compositor wants to turn it off, we have to
6160 * forward the turn-off to the backend so it knows to do it.
6161 * The backend cannot initially turn off everything, because it
6162 * would cause unnecessary mode-sets for all outputs the compositor
6163 * wants to be on.
6164 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006165 if (output->disable(output) < 0)
6166 return;
6167
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006168 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006169 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006170
6171 output->destroying = 0;
6172}
6173
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006174/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006175 *
6176 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006177 *
6178 * If there are new or changed heads, calls the heads_changed hook and
6179 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006180 *
6181 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006182 */
6183WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006184weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006185{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006186 if (compositor->heads_changed_source) {
6187 wl_event_source_remove(compositor->heads_changed_source);
6188 weston_compositor_call_heads_changed(compositor);
6189 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006190}
6191
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006192/** Add destroy callback for an output
6193 *
6194 * \param output The output to watch.
6195 * \param listener The listener to add. The \c notify member must be set.
6196 *
6197 * The listener callback will be called when user destroys an output. This
6198 * may be delayed by a backend in some cases. The main purpose of the
6199 * listener is to allow hooking up custom data to the output. The custom data
6200 * can be fetched via weston_output_get_destroy_listener() followed by
6201 * container_of().
6202 *
6203 * The \c data argument to the notify callback is the weston_output being
6204 * destroyed.
6205 *
6206 * @note This is for the final destruction of an output, not when it gets
6207 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006208 *
6209 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006210 */
6211WL_EXPORT void
6212weston_output_add_destroy_listener(struct weston_output *output,
6213 struct wl_listener *listener)
6214{
6215 wl_signal_add(&output->user_destroy_signal, listener);
6216}
6217
6218/** Look up destroy listener for an output
6219 *
6220 * \param output The output to query.
6221 * \param notify The notify function used used for the added destroy listener.
6222 * \return The listener, or NULL if not found.
6223 *
6224 * This looks up the previously added destroy listener struct based on the
6225 * notify function it has. The listener can be used to access user data
6226 * through \c container_of().
6227 *
6228 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006229 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006230 */
6231WL_EXPORT struct wl_listener *
6232weston_output_get_destroy_listener(struct weston_output *output,
6233 wl_notify_func_t notify)
6234{
6235 return wl_signal_get(&output->user_destroy_signal, notify);
6236}
6237
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006238/** Uninitialize an output
6239 *
6240 * Removes the output from the list of enabled outputs if necessary, but
6241 * does not call the backend's output disable function. The output will no
6242 * longer be in the list of pending outputs either.
6243 *
6244 * All fields of weston_output become uninitialized, i.e. should not be used
6245 * anymore. The caller can free the memory after this.
6246 *
Marius Vlad55d87362019-06-11 01:15:35 +03006247 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006248 * \internal
6249 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006250WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006251weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006252{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006253 struct weston_head *head, *tmp;
6254
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006255 output->destroying = 1;
6256
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006257 wl_signal_emit(&output->user_destroy_signal, output);
6258
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006259 if (output->idle_repaint_source)
6260 wl_event_source_remove(output->idle_repaint_source);
6261
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006262 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006263 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006264
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006265 pixman_region32_fini(&output->region);
6266 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006267 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006268
6269 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6270 weston_head_detach(head);
6271
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006272 free(output->name);
6273}
6274
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006275/** Find an output by its given name
6276 *
6277 * \param compositor The compositor to search in.
6278 * \param name The output name to search for.
6279 * \return An existing output with the given name, or NULL if not found.
6280 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006281 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006282 */
6283WL_EXPORT struct weston_output *
6284weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6285 const char *name)
6286{
6287 struct weston_output *output;
6288
6289 wl_list_for_each(output, &compositor->output_list, link)
6290 if (strcmp(output->name, name) == 0)
6291 return output;
6292
6293 wl_list_for_each(output, &compositor->pending_output_list, link)
6294 if (strcmp(output->name, name) == 0)
6295 return output;
6296
6297 return NULL;
6298}
6299
6300/** Create a named output
6301 *
6302 * \param compositor The compositor.
6303 * \param name The name for the output.
6304 * \return A new \c weston_output, or NULL on failure.
6305 *
6306 * This creates a new weston_output that starts with no heads attached.
6307 *
6308 * An output must be configured and it must have at least one head before
6309 * it can be enabled.
6310 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006311 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006312 */
6313WL_EXPORT struct weston_output *
6314weston_compositor_create_output(struct weston_compositor *compositor,
6315 const char *name)
6316{
6317 assert(compositor->backend->create_output);
6318
6319 if (weston_compositor_find_output_by_name(compositor, name)) {
6320 weston_log("Warning: attempted to create an output with a "
6321 "duplicate name '%s'.\n", name);
6322 return NULL;
6323 }
6324
6325 return compositor->backend->create_output(compositor, name);
6326}
6327
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006328/** Create an output for an unused head
6329 *
6330 * \param compositor The compositor.
6331 * \param head The head to attach to the output.
6332 * \return A new \c weston_output, or NULL on failure.
6333 *
6334 * This creates a new weston_output that starts with the given head attached.
6335 * The output inherits the name of the head. The head must not be already
6336 * attached to another output.
6337 *
6338 * An output must be configured before it can be enabled.
6339 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006340 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006341 */
6342WL_EXPORT struct weston_output *
6343weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6344 struct weston_head *head)
6345{
6346 struct weston_output *output;
6347
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006348 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006349 if (!output)
6350 return NULL;
6351
6352 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006353 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006354 return NULL;
6355 }
6356
6357 return output;
6358}
6359
6360/** Destroy an output
6361 *
6362 * \param output The output to destroy.
6363 *
6364 * The heads attached to the given output are detached and become unused again.
6365 *
6366 * It is not necessary to explicitly destroy all outputs at compositor exit.
6367 * weston_compositor_destroy() will automatically destroy any remaining
6368 * outputs.
6369 *
Marius Vlad55d87362019-06-11 01:15:35 +03006370 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006371 */
6372WL_EXPORT void
6373weston_output_destroy(struct weston_output *output)
6374{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006375 output->destroy(output);
6376}
6377
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006378/** When you need a head...
6379 *
6380 * This function is a hack, used until all code has been converted to become
6381 * multi-head aware.
6382 *
6383 * \param output The weston_output whose head to get.
6384 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006385 *
6386 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006387 */
6388WL_EXPORT struct weston_head *
6389weston_output_get_first_head(struct weston_output *output)
6390{
6391 if (wl_list_empty(&output->head_list))
6392 return NULL;
6393
6394 return container_of(output->head_list.next,
6395 struct weston_head, output_link);
6396}
6397
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006398static void
Roman Gilge97391c2019-03-29 13:01:06 +01006399xdg_output_unlist(struct wl_resource *resource)
6400{
6401 wl_list_remove(wl_resource_get_link(resource));
6402}
6403
6404static void
6405xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6406{
6407 wl_resource_destroy(resource);
6408}
6409
6410static const struct zxdg_output_v1_interface xdg_output_interface = {
6411 xdg_output_destroy
6412};
6413
6414static void
6415xdg_output_manager_destroy(struct wl_client *client,
6416 struct wl_resource *resource)
6417{
6418 wl_resource_destroy(resource);
6419}
6420
6421static void
6422xdg_output_manager_get_xdg_output(struct wl_client *client,
6423 struct wl_resource *manager,
6424 uint32_t id,
6425 struct wl_resource *output_resource)
6426{
6427 int version = wl_resource_get_version(manager);
6428 struct weston_head *head = wl_resource_get_user_data(output_resource);
6429 struct weston_output *output = head->output;
6430 struct wl_resource *resource;
6431
6432 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6433 version, id);
6434 if (resource == NULL) {
6435 wl_client_post_no_memory(client);
6436 return;
6437 }
6438
6439 wl_list_insert(&head->xdg_output_resource_list,
6440 wl_resource_get_link(resource));
6441
6442 wl_resource_set_implementation(resource, &xdg_output_interface,
6443 NULL, xdg_output_unlist);
6444
6445 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6446 zxdg_output_v1_send_logical_size(resource,
6447 output->width,
6448 output->height);
6449 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6450 zxdg_output_v1_send_name(resource, head->name);
6451
6452 zxdg_output_v1_send_done(resource);
6453}
6454
6455static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6456 xdg_output_manager_destroy,
6457 xdg_output_manager_get_xdg_output
6458};
6459
6460static void
6461bind_xdg_output_manager(struct wl_client *client,
6462 void *data, uint32_t version, uint32_t id)
6463{
6464 struct wl_resource *resource;
6465
6466 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6467 version, id);
6468 if (resource == NULL) {
6469 wl_client_post_no_memory(client);
6470 return;
6471 }
6472
6473 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6474 NULL, NULL);
6475}
6476
6477static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006478destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006479{
Jonny Lamb74130762013-11-26 18:19:46 +01006480 struct weston_surface *surface =
6481 wl_resource_get_user_data(resource);
6482
Pekka Paalanen4826f872016-04-22 14:14:38 +03006483 if (!surface)
6484 return;
6485
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006486 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006487 surface->pending.buffer_viewport.buffer.src_width =
6488 wl_fixed_from_int(-1);
6489 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006490 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006491}
6492
6493static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006494viewport_destroy(struct wl_client *client,
6495 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006496{
6497 wl_resource_destroy(resource);
6498}
6499
6500static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006501viewport_set_source(struct wl_client *client,
6502 struct wl_resource *resource,
6503 wl_fixed_t src_x,
6504 wl_fixed_t src_y,
6505 wl_fixed_t src_width,
6506 wl_fixed_t src_height)
6507{
6508 struct weston_surface *surface =
6509 wl_resource_get_user_data(resource);
6510
Pekka Paalanen4826f872016-04-22 14:14:38 +03006511 if (!surface) {
6512 wl_resource_post_error(resource,
6513 WP_VIEWPORT_ERROR_NO_SURFACE,
6514 "wl_surface for this viewport is no longer exists");
6515 return;
6516 }
6517
6518 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006519 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006520
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006521 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006522 src_height == wl_fixed_from_int(-1) &&
6523 src_x == wl_fixed_from_int(-1) &&
6524 src_y == wl_fixed_from_int(-1)) {
6525 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006526 surface->pending.buffer_viewport.buffer.src_width =
6527 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006528 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006529 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006530 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006531
Pekka Paalanen201769a2016-04-26 14:42:11 +03006532 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006533 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006534 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006535 "wl_surface@%d viewport source "
6536 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6537 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006538 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006539 wl_fixed_to_double(src_height),
6540 wl_fixed_to_double(src_x),
6541 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006542 return;
6543 }
6544
6545 surface->pending.buffer_viewport.buffer.src_x = src_x;
6546 surface->pending.buffer_viewport.buffer.src_y = src_y;
6547 surface->pending.buffer_viewport.buffer.src_width = src_width;
6548 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006549 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006550}
6551
6552static void
6553viewport_set_destination(struct wl_client *client,
6554 struct wl_resource *resource,
6555 int32_t dst_width,
6556 int32_t dst_height)
6557{
6558 struct weston_surface *surface =
6559 wl_resource_get_user_data(resource);
6560
Pekka Paalanen4826f872016-04-22 14:14:38 +03006561 if (!surface) {
6562 wl_resource_post_error(resource,
6563 WP_VIEWPORT_ERROR_NO_SURFACE,
6564 "wl_surface for this viewport no longer exists");
6565 return;
6566 }
6567
6568 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006569
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006570 if (dst_width == -1 && dst_height == -1) {
6571 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006572 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006573 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006574 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006575 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006576
6577 if (dst_width <= 0 || dst_height <= 0) {
6578 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006579 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006580 "destination size must be positive (%dx%d)",
6581 dst_width, dst_height);
6582 return;
6583 }
6584
6585 surface->pending.buffer_viewport.surface.width = dst_width;
6586 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006587 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006588}
6589
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006590static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006591 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006592 viewport_set_source,
6593 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006594};
6595
6596static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006597viewporter_destroy(struct wl_client *client,
6598 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006599{
6600 wl_resource_destroy(resource);
6601}
6602
6603static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006604viewporter_get_viewport(struct wl_client *client,
6605 struct wl_resource *viewporter,
6606 uint32_t id,
6607 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006608{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006609 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006610 struct weston_surface *surface =
6611 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006612 struct wl_resource *resource;
6613
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006614 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006615 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006616 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006617 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006618 return;
6619 }
6620
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006621 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006622 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006623 if (resource == NULL) {
6624 wl_client_post_no_memory(client);
6625 return;
6626 }
6627
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006628 wl_resource_set_implementation(resource, &viewport_interface,
6629 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006630
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006631 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006632}
6633
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006634static const struct wp_viewporter_interface viewporter_interface = {
6635 viewporter_destroy,
6636 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006637};
6638
6639static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006640bind_viewporter(struct wl_client *client,
6641 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006642{
6643 struct wl_resource *resource;
6644
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006645 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006646 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006647 if (resource == NULL) {
6648 wl_client_post_no_memory(client);
6649 return;
6650 }
6651
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006652 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006653 NULL, NULL);
6654}
6655
6656static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006657destroy_presentation_feedback(struct wl_resource *feedback_resource)
6658{
6659 struct weston_presentation_feedback *feedback;
6660
6661 feedback = wl_resource_get_user_data(feedback_resource);
6662
6663 wl_list_remove(&feedback->link);
6664 free(feedback);
6665}
6666
6667static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006668presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6669{
6670 wl_resource_destroy(resource);
6671}
6672
6673static void
6674presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006675 struct wl_resource *presentation_resource,
6676 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006677 uint32_t callback)
6678{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006679 struct weston_surface *surface;
6680 struct weston_presentation_feedback *feedback;
6681
6682 surface = wl_resource_get_user_data(surface_resource);
6683
Bryce Harringtonde16d892014-11-20 22:21:57 -08006684 feedback = zalloc(sizeof *feedback);
6685 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006686 goto err_calloc;
6687
6688 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006689 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006690 1, callback);
6691 if (!feedback->resource)
6692 goto err_create;
6693
6694 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6695 destroy_presentation_feedback);
6696
6697 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6698
6699 return;
6700
6701err_create:
6702 free(feedback);
6703
6704err_calloc:
6705 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006706}
6707
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006708static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006709 presentation_destroy,
6710 presentation_feedback
6711};
6712
6713static void
6714bind_presentation(struct wl_client *client,
6715 void *data, uint32_t version, uint32_t id)
6716{
6717 struct weston_compositor *compositor = data;
6718 struct wl_resource *resource;
6719
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006720 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006721 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006722 if (resource == NULL) {
6723 wl_client_post_no_memory(client);
6724 return;
6725 }
6726
6727 wl_resource_set_implementation(resource, &presentation_implementation,
6728 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006729 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006730}
6731
6732static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006733compositor_bind(struct wl_client *client,
6734 void *data, uint32_t version, uint32_t id)
6735{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006736 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006737 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006738
Jason Ekstranda85118c2013-06-27 20:17:02 -05006739 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006740 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006741 if (resource == NULL) {
6742 wl_client_post_no_memory(client);
6743 return;
6744 }
6745
6746 wl_resource_set_implementation(resource, &compositor_interface,
6747 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006748}
6749
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006750WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006751weston_environment_get_fd(const char *env)
6752{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006753 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006754 int fd, flags;
6755
6756 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006757 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006758 return -1;
6759
6760 flags = fcntl(fd, F_GETFD);
6761 if (flags == -1)
6762 return -1;
6763
6764 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6765 unsetenv(env);
6766
6767 return fd;
6768}
6769
Pekka Paalanenb5026542014-11-12 15:09:24 +02006770static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006771timeline_key_binding_handler(struct weston_keyboard *keyboard,
6772 const struct timespec *time, uint32_t key,
6773 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006774{
6775 struct weston_compositor *compositor = data;
6776
6777 if (weston_timeline_enabled_)
6778 weston_timeline_close();
6779 else
6780 weston_timeline_open(compositor);
6781}
6782
Daniel Stonece62cb32018-07-20 09:46:24 +01006783static const char *
6784output_repaint_status_text(struct weston_output *output)
6785{
6786 switch (output->repaint_status) {
6787 case REPAINT_NOT_SCHEDULED:
6788 return "no repaint";
6789 case REPAINT_BEGIN_FROM_IDLE:
6790 return "start_repaint_loop scheduled";
6791 case REPAINT_SCHEDULED:
6792 return "repaint scheduled";
6793 case REPAINT_AWAITING_COMPLETION:
6794 return "awaiting completion";
6795 }
6796
6797 assert(!"output_repaint_status_text missing enum");
6798 return NULL;
6799}
6800
6801static void
6802debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6803{
6804 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6805 struct wl_shm_buffer *shm;
6806 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006807 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006808
6809 if (!buffer) {
6810 fprintf(fp, "\t\t[buffer not available]\n");
6811 return;
6812 }
6813
6814 shm = wl_shm_buffer_get(buffer->resource);
6815 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006816 uint32_t _format = wl_shm_buffer_get_format(shm);
6817 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006818 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006819 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6820 (unsigned long) _format,
6821 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006822 return;
6823 }
6824
6825 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6826 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006827 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006828 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006829 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6830 (unsigned long) dmabuf->attributes.format,
6831 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006832 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6833 (unsigned long long) dmabuf->attributes.modifier[0]);
6834 return;
6835 }
6836
Marius Vlad253ba9a2019-03-17 18:22:21 +02006837 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006838}
6839
6840static void
6841debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6842{
6843 struct weston_compositor *ec = view->surface->compositor;
6844 struct weston_output *output;
6845 char desc[512];
6846 pixman_box32_t *box;
6847 uint32_t surface_id = 0;
6848 pid_t pid = 0;
6849
6850 if (view->surface->resource) {
6851 struct wl_resource *resource = view->surface->resource;
6852 wl_client_get_credentials(wl_resource_get_client(resource),
6853 &pid, NULL, NULL);
6854 surface_id = wl_resource_get_id(view->surface->resource);
6855 }
6856
6857 if (!view->surface->get_label ||
6858 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6859 strcpy(desc, "[no description available]");
6860 }
6861 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6862 view_idx, view->surface->role_name, pid, surface_id,
6863 desc, view);
6864
6865 box = pixman_region32_extents(&view->transform.boundingbox);
6866 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6867 box->x1, box->y1, box->x2, box->y2);
6868 box = pixman_region32_extents(&view->transform.opaque);
6869
6870 if (pixman_region32_equal(&view->transform.opaque,
6871 &view->transform.boundingbox)) {
6872 fprintf(fp, "\t\t[fully opaque]\n");
6873 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6874 fprintf(fp, "\t\t[not opaque]\n");
6875 } else {
6876 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6877 box->x1, box->y1, box->x2, box->y2);
6878 }
6879
6880 if (view->alpha < 1.0)
6881 fprintf(fp, "\t\talpha: %f\n", view->alpha);
6882
6883 if (view->output_mask != 0) {
6884 bool first_output = true;
6885 fprintf(fp, "\t\toutputs: ");
6886 wl_list_for_each(output, &ec->output_list, link) {
6887 if (!(view->output_mask & (1 << output->id)))
6888 continue;
6889 fprintf(fp, "%s%d (%s)%s",
6890 (first_output) ? "" : ", ",
6891 output->id, output->name,
6892 (view->output == output) ? " (primary)" : "");
6893 first_output = false;
6894 }
6895 } else {
6896 fprintf(fp, "\t\t[no outputs]");
6897 }
6898
6899 fprintf(fp, "\n");
6900
6901 debug_scene_view_print_buffer(fp, view);
6902}
6903
Marius Vlad433f4e72019-02-17 22:14:23 +02006904static void
6905debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02006906 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02006907{
6908 struct weston_subsurface *sub;
6909 struct weston_view *ev;
6910
6911 /*
6912 * print the view first, then we recursively go on printing
6913 * sub-surfaces. We bail out once no more sub-surfaces are available.
6914 */
Marius Vlada6acfa82019-03-17 18:10:09 +02006915 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02006916
6917 /* no more sub-surfaces */
6918 if (wl_list_empty(&view->surface->subsurface_list))
6919 return;
6920
6921 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
6922 wl_list_for_each(ev, &sub->surface->views, surface_link) {
6923 /* do not print again the parent view */
6924 if (view == ev)
6925 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02006926
6927 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02006928 debug_scene_view_print_tree(ev, fp, view_idx);
6929 }
6930 }
6931}
6932
Daniel Stonece62cb32018-07-20 09:46:24 +01006933/**
6934 * Output information on how libweston is currently composing the scene
6935 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006936 *
6937 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01006938 */
6939WL_EXPORT char *
6940weston_compositor_print_scene_graph(struct weston_compositor *ec)
6941{
6942 struct weston_output *output;
6943 struct weston_layer *layer;
6944 struct timespec now;
6945 int layer_idx = 0;
6946 FILE *fp;
6947 char *ret;
6948 size_t len;
6949 int err;
6950
6951 fp = open_memstream(&ret, &len);
6952 assert(fp);
6953
6954 weston_compositor_read_presentation_clock(ec, &now);
6955 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
6956 now.tv_sec, now.tv_nsec);
6957
6958 wl_list_for_each(output, &ec->output_list, link) {
6959 struct weston_head *head;
6960 int head_idx = 0;
6961
6962 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
6963 assert(output->enabled);
6964
6965 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
6966 output->x, output->y,
6967 output->x + output->width,
6968 output->y + output->height);
6969 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
6970 output->current_mode->width,
6971 output->current_mode->height,
6972 output->current_mode->refresh / 1000.0);
6973 fprintf(fp, "\tscale: %d\n", output->scale);
6974
6975 fprintf(fp, "\trepaint status: %s\n",
6976 output_repaint_status_text(output));
6977 if (output->repaint_status == REPAINT_SCHEDULED)
6978 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
6979 output->next_repaint.tv_sec,
6980 output->next_repaint.tv_nsec);
6981
6982 wl_list_for_each(head, &output->head_list, output_link) {
6983 fprintf(fp, "\tHead %d (%s): %sconnected\n",
6984 head_idx++, head->name,
6985 (head->connected) ? "" : "not ");
6986 }
6987 }
6988
6989 fprintf(fp, "\n");
6990
6991 wl_list_for_each(layer, &ec->layer_list, link) {
6992 struct weston_view *view;
6993 int view_idx = 0;
6994
6995 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
6996 (unsigned long) layer->position);
6997
6998 if (!weston_layer_mask_is_infinite(layer)) {
6999 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7000 layer->mask.x1, layer->mask.y1,
7001 layer->mask.x2, layer->mask.y2);
7002 }
7003
Marius Vlada6acfa82019-03-17 18:10:09 +02007004 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7005 debug_scene_view_print_tree(view, fp, &view_idx);
7006 view_idx++;
7007 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007008
7009 if (wl_list_empty(&layer->view_list.link))
7010 fprintf(fp, "\t[no views]\n");
7011
7012 fprintf(fp, "\n");
7013 }
7014
7015 err = fclose(fp);
7016 assert(err == 0);
7017
7018 return ret;
7019}
7020
7021/**
7022 * Called when the 'scene-graph' debug scope is bound by a client. This
7023 * one-shot weston-debug scope prints the current scene graph when bound,
7024 * and then terminates the stream.
7025 */
7026static void
7027debug_scene_graph_cb(struct weston_debug_stream *stream, void *data)
7028{
7029 struct weston_compositor *ec = data;
7030 char *str = weston_compositor_print_scene_graph(ec);
7031
7032 weston_debug_stream_printf(stream, "%s", str);
7033 free(str);
7034 weston_debug_stream_complete(stream);
7035}
7036
Giulio Camuffo459137b2014-10-11 23:56:24 +03007037/** Create the compositor.
7038 *
7039 * This functions creates and initializes a compositor instance.
7040 *
7041 * \param display The Wayland display to be used.
7042 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007043 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007044 * using the \ref weston_compositor_get_user_data function.
7045 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007046 *
7047 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007048 */
7049WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007050weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007051 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007052 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007053{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007054 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007055 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007056
Giulio Camuffo459137b2014-10-11 23:56:24 +03007057 ec = zalloc(sizeof *ec);
7058 if (!ec)
7059 return NULL;
7060
7061 ec->wl_display = display;
7062 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007063 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007064 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007065 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007066 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007067 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007068 wl_signal_init(&ec->idle_signal);
7069 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007070 wl_signal_init(&ec->show_input_panel_signal);
7071 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007072 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007073 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007074 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007075 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007076 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007077 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007078 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007079 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007080 wl_signal_init(&ec->session_signal);
7081 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007082
Casey Dahlin58ba1372012-04-19 22:50:08 -04007083 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007084 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007085
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007086 ec->activate_serial = 1;
7087
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007088 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7089
Derek Foreman152254b2015-11-26 14:17:48 -06007090 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007091 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007092 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007093
Giulio Camuffo954f1832014-10-11 18:27:30 +03007094 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007095 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007096 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007097
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007098 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007099 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007100 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007101
Roman Gilge97391c2019-03-29 13:01:06 +01007102 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7103 ec, bind_xdg_output_manager))
7104 goto fail;
7105
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007106 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007107 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007108 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007109
Marius Vlad3d7d9782019-04-17 12:35:38 +03007110 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007111 goto fail;
7112
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007113 if (weston_input_init(ec) != 0)
7114 goto fail;
7115
Jason Ekstranda7af7042013-10-12 22:38:11 -05007116 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007117 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007118 wl_list_init(&ec->layer_list);
7119 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007120 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007121 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007122 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007123 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007124 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007125 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007126 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007127 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007128 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007129
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007130 wl_list_init(&ec->plugin_api_list);
7131
Xiong Zhang97116532013-10-23 13:58:31 +08007132 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007133 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007134
Giulio Camuffo459137b2014-10-11 23:56:24 +03007135 wl_data_device_manager_init(ec->wl_display);
7136
7137 wl_display_init_shm(ec->wl_display);
7138
7139 loop = wl_display_get_event_loop(ec->wl_display);
7140 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007141 ec->repaint_timer =
7142 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7143 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007144
Quentin Glidic82681572016-12-17 13:40:51 +01007145 weston_layer_init(&ec->fade_layer, ec);
7146 weston_layer_init(&ec->cursor_layer, ec);
7147
7148 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7149 weston_layer_set_position(&ec->cursor_layer,
7150 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007151
7152 weston_compositor_add_debug_binding(ec, KEY_T,
7153 timeline_key_binding_handler, ec);
7154
Daniel Stonece62cb32018-07-20 09:46:24 +01007155 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03007156 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
7157 "Scene graph details\n",
7158 debug_scene_graph_cb,
7159 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007160
Giulio Camuffo459137b2014-10-11 23:56:24 +03007161 return ec;
7162
7163fail:
7164 free(ec);
7165 return NULL;
7166}
7167
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007168/** weston_compositor_shutdown
7169 * \ingroup compositor
7170 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007171WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007172weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007173{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007174 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007175
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007176 wl_event_source_remove(ec->idle_source);
7177
Matt Roper361d2ad2011-08-29 13:52:23 -07007178 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007179 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007180 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007181
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007182 /* Destroy all pending outputs associated with this compositor */
7183 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7184 output->destroy(output);
7185
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007186 if (ec->renderer)
7187 ec->renderer->destroy(ec);
7188
Daniel Stone325fc2d2012-05-30 16:31:58 +01007189 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007190 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007191 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007192 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007193 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007194 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007195
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007196 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007197}
7198
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007199/** weston_compositor_exit_with_code
7200 * \ingroup compositor
7201 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007202WL_EXPORT void
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007203weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007204 int exit_code)
7205{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007206 if (compositor->exit_code == EXIT_SUCCESS)
7207 compositor->exit_code = exit_code;
7208
Giulio Camuffo459137b2014-10-11 23:56:24 +03007209 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007210}
7211
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007212/** weston_compositor_set_default_pointer_grab
7213 * \ingroup compositor
7214 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007215WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007216weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7217 const struct weston_pointer_grab_interface *interface)
7218{
7219 struct weston_seat *seat;
7220
7221 ec->default_pointer_grab = interface;
7222 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007223 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7224
7225 if (pointer)
7226 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007227 }
7228}
7229
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007230/** weston_compositor_set_presentation_clock
7231 * \ingroup compositor
7232 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007233WL_EXPORT int
7234weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7235 clockid_t clk_id)
7236{
7237 struct timespec ts;
7238
7239 if (clock_gettime(clk_id, &ts) < 0)
7240 return -1;
7241
7242 compositor->presentation_clock = clk_id;
7243
7244 return 0;
7245}
7246
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007247/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007248 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007249 *
7250 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007251 */
7252WL_EXPORT int
7253weston_compositor_set_presentation_clock_software(
7254 struct weston_compositor *compositor)
7255{
7256 /* In order of preference */
7257 static const clockid_t clocks[] = {
7258 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7259 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7260 CLOCK_MONOTONIC, /* no jumps, may crawl */
7261 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7262 CLOCK_REALTIME /* may jump and crawl */
7263 };
7264 unsigned i;
7265
7266 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7267 if (weston_compositor_set_presentation_clock(compositor,
7268 clocks[i]) == 0)
7269 return 0;
7270
7271 weston_log("Error: no suitable presentation clock available.\n");
7272
7273 return -1;
7274}
7275
Pekka Paalanen662f3842015-03-18 12:17:26 +02007276/** Read the current time from the Presentation clock
7277 *
7278 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007279 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007280 *
7281 * \note Reading the current time in user space is always imprecise to some
7282 * degree.
7283 *
7284 * This function is never meant to fail. If reading the clock does fail,
7285 * an error message is logged and a zero time is returned. Callers are not
7286 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007287 *
7288 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007289 */
7290WL_EXPORT void
7291weston_compositor_read_presentation_clock(
7292 const struct weston_compositor *compositor,
7293 struct timespec *ts)
7294{
7295 static bool warned;
7296 int ret;
7297
7298 ret = clock_gettime(compositor->presentation_clock, ts);
7299 if (ret < 0) {
7300 ts->tv_sec = 0;
7301 ts->tv_nsec = 0;
7302
7303 if (!warned)
7304 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007305 "the presentation clock %#x: '%s' (%d)\n",
7306 compositor->presentation_clock,
7307 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007308 warned = true;
7309 }
7310}
7311
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007312/** Import dmabuf buffer into current renderer
7313 *
7314 * \param compositor
7315 * \param buffer the dmabuf buffer to import
7316 * \return true on usable buffers, false otherwise
7317 *
7318 * This function tests that the linux_dmabuf_buffer is usable
7319 * for the current renderer. Returns false on unusable buffers. Usually
7320 * usability is tested by importing the dmabufs for composition.
7321 *
7322 * This hook is also used for detecting if the renderer supports
7323 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7324 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007325 *
7326 * \ingroup compositor
7327 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007328WL_EXPORT bool
7329weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7330 struct linux_dmabuf_buffer *buffer)
7331{
7332 struct weston_renderer *renderer;
7333
7334 renderer = compositor->renderer;
7335
7336 if (renderer->import_dmabuf == NULL)
7337 return false;
7338
7339 return renderer->import_dmabuf(compositor, buffer);
7340}
7341
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007342WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007343weston_version(int *major, int *minor, int *micro)
7344{
7345 *major = WESTON_VERSION_MAJOR;
7346 *minor = WESTON_VERSION_MINOR;
7347 *micro = WESTON_VERSION_MICRO;
7348}
7349
Daniel Stonee03c1112016-11-24 20:45:45 +00007350/**
7351 * Attempts to find a module path from the module map specified in the
7352 * environment. If found, writes the full path into the path variable.
7353 *
7354 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7355 * each entry is of the form "name=path" and entries are separated by
7356 * semicolons. Whitespace is significant.
7357 *
7358 * \param name The name to search for.
7359 * \param path Where the path is written to if found.
7360 * \param path_len Allocated bytes at \c path .
7361 * \returns The length of the string written to path on success, or 0 if the
7362 * module was not specified in the environment map or path_len was too small.
7363 */
7364WL_EXPORT size_t
7365weston_module_path_from_env(const char *name, char *path, size_t path_len)
7366{
7367 const char *mapping = getenv("WESTON_MODULE_MAP");
7368 const char *end;
7369 const int name_len = strlen(name);
7370
7371 if (!mapping)
7372 return 0;
7373
7374 end = mapping + strlen(mapping);
7375 while (mapping < end && *mapping) {
7376 const char *filename, *next;
7377
7378 /* early out: impossibly short string */
7379 if (end - mapping < name_len + 1)
7380 return 0;
7381
7382 filename = &mapping[name_len + 1];
7383 next = strchrnul(mapping, ';');
7384
7385 if (strncmp(mapping, name, name_len) == 0 &&
7386 mapping[name_len] == '=') {
7387 size_t file_len = next - filename; /* no trailing NUL */
7388 if (file_len >= path_len)
7389 return 0;
7390 strncpy(path, filename, file_len);
7391 path[file_len] = '\0';
7392 return file_len;
7393 }
7394
7395 mapping = next + 1;
7396 }
7397
7398 return 0;
7399}
7400
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007401WL_EXPORT void *
7402weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007403{
7404 char path[PATH_MAX];
7405 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007406 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007407
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007408 if (name == NULL)
7409 return NULL;
7410
Derek Foreman3f86e502015-06-08 11:46:54 -05007411 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007412 len = weston_module_path_from_env(name, path, sizeof path);
7413 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007414 len = snprintf(path, sizeof path, "%s/%s",
7415 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007416 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007417 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007418 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007419
Daniel Stonebeb97e52016-11-28 12:13:54 +00007420 /* snprintf returns the length of the string it would've written,
7421 * _excluding_ the NUL byte. So even being equal to the size of
7422 * our buffer is an error here. */
7423 if (len >= sizeof path)
7424 return NULL;
7425
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007426 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7427 if (module) {
7428 weston_log("Module '%s' already loaded\n", path);
7429 dlclose(module);
7430 return NULL;
7431 }
7432
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007433 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007434 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007435 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007436 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007437 return NULL;
7438 }
7439
7440 init = dlsym(module, entrypoint);
7441 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007442 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007443 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007444 return NULL;
7445 }
7446
7447 return init;
7448}
7449
Giulio Camuffo459137b2014-10-11 23:56:24 +03007450
7451/** Destroys the compositor.
7452 *
7453 * This function cleans up the compositor state and destroys it.
7454 *
7455 * \param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007456 *
7457 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007458 */
7459WL_EXPORT void
7460weston_compositor_destroy(struct weston_compositor *compositor)
7461{
7462 /* prevent further rendering while shutting down */
7463 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7464
7465 wl_signal_emit(&compositor->destroy_signal, compositor);
7466
7467 weston_compositor_xkb_destroy(compositor);
7468
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007469 if (compositor->backend)
7470 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007471
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007472 /* The backend is responsible for destroying the heads. */
7473 assert(wl_list_empty(&compositor->head_list));
7474
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007475 weston_plugin_api_destroy_list(compositor);
7476
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007477 if (compositor->heads_changed_source)
7478 wl_event_source_remove(compositor->heads_changed_source);
7479
Marius Vlad7e4db952019-04-17 13:47:06 +03007480 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007481 compositor->debug_scene = NULL;
Marius Vlad3d7d9782019-04-17 12:35:38 +03007482 weston_log_ctx_compositor_destroy(compositor);
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007483
Giulio Camuffo459137b2014-10-11 23:56:24 +03007484 free(compositor);
7485}
7486
7487/** Instruct the compositor to exit.
7488 *
7489 * This functions does not directly destroy the compositor object, it merely
7490 * command it to start the tear down process. It is not guaranteed that the
7491 * tear down will happen immediately.
7492 *
7493 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007494 *
7495 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007496 */
7497WL_EXPORT void
7498weston_compositor_exit(struct weston_compositor *compositor)
7499{
7500 compositor->exit(compositor);
7501}
7502
7503/** Return the user data stored in the compositor.
7504 *
7505 * This function returns the user data pointer set with user_data parameter
7506 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007507 *
7508 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007509 */
7510WL_EXPORT void *
7511weston_compositor_get_user_data(struct weston_compositor *compositor)
7512{
7513 return compositor->user_data;
7514}
Pekka Paalanendd186732016-06-03 14:49:54 +03007515
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007516static const char * const backend_map[] = {
7517 [WESTON_BACKEND_DRM] = "drm-backend.so",
7518 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7519 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7520 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7521 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7522 [WESTON_BACKEND_X11] = "x11-backend.so",
7523};
7524
Pekka Paalanendd186732016-06-03 14:49:54 +03007525/** Load a backend into a weston_compositor
7526 *
7527 * A backend must be loaded to make a weston_compositor work. A backend
7528 * provides input and output capabilities, and determines the renderer to use.
7529 *
7530 * \param compositor A compositor that has not had a backend loaded yet.
7531 * \param backend Name of the backend file.
7532 * \param config_base A pointer to a backend-specific configuration
7533 * structure's 'base' member.
7534 *
7535 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007536 *
7537 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007538 */
7539WL_EXPORT int
7540weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007541 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007542 struct weston_backend_config *config_base)
7543{
7544 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007545 struct weston_backend_config *config_base);
7546
Pekka Paalanend7e35112017-08-29 17:04:12 +03007547 if (compositor->backend) {
7548 weston_log("Error: attempt to load a backend when one is already loaded\n");
7549 return -1;
7550 }
7551
Quentin Glidic887c0182016-07-10 11:00:53 +02007552 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007553 return -1;
7554
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007555 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007556 if (!backend_init)
7557 return -1;
7558
Pekka Paalanend7e35112017-08-29 17:04:12 +03007559 if (backend_init(compositor, config_base) < 0) {
7560 compositor->backend = NULL;
7561 return -1;
7562 }
7563
7564 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007565}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007566
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007567/** weston_compositor_load_xwayland
7568 * \ingroup compositor
7569 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007570WL_EXPORT int
7571weston_compositor_load_xwayland(struct weston_compositor *compositor)
7572{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007573 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007574
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007575 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007576 if (!module_init)
7577 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007578 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007579 return -1;
7580 return 0;
7581}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007582
7583/** Resolve an internal compositor error by disconnecting the client.
7584 *
7585 * This function is used in cases when the wl_buffer turns out
7586 * unusable and there is no fallback path.
7587 *
7588 * It is possible the fault is caused by a compositor bug, the underlying
7589 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7590 * In any case, the options are to either composite garbage or nothing,
7591 * or disconnect the client. This is a helper function for the latter.
7592 *
7593 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7594 *
7595 * \param buffer The weston buffer that is unusable.
7596 * \param msg A custom error message attached to the protocol error.
7597 */
7598WL_EXPORT void
7599weston_buffer_send_server_error(struct weston_buffer *buffer,
7600 const char *msg)
7601{
7602 struct wl_client *client;
7603 struct wl_resource *display_resource;
7604 uint32_t id;
7605
7606 assert(buffer->resource);
7607 id = wl_resource_get_id(buffer->resource);
7608 client = wl_resource_get_client(buffer->resource);
7609 display_resource = wl_client_get_object(client, 1);
7610
7611 assert(display_resource);
7612 wl_resource_post_error(display_resource,
7613 WL_DISPLAY_ERROR_INVALID_OBJECT,
7614 "server error with "
7615 "wl_buffer@%u: %s", id, msg);
7616}