blob: a7cd87512504245bf92d5a604d8e1c6361fdf3a2 [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;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500482}
483
484static void
485weston_surface_state_fini(struct weston_surface_state *state)
486{
487 struct weston_frame_callback *cb, *next;
488
489 wl_list_for_each_safe(cb, next,
490 &state->frame_callback_list, link)
491 wl_resource_destroy(cb->resource);
492
Pekka Paalanen133e4392014-09-23 22:08:46 -0400493 weston_presentation_feedback_discard_list(&state->feedback_list);
494
Jason Ekstrand7b982072014-05-20 14:33:03 -0500495 pixman_region32_fini(&state->input);
496 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600497 pixman_region32_fini(&state->damage_surface);
498 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500499
500 if (state->buffer)
501 wl_list_remove(&state->buffer_destroy_listener.link);
502 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300503
504 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300505 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500506}
507
508static void
509weston_surface_state_set_buffer(struct weston_surface_state *state,
510 struct weston_buffer *buffer)
511{
512 if (state->buffer == buffer)
513 return;
514
515 if (state->buffer)
516 wl_list_remove(&state->buffer_destroy_listener.link);
517 state->buffer = buffer;
518 if (state->buffer)
519 wl_signal_add(&state->buffer->destroy_signal,
520 &state->buffer_destroy_listener);
521}
522
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500523WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500524weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500525{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500526 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400527
Bryce Harringtonde16d892014-11-20 22:21:57 -0800528 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400529 if (surface == NULL)
530 return NULL;
531
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500532 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800533 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500534
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500535 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200536 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400537
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200538 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
539 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200540 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
541 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500542
543 weston_surface_state_init(&surface->pending);
544
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400545 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500546 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300547 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400548
Jason Ekstranda7af7042013-10-12 22:38:11 -0500549 wl_list_init(&surface->views);
550
551 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400552 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500553
Pekka Paalanene67858b2013-04-25 13:57:42 +0300554 wl_list_init(&surface->subsurface_list);
555 wl_list_init(&surface->subsurface_list_pending);
556
Jason Ekstrand1e059042014-10-16 10:55:19 -0500557 weston_matrix_init(&surface->buffer_to_surface_matrix);
558 weston_matrix_init(&surface->surface_to_buffer_matrix);
559
Jonas Ådahld3414f22016-07-22 17:56:31 +0800560 wl_list_init(&surface->pointer_constraints);
561
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300562 surface->acquire_fence_fd = -1;
563
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400564 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500565}
566
Alex Wu8811bf92012-02-28 18:07:54 +0800567WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500568weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200569 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500570{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100571 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200572 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500573}
574
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400575WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500576weston_view_to_global_float(struct weston_view *view,
577 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200578{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500579 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200580 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
581
Jason Ekstranda7af7042013-10-12 22:38:11 -0500582 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200583
584 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200585 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700586 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200587 v.f[3]);
588 *x = 0;
589 *y = 0;
590 return;
591 }
592
593 *x = v.f[0] / v.f[3];
594 *y = v.f[1] / v.f[3];
595 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500596 *x = sx + view->geometry.x;
597 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200598 }
599}
600
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500601WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200602weston_transformed_coord(int width, int height,
603 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200604 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200605 float sx, float sy, float *bx, float *by)
606{
607 switch (transform) {
608 case WL_OUTPUT_TRANSFORM_NORMAL:
609 default:
610 *bx = sx;
611 *by = sy;
612 break;
613 case WL_OUTPUT_TRANSFORM_FLIPPED:
614 *bx = width - sx;
615 *by = sy;
616 break;
617 case WL_OUTPUT_TRANSFORM_90:
618 *bx = height - sy;
619 *by = sx;
620 break;
621 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
622 *bx = height - sy;
623 *by = width - sx;
624 break;
625 case WL_OUTPUT_TRANSFORM_180:
626 *bx = width - sx;
627 *by = height - sy;
628 break;
629 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
630 *bx = sx;
631 *by = height - sy;
632 break;
633 case WL_OUTPUT_TRANSFORM_270:
634 *bx = sy;
635 *by = width - sx;
636 break;
637 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
638 *bx = sy;
639 *by = sx;
640 break;
641 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200642
643 *bx *= scale;
644 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200645}
646
647WL_EXPORT pixman_box32_t
648weston_transformed_rect(int width, int height,
649 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200650 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200651 pixman_box32_t rect)
652{
653 float x1, x2, y1, y2;
654
655 pixman_box32_t ret;
656
Alexander Larsson4ea95522013-05-22 14:41:37 +0200657 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200658 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200659 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200660 rect.x2, rect.y2, &x2, &y2);
661
662 if (x1 <= x2) {
663 ret.x1 = x1;
664 ret.x2 = x2;
665 } else {
666 ret.x1 = x2;
667 ret.x2 = x1;
668 }
669
670 if (y1 <= y2) {
671 ret.y1 = y1;
672 ret.y2 = y2;
673 } else {
674 ret.y1 = y2;
675 ret.y2 = y1;
676 }
677
678 return ret;
679}
680
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600681/** Transform a region by a matrix, restricted to axis-aligned transformations
682 *
683 * Warning: This function does not work for projective, affine, or matrices
684 * that encode arbitrary rotations. Only 90-degree step rotations are
685 * supported.
686 */
687WL_EXPORT void
688weston_matrix_transform_region(pixman_region32_t *dest,
689 struct weston_matrix *matrix,
690 pixman_region32_t *src)
691{
692 pixman_box32_t *src_rects, *dest_rects;
693 int nrects, i;
694
695 src_rects = pixman_region32_rectangles(src, &nrects);
696 dest_rects = malloc(nrects * sizeof(*dest_rects));
697 if (!dest_rects)
698 return;
699
700 for (i = 0; i < nrects; i++) {
701 struct weston_vector vec1 = {{
702 src_rects[i].x1, src_rects[i].y1, 0, 1
703 }};
704 weston_matrix_transform(matrix, &vec1);
705 vec1.f[0] /= vec1.f[3];
706 vec1.f[1] /= vec1.f[3];
707
708 struct weston_vector vec2 = {{
709 src_rects[i].x2, src_rects[i].y2, 0, 1
710 }};
711 weston_matrix_transform(matrix, &vec2);
712 vec2.f[0] /= vec2.f[3];
713 vec2.f[1] /= vec2.f[3];
714
715 if (vec1.f[0] < vec2.f[0]) {
716 dest_rects[i].x1 = floor(vec1.f[0]);
717 dest_rects[i].x2 = ceil(vec2.f[0]);
718 } else {
719 dest_rects[i].x1 = floor(vec2.f[0]);
720 dest_rects[i].x2 = ceil(vec1.f[0]);
721 }
722
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600723 if (vec1.f[1] < vec2.f[1]) {
724 dest_rects[i].y1 = floor(vec1.f[1]);
725 dest_rects[i].y2 = ceil(vec2.f[1]);
726 } else {
727 dest_rects[i].y1 = floor(vec2.f[1]);
728 dest_rects[i].y2 = ceil(vec1.f[1]);
729 }
730 }
731
732 pixman_region32_clear(dest);
733 pixman_region32_init_rects(dest, dest_rects, nrects);
734 free(dest_rects);
735}
736
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200737WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500738weston_transformed_region(int width, int height,
739 enum wl_output_transform transform,
740 int32_t scale,
741 pixman_region32_t *src, pixman_region32_t *dest)
742{
743 pixman_box32_t *src_rects, *dest_rects;
744 int nrects, i;
745
746 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
747 if (src != dest)
748 pixman_region32_copy(dest, src);
749 return;
750 }
751
752 src_rects = pixman_region32_rectangles(src, &nrects);
753 dest_rects = malloc(nrects * sizeof(*dest_rects));
754 if (!dest_rects)
755 return;
756
757 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
758 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
759 } else {
760 for (i = 0; i < nrects; i++) {
761 switch (transform) {
762 default:
763 case WL_OUTPUT_TRANSFORM_NORMAL:
764 dest_rects[i].x1 = src_rects[i].x1;
765 dest_rects[i].y1 = src_rects[i].y1;
766 dest_rects[i].x2 = src_rects[i].x2;
767 dest_rects[i].y2 = src_rects[i].y2;
768 break;
769 case WL_OUTPUT_TRANSFORM_90:
770 dest_rects[i].x1 = height - src_rects[i].y2;
771 dest_rects[i].y1 = src_rects[i].x1;
772 dest_rects[i].x2 = height - src_rects[i].y1;
773 dest_rects[i].y2 = src_rects[i].x2;
774 break;
775 case WL_OUTPUT_TRANSFORM_180:
776 dest_rects[i].x1 = width - src_rects[i].x2;
777 dest_rects[i].y1 = height - src_rects[i].y2;
778 dest_rects[i].x2 = width - src_rects[i].x1;
779 dest_rects[i].y2 = height - src_rects[i].y1;
780 break;
781 case WL_OUTPUT_TRANSFORM_270:
782 dest_rects[i].x1 = src_rects[i].y1;
783 dest_rects[i].y1 = width - src_rects[i].x2;
784 dest_rects[i].x2 = src_rects[i].y2;
785 dest_rects[i].y2 = width - src_rects[i].x1;
786 break;
787 case WL_OUTPUT_TRANSFORM_FLIPPED:
788 dest_rects[i].x1 = width - src_rects[i].x2;
789 dest_rects[i].y1 = src_rects[i].y1;
790 dest_rects[i].x2 = width - src_rects[i].x1;
791 dest_rects[i].y2 = src_rects[i].y2;
792 break;
793 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
794 dest_rects[i].x1 = height - src_rects[i].y2;
795 dest_rects[i].y1 = width - src_rects[i].x2;
796 dest_rects[i].x2 = height - src_rects[i].y1;
797 dest_rects[i].y2 = width - src_rects[i].x1;
798 break;
799 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
800 dest_rects[i].x1 = src_rects[i].x1;
801 dest_rects[i].y1 = height - src_rects[i].y2;
802 dest_rects[i].x2 = src_rects[i].x2;
803 dest_rects[i].y2 = height - src_rects[i].y1;
804 break;
805 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
806 dest_rects[i].x1 = src_rects[i].y1;
807 dest_rects[i].y1 = src_rects[i].x1;
808 dest_rects[i].x2 = src_rects[i].y2;
809 dest_rects[i].y2 = src_rects[i].x2;
810 break;
811 }
812 }
813 }
814
815 if (scale != 1) {
816 for (i = 0; i < nrects; i++) {
817 dest_rects[i].x1 *= scale;
818 dest_rects[i].x2 *= scale;
819 dest_rects[i].y1 *= scale;
820 dest_rects[i].y2 *= scale;
821 }
822 }
823
824 pixman_region32_clear(dest);
825 pixman_region32_init_rects(dest, dest_rects, nrects);
826 free(dest_rects);
827}
828
Jonny Lamb74130762013-11-26 18:19:46 +0100829static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300830viewport_surface_to_buffer(struct weston_surface *surface,
831 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100832{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200833 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200834 double src_width, src_height;
835 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200836
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200837 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
838 if (vp->surface.width == -1) {
839 *bx = sx;
840 *by = sy;
841 return;
842 }
Jonny Lamb74130762013-11-26 18:19:46 +0100843
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200844 src_x = 0.0;
845 src_y = 0.0;
846 src_width = surface->width_from_buffer;
847 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100848 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200849 src_x = wl_fixed_to_double(vp->buffer.src_x);
850 src_y = wl_fixed_to_double(vp->buffer.src_y);
851 src_width = wl_fixed_to_double(vp->buffer.src_width);
852 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100853 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200854
855 *bx = sx * src_width / surface->width + src_x;
856 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100857}
858
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500859WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200860weston_surface_to_buffer_float(struct weston_surface *surface,
861 float sx, float sy, float *bx, float *by)
862{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200863 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
864
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300865 /* first transform coordinates if the viewport is set */
866 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100867
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500868 weston_transformed_coord(surface->width_from_buffer,
869 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200870 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100871 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200872}
873
Derek Foreman99739672015-12-03 16:38:11 -0600874/** Transform a rectangle from surface coordinates to buffer coordinates
875 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300876 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600877 * from.
878 * \param rect The rectangle to transform.
879 * \return The transformed rectangle.
880 *
881 * Viewport and buffer transformations can only do translation, scaling,
882 * and rotations in 90-degree steps. Therefore the only loss in the
883 * conversion is coordinate rounding.
884 *
885 * However, some coordinate rounding takes place as an intermediate
886 * step before the buffer scale factor is applied, so the rectangle
887 * boundary may not be exactly as expected.
888 *
889 * This is OK for damage tracking since a little extra coverage is
890 * not a problem.
891 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200892WL_EXPORT pixman_box32_t
893weston_surface_to_buffer_rect(struct weston_surface *surface,
894 pixman_box32_t rect)
895{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200896 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100897 float xf, yf;
898
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300899 /* first transform box coordinates if the viewport is set */
900 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100901 rect.x1 = floorf(xf);
902 rect.y1 = floorf(yf);
903
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300904 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600905 rect.x2 = ceilf(xf);
906 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100907
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500908 return weston_transformed_rect(surface->width_from_buffer,
909 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200910 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200911 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200912}
913
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200914/** Transform a region from surface coordinates to buffer coordinates
915 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300916 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200917 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300918 * \param[in] surface_region The region in surface coordinates.
919 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200920 *
921 * Buffer_region must be init'd, but will be completely overwritten.
922 *
923 * Viewport and buffer transformations can only do translation, scaling,
924 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600925 * conversion is from the coordinate rounding that takes place in
926 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300927 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200928 */
929WL_EXPORT void
930weston_surface_to_buffer_region(struct weston_surface *surface,
931 pixman_region32_t *surface_region,
932 pixman_region32_t *buffer_region)
933{
934 pixman_box32_t *src_rects, *dest_rects;
935 int nrects, i;
936
937 src_rects = pixman_region32_rectangles(surface_region, &nrects);
938 dest_rects = malloc(nrects * sizeof(*dest_rects));
939 if (!dest_rects)
940 return;
941
942 for (i = 0; i < nrects; i++) {
943 dest_rects[i] = weston_surface_to_buffer_rect(surface,
944 src_rects[i]);
945 }
946
947 pixman_region32_fini(buffer_region);
948 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
949 free(dest_rects);
950}
951
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200952WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500953weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400954 struct weston_plane *plane)
955{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500956 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400957 return;
958
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 weston_view_damage_below(view);
960 view->plane = plane;
961 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400962}
963
Pekka Paalanen51723d52015-02-17 13:10:01 +0200964/** Inflict damage on the plane where the view is visible.
965 *
966 * \param view The view that causes the damage.
967 *
968 * If the view is currently on a plane (including the primary plane),
969 * take the view's boundingbox, subtract all the opaque views that cover it,
970 * and add the remaining region as damage to the plane. This corresponds
971 * to the damage inflicted to the plane if this view disappeared.
972 *
973 * A repaint is scheduled for this view.
974 *
975 * The region of all opaque views covering this view is stored in
976 * weston_view::clip and updated by view_accumulate_damage() during
977 * weston_output_repaint(). Specifically, that region matches the
978 * scenegraph as it was last painted.
979 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400980WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500981weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200982{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500983 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200984
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500985 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200986 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800988 if (view->plane)
989 pixman_region32_union(&view->plane->damage,
990 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500991 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800992 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200993}
994
Pekka Paalanen01e00682017-03-24 16:21:06 +0200995/** Send wl_surface.enter/leave events
996 *
997 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300998 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200999 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001000 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001001 *
1002 * Send the enter/leave events for all protocol objects bound to the given
1003 * output by the client owning the surface.
1004 */
1005static void
1006weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001007 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001008 bool enter,
1009 bool leave)
1010{
1011 struct wl_resource *wloutput;
1012 struct wl_client *client;
1013
1014 assert(enter != leave);
1015
1016 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001017 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001018 if (wl_resource_get_client(wloutput) != client)
1019 continue;
1020
1021 if (enter)
1022 wl_surface_send_enter(surface->resource, wloutput);
1023 if (leave)
1024 wl_surface_send_leave(surface->resource, wloutput);
1025 }
1026}
1027
Bryce Harrington3f650b82015-12-23 11:01:58 -08001028/**
1029 * \param es The surface
1030 * \param mask The new set of outputs for the surface
1031 *
1032 * Sets the surface's set of outputs to the ones specified by
1033 * the new output mask provided. Identifies the outputs that
1034 * have changed, the posts enter and leave events for these
1035 * outputs as appropriate.
1036 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001037static void
1038weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1039{
1040 uint32_t different = es->output_mask ^ mask;
1041 uint32_t entered = mask & different;
1042 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001043 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001044 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001045 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001046
1047 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001048 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001049 return;
1050 if (different == 0)
1051 return;
1052
1053 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001054 output_bit = 1u << output->id;
1055 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001056 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001057
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001058 wl_list_for_each(head, &output->head_list, output_link) {
1059 weston_surface_send_enter_leave(es, head,
1060 output_bit & entered,
1061 output_bit & left);
1062 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001063 }
1064}
1065
Semi Malinene7a52fb2018-04-26 11:08:10 +02001066static void
1067notify_view_output_destroy(struct wl_listener *listener, void *data)
1068{
1069 struct weston_view *view =
1070 container_of(listener,
1071 struct weston_view, output_destroy_listener);
1072
1073 view->output = NULL;
1074 view->output_destroy_listener.notify = NULL;
1075}
1076
1077/** Set the primary output of the view
1078 *
1079 * \param view The view whose primary output to set
1080 * \param output The new primary output for the view
1081 *
1082 * Set \a output to be the primary output of the \a view.
1083 *
1084 * Notice that the assignment may be temporary; the primary output could be
1085 * automatically changed. Hence, one cannot rely on the value persisting.
1086 *
1087 * Passing NULL as /a output will set the primary output to NULL.
1088 */
1089WL_EXPORT void
1090weston_view_set_output(struct weston_view *view, struct weston_output *output)
1091{
1092 if (view->output_destroy_listener.notify) {
1093 wl_list_remove(&view->output_destroy_listener.link);
1094 view->output_destroy_listener.notify = NULL;
1095 }
1096 view->output = output;
1097 if (output) {
1098 view->output_destroy_listener.notify =
1099 notify_view_output_destroy;
1100 wl_signal_add(&output->destroy_signal,
1101 &view->output_destroy_listener);
1102 }
1103}
1104
Bryce Harrington3f650b82015-12-23 11:01:58 -08001105/** Recalculate which output(s) the surface has views displayed on
1106 *
1107 * \param es The surface to remap to outputs
1108 *
1109 * Finds the output that is showing the largest amount of one
1110 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001111 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001112 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001113 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001114 * in the output_mask for the surface.
1115 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001116static void
1117weston_surface_assign_output(struct weston_surface *es)
1118{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 struct weston_output *new_output;
1120 struct weston_view *view;
1121 pixman_region32_t region;
1122 uint32_t max, area, mask;
1123 pixman_box32_t *e;
1124
1125 new_output = NULL;
1126 max = 0;
1127 mask = 0;
1128 pixman_region32_init(&region);
1129 wl_list_for_each(view, &es->views, surface_link) {
1130 if (!view->output)
1131 continue;
1132
1133 pixman_region32_intersect(&region, &view->transform.boundingbox,
1134 &view->output->region);
1135
1136 e = pixman_region32_extents(&region);
1137 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1138
1139 mask |= view->output_mask;
1140
1141 if (area >= max) {
1142 new_output = view->output;
1143 max = area;
1144 }
1145 }
1146 pixman_region32_fini(&region);
1147
1148 es->output = new_output;
1149 weston_surface_update_output_mask(es, mask);
1150}
1151
Bryce Harrington3f650b82015-12-23 11:01:58 -08001152/** Recalculate which output(s) the view is displayed on
1153 *
1154 * \param ev The view to remap to outputs
1155 *
1156 * Identifies the set of outputs that the view is visible on,
1157 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001158 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001159 *
1160 * Also does the same for the view's surface. See
1161 * weston_surface_assign_output().
1162 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163static void
1164weston_view_assign_output(struct weston_view *ev)
1165{
1166 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001167 struct weston_output *output, *new_output;
1168 pixman_region32_t region;
1169 uint32_t max, area, mask;
1170 pixman_box32_t *e;
1171
1172 new_output = NULL;
1173 max = 0;
1174 mask = 0;
1175 pixman_region32_init(&region);
1176 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001177 if (output->destroying)
1178 continue;
1179
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001181 &output->region);
1182
1183 e = pixman_region32_extents(&region);
1184 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1185
1186 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001187 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001188
1189 if (area >= max) {
1190 new_output = output;
1191 max = area;
1192 }
1193 }
1194 pixman_region32_fini(&region);
1195
Semi Malinene7a52fb2018-04-26 11:08:10 +02001196 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197 ev->output_mask = mask;
1198
1199 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001200}
1201
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001202static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001203weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1204 int from_x, int from_y, int *to_x, int *to_y)
1205{
1206 float x, y;
1207
1208 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1209 weston_view_from_global_float(to, x, y, &x, &y);
1210
1211 *to_x = round(x);
1212 *to_y = round(y);
1213}
1214
1215static void
1216weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1217{
1218 pixman_box32_t *a;
1219 pixman_box32_t b;
1220
1221 a = pixman_region32_extents(&from->geometry.scissor);
1222
1223 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1224 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1225
1226 pixman_region32_fini(&to->geometry.scissor);
1227 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1228}
1229
1230static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001231view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001233{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001234 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1235 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001236 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001237 { inbox->x1, inbox->y1 },
1238 { inbox->x1, inbox->y2 },
1239 { inbox->x2, inbox->y1 },
1240 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001241 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001242 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001243 int i;
1244
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001245 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001246 /* avoid rounding empty bbox to 1x1 */
1247 pixman_region32_init(bbox);
1248 return;
1249 }
1250
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001251 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001252 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001254 if (x < min_x)
1255 min_x = x;
1256 if (x > max_x)
1257 max_x = x;
1258 if (y < min_y)
1259 min_y = y;
1260 if (y > max_y)
1261 max_y = y;
1262 }
1263
Pekka Paalanen219b9822012-02-08 15:38:37 +02001264 int_x = floorf(min_x);
1265 int_y = floorf(min_y);
1266 pixman_region32_init_rect(bbox, int_x, int_y,
1267 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001268}
1269
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001270static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001272{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001274
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001275 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 view->geometry.x = roundf(view->geometry.x);
1277 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001278
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001279 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1281 view->transform.position.matrix.d[12] = view->geometry.x;
1282 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001283
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001285
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 view->transform.inverse = view->transform.position.matrix;
1287 view->transform.inverse.d[12] = -view->geometry.x;
1288 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001289
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001291 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001292 view->surface->width,
1293 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001294 if (view->geometry.scissor_enabled)
1295 pixman_region32_intersect(&view->transform.boundingbox,
1296 &view->transform.boundingbox,
1297 &view->geometry.scissor);
1298
1299 pixman_region32_translate(&view->transform.boundingbox,
1300 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001301
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 if (view->alpha == 1.0) {
1303 pixman_region32_copy(&view->transform.opaque,
1304 &view->surface->opaque);
1305 pixman_region32_translate(&view->transform.opaque,
1306 view->geometry.x,
1307 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001308 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001309}
1310
1311static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001313{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 struct weston_view *parent = view->geometry.parent;
1315 struct weston_matrix *matrix = &view->transform.matrix;
1316 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001317 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001318 pixman_region32_t surfregion;
1319 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001320
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001322
1323 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1325 view->transform.position.matrix.d[12] = view->geometry.x;
1326 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001327
1328 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001330 weston_matrix_multiply(matrix, &tform->matrix);
1331
Pekka Paalanen483243f2013-03-08 14:56:50 +02001332 if (parent)
1333 weston_matrix_multiply(matrix, &parent->transform.matrix);
1334
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001335 if (weston_matrix_invert(inverse, matrix) < 0) {
1336 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 weston_log("error: weston_view %p"
1338 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001339 return -1;
1340 }
1341
Giulio Camuffo148c1992016-09-04 18:50:46 +03001342 if (view->alpha == 1.0 &&
1343 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1344 pixman_region32_copy(&view->transform.opaque,
1345 &view->surface->opaque);
1346 pixman_region32_translate(&view->transform.opaque,
1347 matrix->d[12],
1348 matrix->d[13]);
1349 }
1350
Pekka Paalanen380adf52015-02-16 14:39:11 +02001351 pixman_region32_init_rect(&surfregion, 0, 0,
1352 view->surface->width, view->surface->height);
1353 if (view->geometry.scissor_enabled)
1354 pixman_region32_intersect(&surfregion, &surfregion,
1355 &view->geometry.scissor);
1356 surfbox = pixman_region32_extents(&surfregion);
1357
1358 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1359 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001360
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001361 return 0;
1362}
1363
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001364static struct weston_layer *
1365get_view_layer(struct weston_view *view)
1366{
1367 if (view->parent_view)
1368 return get_view_layer(view->parent_view);
1369 return view->layer_link.layer;
1370}
1371
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001372WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001374{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001376 struct weston_layer *layer;
1377 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001378
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001380 return;
1381
Pekka Paalanen483243f2013-03-08 14:56:50 +02001382 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001384
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001386
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001388
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 pixman_region32_fini(&view->transform.boundingbox);
1390 pixman_region32_fini(&view->transform.opaque);
1391 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001392
Pekka Paalanencd403622012-01-25 13:37:39 +02001393 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001394 if (view->geometry.transformation_list.next ==
1395 &view->transform.position.link &&
1396 view->geometry.transformation_list.prev ==
1397 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001398 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001400 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 if (weston_view_update_transform_enable(view) < 0)
1402 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001403 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001404
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001405 layer = get_view_layer(view);
1406 if (layer) {
1407 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001408 pixman_region32_intersect(&view->transform.boundingbox,
1409 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001410 pixman_region32_intersect(&view->transform.opaque,
1411 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001412 pixman_region32_fini(&mask);
1413 }
1414
Pekka Paalanen380adf52015-02-16 14:39:11 +02001415 if (parent) {
1416 if (parent->geometry.scissor_enabled) {
1417 view->geometry.scissor_enabled = true;
1418 weston_view_transfer_scissor(parent, view);
1419 } else {
1420 view->geometry.scissor_enabled = false;
1421 }
1422 }
1423
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001425
Jason Ekstranda7af7042013-10-12 22:38:11 -05001426 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001427
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 wl_signal_emit(&view->surface->compositor->transform_signal,
1429 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001430}
1431
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001432WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001434{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001436
1437 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 * The invariant: if view->geometry.dirty, then all views
1439 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001440 * Corollary: if not parent->geometry.dirty, then all ancestors
1441 * are not dirty.
1442 */
1443
Jason Ekstranda7af7042013-10-12 22:38:11 -05001444 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001445 return;
1446
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001448
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001450 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001452}
1453
1454WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455weston_view_to_global_fixed(struct weston_view *view,
1456 wl_fixed_t vx, wl_fixed_t vy,
1457 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001458{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001459 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001460
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 weston_view_to_global_float(view,
1462 wl_fixed_to_double(vx),
1463 wl_fixed_to_double(vy),
1464 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001465 *x = wl_fixed_from_double(xf);
1466 *y = wl_fixed_from_double(yf);
1467}
1468
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001469WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470weston_view_from_global_float(struct weston_view *view,
1471 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001472{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001474 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1475
Jason Ekstranda7af7042013-10-12 22:38:11 -05001476 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001477
1478 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001479 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001481 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001482 *vx = 0;
1483 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001484 return;
1485 }
1486
Jason Ekstranda7af7042013-10-12 22:38:11 -05001487 *vx = v.f[0] / v.f[3];
1488 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001489 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490 *vx = x - view->geometry.x;
1491 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001492 }
1493}
1494
1495WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496weston_view_from_global_fixed(struct weston_view *view,
1497 wl_fixed_t x, wl_fixed_t y,
1498 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001499{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001500 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001501
Jason Ekstranda7af7042013-10-12 22:38:11 -05001502 weston_view_from_global_float(view,
1503 wl_fixed_to_double(x),
1504 wl_fixed_to_double(y),
1505 &vxf, &vyf);
1506 *vx = wl_fixed_from_double(vxf);
1507 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001508}
1509
1510WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511weston_view_from_global(struct weston_view *view,
1512 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001513{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001515
Jason Ekstranda7af7042013-10-12 22:38:11 -05001516 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1517 *vx = floorf(vxf);
1518 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001519}
1520
Bryce Harrington3f650b82015-12-23 11:01:58 -08001521/**
1522 * \param surface The surface to be repainted
1523 *
1524 * Marks the output(s) that the surface is shown on as needing to be
1525 * repainted. See weston_output_schedule_repaint().
1526 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001527WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001528weston_surface_schedule_repaint(struct weston_surface *surface)
1529{
1530 struct weston_output *output;
1531
1532 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001533 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001534 weston_output_schedule_repaint(output);
1535}
1536
Bryce Harrington3f650b82015-12-23 11:01:58 -08001537/**
1538 * \param view The view to be repainted
1539 *
1540 * Marks the output(s) that the view is shown on as needing to be
1541 * repainted. See weston_output_schedule_repaint().
1542 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001543WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001544weston_view_schedule_repaint(struct weston_view *view)
1545{
1546 struct weston_output *output;
1547
1548 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001549 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550 weston_output_schedule_repaint(output);
1551}
1552
Pekka Paalanene508ce62015-02-19 13:59:55 +02001553/**
1554 * XXX: This function does it the wrong way.
1555 * surface->damage is the damage from the client, and causes
1556 * surface_flush_damage() to copy pixels. No window management action can
1557 * cause damage to the client-provided content, warranting re-upload!
1558 *
1559 * Instead of surface->damage, this function should record the damage
1560 * with all the views for this surface to avoid extraneous texture
1561 * uploads.
1562 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001563WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001564weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001565{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001566 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001567 0, 0, surface->width,
1568 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001569
Kristian Høgsberg98238702012-08-03 16:29:12 -04001570 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001571}
1572
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001573WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001575{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001576 if (view->geometry.x == x && view->geometry.y == y)
1577 return;
1578
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 view->geometry.x = x;
1580 view->geometry.y = y;
1581 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001582}
1583
Pekka Paalanen483243f2013-03-08 14:56:50 +02001584static void
1585transform_parent_handle_parent_destroy(struct wl_listener *listener,
1586 void *data)
1587{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 struct weston_view *view =
1589 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001590 geometry.parent_destroy_listener);
1591
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001593}
1594
1595WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001596weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001597 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001598{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599 if (view->geometry.parent) {
1600 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1601 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001602
1603 if (!parent)
1604 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001605 }
1606
Jason Ekstranda7af7042013-10-12 22:38:11 -05001607 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001608
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001610 transform_parent_handle_parent_destroy;
1611 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001612 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001613 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001614 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001615 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001616 }
1617
Jason Ekstranda7af7042013-10-12 22:38:11 -05001618 weston_view_geometry_dirty(view);
1619}
1620
Pekka Paalanen380adf52015-02-16 14:39:11 +02001621/** Set a clip mask rectangle on a view
1622 *
1623 * \param view The view to set the clip mask on.
1624 * \param x Top-left corner X coordinate of the clip rectangle.
1625 * \param y Top-left corner Y coordinate of the clip rectangle.
1626 * \param width Width of the clip rectangle, non-negative.
1627 * \param height Height of the clip rectangle, non-negative.
1628 *
1629 * A shell may set a clip mask rectangle on a view. Everything outside
1630 * the rectangle is cut away for input and output purposes: it is
1631 * not drawn and cannot be hit by hit-test based input like pointer
1632 * motion or touch-downs. Everything inside the rectangle will behave
1633 * normally. Clients are unaware of clipping.
1634 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001635 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001636 * mask rectangle does not affect the view position, the view is positioned
1637 * as it would be without a clip. The clip also does not change
1638 * weston_surface::width,height.
1639 *
1640 * The clip mask rectangle is part of transformation inheritance
1641 * (weston_view_set_transform_parent()). A clip set in the root of the
1642 * transformation inheritance tree will affect all views in the tree.
1643 * A clip can be set only on the root view. Attempting to set a clip
1644 * on view that has a transformation parent will fail. Assigning a parent
1645 * to a view that has a clip set will cause the clip to be forgotten.
1646 *
1647 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1648 * on the additional transformations in the child views. These transformations
1649 * may not rotate the coordinate axes, i.e., only translation and scaling
1650 * are allowed. Violating this restriction causes the clipping to malfunction.
1651 * Furthermore, using scaling may cause rounding errors in child clipping.
1652 *
1653 * The clip mask rectangle is not automatically adjusted based on
1654 * wl_surface.attach dx and dy arguments.
1655 *
1656 * A clip mask rectangle can be set only if the compositor capability
1657 * WESTON_CAP_VIEW_CLIP_MASK is present.
1658 *
1659 * This function sets the clip mask rectangle and schedules a repaint for
1660 * the view.
1661 */
1662WL_EXPORT void
1663weston_view_set_mask(struct weston_view *view,
1664 int x, int y, int width, int height)
1665{
1666 struct weston_compositor *compositor = view->surface->compositor;
1667
1668 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1669 weston_log("%s not allowed without capability!\n", __func__);
1670 return;
1671 }
1672
1673 if (view->geometry.parent) {
1674 weston_log("view %p has a parent, clip forbidden!\n", view);
1675 return;
1676 }
1677
1678 if (width < 0 || height < 0) {
1679 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1680 x, y, width, height);
1681 return;
1682 }
1683
1684 pixman_region32_fini(&view->geometry.scissor);
1685 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1686 view->geometry.scissor_enabled = true;
1687 weston_view_geometry_dirty(view);
1688 weston_view_schedule_repaint(view);
1689}
1690
1691/** Remove the clip mask from a view
1692 *
1693 * \param view The view to remove the clip mask from.
1694 *
1695 * Removed the clip mask rectangle and schedules a repaint.
1696 *
1697 * \sa weston_view_set_mask
1698 */
1699WL_EXPORT void
1700weston_view_set_mask_infinite(struct weston_view *view)
1701{
1702 view->geometry.scissor_enabled = false;
1703 weston_view_geometry_dirty(view);
1704 weston_view_schedule_repaint(view);
1705}
1706
Armin Krezović0da12b82016-06-30 06:04:33 +02001707/* Check if view should be displayed
1708 *
1709 * The indicator is set manually when assigning
1710 * a view to a surface.
1711 *
1712 * This needs reworking. See the thread starting at:
1713 *
1714 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1715 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001716WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001717weston_view_is_mapped(struct weston_view *view)
1718{
Armin Krezović0da12b82016-06-30 06:04:33 +02001719 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001720}
1721
Philipp Zabel70decd52018-09-03 20:11:15 +02001722/* Check if view is opaque in specified region
1723 *
1724 * \param view The view to check for opacity.
1725 * \param region The region to check for opacity, in view coordinates.
1726 *
1727 * Returns true if the view is opaque in the specified region, because view
1728 * alpha is 1.0 and either the opaque region set by the client contains the
1729 * specified region, or the buffer pixel format or solid color is opaque.
1730 */
1731WL_EXPORT bool
1732weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1733{
1734 pixman_region32_t r;
1735 bool ret = false;
1736
1737 if (ev->alpha < 1.0)
1738 return false;
1739
1740 if (ev->surface->is_opaque)
1741 return true;
1742
1743 if (ev->transform.dirty) {
1744 weston_log("%s: transform dirty", __func__);
1745 return false;
1746 }
1747
1748 pixman_region32_init(&r);
1749 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1750
1751 if (!pixman_region32_not_empty(&r))
1752 ret = true;
1753
1754 pixman_region32_fini(&r);
1755
1756 return ret;
1757}
1758
Armin Krezović0da12b82016-06-30 06:04:33 +02001759/* Check if a surface has a view assigned to it
1760 *
1761 * The indicator is set manually when mapping
1762 * a surface and creating a view for it.
1763 *
1764 * This needs to go. See the thread starting at:
1765 *
1766 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1767 *
1768 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001769WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001770weston_surface_is_mapped(struct weston_surface *surface)
1771{
Armin Krezović0da12b82016-06-30 06:04:33 +02001772 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001773}
1774
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001775static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001776surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001777{
1778 struct weston_view *view;
1779
1780 if (surface->width == width && surface->height == height)
1781 return;
1782
1783 surface->width = width;
1784 surface->height = height;
1785
1786 wl_list_for_each(view, &surface->views, surface_link)
1787 weston_view_geometry_dirty(view);
1788}
1789
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001790WL_EXPORT void
1791weston_surface_set_size(struct weston_surface *surface,
1792 int32_t width, int32_t height)
1793{
1794 assert(!surface->resource);
1795 surface_set_size(surface, width, height);
1796}
1797
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001798static int
1799fixed_round_up_to_int(wl_fixed_t f)
1800{
1801 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1802}
1803
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001804static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001805convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1806 int32_t width, int32_t height,
1807 uint32_t transform,
1808 int32_t scale)
1809{
1810 assert(scale > 0);
1811
1812 switch (transform) {
1813 case WL_OUTPUT_TRANSFORM_NORMAL:
1814 case WL_OUTPUT_TRANSFORM_180:
1815 case WL_OUTPUT_TRANSFORM_FLIPPED:
1816 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1817 *width_out = width / scale;
1818 *height_out = height / scale;
1819 break;
1820 case WL_OUTPUT_TRANSFORM_90:
1821 case WL_OUTPUT_TRANSFORM_270:
1822 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1823 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1824 *width_out = height / scale;
1825 *height_out = width / scale;
1826 break;
1827 default:
1828 assert(0 && "invalid transform");
1829 }
1830}
1831
1832static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001833weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001834{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001835 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001836
1837 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001838 surface->width_from_buffer = 0;
1839 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001840 return;
1841 }
1842
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001843 convert_size_by_transform_scale(&surface->width_from_buffer,
1844 &surface->height_from_buffer,
1845 surface->buffer_ref.buffer->width,
1846 surface->buffer_ref.buffer->height,
1847 vp->buffer.transform,
1848 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001849}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001850
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001851static void
1852weston_surface_update_size(struct weston_surface *surface)
1853{
1854 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1855 int32_t width, height;
1856
1857 width = surface->width_from_buffer;
1858 height = surface->height_from_buffer;
1859
1860 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001861 surface_set_size(surface,
1862 vp->surface.width, vp->surface.height);
1863 return;
1864 }
1865
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001866 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001867 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1868 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1869
1870 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001871 return;
1872 }
1873
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001874 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001875}
1876
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001877/** weston_compositor_get_time
1878 * \ingroup compositor
1879 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001880WL_EXPORT void
1881weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001882{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001883 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001884}
1885
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001886/** weston_compositor_pick_view
1887 * \ingroup compositor
1888 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001889WL_EXPORT struct weston_view *
1890weston_compositor_pick_view(struct weston_compositor *compositor,
1891 wl_fixed_t x, wl_fixed_t y,
1892 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001893{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001894 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001895 wl_fixed_t view_x, view_y;
1896 int view_ix, view_iy;
1897 int ix = wl_fixed_to_int(x);
1898 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001899
Jason Ekstranda7af7042013-10-12 22:38:11 -05001900 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001901 if (!pixman_region32_contains_point(
1902 &view->transform.boundingbox, ix, iy, NULL))
1903 continue;
1904
1905 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1906 view_ix = wl_fixed_to_int(view_x);
1907 view_iy = wl_fixed_to_int(view_y);
1908
1909 if (!pixman_region32_contains_point(&view->surface->input,
1910 view_ix, view_iy, NULL))
1911 continue;
1912
Pekka Paalanen380adf52015-02-16 14:39:11 +02001913 if (view->geometry.scissor_enabled &&
1914 !pixman_region32_contains_point(&view->geometry.scissor,
1915 view_ix, view_iy, NULL))
1916 continue;
1917
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001918 *vx = view_x;
1919 *vy = view_y;
1920 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001921 }
1922
Derek Foremanf9318d12015-05-11 15:40:11 -05001923 *vx = wl_fixed_from_int(-1000000);
1924 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001925 return NULL;
1926}
1927
1928static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001929weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001930{
Daniel Stone37816df2012-05-16 18:45:18 +01001931 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001932
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001933 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001934 return;
1935
Daniel Stone37816df2012-05-16 18:45:18 +01001936 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001937 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001938}
1939
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001940WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001941weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001942{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001943 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001944
Jason Ekstranda7af7042013-10-12 22:38:11 -05001945 if (!weston_view_is_mapped(view))
1946 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001947
Jason Ekstranda7af7042013-10-12 22:38:11 -05001948 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02001949 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08001950 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001951 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001952 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001953 wl_list_remove(&view->link);
1954 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001955 view->output_mask = 0;
1956 weston_surface_assign_output(view->surface);
1957
1958 if (weston_surface_is_mapped(view->surface))
1959 return;
1960
1961 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001962 struct weston_touch *touch = weston_seat_get_touch(seat);
1963 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1964 struct weston_keyboard *keyboard =
1965 weston_seat_get_keyboard(seat);
1966
1967 if (keyboard && keyboard->focus == view->surface)
1968 weston_keyboard_set_focus(keyboard, NULL);
1969 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001970 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001971 if (touch && touch->focus == view)
1972 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001973 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001974}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001975
Jason Ekstranda7af7042013-10-12 22:38:11 -05001976WL_EXPORT void
1977weston_surface_unmap(struct weston_surface *surface)
1978{
1979 struct weston_view *view;
1980
Armin Krezovićf8486c32016-06-30 06:04:28 +02001981 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001982 wl_list_for_each(view, &surface->views, surface_link)
1983 weston_view_unmap(view);
1984 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001985}
1986
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001987static void
1988weston_surface_reset_pending_buffer(struct weston_surface *surface)
1989{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001990 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001991 surface->pending.sx = 0;
1992 surface->pending.sy = 0;
1993 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001994 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001995}
1996
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997WL_EXPORT void
1998weston_view_destroy(struct weston_view *view)
1999{
2000 wl_signal_emit(&view->destroy_signal, view);
2001
2002 assert(wl_list_empty(&view->geometry.child_list));
2003
2004 if (weston_view_is_mapped(view)) {
2005 weston_view_unmap(view);
2006 weston_compositor_build_view_list(view->surface->compositor);
2007 }
2008
2009 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002010 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002011
2012 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002013 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002014 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002015 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002016
2017 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002018 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002019
Jason Ekstranda7af7042013-10-12 22:38:11 -05002020 wl_list_remove(&view->surface_link);
2021
2022 free(view);
2023}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002024
2025WL_EXPORT void
2026weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002027{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002028 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002029 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002030 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002031
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002032 if (--surface->ref_count > 0)
2033 return;
2034
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002035 assert(surface->resource == NULL);
2036
Pekka Paalanenca790762015-04-17 14:23:38 +03002037 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002038
Pekka Paalanene67858b2013-04-25 13:57:42 +03002039 assert(wl_list_empty(&surface->subsurface_list_pending));
2040 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002041
Jason Ekstranda7af7042013-10-12 22:38:11 -05002042 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2043 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002044
Jason Ekstrand7b982072014-05-20 14:33:03 -05002045 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002046
Pekka Paalanende685b82012-12-04 15:58:12 +02002047 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002048 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002049
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002050 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002051 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002052 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002053
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002054 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002055 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002056
Pekka Paalanen133e4392014-09-23 22:08:46 -04002057 weston_presentation_feedback_discard_list(&surface->feedback_list);
2058
Jonas Ådahld3414f22016-07-22 17:56:31 +08002059 wl_list_for_each_safe(constraint, next_constraint,
2060 &surface->pointer_constraints,
2061 link)
2062 weston_pointer_constraint_destroy(constraint);
2063
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002064 fd_clear(&surface->acquire_fence_fd);
2065
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002066 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002067}
2068
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002069static void
2070destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002071{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002072 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002073
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002074 assert(surface);
2075
Giulio Camuffo0d379742013-11-15 22:06:15 +01002076 /* Set the resource to NULL, since we don't want to leave a
2077 * dangling pointer if the surface was refcounted and survives
2078 * the weston_surface_destroy() call. */
2079 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002080
2081 if (surface->viewport_resource)
2082 wl_resource_set_user_data(surface->viewport_resource, NULL);
2083
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002084 if (surface->synchronization_resource) {
2085 wl_resource_set_user_data(surface->synchronization_resource,
2086 NULL);
2087 }
2088
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002089 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002090}
2091
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002092static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002093weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2094{
2095 struct weston_buffer *buffer =
2096 container_of(listener, struct weston_buffer, destroy_listener);
2097
2098 wl_signal_emit(&buffer->destroy_signal, buffer);
2099 free(buffer);
2100}
2101
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002102WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002103weston_buffer_from_resource(struct wl_resource *resource)
2104{
2105 struct weston_buffer *buffer;
2106 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002107
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002108 listener = wl_resource_get_destroy_listener(resource,
2109 weston_buffer_destroy_handler);
2110
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002111 if (listener)
2112 return container_of(listener, struct weston_buffer,
2113 destroy_listener);
2114
2115 buffer = zalloc(sizeof *buffer);
2116 if (buffer == NULL)
2117 return NULL;
2118
2119 buffer->resource = resource;
2120 wl_signal_init(&buffer->destroy_signal);
2121 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002122 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002123 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002124
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002125 return buffer;
2126}
2127
2128static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002129weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2130 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002131{
Pekka Paalanende685b82012-12-04 15:58:12 +02002132 struct weston_buffer_reference *ref =
2133 container_of(listener, struct weston_buffer_reference,
2134 destroy_listener);
2135
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002136 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002137 ref->buffer = NULL;
2138}
2139
2140WL_EXPORT void
2141weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002142 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002143{
2144 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002145 ref->buffer->busy_count--;
2146 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002147 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002148 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002149 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002150 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002151 }
2152
Pekka Paalanende685b82012-12-04 15:58:12 +02002153 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002154 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002155 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002156 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002157 }
2158
Pekka Paalanende685b82012-12-04 15:58:12 +02002159 ref->buffer = buffer;
2160 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2161}
2162
2163static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002164weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2165 void *data)
2166{
2167 struct weston_buffer_release_reference *ref =
2168 container_of(listener, struct weston_buffer_release_reference,
2169 destroy_listener);
2170
2171 assert((struct wl_resource *)data == ref->buffer_release->resource);
2172 ref->buffer_release = NULL;
2173}
2174
2175static void
2176weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2177{
2178 struct wl_resource *resource = buffer_release->resource;
2179 int release_fence_fd = buffer_release->fence_fd;
2180
2181 if (release_fence_fd >= 0) {
2182 zwp_linux_buffer_release_v1_send_fenced_release(
2183 resource, release_fence_fd);
2184 } else {
2185 zwp_linux_buffer_release_v1_send_immediate_release(
2186 resource);
2187 }
2188
2189 wl_resource_destroy(resource);
2190}
2191
2192WL_EXPORT void
2193weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2194 struct weston_buffer_release *buffer_release)
2195{
2196 if (buffer_release == ref->buffer_release)
2197 return;
2198
2199 if (ref->buffer_release) {
2200 ref->buffer_release->ref_count--;
2201 wl_list_remove(&ref->destroy_listener.link);
2202 if (ref->buffer_release->ref_count == 0)
2203 weston_buffer_release_destroy(ref->buffer_release);
2204 }
2205
2206 if (buffer_release) {
2207 buffer_release->ref_count++;
2208 wl_resource_add_destroy_listener(buffer_release->resource,
2209 &ref->destroy_listener);
2210 }
2211
2212 ref->buffer_release = buffer_release;
2213 ref->destroy_listener.notify =
2214 weston_buffer_release_reference_handle_destroy;
2215}
2216
2217WL_EXPORT void
2218weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2219 struct weston_buffer_release_reference *src)
2220{
2221 weston_buffer_release_reference(dest, src->buffer_release);
2222 weston_buffer_release_reference(src, NULL);
2223}
2224
2225static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002226weston_surface_attach(struct weston_surface *surface,
2227 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002228{
2229 weston_buffer_reference(&surface->buffer_ref, buffer);
2230
Pekka Paalanena6421c42012-12-04 15:58:10 +02002231 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002232 if (weston_surface_is_mapped(surface))
2233 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002234 }
2235
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002236 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002237
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002238 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002239 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002240}
2241
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002242/** weston_compositor_damage_all
2243 * \ingroup compositor
2244 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002245WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002246weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002247{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002248 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002249
2250 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002251 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002252}
2253
Marius Vlad55d87362019-06-11 01:15:35 +03002254/**
2255 * \ingroup output
2256 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002257WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002258weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002259{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002260 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002261
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002262 pixman_region32_union(&compositor->primary_plane.damage,
2263 &compositor->primary_plane.damage,
2264 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002265 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002266}
2267
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002268static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002269surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002270{
Pekka Paalanende685b82012-12-04 15:58:12 +02002271 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002272 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002273 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002274
Pekka Paalanenb5026542014-11-12 15:09:24 +02002275 if (weston_timeline_enabled_ &&
2276 pixman_region32_not_empty(&surface->damage))
2277 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2278 TLP_OUTPUT(surface->output), TLP_END);
2279
Jason Ekstrandef540082014-06-26 10:37:36 -07002280 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002281}
2282
2283static void
2284view_accumulate_damage(struct weston_view *view,
2285 pixman_region32_t *opaque)
2286{
2287 pixman_region32_t damage;
2288
2289 pixman_region32_init(&damage);
2290 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002291 pixman_box32_t *extents;
2292
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002294 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002295 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002296 pixman_region32_copy(&damage, &view->surface->damage);
2297 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002298 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002299 }
2300
Pekka Paalanen380adf52015-02-16 14:39:11 +02002301 pixman_region32_intersect(&damage, &damage,
2302 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002303 pixman_region32_subtract(&damage, &damage, opaque);
2304 pixman_region32_union(&view->plane->damage,
2305 &view->plane->damage, &damage);
2306 pixman_region32_fini(&damage);
2307 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002308 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002309}
2310
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002311static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002312compositor_accumulate_damage(struct weston_compositor *ec)
2313{
2314 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002315 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002316 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002317
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002318 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002319
2320 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002321 pixman_region32_copy(&plane->clip, &clip);
2322
2323 pixman_region32_init(&opaque);
2324
Jason Ekstranda7af7042013-10-12 22:38:11 -05002325 wl_list_for_each(ev, &ec->view_list, link) {
2326 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002327 continue;
2328
Jason Ekstranda7af7042013-10-12 22:38:11 -05002329 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002330 }
2331
2332 pixman_region32_union(&clip, &clip, &opaque);
2333 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002334 }
2335
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002336 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002337
Jason Ekstranda7af7042013-10-12 22:38:11 -05002338 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002339 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002340
2341 wl_list_for_each(ev, &ec->view_list, link) {
2342 if (ev->surface->touched)
2343 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002344 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002345
2346 surface_flush_damage(ev->surface);
2347
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002348 /* Both the renderer and the backend have seen the buffer
2349 * by now. If renderer needs the buffer, it has its own
2350 * reference set. If the backend wants to keep the buffer
2351 * around for migrating the surface into a non-primary plane
2352 * later, keep_buffer is true. Otherwise, drop the core
2353 * reference now, and allow early buffer release. This enables
2354 * clients to use single-buffering.
2355 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002356 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002357 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002358 weston_buffer_release_reference(
2359 &ev->surface->buffer_release_ref, NULL);
2360 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002361 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002362}
2363
2364static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002365surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002366{
2367 struct weston_subsurface *sub;
2368
Pekka Paalanene67858b2013-04-25 13:57:42 +03002369 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002370 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002371 continue;
2372
Jason Ekstranda7af7042013-10-12 22:38:11 -05002373 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2374 wl_list_init(&sub->surface->views);
2375
2376 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002377 }
2378}
2379
2380static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002381surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002382{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002383 struct weston_subsurface *sub;
2384 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002385
Jason Ekstranda7af7042013-10-12 22:38:11 -05002386 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2387 if (sub->surface == surface)
2388 continue;
2389
George Kiagiadakised04d382014-06-13 18:10:26 +02002390 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2391 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002392 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002393 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002394
2395 surface_free_unused_subsurface_views(sub->surface);
2396 }
2397}
2398
2399static void
2400view_list_add_subsurface_view(struct weston_compositor *compositor,
2401 struct weston_subsurface *sub,
2402 struct weston_view *parent)
2403{
2404 struct weston_subsurface *child;
2405 struct weston_view *view = NULL, *iv;
2406
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002407 if (!weston_surface_is_mapped(sub->surface))
2408 return;
2409
Jason Ekstranda7af7042013-10-12 22:38:11 -05002410 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2411 if (iv->geometry.parent == parent) {
2412 view = iv;
2413 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002414 }
2415 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002416
2417 if (view) {
2418 /* Put it back in the surface's list of views */
2419 wl_list_remove(&view->surface_link);
2420 wl_list_insert(&sub->surface->views, &view->surface_link);
2421 } else {
2422 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002423 weston_view_set_position(view,
2424 sub->position.x,
2425 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002426 weston_view_set_transform_parent(view, parent);
2427 }
2428
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002429 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002430 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002431 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002432
Pekka Paalanenb188e912013-11-19 14:03:35 +02002433 if (wl_list_empty(&sub->surface->subsurface_list)) {
2434 wl_list_insert(compositor->view_list.prev, &view->link);
2435 return;
2436 }
2437
2438 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2439 if (child->surface == sub->surface)
2440 wl_list_insert(compositor->view_list.prev, &view->link);
2441 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002442 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002443 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002444}
2445
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002446/* This recursively adds the sub-surfaces for a view, relying on the
2447 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2448 * change first happens to the sub-surface list, and then automatically
2449 * propagates here. See weston_surface_damage_subsurfaces() for how the
2450 * sub-surfaces receive damage when the client changes the state.
2451 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002452static void
2453view_list_add(struct weston_compositor *compositor,
2454 struct weston_view *view)
2455{
2456 struct weston_subsurface *sub;
2457
2458 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002459
Pekka Paalanenb188e912013-11-19 14:03:35 +02002460 if (wl_list_empty(&view->surface->subsurface_list)) {
2461 wl_list_insert(compositor->view_list.prev, &view->link);
2462 return;
2463 }
2464
2465 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2466 if (sub->surface == view->surface)
2467 wl_list_insert(compositor->view_list.prev, &view->link);
2468 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002469 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002470 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002471}
2472
2473static void
2474weston_compositor_build_view_list(struct weston_compositor *compositor)
2475{
2476 struct weston_view *view;
2477 struct weston_layer *layer;
2478
2479 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002480 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002481 surface_stash_subsurface_views(view->surface);
2482
2483 wl_list_init(&compositor->view_list);
2484 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002485 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002486 view_list_add(compositor, view);
2487 }
2488 }
2489
2490 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002491 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002492 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002493}
2494
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002495static void
2496weston_output_take_feedback_list(struct weston_output *output,
2497 struct weston_surface *surface)
2498{
2499 struct weston_view *view;
2500 struct weston_presentation_feedback *feedback;
2501 uint32_t flags = 0xffffffff;
2502
2503 if (wl_list_empty(&surface->feedback_list))
2504 return;
2505
2506 /* All views must have the flag for the flag to survive. */
2507 wl_list_for_each(view, &surface->views, surface_link) {
2508 /* ignore views that are not on this output at all */
2509 if (view->output_mask & (1u << output->id))
2510 flags &= view->psf_flags;
2511 }
2512
2513 wl_list_for_each(feedback, &surface->feedback_list, link)
2514 feedback->psf_flags = flags;
2515
2516 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2517 wl_list_init(&surface->feedback_list);
2518}
2519
David Herrmann1edf44c2013-10-22 17:11:26 +02002520static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002521weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002522{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002523 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002524 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002525 struct weston_animation *animation, *next;
2526 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002527 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002528 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002529 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002530 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002531
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002532 if (output->destroying)
2533 return 0;
2534
Pekka Paalanenb5026542014-11-12 15:09:24 +02002535 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2536
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002537 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002538 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002539
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002540 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002541 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002542 } else {
2543 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002544 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002545 ev->psf_flags = 0;
2546 }
2547 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002548
Pekka Paalanene67858b2013-04-25 13:57:42 +03002549 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002550 wl_list_for_each(ev, &ec->view_list, link) {
2551 /* Note: This operation is safe to do multiple times on the
2552 * same surface.
2553 */
2554 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002555 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002556 &ev->surface->frame_callback_list);
2557 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002558
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002559 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002560 }
2561 }
2562
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002563 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002564
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002565 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002566 pixman_region32_intersect(&output_damage,
2567 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002568 pixman_region32_subtract(&output_damage,
2569 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002570
Scott Moreauccbf29d2012-02-22 14:21:41 -07002571 if (output->dirty)
2572 weston_output_update_matrix(output);
2573
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002574 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002575
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002576 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002577
Daniel Stone09a97e22017-03-01 11:34:06 +00002578 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002579 if (r == 0)
2580 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002581
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002582 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002583
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002584 frame_time_msec = timespec_to_msec(&output->frame_time);
2585
Jonas Ådahldb773762012-06-13 00:01:21 +02002586 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002587 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002588 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002589 }
2590
Scott Moreaud64cf212012-06-08 19:40:54 -06002591 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002592 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002593 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002594 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002595
Pekka Paalanenb5026542014-11-12 15:09:24 +02002596 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2597
David Herrmann1edf44c2013-10-22 17:11:26 +02002598 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002599}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002600
Pekka Paalanen82919792014-05-21 13:51:49 +03002601static void
2602weston_output_schedule_repaint_reset(struct weston_output *output)
2603{
Daniel Stone05df8c12017-03-03 16:59:42 +00002604 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002605 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002606}
2607
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002608static int
2609weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2610 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002611{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002612 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002613 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002614 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002615
Daniel Stone6847b852017-03-01 11:34:08 +00002616 /* We're not ready yet; come back to make a decision later. */
2617 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002618 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002619
2620 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2621 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002622 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002623
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002624 /* If we're sleeping, drop the repaint machinery entirely; we will
2625 * explicitly repaint all outputs when we come back. */
2626 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2627 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2628 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002629
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002630 /* We don't actually need to repaint this output; drop it from
2631 * repaint until something causes damage. */
2632 if (!output->repaint_needed)
2633 goto err;
2634
2635 /* If repaint fails, we aren't going to get weston_output_finish_frame
2636 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002637 * something schedules a successful repaint later. As repainting may
2638 * take some time, re-read our clock as a courtesy to the next
2639 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002640 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002641 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002642 if (ret != 0)
2643 goto err;
2644
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002645 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002646 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002647
2648err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002649 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002650 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002651}
2652
2653static void
2654output_repaint_timer_arm(struct weston_compositor *compositor)
2655{
2656 struct weston_output *output;
2657 bool any_should_repaint = false;
2658 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002659 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002660
2661 weston_compositor_read_presentation_clock(compositor, &now);
2662
2663 wl_list_for_each(output, &compositor->output_list, link) {
2664 int64_t msec_to_this;
2665
2666 if (output->repaint_status != REPAINT_SCHEDULED)
2667 continue;
2668
2669 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2670 &now);
2671 if (!any_should_repaint || msec_to_this < msec_to_next)
2672 msec_to_next = msec_to_this;
2673
2674 any_should_repaint = true;
2675 }
2676
2677 if (!any_should_repaint)
2678 return;
2679
2680 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2681 * This is a workaround to allow coalescing multiple output repaints
2682 * particularly from weston_output_finish_frame()
2683 * into the same call, which would not happen if we called
2684 * output_repaint_timer_handler() directly.
2685 */
2686 if (msec_to_next < 1)
2687 msec_to_next = 1;
2688
2689 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2690}
2691
2692static int
2693output_repaint_timer_handler(void *data)
2694{
2695 struct weston_compositor *compositor = data;
2696 struct weston_output *output;
2697 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002698 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002699 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002700
2701 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002702
2703 if (compositor->backend->repaint_begin)
2704 repaint_data = compositor->backend->repaint_begin(compositor);
2705
2706 wl_list_for_each(output, &compositor->output_list, link) {
2707 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2708 if (ret)
2709 break;
2710 }
2711
2712 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002713 if (compositor->backend->repaint_flush)
2714 compositor->backend->repaint_flush(compositor,
2715 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002716 } else {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002717 wl_list_for_each(output, &compositor->output_list, link) {
2718 if (output->repainted)
2719 weston_output_schedule_repaint_reset(output);
2720 }
2721
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002722 if (compositor->backend->repaint_cancel)
2723 compositor->backend->repaint_cancel(compositor,
2724 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002725 }
Daniel Stone6847b852017-03-01 11:34:08 +00002726
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002727 wl_list_for_each(output, &compositor->output_list, link)
2728 output->repainted = false;
2729
Daniel Stone6847b852017-03-01 11:34:08 +00002730 output_repaint_timer_arm(compositor);
2731
Pekka Paalanen0513a952014-05-21 16:17:27 +03002732 return 0;
2733}
2734
Marius Vlad55d87362019-06-11 01:15:35 +03002735/**
2736 * \ingroup output
2737 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002738WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002739weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002740 const struct timespec *stamp,
2741 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002742{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002743 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002744 int32_t refresh_nsec;
2745 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002746 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002747
Pekka Paalanenb5026542014-11-12 15:09:24 +02002748
Daniel Stone05df8c12017-03-03 16:59:42 +00002749 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002750 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2751
Daniel Stone6847b852017-03-01 11:34:08 +00002752 weston_compositor_read_presentation_clock(compositor, &now);
2753
Daniel Stone3615ce12017-03-01 11:34:05 +00002754 /* If we haven't been supplied any timestamp at all, we don't have a
2755 * timebase to work against, so any delay just wastes time. Push a
2756 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002757 if (!stamp) {
2758 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002759 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002760 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002761
Marius Vladdf9278a2018-03-06 18:56:23 +02002762 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2763 TLP_VBLANK(stamp), TLP_END);
2764
Pekka Paalanend7894d02015-07-03 15:08:53 +03002765 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002766 weston_presentation_feedback_present_list(&output->feedback_list,
2767 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002768 output->msc,
2769 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002770
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002771 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002772
Daniel Stone6847b852017-03-01 11:34:08 +00002773 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2774 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2775 -compositor->repaint_msec);
2776 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002777
2778 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002779 static bool warned;
2780
2781 if (!warned)
2782 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002783 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002784 warned = true;
2785
Daniel Stone6847b852017-03-01 11:34:08 +00002786 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002787 }
2788
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002789 /* Called from restart_repaint_loop and restart happens already after
2790 * the deadline given by repaint_msec? In that case we delay until
2791 * the deadline of the next frame, to give clients a more predictable
2792 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002793 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2794 msec_rel < 0) {
2795 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2796 timespec_add_nsec(&output->next_repaint,
2797 &output->next_repaint,
2798 refresh_nsec);
2799 }
2800 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002801
Daniel Stone3615ce12017-03-01 11:34:05 +00002802out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002803 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002804 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002805}
2806
2807static void
2808idle_repaint(void *data)
2809{
2810 struct weston_output *output = data;
2811
Daniel Stone05df8c12017-03-03 16:59:42 +00002812 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2813 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002814 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002815 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002816}
2817
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002818WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002819weston_layer_entry_insert(struct weston_layer_entry *list,
2820 struct weston_layer_entry *entry)
2821{
2822 wl_list_insert(&list->link, &entry->link);
2823 entry->layer = list->layer;
2824}
2825
2826WL_EXPORT void
2827weston_layer_entry_remove(struct weston_layer_entry *entry)
2828{
2829 wl_list_remove(&entry->link);
2830 wl_list_init(&entry->link);
2831 entry->layer = NULL;
2832}
2833
Quentin Glidic82681572016-12-17 13:40:51 +01002834
2835/** Initialize the weston_layer struct.
2836 *
2837 * \param compositor The compositor instance
2838 * \param layer The layer to initialize
2839 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002840WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002841weston_layer_init(struct weston_layer *layer,
2842 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002843{
Quentin Glidic82681572016-12-17 13:40:51 +01002844 layer->compositor = compositor;
2845 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002846 wl_list_init(&layer->view_list.link);
2847 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002848 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002849}
2850
2851/** Sets the position of the layer in the layer list. The layer will be placed
2852 * below any layer with the same position value, if any.
2853 * This function is safe to call if the layer is already on the list, but the
2854 * layer may be moved below other layers at the same position, if any.
2855 *
2856 * \param layer The layer to modify
2857 * \param position The position the layer will be placed at
2858 */
2859WL_EXPORT void
2860weston_layer_set_position(struct weston_layer *layer,
2861 enum weston_layer_position position)
2862{
2863 struct weston_layer *below;
2864
2865 wl_list_remove(&layer->link);
2866
2867 /* layer_list is ordered from top to bottom, the last layer being the
2868 * background with the smallest position value */
2869
2870 layer->position = position;
2871 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2872 if (below->position >= layer->position) {
2873 wl_list_insert(&below->link, &layer->link);
2874 return;
2875 }
2876 }
2877 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2878}
2879
2880/** Hide a layer by taking it off the layer list.
2881 * This function is safe to call if the layer is not on the list.
2882 *
2883 * \param layer The layer to hide
2884 */
2885WL_EXPORT void
2886weston_layer_unset_position(struct weston_layer *layer)
2887{
2888 wl_list_remove(&layer->link);
2889 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002890}
2891
2892WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002893weston_layer_set_mask(struct weston_layer *layer,
2894 int x, int y, int width, int height)
2895{
2896 struct weston_view *view;
2897
2898 layer->mask.x1 = x;
2899 layer->mask.x2 = x + width;
2900 layer->mask.y1 = y;
2901 layer->mask.y2 = y + height;
2902
2903 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2904 weston_view_geometry_dirty(view);
2905 }
2906}
2907
2908WL_EXPORT void
2909weston_layer_set_mask_infinite(struct weston_layer *layer)
2910{
2911 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2912 UINT32_MAX, UINT32_MAX);
2913}
2914
Daniel Stone3b775632018-07-20 08:38:25 +01002915WL_EXPORT bool
2916weston_layer_mask_is_infinite(struct weston_layer *layer)
2917{
2918 return layer->mask.x1 == INT32_MIN &&
2919 layer->mask.y1 == INT32_MIN &&
2920 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
2921 layer->mask.y2 == INT32_MIN + UINT32_MAX;
2922}
2923
Marius Vlad55d87362019-06-11 01:15:35 +03002924/**
2925 * \ingroup output
2926 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002927WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002928weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002929{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002930 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002931 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002932
Bryce Harrington08976ac2016-08-30 12:05:16 -07002933 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2934 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002935 return;
2936
Pekka Paalanenb5026542014-11-12 15:09:24 +02002937 if (!output->repaint_needed)
2938 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2939
Kristian Høgsbergef044142011-06-21 15:02:12 -04002940 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002941 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002942
2943 /* If we already have a repaint scheduled for our idle handler,
2944 * no need to set it again. If the repaint has been called but
2945 * not finished, then weston_output_finish_frame() will notice
2946 * that a repaint is needed and schedule one. */
2947 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002948 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002949
Daniel Stone05df8c12017-03-03 16:59:42 +00002950 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002951 assert(!output->idle_repaint_source);
2952 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2953 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002954 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002955}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002956
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002957/** weston_compositor_schedule_repaint
2958 * \ingroup compositor
2959 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002960WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002961weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2962{
2963 struct weston_output *output;
2964
2965 wl_list_for_each(output, &compositor->output_list, link)
2966 weston_output_schedule_repaint(output);
2967}
2968
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002969static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002970surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002971{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002972 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002973}
2974
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002975static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002976surface_attach(struct wl_client *client,
2977 struct wl_resource *resource,
2978 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2979{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002980 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002981 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002982
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002983 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002984 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002985 if (buffer == NULL) {
2986 wl_client_post_no_memory(client);
2987 return;
2988 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002989 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002990
Pekka Paalanende685b82012-12-04 15:58:12 +02002991 /* Attach, attach, without commit in between does not send
2992 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002993 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002994
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002995 surface->pending.sx = sx;
2996 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002997 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002998}
2999
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003000static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003001surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003002 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003003 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003004{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003005 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003006
Derek Foreman57e92ed2015-11-17 14:11:35 -06003007 if (width <= 0 || height <= 0)
3008 return;
3009
Derek Foreman152254b2015-11-26 14:17:48 -06003010 pixman_region32_union_rect(&surface->pending.damage_surface,
3011 &surface->pending.damage_surface,
3012 x, y, width, height);
3013}
3014
3015static void
3016surface_damage_buffer(struct wl_client *client,
3017 struct wl_resource *resource,
3018 int32_t x, int32_t y, int32_t width, int32_t height)
3019{
3020 struct weston_surface *surface = wl_resource_get_user_data(resource);
3021
3022 if (width <= 0 || height <= 0)
3023 return;
3024
3025 pixman_region32_union_rect(&surface->pending.damage_buffer,
3026 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003027 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003028}
3029
Kristian Høgsberg33418202011-08-16 23:01:28 -04003030static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003031destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003032{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003033 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003034
3035 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003036 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003037}
3038
3039static void
3040surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003041 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003042{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003043 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003044 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003045
3046 cb = malloc(sizeof *cb);
3047 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003048 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003049 return;
3050 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003051
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003052 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3053 callback);
3054 if (cb->resource == NULL) {
3055 free(cb);
3056 wl_resource_post_no_memory(resource);
3057 return;
3058 }
3059
Jason Ekstranda85118c2013-06-27 20:17:02 -05003060 wl_resource_set_implementation(cb->resource, NULL, cb,
3061 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003062
Pekka Paalanenbc106382012-10-10 12:49:31 +03003063 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003064}
3065
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003066static void
3067surface_set_opaque_region(struct wl_client *client,
3068 struct wl_resource *resource,
3069 struct wl_resource *region_resource)
3070{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003071 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003072 struct weston_region *region;
3073
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003074 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003075 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003076 pixman_region32_copy(&surface->pending.opaque,
3077 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003078 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003079 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003080 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003081}
3082
3083static void
3084surface_set_input_region(struct wl_client *client,
3085 struct wl_resource *resource,
3086 struct wl_resource *region_resource)
3087{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003088 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003089 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003090
3091 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003092 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003093 pixman_region32_copy(&surface->pending.input,
3094 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003095 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003096 pixman_region32_fini(&surface->pending.input);
3097 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003098 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003099}
3100
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003101/* Cause damage to this sub-surface and all its children.
3102 *
3103 * This is useful when there are state changes that need an implicit
3104 * damage, e.g. a z-order change.
3105 */
3106static void
3107weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3108{
3109 struct weston_subsurface *child;
3110
3111 weston_surface_damage(sub->surface);
3112 sub->reordered = false;
3113
3114 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3115 if (child != sub)
3116 weston_surface_damage_subsurfaces(child);
3117}
3118
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003119static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003120weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003121{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003122 struct weston_subsurface *sub;
3123
3124 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3125 parent_link_pending) {
3126 wl_list_remove(&sub->parent_link);
3127 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003128
3129 if (sub->reordered)
3130 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003131 }
3132}
3133
3134static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003135weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003136 struct weston_matrix *matrix)
3137{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003138 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003139 double src_width, src_height, dest_width, dest_height;
3140
3141 weston_matrix_init(matrix);
3142
3143 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3144 src_width = surface->width_from_buffer;
3145 src_height = surface->height_from_buffer;
3146 } else {
3147 src_width = wl_fixed_to_double(vp->buffer.src_width);
3148 src_height = wl_fixed_to_double(vp->buffer.src_height);
3149 }
3150
3151 if (vp->surface.width == -1) {
3152 dest_width = src_width;
3153 dest_height = src_height;
3154 } else {
3155 dest_width = vp->surface.width;
3156 dest_height = vp->surface.height;
3157 }
3158
3159 if (src_width != dest_width || src_height != dest_height)
3160 weston_matrix_scale(matrix,
3161 src_width / dest_width,
3162 src_height / dest_height, 1);
3163
3164 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3165 weston_matrix_translate(matrix,
3166 wl_fixed_to_double(vp->buffer.src_x),
3167 wl_fixed_to_double(vp->buffer.src_y),
3168 0);
3169
3170 switch (vp->buffer.transform) {
3171 case WL_OUTPUT_TRANSFORM_FLIPPED:
3172 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3173 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3174 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3175 weston_matrix_scale(matrix, -1, 1, 1);
3176 weston_matrix_translate(matrix,
3177 surface->width_from_buffer, 0, 0);
3178 break;
3179 }
3180
3181 switch (vp->buffer.transform) {
3182 default:
3183 case WL_OUTPUT_TRANSFORM_NORMAL:
3184 case WL_OUTPUT_TRANSFORM_FLIPPED:
3185 break;
3186 case WL_OUTPUT_TRANSFORM_90:
3187 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3188 weston_matrix_rotate_xy(matrix, 0, 1);
3189 weston_matrix_translate(matrix,
3190 surface->height_from_buffer, 0, 0);
3191 break;
3192 case WL_OUTPUT_TRANSFORM_180:
3193 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3194 weston_matrix_rotate_xy(matrix, -1, 0);
3195 weston_matrix_translate(matrix,
3196 surface->width_from_buffer,
3197 surface->height_from_buffer, 0);
3198 break;
3199 case WL_OUTPUT_TRANSFORM_270:
3200 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3201 weston_matrix_rotate_xy(matrix, 0, -1);
3202 weston_matrix_translate(matrix,
3203 0, surface->width_from_buffer, 0);
3204 break;
3205 }
3206
3207 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3208}
3209
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003210/**
3211 * Compute a + b > c while being safe to overflows.
3212 */
3213static bool
3214fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3215{
3216 return (int64_t)a + (int64_t)b > (int64_t)c;
3217}
3218
3219static bool
3220weston_surface_is_pending_viewport_source_valid(
3221 const struct weston_surface *surface)
3222{
3223 const struct weston_surface_state *pend = &surface->pending;
3224 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3225 int width_from_buffer = 0;
3226 int height_from_buffer = 0;
3227 wl_fixed_t w;
3228 wl_fixed_t h;
3229
3230 /* If viewport source rect is not set, it is always ok. */
3231 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3232 return true;
3233
3234 if (pend->newly_attached) {
3235 if (pend->buffer) {
3236 convert_size_by_transform_scale(&width_from_buffer,
3237 &height_from_buffer,
3238 pend->buffer->width,
3239 pend->buffer->height,
3240 vp->buffer.transform,
3241 vp->buffer.scale);
3242 } else {
3243 /* No buffer: viewport is irrelevant. */
3244 return true;
3245 }
3246 } else {
3247 width_from_buffer = surface->width_from_buffer;
3248 height_from_buffer = surface->height_from_buffer;
3249 }
3250
3251 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3252 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3253
3254 /* No buffer: viewport is irrelevant. */
3255 if (width_from_buffer == 0 || height_from_buffer == 0)
3256 return true;
3257
3258 /* overflow checks for wl_fixed_from_int() */
3259 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3260 return false;
3261 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3262 return false;
3263
3264 w = wl_fixed_from_int(width_from_buffer);
3265 h = wl_fixed_from_int(height_from_buffer);
3266
3267 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3268 return false;
3269 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3270 return false;
3271
3272 return true;
3273}
3274
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003275static bool
3276fixed_is_integer(wl_fixed_t v)
3277{
3278 return (v & 0xff) == 0;
3279}
3280
3281static bool
3282weston_surface_is_pending_viewport_dst_size_int(
3283 const struct weston_surface *surface)
3284{
3285 const struct weston_buffer_viewport *vp =
3286 &surface->pending.buffer_viewport;
3287
3288 if (vp->surface.width != -1) {
3289 assert(vp->surface.width > 0 && vp->surface.height > 0);
3290 return true;
3291 }
3292
3293 return fixed_is_integer(vp->buffer.src_width) &&
3294 fixed_is_integer(vp->buffer.src_height);
3295}
3296
Derek Foreman152254b2015-11-26 14:17:48 -06003297/* Translate pending damage in buffer co-ordinates to surface
3298 * co-ordinates and union it with a pixman_region32_t.
3299 * This should only be called after the buffer is attached.
3300 */
3301static void
3302apply_damage_buffer(pixman_region32_t *dest,
3303 struct weston_surface *surface,
3304 struct weston_surface_state *state)
3305{
3306 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3307
3308 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3309 * translated into surface co-ordinates and unioned with
3310 * any other surface damage.
3311 * None of this makes sense if there is no buffer though.
3312 */
3313 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3314 pixman_region32_t buffer_damage;
3315
3316 pixman_region32_intersect_rect(&state->damage_buffer,
3317 &state->damage_buffer,
3318 0, 0, buffer->width,
3319 buffer->height);
3320 pixman_region32_init(&buffer_damage);
3321 weston_matrix_transform_region(&buffer_damage,
3322 &surface->buffer_to_surface_matrix,
3323 &state->damage_buffer);
3324 pixman_region32_union(dest, dest, &buffer_damage);
3325 pixman_region32_fini(&buffer_damage);
3326 }
3327 /* We should clear this on commit even if there was no buffer */
3328 pixman_region32_clear(&state->damage_buffer);
3329}
3330
Jason Ekstrand1e059042014-10-16 10:55:19 -05003331static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003332weston_surface_commit_state(struct weston_surface *surface,
3333 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003334{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003335 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003336 pixman_region32_t opaque;
3337
Alexander Larsson4ea95522013-05-22 14:41:37 +02003338 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003339 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003340 /* wp_viewport.set_source */
3341 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003342 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003343
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003344 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003345 if (state->newly_attached) {
3346 /* zwp_surface_synchronization_v1.set_acquire_fence */
3347 fd_move(&surface->acquire_fence_fd,
3348 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003349 /* zwp_surface_synchronization_v1.get_release */
3350 weston_buffer_release_move(&surface->buffer_release_ref,
3351 &state->buffer_release_ref);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003352
Jason Ekstrand7b982072014-05-20 14:33:03 -05003353 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003354 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003355 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003356 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003357 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003358
Jason Ekstrand1e059042014-10-16 10:55:19 -05003359 weston_surface_build_buffer_matrix(surface,
3360 &surface->surface_to_buffer_matrix);
3361 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3362 &surface->surface_to_buffer_matrix);
3363
Jason Ekstrand7b982072014-05-20 14:33:03 -05003364 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003365 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003366 if (surface->committed)
3367 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003368 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003369
Jason Ekstrand7b982072014-05-20 14:33:03 -05003370 state->sx = 0;
3371 state->sy = 0;
3372 state->newly_attached = 0;
3373 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003374
Derek Foreman152254b2015-11-26 14:17:48 -06003375 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003376 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003377 (pixman_region32_not_empty(&state->damage_surface) ||
3378 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003379 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003380
Pekka Paalanen8e159182012-10-10 12:49:25 +03003381 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003382 &state->damage_surface);
3383
3384 apply_damage_buffer(&surface->damage, surface, state);
3385
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003386 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003387 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003388 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003389
3390 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003391 pixman_region32_init(&opaque);
3392 pixman_region32_intersect_rect(&opaque, &state->opaque,
3393 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003394
3395 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3396 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003397 wl_list_for_each(view, &surface->views, surface_link)
3398 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003399 }
3400
3401 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003402
3403 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003404 pixman_region32_intersect_rect(&surface->input, &state->input,
3405 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003406
Pekka Paalanenbc106382012-10-10 12:49:31 +03003407 /* wl_surface.frame */
3408 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003409 &state->frame_callback_list);
3410 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003411
3412 /* XXX:
3413 * What should happen with a feedback request, if there
3414 * is no wl_buffer attached for this commit?
3415 */
3416
3417 /* presentation.feedback */
3418 wl_list_insert_list(&surface->feedback_list,
3419 &state->feedback_list);
3420 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003421
3422 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003423}
3424
3425static void
3426weston_surface_commit(struct weston_surface *surface)
3427{
3428 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003429
Pekka Paalanene67858b2013-04-25 13:57:42 +03003430 weston_surface_commit_subsurface_order(surface);
3431
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003432 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003433}
3434
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003435static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003436weston_subsurface_commit(struct weston_subsurface *sub);
3437
3438static void
3439weston_subsurface_parent_commit(struct weston_subsurface *sub,
3440 int parent_is_synchronized);
3441
3442static void
3443surface_commit(struct wl_client *client, struct wl_resource *resource)
3444{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003445 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003446 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3447
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003448 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3449 assert(surface->viewport_resource);
3450
3451 wl_resource_post_error(surface->viewport_resource,
3452 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3453 "wl_surface@%d has viewport source outside buffer",
3454 wl_resource_get_id(resource));
3455 return;
3456 }
3457
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003458 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3459 assert(surface->viewport_resource);
3460
3461 wl_resource_post_error(surface->viewport_resource,
3462 WP_VIEWPORT_ERROR_BAD_SIZE,
3463 "wl_surface@%d viewport dst size not integer",
3464 wl_resource_get_id(resource));
3465 return;
3466 }
3467
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003468 if (surface->pending.acquire_fence_fd >= 0) {
3469 assert(surface->synchronization_resource);
3470
3471 if (!surface->pending.buffer) {
3472 fd_clear(&surface->pending.acquire_fence_fd);
3473 wl_resource_post_error(surface->synchronization_resource,
3474 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3475 "wl_surface@%"PRIu32" no buffer for synchronization",
3476 wl_resource_get_id(resource));
3477 return;
3478 }
3479
3480 /* We support fences for both wp_linux_dmabuf and opaque EGL
3481 * buffers, as mandated by minor version 2 of the
3482 * zwp_linux_explicit_synchronization_v1 protocol. Since
3483 * renderers that support fences currently only support these
3484 * two buffer types plus SHM buffers, we can just check for the
3485 * SHM buffer case here.
3486 */
3487 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3488 fd_clear(&surface->pending.acquire_fence_fd);
3489 wl_resource_post_error(surface->synchronization_resource,
3490 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3491 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3492 wl_resource_get_id(resource));
3493 return;
3494 }
3495 }
3496
Alexandros Frantzis67629672018-10-19 12:14:11 +03003497 if (surface->pending.buffer_release_ref.buffer_release &&
3498 !surface->pending.buffer) {
3499 assert(surface->synchronization_resource);
3500
3501 wl_resource_post_error(surface->synchronization_resource,
3502 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3503 "wl_surface@%"PRIu32" no buffer for synchronization",
3504 wl_resource_get_id(resource));
3505 return;
3506 }
3507
Pekka Paalanene67858b2013-04-25 13:57:42 +03003508 if (sub) {
3509 weston_subsurface_commit(sub);
3510 return;
3511 }
3512
3513 weston_surface_commit(surface);
3514
3515 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3516 if (sub->surface != surface)
3517 weston_subsurface_parent_commit(sub, 0);
3518 }
3519}
3520
3521static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003522surface_set_buffer_transform(struct wl_client *client,
3523 struct wl_resource *resource, int transform)
3524{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003525 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003526
Jonny Lamba55f1392014-05-30 12:07:15 +02003527 /* if wl_output.transform grows more members this will need to be updated. */
3528 if (transform < 0 ||
3529 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3530 wl_resource_post_error(resource,
3531 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3532 "buffer transform must be a valid transform "
3533 "('%d' specified)", transform);
3534 return;
3535 }
3536
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003537 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003538 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003539}
3540
Alexander Larsson4ea95522013-05-22 14:41:37 +02003541static void
3542surface_set_buffer_scale(struct wl_client *client,
3543 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003544 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003545{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003546 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003547
Jonny Lamba55f1392014-05-30 12:07:15 +02003548 if (scale < 1) {
3549 wl_resource_post_error(resource,
3550 WL_SURFACE_ERROR_INVALID_SCALE,
3551 "buffer scale must be at least one "
3552 "('%d' specified)", scale);
3553 return;
3554 }
3555
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003556 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003557 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003558}
3559
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003560static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003561 surface_destroy,
3562 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003563 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003564 surface_frame,
3565 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003566 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003567 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003568 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003569 surface_set_buffer_scale,
3570 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003571};
3572
3573static void
3574compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003575 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003576{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003577 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003578 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003579
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003580 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003581 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003582 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003583 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003584 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003585
Jason Ekstranda85118c2013-06-27 20:17:02 -05003586 surface->resource =
3587 wl_resource_create(client, &wl_surface_interface,
3588 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003589 if (surface->resource == NULL) {
3590 weston_surface_destroy(surface);
3591 wl_resource_post_no_memory(resource);
3592 return;
3593 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003594 wl_resource_set_implementation(surface->resource, &surface_interface,
3595 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003596
3597 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003598}
3599
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003600static void
3601destroy_region(struct wl_resource *resource)
3602{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003603 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003604
3605 pixman_region32_fini(&region->region);
3606 free(region);
3607}
3608
3609static void
3610region_destroy(struct wl_client *client, struct wl_resource *resource)
3611{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003612 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003613}
3614
3615static void
3616region_add(struct wl_client *client, struct wl_resource *resource,
3617 int32_t x, int32_t y, int32_t width, int32_t height)
3618{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003619 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003620
3621 pixman_region32_union_rect(&region->region, &region->region,
3622 x, y, width, height);
3623}
3624
3625static void
3626region_subtract(struct wl_client *client, struct wl_resource *resource,
3627 int32_t x, int32_t y, int32_t width, int32_t height)
3628{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003629 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003630 pixman_region32_t rect;
3631
3632 pixman_region32_init_rect(&rect, x, y, width, height);
3633 pixman_region32_subtract(&region->region, &region->region, &rect);
3634 pixman_region32_fini(&rect);
3635}
3636
3637static const struct wl_region_interface region_interface = {
3638 region_destroy,
3639 region_add,
3640 region_subtract
3641};
3642
3643static void
3644compositor_create_region(struct wl_client *client,
3645 struct wl_resource *resource, uint32_t id)
3646{
3647 struct weston_region *region;
3648
3649 region = malloc(sizeof *region);
3650 if (region == NULL) {
3651 wl_resource_post_no_memory(resource);
3652 return;
3653 }
3654
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003655 pixman_region32_init(&region->region);
3656
Jason Ekstranda85118c2013-06-27 20:17:02 -05003657 region->resource =
3658 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003659 if (region->resource == NULL) {
3660 free(region);
3661 wl_resource_post_no_memory(resource);
3662 return;
3663 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003664 wl_resource_set_implementation(region->resource, &region_interface,
3665 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003666}
3667
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003668static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003669 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003670 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003671};
3672
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003673static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003674weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3675{
3676 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003677
Jason Ekstrand7b982072014-05-20 14:33:03 -05003678 weston_surface_commit_state(surface, &sub->cached);
3679 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003680
3681 weston_surface_commit_subsurface_order(surface);
3682
3683 weston_surface_schedule_repaint(surface);
3684
Jason Ekstrand7b982072014-05-20 14:33:03 -05003685 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003686}
3687
3688static void
3689weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3690{
3691 struct weston_surface *surface = sub->surface;
3692
3693 /*
3694 * If this commit would cause the surface to move by the
3695 * attach(dx, dy) parameters, the old damage region must be
3696 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003697 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003698 */
Derek Foreman152254b2015-11-26 14:17:48 -06003699 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003700 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003701 pixman_region32_union(&sub->cached.damage_surface,
3702 &sub->cached.damage_surface,
3703 &surface->pending.damage_surface);
3704 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003705
3706 if (surface->pending.newly_attached) {
3707 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003708 weston_surface_state_set_buffer(&sub->cached,
3709 surface->pending.buffer);
3710 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003711 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003712 weston_presentation_feedback_discard_list(
3713 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003714 /* zwp_surface_synchronization_v1.set_acquire_fence */
3715 fd_move(&sub->cached.acquire_fence_fd,
3716 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003717 /* zwp_surface_synchronization_v1.get_release */
3718 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3719 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003720 }
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003721 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003722 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003723 sub->cached.sx += surface->pending.sx;
3724 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003725
Derek Foreman152254b2015-11-26 14:17:48 -06003726 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3727
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003728 sub->cached.buffer_viewport.changed |=
3729 surface->pending.buffer_viewport.changed;
3730 sub->cached.buffer_viewport.buffer =
3731 surface->pending.buffer_viewport.buffer;
3732 sub->cached.buffer_viewport.surface =
3733 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003734
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003735 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003736
3737 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3738
3739 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3740
3741 wl_list_insert_list(&sub->cached.frame_callback_list,
3742 &surface->pending.frame_callback_list);
3743 wl_list_init(&surface->pending.frame_callback_list);
3744
Pekka Paalanen133e4392014-09-23 22:08:46 -04003745 wl_list_insert_list(&sub->cached.feedback_list,
3746 &surface->pending.feedback_list);
3747 wl_list_init(&surface->pending.feedback_list);
3748
Jason Ekstrand7b982072014-05-20 14:33:03 -05003749 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003750}
3751
Derek Foreman280e7dd2014-10-03 13:13:42 -05003752static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003753weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3754{
3755 while (sub) {
3756 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003757 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003758
3759 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003760 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003761
3762 sub = weston_surface_to_subsurface(sub->parent);
3763 }
3764
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003765 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003766}
3767
3768static void
3769weston_subsurface_commit(struct weston_subsurface *sub)
3770{
3771 struct weston_surface *surface = sub->surface;
3772 struct weston_subsurface *tmp;
3773
3774 /* Recursive check for effectively synchronized. */
3775 if (weston_subsurface_is_synchronized(sub)) {
3776 weston_subsurface_commit_to_cache(sub);
3777 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003778 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003779 /* flush accumulated state from cache */
3780 weston_subsurface_commit_to_cache(sub);
3781 weston_subsurface_commit_from_cache(sub);
3782 } else {
3783 weston_surface_commit(surface);
3784 }
3785
3786 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3787 if (tmp->surface != surface)
3788 weston_subsurface_parent_commit(tmp, 0);
3789 }
3790 }
3791}
3792
3793static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003794weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003795{
3796 struct weston_surface *surface = sub->surface;
3797 struct weston_subsurface *tmp;
3798
Pekka Paalanene67858b2013-04-25 13:57:42 +03003799 /* From now on, commit_from_cache the whole sub-tree, regardless of
3800 * the synchronized mode of each child. This sub-surface or some
3801 * of its ancestors were synchronized, so we are synchronized
3802 * all the way down.
3803 */
3804
Jason Ekstrand7b982072014-05-20 14:33:03 -05003805 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003806 weston_subsurface_commit_from_cache(sub);
3807
3808 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3809 if (tmp->surface != surface)
3810 weston_subsurface_parent_commit(tmp, 1);
3811 }
3812}
3813
3814static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003815weston_subsurface_parent_commit(struct weston_subsurface *sub,
3816 int parent_is_synchronized)
3817{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003818 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003819 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003820 wl_list_for_each(view, &sub->surface->views, surface_link)
3821 weston_view_set_position(view,
3822 sub->position.x,
3823 sub->position.y);
3824
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003825 sub->position.set = 0;
3826 }
3827
3828 if (parent_is_synchronized || sub->synchronized)
3829 weston_subsurface_synchronized_commit(sub);
3830}
3831
Pekka Paalanen8274d902014-08-06 19:36:51 +03003832static int
3833subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3834{
3835 return snprintf(buf, len, "sub-surface");
3836}
3837
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003838static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003839subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003840{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003841 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003842
Jason Ekstranda7af7042013-10-12 22:38:11 -05003843 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003844 weston_view_set_position(view,
3845 view->geometry.x + dx,
3846 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003847
3848 /* No need to check parent mappedness, because if parent is not
3849 * mapped, parent is not in a visible layer, so this sub-surface
3850 * will not be drawn either.
3851 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003852
Pekka Paalanene67858b2013-04-25 13:57:42 +03003853 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003854 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003855
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003856 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003857 * because that would call it also for the parent surface,
3858 * which might not be mapped yet. That would lead to
3859 * inconsistent state, where the window could never be
3860 * mapped.
3861 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003862 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003863 * weston_surface_is_mapped() return true, so that when the
3864 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003865 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003866 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003867 }
3868}
3869
3870static struct weston_subsurface *
3871weston_surface_to_subsurface(struct weston_surface *surface)
3872{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003873 if (surface->committed == subsurface_committed)
3874 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003875
3876 return NULL;
3877}
3878
Pekka Paalanen01388e22013-04-25 13:57:44 +03003879WL_EXPORT struct weston_surface *
3880weston_surface_get_main_surface(struct weston_surface *surface)
3881{
3882 struct weston_subsurface *sub;
3883
3884 while (surface && (sub = weston_surface_to_subsurface(surface)))
3885 surface = sub->parent;
3886
3887 return surface;
3888}
3889
Pekka Paalanen50b67472014-10-01 15:02:41 +03003890WL_EXPORT int
3891weston_surface_set_role(struct weston_surface *surface,
3892 const char *role_name,
3893 struct wl_resource *error_resource,
3894 uint32_t error_code)
3895{
3896 assert(role_name);
3897
3898 if (surface->role_name == NULL ||
3899 surface->role_name == role_name ||
3900 strcmp(surface->role_name, role_name) == 0) {
3901 surface->role_name = role_name;
3902
3903 return 0;
3904 }
3905
3906 wl_resource_post_error(error_resource, error_code,
3907 "Cannot assign role %s to wl_surface@%d,"
3908 " already has role %s\n",
3909 role_name,
3910 wl_resource_get_id(surface->resource),
3911 surface->role_name);
3912 return -1;
3913}
3914
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003915WL_EXPORT const char *
3916weston_surface_get_role(struct weston_surface *surface)
3917{
3918 return surface->role_name;
3919}
3920
Pekka Paalanen8274d902014-08-06 19:36:51 +03003921WL_EXPORT void
3922weston_surface_set_label_func(struct weston_surface *surface,
3923 int (*desc)(struct weston_surface *,
3924 char *, size_t))
3925{
3926 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003927 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003928}
3929
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003930/** Get the size of surface contents
3931 *
3932 * \param surface The surface to query.
3933 * \param width Returns the width of raw contents.
3934 * \param height Returns the height of raw contents.
3935 *
3936 * Retrieves the raw surface content size in pixels for the given surface.
3937 * This is the whole content size in buffer pixels. If the surface
3938 * has no content or the renderer does not implement this feature,
3939 * zeroes are returned.
3940 *
3941 * This function is used to determine the buffer size needed for
3942 * a weston_surface_copy_content() call.
3943 */
3944WL_EXPORT void
3945weston_surface_get_content_size(struct weston_surface *surface,
3946 int *width, int *height)
3947{
3948 struct weston_renderer *rer = surface->compositor->renderer;
3949
3950 if (!rer->surface_get_content_size) {
3951 *width = 0;
3952 *height = 0;
3953 return;
3954 }
3955
3956 rer->surface_get_content_size(surface, width, height);
3957}
3958
Quentin Glidic248dd102016-08-12 10:41:34 +02003959/** Get the bounding box of a surface and its subsurfaces
3960 *
3961 * \param surface The surface to query.
3962 * \return The bounding box relative to the surface origin.
3963 *
3964 */
3965WL_EXPORT struct weston_geometry
3966weston_surface_get_bounding_box(struct weston_surface *surface)
3967{
3968 pixman_region32_t region;
3969 pixman_box32_t *box;
3970 struct weston_subsurface *subsurface;
3971
3972 pixman_region32_init_rect(&region,
3973 0, 0,
3974 surface->width, surface->height);
3975
3976 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3977 pixman_region32_union_rect(&region, &region,
3978 subsurface->position.x,
3979 subsurface->position.y,
3980 subsurface->surface->width,
3981 subsurface->surface->height);
3982
3983 box = pixman_region32_extents(&region);
3984 struct weston_geometry geometry = {
3985 .x = box->x1,
3986 .y = box->y1,
3987 .width = box->x2 - box->x1,
3988 .height = box->y2 - box->y1,
3989 };
3990
3991 pixman_region32_fini(&region);
3992
3993 return geometry;
3994}
3995
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003996/** Copy surface contents to system memory.
3997 *
3998 * \param surface The surface to copy from.
3999 * \param target Pointer to the target memory buffer.
4000 * \param size Size of the target buffer in bytes.
4001 * \param src_x X location on contents to copy from.
4002 * \param src_y Y location on contents to copy from.
4003 * \param width Width in pixels of the area to copy.
4004 * \param height Height in pixels of the area to copy.
4005 * \return 0 for success, -1 for failure.
4006 *
4007 * Surface contents are maintained by the renderer. They can be in a
4008 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4009 * else.
4010 *
4011 * Surface contents are copied into memory pointed to by target,
4012 * which has size bytes of space available. The target memory
4013 * may be larger than needed, but being smaller returns an error.
4014 * The extra bytes in target may or may not be written; their content is
4015 * unspecified. Size must be large enough to hold the image.
4016 *
4017 * The image in the target memory will be arranged in rows from
4018 * top to bottom, and pixels on a row from left to right. The pixel
4019 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4020 * width * 4.
4021 *
4022 * Parameters src_x and src_y define the upper-left corner in buffer
4023 * coordinates (pixels) to copy from. Parameters width and height
4024 * define the size of the area to copy in pixels.
4025 *
4026 * The rectangle defined by src_x, src_y, width, height must fit in
4027 * the surface contents. Otherwise an error is returned.
4028 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004029 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004030 * needed target buffer size and rectangle limits.
4031 *
4032 * CURRENT IMPLEMENTATION RESTRICTIONS:
4033 * - the machine must be little-endian due to Pixman formats.
4034 *
4035 * NOTE: Pixman formats are premultiplied.
4036 */
4037WL_EXPORT int
4038weston_surface_copy_content(struct weston_surface *surface,
4039 void *target, size_t size,
4040 int src_x, int src_y,
4041 int width, int height)
4042{
4043 struct weston_renderer *rer = surface->compositor->renderer;
4044 int cw, ch;
4045 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4046
4047 if (!rer->surface_copy_content)
4048 return -1;
4049
4050 weston_surface_get_content_size(surface, &cw, &ch);
4051
4052 if (src_x < 0 || src_y < 0)
4053 return -1;
4054
4055 if (width <= 0 || height <= 0)
4056 return -1;
4057
4058 if (src_x + width > cw || src_y + height > ch)
4059 return -1;
4060
4061 if (width * bytespp * height > size)
4062 return -1;
4063
4064 return rer->surface_copy_content(surface, target, size,
4065 src_x, src_y, width, height);
4066}
4067
Pekka Paalanene67858b2013-04-25 13:57:42 +03004068static void
4069subsurface_set_position(struct wl_client *client,
4070 struct wl_resource *resource, int32_t x, int32_t y)
4071{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004072 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004073
4074 if (!sub)
4075 return;
4076
4077 sub->position.x = x;
4078 sub->position.y = y;
4079 sub->position.set = 1;
4080}
4081
4082static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004083subsurface_find_sibling(struct weston_subsurface *sub,
4084 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004085{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004086 struct weston_surface *parent = sub->parent;
4087 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004088
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004089 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4090 if (sibling->surface == surface && sibling != sub)
4091 return sibling;
4092 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004093
4094 return NULL;
4095}
4096
4097static struct weston_subsurface *
4098subsurface_sibling_check(struct weston_subsurface *sub,
4099 struct weston_surface *surface,
4100 const char *request)
4101{
4102 struct weston_subsurface *sibling;
4103
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004104 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004105 if (!sibling) {
4106 wl_resource_post_error(sub->resource,
4107 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4108 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004109 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004110 return NULL;
4111 }
4112
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004113 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004114
4115 return sibling;
4116}
4117
4118static void
4119subsurface_place_above(struct wl_client *client,
4120 struct wl_resource *resource,
4121 struct wl_resource *sibling_resource)
4122{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004123 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004124 struct weston_surface *surface =
4125 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004126 struct weston_subsurface *sibling;
4127
4128 if (!sub)
4129 return;
4130
4131 sibling = subsurface_sibling_check(sub, surface, "place_above");
4132 if (!sibling)
4133 return;
4134
4135 wl_list_remove(&sub->parent_link_pending);
4136 wl_list_insert(sibling->parent_link_pending.prev,
4137 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004138
4139 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004140}
4141
4142static void
4143subsurface_place_below(struct wl_client *client,
4144 struct wl_resource *resource,
4145 struct wl_resource *sibling_resource)
4146{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004147 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004148 struct weston_surface *surface =
4149 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004150 struct weston_subsurface *sibling;
4151
4152 if (!sub)
4153 return;
4154
4155 sibling = subsurface_sibling_check(sub, surface, "place_below");
4156 if (!sibling)
4157 return;
4158
4159 wl_list_remove(&sub->parent_link_pending);
4160 wl_list_insert(&sibling->parent_link_pending,
4161 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004162
4163 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004164}
4165
4166static void
4167subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4168{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004169 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004170
4171 if (sub)
4172 sub->synchronized = 1;
4173}
4174
4175static void
4176subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4177{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004178 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004179
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004180 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004181 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004182
4183 /* If sub became effectively desynchronized, flush. */
4184 if (!weston_subsurface_is_synchronized(sub))
4185 weston_subsurface_synchronized_commit(sub);
4186 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004187}
4188
4189static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004190weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4191{
4192 wl_list_remove(&sub->parent_link);
4193 wl_list_remove(&sub->parent_link_pending);
4194 wl_list_remove(&sub->parent_destroy_listener.link);
4195 sub->parent = NULL;
4196}
4197
4198static void
4199weston_subsurface_destroy(struct weston_subsurface *sub);
4200
4201static void
4202subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4203{
4204 struct weston_subsurface *sub =
4205 container_of(listener, struct weston_subsurface,
4206 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004207 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004208
4209 /* The protocol object (wl_resource) is left inert. */
4210 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004211 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004212
4213 weston_subsurface_destroy(sub);
4214}
4215
4216static void
4217subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4218{
4219 struct weston_subsurface *sub =
4220 container_of(listener, struct weston_subsurface,
4221 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004222 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004223 assert(sub->surface != sub->parent);
4224
4225 if (weston_surface_is_mapped(sub->surface))
4226 weston_surface_unmap(sub->surface);
4227
4228 weston_subsurface_unlink_parent(sub);
4229}
4230
4231static void
4232subsurface_resource_destroy(struct wl_resource *resource)
4233{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004234 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004235
4236 if (sub)
4237 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004238}
4239
4240static void
4241subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4242{
4243 wl_resource_destroy(resource);
4244}
4245
4246static void
4247weston_subsurface_link_parent(struct weston_subsurface *sub,
4248 struct weston_surface *parent)
4249{
4250 sub->parent = parent;
4251 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004252 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004253 &sub->parent_destroy_listener);
4254
4255 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4256 wl_list_insert(&parent->subsurface_list_pending,
4257 &sub->parent_link_pending);
4258}
4259
4260static void
4261weston_subsurface_link_surface(struct weston_subsurface *sub,
4262 struct weston_surface *surface)
4263{
4264 sub->surface = surface;
4265 sub->surface_destroy_listener.notify =
4266 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004267 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004268 &sub->surface_destroy_listener);
4269}
4270
4271static void
4272weston_subsurface_destroy(struct weston_subsurface *sub)
4273{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004274 struct weston_view *view, *next;
4275
Pekka Paalanene67858b2013-04-25 13:57:42 +03004276 assert(sub->surface);
4277
4278 if (sub->resource) {
4279 assert(weston_surface_to_subsurface(sub->surface) == sub);
4280 assert(sub->parent_destroy_listener.notify ==
4281 subsurface_handle_parent_destroy);
4282
George Kiagiadakised04d382014-06-13 18:10:26 +02004283 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4284 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004285 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004286 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004287
Pekka Paalanene67858b2013-04-25 13:57:42 +03004288 if (sub->parent)
4289 weston_subsurface_unlink_parent(sub);
4290
Jason Ekstrand7b982072014-05-20 14:33:03 -05004291 weston_surface_state_fini(&sub->cached);
4292 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004293
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004294 sub->surface->committed = NULL;
4295 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004296 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004297 } else {
4298 /* the dummy weston_subsurface for the parent itself */
4299 assert(sub->parent_destroy_listener.notify == NULL);
4300 wl_list_remove(&sub->parent_link);
4301 wl_list_remove(&sub->parent_link_pending);
4302 }
4303
4304 wl_list_remove(&sub->surface_destroy_listener.link);
4305 free(sub);
4306}
4307
4308static const struct wl_subsurface_interface subsurface_implementation = {
4309 subsurface_destroy,
4310 subsurface_set_position,
4311 subsurface_place_above,
4312 subsurface_place_below,
4313 subsurface_set_sync,
4314 subsurface_set_desync
4315};
4316
4317static struct weston_subsurface *
4318weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4319 struct weston_surface *parent)
4320{
4321 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004322 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004323
Bryce Harringtonde16d892014-11-20 22:21:57 -08004324 sub = zalloc(sizeof *sub);
4325 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004326 return NULL;
4327
Jason Ekstranda7af7042013-10-12 22:38:11 -05004328 wl_list_init(&sub->unused_views);
4329
Jason Ekstranda85118c2013-06-27 20:17:02 -05004330 sub->resource =
4331 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004332 if (!sub->resource) {
4333 free(sub);
4334 return NULL;
4335 }
4336
Jason Ekstranda85118c2013-06-27 20:17:02 -05004337 wl_resource_set_implementation(sub->resource,
4338 &subsurface_implementation,
4339 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004340 weston_subsurface_link_surface(sub, surface);
4341 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004342 weston_surface_state_init(&sub->cached);
4343 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004344 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004345
4346 return sub;
4347}
4348
4349/* Create a dummy subsurface for having the parent itself in its
4350 * sub-surface lists. Makes stacking order manipulation easy.
4351 */
4352static struct weston_subsurface *
4353weston_subsurface_create_for_parent(struct weston_surface *parent)
4354{
4355 struct weston_subsurface *sub;
4356
Bryce Harringtonde16d892014-11-20 22:21:57 -08004357 sub = zalloc(sizeof *sub);
4358 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004359 return NULL;
4360
4361 weston_subsurface_link_surface(sub, parent);
4362 sub->parent = parent;
4363 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4364 wl_list_insert(&parent->subsurface_list_pending,
4365 &sub->parent_link_pending);
4366
4367 return sub;
4368}
4369
4370static void
4371subcompositor_get_subsurface(struct wl_client *client,
4372 struct wl_resource *resource,
4373 uint32_t id,
4374 struct wl_resource *surface_resource,
4375 struct wl_resource *parent_resource)
4376{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004377 struct weston_surface *surface =
4378 wl_resource_get_user_data(surface_resource);
4379 struct weston_surface *parent =
4380 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004381 struct weston_subsurface *sub;
4382 static const char where[] = "get_subsurface: wl_subsurface@";
4383
4384 if (surface == parent) {
4385 wl_resource_post_error(resource,
4386 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4387 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004388 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004389 return;
4390 }
4391
4392 if (weston_surface_to_subsurface(surface)) {
4393 wl_resource_post_error(resource,
4394 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4395 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004396 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004397 return;
4398 }
4399
Pekka Paalanen50b67472014-10-01 15:02:41 +03004400 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4401 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004402 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004403
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004404 if (weston_surface_get_main_surface(parent) == surface) {
4405 wl_resource_post_error(resource,
4406 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4407 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004408 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004409 return;
4410 }
4411
Pekka Paalanene67858b2013-04-25 13:57:42 +03004412 /* make sure the parent is in its own list */
4413 if (wl_list_empty(&parent->subsurface_list)) {
4414 if (!weston_subsurface_create_for_parent(parent)) {
4415 wl_resource_post_no_memory(resource);
4416 return;
4417 }
4418 }
4419
4420 sub = weston_subsurface_create(id, surface, parent);
4421 if (!sub) {
4422 wl_resource_post_no_memory(resource);
4423 return;
4424 }
4425
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004426 surface->committed = subsurface_committed;
4427 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004428 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004429}
4430
4431static void
4432subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4433{
4434 wl_resource_destroy(resource);
4435}
4436
4437static const struct wl_subcompositor_interface subcompositor_interface = {
4438 subcompositor_destroy,
4439 subcompositor_get_subsurface
4440};
4441
4442static void
4443bind_subcompositor(struct wl_client *client,
4444 void *data, uint32_t version, uint32_t id)
4445{
4446 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004447 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004448
Jason Ekstranda85118c2013-06-27 20:17:02 -05004449 resource =
4450 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004451 if (resource == NULL) {
4452 wl_client_post_no_memory(client);
4453 return;
4454 }
4455 wl_resource_set_implementation(resource, &subcompositor_interface,
4456 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004457}
4458
Bryce Harrington0795ece2016-08-30 12:04:26 -07004459/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004460 *
4461 * \param compositor The compositor instance
4462 * \param state The DPMS state the outputs will be set to
4463 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004464static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004465weston_compositor_dpms(struct weston_compositor *compositor,
4466 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004467{
4468 struct weston_output *output;
4469
Bryce Harrington08976ac2016-08-30 12:05:16 -07004470 wl_list_for_each(output, &compositor->output_list, link)
4471 if (output->set_dpms)
4472 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004473}
4474
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004475/** Restores the compositor to active status
4476 *
4477 * \param compositor The compositor instance
4478 *
4479 * If the compositor was in a sleeping mode, all outputs are powered
4480 * back on via DPMS. Otherwise if the compositor was inactive
4481 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4482 * signal will fire.
4483 *
4484 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004485 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004486 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004487WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004488weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004489{
Neil Roberts8b62e202013-09-30 13:14:47 +01004490 uint32_t old_state = compositor->state;
4491
4492 /* The state needs to be changed before emitting the wake
4493 * signal because that may try to schedule a repaint which
4494 * will not work if the compositor is still sleeping */
4495 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4496
4497 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004498 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004499 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004500 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004501 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004502 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004503 /* fall through */
4504 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004505 wl_event_source_timer_update(compositor->idle_source,
4506 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004507 }
4508}
4509
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004510/** Turns off rendering and frame events for the compositor.
4511 *
4512 * \param compositor The compositor instance
4513 *
4514 * This is used for example to prevent further rendering while the
4515 * compositor is shutting down.
4516 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004517 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004518 *
4519 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004520 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004521WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004522weston_compositor_offscreen(struct weston_compositor *compositor)
4523{
4524 switch (compositor->state) {
4525 case WESTON_COMPOSITOR_OFFSCREEN:
4526 return;
4527 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004528 default:
4529 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4530 wl_event_source_timer_update(compositor->idle_source, 0);
4531 }
4532}
4533
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004534/** Powers down all attached output devices
4535 *
4536 * \param compositor The compositor instance
4537 *
4538 * Causes rendering to the outputs to cease, and no frame events to be
4539 * sent. Only powers down the outputs if the compositor is not already
4540 * in sleep mode.
4541 *
4542 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004543 *
4544 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004545 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004546WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004547weston_compositor_sleep(struct weston_compositor *compositor)
4548{
4549 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4550 return;
4551
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004552 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004553 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4554 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4555}
4556
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004557/** Sets compositor to idle mode
4558 *
4559 * \param data The compositor instance
4560 *
4561 * This is called when the idle timer fires. Once the compositor is in
4562 * idle mode it requires a wake action (e.g. via
4563 * weston_compositor_wake()) to restore it. The compositor's
4564 * idle_signal will be triggered when the idle event occurs.
4565 *
4566 * Idleness can be inhibited by setting the compositor's idle_inhibit
4567 * property.
4568 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004569static int
4570idle_handler(void *data)
4571{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004572 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004573
4574 if (compositor->idle_inhibit)
4575 return 1;
4576
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004577 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004578 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004579
4580 return 1;
4581}
4582
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004583WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004584weston_plane_init(struct weston_plane *plane,
4585 struct weston_compositor *ec,
4586 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004587{
4588 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004589 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004590 plane->x = x;
4591 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004592 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004593
4594 /* Init the link so that the call to wl_list_remove() when releasing
4595 * the plane without ever stacking doesn't lead to a crash */
4596 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004597}
4598
4599WL_EXPORT void
4600weston_plane_release(struct weston_plane *plane)
4601{
Xiong Zhang97116532013-10-23 13:58:31 +08004602 struct weston_view *view;
4603
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004604 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004605 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004606
Xiong Zhang97116532013-10-23 13:58:31 +08004607 wl_list_for_each(view, &plane->compositor->view_list, link) {
4608 if (view->plane == plane)
4609 view->plane = NULL;
4610 }
4611
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004612 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004613}
4614
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004615/** weston_compositor_stack_plane
4616 * \ingroup compositor
4617 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004618WL_EXPORT void
4619weston_compositor_stack_plane(struct weston_compositor *ec,
4620 struct weston_plane *plane,
4621 struct weston_plane *above)
4622{
4623 if (above)
4624 wl_list_insert(above->link.prev, &plane->link);
4625 else
4626 wl_list_insert(&ec->plane_list, &plane->link);
4627}
4628
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004629static void
4630output_release(struct wl_client *client, struct wl_resource *resource)
4631{
4632 wl_resource_destroy(resource);
4633}
4634
4635static const struct wl_output_interface output_interface = {
4636 output_release,
4637};
4638
4639
Casey Dahlin9074db52012-04-19 22:50:09 -04004640static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004641{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004642 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004643}
4644
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004645static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004646bind_output(struct wl_client *client,
4647 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004648{
Pekka Paalanen05347622017-03-27 12:24:34 +03004649 struct weston_head *head = data;
4650 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004651 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004652 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004653
Jason Ekstranda85118c2013-06-27 20:17:02 -05004654 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004655 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004656 if (resource == NULL) {
4657 wl_client_post_no_memory(client);
4658 return;
4659 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004660
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004661 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004662 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004663 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004664
Pekka Paalanen05347622017-03-27 12:24:34 +03004665 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004666 wl_output_send_geometry(resource,
4667 output->x,
4668 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004669 head->mm_width,
4670 head->mm_height,
4671 head->subpixel,
4672 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004673 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004674 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004675 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004676 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004677
4678 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004679 wl_output_send_mode(resource,
4680 mode->flags,
4681 mode->width,
4682 mode->height,
4683 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004684 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004685
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004686 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004687 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004688}
4689
Pekka Paalanendcac3512017-12-08 14:13:34 +02004690static void
4691weston_head_add_global(struct weston_head *head)
4692{
4693 head->global = wl_global_create(head->compositor->wl_display,
4694 &wl_output_interface, 3,
4695 head, bind_output);
4696}
4697
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004698/** Remove the global wl_output protocol object
4699 *
4700 * \param head The head whose global to remove.
4701 *
4702 * Also orphans the wl_resources for this head (wl_output).
4703 */
4704static void
4705weston_head_remove_global(struct weston_head *head)
4706{
4707 struct wl_resource *resource, *tmp;
4708
4709 if (head->global)
4710 wl_global_destroy(head->global);
4711 head->global = NULL;
4712
4713 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4714 unbind_resource(resource);
4715 wl_resource_set_destructor(resource, NULL);
4716 wl_resource_set_user_data(resource, NULL);
4717 }
Roman Gilge97391c2019-03-29 13:01:06 +01004718
4719 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
4720 /* It's sufficient to unset the destructor, then the list elements
4721 * won't be accessed.
4722 */
4723 wl_resource_set_destructor(resource, NULL);
4724 }
4725 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004726}
4727
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004728/** Get the backing object of wl_output
4729 *
4730 * \param resource A wl_output protocol object.
4731 * \return The backing object (user data) of a wl_resource representing a
4732 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004733 *
4734 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004735 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004736WL_EXPORT struct weston_head *
4737weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004738{
4739 assert(wl_resource_instance_of(resource, &wl_output_interface,
4740 &output_interface));
4741
4742 return wl_resource_get_user_data(resource);
4743}
4744
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004745/** Initialize a pre-allocated weston_head
4746 *
4747 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004748 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004749 *
4750 * The head will be safe to attach, detach and release.
4751 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004752 * The name is used in logs, and can be used by compositors as a configuration
4753 * identifier.
4754 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004755 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004756 * \internal
4757 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004758WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004759weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004760{
4761 /* Add some (in)sane defaults which can be used
4762 * for checking if an output was properly configured
4763 */
4764 memset(head, 0, sizeof *head);
4765
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004766 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004767 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004768 wl_list_init(&head->output_link);
4769 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01004770 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004771 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004772}
4773
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004774/** Send output heads changed signal
4775 *
4776 * \param output The output that changed.
4777 *
4778 * Notify that the enabled output gained and/or lost heads, or that the
4779 * associated heads may have changed their connection status. This does not
4780 * include cases where the output becomes enabled or disabled. The registered
4781 * callbacks are called after the change has successfully happened.
4782 *
4783 * If connection status change causes the compositor to attach or detach a head
4784 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03004785 *
4786 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004787 */
4788static void
4789weston_output_emit_heads_changed(struct weston_output *output)
4790{
4791 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4792 output);
4793}
4794
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004795/** Idle task for emitting heads_changed_signal */
4796static void
4797weston_compositor_call_heads_changed(void *data)
4798{
4799 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004800 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004801
4802 compositor->heads_changed_source = NULL;
4803
4804 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004805
4806 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4807 if (head->output && head->output->enabled)
4808 weston_output_emit_heads_changed(head->output);
4809 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004810}
4811
4812/** Schedule a call on idle to heads_changed callback
4813 *
4814 * \param compositor The Compositor.
4815 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004816 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004817 * \internal
4818 */
4819static void
4820weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4821{
4822 struct wl_event_loop *loop;
4823
4824 if (compositor->heads_changed_source)
4825 return;
4826
4827 loop = wl_display_get_event_loop(compositor->wl_display);
4828 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4829 weston_compositor_call_heads_changed, compositor);
4830}
4831
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004832/** Register a new head
4833 *
4834 * \param compositor The compositor.
4835 * \param head The head to register, must not be already registered.
4836 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004837 * This signals the core that a new head has become available, leading to
4838 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004839 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004840 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004841 * \internal
4842 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004843WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004844weston_compositor_add_head(struct weston_compositor *compositor,
4845 struct weston_head *head)
4846{
4847 assert(wl_list_empty(&head->compositor_link));
4848 assert(head->name);
4849
4850 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4851 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004852 weston_compositor_schedule_heads_changed(compositor);
4853}
4854
4855/** Adds a listener to be called when heads change
4856 *
4857 * \param compositor The compositor.
4858 * \param listener The listener to add.
4859 *
Marius Vlada2dace22019-06-12 16:05:44 +03004860 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004861 *
4862 * The listener function will be called after heads are added or their
4863 * connection status has changed. Several changes may be accumulated into a
4864 * single call. The user is expected to iterate over the existing heads and
4865 * check their statuses to find out what changed.
4866 *
4867 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4868 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004869 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004870 */
4871WL_EXPORT void
4872weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4873 struct wl_listener *listener)
4874{
4875 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004876}
4877
4878/** Iterate over available heads
4879 *
4880 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03004881 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004882 * \return The next available head in the list.
4883 *
4884 * Returns all available heads, regardless of being connected or enabled.
4885 *
4886 * You can iterate over all heads as follows:
4887 * \code
4888 * struct weston_head *head = NULL;
4889 *
4890 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4891 * ...
4892 * }
4893 * \endcode
4894 *
4895 * If you cause \c iter to be removed from the list, you cannot use it to
4896 * continue iterating. Removing any other item is safe.
4897 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004898 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004899 */
4900WL_EXPORT struct weston_head *
4901weston_compositor_iterate_heads(struct weston_compositor *compositor,
4902 struct weston_head *iter)
4903{
4904 struct wl_list *list = &compositor->head_list;
4905 struct wl_list *node;
4906
4907 assert(compositor);
4908 assert(!iter || iter->compositor == compositor);
4909
4910 if (iter)
4911 node = iter->compositor_link.next;
4912 else
4913 node = list->next;
4914
4915 assert(node);
4916 assert(!iter || node != &iter->compositor_link);
4917
4918 if (node == list)
4919 return NULL;
4920
4921 return container_of(node, struct weston_head, compositor_link);
4922}
4923
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004924/** Iterate over attached heads
4925 *
4926 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03004927 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004928 * \return The next attached head in the list.
4929 *
4930 * Returns all heads currently attached to the output.
4931 *
4932 * You can iterate over all heads as follows:
4933 * \code
4934 * struct weston_head *head = NULL;
4935 *
4936 * while ((head = weston_output_iterate_heads(output, head))) {
4937 * ...
4938 * }
4939 * \endcode
4940 *
4941 * If you cause \c iter to be removed from the list, you cannot use it to
4942 * continue iterating. Removing any other item is safe.
4943 *
Marius Vlad55d87362019-06-11 01:15:35 +03004944 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004945 */
4946WL_EXPORT struct weston_head *
4947weston_output_iterate_heads(struct weston_output *output,
4948 struct weston_head *iter)
4949{
4950 struct wl_list *list = &output->head_list;
4951 struct wl_list *node;
4952
4953 assert(output);
4954 assert(!iter || iter->output == output);
4955
4956 if (iter)
4957 node = iter->output_link.next;
4958 else
4959 node = list->next;
4960
4961 assert(node);
4962 assert(!iter || node != &iter->output_link);
4963
4964 if (node == list)
4965 return NULL;
4966
4967 return container_of(node, struct weston_head, output_link);
4968}
4969
Pekka Paalanendcac3512017-12-08 14:13:34 +02004970/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004971 *
4972 * \param output The output to attach to.
4973 * \param head The head that is not yet attached.
4974 * \return 0 on success, -1 on failure.
4975 *
4976 * Attaches the given head to the output. All heads of an output are clones
4977 * and share the resolution and timings.
4978 *
4979 * Cloning heads this way uses less resources than creating an output for
4980 * each head, but is not always possible due to environment, driver and hardware
4981 * limitations.
4982 *
4983 * On failure, the head remains unattached. Success of this function does not
4984 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004985 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004986 *
Marius Vlad55d87362019-06-11 01:15:35 +03004987 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004988 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004989WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004990weston_output_attach_head(struct weston_output *output,
4991 struct weston_head *head)
4992{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004993 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004994
4995 if (!wl_list_empty(&head->output_link))
4996 return -1;
4997
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004998 if (output->attach_head) {
4999 if (output->attach_head(output, head) < 0)
5000 return -1;
5001 } else if (!wl_list_empty(&output->head_list)) {
5002 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005003 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005004 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005005
5006 head->output = output;
5007 wl_list_insert(output->head_list.prev, &head->output_link);
5008
Pekka Paalanendcac3512017-12-08 14:13:34 +02005009 if (output->enabled) {
5010 weston_head_add_global(head);
5011
5012 head_names = weston_output_create_heads_string(output);
5013 weston_log("Output '%s' updated to have head(s) %s\n",
5014 output->name, head_names);
5015 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005016
5017 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005018 }
5019
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005020 return 0;
5021}
5022
5023/** Detach a head from its output
5024 *
5025 * \param head The head to detach.
5026 *
5027 * It is safe to detach a non-attached head.
5028 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005029 * If the head is attached to an enabled output and the output will be left
5030 * with no heads, the output will be disabled.
5031 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005032 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005033 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005034 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005035WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005036weston_head_detach(struct weston_head *head)
5037{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005038 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005039 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005040
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005041 wl_list_remove(&head->output_link);
5042 wl_list_init(&head->output_link);
5043 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005044
5045 if (!output)
5046 return;
5047
5048 if (output->detach_head)
5049 output->detach_head(output, head);
5050
5051 if (output->enabled) {
5052 weston_head_remove_global(head);
5053
Pekka Paalanena0106992017-12-08 16:11:17 +02005054 if (wl_list_empty(&output->head_list)) {
5055 weston_log("Output '%s' no heads left, disabling.\n",
5056 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005057 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005058 } else {
5059 head_names = weston_output_create_heads_string(output);
5060 weston_log("Output '%s' updated to have head(s) %s\n",
5061 output->name, head_names);
5062 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005063
5064 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005065 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005066 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005067}
5068
5069/** Destroy a head
5070 *
5071 * \param head The head to be released.
5072 *
5073 * Destroys the head. The caller is responsible for freeing the memory pointed
5074 * to by \c head.
5075 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005076 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005077 * \internal
5078 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005079WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005080weston_head_release(struct weston_head *head)
5081{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005082 wl_signal_emit(&head->destroy_signal, head);
5083
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005084 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005085
5086 free(head->make);
5087 free(head->model);
5088 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005089 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005090
5091 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005092}
5093
Pekka Paalanene19970f2017-08-28 14:11:02 +03005094static void
5095weston_head_set_device_changed(struct weston_head *head)
5096{
5097 head->device_changed = true;
5098
5099 if (head->compositor)
5100 weston_compositor_schedule_heads_changed(head->compositor);
5101}
5102
5103/** String equal comparison with NULLs being equal */
5104static bool
5105str_null_eq(const char *a, const char *b)
5106{
5107 if (!a && !b)
5108 return true;
5109
5110 if (!!a != !!b)
5111 return false;
5112
5113 return strcmp(a, b) == 0;
5114}
5115
Pekka Paalanen01f60212017-03-24 15:39:24 +02005116/** Store monitor make, model and serial number
5117 *
5118 * \param head The head to modify.
5119 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5120 * any string, or NULL for none.
5121 * \param model The monitor model or name, or a made-up string, or NULL for
5122 * none.
5123 * \param serialno The monitor serial number, a made-up string, or NULL for
5124 * none.
5125 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005126 * This may set the device_changed flag.
5127 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005128 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005129 * \internal
5130 */
5131WL_EXPORT void
5132weston_head_set_monitor_strings(struct weston_head *head,
5133 const char *make,
5134 const char *model,
5135 const char *serialno)
5136{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005137 if (str_null_eq(head->make, make) &&
5138 str_null_eq(head->model, model) &&
5139 str_null_eq(head->serial_number, serialno))
5140 return;
5141
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005142 free(head->make);
5143 free(head->model);
5144 free(head->serial_number);
5145
5146 head->make = make ? strdup(make) : NULL;
5147 head->model = model ? strdup(model) : NULL;
5148 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005149
5150 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005151}
5152
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005153/** Store display non-desktop status
5154 *
5155 * \param head The head to modify.
5156 * \param non_desktop Whether the head connects to a non-desktop display.
5157 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005158 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005159 * \internal
5160 */
5161WL_EXPORT void
5162weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5163{
5164 if (head->non_desktop == non_desktop)
5165 return;
5166
5167 head->non_desktop = non_desktop;
5168
5169 weston_head_set_device_changed(head);
5170}
5171
Pekka Paalanen01f60212017-03-24 15:39:24 +02005172/** Store physical image size
5173 *
5174 * \param head The head to modify.
5175 * \param mm_width Image area width in millimeters.
5176 * \param mm_height Image area height in millimeters.
5177 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005178 * This may set the device_changed flag.
5179 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005180 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005181 * \internal
5182 */
5183WL_EXPORT void
5184weston_head_set_physical_size(struct weston_head *head,
5185 int32_t mm_width, int32_t mm_height)
5186{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005187 if (head->mm_width == mm_width &&
5188 head->mm_height == mm_height)
5189 return;
5190
Pekka Paalanen01f60212017-03-24 15:39:24 +02005191 head->mm_width = mm_width;
5192 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005193
5194 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005195}
5196
5197/** Store monitor sub-pixel layout
5198 *
5199 * \param head The head to modify.
5200 * \param sp Sub-pixel layout. The possible values are:
5201 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5202 * - WL_OUTPUT_SUBPIXEL_NONE,
5203 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5204 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5205 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5206 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5207 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005208 * This may set the device_changed flag.
5209 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005210 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005211 * \internal
5212 */
5213WL_EXPORT void
5214weston_head_set_subpixel(struct weston_head *head,
5215 enum wl_output_subpixel sp)
5216{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005217 if (head->subpixel == sp)
5218 return;
5219
Pekka Paalanen01f60212017-03-24 15:39:24 +02005220 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005221
5222 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005223}
5224
5225/** Mark the monitor as internal
5226 *
5227 * This is used for embedded screens, like laptop panels.
5228 *
5229 * \param head The head to mark as internal.
5230 *
5231 * By default a head is external. The type is often inferred from the physical
5232 * connector type.
5233 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005234 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005235 * \internal
5236 */
5237WL_EXPORT void
5238weston_head_set_internal(struct weston_head *head)
5239{
5240 head->connection_internal = true;
5241}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005242
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005243/** Store connector status
5244 *
5245 * \param head The head to modify.
5246 * \param connected Whether the head is connected.
5247 *
5248 * Connectors are created as disconnected. This function can be used to
5249 * set the connector status.
5250 *
5251 * The status should be set to true when a physical connector is connected to
5252 * a video sink device like a monitor and to false when the connector is
5253 * disconnected. For nested backends, the connection status should reflect the
5254 * connection to the parent display server.
5255 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005256 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005257 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005258 *
5259 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005260 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005261 * \internal
5262 */
5263WL_EXPORT void
5264weston_head_set_connection_status(struct weston_head *head, bool connected)
5265{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005266 if (head->connected == connected)
5267 return;
5268
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005269 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005270
Pekka Paalanene19970f2017-08-28 14:11:02 +03005271 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005272}
5273
5274/** Is the head currently connected?
5275 *
5276 * \param head The head to query.
5277 * \return Connection status.
5278 *
5279 * Returns true if the head is physically connected to a monitor, or in
5280 * case of a nested backend returns true when there is a connection to the
5281 * parent display server.
5282 *
5283 * This is independent from the head being enabled.
5284 *
5285 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005286 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005287 */
5288WL_EXPORT bool
5289weston_head_is_connected(struct weston_head *head)
5290{
5291 return head->connected;
5292}
5293
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005294/** Is the head currently enabled?
5295 *
5296 * \param head The head to query.
5297 * \return Video status.
5298 *
5299 * Returns true if the head is currently transmitting a video stream.
5300 *
5301 * This is independent of the head being connected.
5302 *
5303 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005304 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005305 */
5306WL_EXPORT bool
5307weston_head_is_enabled(struct weston_head *head)
5308{
5309 if (!head->output)
5310 return false;
5311
5312 return head->output->enabled;
5313}
5314
Pekka Paalanene19970f2017-08-28 14:11:02 +03005315/** Has the device information changed?
5316 *
5317 * \param head The head to query.
5318 * \return True if the device information has changed since last reset.
5319 *
5320 * The information about the connected display device, e.g. a monitor, may
5321 * change without being disconnected in between. Changing information
5322 * causes a call to the heads_changed hook.
5323 *
5324 * The information includes make, model, serial number, physical size,
5325 * and sub-pixel type. The connection status is also included.
5326 *
5327 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005328 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005329 */
5330WL_EXPORT bool
5331weston_head_is_device_changed(struct weston_head *head)
5332{
5333 return head->device_changed;
5334}
5335
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005336/** Does the head represent a non-desktop display?
5337 *
5338 * \param head The head to query.
5339 * \return True if the device is a non-desktop display.
5340 *
5341 * Non-desktop heads are not attached to outputs by default.
5342 * This stops weston from extending the desktop onto head mounted displays.
5343 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005344 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005345 */
5346WL_EXPORT bool
5347weston_head_is_non_desktop(struct weston_head *head)
5348{
5349 return head->non_desktop;
5350}
5351
Pekka Paalanene19970f2017-08-28 14:11:02 +03005352/** Acknowledge device information change
5353 *
5354 * \param head The head to acknowledge.
5355 *
5356 * Clears the device changed flag on this head. When a compositor has processed
5357 * device information, it should call this to be able to notice further
5358 * changes.
5359 *
5360 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005361 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005362 */
5363WL_EXPORT void
5364weston_head_reset_device_changed(struct weston_head *head)
5365{
5366 head->device_changed = false;
5367}
5368
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005369/** Get the name of a head
5370 *
5371 * \param head The head to query.
5372 * \return The head's name, not NULL.
5373 *
5374 * The name depends on the backend. The DRM backend uses connector names,
5375 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005376 *
5377 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005378 */
5379WL_EXPORT const char *
5380weston_head_get_name(struct weston_head *head)
5381{
5382 return head->name;
5383}
5384
5385/** Get the output the head is attached to
5386 *
5387 * \param head The head to query.
5388 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005389 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005390 */
5391WL_EXPORT struct weston_output *
5392weston_head_get_output(struct weston_head *head)
5393{
5394 return head->output;
5395}
5396
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005397/** Add destroy callback for a head
5398 *
5399 * \param head The head to watch for.
5400 * \param listener The listener to add. The \c notify member must be set.
5401 *
5402 * Heads may get destroyed for various reasons by the backends. If a head is
5403 * attached to an output, the compositor should listen for head destruction
5404 * and reconfigure or destroy the output if necessary.
5405 *
5406 * The destroy callbacks will be called on weston_head destruction before any
5407 * automatic detaching from an associated weston_output and before any
5408 * weston_head information is lost.
5409 *
5410 * The \c data argument to the notify callback is the weston_head being
5411 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005412 *
5413 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005414 */
5415WL_EXPORT void
5416weston_head_add_destroy_listener(struct weston_head *head,
5417 struct wl_listener *listener)
5418{
5419 wl_signal_add(&head->destroy_signal, listener);
5420}
5421
5422/** Look up destroy listener for a head
5423 *
5424 * \param head The head to query.
5425 * \param notify The notify function used used for the added destroy listener.
5426 * \return The listener, or NULL if not found.
5427 *
5428 * This looks up the previously added destroy listener struct based on the
5429 * notify function it has. The listener can be used to access user data
5430 * through \c container_of().
5431 *
5432 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005433 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005434 */
5435WL_EXPORT struct wl_listener *
5436weston_head_get_destroy_listener(struct weston_head *head,
5437 wl_notify_func_t notify)
5438{
5439 return wl_signal_get(&head->destroy_signal, notify);
5440}
5441
David Fort0de859e2016-05-27 23:22:57 +02005442/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005443static void
David Fort0de859e2016-05-27 23:22:57 +02005444weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5445 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005446{
5447 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005448 bool start_resizing = false;
5449
5450 if (!delta_width)
5451 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005452
5453 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005454 if (output == resized_output) {
5455 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005456 continue;
5457 }
5458
David Fort0de859e2016-05-27 23:22:57 +02005459 if (start_resizing) {
5460 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005461 output->dirty = 1;
5462 }
5463 }
5464}
5465
Pekka Paalanend72bad22017-03-29 17:01:41 +03005466static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005467weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005468{
Scott Moreau850ca422012-05-21 15:21:25 -06005469 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005470
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005471 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005472 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005473
Scott Moreauccbf29d2012-02-22 14:21:41 -07005474 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005475 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005476 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005477 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005478 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005479 weston_matrix_scale(&output->matrix, magnification,
5480 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005481 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005482
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005483 switch (output->transform) {
5484 case WL_OUTPUT_TRANSFORM_FLIPPED:
5485 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5486 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5487 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5488 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5489 weston_matrix_scale(&output->matrix, -1, 1, 1);
5490 break;
5491 }
5492
5493 switch (output->transform) {
5494 default:
5495 case WL_OUTPUT_TRANSFORM_NORMAL:
5496 case WL_OUTPUT_TRANSFORM_FLIPPED:
5497 break;
5498 case WL_OUTPUT_TRANSFORM_90:
5499 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5500 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5501 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5502 break;
5503 case WL_OUTPUT_TRANSFORM_180:
5504 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5505 weston_matrix_translate(&output->matrix,
5506 -output->width, -output->height, 0);
5507 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5508 break;
5509 case WL_OUTPUT_TRANSFORM_270:
5510 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5511 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5512 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5513 break;
5514 }
5515
5516 if (output->current_scale != 1)
5517 weston_matrix_scale(&output->matrix,
5518 output->current_scale,
5519 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005520
Scott Moreauccbf29d2012-02-22 14:21:41 -07005521 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005522
5523 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005524}
5525
Scott Moreau1bad5db2012-08-18 01:04:05 -06005526static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005527weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005528{
5529 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005530 output->native_scale = scale;
5531 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005532
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005533 convert_size_by_transform_scale(&output->width, &output->height,
5534 output->current_mode->width,
5535 output->current_mode->height,
5536 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005537}
5538
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005539static void
5540weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005541{
5542 output->x = x;
5543 output->y = y;
5544
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005545 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005546 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005547
5548 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005549 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005550 output->width,
5551 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005552}
5553
Marius Vlad55d87362019-06-11 01:15:35 +03005554/**
5555 * \ingroup output
5556 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005557WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005558weston_output_move(struct weston_output *output, int x, int y)
5559{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005560 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005561 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005562 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005563
5564 output->move_x = x - output->x;
5565 output->move_y = y - output->y;
5566
5567 if (output->move_x == 0 && output->move_y == 0)
5568 return;
5569
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005570 weston_output_init_geometry(output, x, y);
5571
5572 output->dirty = 1;
5573
5574 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005575 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005576
5577 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005578 wl_list_for_each(head, &output->head_list, output_link) {
5579 wl_resource_for_each(resource, &head->resource_list) {
5580 wl_output_send_geometry(resource,
5581 output->x,
5582 output->y,
5583 head->mm_width,
5584 head->mm_height,
5585 head->subpixel,
5586 head->make,
5587 head->model,
5588 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005589
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005590 ver = wl_resource_get_version(resource);
5591 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5592 wl_output_send_done(resource);
5593 }
Roman Gilge97391c2019-03-29 13:01:06 +01005594
5595 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5596 zxdg_output_v1_send_logical_position(resource,
5597 output->x,
5598 output->y);
5599 zxdg_output_v1_send_done(resource);
5600 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005601 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005602}
5603
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005604/** Signal that a pending output is taken into use.
5605 *
5606 * Removes the output from the pending list and adds it to the compositor's
5607 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005608 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005609 * The output gets an internal ID assigned, and the wl_output global is
5610 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005611 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005612 * \param compositor The compositor instance.
5613 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005614 *
5615 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005616 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005617 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005618static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005619weston_compositor_add_output(struct weston_compositor *compositor,
5620 struct weston_output *output)
5621{
Armin Krezoviće5403842016-08-05 15:28:29 +02005622 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005623 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005624
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005625 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005626
5627 /* Verify we haven't reached the limit of 32 available output IDs */
5628 assert(ffs(~compositor->output_id_pool) > 0);
5629
5630 /* Invert the output id pool and look for the lowest numbered
5631 * switch (the least significant bit). Take that bit's position
5632 * as our ID, and mark it used in the compositor's output_id_pool.
5633 */
5634 output->id = ffs(~compositor->output_id_pool) - 1;
5635 compositor->output_id_pool |= 1u << output->id;
5636
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005637 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005638 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005639 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005640
Pekka Paalanendcac3512017-12-08 14:13:34 +02005641 wl_list_for_each(head, &output->head_list, output_link)
5642 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005643
Giulio Camuffob1147152015-05-06 21:41:57 +03005644 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005645
5646 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5647 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005648}
5649
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005650/** Transform device coordinates into global coordinates
5651 *
Marius Vlada2dace22019-06-12 16:05:44 +03005652 * \param output the weston_output object
5653 * \param[in] device_x X coordinate in device units.
5654 * \param[in] device_y Y coordinate in device units.
5655 * \param[out] x X coordinate in the global space.
5656 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005657 *
Marius Vlada2dace22019-06-12 16:05:44 +03005658 * Transforms coordinates from the device coordinate space (physical pixel
5659 * units) to the global coordinate space (logical pixel units). This takes
5660 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005661 *
Marius Vlad55d87362019-06-11 01:15:35 +03005662 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005663 * \internal
5664 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005665WL_EXPORT void
5666weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005667 double device_x, double device_y,
5668 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005669{
Derek Foreman0f679412014-10-02 13:41:17 -05005670 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005671 device_x,
5672 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005673 0.0,
5674 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005675
Derek Foreman67a18b92015-03-24 11:36:14 -05005676 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005677
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005678 *x = p.f[0] / p.f[3];
5679 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005680}
5681
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005682/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005683 *
5684 * \param output The weston_output object that is being removed.
5685 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005686 * The following happens:
5687 *
5688 * - The output assignments of all views in the current scenegraph are
5689 * recomputed.
5690 *
5691 * - Presentation feedback is discarded.
5692 *
5693 * - Compositor is notified that outputs were changed and
5694 * applies the necessary changes to re-layout outputs.
5695 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005696 * - The output is put back in the pending outputs list.
5697 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005698 * - Signal is emitted to notify all users of the weston_output
5699 * object that the output is being destroyed.
5700 *
5701 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005702 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005703 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005704 * - The output's internal ID is released.
5705 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005706 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005707 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005708 */
5709static void
5710weston_compositor_remove_output(struct weston_output *output)
5711{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005712 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005713 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005714 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005715
5716 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005717 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005718
Pekka Paalanenbccda712017-03-29 16:16:04 +03005719 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005720 if (view->output_mask & (1u << output->id))
5721 weston_view_assign_output(view);
5722 }
5723
5724 weston_presentation_feedback_discard_list(&output->feedback_list);
5725
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005726 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005727
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005728 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005729 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005730 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005731
Pekka Paalanenbccda712017-03-29 16:16:04 +03005732 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005733 wl_signal_emit(&output->destroy_signal, output);
5734
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005735 wl_list_for_each(head, &output->head_list, output_link)
5736 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005737
5738 compositor->output_id_pool &= ~(1u << output->id);
5739 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005740}
5741
5742/** Sets the output scale for a given output.
5743 *
5744 * \param output The weston_output object that the scale is set for.
5745 * \param scale Scale factor for the given output.
5746 *
5747 * It only supports setting scale for an output that
5748 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005749 *
Marius Vlad55d87362019-06-11 01:15:35 +03005750 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005751 */
5752WL_EXPORT void
5753weston_output_set_scale(struct weston_output *output,
5754 int32_t scale)
5755{
5756 /* We can only set scale on a disabled output */
5757 assert(!output->enabled);
5758
5759 /* We only want to set scale once */
5760 assert(!output->scale);
5761
5762 output->scale = scale;
5763}
5764
5765/** Sets the output transform for a given output.
5766 *
5767 * \param output The weston_output object that the transform is set for.
5768 * \param transform Transform value for the given output.
5769 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005770 * Refer to wl_output::transform section located at
5771 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5772 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005773 *
Marius Vlad55d87362019-06-11 01:15:35 +03005774 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005775 */
5776WL_EXPORT void
5777weston_output_set_transform(struct weston_output *output,
5778 uint32_t transform)
5779{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005780 struct weston_pointer_motion_event ev;
5781 struct wl_resource *resource;
5782 struct weston_seat *seat;
5783 pixman_region32_t old_region;
5784 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005785 struct weston_head *head;
5786 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005787
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005788 if (!output->enabled && output->transform == UINT32_MAX) {
5789 output->transform = transform;
5790 return;
5791 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005792
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005793 weston_output_transform_scale_init(output, transform, output->scale);
5794
5795 pixman_region32_init(&old_region);
5796 pixman_region32_copy(&old_region, &output->region);
5797
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005798 weston_output_init_geometry(output, output->x, output->y);
5799
5800 output->dirty = 1;
5801
5802 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005803 wl_list_for_each(head, &output->head_list, output_link) {
5804 wl_resource_for_each(resource, &head->resource_list) {
5805 wl_output_send_geometry(resource,
5806 output->x,
5807 output->y,
5808 head->mm_width,
5809 head->mm_height,
5810 head->subpixel,
5811 head->make,
5812 head->model,
5813 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005814
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005815 ver = wl_resource_get_version(resource);
5816 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5817 wl_output_send_done(resource);
5818 }
Roman Gilge97391c2019-03-29 13:01:06 +01005819 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5820 zxdg_output_v1_send_logical_position(resource,
5821 output->x,
5822 output->y);
5823 zxdg_output_v1_send_logical_size(resource,
5824 output->width,
5825 output->height);
5826 zxdg_output_v1_send_done(resource);
5827 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005828 }
5829
5830 /* we must ensure that pointers are inside output, otherwise they disappear */
5831 mid_x = output->x + output->width / 2;
5832 mid_y = output->y + output->height / 2;
5833
5834 ev.mask = WESTON_POINTER_MOTION_ABS;
5835 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5836 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5837
5838 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5839 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5840
5841 if (pointer && pixman_region32_contains_point(&old_region,
5842 wl_fixed_to_int(pointer->x),
5843 wl_fixed_to_int(pointer->y),
5844 NULL))
5845 weston_pointer_move(pointer, &ev);
5846 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005847}
5848
5849/** Initializes a weston_output object with enough data so
5850 ** an output can be configured.
5851 *
5852 * \param output The weston_output object to initialize
5853 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005854 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005855 *
5856 * Sets initial values for fields that are expected to be
5857 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005858 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005859 * The name is used in logs, and can be used by compositors as a configuration
5860 * identifier.
5861 *
Marius Vlad55d87362019-06-11 01:15:35 +03005862 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005863 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005864 */
5865WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005866weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005867 struct weston_compositor *compositor,
5868 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005869{
5870 output->compositor = compositor;
5871 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005872 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005873 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005874 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005875 output->enabled = false;
5876
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005877 wl_list_init(&output->head_list);
5878
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005879 /* Add some (in)sane defaults which can be used
5880 * for checking if an output was properly configured
5881 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005882 output->scale = 0;
5883 /* Can't use -1 on uint32_t and 0 is valid enum value */
5884 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005885
5886 pixman_region32_init(&output->previous_damage);
5887 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005888 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005889}
5890
5891/** Adds weston_output object to pending output list.
5892 *
5893 * \param output The weston_output object to add
5894 * \param compositor The compositor instance.
5895 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005896 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005897 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005898 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005899 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005900 */
5901WL_EXPORT void
5902weston_compositor_add_pending_output(struct weston_output *output,
5903 struct weston_compositor *compositor)
5904{
Pekka Paalanene952a012017-03-29 17:14:00 +03005905 assert(output->disable);
5906 assert(output->enable);
5907
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005908 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005909 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005910}
5911
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005912/** Create a string with the attached heads' names.
5913 *
5914 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03005915 *
5916 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005917 */
5918static char *
5919weston_output_create_heads_string(struct weston_output *output)
5920{
5921 FILE *fp;
5922 char *str = NULL;
5923 size_t size = 0;
5924 struct weston_head *head;
5925 const char *sep = "";
5926
5927 fp = open_memstream(&str, &size);
5928 if (!fp)
5929 return NULL;
5930
5931 wl_list_for_each(head, &output->head_list, output_link) {
5932 fprintf(fp, "%s%s", sep, head->name);
5933 sep = ", ";
5934 }
5935 fclose(fp);
5936
5937 return str;
5938}
5939
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005940/** Constructs a weston_output object that can be used by the compositor.
5941 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005942 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005943 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005944 *
5945 * Output coordinates are calculated and each new output is by default
5946 * assigned to the right of previous one.
5947 *
5948 * Sets up the transformation, zoom, and geometry of the output using
5949 * the properties that need to be configured by the compositor.
5950 *
5951 * Establishes a repaint timer for the output with the relevant display
5952 * object's event loop. See output_repaint_timer_handler().
5953 *
5954 * The output is assigned an ID. Weston can support up to 32 distinct
5955 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5956 * is referred to and used to find the first available ID number, and
5957 * then this ID is marked as used in output_id_pool.
5958 *
5959 * The output is also assigned a Wayland global with the wl_output
5960 * external interface.
5961 *
5962 * Backend specific function is called to set up the output output.
5963 *
5964 * Output is added to the compositor's output list
5965 *
5966 * If the backend specific function fails, the weston_output object
5967 * is returned to a state it was before calling this function and
5968 * is added to the compositor's pending_output_list in case it needs
5969 * to be reconfigured or just so it can be destroyed at shutdown.
5970 *
5971 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03005972 *
5973 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005974 */
5975WL_EXPORT int
5976weston_output_enable(struct weston_output *output)
5977{
Armin Krezović782f5df2016-09-30 14:11:11 +02005978 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005979 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005980 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005981 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005982 int x = 0, y = 0;
5983
Pekka Paalanencc201e42017-03-30 15:11:25 +03005984 if (output->enabled) {
5985 weston_log("Error: attempt to enable an enabled output '%s'\n",
5986 output->name);
5987 return -1;
5988 }
5989
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005990 if (wl_list_empty(&output->head_list)) {
5991 weston_log("Error: cannot enable output '%s' without heads.\n",
5992 output->name);
5993 return -1;
5994 }
5995
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005996 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5997 weston_log("Error: no video mode for output '%s'.\n",
5998 output->name);
5999 return -1;
6000 }
6001
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006002 wl_list_for_each(head, &output->head_list, output_link) {
6003 assert(head->make);
6004 assert(head->model);
6005 }
6006
Armin Krezović782f5df2016-09-30 14:11:11 +02006007 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006008 struct weston_output, link);
6009
Armin Krezović782f5df2016-09-30 14:11:11 +02006010 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006011 x = iterator->x + iterator->width;
6012
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006013 /* Make sure the scale is set up */
6014 assert(output->scale);
6015
6016 /* Make sure we have a transform set */
6017 assert(output->transform != UINT32_MAX);
6018
Armin Krezović782f5df2016-09-30 14:11:11 +02006019 output->x = x;
6020 output->y = y;
6021 output->dirty = 1;
6022 output->original_scale = output->scale;
6023
6024 weston_output_transform_scale_init(output, output->transform, output->scale);
6025 weston_output_init_zoom(output);
6026
6027 weston_output_init_geometry(output, x, y);
6028 weston_output_damage(output);
6029
6030 wl_signal_init(&output->frame_signal);
6031 wl_signal_init(&output->destroy_signal);
6032 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006033 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006034
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006035 /* Enable the output (set up the crtc or create a
6036 * window representing the output, set up the
6037 * renderer, etc)
6038 */
6039 if (output->enable(output) < 0) {
6040 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006041 return -1;
6042 }
6043
6044 weston_compositor_add_output(output->compositor, output);
6045
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006046 head_names = weston_output_create_heads_string(output);
6047 weston_log("Output '%s' enabled with head(s) %s\n",
6048 output->name, head_names);
6049 free(head_names);
6050
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006051 return 0;
6052}
6053
6054/** Converts a weston_output object to a pending output state, so it
6055 ** can be configured again or destroyed.
6056 *
6057 * \param output The weston_output object that needs to be disabled.
6058 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006059 * Calls a backend specific function to disable an output, in case
6060 * such function exists.
6061 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006062 * The backend specific disable function may choose to postpone the disabling
6063 * by returning a negative value, in which case this function returns early.
6064 * In that case the backend will guarantee the output will be disabled soon
6065 * by the backend calling this function again. One must not attempt to re-enable
6066 * the output until that happens.
6067 *
6068 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006069 * from weston's output_list (see weston_compositor_remove_output())
6070 * and is returned to a state it was before weston_output_enable()
6071 * was ran (see weston_output_enable_undo()).
6072 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006073 * See weston_output_init() for more information on the
6074 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006075 *
6076 * If the output has never been enabled yet, this function can still be
6077 * called to ensure that the output is actually turned off rather than left
6078 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006079 *
6080 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006081 */
6082WL_EXPORT void
6083weston_output_disable(struct weston_output *output)
6084{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006085 /* Should we rename this? */
6086 output->destroying = 1;
6087
Pekka Paalanenc65df642017-03-29 15:45:46 +03006088 /* Disable is called unconditionally also for not-enabled outputs,
6089 * because at compositor start-up, if there is an output that is
6090 * already on but the compositor wants to turn it off, we have to
6091 * forward the turn-off to the backend so it knows to do it.
6092 * The backend cannot initially turn off everything, because it
6093 * would cause unnecessary mode-sets for all outputs the compositor
6094 * wants to be on.
6095 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006096 if (output->disable(output) < 0)
6097 return;
6098
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006099 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006100 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006101
6102 output->destroying = 0;
6103}
6104
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006105/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006106 *
6107 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006108 *
6109 * If there are new or changed heads, calls the heads_changed hook and
6110 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006111 *
6112 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006113 */
6114WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006115weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006116{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006117 if (compositor->heads_changed_source) {
6118 wl_event_source_remove(compositor->heads_changed_source);
6119 weston_compositor_call_heads_changed(compositor);
6120 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006121}
6122
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006123/** Add destroy callback for an output
6124 *
6125 * \param output The output to watch.
6126 * \param listener The listener to add. The \c notify member must be set.
6127 *
6128 * The listener callback will be called when user destroys an output. This
6129 * may be delayed by a backend in some cases. The main purpose of the
6130 * listener is to allow hooking up custom data to the output. The custom data
6131 * can be fetched via weston_output_get_destroy_listener() followed by
6132 * container_of().
6133 *
6134 * The \c data argument to the notify callback is the weston_output being
6135 * destroyed.
6136 *
6137 * @note This is for the final destruction of an output, not when it gets
6138 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006139 *
6140 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006141 */
6142WL_EXPORT void
6143weston_output_add_destroy_listener(struct weston_output *output,
6144 struct wl_listener *listener)
6145{
6146 wl_signal_add(&output->user_destroy_signal, listener);
6147}
6148
6149/** Look up destroy listener for an output
6150 *
6151 * \param output The output to query.
6152 * \param notify The notify function used used for the added destroy listener.
6153 * \return The listener, or NULL if not found.
6154 *
6155 * This looks up the previously added destroy listener struct based on the
6156 * notify function it has. The listener can be used to access user data
6157 * through \c container_of().
6158 *
6159 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006160 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006161 */
6162WL_EXPORT struct wl_listener *
6163weston_output_get_destroy_listener(struct weston_output *output,
6164 wl_notify_func_t notify)
6165{
6166 return wl_signal_get(&output->user_destroy_signal, notify);
6167}
6168
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006169/** Uninitialize an output
6170 *
6171 * Removes the output from the list of enabled outputs if necessary, but
6172 * does not call the backend's output disable function. The output will no
6173 * longer be in the list of pending outputs either.
6174 *
6175 * All fields of weston_output become uninitialized, i.e. should not be used
6176 * anymore. The caller can free the memory after this.
6177 *
Marius Vlad55d87362019-06-11 01:15:35 +03006178 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006179 * \internal
6180 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006181WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006182weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006183{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006184 struct weston_head *head, *tmp;
6185
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006186 output->destroying = 1;
6187
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006188 wl_signal_emit(&output->user_destroy_signal, output);
6189
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006190 if (output->idle_repaint_source)
6191 wl_event_source_remove(output->idle_repaint_source);
6192
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006193 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006194 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006195
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006196 pixman_region32_fini(&output->region);
6197 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006198 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006199
6200 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6201 weston_head_detach(head);
6202
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006203 free(output->name);
6204}
6205
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006206/** Find an output by its given name
6207 *
6208 * \param compositor The compositor to search in.
6209 * \param name The output name to search for.
6210 * \return An existing output with the given name, or NULL if not found.
6211 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006212 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006213 */
6214WL_EXPORT struct weston_output *
6215weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6216 const char *name)
6217{
6218 struct weston_output *output;
6219
6220 wl_list_for_each(output, &compositor->output_list, link)
6221 if (strcmp(output->name, name) == 0)
6222 return output;
6223
6224 wl_list_for_each(output, &compositor->pending_output_list, link)
6225 if (strcmp(output->name, name) == 0)
6226 return output;
6227
6228 return NULL;
6229}
6230
6231/** Create a named output
6232 *
6233 * \param compositor The compositor.
6234 * \param name The name for the output.
6235 * \return A new \c weston_output, or NULL on failure.
6236 *
6237 * This creates a new weston_output that starts with no heads attached.
6238 *
6239 * An output must be configured and it must have at least one head before
6240 * it can be enabled.
6241 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006242 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006243 */
6244WL_EXPORT struct weston_output *
6245weston_compositor_create_output(struct weston_compositor *compositor,
6246 const char *name)
6247{
6248 assert(compositor->backend->create_output);
6249
6250 if (weston_compositor_find_output_by_name(compositor, name)) {
6251 weston_log("Warning: attempted to create an output with a "
6252 "duplicate name '%s'.\n", name);
6253 return NULL;
6254 }
6255
6256 return compositor->backend->create_output(compositor, name);
6257}
6258
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006259/** Create an output for an unused head
6260 *
6261 * \param compositor The compositor.
6262 * \param head The head to attach to the output.
6263 * \return A new \c weston_output, or NULL on failure.
6264 *
6265 * This creates a new weston_output that starts with the given head attached.
6266 * The output inherits the name of the head. The head must not be already
6267 * attached to another output.
6268 *
6269 * An output must be configured before it can be enabled.
6270 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006271 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006272 */
6273WL_EXPORT struct weston_output *
6274weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6275 struct weston_head *head)
6276{
6277 struct weston_output *output;
6278
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006279 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006280 if (!output)
6281 return NULL;
6282
6283 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006284 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006285 return NULL;
6286 }
6287
6288 return output;
6289}
6290
6291/** Destroy an output
6292 *
6293 * \param output The output to destroy.
6294 *
6295 * The heads attached to the given output are detached and become unused again.
6296 *
6297 * It is not necessary to explicitly destroy all outputs at compositor exit.
6298 * weston_compositor_destroy() will automatically destroy any remaining
6299 * outputs.
6300 *
Marius Vlad55d87362019-06-11 01:15:35 +03006301 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006302 */
6303WL_EXPORT void
6304weston_output_destroy(struct weston_output *output)
6305{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006306 output->destroy(output);
6307}
6308
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006309/** When you need a head...
6310 *
6311 * This function is a hack, used until all code has been converted to become
6312 * multi-head aware.
6313 *
6314 * \param output The weston_output whose head to get.
6315 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006316 *
6317 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006318 */
6319WL_EXPORT struct weston_head *
6320weston_output_get_first_head(struct weston_output *output)
6321{
6322 if (wl_list_empty(&output->head_list))
6323 return NULL;
6324
6325 return container_of(output->head_list.next,
6326 struct weston_head, output_link);
6327}
6328
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006329static void
Roman Gilge97391c2019-03-29 13:01:06 +01006330xdg_output_unlist(struct wl_resource *resource)
6331{
6332 wl_list_remove(wl_resource_get_link(resource));
6333}
6334
6335static void
6336xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6337{
6338 wl_resource_destroy(resource);
6339}
6340
6341static const struct zxdg_output_v1_interface xdg_output_interface = {
6342 xdg_output_destroy
6343};
6344
6345static void
6346xdg_output_manager_destroy(struct wl_client *client,
6347 struct wl_resource *resource)
6348{
6349 wl_resource_destroy(resource);
6350}
6351
6352static void
6353xdg_output_manager_get_xdg_output(struct wl_client *client,
6354 struct wl_resource *manager,
6355 uint32_t id,
6356 struct wl_resource *output_resource)
6357{
6358 int version = wl_resource_get_version(manager);
6359 struct weston_head *head = wl_resource_get_user_data(output_resource);
6360 struct weston_output *output = head->output;
6361 struct wl_resource *resource;
6362
6363 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6364 version, id);
6365 if (resource == NULL) {
6366 wl_client_post_no_memory(client);
6367 return;
6368 }
6369
6370 wl_list_insert(&head->xdg_output_resource_list,
6371 wl_resource_get_link(resource));
6372
6373 wl_resource_set_implementation(resource, &xdg_output_interface,
6374 NULL, xdg_output_unlist);
6375
6376 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6377 zxdg_output_v1_send_logical_size(resource,
6378 output->width,
6379 output->height);
6380 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6381 zxdg_output_v1_send_name(resource, head->name);
6382
6383 zxdg_output_v1_send_done(resource);
6384}
6385
6386static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6387 xdg_output_manager_destroy,
6388 xdg_output_manager_get_xdg_output
6389};
6390
6391static void
6392bind_xdg_output_manager(struct wl_client *client,
6393 void *data, uint32_t version, uint32_t id)
6394{
6395 struct wl_resource *resource;
6396
6397 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6398 version, id);
6399 if (resource == NULL) {
6400 wl_client_post_no_memory(client);
6401 return;
6402 }
6403
6404 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6405 NULL, NULL);
6406}
6407
6408static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006409destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006410{
Jonny Lamb74130762013-11-26 18:19:46 +01006411 struct weston_surface *surface =
6412 wl_resource_get_user_data(resource);
6413
Pekka Paalanen4826f872016-04-22 14:14:38 +03006414 if (!surface)
6415 return;
6416
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006417 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006418 surface->pending.buffer_viewport.buffer.src_width =
6419 wl_fixed_from_int(-1);
6420 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006421 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006422}
6423
6424static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006425viewport_destroy(struct wl_client *client,
6426 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006427{
6428 wl_resource_destroy(resource);
6429}
6430
6431static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006432viewport_set_source(struct wl_client *client,
6433 struct wl_resource *resource,
6434 wl_fixed_t src_x,
6435 wl_fixed_t src_y,
6436 wl_fixed_t src_width,
6437 wl_fixed_t src_height)
6438{
6439 struct weston_surface *surface =
6440 wl_resource_get_user_data(resource);
6441
Pekka Paalanen4826f872016-04-22 14:14:38 +03006442 if (!surface) {
6443 wl_resource_post_error(resource,
6444 WP_VIEWPORT_ERROR_NO_SURFACE,
6445 "wl_surface for this viewport is no longer exists");
6446 return;
6447 }
6448
6449 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006450 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006451
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006452 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006453 src_height == wl_fixed_from_int(-1) &&
6454 src_x == wl_fixed_from_int(-1) &&
6455 src_y == wl_fixed_from_int(-1)) {
6456 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006457 surface->pending.buffer_viewport.buffer.src_width =
6458 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006459 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006460 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006461 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006462
Pekka Paalanen201769a2016-04-26 14:42:11 +03006463 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006464 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006465 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006466 "wl_surface@%d viewport source "
6467 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6468 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006469 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006470 wl_fixed_to_double(src_height),
6471 wl_fixed_to_double(src_x),
6472 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006473 return;
6474 }
6475
6476 surface->pending.buffer_viewport.buffer.src_x = src_x;
6477 surface->pending.buffer_viewport.buffer.src_y = src_y;
6478 surface->pending.buffer_viewport.buffer.src_width = src_width;
6479 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006480 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006481}
6482
6483static void
6484viewport_set_destination(struct wl_client *client,
6485 struct wl_resource *resource,
6486 int32_t dst_width,
6487 int32_t dst_height)
6488{
6489 struct weston_surface *surface =
6490 wl_resource_get_user_data(resource);
6491
Pekka Paalanen4826f872016-04-22 14:14:38 +03006492 if (!surface) {
6493 wl_resource_post_error(resource,
6494 WP_VIEWPORT_ERROR_NO_SURFACE,
6495 "wl_surface for this viewport no longer exists");
6496 return;
6497 }
6498
6499 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006500
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006501 if (dst_width == -1 && dst_height == -1) {
6502 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006503 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006504 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006505 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006506 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006507
6508 if (dst_width <= 0 || dst_height <= 0) {
6509 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006510 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006511 "destination size must be positive (%dx%d)",
6512 dst_width, dst_height);
6513 return;
6514 }
6515
6516 surface->pending.buffer_viewport.surface.width = dst_width;
6517 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006518 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006519}
6520
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006521static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006522 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006523 viewport_set_source,
6524 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006525};
6526
6527static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006528viewporter_destroy(struct wl_client *client,
6529 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006530{
6531 wl_resource_destroy(resource);
6532}
6533
6534static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006535viewporter_get_viewport(struct wl_client *client,
6536 struct wl_resource *viewporter,
6537 uint32_t id,
6538 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006539{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006540 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006541 struct weston_surface *surface =
6542 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006543 struct wl_resource *resource;
6544
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006545 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006546 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006547 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006548 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006549 return;
6550 }
6551
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006552 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006553 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006554 if (resource == NULL) {
6555 wl_client_post_no_memory(client);
6556 return;
6557 }
6558
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006559 wl_resource_set_implementation(resource, &viewport_interface,
6560 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006561
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006562 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006563}
6564
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006565static const struct wp_viewporter_interface viewporter_interface = {
6566 viewporter_destroy,
6567 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006568};
6569
6570static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006571bind_viewporter(struct wl_client *client,
6572 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006573{
6574 struct wl_resource *resource;
6575
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006576 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006577 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006578 if (resource == NULL) {
6579 wl_client_post_no_memory(client);
6580 return;
6581 }
6582
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006583 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006584 NULL, NULL);
6585}
6586
6587static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006588destroy_presentation_feedback(struct wl_resource *feedback_resource)
6589{
6590 struct weston_presentation_feedback *feedback;
6591
6592 feedback = wl_resource_get_user_data(feedback_resource);
6593
6594 wl_list_remove(&feedback->link);
6595 free(feedback);
6596}
6597
6598static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006599presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6600{
6601 wl_resource_destroy(resource);
6602}
6603
6604static void
6605presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006606 struct wl_resource *presentation_resource,
6607 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006608 uint32_t callback)
6609{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006610 struct weston_surface *surface;
6611 struct weston_presentation_feedback *feedback;
6612
6613 surface = wl_resource_get_user_data(surface_resource);
6614
Bryce Harringtonde16d892014-11-20 22:21:57 -08006615 feedback = zalloc(sizeof *feedback);
6616 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006617 goto err_calloc;
6618
6619 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006620 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006621 1, callback);
6622 if (!feedback->resource)
6623 goto err_create;
6624
6625 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6626 destroy_presentation_feedback);
6627
6628 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6629
6630 return;
6631
6632err_create:
6633 free(feedback);
6634
6635err_calloc:
6636 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006637}
6638
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006639static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006640 presentation_destroy,
6641 presentation_feedback
6642};
6643
6644static void
6645bind_presentation(struct wl_client *client,
6646 void *data, uint32_t version, uint32_t id)
6647{
6648 struct weston_compositor *compositor = data;
6649 struct wl_resource *resource;
6650
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006651 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006652 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006653 if (resource == NULL) {
6654 wl_client_post_no_memory(client);
6655 return;
6656 }
6657
6658 wl_resource_set_implementation(resource, &presentation_implementation,
6659 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006660 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006661}
6662
6663static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006664compositor_bind(struct wl_client *client,
6665 void *data, uint32_t version, uint32_t id)
6666{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006667 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006668 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006669
Jason Ekstranda85118c2013-06-27 20:17:02 -05006670 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006671 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006672 if (resource == NULL) {
6673 wl_client_post_no_memory(client);
6674 return;
6675 }
6676
6677 wl_resource_set_implementation(resource, &compositor_interface,
6678 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006679}
6680
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006681WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006682weston_environment_get_fd(const char *env)
6683{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006684 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006685 int fd, flags;
6686
6687 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006688 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006689 return -1;
6690
6691 flags = fcntl(fd, F_GETFD);
6692 if (flags == -1)
6693 return -1;
6694
6695 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6696 unsetenv(env);
6697
6698 return fd;
6699}
6700
Pekka Paalanenb5026542014-11-12 15:09:24 +02006701static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006702timeline_key_binding_handler(struct weston_keyboard *keyboard,
6703 const struct timespec *time, uint32_t key,
6704 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006705{
6706 struct weston_compositor *compositor = data;
6707
6708 if (weston_timeline_enabled_)
6709 weston_timeline_close();
6710 else
6711 weston_timeline_open(compositor);
6712}
6713
Daniel Stonece62cb32018-07-20 09:46:24 +01006714static const char *
6715output_repaint_status_text(struct weston_output *output)
6716{
6717 switch (output->repaint_status) {
6718 case REPAINT_NOT_SCHEDULED:
6719 return "no repaint";
6720 case REPAINT_BEGIN_FROM_IDLE:
6721 return "start_repaint_loop scheduled";
6722 case REPAINT_SCHEDULED:
6723 return "repaint scheduled";
6724 case REPAINT_AWAITING_COMPLETION:
6725 return "awaiting completion";
6726 }
6727
6728 assert(!"output_repaint_status_text missing enum");
6729 return NULL;
6730}
6731
6732static void
6733debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6734{
6735 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6736 struct wl_shm_buffer *shm;
6737 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006738 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006739
6740 if (!buffer) {
6741 fprintf(fp, "\t\t[buffer not available]\n");
6742 return;
6743 }
6744
6745 shm = wl_shm_buffer_get(buffer->resource);
6746 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006747 uint32_t _format = wl_shm_buffer_get_format(shm);
6748 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006749 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006750 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6751 (unsigned long) _format,
6752 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006753 return;
6754 }
6755
6756 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6757 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006758 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006759 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006760 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6761 (unsigned long) dmabuf->attributes.format,
6762 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006763 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6764 (unsigned long long) dmabuf->attributes.modifier[0]);
6765 return;
6766 }
6767
Marius Vlad253ba9a2019-03-17 18:22:21 +02006768 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006769}
6770
6771static void
6772debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6773{
6774 struct weston_compositor *ec = view->surface->compositor;
6775 struct weston_output *output;
6776 char desc[512];
6777 pixman_box32_t *box;
6778 uint32_t surface_id = 0;
6779 pid_t pid = 0;
6780
6781 if (view->surface->resource) {
6782 struct wl_resource *resource = view->surface->resource;
6783 wl_client_get_credentials(wl_resource_get_client(resource),
6784 &pid, NULL, NULL);
6785 surface_id = wl_resource_get_id(view->surface->resource);
6786 }
6787
6788 if (!view->surface->get_label ||
6789 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6790 strcpy(desc, "[no description available]");
6791 }
6792 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6793 view_idx, view->surface->role_name, pid, surface_id,
6794 desc, view);
6795
6796 box = pixman_region32_extents(&view->transform.boundingbox);
6797 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6798 box->x1, box->y1, box->x2, box->y2);
6799 box = pixman_region32_extents(&view->transform.opaque);
6800
6801 if (pixman_region32_equal(&view->transform.opaque,
6802 &view->transform.boundingbox)) {
6803 fprintf(fp, "\t\t[fully opaque]\n");
6804 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6805 fprintf(fp, "\t\t[not opaque]\n");
6806 } else {
6807 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6808 box->x1, box->y1, box->x2, box->y2);
6809 }
6810
6811 if (view->alpha < 1.0)
6812 fprintf(fp, "\t\talpha: %f\n", view->alpha);
6813
6814 if (view->output_mask != 0) {
6815 bool first_output = true;
6816 fprintf(fp, "\t\toutputs: ");
6817 wl_list_for_each(output, &ec->output_list, link) {
6818 if (!(view->output_mask & (1 << output->id)))
6819 continue;
6820 fprintf(fp, "%s%d (%s)%s",
6821 (first_output) ? "" : ", ",
6822 output->id, output->name,
6823 (view->output == output) ? " (primary)" : "");
6824 first_output = false;
6825 }
6826 } else {
6827 fprintf(fp, "\t\t[no outputs]");
6828 }
6829
6830 fprintf(fp, "\n");
6831
6832 debug_scene_view_print_buffer(fp, view);
6833}
6834
Marius Vlad433f4e72019-02-17 22:14:23 +02006835static void
6836debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02006837 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02006838{
6839 struct weston_subsurface *sub;
6840 struct weston_view *ev;
6841
6842 /*
6843 * print the view first, then we recursively go on printing
6844 * sub-surfaces. We bail out once no more sub-surfaces are available.
6845 */
Marius Vlada6acfa82019-03-17 18:10:09 +02006846 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02006847
6848 /* no more sub-surfaces */
6849 if (wl_list_empty(&view->surface->subsurface_list))
6850 return;
6851
6852 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
6853 wl_list_for_each(ev, &sub->surface->views, surface_link) {
6854 /* do not print again the parent view */
6855 if (view == ev)
6856 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02006857
6858 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02006859 debug_scene_view_print_tree(ev, fp, view_idx);
6860 }
6861 }
6862}
6863
Daniel Stonece62cb32018-07-20 09:46:24 +01006864/**
6865 * Output information on how libweston is currently composing the scene
6866 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006867 *
6868 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01006869 */
6870WL_EXPORT char *
6871weston_compositor_print_scene_graph(struct weston_compositor *ec)
6872{
6873 struct weston_output *output;
6874 struct weston_layer *layer;
6875 struct timespec now;
6876 int layer_idx = 0;
6877 FILE *fp;
6878 char *ret;
6879 size_t len;
6880 int err;
6881
6882 fp = open_memstream(&ret, &len);
6883 assert(fp);
6884
6885 weston_compositor_read_presentation_clock(ec, &now);
6886 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
6887 now.tv_sec, now.tv_nsec);
6888
6889 wl_list_for_each(output, &ec->output_list, link) {
6890 struct weston_head *head;
6891 int head_idx = 0;
6892
6893 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
6894 assert(output->enabled);
6895
6896 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
6897 output->x, output->y,
6898 output->x + output->width,
6899 output->y + output->height);
6900 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
6901 output->current_mode->width,
6902 output->current_mode->height,
6903 output->current_mode->refresh / 1000.0);
6904 fprintf(fp, "\tscale: %d\n", output->scale);
6905
6906 fprintf(fp, "\trepaint status: %s\n",
6907 output_repaint_status_text(output));
6908 if (output->repaint_status == REPAINT_SCHEDULED)
6909 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
6910 output->next_repaint.tv_sec,
6911 output->next_repaint.tv_nsec);
6912
6913 wl_list_for_each(head, &output->head_list, output_link) {
6914 fprintf(fp, "\tHead %d (%s): %sconnected\n",
6915 head_idx++, head->name,
6916 (head->connected) ? "" : "not ");
6917 }
6918 }
6919
6920 fprintf(fp, "\n");
6921
6922 wl_list_for_each(layer, &ec->layer_list, link) {
6923 struct weston_view *view;
6924 int view_idx = 0;
6925
6926 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
6927 (unsigned long) layer->position);
6928
6929 if (!weston_layer_mask_is_infinite(layer)) {
6930 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
6931 layer->mask.x1, layer->mask.y1,
6932 layer->mask.x2, layer->mask.y2);
6933 }
6934
Marius Vlada6acfa82019-03-17 18:10:09 +02006935 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
6936 debug_scene_view_print_tree(view, fp, &view_idx);
6937 view_idx++;
6938 }
Daniel Stonece62cb32018-07-20 09:46:24 +01006939
6940 if (wl_list_empty(&layer->view_list.link))
6941 fprintf(fp, "\t[no views]\n");
6942
6943 fprintf(fp, "\n");
6944 }
6945
6946 err = fclose(fp);
6947 assert(err == 0);
6948
6949 return ret;
6950}
6951
6952/**
6953 * Called when the 'scene-graph' debug scope is bound by a client. This
6954 * one-shot weston-debug scope prints the current scene graph when bound,
6955 * and then terminates the stream.
6956 */
6957static void
6958debug_scene_graph_cb(struct weston_debug_stream *stream, void *data)
6959{
6960 struct weston_compositor *ec = data;
6961 char *str = weston_compositor_print_scene_graph(ec);
6962
6963 weston_debug_stream_printf(stream, "%s", str);
6964 free(str);
6965 weston_debug_stream_complete(stream);
6966}
6967
Giulio Camuffo459137b2014-10-11 23:56:24 +03006968/** Create the compositor.
6969 *
6970 * This functions creates and initializes a compositor instance.
6971 *
6972 * \param display The Wayland display to be used.
6973 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03006974 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03006975 * using the \ref weston_compositor_get_user_data function.
6976 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006977 *
6978 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03006979 */
6980WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03006981weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03006982 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03006983 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006984{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006985 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006986 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006987
Giulio Camuffo459137b2014-10-11 23:56:24 +03006988 ec = zalloc(sizeof *ec);
6989 if (!ec)
6990 return NULL;
6991
6992 ec->wl_display = display;
6993 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006994 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006995 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006996 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006997 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006998 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006999 wl_signal_init(&ec->idle_signal);
7000 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007001 wl_signal_init(&ec->show_input_panel_signal);
7002 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007003 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007004 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007005 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007006 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007007 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007008 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007009 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007010 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007011 wl_signal_init(&ec->session_signal);
7012 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007013
Casey Dahlin58ba1372012-04-19 22:50:08 -04007014 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007015 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007016
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007017 ec->activate_serial = 1;
7018
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007019 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7020
Derek Foreman152254b2015-11-26 14:17:48 -06007021 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007022 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007023 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007024
Giulio Camuffo954f1832014-10-11 18:27:30 +03007025 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007026 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007027 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007028
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007029 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007030 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007031 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007032
Roman Gilge97391c2019-03-29 13:01:06 +01007033 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7034 ec, bind_xdg_output_manager))
7035 goto fail;
7036
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007037 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007038 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007039 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007040
Marius Vlad3d7d9782019-04-17 12:35:38 +03007041 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007042 goto fail;
7043
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007044 if (weston_input_init(ec) != 0)
7045 goto fail;
7046
Jason Ekstranda7af7042013-10-12 22:38:11 -05007047 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007048 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007049 wl_list_init(&ec->layer_list);
7050 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007051 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007052 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007053 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007054 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007055 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007056 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007057 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007058 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007059 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007060
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007061 wl_list_init(&ec->plugin_api_list);
7062
Xiong Zhang97116532013-10-23 13:58:31 +08007063 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007064 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007065
Giulio Camuffo459137b2014-10-11 23:56:24 +03007066 wl_data_device_manager_init(ec->wl_display);
7067
7068 wl_display_init_shm(ec->wl_display);
7069
7070 loop = wl_display_get_event_loop(ec->wl_display);
7071 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007072 ec->repaint_timer =
7073 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7074 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007075
Quentin Glidic82681572016-12-17 13:40:51 +01007076 weston_layer_init(&ec->fade_layer, ec);
7077 weston_layer_init(&ec->cursor_layer, ec);
7078
7079 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7080 weston_layer_set_position(&ec->cursor_layer,
7081 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007082
7083 weston_compositor_add_debug_binding(ec, KEY_T,
7084 timeline_key_binding_handler, ec);
7085
Daniel Stonece62cb32018-07-20 09:46:24 +01007086 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03007087 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
7088 "Scene graph details\n",
7089 debug_scene_graph_cb,
7090 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007091
Giulio Camuffo459137b2014-10-11 23:56:24 +03007092 return ec;
7093
7094fail:
7095 free(ec);
7096 return NULL;
7097}
7098
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007099/** weston_compositor_shutdown
7100 * \ingroup compositor
7101 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007102WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007103weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007104{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007105 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007106
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007107 wl_event_source_remove(ec->idle_source);
7108
Matt Roper361d2ad2011-08-29 13:52:23 -07007109 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007110 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007111 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007112
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007113 /* Destroy all pending outputs associated with this compositor */
7114 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7115 output->destroy(output);
7116
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007117 if (ec->renderer)
7118 ec->renderer->destroy(ec);
7119
Daniel Stone325fc2d2012-05-30 16:31:58 +01007120 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007121 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007122 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007123 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007124 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007125 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007126
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007127 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007128}
7129
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007130/** weston_compositor_exit_with_code
7131 * \ingroup compositor
7132 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007133WL_EXPORT void
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007134weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007135 int exit_code)
7136{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007137 if (compositor->exit_code == EXIT_SUCCESS)
7138 compositor->exit_code = exit_code;
7139
Giulio Camuffo459137b2014-10-11 23:56:24 +03007140 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007141}
7142
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007143/** weston_compositor_set_default_pointer_grab
7144 * \ingroup compositor
7145 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007146WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007147weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7148 const struct weston_pointer_grab_interface *interface)
7149{
7150 struct weston_seat *seat;
7151
7152 ec->default_pointer_grab = interface;
7153 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007154 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7155
7156 if (pointer)
7157 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007158 }
7159}
7160
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007161/** weston_compositor_set_presentation_clock
7162 * \ingroup compositor
7163 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007164WL_EXPORT int
7165weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7166 clockid_t clk_id)
7167{
7168 struct timespec ts;
7169
7170 if (clock_gettime(clk_id, &ts) < 0)
7171 return -1;
7172
7173 compositor->presentation_clock = clk_id;
7174
7175 return 0;
7176}
7177
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007178/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007179 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007180 *
7181 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007182 */
7183WL_EXPORT int
7184weston_compositor_set_presentation_clock_software(
7185 struct weston_compositor *compositor)
7186{
7187 /* In order of preference */
7188 static const clockid_t clocks[] = {
7189 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7190 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7191 CLOCK_MONOTONIC, /* no jumps, may crawl */
7192 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7193 CLOCK_REALTIME /* may jump and crawl */
7194 };
7195 unsigned i;
7196
7197 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7198 if (weston_compositor_set_presentation_clock(compositor,
7199 clocks[i]) == 0)
7200 return 0;
7201
7202 weston_log("Error: no suitable presentation clock available.\n");
7203
7204 return -1;
7205}
7206
Pekka Paalanen662f3842015-03-18 12:17:26 +02007207/** Read the current time from the Presentation clock
7208 *
7209 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007210 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007211 *
7212 * \note Reading the current time in user space is always imprecise to some
7213 * degree.
7214 *
7215 * This function is never meant to fail. If reading the clock does fail,
7216 * an error message is logged and a zero time is returned. Callers are not
7217 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007218 *
7219 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007220 */
7221WL_EXPORT void
7222weston_compositor_read_presentation_clock(
7223 const struct weston_compositor *compositor,
7224 struct timespec *ts)
7225{
7226 static bool warned;
7227 int ret;
7228
7229 ret = clock_gettime(compositor->presentation_clock, ts);
7230 if (ret < 0) {
7231 ts->tv_sec = 0;
7232 ts->tv_nsec = 0;
7233
7234 if (!warned)
7235 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007236 "the presentation clock %#x: '%s' (%d)\n",
7237 compositor->presentation_clock,
7238 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007239 warned = true;
7240 }
7241}
7242
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007243/** Import dmabuf buffer into current renderer
7244 *
7245 * \param compositor
7246 * \param buffer the dmabuf buffer to import
7247 * \return true on usable buffers, false otherwise
7248 *
7249 * This function tests that the linux_dmabuf_buffer is usable
7250 * for the current renderer. Returns false on unusable buffers. Usually
7251 * usability is tested by importing the dmabufs for composition.
7252 *
7253 * This hook is also used for detecting if the renderer supports
7254 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7255 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007256 *
7257 * \ingroup compositor
7258 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007259WL_EXPORT bool
7260weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7261 struct linux_dmabuf_buffer *buffer)
7262{
7263 struct weston_renderer *renderer;
7264
7265 renderer = compositor->renderer;
7266
7267 if (renderer->import_dmabuf == NULL)
7268 return false;
7269
7270 return renderer->import_dmabuf(compositor, buffer);
7271}
7272
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007273WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007274weston_version(int *major, int *minor, int *micro)
7275{
7276 *major = WESTON_VERSION_MAJOR;
7277 *minor = WESTON_VERSION_MINOR;
7278 *micro = WESTON_VERSION_MICRO;
7279}
7280
Daniel Stonee03c1112016-11-24 20:45:45 +00007281/**
7282 * Attempts to find a module path from the module map specified in the
7283 * environment. If found, writes the full path into the path variable.
7284 *
7285 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7286 * each entry is of the form "name=path" and entries are separated by
7287 * semicolons. Whitespace is significant.
7288 *
7289 * \param name The name to search for.
7290 * \param path Where the path is written to if found.
7291 * \param path_len Allocated bytes at \c path .
7292 * \returns The length of the string written to path on success, or 0 if the
7293 * module was not specified in the environment map or path_len was too small.
7294 */
7295WL_EXPORT size_t
7296weston_module_path_from_env(const char *name, char *path, size_t path_len)
7297{
7298 const char *mapping = getenv("WESTON_MODULE_MAP");
7299 const char *end;
7300 const int name_len = strlen(name);
7301
7302 if (!mapping)
7303 return 0;
7304
7305 end = mapping + strlen(mapping);
7306 while (mapping < end && *mapping) {
7307 const char *filename, *next;
7308
7309 /* early out: impossibly short string */
7310 if (end - mapping < name_len + 1)
7311 return 0;
7312
7313 filename = &mapping[name_len + 1];
7314 next = strchrnul(mapping, ';');
7315
7316 if (strncmp(mapping, name, name_len) == 0 &&
7317 mapping[name_len] == '=') {
7318 size_t file_len = next - filename; /* no trailing NUL */
7319 if (file_len >= path_len)
7320 return 0;
7321 strncpy(path, filename, file_len);
7322 path[file_len] = '\0';
7323 return file_len;
7324 }
7325
7326 mapping = next + 1;
7327 }
7328
7329 return 0;
7330}
7331
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007332WL_EXPORT void *
7333weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007334{
7335 char path[PATH_MAX];
7336 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007337 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007338
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007339 if (name == NULL)
7340 return NULL;
7341
Derek Foreman3f86e502015-06-08 11:46:54 -05007342 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007343 len = weston_module_path_from_env(name, path, sizeof path);
7344 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007345 len = snprintf(path, sizeof path, "%s/%s",
7346 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007347 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007348 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007349 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007350
Daniel Stonebeb97e52016-11-28 12:13:54 +00007351 /* snprintf returns the length of the string it would've written,
7352 * _excluding_ the NUL byte. So even being equal to the size of
7353 * our buffer is an error here. */
7354 if (len >= sizeof path)
7355 return NULL;
7356
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007357 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7358 if (module) {
7359 weston_log("Module '%s' already loaded\n", path);
7360 dlclose(module);
7361 return NULL;
7362 }
7363
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007364 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007365 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007366 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007367 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007368 return NULL;
7369 }
7370
7371 init = dlsym(module, entrypoint);
7372 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007373 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007374 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007375 return NULL;
7376 }
7377
7378 return init;
7379}
7380
Giulio Camuffo459137b2014-10-11 23:56:24 +03007381
7382/** Destroys the compositor.
7383 *
7384 * This function cleans up the compositor state and destroys it.
7385 *
7386 * \param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007387 *
7388 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007389 */
7390WL_EXPORT void
7391weston_compositor_destroy(struct weston_compositor *compositor)
7392{
7393 /* prevent further rendering while shutting down */
7394 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7395
7396 wl_signal_emit(&compositor->destroy_signal, compositor);
7397
7398 weston_compositor_xkb_destroy(compositor);
7399
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007400 if (compositor->backend)
7401 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007402
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007403 /* The backend is responsible for destroying the heads. */
7404 assert(wl_list_empty(&compositor->head_list));
7405
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007406 weston_plugin_api_destroy_list(compositor);
7407
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007408 if (compositor->heads_changed_source)
7409 wl_event_source_remove(compositor->heads_changed_source);
7410
Marius Vlad7e4db952019-04-17 13:47:06 +03007411 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007412 compositor->debug_scene = NULL;
Marius Vlad3d7d9782019-04-17 12:35:38 +03007413 weston_log_ctx_compositor_destroy(compositor);
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007414
Giulio Camuffo459137b2014-10-11 23:56:24 +03007415 free(compositor);
7416}
7417
7418/** Instruct the compositor to exit.
7419 *
7420 * This functions does not directly destroy the compositor object, it merely
7421 * command it to start the tear down process. It is not guaranteed that the
7422 * tear down will happen immediately.
7423 *
7424 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007425 *
7426 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007427 */
7428WL_EXPORT void
7429weston_compositor_exit(struct weston_compositor *compositor)
7430{
7431 compositor->exit(compositor);
7432}
7433
7434/** Return the user data stored in the compositor.
7435 *
7436 * This function returns the user data pointer set with user_data parameter
7437 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007438 *
7439 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007440 */
7441WL_EXPORT void *
7442weston_compositor_get_user_data(struct weston_compositor *compositor)
7443{
7444 return compositor->user_data;
7445}
Pekka Paalanendd186732016-06-03 14:49:54 +03007446
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007447static const char * const backend_map[] = {
7448 [WESTON_BACKEND_DRM] = "drm-backend.so",
7449 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7450 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7451 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7452 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7453 [WESTON_BACKEND_X11] = "x11-backend.so",
7454};
7455
Pekka Paalanendd186732016-06-03 14:49:54 +03007456/** Load a backend into a weston_compositor
7457 *
7458 * A backend must be loaded to make a weston_compositor work. A backend
7459 * provides input and output capabilities, and determines the renderer to use.
7460 *
7461 * \param compositor A compositor that has not had a backend loaded yet.
7462 * \param backend Name of the backend file.
7463 * \param config_base A pointer to a backend-specific configuration
7464 * structure's 'base' member.
7465 *
7466 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007467 *
7468 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007469 */
7470WL_EXPORT int
7471weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007472 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007473 struct weston_backend_config *config_base)
7474{
7475 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007476 struct weston_backend_config *config_base);
7477
Pekka Paalanend7e35112017-08-29 17:04:12 +03007478 if (compositor->backend) {
7479 weston_log("Error: attempt to load a backend when one is already loaded\n");
7480 return -1;
7481 }
7482
Quentin Glidic887c0182016-07-10 11:00:53 +02007483 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007484 return -1;
7485
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007486 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007487 if (!backend_init)
7488 return -1;
7489
Pekka Paalanend7e35112017-08-29 17:04:12 +03007490 if (backend_init(compositor, config_base) < 0) {
7491 compositor->backend = NULL;
7492 return -1;
7493 }
7494
7495 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007496}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007497
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007498/** weston_compositor_load_xwayland
7499 * \ingroup compositor
7500 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007501WL_EXPORT int
7502weston_compositor_load_xwayland(struct weston_compositor *compositor)
7503{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007504 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007505
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007506 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007507 if (!module_init)
7508 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007509 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007510 return -1;
7511 return 0;
7512}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007513
7514/** Resolve an internal compositor error by disconnecting the client.
7515 *
7516 * This function is used in cases when the wl_buffer turns out
7517 * unusable and there is no fallback path.
7518 *
7519 * It is possible the fault is caused by a compositor bug, the underlying
7520 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7521 * In any case, the options are to either composite garbage or nothing,
7522 * or disconnect the client. This is a helper function for the latter.
7523 *
7524 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7525 *
7526 * \param buffer The weston buffer that is unusable.
7527 * \param msg A custom error message attached to the protocol error.
7528 */
7529WL_EXPORT void
7530weston_buffer_send_server_error(struct weston_buffer *buffer,
7531 const char *msg)
7532{
7533 struct wl_client *client;
7534 struct wl_resource *display_resource;
7535 uint32_t id;
7536
7537 assert(buffer->resource);
7538 id = wl_resource_get_id(buffer->resource);
7539 client = wl_resource_get_client(buffer->resource);
7540 display_resource = wl_client_get_object(client, 1);
7541
7542 assert(display_resource);
7543 wl_resource_post_error(display_resource,
7544 WL_DISPLAY_ERROR_INVALID_OBJECT,
7545 "server error with "
7546 "wl_buffer@%u: %s", id, msg);
7547}