blob: deb2edd7f3b6618e7daef1f58a8275f7530b6163 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanen925788f2018-04-19 14:20:01 +03004 * Copyright © 2012-2018 Collabora, Ltd.
5 * Copyright © 2017, 2018 General Electric Company
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05006 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07007 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050014 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070015 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050027 */
28
Kristian Høgsberga9410222011-01-14 17:22:35 -050029#include "config.h"
30
Daniel Stoneb7452fe2012-06-01 12:14:06 +010031#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040032#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010036#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050037#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020038#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040039#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010040#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040041#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020042#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020043#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020044#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040045#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050046#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040047#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040048#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040049#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050050#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040051#include <sys/time.h>
52#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030053#include <errno.h>
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030054#include <inttypes.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050055
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020058#include <libweston/libweston.h>
Marius Vladf4f4c2b2019-04-29 13:27:47 +030059#include <libweston/weston-debug.h>
Daniel Stonece62cb32018-07-20 09:46:24 +010060#include "linux-dmabuf.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030061#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020062#include "presentation-time-server-protocol.h"
Roman Gilge97391c2019-03-29 13:01:06 +010063#include "xdg-output-unstable-v1-server-protocol.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030064#include "linux-explicit-synchronization-unstable-v1-server-protocol.h"
Alexandros Frantzis67629672018-10-19 12:14:11 +030065#include "linux-explicit-synchronization.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030066#include "shared/fd-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070067#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070068#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070069#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030070#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040071#include "git-version.h"
Pekka Paalanencda14882019-04-04 15:41:02 +030072#include <libweston/version.h>
Pekka Paalanen27b377f2019-03-29 17:07:34 +020073#include <libweston/plugin-registry.h>
Marius Vlad00a6e012018-11-20 17:52:31 +020074#include "pixel-formats.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050075
Marius Vlad78984ee2019-06-11 00:05:08 +030076/**
77 * \defgroup head Head
78 * \defgroup output Output
79 * \defgroup compositor Compositor
80 */
81
Pekka Paalanen0513a952014-05-21 16:17:27 +030082#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
83
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020084static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030085weston_output_update_matrix(struct weston_output *output);
86
87static void
Alexander Larsson0b135062013-05-28 16:23:36 +020088weston_output_transform_scale_init(struct weston_output *output,
89 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020090
Rob Bradford27b17932013-06-26 18:08:46 +010091static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050092weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010093
Pekka Paalanendcac3512017-12-08 14:13:34 +020094static char *
95weston_output_create_heads_string(struct weston_output *output);
96
Pekka Paalanen6528c032017-03-28 15:27:10 +030097/** Send wl_output events for mode and scale changes
98 *
99 * \param head Send on all resources bound to this head.
100 * \param mode_changed If true, send the current mode.
101 * \param scale_changed If true, send the current scale.
102 */
103static void
104weston_mode_switch_send_events(struct weston_head *head,
105 bool mode_changed, bool scale_changed)
106{
107 struct weston_output *output = head->output;
108 struct wl_resource *resource;
109 int version;
110
111 wl_resource_for_each(resource, &head->resource_list) {
112 if (mode_changed) {
113 wl_output_send_mode(resource,
114 output->current_mode->flags,
115 output->current_mode->width,
116 output->current_mode->height,
117 output->current_mode->refresh);
118 }
119
120 version = wl_resource_get_version(resource);
121 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
122 wl_output_send_scale(resource, output->current_scale);
123
124 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
125 wl_output_send_done(resource);
126 }
Roman Gilge97391c2019-03-29 13:01:06 +0100127 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
128 zxdg_output_v1_send_logical_position(resource,
129 output->x,
130 output->y);
131 zxdg_output_v1_send_logical_size(resource,
132 output->width,
133 output->height);
134 zxdg_output_v1_send_done(resource);
135 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300136}
137
138static void
139weston_mode_switch_finish(struct weston_output *output,
140 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800141{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200142 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300143 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200144 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200145
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200146 pixman_region32_init(&old_output_region);
147 pixman_region32_copy(&old_output_region, &output->region);
148
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200149 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200150 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200151
152 pixman_region32_init(&output->previous_damage);
153 pixman_region32_init_rect(&output->region, output->x, output->y,
154 output->width, output->height);
155
156 weston_output_update_matrix(output);
157
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200158 /* If a pointer falls outside the outputs new geometry, move it to its
159 * lower-right corner */
160 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500161 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200162 int32_t x, y;
163
164 if (!pointer)
165 continue;
166
167 x = wl_fixed_to_int(pointer->x);
168 y = wl_fixed_to_int(pointer->y);
169
170 if (!pixman_region32_contains_point(&old_output_region,
171 x, y, NULL) ||
172 pixman_region32_contains_point(&output->region,
173 x, y, NULL))
174 continue;
175
176 if (x >= output->x + output->width)
177 x = output->x + output->width - 1;
178 if (y >= output->y + output->height)
179 y = output->y + output->height - 1;
180
181 pointer->x = wl_fixed_from_int(x);
182 pointer->y = wl_fixed_from_int(y);
183 }
184
185 pixman_region32_fini(&old_output_region);
186
Derek Foremandd4cd332014-11-10 10:29:59 -0600187 if (!mode_changed && !scale_changed)
188 return;
189
Hardening57388e42013-09-18 23:56:36 +0200190 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300191 wl_list_for_each(head, &output->head_list, output_link)
192 weston_mode_switch_send_events(head,
193 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600194}
195
David Fort0de859e2016-05-27 23:22:57 +0200196static void
197weston_compositor_reflow_outputs(struct weston_compositor *compositor,
198 struct weston_output *resized_output, int delta_width);
199
Marius Vlad55d87362019-06-11 01:15:35 +0300200/**
201 * \ingroup output
202 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600203WL_EXPORT int
204weston_output_mode_set_native(struct weston_output *output,
205 struct weston_mode *mode,
206 int32_t scale)
207{
208 int ret;
209 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200210 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600211
212 if (!output->switch_mode)
213 return -1;
214
215 if (!output->original_mode) {
216 mode_changed = 1;
217 ret = output->switch_mode(output, mode);
218 if (ret < 0)
219 return ret;
220 if (output->current_scale != scale) {
221 scale_changed = 1;
222 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200223 }
224 }
225
David Fort0de859e2016-05-27 23:22:57 +0200226 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600227 output->native_mode = mode;
228 output->native_scale = scale;
229
230 weston_mode_switch_finish(output, mode_changed, scale_changed);
231
David Fort0de859e2016-05-27 23:22:57 +0200232 if (mode_changed || scale_changed) {
233 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
234
235 wl_signal_emit(&output->compositor->output_resized_signal, output);
236 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600237 return 0;
238}
239
Marius Vlad55d87362019-06-11 01:15:35 +0300240/**
241 * \ingroup output
242 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600243WL_EXPORT int
244weston_output_mode_switch_to_native(struct weston_output *output)
245{
246 int ret;
247 int mode_changed = 0, scale_changed = 0;
248
249 if (!output->switch_mode)
250 return -1;
251
252 if (!output->original_mode) {
253 weston_log("already in the native mode\n");
254 return -1;
255 }
256 /* the non fullscreen clients haven't seen a mode set since we
257 * switched into a temporary, so we need to notify them if the
258 * mode at that time is different from the native mode now.
259 */
260 mode_changed = (output->original_mode != output->native_mode);
261 scale_changed = (output->original_scale != output->native_scale);
262
263 ret = output->switch_mode(output, output->native_mode);
264 if (ret < 0)
265 return ret;
266
267 output->current_scale = output->native_scale;
268
269 output->original_mode = NULL;
270 output->original_scale = 0;
271
272 weston_mode_switch_finish(output, mode_changed, scale_changed);
273
274 return 0;
275}
276
Marius Vlad55d87362019-06-11 01:15:35 +0300277/**
278 * \ingroup output
279 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600280WL_EXPORT int
281weston_output_mode_switch_to_temporary(struct weston_output *output,
282 struct weston_mode *mode,
283 int32_t scale)
284{
285 int ret;
286
287 if (!output->switch_mode)
288 return -1;
289
290 /* original_mode is the last mode non full screen clients have seen,
291 * so we shouldn't change it if we already have one set.
292 */
293 if (!output->original_mode) {
294 output->original_mode = output->native_mode;
295 output->original_scale = output->native_scale;
296 }
297 ret = output->switch_mode(output, mode);
298 if (ret < 0)
299 return ret;
300
301 output->current_scale = scale;
302
303 weston_mode_switch_finish(output, 0, 0);
304
305 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800306}
307
Benjamin Franzke06286262011-05-06 19:12:33 +0200308static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300309region_init_infinite(pixman_region32_t *region)
310{
311 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
312 UINT32_MAX, UINT32_MAX);
313}
314
Pekka Paalanene67858b2013-04-25 13:57:42 +0300315static struct weston_subsurface *
316weston_surface_to_subsurface(struct weston_surface *surface);
317
Jason Ekstranda7af7042013-10-12 22:38:11 -0500318WL_EXPORT struct weston_view *
319weston_view_create(struct weston_surface *surface)
320{
321 struct weston_view *view;
322
Bryce Harringtonde16d892014-11-20 22:21:57 -0800323 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500324 if (view == NULL)
325 return NULL;
326
327 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000328 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500329
Jason Ekstranda7af7042013-10-12 22:38:11 -0500330 /* Assign to surface */
331 wl_list_insert(&surface->views, &view->surface_link);
332
333 wl_signal_init(&view->destroy_signal);
334 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300335 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500336
Jason Ekstranda7af7042013-10-12 22:38:11 -0500337 pixman_region32_init(&view->clip);
338
339 view->alpha = 1.0;
340 pixman_region32_init(&view->transform.opaque);
341
342 wl_list_init(&view->geometry.transformation_list);
343 wl_list_insert(&view->geometry.transformation_list,
344 &view->transform.position.link);
345 weston_matrix_init(&view->transform.position.matrix);
346 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200347 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500348 pixman_region32_init(&view->transform.boundingbox);
349 view->transform.dirty = 1;
350
Jason Ekstranda7af7042013-10-12 22:38:11 -0500351 return view;
352}
353
Jason Ekstrand108865d2014-06-26 10:04:49 -0700354struct weston_frame_callback {
355 struct wl_resource *resource;
356 struct wl_list link;
357};
358
Pekka Paalanen133e4392014-09-23 22:08:46 -0400359struct weston_presentation_feedback {
360 struct wl_resource *resource;
361
362 /* XXX: could use just wl_resource_get_link() instead */
363 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200364
365 /* The per-surface feedback flags */
366 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400367};
368
369static void
370weston_presentation_feedback_discard(
371 struct weston_presentation_feedback *feedback)
372{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200373 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400374 wl_resource_destroy(feedback->resource);
375}
376
377static void
378weston_presentation_feedback_discard_list(struct wl_list *list)
379{
380 struct weston_presentation_feedback *feedback, *tmp;
381
382 wl_list_for_each_safe(feedback, tmp, list, link)
383 weston_presentation_feedback_discard(feedback);
384}
385
386static void
387weston_presentation_feedback_present(
388 struct weston_presentation_feedback *feedback,
389 struct weston_output *output,
390 uint32_t refresh_nsec,
391 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200392 uint64_t seq,
393 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400394{
395 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300396 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400397 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200398 uint32_t tv_sec_hi;
399 uint32_t tv_sec_lo;
400 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300401 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400402
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300403 wl_list_for_each(head, &output->head_list, output_link) {
404 wl_resource_for_each(o, &head->resource_list) {
405 if (wl_resource_get_client(o) != client)
406 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400407
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300408 wp_presentation_feedback_send_sync_output(feedback->resource, o);
409 done = true;
410 }
411
412 /* For clone mode, send it for just one wl_output global,
413 * they are all equivalent anyway.
414 */
415 if (done)
416 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400417 }
418
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200419 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200420 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200421 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200422 refresh_nsec,
423 seq >> 32, seq & 0xffffffff,
424 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400425 wl_resource_destroy(feedback->resource);
426}
427
428static void
429weston_presentation_feedback_present_list(struct wl_list *list,
430 struct weston_output *output,
431 uint32_t refresh_nsec,
432 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200433 uint64_t seq,
434 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400435{
436 struct weston_presentation_feedback *feedback, *tmp;
437
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200438 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200439 wl_list_empty(list));
440
Pekka Paalanen133e4392014-09-23 22:08:46 -0400441 wl_list_for_each_safe(feedback, tmp, list, link)
442 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200443 refresh_nsec, ts, seq,
444 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400445}
446
Jason Ekstrand7b982072014-05-20 14:33:03 -0500447static void
448surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
449{
450 struct weston_surface_state *state =
451 container_of(listener, struct weston_surface_state,
452 buffer_destroy_listener);
453
454 state->buffer = NULL;
455}
456
457static void
458weston_surface_state_init(struct weston_surface_state *state)
459{
460 state->newly_attached = 0;
461 state->buffer = NULL;
462 state->buffer_destroy_listener.notify =
463 surface_state_handle_buffer_destroy;
464 state->sx = 0;
465 state->sy = 0;
466
Derek Foreman152254b2015-11-26 14:17:48 -0600467 pixman_region32_init(&state->damage_surface);
468 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500469 pixman_region32_init(&state->opaque);
470 region_init_infinite(&state->input);
471
472 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400473 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500474
475 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
476 state->buffer_viewport.buffer.scale = 1;
477 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
478 state->buffer_viewport.surface.width = -1;
479 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300480
481 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530482
483 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530484 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500485}
486
487static void
488weston_surface_state_fini(struct weston_surface_state *state)
489{
490 struct weston_frame_callback *cb, *next;
491
492 wl_list_for_each_safe(cb, next,
493 &state->frame_callback_list, link)
494 wl_resource_destroy(cb->resource);
495
Pekka Paalanen133e4392014-09-23 22:08:46 -0400496 weston_presentation_feedback_discard_list(&state->feedback_list);
497
Jason Ekstrand7b982072014-05-20 14:33:03 -0500498 pixman_region32_fini(&state->input);
499 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600500 pixman_region32_fini(&state->damage_surface);
501 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500502
503 if (state->buffer)
504 wl_list_remove(&state->buffer_destroy_listener.link);
505 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300506
507 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300508 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500509}
510
511static void
512weston_surface_state_set_buffer(struct weston_surface_state *state,
513 struct weston_buffer *buffer)
514{
515 if (state->buffer == buffer)
516 return;
517
518 if (state->buffer)
519 wl_list_remove(&state->buffer_destroy_listener.link);
520 state->buffer = buffer;
521 if (state->buffer)
522 wl_signal_add(&state->buffer->destroy_signal,
523 &state->buffer_destroy_listener);
524}
525
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500526WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500527weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500528{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500529 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400530
Bryce Harringtonde16d892014-11-20 22:21:57 -0800531 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400532 if (surface == NULL)
533 return NULL;
534
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500535 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800536 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500537
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500538 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200539 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400540
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200541 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
542 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200543 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
544 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500545
546 weston_surface_state_init(&surface->pending);
547
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400548 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500549 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300550 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400551
Jason Ekstranda7af7042013-10-12 22:38:11 -0500552 wl_list_init(&surface->views);
553
554 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400555 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500556
Pekka Paalanene67858b2013-04-25 13:57:42 +0300557 wl_list_init(&surface->subsurface_list);
558 wl_list_init(&surface->subsurface_list_pending);
559
Jason Ekstrand1e059042014-10-16 10:55:19 -0500560 weston_matrix_init(&surface->buffer_to_surface_matrix);
561 weston_matrix_init(&surface->surface_to_buffer_matrix);
562
Jonas Ådahld3414f22016-07-22 17:56:31 +0800563 wl_list_init(&surface->pointer_constraints);
564
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300565 surface->acquire_fence_fd = -1;
566
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530567 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530568 surface->current_protection = WESTON_HDCP_DISABLE;
569 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530570
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400571 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500572}
573
Alex Wu8811bf92012-02-28 18:07:54 +0800574WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500575weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200576 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500577{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100578 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200579 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500580}
581
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400582WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500583weston_view_to_global_float(struct weston_view *view,
584 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200585{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200587 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
588
Jason Ekstranda7af7042013-10-12 22:38:11 -0500589 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200590
591 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200592 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700593 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200594 v.f[3]);
595 *x = 0;
596 *y = 0;
597 return;
598 }
599
600 *x = v.f[0] / v.f[3];
601 *y = v.f[1] / v.f[3];
602 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500603 *x = sx + view->geometry.x;
604 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200605 }
606}
607
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500608WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200609weston_transformed_coord(int width, int height,
610 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200611 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200612 float sx, float sy, float *bx, float *by)
613{
614 switch (transform) {
615 case WL_OUTPUT_TRANSFORM_NORMAL:
616 default:
617 *bx = sx;
618 *by = sy;
619 break;
620 case WL_OUTPUT_TRANSFORM_FLIPPED:
621 *bx = width - sx;
622 *by = sy;
623 break;
624 case WL_OUTPUT_TRANSFORM_90:
625 *bx = height - sy;
626 *by = sx;
627 break;
628 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
629 *bx = height - sy;
630 *by = width - sx;
631 break;
632 case WL_OUTPUT_TRANSFORM_180:
633 *bx = width - sx;
634 *by = height - sy;
635 break;
636 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
637 *bx = sx;
638 *by = height - sy;
639 break;
640 case WL_OUTPUT_TRANSFORM_270:
641 *bx = sy;
642 *by = width - sx;
643 break;
644 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
645 *bx = sy;
646 *by = sx;
647 break;
648 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200649
650 *bx *= scale;
651 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200652}
653
654WL_EXPORT pixman_box32_t
655weston_transformed_rect(int width, int height,
656 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200657 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200658 pixman_box32_t rect)
659{
660 float x1, x2, y1, y2;
661
662 pixman_box32_t ret;
663
Alexander Larsson4ea95522013-05-22 14:41:37 +0200664 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200665 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200666 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200667 rect.x2, rect.y2, &x2, &y2);
668
669 if (x1 <= x2) {
670 ret.x1 = x1;
671 ret.x2 = x2;
672 } else {
673 ret.x1 = x2;
674 ret.x2 = x1;
675 }
676
677 if (y1 <= y2) {
678 ret.y1 = y1;
679 ret.y2 = y2;
680 } else {
681 ret.y1 = y2;
682 ret.y2 = y1;
683 }
684
685 return ret;
686}
687
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600688/** Transform a region by a matrix, restricted to axis-aligned transformations
689 *
690 * Warning: This function does not work for projective, affine, or matrices
691 * that encode arbitrary rotations. Only 90-degree step rotations are
692 * supported.
693 */
694WL_EXPORT void
695weston_matrix_transform_region(pixman_region32_t *dest,
696 struct weston_matrix *matrix,
697 pixman_region32_t *src)
698{
699 pixman_box32_t *src_rects, *dest_rects;
700 int nrects, i;
701
702 src_rects = pixman_region32_rectangles(src, &nrects);
703 dest_rects = malloc(nrects * sizeof(*dest_rects));
704 if (!dest_rects)
705 return;
706
707 for (i = 0; i < nrects; i++) {
708 struct weston_vector vec1 = {{
709 src_rects[i].x1, src_rects[i].y1, 0, 1
710 }};
711 weston_matrix_transform(matrix, &vec1);
712 vec1.f[0] /= vec1.f[3];
713 vec1.f[1] /= vec1.f[3];
714
715 struct weston_vector vec2 = {{
716 src_rects[i].x2, src_rects[i].y2, 0, 1
717 }};
718 weston_matrix_transform(matrix, &vec2);
719 vec2.f[0] /= vec2.f[3];
720 vec2.f[1] /= vec2.f[3];
721
722 if (vec1.f[0] < vec2.f[0]) {
723 dest_rects[i].x1 = floor(vec1.f[0]);
724 dest_rects[i].x2 = ceil(vec2.f[0]);
725 } else {
726 dest_rects[i].x1 = floor(vec2.f[0]);
727 dest_rects[i].x2 = ceil(vec1.f[0]);
728 }
729
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600730 if (vec1.f[1] < vec2.f[1]) {
731 dest_rects[i].y1 = floor(vec1.f[1]);
732 dest_rects[i].y2 = ceil(vec2.f[1]);
733 } else {
734 dest_rects[i].y1 = floor(vec2.f[1]);
735 dest_rects[i].y2 = ceil(vec1.f[1]);
736 }
737 }
738
739 pixman_region32_clear(dest);
740 pixman_region32_init_rects(dest, dest_rects, nrects);
741 free(dest_rects);
742}
743
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200744WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500745weston_transformed_region(int width, int height,
746 enum wl_output_transform transform,
747 int32_t scale,
748 pixman_region32_t *src, pixman_region32_t *dest)
749{
750 pixman_box32_t *src_rects, *dest_rects;
751 int nrects, i;
752
753 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
754 if (src != dest)
755 pixman_region32_copy(dest, src);
756 return;
757 }
758
759 src_rects = pixman_region32_rectangles(src, &nrects);
760 dest_rects = malloc(nrects * sizeof(*dest_rects));
761 if (!dest_rects)
762 return;
763
764 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
765 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
766 } else {
767 for (i = 0; i < nrects; i++) {
768 switch (transform) {
769 default:
770 case WL_OUTPUT_TRANSFORM_NORMAL:
771 dest_rects[i].x1 = src_rects[i].x1;
772 dest_rects[i].y1 = src_rects[i].y1;
773 dest_rects[i].x2 = src_rects[i].x2;
774 dest_rects[i].y2 = src_rects[i].y2;
775 break;
776 case WL_OUTPUT_TRANSFORM_90:
777 dest_rects[i].x1 = height - src_rects[i].y2;
778 dest_rects[i].y1 = src_rects[i].x1;
779 dest_rects[i].x2 = height - src_rects[i].y1;
780 dest_rects[i].y2 = src_rects[i].x2;
781 break;
782 case WL_OUTPUT_TRANSFORM_180:
783 dest_rects[i].x1 = width - src_rects[i].x2;
784 dest_rects[i].y1 = height - src_rects[i].y2;
785 dest_rects[i].x2 = width - src_rects[i].x1;
786 dest_rects[i].y2 = height - src_rects[i].y1;
787 break;
788 case WL_OUTPUT_TRANSFORM_270:
789 dest_rects[i].x1 = src_rects[i].y1;
790 dest_rects[i].y1 = width - src_rects[i].x2;
791 dest_rects[i].x2 = src_rects[i].y2;
792 dest_rects[i].y2 = width - src_rects[i].x1;
793 break;
794 case WL_OUTPUT_TRANSFORM_FLIPPED:
795 dest_rects[i].x1 = width - src_rects[i].x2;
796 dest_rects[i].y1 = src_rects[i].y1;
797 dest_rects[i].x2 = width - src_rects[i].x1;
798 dest_rects[i].y2 = src_rects[i].y2;
799 break;
800 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
801 dest_rects[i].x1 = height - src_rects[i].y2;
802 dest_rects[i].y1 = width - src_rects[i].x2;
803 dest_rects[i].x2 = height - src_rects[i].y1;
804 dest_rects[i].y2 = width - src_rects[i].x1;
805 break;
806 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
807 dest_rects[i].x1 = src_rects[i].x1;
808 dest_rects[i].y1 = height - src_rects[i].y2;
809 dest_rects[i].x2 = src_rects[i].x2;
810 dest_rects[i].y2 = height - src_rects[i].y1;
811 break;
812 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
813 dest_rects[i].x1 = src_rects[i].y1;
814 dest_rects[i].y1 = src_rects[i].x1;
815 dest_rects[i].x2 = src_rects[i].y2;
816 dest_rects[i].y2 = src_rects[i].x2;
817 break;
818 }
819 }
820 }
821
822 if (scale != 1) {
823 for (i = 0; i < nrects; i++) {
824 dest_rects[i].x1 *= scale;
825 dest_rects[i].x2 *= scale;
826 dest_rects[i].y1 *= scale;
827 dest_rects[i].y2 *= scale;
828 }
829 }
830
831 pixman_region32_clear(dest);
832 pixman_region32_init_rects(dest, dest_rects, nrects);
833 free(dest_rects);
834}
835
Jonny Lamb74130762013-11-26 18:19:46 +0100836static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300837viewport_surface_to_buffer(struct weston_surface *surface,
838 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100839{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200840 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200841 double src_width, src_height;
842 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200843
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200844 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
845 if (vp->surface.width == -1) {
846 *bx = sx;
847 *by = sy;
848 return;
849 }
Jonny Lamb74130762013-11-26 18:19:46 +0100850
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200851 src_x = 0.0;
852 src_y = 0.0;
853 src_width = surface->width_from_buffer;
854 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100855 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200856 src_x = wl_fixed_to_double(vp->buffer.src_x);
857 src_y = wl_fixed_to_double(vp->buffer.src_y);
858 src_width = wl_fixed_to_double(vp->buffer.src_width);
859 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100860 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200861
862 *bx = sx * src_width / surface->width + src_x;
863 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100864}
865
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500866WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200867weston_surface_to_buffer_float(struct weston_surface *surface,
868 float sx, float sy, float *bx, float *by)
869{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200870 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
871
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300872 /* first transform coordinates if the viewport is set */
873 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100874
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500875 weston_transformed_coord(surface->width_from_buffer,
876 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200877 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100878 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200879}
880
Derek Foreman99739672015-12-03 16:38:11 -0600881/** Transform a rectangle from surface coordinates to buffer coordinates
882 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300883 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600884 * from.
885 * \param rect The rectangle to transform.
886 * \return The transformed rectangle.
887 *
888 * Viewport and buffer transformations can only do translation, scaling,
889 * and rotations in 90-degree steps. Therefore the only loss in the
890 * conversion is coordinate rounding.
891 *
892 * However, some coordinate rounding takes place as an intermediate
893 * step before the buffer scale factor is applied, so the rectangle
894 * boundary may not be exactly as expected.
895 *
896 * This is OK for damage tracking since a little extra coverage is
897 * not a problem.
898 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200899WL_EXPORT pixman_box32_t
900weston_surface_to_buffer_rect(struct weston_surface *surface,
901 pixman_box32_t rect)
902{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200903 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100904 float xf, yf;
905
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300906 /* first transform box coordinates if the viewport is set */
907 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100908 rect.x1 = floorf(xf);
909 rect.y1 = floorf(yf);
910
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300911 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600912 rect.x2 = ceilf(xf);
913 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100914
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500915 return weston_transformed_rect(surface->width_from_buffer,
916 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200917 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200918 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200919}
920
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200921/** Transform a region from surface coordinates to buffer coordinates
922 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300923 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200924 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300925 * \param[in] surface_region The region in surface coordinates.
926 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200927 *
928 * Buffer_region must be init'd, but will be completely overwritten.
929 *
930 * Viewport and buffer transformations can only do translation, scaling,
931 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600932 * conversion is from the coordinate rounding that takes place in
933 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300934 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200935 */
936WL_EXPORT void
937weston_surface_to_buffer_region(struct weston_surface *surface,
938 pixman_region32_t *surface_region,
939 pixman_region32_t *buffer_region)
940{
941 pixman_box32_t *src_rects, *dest_rects;
942 int nrects, i;
943
944 src_rects = pixman_region32_rectangles(surface_region, &nrects);
945 dest_rects = malloc(nrects * sizeof(*dest_rects));
946 if (!dest_rects)
947 return;
948
949 for (i = 0; i < nrects; i++) {
950 dest_rects[i] = weston_surface_to_buffer_rect(surface,
951 src_rects[i]);
952 }
953
954 pixman_region32_fini(buffer_region);
955 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
956 free(dest_rects);
957}
958
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200959WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500960weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400961 struct weston_plane *plane)
962{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400964 return;
965
Jason Ekstranda7af7042013-10-12 22:38:11 -0500966 weston_view_damage_below(view);
967 view->plane = plane;
968 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400969}
970
Pekka Paalanen51723d52015-02-17 13:10:01 +0200971/** Inflict damage on the plane where the view is visible.
972 *
973 * \param view The view that causes the damage.
974 *
975 * If the view is currently on a plane (including the primary plane),
976 * take the view's boundingbox, subtract all the opaque views that cover it,
977 * and add the remaining region as damage to the plane. This corresponds
978 * to the damage inflicted to the plane if this view disappeared.
979 *
980 * A repaint is scheduled for this view.
981 *
982 * The region of all opaque views covering this view is stored in
983 * weston_view::clip and updated by view_accumulate_damage() during
984 * weston_output_repaint(). Specifically, that region matches the
985 * scenegraph as it was last painted.
986 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400987WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200989{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500990 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200991
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500992 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200993 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800995 if (view->plane)
996 pixman_region32_union(&view->plane->damage,
997 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500998 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800999 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001000}
1001
Pekka Paalanen01e00682017-03-24 16:21:06 +02001002/** Send wl_surface.enter/leave events
1003 *
1004 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001005 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001006 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001007 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001008 *
1009 * Send the enter/leave events for all protocol objects bound to the given
1010 * output by the client owning the surface.
1011 */
1012static void
1013weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001014 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001015 bool enter,
1016 bool leave)
1017{
1018 struct wl_resource *wloutput;
1019 struct wl_client *client;
1020
1021 assert(enter != leave);
1022
1023 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001024 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001025 if (wl_resource_get_client(wloutput) != client)
1026 continue;
1027
1028 if (enter)
1029 wl_surface_send_enter(surface->resource, wloutput);
1030 if (leave)
1031 wl_surface_send_leave(surface->resource, wloutput);
1032 }
1033}
1034
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301035static void
1036weston_surface_compute_protection(struct protected_surface *psurface)
1037{
1038 enum weston_hdcp_protection min_protection;
1039 bool min_protection_valid = false;
1040 struct weston_surface *surface = psurface->surface;
1041 struct weston_output *output;
1042
1043 wl_list_for_each(output, &surface->compositor->output_list, link)
1044 if (surface->output_mask & (1u << output->id)) {
1045 if (!min_protection_valid) {
1046 min_protection = output->current_protection;
1047 min_protection_valid = true;
1048 }
1049 if (output->current_protection < min_protection)
1050 min_protection = output->current_protection;
1051 }
1052 if (!min_protection_valid)
1053 min_protection = WESTON_HDCP_DISABLE;
1054
1055 surface->current_protection = min_protection;
1056 weston_protected_surface_send_event(psurface,
1057 surface->current_protection);
1058}
1059
1060static void
1061notify_surface_protection_change(void *data)
1062{
1063 struct weston_compositor *compositor = data;
1064 struct content_protection *cp;
1065 struct protected_surface *psurface;
1066
1067 cp = compositor->content_protection;
1068 cp->surface_protection_update = NULL;
1069
1070 /* Notify the clients, whose surfaces are changed */
1071 wl_list_for_each(psurface, &cp->protected_list, link)
1072 if (psurface && psurface->surface)
1073 weston_surface_compute_protection(psurface);
1074}
1075
Bryce Harrington3f650b82015-12-23 11:01:58 -08001076/**
1077 * \param es The surface
1078 * \param mask The new set of outputs for the surface
1079 *
1080 * Sets the surface's set of outputs to the ones specified by
1081 * the new output mask provided. Identifies the outputs that
1082 * have changed, the posts enter and leave events for these
1083 * outputs as appropriate.
1084 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001085static void
1086weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1087{
1088 uint32_t different = es->output_mask ^ mask;
1089 uint32_t entered = mask & different;
1090 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001091 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001092 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001093 struct weston_head *head;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301094 struct content_protection *cp;
1095 struct wl_event_loop *loop;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001096
1097 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001098 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001099 return;
1100 if (different == 0)
1101 return;
1102
1103 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001104 output_bit = 1u << output->id;
1105 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001106 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001107
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001108 wl_list_for_each(head, &output->head_list, output_link) {
1109 weston_surface_send_enter_leave(es, head,
1110 output_bit & entered,
1111 output_bit & left);
1112 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001113 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301114 /*
1115 * Change in surfaces' output mask might trigger a change in its
1116 * protection.
1117 */
1118 loop = wl_display_get_event_loop(es->compositor->wl_display);
1119 cp = es->compositor->content_protection;
1120 if (!cp || cp->surface_protection_update)
1121 return;
1122 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1123 notify_surface_protection_change,
1124 es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001125}
1126
Semi Malinene7a52fb2018-04-26 11:08:10 +02001127static void
1128notify_view_output_destroy(struct wl_listener *listener, void *data)
1129{
1130 struct weston_view *view =
1131 container_of(listener,
1132 struct weston_view, output_destroy_listener);
1133
1134 view->output = NULL;
1135 view->output_destroy_listener.notify = NULL;
1136}
1137
1138/** Set the primary output of the view
1139 *
1140 * \param view The view whose primary output to set
1141 * \param output The new primary output for the view
1142 *
1143 * Set \a output to be the primary output of the \a view.
1144 *
1145 * Notice that the assignment may be temporary; the primary output could be
1146 * automatically changed. Hence, one cannot rely on the value persisting.
1147 *
1148 * Passing NULL as /a output will set the primary output to NULL.
1149 */
1150WL_EXPORT void
1151weston_view_set_output(struct weston_view *view, struct weston_output *output)
1152{
1153 if (view->output_destroy_listener.notify) {
1154 wl_list_remove(&view->output_destroy_listener.link);
1155 view->output_destroy_listener.notify = NULL;
1156 }
1157 view->output = output;
1158 if (output) {
1159 view->output_destroy_listener.notify =
1160 notify_view_output_destroy;
1161 wl_signal_add(&output->destroy_signal,
1162 &view->output_destroy_listener);
1163 }
1164}
1165
Bryce Harrington3f650b82015-12-23 11:01:58 -08001166/** Recalculate which output(s) the surface has views displayed on
1167 *
1168 * \param es The surface to remap to outputs
1169 *
1170 * Finds the output that is showing the largest amount of one
1171 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001172 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001173 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001174 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001175 * in the output_mask for the surface.
1176 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001177static void
1178weston_surface_assign_output(struct weston_surface *es)
1179{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 struct weston_output *new_output;
1181 struct weston_view *view;
1182 pixman_region32_t region;
1183 uint32_t max, area, mask;
1184 pixman_box32_t *e;
1185
1186 new_output = NULL;
1187 max = 0;
1188 mask = 0;
1189 pixman_region32_init(&region);
1190 wl_list_for_each(view, &es->views, surface_link) {
1191 if (!view->output)
1192 continue;
1193
1194 pixman_region32_intersect(&region, &view->transform.boundingbox,
1195 &view->output->region);
1196
1197 e = pixman_region32_extents(&region);
1198 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1199
1200 mask |= view->output_mask;
1201
1202 if (area >= max) {
1203 new_output = view->output;
1204 max = area;
1205 }
1206 }
1207 pixman_region32_fini(&region);
1208
1209 es->output = new_output;
1210 weston_surface_update_output_mask(es, mask);
1211}
1212
Bryce Harrington3f650b82015-12-23 11:01:58 -08001213/** Recalculate which output(s) the view is displayed on
1214 *
1215 * \param ev The view to remap to outputs
1216 *
1217 * Identifies the set of outputs that the view is visible on,
1218 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001219 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001220 *
1221 * Also does the same for the view's surface. See
1222 * weston_surface_assign_output().
1223 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224static void
1225weston_view_assign_output(struct weston_view *ev)
1226{
1227 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001228 struct weston_output *output, *new_output;
1229 pixman_region32_t region;
1230 uint32_t max, area, mask;
1231 pixman_box32_t *e;
1232
1233 new_output = NULL;
1234 max = 0;
1235 mask = 0;
1236 pixman_region32_init(&region);
1237 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001238 if (output->destroying)
1239 continue;
1240
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001242 &output->region);
1243
1244 e = pixman_region32_extents(&region);
1245 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1246
1247 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001248 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001249
1250 if (area >= max) {
1251 new_output = output;
1252 max = area;
1253 }
1254 }
1255 pixman_region32_fini(&region);
1256
Semi Malinene7a52fb2018-04-26 11:08:10 +02001257 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 ev->output_mask = mask;
1259
1260 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001261}
1262
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001263static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001264weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1265 int from_x, int from_y, int *to_x, int *to_y)
1266{
1267 float x, y;
1268
1269 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1270 weston_view_from_global_float(to, x, y, &x, &y);
1271
1272 *to_x = round(x);
1273 *to_y = round(y);
1274}
1275
1276static void
1277weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1278{
1279 pixman_box32_t *a;
1280 pixman_box32_t b;
1281
1282 a = pixman_region32_extents(&from->geometry.scissor);
1283
1284 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1285 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1286
1287 pixman_region32_fini(&to->geometry.scissor);
1288 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1289}
1290
1291static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001292view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001294{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001295 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1296 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001297 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001298 { inbox->x1, inbox->y1 },
1299 { inbox->x1, inbox->y2 },
1300 { inbox->x2, inbox->y1 },
1301 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001302 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001303 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001304 int i;
1305
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001306 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001307 /* avoid rounding empty bbox to 1x1 */
1308 pixman_region32_init(bbox);
1309 return;
1310 }
1311
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001312 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001313 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001315 if (x < min_x)
1316 min_x = x;
1317 if (x > max_x)
1318 max_x = x;
1319 if (y < min_y)
1320 min_y = y;
1321 if (y > max_y)
1322 max_y = y;
1323 }
1324
Pekka Paalanen219b9822012-02-08 15:38:37 +02001325 int_x = floorf(min_x);
1326 int_y = floorf(min_y);
1327 pixman_region32_init_rect(bbox, int_x, int_y,
1328 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001329}
1330
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001331static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001333{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001335
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001336 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 view->geometry.x = roundf(view->geometry.x);
1338 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001339
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001340 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1342 view->transform.position.matrix.d[12] = view->geometry.x;
1343 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001344
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001346
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 view->transform.inverse = view->transform.position.matrix;
1348 view->transform.inverse.d[12] = -view->geometry.x;
1349 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001350
Jason Ekstranda7af7042013-10-12 22:38:11 -05001351 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001352 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001353 view->surface->width,
1354 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001355 if (view->geometry.scissor_enabled)
1356 pixman_region32_intersect(&view->transform.boundingbox,
1357 &view->transform.boundingbox,
1358 &view->geometry.scissor);
1359
1360 pixman_region32_translate(&view->transform.boundingbox,
1361 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001362
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 if (view->alpha == 1.0) {
1364 pixman_region32_copy(&view->transform.opaque,
1365 &view->surface->opaque);
1366 pixman_region32_translate(&view->transform.opaque,
1367 view->geometry.x,
1368 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001369 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001370}
1371
1372static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001374{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 struct weston_view *parent = view->geometry.parent;
1376 struct weston_matrix *matrix = &view->transform.matrix;
1377 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001378 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001379 pixman_region32_t surfregion;
1380 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001381
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001383
1384 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1386 view->transform.position.matrix.d[12] = view->geometry.x;
1387 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001388
1389 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001391 weston_matrix_multiply(matrix, &tform->matrix);
1392
Pekka Paalanen483243f2013-03-08 14:56:50 +02001393 if (parent)
1394 weston_matrix_multiply(matrix, &parent->transform.matrix);
1395
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001396 if (weston_matrix_invert(inverse, matrix) < 0) {
1397 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 weston_log("error: weston_view %p"
1399 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001400 return -1;
1401 }
1402
Giulio Camuffo148c1992016-09-04 18:50:46 +03001403 if (view->alpha == 1.0 &&
1404 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1405 pixman_region32_copy(&view->transform.opaque,
1406 &view->surface->opaque);
1407 pixman_region32_translate(&view->transform.opaque,
1408 matrix->d[12],
1409 matrix->d[13]);
1410 }
1411
Pekka Paalanen380adf52015-02-16 14:39:11 +02001412 pixman_region32_init_rect(&surfregion, 0, 0,
1413 view->surface->width, view->surface->height);
1414 if (view->geometry.scissor_enabled)
1415 pixman_region32_intersect(&surfregion, &surfregion,
1416 &view->geometry.scissor);
1417 surfbox = pixman_region32_extents(&surfregion);
1418
1419 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1420 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001421
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001422 return 0;
1423}
1424
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001425static struct weston_layer *
1426get_view_layer(struct weston_view *view)
1427{
1428 if (view->parent_view)
1429 return get_view_layer(view->parent_view);
1430 return view->layer_link.layer;
1431}
1432
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001433WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001435{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001437 struct weston_layer *layer;
1438 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001439
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001441 return;
1442
Pekka Paalanen483243f2013-03-08 14:56:50 +02001443 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001444 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001445
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001447
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 pixman_region32_fini(&view->transform.boundingbox);
1451 pixman_region32_fini(&view->transform.opaque);
1452 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001453
Pekka Paalanencd403622012-01-25 13:37:39 +02001454 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 if (view->geometry.transformation_list.next ==
1456 &view->transform.position.link &&
1457 view->geometry.transformation_list.prev ==
1458 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001459 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001461 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 if (weston_view_update_transform_enable(view) < 0)
1463 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001464 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001465
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001466 layer = get_view_layer(view);
1467 if (layer) {
1468 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001469 pixman_region32_intersect(&view->transform.boundingbox,
1470 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001471 pixman_region32_intersect(&view->transform.opaque,
1472 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001473 pixman_region32_fini(&mask);
1474 }
1475
Pekka Paalanen380adf52015-02-16 14:39:11 +02001476 if (parent) {
1477 if (parent->geometry.scissor_enabled) {
1478 view->geometry.scissor_enabled = true;
1479 weston_view_transfer_scissor(parent, view);
1480 } else {
1481 view->geometry.scissor_enabled = false;
1482 }
1483 }
1484
Jason Ekstranda7af7042013-10-12 22:38:11 -05001485 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001486
Jason Ekstranda7af7042013-10-12 22:38:11 -05001487 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001488
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 wl_signal_emit(&view->surface->compositor->transform_signal,
1490 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001491}
1492
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001493WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001494weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001495{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001497
1498 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001499 * The invariant: if view->geometry.dirty, then all views
1500 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001501 * Corollary: if not parent->geometry.dirty, then all ancestors
1502 * are not dirty.
1503 */
1504
Jason Ekstranda7af7042013-10-12 22:38:11 -05001505 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001506 return;
1507
Jason Ekstranda7af7042013-10-12 22:38:11 -05001508 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001509
Jason Ekstranda7af7042013-10-12 22:38:11 -05001510 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001511 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001512 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001513}
1514
1515WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001516weston_view_to_global_fixed(struct weston_view *view,
1517 wl_fixed_t vx, wl_fixed_t vy,
1518 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001519{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001520 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001521
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522 weston_view_to_global_float(view,
1523 wl_fixed_to_double(vx),
1524 wl_fixed_to_double(vy),
1525 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001526 *x = wl_fixed_from_double(xf);
1527 *y = wl_fixed_from_double(yf);
1528}
1529
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001530WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531weston_view_from_global_float(struct weston_view *view,
1532 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001533{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001535 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1536
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001538
1539 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001540 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001541 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001542 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001543 *vx = 0;
1544 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001545 return;
1546 }
1547
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548 *vx = v.f[0] / v.f[3];
1549 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001550 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 *vx = x - view->geometry.x;
1552 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001553 }
1554}
1555
1556WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557weston_view_from_global_fixed(struct weston_view *view,
1558 wl_fixed_t x, wl_fixed_t y,
1559 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001560{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001561 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001562
Jason Ekstranda7af7042013-10-12 22:38:11 -05001563 weston_view_from_global_float(view,
1564 wl_fixed_to_double(x),
1565 wl_fixed_to_double(y),
1566 &vxf, &vyf);
1567 *vx = wl_fixed_from_double(vxf);
1568 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001569}
1570
1571WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572weston_view_from_global(struct weston_view *view,
1573 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001574{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001576
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1578 *vx = floorf(vxf);
1579 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001580}
1581
Bryce Harrington3f650b82015-12-23 11:01:58 -08001582/**
1583 * \param surface The surface to be repainted
1584 *
1585 * Marks the output(s) that the surface is shown on as needing to be
1586 * repainted. See weston_output_schedule_repaint().
1587 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001588WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001589weston_surface_schedule_repaint(struct weston_surface *surface)
1590{
1591 struct weston_output *output;
1592
1593 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001594 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001595 weston_output_schedule_repaint(output);
1596}
1597
Bryce Harrington3f650b82015-12-23 11:01:58 -08001598/**
1599 * \param view The view to be repainted
1600 *
1601 * Marks the output(s) that the view is shown on as needing to be
1602 * repainted. See weston_output_schedule_repaint().
1603 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001604WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001605weston_view_schedule_repaint(struct weston_view *view)
1606{
1607 struct weston_output *output;
1608
1609 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001610 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 weston_output_schedule_repaint(output);
1612}
1613
Pekka Paalanene508ce62015-02-19 13:59:55 +02001614/**
1615 * XXX: This function does it the wrong way.
1616 * surface->damage is the damage from the client, and causes
1617 * surface_flush_damage() to copy pixels. No window management action can
1618 * cause damage to the client-provided content, warranting re-upload!
1619 *
1620 * Instead of surface->damage, this function should record the damage
1621 * with all the views for this surface to avoid extraneous texture
1622 * uploads.
1623 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001624WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001625weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001626{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001627 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001628 0, 0, surface->width,
1629 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001630
Kristian Høgsberg98238702012-08-03 16:29:12 -04001631 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001632}
1633
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001634WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001636{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001637 if (view->geometry.x == x && view->geometry.y == y)
1638 return;
1639
Jason Ekstranda7af7042013-10-12 22:38:11 -05001640 view->geometry.x = x;
1641 view->geometry.y = y;
1642 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001643}
1644
Pekka Paalanen483243f2013-03-08 14:56:50 +02001645static void
1646transform_parent_handle_parent_destroy(struct wl_listener *listener,
1647 void *data)
1648{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 struct weston_view *view =
1650 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001651 geometry.parent_destroy_listener);
1652
Jason Ekstranda7af7042013-10-12 22:38:11 -05001653 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001654}
1655
1656WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001657weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001658 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001659{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001660 if (view->geometry.parent) {
1661 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1662 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001663
1664 if (!parent)
1665 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001666 }
1667
Jason Ekstranda7af7042013-10-12 22:38:11 -05001668 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001669
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001671 transform_parent_handle_parent_destroy;
1672 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001673 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001674 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001675 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001677 }
1678
Jason Ekstranda7af7042013-10-12 22:38:11 -05001679 weston_view_geometry_dirty(view);
1680}
1681
Pekka Paalanen380adf52015-02-16 14:39:11 +02001682/** Set a clip mask rectangle on a view
1683 *
1684 * \param view The view to set the clip mask on.
1685 * \param x Top-left corner X coordinate of the clip rectangle.
1686 * \param y Top-left corner Y coordinate of the clip rectangle.
1687 * \param width Width of the clip rectangle, non-negative.
1688 * \param height Height of the clip rectangle, non-negative.
1689 *
1690 * A shell may set a clip mask rectangle on a view. Everything outside
1691 * the rectangle is cut away for input and output purposes: it is
1692 * not drawn and cannot be hit by hit-test based input like pointer
1693 * motion or touch-downs. Everything inside the rectangle will behave
1694 * normally. Clients are unaware of clipping.
1695 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001696 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001697 * mask rectangle does not affect the view position, the view is positioned
1698 * as it would be without a clip. The clip also does not change
1699 * weston_surface::width,height.
1700 *
1701 * The clip mask rectangle is part of transformation inheritance
1702 * (weston_view_set_transform_parent()). A clip set in the root of the
1703 * transformation inheritance tree will affect all views in the tree.
1704 * A clip can be set only on the root view. Attempting to set a clip
1705 * on view that has a transformation parent will fail. Assigning a parent
1706 * to a view that has a clip set will cause the clip to be forgotten.
1707 *
1708 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1709 * on the additional transformations in the child views. These transformations
1710 * may not rotate the coordinate axes, i.e., only translation and scaling
1711 * are allowed. Violating this restriction causes the clipping to malfunction.
1712 * Furthermore, using scaling may cause rounding errors in child clipping.
1713 *
1714 * The clip mask rectangle is not automatically adjusted based on
1715 * wl_surface.attach dx and dy arguments.
1716 *
1717 * A clip mask rectangle can be set only if the compositor capability
1718 * WESTON_CAP_VIEW_CLIP_MASK is present.
1719 *
1720 * This function sets the clip mask rectangle and schedules a repaint for
1721 * the view.
1722 */
1723WL_EXPORT void
1724weston_view_set_mask(struct weston_view *view,
1725 int x, int y, int width, int height)
1726{
1727 struct weston_compositor *compositor = view->surface->compositor;
1728
1729 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1730 weston_log("%s not allowed without capability!\n", __func__);
1731 return;
1732 }
1733
1734 if (view->geometry.parent) {
1735 weston_log("view %p has a parent, clip forbidden!\n", view);
1736 return;
1737 }
1738
1739 if (width < 0 || height < 0) {
1740 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1741 x, y, width, height);
1742 return;
1743 }
1744
1745 pixman_region32_fini(&view->geometry.scissor);
1746 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1747 view->geometry.scissor_enabled = true;
1748 weston_view_geometry_dirty(view);
1749 weston_view_schedule_repaint(view);
1750}
1751
1752/** Remove the clip mask from a view
1753 *
1754 * \param view The view to remove the clip mask from.
1755 *
1756 * Removed the clip mask rectangle and schedules a repaint.
1757 *
1758 * \sa weston_view_set_mask
1759 */
1760WL_EXPORT void
1761weston_view_set_mask_infinite(struct weston_view *view)
1762{
1763 view->geometry.scissor_enabled = false;
1764 weston_view_geometry_dirty(view);
1765 weston_view_schedule_repaint(view);
1766}
1767
Armin Krezović0da12b82016-06-30 06:04:33 +02001768/* Check if view should be displayed
1769 *
1770 * The indicator is set manually when assigning
1771 * a view to a surface.
1772 *
1773 * This needs reworking. See the thread starting at:
1774 *
1775 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1776 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001777WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001778weston_view_is_mapped(struct weston_view *view)
1779{
Armin Krezović0da12b82016-06-30 06:04:33 +02001780 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001781}
1782
Philipp Zabel70decd52018-09-03 20:11:15 +02001783/* Check if view is opaque in specified region
1784 *
1785 * \param view The view to check for opacity.
1786 * \param region The region to check for opacity, in view coordinates.
1787 *
1788 * Returns true if the view is opaque in the specified region, because view
1789 * alpha is 1.0 and either the opaque region set by the client contains the
1790 * specified region, or the buffer pixel format or solid color is opaque.
1791 */
1792WL_EXPORT bool
1793weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1794{
1795 pixman_region32_t r;
1796 bool ret = false;
1797
1798 if (ev->alpha < 1.0)
1799 return false;
1800
1801 if (ev->surface->is_opaque)
1802 return true;
1803
1804 if (ev->transform.dirty) {
1805 weston_log("%s: transform dirty", __func__);
1806 return false;
1807 }
1808
1809 pixman_region32_init(&r);
1810 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1811
1812 if (!pixman_region32_not_empty(&r))
1813 ret = true;
1814
1815 pixman_region32_fini(&r);
1816
1817 return ret;
1818}
1819
Armin Krezović0da12b82016-06-30 06:04:33 +02001820/* Check if a surface has a view assigned to it
1821 *
1822 * The indicator is set manually when mapping
1823 * a surface and creating a view for it.
1824 *
1825 * This needs to go. See the thread starting at:
1826 *
1827 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1828 *
1829 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001830WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001831weston_surface_is_mapped(struct weston_surface *surface)
1832{
Armin Krezović0da12b82016-06-30 06:04:33 +02001833 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001834}
1835
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001836static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001837surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001838{
1839 struct weston_view *view;
1840
1841 if (surface->width == width && surface->height == height)
1842 return;
1843
1844 surface->width = width;
1845 surface->height = height;
1846
1847 wl_list_for_each(view, &surface->views, surface_link)
1848 weston_view_geometry_dirty(view);
1849}
1850
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001851WL_EXPORT void
1852weston_surface_set_size(struct weston_surface *surface,
1853 int32_t width, int32_t height)
1854{
1855 assert(!surface->resource);
1856 surface_set_size(surface, width, height);
1857}
1858
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001859static int
1860fixed_round_up_to_int(wl_fixed_t f)
1861{
1862 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1863}
1864
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001865static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001866convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1867 int32_t width, int32_t height,
1868 uint32_t transform,
1869 int32_t scale)
1870{
1871 assert(scale > 0);
1872
1873 switch (transform) {
1874 case WL_OUTPUT_TRANSFORM_NORMAL:
1875 case WL_OUTPUT_TRANSFORM_180:
1876 case WL_OUTPUT_TRANSFORM_FLIPPED:
1877 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1878 *width_out = width / scale;
1879 *height_out = height / scale;
1880 break;
1881 case WL_OUTPUT_TRANSFORM_90:
1882 case WL_OUTPUT_TRANSFORM_270:
1883 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1884 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1885 *width_out = height / scale;
1886 *height_out = width / scale;
1887 break;
1888 default:
1889 assert(0 && "invalid transform");
1890 }
1891}
1892
1893static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001894weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001895{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001896 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001897
1898 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001899 surface->width_from_buffer = 0;
1900 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001901 return;
1902 }
1903
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001904 convert_size_by_transform_scale(&surface->width_from_buffer,
1905 &surface->height_from_buffer,
1906 surface->buffer_ref.buffer->width,
1907 surface->buffer_ref.buffer->height,
1908 vp->buffer.transform,
1909 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001910}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001911
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001912static void
1913weston_surface_update_size(struct weston_surface *surface)
1914{
1915 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1916 int32_t width, height;
1917
1918 width = surface->width_from_buffer;
1919 height = surface->height_from_buffer;
1920
1921 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001922 surface_set_size(surface,
1923 vp->surface.width, vp->surface.height);
1924 return;
1925 }
1926
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001927 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001928 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1929 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1930
1931 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001932 return;
1933 }
1934
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001935 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001936}
1937
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001938/** weston_compositor_get_time
1939 * \ingroup compositor
1940 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001941WL_EXPORT void
1942weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001943{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001944 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001945}
1946
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001947/** weston_compositor_pick_view
1948 * \ingroup compositor
1949 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001950WL_EXPORT struct weston_view *
1951weston_compositor_pick_view(struct weston_compositor *compositor,
1952 wl_fixed_t x, wl_fixed_t y,
1953 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001954{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001955 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001956 wl_fixed_t view_x, view_y;
1957 int view_ix, view_iy;
1958 int ix = wl_fixed_to_int(x);
1959 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001960
Jason Ekstranda7af7042013-10-12 22:38:11 -05001961 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001962 if (!pixman_region32_contains_point(
1963 &view->transform.boundingbox, ix, iy, NULL))
1964 continue;
1965
1966 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1967 view_ix = wl_fixed_to_int(view_x);
1968 view_iy = wl_fixed_to_int(view_y);
1969
1970 if (!pixman_region32_contains_point(&view->surface->input,
1971 view_ix, view_iy, NULL))
1972 continue;
1973
Pekka Paalanen380adf52015-02-16 14:39:11 +02001974 if (view->geometry.scissor_enabled &&
1975 !pixman_region32_contains_point(&view->geometry.scissor,
1976 view_ix, view_iy, NULL))
1977 continue;
1978
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001979 *vx = view_x;
1980 *vy = view_y;
1981 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001982 }
1983
Derek Foremanf9318d12015-05-11 15:40:11 -05001984 *vx = wl_fixed_from_int(-1000000);
1985 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001986 return NULL;
1987}
1988
1989static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001990weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001991{
Daniel Stone37816df2012-05-16 18:45:18 +01001992 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001993
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001994 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001995 return;
1996
Daniel Stone37816df2012-05-16 18:45:18 +01001997 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001998 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001999}
2000
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002001WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002002weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002003{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002004 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002005
Jason Ekstranda7af7042013-10-12 22:38:11 -05002006 if (!weston_view_is_mapped(view))
2007 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002008
Jason Ekstranda7af7042013-10-12 22:38:11 -05002009 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002010 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002011 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002012 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002013 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002014 wl_list_remove(&view->link);
2015 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002016 view->output_mask = 0;
2017 weston_surface_assign_output(view->surface);
2018
2019 if (weston_surface_is_mapped(view->surface))
2020 return;
2021
2022 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002023 struct weston_touch *touch = weston_seat_get_touch(seat);
2024 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2025 struct weston_keyboard *keyboard =
2026 weston_seat_get_keyboard(seat);
2027
2028 if (keyboard && keyboard->focus == view->surface)
2029 weston_keyboard_set_focus(keyboard, NULL);
2030 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002031 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002032 if (touch && touch->focus == view)
2033 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002034 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002036
Jason Ekstranda7af7042013-10-12 22:38:11 -05002037WL_EXPORT void
2038weston_surface_unmap(struct weston_surface *surface)
2039{
2040 struct weston_view *view;
2041
Armin Krezovićf8486c32016-06-30 06:04:28 +02002042 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002043 wl_list_for_each(view, &surface->views, surface_link)
2044 weston_view_unmap(view);
2045 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002046}
2047
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002048static void
2049weston_surface_reset_pending_buffer(struct weston_surface *surface)
2050{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002051 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002052 surface->pending.sx = 0;
2053 surface->pending.sy = 0;
2054 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002055 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002056}
2057
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058WL_EXPORT void
2059weston_view_destroy(struct weston_view *view)
2060{
2061 wl_signal_emit(&view->destroy_signal, view);
2062
2063 assert(wl_list_empty(&view->geometry.child_list));
2064
2065 if (weston_view_is_mapped(view)) {
2066 weston_view_unmap(view);
2067 weston_compositor_build_view_list(view->surface->compositor);
2068 }
2069
2070 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002071 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002072
2073 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002074 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002075 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002076 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002077
2078 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002079 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002080
Jason Ekstranda7af7042013-10-12 22:38:11 -05002081 wl_list_remove(&view->surface_link);
2082
2083 free(view);
2084}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002085
2086WL_EXPORT void
2087weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002088{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002089 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002091 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002092
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002093 if (--surface->ref_count > 0)
2094 return;
2095
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002096 assert(surface->resource == NULL);
2097
Pekka Paalanenca790762015-04-17 14:23:38 +03002098 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002099
Pekka Paalanene67858b2013-04-25 13:57:42 +03002100 assert(wl_list_empty(&surface->subsurface_list_pending));
2101 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002102
Jason Ekstranda7af7042013-10-12 22:38:11 -05002103 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2104 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002105
Jason Ekstrand7b982072014-05-20 14:33:03 -05002106 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002107
Pekka Paalanende685b82012-12-04 15:58:12 +02002108 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002109 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002110
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002111 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002112 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002113 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002114
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002115 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002116 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002117
Pekka Paalanen133e4392014-09-23 22:08:46 -04002118 weston_presentation_feedback_discard_list(&surface->feedback_list);
2119
Jonas Ådahld3414f22016-07-22 17:56:31 +08002120 wl_list_for_each_safe(constraint, next_constraint,
2121 &surface->pointer_constraints,
2122 link)
2123 weston_pointer_constraint_destroy(constraint);
2124
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002125 fd_clear(&surface->acquire_fence_fd);
2126
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002127 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002128}
2129
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002130static void
2131destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002132{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002133 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002134
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002135 assert(surface);
2136
Giulio Camuffo0d379742013-11-15 22:06:15 +01002137 /* Set the resource to NULL, since we don't want to leave a
2138 * dangling pointer if the surface was refcounted and survives
2139 * the weston_surface_destroy() call. */
2140 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002141
2142 if (surface->viewport_resource)
2143 wl_resource_set_user_data(surface->viewport_resource, NULL);
2144
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002145 if (surface->synchronization_resource) {
2146 wl_resource_set_user_data(surface->synchronization_resource,
2147 NULL);
2148 }
2149
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002150 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002151}
2152
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002153static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002154weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2155{
2156 struct weston_buffer *buffer =
2157 container_of(listener, struct weston_buffer, destroy_listener);
2158
2159 wl_signal_emit(&buffer->destroy_signal, buffer);
2160 free(buffer);
2161}
2162
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002163WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002164weston_buffer_from_resource(struct wl_resource *resource)
2165{
2166 struct weston_buffer *buffer;
2167 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002168
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002169 listener = wl_resource_get_destroy_listener(resource,
2170 weston_buffer_destroy_handler);
2171
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002172 if (listener)
2173 return container_of(listener, struct weston_buffer,
2174 destroy_listener);
2175
2176 buffer = zalloc(sizeof *buffer);
2177 if (buffer == NULL)
2178 return NULL;
2179
2180 buffer->resource = resource;
2181 wl_signal_init(&buffer->destroy_signal);
2182 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002183 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002184 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002185
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002186 return buffer;
2187}
2188
2189static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002190weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2191 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002192{
Pekka Paalanende685b82012-12-04 15:58:12 +02002193 struct weston_buffer_reference *ref =
2194 container_of(listener, struct weston_buffer_reference,
2195 destroy_listener);
2196
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002197 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002198 ref->buffer = NULL;
2199}
2200
2201WL_EXPORT void
2202weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002203 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002204{
2205 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002206 ref->buffer->busy_count--;
2207 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002208 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002209 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002210 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002211 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002212 }
2213
Pekka Paalanende685b82012-12-04 15:58:12 +02002214 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002215 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002216 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002217 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002218 }
2219
Pekka Paalanende685b82012-12-04 15:58:12 +02002220 ref->buffer = buffer;
2221 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2222}
2223
2224static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002225weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2226 void *data)
2227{
2228 struct weston_buffer_release_reference *ref =
2229 container_of(listener, struct weston_buffer_release_reference,
2230 destroy_listener);
2231
2232 assert((struct wl_resource *)data == ref->buffer_release->resource);
2233 ref->buffer_release = NULL;
2234}
2235
2236static void
2237weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2238{
2239 struct wl_resource *resource = buffer_release->resource;
2240 int release_fence_fd = buffer_release->fence_fd;
2241
2242 if (release_fence_fd >= 0) {
2243 zwp_linux_buffer_release_v1_send_fenced_release(
2244 resource, release_fence_fd);
2245 } else {
2246 zwp_linux_buffer_release_v1_send_immediate_release(
2247 resource);
2248 }
2249
2250 wl_resource_destroy(resource);
2251}
2252
2253WL_EXPORT void
2254weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2255 struct weston_buffer_release *buffer_release)
2256{
2257 if (buffer_release == ref->buffer_release)
2258 return;
2259
2260 if (ref->buffer_release) {
2261 ref->buffer_release->ref_count--;
2262 wl_list_remove(&ref->destroy_listener.link);
2263 if (ref->buffer_release->ref_count == 0)
2264 weston_buffer_release_destroy(ref->buffer_release);
2265 }
2266
2267 if (buffer_release) {
2268 buffer_release->ref_count++;
2269 wl_resource_add_destroy_listener(buffer_release->resource,
2270 &ref->destroy_listener);
2271 }
2272
2273 ref->buffer_release = buffer_release;
2274 ref->destroy_listener.notify =
2275 weston_buffer_release_reference_handle_destroy;
2276}
2277
2278WL_EXPORT void
2279weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2280 struct weston_buffer_release_reference *src)
2281{
2282 weston_buffer_release_reference(dest, src->buffer_release);
2283 weston_buffer_release_reference(src, NULL);
2284}
2285
2286static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002287weston_surface_attach(struct weston_surface *surface,
2288 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002289{
2290 weston_buffer_reference(&surface->buffer_ref, buffer);
2291
Pekka Paalanena6421c42012-12-04 15:58:10 +02002292 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002293 if (weston_surface_is_mapped(surface))
2294 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002295 }
2296
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002297 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002298
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002299 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002300 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002301}
2302
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002303/** weston_compositor_damage_all
2304 * \ingroup compositor
2305 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002306WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002307weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002308{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002309 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002310
2311 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002312 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002313}
2314
Marius Vlad55d87362019-06-11 01:15:35 +03002315/**
2316 * \ingroup output
2317 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002318WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002319weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002320{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002321 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002322
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002323 pixman_region32_union(&compositor->primary_plane.damage,
2324 &compositor->primary_plane.damage,
2325 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002326 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002327}
2328
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002329static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002330surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002331{
Pekka Paalanende685b82012-12-04 15:58:12 +02002332 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002333 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002334 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002335
Pekka Paalanenb5026542014-11-12 15:09:24 +02002336 if (weston_timeline_enabled_ &&
2337 pixman_region32_not_empty(&surface->damage))
2338 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2339 TLP_OUTPUT(surface->output), TLP_END);
2340
Jason Ekstrandef540082014-06-26 10:37:36 -07002341 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002342}
2343
2344static void
2345view_accumulate_damage(struct weston_view *view,
2346 pixman_region32_t *opaque)
2347{
2348 pixman_region32_t damage;
2349
2350 pixman_region32_init(&damage);
2351 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002352 pixman_box32_t *extents;
2353
Jason Ekstranda7af7042013-10-12 22:38:11 -05002354 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002355 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002356 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002357 pixman_region32_copy(&damage, &view->surface->damage);
2358 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002359 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002360 }
2361
Pekka Paalanen380adf52015-02-16 14:39:11 +02002362 pixman_region32_intersect(&damage, &damage,
2363 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002364 pixman_region32_subtract(&damage, &damage, opaque);
2365 pixman_region32_union(&view->plane->damage,
2366 &view->plane->damage, &damage);
2367 pixman_region32_fini(&damage);
2368 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002369 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002370}
2371
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002372static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002373compositor_accumulate_damage(struct weston_compositor *ec)
2374{
2375 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002376 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002377 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002378
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002379 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002380
2381 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002382 pixman_region32_copy(&plane->clip, &clip);
2383
2384 pixman_region32_init(&opaque);
2385
Jason Ekstranda7af7042013-10-12 22:38:11 -05002386 wl_list_for_each(ev, &ec->view_list, link) {
2387 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002388 continue;
2389
Jason Ekstranda7af7042013-10-12 22:38:11 -05002390 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002391 }
2392
2393 pixman_region32_union(&clip, &clip, &opaque);
2394 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002395 }
2396
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002397 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002398
Jason Ekstranda7af7042013-10-12 22:38:11 -05002399 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002400 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002401
2402 wl_list_for_each(ev, &ec->view_list, link) {
2403 if (ev->surface->touched)
2404 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002405 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002406
2407 surface_flush_damage(ev->surface);
2408
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002409 /* Both the renderer and the backend have seen the buffer
2410 * by now. If renderer needs the buffer, it has its own
2411 * reference set. If the backend wants to keep the buffer
2412 * around for migrating the surface into a non-primary plane
2413 * later, keep_buffer is true. Otherwise, drop the core
2414 * reference now, and allow early buffer release. This enables
2415 * clients to use single-buffering.
2416 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002417 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002418 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002419 weston_buffer_release_reference(
2420 &ev->surface->buffer_release_ref, NULL);
2421 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002422 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002423}
2424
2425static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002426surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002427{
2428 struct weston_subsurface *sub;
2429
Pekka Paalanene67858b2013-04-25 13:57:42 +03002430 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002431 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002432 continue;
2433
Jason Ekstranda7af7042013-10-12 22:38:11 -05002434 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2435 wl_list_init(&sub->surface->views);
2436
2437 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002438 }
2439}
2440
2441static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002442surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002443{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002444 struct weston_subsurface *sub;
2445 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002446
Jason Ekstranda7af7042013-10-12 22:38:11 -05002447 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2448 if (sub->surface == surface)
2449 continue;
2450
George Kiagiadakised04d382014-06-13 18:10:26 +02002451 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2452 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002453 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002454 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002455
2456 surface_free_unused_subsurface_views(sub->surface);
2457 }
2458}
2459
2460static void
2461view_list_add_subsurface_view(struct weston_compositor *compositor,
2462 struct weston_subsurface *sub,
2463 struct weston_view *parent)
2464{
2465 struct weston_subsurface *child;
2466 struct weston_view *view = NULL, *iv;
2467
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002468 if (!weston_surface_is_mapped(sub->surface))
2469 return;
2470
Jason Ekstranda7af7042013-10-12 22:38:11 -05002471 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2472 if (iv->geometry.parent == parent) {
2473 view = iv;
2474 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002475 }
2476 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002477
2478 if (view) {
2479 /* Put it back in the surface's list of views */
2480 wl_list_remove(&view->surface_link);
2481 wl_list_insert(&sub->surface->views, &view->surface_link);
2482 } else {
2483 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002484 weston_view_set_position(view,
2485 sub->position.x,
2486 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002487 weston_view_set_transform_parent(view, parent);
2488 }
2489
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002490 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002491 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002492 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002493
Pekka Paalanenb188e912013-11-19 14:03:35 +02002494 if (wl_list_empty(&sub->surface->subsurface_list)) {
2495 wl_list_insert(compositor->view_list.prev, &view->link);
2496 return;
2497 }
2498
2499 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2500 if (child->surface == sub->surface)
2501 wl_list_insert(compositor->view_list.prev, &view->link);
2502 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002503 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002504 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002505}
2506
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002507/* This recursively adds the sub-surfaces for a view, relying on the
2508 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2509 * change first happens to the sub-surface list, and then automatically
2510 * propagates here. See weston_surface_damage_subsurfaces() for how the
2511 * sub-surfaces receive damage when the client changes the state.
2512 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002513static void
2514view_list_add(struct weston_compositor *compositor,
2515 struct weston_view *view)
2516{
2517 struct weston_subsurface *sub;
2518
2519 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002520
Pekka Paalanenb188e912013-11-19 14:03:35 +02002521 if (wl_list_empty(&view->surface->subsurface_list)) {
2522 wl_list_insert(compositor->view_list.prev, &view->link);
2523 return;
2524 }
2525
2526 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2527 if (sub->surface == view->surface)
2528 wl_list_insert(compositor->view_list.prev, &view->link);
2529 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002530 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002531 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002532}
2533
2534static void
2535weston_compositor_build_view_list(struct weston_compositor *compositor)
2536{
2537 struct weston_view *view;
2538 struct weston_layer *layer;
2539
2540 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002541 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002542 surface_stash_subsurface_views(view->surface);
2543
2544 wl_list_init(&compositor->view_list);
2545 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002546 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002547 view_list_add(compositor, view);
2548 }
2549 }
2550
2551 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002552 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002553 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002554}
2555
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002556static void
2557weston_output_take_feedback_list(struct weston_output *output,
2558 struct weston_surface *surface)
2559{
2560 struct weston_view *view;
2561 struct weston_presentation_feedback *feedback;
2562 uint32_t flags = 0xffffffff;
2563
2564 if (wl_list_empty(&surface->feedback_list))
2565 return;
2566
2567 /* All views must have the flag for the flag to survive. */
2568 wl_list_for_each(view, &surface->views, surface_link) {
2569 /* ignore views that are not on this output at all */
2570 if (view->output_mask & (1u << output->id))
2571 flags &= view->psf_flags;
2572 }
2573
2574 wl_list_for_each(feedback, &surface->feedback_list, link)
2575 feedback->psf_flags = flags;
2576
2577 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2578 wl_list_init(&surface->feedback_list);
2579}
2580
David Herrmann1edf44c2013-10-22 17:11:26 +02002581static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002582weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002583{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002584 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002585 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002586 struct weston_animation *animation, *next;
2587 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002588 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002589 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002590 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002591 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302592 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002593
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002594 if (output->destroying)
2595 return 0;
2596
Pekka Paalanenb5026542014-11-12 15:09:24 +02002597 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2598
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002599 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002600 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002601
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302602 /* Find the highest protection desired for an output */
2603 wl_list_for_each(ev, &ec->view_list, link) {
2604 if (ev->surface->output_mask & (1u << output->id)) {
2605 /*
2606 * The desired_protection of the output should be the
2607 * maximum of the desired_protection of the surfaces,
2608 * that are displayed on that output, to avoid
2609 * reducing the protection for existing surfaces.
2610 */
2611 if (ev->surface->desired_protection > highest_requested)
2612 highest_requested =
2613 ev->surface->desired_protection;
2614 }
2615 }
2616
2617 output->desired_protection = highest_requested;
2618
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002619 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002620 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002621 } else {
2622 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002623 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002624 ev->psf_flags = 0;
2625 }
2626 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002627
Pekka Paalanene67858b2013-04-25 13:57:42 +03002628 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002629 wl_list_for_each(ev, &ec->view_list, link) {
2630 /* Note: This operation is safe to do multiple times on the
2631 * same surface.
2632 */
2633 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002634 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002635 &ev->surface->frame_callback_list);
2636 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002637
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002638 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002639 }
2640 }
2641
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002642 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002643
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002644 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002645 pixman_region32_intersect(&output_damage,
2646 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002647 pixman_region32_subtract(&output_damage,
2648 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002649
Scott Moreauccbf29d2012-02-22 14:21:41 -07002650 if (output->dirty)
2651 weston_output_update_matrix(output);
2652
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002653 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002654
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002655 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002656
Daniel Stone09a97e22017-03-01 11:34:06 +00002657 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002658 if (r == 0)
2659 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002660
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002661 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002662
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002663 frame_time_msec = timespec_to_msec(&output->frame_time);
2664
Jonas Ådahldb773762012-06-13 00:01:21 +02002665 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002666 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002667 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002668 }
2669
Scott Moreaud64cf212012-06-08 19:40:54 -06002670 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002671 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002672 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002673 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002674
Pekka Paalanenb5026542014-11-12 15:09:24 +02002675 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2676
David Herrmann1edf44c2013-10-22 17:11:26 +02002677 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002678}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002679
Pekka Paalanen82919792014-05-21 13:51:49 +03002680static void
2681weston_output_schedule_repaint_reset(struct weston_output *output)
2682{
Daniel Stone05df8c12017-03-03 16:59:42 +00002683 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002684 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002685}
2686
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002687static int
2688weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2689 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002690{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002691 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002692 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002693 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002694
Daniel Stone6847b852017-03-01 11:34:08 +00002695 /* We're not ready yet; come back to make a decision later. */
2696 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002697 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002698
2699 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2700 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002701 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002702
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002703 /* If we're sleeping, drop the repaint machinery entirely; we will
2704 * explicitly repaint all outputs when we come back. */
2705 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2706 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2707 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002708
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002709 /* We don't actually need to repaint this output; drop it from
2710 * repaint until something causes damage. */
2711 if (!output->repaint_needed)
2712 goto err;
2713
2714 /* If repaint fails, we aren't going to get weston_output_finish_frame
2715 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002716 * something schedules a successful repaint later. As repainting may
2717 * take some time, re-read our clock as a courtesy to the next
2718 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002719 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002720 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002721 if (ret != 0)
2722 goto err;
2723
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002724 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002725 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002726
2727err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002728 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002729 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002730}
2731
2732static void
2733output_repaint_timer_arm(struct weston_compositor *compositor)
2734{
2735 struct weston_output *output;
2736 bool any_should_repaint = false;
2737 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002738 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002739
2740 weston_compositor_read_presentation_clock(compositor, &now);
2741
2742 wl_list_for_each(output, &compositor->output_list, link) {
2743 int64_t msec_to_this;
2744
2745 if (output->repaint_status != REPAINT_SCHEDULED)
2746 continue;
2747
2748 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2749 &now);
2750 if (!any_should_repaint || msec_to_this < msec_to_next)
2751 msec_to_next = msec_to_this;
2752
2753 any_should_repaint = true;
2754 }
2755
2756 if (!any_should_repaint)
2757 return;
2758
2759 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2760 * This is a workaround to allow coalescing multiple output repaints
2761 * particularly from weston_output_finish_frame()
2762 * into the same call, which would not happen if we called
2763 * output_repaint_timer_handler() directly.
2764 */
2765 if (msec_to_next < 1)
2766 msec_to_next = 1;
2767
2768 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2769}
2770
2771static int
2772output_repaint_timer_handler(void *data)
2773{
2774 struct weston_compositor *compositor = data;
2775 struct weston_output *output;
2776 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002777 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002778 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002779
2780 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002781
2782 if (compositor->backend->repaint_begin)
2783 repaint_data = compositor->backend->repaint_begin(compositor);
2784
2785 wl_list_for_each(output, &compositor->output_list, link) {
2786 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2787 if (ret)
2788 break;
2789 }
2790
2791 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002792 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002793 ret = compositor->backend->repaint_flush(compositor,
2794 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002795 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002796 if (compositor->backend->repaint_cancel)
2797 compositor->backend->repaint_cancel(compositor,
2798 repaint_data);
2799 }
2800
2801 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002802 wl_list_for_each(output, &compositor->output_list, link) {
2803 if (output->repainted)
2804 weston_output_schedule_repaint_reset(output);
2805 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002806 }
Daniel Stone6847b852017-03-01 11:34:08 +00002807
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002808 wl_list_for_each(output, &compositor->output_list, link)
2809 output->repainted = false;
2810
Daniel Stone6847b852017-03-01 11:34:08 +00002811 output_repaint_timer_arm(compositor);
2812
Pekka Paalanen0513a952014-05-21 16:17:27 +03002813 return 0;
2814}
2815
Marius Vlad55d87362019-06-11 01:15:35 +03002816/**
2817 * \ingroup output
2818 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002819WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002820weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002821 const struct timespec *stamp,
2822 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002823{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002824 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002825 int32_t refresh_nsec;
2826 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002827 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002828
Pekka Paalanenb5026542014-11-12 15:09:24 +02002829
Daniel Stone05df8c12017-03-03 16:59:42 +00002830 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002831 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2832
Daniel Stone6847b852017-03-01 11:34:08 +00002833 weston_compositor_read_presentation_clock(compositor, &now);
2834
Daniel Stone3615ce12017-03-01 11:34:05 +00002835 /* If we haven't been supplied any timestamp at all, we don't have a
2836 * timebase to work against, so any delay just wastes time. Push a
2837 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002838 if (!stamp) {
2839 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002840 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002841 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002842
Marius Vladdf9278a2018-03-06 18:56:23 +02002843 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2844 TLP_VBLANK(stamp), TLP_END);
2845
Pekka Paalanend7894d02015-07-03 15:08:53 +03002846 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002847 weston_presentation_feedback_present_list(&output->feedback_list,
2848 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002849 output->msc,
2850 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002851
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002852 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002853
Daniel Stone6847b852017-03-01 11:34:08 +00002854 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2855 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2856 -compositor->repaint_msec);
2857 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002858
2859 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002860 static bool warned;
2861
2862 if (!warned)
2863 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002864 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002865 warned = true;
2866
Daniel Stone6847b852017-03-01 11:34:08 +00002867 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002868 }
2869
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002870 /* Called from restart_repaint_loop and restart happens already after
2871 * the deadline given by repaint_msec? In that case we delay until
2872 * the deadline of the next frame, to give clients a more predictable
2873 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002874 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2875 msec_rel < 0) {
2876 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2877 timespec_add_nsec(&output->next_repaint,
2878 &output->next_repaint,
2879 refresh_nsec);
2880 }
2881 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002882
Daniel Stone3615ce12017-03-01 11:34:05 +00002883out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002884 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002885 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002886}
2887
2888static void
2889idle_repaint(void *data)
2890{
2891 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002892 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002893
Daniel Stone05df8c12017-03-03 16:59:42 +00002894 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2895 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002896 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002897 ret = output->start_repaint_loop(output);
2898 if (ret != 0)
2899 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002900}
2901
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002902WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002903weston_layer_entry_insert(struct weston_layer_entry *list,
2904 struct weston_layer_entry *entry)
2905{
2906 wl_list_insert(&list->link, &entry->link);
2907 entry->layer = list->layer;
2908}
2909
2910WL_EXPORT void
2911weston_layer_entry_remove(struct weston_layer_entry *entry)
2912{
2913 wl_list_remove(&entry->link);
2914 wl_list_init(&entry->link);
2915 entry->layer = NULL;
2916}
2917
Quentin Glidic82681572016-12-17 13:40:51 +01002918
2919/** Initialize the weston_layer struct.
2920 *
2921 * \param compositor The compositor instance
2922 * \param layer The layer to initialize
2923 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002924WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002925weston_layer_init(struct weston_layer *layer,
2926 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002927{
Quentin Glidic82681572016-12-17 13:40:51 +01002928 layer->compositor = compositor;
2929 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002930 wl_list_init(&layer->view_list.link);
2931 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002932 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002933}
2934
2935/** Sets the position of the layer in the layer list. The layer will be placed
2936 * below any layer with the same position value, if any.
2937 * This function is safe to call if the layer is already on the list, but the
2938 * layer may be moved below other layers at the same position, if any.
2939 *
2940 * \param layer The layer to modify
2941 * \param position The position the layer will be placed at
2942 */
2943WL_EXPORT void
2944weston_layer_set_position(struct weston_layer *layer,
2945 enum weston_layer_position position)
2946{
2947 struct weston_layer *below;
2948
2949 wl_list_remove(&layer->link);
2950
2951 /* layer_list is ordered from top to bottom, the last layer being the
2952 * background with the smallest position value */
2953
2954 layer->position = position;
2955 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2956 if (below->position >= layer->position) {
2957 wl_list_insert(&below->link, &layer->link);
2958 return;
2959 }
2960 }
2961 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2962}
2963
2964/** Hide a layer by taking it off the layer list.
2965 * This function is safe to call if the layer is not on the list.
2966 *
2967 * \param layer The layer to hide
2968 */
2969WL_EXPORT void
2970weston_layer_unset_position(struct weston_layer *layer)
2971{
2972 wl_list_remove(&layer->link);
2973 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002974}
2975
2976WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002977weston_layer_set_mask(struct weston_layer *layer,
2978 int x, int y, int width, int height)
2979{
2980 struct weston_view *view;
2981
2982 layer->mask.x1 = x;
2983 layer->mask.x2 = x + width;
2984 layer->mask.y1 = y;
2985 layer->mask.y2 = y + height;
2986
2987 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2988 weston_view_geometry_dirty(view);
2989 }
2990}
2991
2992WL_EXPORT void
2993weston_layer_set_mask_infinite(struct weston_layer *layer)
2994{
2995 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2996 UINT32_MAX, UINT32_MAX);
2997}
2998
Daniel Stone3b775632018-07-20 08:38:25 +01002999WL_EXPORT bool
3000weston_layer_mask_is_infinite(struct weston_layer *layer)
3001{
3002 return layer->mask.x1 == INT32_MIN &&
3003 layer->mask.y1 == INT32_MIN &&
3004 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
3005 layer->mask.y2 == INT32_MIN + UINT32_MAX;
3006}
3007
Marius Vlad55d87362019-06-11 01:15:35 +03003008/**
3009 * \ingroup output
3010 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003011WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003012weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003013{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003014 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003015 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003016
Bryce Harrington08976ac2016-08-30 12:05:16 -07003017 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3018 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003019 return;
3020
Pekka Paalanenb5026542014-11-12 15:09:24 +02003021 if (!output->repaint_needed)
3022 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
3023
Kristian Høgsbergef044142011-06-21 15:02:12 -04003024 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003025 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003026
3027 /* If we already have a repaint scheduled for our idle handler,
3028 * no need to set it again. If the repaint has been called but
3029 * not finished, then weston_output_finish_frame() will notice
3030 * that a repaint is needed and schedule one. */
3031 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003032 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003033
Daniel Stone05df8c12017-03-03 16:59:42 +00003034 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003035 assert(!output->idle_repaint_source);
3036 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3037 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003038 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003039}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003040
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003041/** weston_compositor_schedule_repaint
3042 * \ingroup compositor
3043 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003044WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003045weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3046{
3047 struct weston_output *output;
3048
3049 wl_list_for_each(output, &compositor->output_list, link)
3050 weston_output_schedule_repaint(output);
3051}
3052
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003053static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003054surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003055{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003056 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003057}
3058
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003059static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003060surface_attach(struct wl_client *client,
3061 struct wl_resource *resource,
3062 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3063{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003064 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003065 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003066
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003067 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003068 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003069 if (buffer == NULL) {
3070 wl_client_post_no_memory(client);
3071 return;
3072 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003073 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003074
Pekka Paalanende685b82012-12-04 15:58:12 +02003075 /* Attach, attach, without commit in between does not send
3076 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003077 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003078
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003079 surface->pending.sx = sx;
3080 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003081 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003082}
3083
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003084static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003085surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003086 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003087 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003088{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003089 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003090
Derek Foreman57e92ed2015-11-17 14:11:35 -06003091 if (width <= 0 || height <= 0)
3092 return;
3093
Derek Foreman152254b2015-11-26 14:17:48 -06003094 pixman_region32_union_rect(&surface->pending.damage_surface,
3095 &surface->pending.damage_surface,
3096 x, y, width, height);
3097}
3098
3099static void
3100surface_damage_buffer(struct wl_client *client,
3101 struct wl_resource *resource,
3102 int32_t x, int32_t y, int32_t width, int32_t height)
3103{
3104 struct weston_surface *surface = wl_resource_get_user_data(resource);
3105
3106 if (width <= 0 || height <= 0)
3107 return;
3108
3109 pixman_region32_union_rect(&surface->pending.damage_buffer,
3110 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003111 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003112}
3113
Kristian Høgsberg33418202011-08-16 23:01:28 -04003114static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003115destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003116{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003117 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003118
3119 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003120 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003121}
3122
3123static void
3124surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003125 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003126{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003127 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003128 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003129
3130 cb = malloc(sizeof *cb);
3131 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003132 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003133 return;
3134 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003135
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003136 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3137 callback);
3138 if (cb->resource == NULL) {
3139 free(cb);
3140 wl_resource_post_no_memory(resource);
3141 return;
3142 }
3143
Jason Ekstranda85118c2013-06-27 20:17:02 -05003144 wl_resource_set_implementation(cb->resource, NULL, cb,
3145 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003146
Pekka Paalanenbc106382012-10-10 12:49:31 +03003147 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003148}
3149
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003150static void
3151surface_set_opaque_region(struct wl_client *client,
3152 struct wl_resource *resource,
3153 struct wl_resource *region_resource)
3154{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003155 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003156 struct weston_region *region;
3157
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003158 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003159 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003160 pixman_region32_copy(&surface->pending.opaque,
3161 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003162 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003163 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003164 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003165}
3166
3167static void
3168surface_set_input_region(struct wl_client *client,
3169 struct wl_resource *resource,
3170 struct wl_resource *region_resource)
3171{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003172 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003173 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003174
3175 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003176 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003177 pixman_region32_copy(&surface->pending.input,
3178 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003179 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003180 pixman_region32_fini(&surface->pending.input);
3181 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003182 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003183}
3184
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003185/* Cause damage to this sub-surface and all its children.
3186 *
3187 * This is useful when there are state changes that need an implicit
3188 * damage, e.g. a z-order change.
3189 */
3190static void
3191weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3192{
3193 struct weston_subsurface *child;
3194
3195 weston_surface_damage(sub->surface);
3196 sub->reordered = false;
3197
3198 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3199 if (child != sub)
3200 weston_surface_damage_subsurfaces(child);
3201}
3202
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003203static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003204weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003205{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003206 struct weston_subsurface *sub;
3207
3208 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3209 parent_link_pending) {
3210 wl_list_remove(&sub->parent_link);
3211 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003212
3213 if (sub->reordered)
3214 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003215 }
3216}
3217
3218static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003219weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003220 struct weston_matrix *matrix)
3221{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003222 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003223 double src_width, src_height, dest_width, dest_height;
3224
3225 weston_matrix_init(matrix);
3226
3227 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3228 src_width = surface->width_from_buffer;
3229 src_height = surface->height_from_buffer;
3230 } else {
3231 src_width = wl_fixed_to_double(vp->buffer.src_width);
3232 src_height = wl_fixed_to_double(vp->buffer.src_height);
3233 }
3234
3235 if (vp->surface.width == -1) {
3236 dest_width = src_width;
3237 dest_height = src_height;
3238 } else {
3239 dest_width = vp->surface.width;
3240 dest_height = vp->surface.height;
3241 }
3242
3243 if (src_width != dest_width || src_height != dest_height)
3244 weston_matrix_scale(matrix,
3245 src_width / dest_width,
3246 src_height / dest_height, 1);
3247
3248 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3249 weston_matrix_translate(matrix,
3250 wl_fixed_to_double(vp->buffer.src_x),
3251 wl_fixed_to_double(vp->buffer.src_y),
3252 0);
3253
3254 switch (vp->buffer.transform) {
3255 case WL_OUTPUT_TRANSFORM_FLIPPED:
3256 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3257 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3258 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3259 weston_matrix_scale(matrix, -1, 1, 1);
3260 weston_matrix_translate(matrix,
3261 surface->width_from_buffer, 0, 0);
3262 break;
3263 }
3264
3265 switch (vp->buffer.transform) {
3266 default:
3267 case WL_OUTPUT_TRANSFORM_NORMAL:
3268 case WL_OUTPUT_TRANSFORM_FLIPPED:
3269 break;
3270 case WL_OUTPUT_TRANSFORM_90:
3271 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3272 weston_matrix_rotate_xy(matrix, 0, 1);
3273 weston_matrix_translate(matrix,
3274 surface->height_from_buffer, 0, 0);
3275 break;
3276 case WL_OUTPUT_TRANSFORM_180:
3277 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3278 weston_matrix_rotate_xy(matrix, -1, 0);
3279 weston_matrix_translate(matrix,
3280 surface->width_from_buffer,
3281 surface->height_from_buffer, 0);
3282 break;
3283 case WL_OUTPUT_TRANSFORM_270:
3284 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3285 weston_matrix_rotate_xy(matrix, 0, -1);
3286 weston_matrix_translate(matrix,
3287 0, surface->width_from_buffer, 0);
3288 break;
3289 }
3290
3291 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3292}
3293
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003294/**
3295 * Compute a + b > c while being safe to overflows.
3296 */
3297static bool
3298fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3299{
3300 return (int64_t)a + (int64_t)b > (int64_t)c;
3301}
3302
3303static bool
3304weston_surface_is_pending_viewport_source_valid(
3305 const struct weston_surface *surface)
3306{
3307 const struct weston_surface_state *pend = &surface->pending;
3308 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3309 int width_from_buffer = 0;
3310 int height_from_buffer = 0;
3311 wl_fixed_t w;
3312 wl_fixed_t h;
3313
3314 /* If viewport source rect is not set, it is always ok. */
3315 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3316 return true;
3317
3318 if (pend->newly_attached) {
3319 if (pend->buffer) {
3320 convert_size_by_transform_scale(&width_from_buffer,
3321 &height_from_buffer,
3322 pend->buffer->width,
3323 pend->buffer->height,
3324 vp->buffer.transform,
3325 vp->buffer.scale);
3326 } else {
3327 /* No buffer: viewport is irrelevant. */
3328 return true;
3329 }
3330 } else {
3331 width_from_buffer = surface->width_from_buffer;
3332 height_from_buffer = surface->height_from_buffer;
3333 }
3334
3335 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3336 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3337
3338 /* No buffer: viewport is irrelevant. */
3339 if (width_from_buffer == 0 || height_from_buffer == 0)
3340 return true;
3341
3342 /* overflow checks for wl_fixed_from_int() */
3343 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3344 return false;
3345 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3346 return false;
3347
3348 w = wl_fixed_from_int(width_from_buffer);
3349 h = wl_fixed_from_int(height_from_buffer);
3350
3351 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3352 return false;
3353 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3354 return false;
3355
3356 return true;
3357}
3358
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003359static bool
3360fixed_is_integer(wl_fixed_t v)
3361{
3362 return (v & 0xff) == 0;
3363}
3364
3365static bool
3366weston_surface_is_pending_viewport_dst_size_int(
3367 const struct weston_surface *surface)
3368{
3369 const struct weston_buffer_viewport *vp =
3370 &surface->pending.buffer_viewport;
3371
3372 if (vp->surface.width != -1) {
3373 assert(vp->surface.width > 0 && vp->surface.height > 0);
3374 return true;
3375 }
3376
3377 return fixed_is_integer(vp->buffer.src_width) &&
3378 fixed_is_integer(vp->buffer.src_height);
3379}
3380
Derek Foreman152254b2015-11-26 14:17:48 -06003381/* Translate pending damage in buffer co-ordinates to surface
3382 * co-ordinates and union it with a pixman_region32_t.
3383 * This should only be called after the buffer is attached.
3384 */
3385static void
3386apply_damage_buffer(pixman_region32_t *dest,
3387 struct weston_surface *surface,
3388 struct weston_surface_state *state)
3389{
3390 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3391
3392 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3393 * translated into surface co-ordinates and unioned with
3394 * any other surface damage.
3395 * None of this makes sense if there is no buffer though.
3396 */
3397 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3398 pixman_region32_t buffer_damage;
3399
3400 pixman_region32_intersect_rect(&state->damage_buffer,
3401 &state->damage_buffer,
3402 0, 0, buffer->width,
3403 buffer->height);
3404 pixman_region32_init(&buffer_damage);
3405 weston_matrix_transform_region(&buffer_damage,
3406 &surface->buffer_to_surface_matrix,
3407 &state->damage_buffer);
3408 pixman_region32_union(dest, dest, &buffer_damage);
3409 pixman_region32_fini(&buffer_damage);
3410 }
3411 /* We should clear this on commit even if there was no buffer */
3412 pixman_region32_clear(&state->damage_buffer);
3413}
3414
Jason Ekstrand1e059042014-10-16 10:55:19 -05003415static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303416weston_surface_set_desired_protection(struct weston_surface *surface,
3417 enum weston_hdcp_protection protection)
3418{
3419 if (surface->desired_protection == protection)
3420 return;
3421 surface->desired_protection = protection;
3422 weston_surface_damage(surface);
3423}
3424
3425static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303426weston_surface_set_protection_mode(struct weston_surface *surface,
3427 enum weston_surface_protection_mode p_mode)
3428{
3429 struct content_protection *cp = surface->compositor->content_protection;
3430 struct protected_surface *psurface;
3431
3432 surface->protection_mode = p_mode;
3433 wl_list_for_each(psurface, &cp->protected_list, link) {
3434 if (!psurface || psurface->surface != surface)
3435 continue;
3436 weston_protected_surface_send_event(psurface,
3437 surface->current_protection);
3438 }
3439}
3440
3441static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003442weston_surface_commit_state(struct weston_surface *surface,
3443 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003444{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003445 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003446 pixman_region32_t opaque;
3447
Alexander Larsson4ea95522013-05-22 14:41:37 +02003448 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003449 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003450 /* wp_viewport.set_source */
3451 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003452 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003453
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003454 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003455 if (state->newly_attached) {
3456 /* zwp_surface_synchronization_v1.set_acquire_fence */
3457 fd_move(&surface->acquire_fence_fd,
3458 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003459 /* zwp_surface_synchronization_v1.get_release */
3460 weston_buffer_release_move(&surface->buffer_release_ref,
3461 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003462 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003463 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003464 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003465 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003466 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003467
Jason Ekstrand1e059042014-10-16 10:55:19 -05003468 weston_surface_build_buffer_matrix(surface,
3469 &surface->surface_to_buffer_matrix);
3470 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3471 &surface->surface_to_buffer_matrix);
3472
Jason Ekstrand7b982072014-05-20 14:33:03 -05003473 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003474 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003475 if (surface->committed)
3476 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003477 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003478
Jason Ekstrand7b982072014-05-20 14:33:03 -05003479 state->sx = 0;
3480 state->sy = 0;
3481 state->newly_attached = 0;
3482 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003483
Derek Foreman152254b2015-11-26 14:17:48 -06003484 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003485 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003486 (pixman_region32_not_empty(&state->damage_surface) ||
3487 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003488 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003489
Pekka Paalanen8e159182012-10-10 12:49:25 +03003490 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003491 &state->damage_surface);
3492
3493 apply_damage_buffer(&surface->damage, surface, state);
3494
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003495 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003496 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003497 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003498
3499 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003500 pixman_region32_init(&opaque);
3501 pixman_region32_intersect_rect(&opaque, &state->opaque,
3502 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003503
3504 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3505 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003506 wl_list_for_each(view, &surface->views, surface_link)
3507 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003508 }
3509
3510 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003511
3512 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003513 pixman_region32_intersect_rect(&surface->input, &state->input,
3514 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003515
Pekka Paalanenbc106382012-10-10 12:49:31 +03003516 /* wl_surface.frame */
3517 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003518 &state->frame_callback_list);
3519 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003520
3521 /* XXX:
3522 * What should happen with a feedback request, if there
3523 * is no wl_buffer attached for this commit?
3524 */
3525
3526 /* presentation.feedback */
3527 wl_list_insert_list(&surface->feedback_list,
3528 &state->feedback_list);
3529 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003530
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303531 /* weston_protected_surface.enforced/relaxed */
3532 if (surface->protection_mode != state->protection_mode)
3533 weston_surface_set_protection_mode(surface,
3534 state->protection_mode);
3535
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303536 /* weston_protected_surface.set_type */
3537 weston_surface_set_desired_protection(surface, state->desired_protection);
3538
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003539 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003540}
3541
3542static void
3543weston_surface_commit(struct weston_surface *surface)
3544{
3545 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003546
Pekka Paalanene67858b2013-04-25 13:57:42 +03003547 weston_surface_commit_subsurface_order(surface);
3548
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003549 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003550}
3551
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003552static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003553weston_subsurface_commit(struct weston_subsurface *sub);
3554
3555static void
3556weston_subsurface_parent_commit(struct weston_subsurface *sub,
3557 int parent_is_synchronized);
3558
3559static void
3560surface_commit(struct wl_client *client, struct wl_resource *resource)
3561{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003562 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003563 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3564
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003565 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3566 assert(surface->viewport_resource);
3567
3568 wl_resource_post_error(surface->viewport_resource,
3569 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3570 "wl_surface@%d has viewport source outside buffer",
3571 wl_resource_get_id(resource));
3572 return;
3573 }
3574
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003575 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3576 assert(surface->viewport_resource);
3577
3578 wl_resource_post_error(surface->viewport_resource,
3579 WP_VIEWPORT_ERROR_BAD_SIZE,
3580 "wl_surface@%d viewport dst size not integer",
3581 wl_resource_get_id(resource));
3582 return;
3583 }
3584
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003585 if (surface->pending.acquire_fence_fd >= 0) {
3586 assert(surface->synchronization_resource);
3587
3588 if (!surface->pending.buffer) {
3589 fd_clear(&surface->pending.acquire_fence_fd);
3590 wl_resource_post_error(surface->synchronization_resource,
3591 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3592 "wl_surface@%"PRIu32" no buffer for synchronization",
3593 wl_resource_get_id(resource));
3594 return;
3595 }
3596
3597 /* We support fences for both wp_linux_dmabuf and opaque EGL
3598 * buffers, as mandated by minor version 2 of the
3599 * zwp_linux_explicit_synchronization_v1 protocol. Since
3600 * renderers that support fences currently only support these
3601 * two buffer types plus SHM buffers, we can just check for the
3602 * SHM buffer case here.
3603 */
3604 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3605 fd_clear(&surface->pending.acquire_fence_fd);
3606 wl_resource_post_error(surface->synchronization_resource,
3607 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3608 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3609 wl_resource_get_id(resource));
3610 return;
3611 }
3612 }
3613
Alexandros Frantzis67629672018-10-19 12:14:11 +03003614 if (surface->pending.buffer_release_ref.buffer_release &&
3615 !surface->pending.buffer) {
3616 assert(surface->synchronization_resource);
3617
3618 wl_resource_post_error(surface->synchronization_resource,
3619 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3620 "wl_surface@%"PRIu32" no buffer for synchronization",
3621 wl_resource_get_id(resource));
3622 return;
3623 }
3624
Pekka Paalanene67858b2013-04-25 13:57:42 +03003625 if (sub) {
3626 weston_subsurface_commit(sub);
3627 return;
3628 }
3629
3630 weston_surface_commit(surface);
3631
3632 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3633 if (sub->surface != surface)
3634 weston_subsurface_parent_commit(sub, 0);
3635 }
3636}
3637
3638static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003639surface_set_buffer_transform(struct wl_client *client,
3640 struct wl_resource *resource, int transform)
3641{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003642 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003643
Jonny Lamba55f1392014-05-30 12:07:15 +02003644 /* if wl_output.transform grows more members this will need to be updated. */
3645 if (transform < 0 ||
3646 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3647 wl_resource_post_error(resource,
3648 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3649 "buffer transform must be a valid transform "
3650 "('%d' specified)", transform);
3651 return;
3652 }
3653
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003654 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003655 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003656}
3657
Alexander Larsson4ea95522013-05-22 14:41:37 +02003658static void
3659surface_set_buffer_scale(struct wl_client *client,
3660 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003661 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003662{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003663 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003664
Jonny Lamba55f1392014-05-30 12:07:15 +02003665 if (scale < 1) {
3666 wl_resource_post_error(resource,
3667 WL_SURFACE_ERROR_INVALID_SCALE,
3668 "buffer scale must be at least one "
3669 "('%d' specified)", scale);
3670 return;
3671 }
3672
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003673 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003674 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003675}
3676
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003677static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003678 surface_destroy,
3679 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003680 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003681 surface_frame,
3682 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003683 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003684 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003685 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003686 surface_set_buffer_scale,
3687 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003688};
3689
3690static void
3691compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003692 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003693{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003694 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003695 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003696
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003697 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003698 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003699 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003700 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003701 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003702
Jason Ekstranda85118c2013-06-27 20:17:02 -05003703 surface->resource =
3704 wl_resource_create(client, &wl_surface_interface,
3705 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003706 if (surface->resource == NULL) {
3707 weston_surface_destroy(surface);
3708 wl_resource_post_no_memory(resource);
3709 return;
3710 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003711 wl_resource_set_implementation(surface->resource, &surface_interface,
3712 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003713
3714 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003715}
3716
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003717static void
3718destroy_region(struct wl_resource *resource)
3719{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003720 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003721
3722 pixman_region32_fini(&region->region);
3723 free(region);
3724}
3725
3726static void
3727region_destroy(struct wl_client *client, struct wl_resource *resource)
3728{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003729 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003730}
3731
3732static void
3733region_add(struct wl_client *client, struct wl_resource *resource,
3734 int32_t x, int32_t y, int32_t width, int32_t height)
3735{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003736 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003737
3738 pixman_region32_union_rect(&region->region, &region->region,
3739 x, y, width, height);
3740}
3741
3742static void
3743region_subtract(struct wl_client *client, struct wl_resource *resource,
3744 int32_t x, int32_t y, int32_t width, int32_t height)
3745{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003746 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003747 pixman_region32_t rect;
3748
3749 pixman_region32_init_rect(&rect, x, y, width, height);
3750 pixman_region32_subtract(&region->region, &region->region, &rect);
3751 pixman_region32_fini(&rect);
3752}
3753
3754static const struct wl_region_interface region_interface = {
3755 region_destroy,
3756 region_add,
3757 region_subtract
3758};
3759
3760static void
3761compositor_create_region(struct wl_client *client,
3762 struct wl_resource *resource, uint32_t id)
3763{
3764 struct weston_region *region;
3765
3766 region = malloc(sizeof *region);
3767 if (region == NULL) {
3768 wl_resource_post_no_memory(resource);
3769 return;
3770 }
3771
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003772 pixman_region32_init(&region->region);
3773
Jason Ekstranda85118c2013-06-27 20:17:02 -05003774 region->resource =
3775 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003776 if (region->resource == NULL) {
3777 free(region);
3778 wl_resource_post_no_memory(resource);
3779 return;
3780 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003781 wl_resource_set_implementation(region->resource, &region_interface,
3782 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003783}
3784
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003785static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003786 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003787 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003788};
3789
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003790static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003791weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3792{
3793 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003794
Jason Ekstrand7b982072014-05-20 14:33:03 -05003795 weston_surface_commit_state(surface, &sub->cached);
3796 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003797
3798 weston_surface_commit_subsurface_order(surface);
3799
3800 weston_surface_schedule_repaint(surface);
3801
Jason Ekstrand7b982072014-05-20 14:33:03 -05003802 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003803}
3804
3805static void
3806weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3807{
3808 struct weston_surface *surface = sub->surface;
3809
3810 /*
3811 * If this commit would cause the surface to move by the
3812 * attach(dx, dy) parameters, the old damage region must be
3813 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003814 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003815 */
Derek Foreman152254b2015-11-26 14:17:48 -06003816 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003817 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003818 pixman_region32_union(&sub->cached.damage_surface,
3819 &sub->cached.damage_surface,
3820 &surface->pending.damage_surface);
3821 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003822
3823 if (surface->pending.newly_attached) {
3824 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003825 weston_surface_state_set_buffer(&sub->cached,
3826 surface->pending.buffer);
3827 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003828 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003829 weston_presentation_feedback_discard_list(
3830 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003831 /* zwp_surface_synchronization_v1.set_acquire_fence */
3832 fd_move(&sub->cached.acquire_fence_fd,
3833 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003834 /* zwp_surface_synchronization_v1.get_release */
3835 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3836 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003837 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303838 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303839 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003840 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003841 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003842 sub->cached.sx += surface->pending.sx;
3843 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003844
Derek Foreman152254b2015-11-26 14:17:48 -06003845 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3846
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003847 sub->cached.buffer_viewport.changed |=
3848 surface->pending.buffer_viewport.changed;
3849 sub->cached.buffer_viewport.buffer =
3850 surface->pending.buffer_viewport.buffer;
3851 sub->cached.buffer_viewport.surface =
3852 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003853
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003854 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003855
3856 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3857
3858 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3859
3860 wl_list_insert_list(&sub->cached.frame_callback_list,
3861 &surface->pending.frame_callback_list);
3862 wl_list_init(&surface->pending.frame_callback_list);
3863
Pekka Paalanen133e4392014-09-23 22:08:46 -04003864 wl_list_insert_list(&sub->cached.feedback_list,
3865 &surface->pending.feedback_list);
3866 wl_list_init(&surface->pending.feedback_list);
3867
Jason Ekstrand7b982072014-05-20 14:33:03 -05003868 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003869}
3870
Derek Foreman280e7dd2014-10-03 13:13:42 -05003871static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003872weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3873{
3874 while (sub) {
3875 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003876 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003877
3878 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003879 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003880
3881 sub = weston_surface_to_subsurface(sub->parent);
3882 }
3883
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003884 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003885}
3886
3887static void
3888weston_subsurface_commit(struct weston_subsurface *sub)
3889{
3890 struct weston_surface *surface = sub->surface;
3891 struct weston_subsurface *tmp;
3892
3893 /* Recursive check for effectively synchronized. */
3894 if (weston_subsurface_is_synchronized(sub)) {
3895 weston_subsurface_commit_to_cache(sub);
3896 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003897 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003898 /* flush accumulated state from cache */
3899 weston_subsurface_commit_to_cache(sub);
3900 weston_subsurface_commit_from_cache(sub);
3901 } else {
3902 weston_surface_commit(surface);
3903 }
3904
3905 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3906 if (tmp->surface != surface)
3907 weston_subsurface_parent_commit(tmp, 0);
3908 }
3909 }
3910}
3911
3912static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003913weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003914{
3915 struct weston_surface *surface = sub->surface;
3916 struct weston_subsurface *tmp;
3917
Pekka Paalanene67858b2013-04-25 13:57:42 +03003918 /* From now on, commit_from_cache the whole sub-tree, regardless of
3919 * the synchronized mode of each child. This sub-surface or some
3920 * of its ancestors were synchronized, so we are synchronized
3921 * all the way down.
3922 */
3923
Jason Ekstrand7b982072014-05-20 14:33:03 -05003924 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003925 weston_subsurface_commit_from_cache(sub);
3926
3927 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3928 if (tmp->surface != surface)
3929 weston_subsurface_parent_commit(tmp, 1);
3930 }
3931}
3932
3933static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003934weston_subsurface_parent_commit(struct weston_subsurface *sub,
3935 int parent_is_synchronized)
3936{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003937 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003938 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003939 wl_list_for_each(view, &sub->surface->views, surface_link)
3940 weston_view_set_position(view,
3941 sub->position.x,
3942 sub->position.y);
3943
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003944 sub->position.set = 0;
3945 }
3946
3947 if (parent_is_synchronized || sub->synchronized)
3948 weston_subsurface_synchronized_commit(sub);
3949}
3950
Pekka Paalanen8274d902014-08-06 19:36:51 +03003951static int
3952subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3953{
3954 return snprintf(buf, len, "sub-surface");
3955}
3956
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003957static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003958subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003959{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003960 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003961
Jason Ekstranda7af7042013-10-12 22:38:11 -05003962 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003963 weston_view_set_position(view,
3964 view->geometry.x + dx,
3965 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003966
3967 /* No need to check parent mappedness, because if parent is not
3968 * mapped, parent is not in a visible layer, so this sub-surface
3969 * will not be drawn either.
3970 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003971
Pekka Paalanene67858b2013-04-25 13:57:42 +03003972 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003973 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003974
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003975 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003976 * because that would call it also for the parent surface,
3977 * which might not be mapped yet. That would lead to
3978 * inconsistent state, where the window could never be
3979 * mapped.
3980 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003981 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003982 * weston_surface_is_mapped() return true, so that when the
3983 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003984 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003985 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003986 }
3987}
3988
3989static struct weston_subsurface *
3990weston_surface_to_subsurface(struct weston_surface *surface)
3991{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003992 if (surface->committed == subsurface_committed)
3993 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003994
3995 return NULL;
3996}
3997
Pekka Paalanen01388e22013-04-25 13:57:44 +03003998WL_EXPORT struct weston_surface *
3999weston_surface_get_main_surface(struct weston_surface *surface)
4000{
4001 struct weston_subsurface *sub;
4002
4003 while (surface && (sub = weston_surface_to_subsurface(surface)))
4004 surface = sub->parent;
4005
4006 return surface;
4007}
4008
Pekka Paalanen50b67472014-10-01 15:02:41 +03004009WL_EXPORT int
4010weston_surface_set_role(struct weston_surface *surface,
4011 const char *role_name,
4012 struct wl_resource *error_resource,
4013 uint32_t error_code)
4014{
4015 assert(role_name);
4016
4017 if (surface->role_name == NULL ||
4018 surface->role_name == role_name ||
4019 strcmp(surface->role_name, role_name) == 0) {
4020 surface->role_name = role_name;
4021
4022 return 0;
4023 }
4024
4025 wl_resource_post_error(error_resource, error_code,
4026 "Cannot assign role %s to wl_surface@%d,"
4027 " already has role %s\n",
4028 role_name,
4029 wl_resource_get_id(surface->resource),
4030 surface->role_name);
4031 return -1;
4032}
4033
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004034WL_EXPORT const char *
4035weston_surface_get_role(struct weston_surface *surface)
4036{
4037 return surface->role_name;
4038}
4039
Pekka Paalanen8274d902014-08-06 19:36:51 +03004040WL_EXPORT void
4041weston_surface_set_label_func(struct weston_surface *surface,
4042 int (*desc)(struct weston_surface *,
4043 char *, size_t))
4044{
4045 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02004046 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004047}
4048
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004049/** Get the size of surface contents
4050 *
4051 * \param surface The surface to query.
4052 * \param width Returns the width of raw contents.
4053 * \param height Returns the height of raw contents.
4054 *
4055 * Retrieves the raw surface content size in pixels for the given surface.
4056 * This is the whole content size in buffer pixels. If the surface
4057 * has no content or the renderer does not implement this feature,
4058 * zeroes are returned.
4059 *
4060 * This function is used to determine the buffer size needed for
4061 * a weston_surface_copy_content() call.
4062 */
4063WL_EXPORT void
4064weston_surface_get_content_size(struct weston_surface *surface,
4065 int *width, int *height)
4066{
4067 struct weston_renderer *rer = surface->compositor->renderer;
4068
4069 if (!rer->surface_get_content_size) {
4070 *width = 0;
4071 *height = 0;
4072 return;
4073 }
4074
4075 rer->surface_get_content_size(surface, width, height);
4076}
4077
Quentin Glidic248dd102016-08-12 10:41:34 +02004078/** Get the bounding box of a surface and its subsurfaces
4079 *
4080 * \param surface The surface to query.
4081 * \return The bounding box relative to the surface origin.
4082 *
4083 */
4084WL_EXPORT struct weston_geometry
4085weston_surface_get_bounding_box(struct weston_surface *surface)
4086{
4087 pixman_region32_t region;
4088 pixman_box32_t *box;
4089 struct weston_subsurface *subsurface;
4090
4091 pixman_region32_init_rect(&region,
4092 0, 0,
4093 surface->width, surface->height);
4094
4095 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4096 pixman_region32_union_rect(&region, &region,
4097 subsurface->position.x,
4098 subsurface->position.y,
4099 subsurface->surface->width,
4100 subsurface->surface->height);
4101
4102 box = pixman_region32_extents(&region);
4103 struct weston_geometry geometry = {
4104 .x = box->x1,
4105 .y = box->y1,
4106 .width = box->x2 - box->x1,
4107 .height = box->y2 - box->y1,
4108 };
4109
4110 pixman_region32_fini(&region);
4111
4112 return geometry;
4113}
4114
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004115/** Copy surface contents to system memory.
4116 *
4117 * \param surface The surface to copy from.
4118 * \param target Pointer to the target memory buffer.
4119 * \param size Size of the target buffer in bytes.
4120 * \param src_x X location on contents to copy from.
4121 * \param src_y Y location on contents to copy from.
4122 * \param width Width in pixels of the area to copy.
4123 * \param height Height in pixels of the area to copy.
4124 * \return 0 for success, -1 for failure.
4125 *
4126 * Surface contents are maintained by the renderer. They can be in a
4127 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4128 * else.
4129 *
4130 * Surface contents are copied into memory pointed to by target,
4131 * which has size bytes of space available. The target memory
4132 * may be larger than needed, but being smaller returns an error.
4133 * The extra bytes in target may or may not be written; their content is
4134 * unspecified. Size must be large enough to hold the image.
4135 *
4136 * The image in the target memory will be arranged in rows from
4137 * top to bottom, and pixels on a row from left to right. The pixel
4138 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4139 * width * 4.
4140 *
4141 * Parameters src_x and src_y define the upper-left corner in buffer
4142 * coordinates (pixels) to copy from. Parameters width and height
4143 * define the size of the area to copy in pixels.
4144 *
4145 * The rectangle defined by src_x, src_y, width, height must fit in
4146 * the surface contents. Otherwise an error is returned.
4147 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004148 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004149 * needed target buffer size and rectangle limits.
4150 *
4151 * CURRENT IMPLEMENTATION RESTRICTIONS:
4152 * - the machine must be little-endian due to Pixman formats.
4153 *
4154 * NOTE: Pixman formats are premultiplied.
4155 */
4156WL_EXPORT int
4157weston_surface_copy_content(struct weston_surface *surface,
4158 void *target, size_t size,
4159 int src_x, int src_y,
4160 int width, int height)
4161{
4162 struct weston_renderer *rer = surface->compositor->renderer;
4163 int cw, ch;
4164 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4165
4166 if (!rer->surface_copy_content)
4167 return -1;
4168
4169 weston_surface_get_content_size(surface, &cw, &ch);
4170
4171 if (src_x < 0 || src_y < 0)
4172 return -1;
4173
4174 if (width <= 0 || height <= 0)
4175 return -1;
4176
4177 if (src_x + width > cw || src_y + height > ch)
4178 return -1;
4179
4180 if (width * bytespp * height > size)
4181 return -1;
4182
4183 return rer->surface_copy_content(surface, target, size,
4184 src_x, src_y, width, height);
4185}
4186
Pekka Paalanene67858b2013-04-25 13:57:42 +03004187static void
4188subsurface_set_position(struct wl_client *client,
4189 struct wl_resource *resource, int32_t x, int32_t y)
4190{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004191 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004192
4193 if (!sub)
4194 return;
4195
4196 sub->position.x = x;
4197 sub->position.y = y;
4198 sub->position.set = 1;
4199}
4200
4201static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004202subsurface_find_sibling(struct weston_subsurface *sub,
4203 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004204{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004205 struct weston_surface *parent = sub->parent;
4206 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004207
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004208 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4209 if (sibling->surface == surface && sibling != sub)
4210 return sibling;
4211 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004212
4213 return NULL;
4214}
4215
4216static struct weston_subsurface *
4217subsurface_sibling_check(struct weston_subsurface *sub,
4218 struct weston_surface *surface,
4219 const char *request)
4220{
4221 struct weston_subsurface *sibling;
4222
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004223 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004224 if (!sibling) {
4225 wl_resource_post_error(sub->resource,
4226 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4227 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004228 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004229 return NULL;
4230 }
4231
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004232 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004233
4234 return sibling;
4235}
4236
4237static void
4238subsurface_place_above(struct wl_client *client,
4239 struct wl_resource *resource,
4240 struct wl_resource *sibling_resource)
4241{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004242 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004243 struct weston_surface *surface =
4244 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004245 struct weston_subsurface *sibling;
4246
4247 if (!sub)
4248 return;
4249
4250 sibling = subsurface_sibling_check(sub, surface, "place_above");
4251 if (!sibling)
4252 return;
4253
4254 wl_list_remove(&sub->parent_link_pending);
4255 wl_list_insert(sibling->parent_link_pending.prev,
4256 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004257
4258 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004259}
4260
4261static void
4262subsurface_place_below(struct wl_client *client,
4263 struct wl_resource *resource,
4264 struct wl_resource *sibling_resource)
4265{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004266 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004267 struct weston_surface *surface =
4268 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004269 struct weston_subsurface *sibling;
4270
4271 if (!sub)
4272 return;
4273
4274 sibling = subsurface_sibling_check(sub, surface, "place_below");
4275 if (!sibling)
4276 return;
4277
4278 wl_list_remove(&sub->parent_link_pending);
4279 wl_list_insert(&sibling->parent_link_pending,
4280 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004281
4282 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004283}
4284
4285static void
4286subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4287{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004288 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004289
4290 if (sub)
4291 sub->synchronized = 1;
4292}
4293
4294static void
4295subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4296{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004297 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004298
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004299 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004300 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004301
4302 /* If sub became effectively desynchronized, flush. */
4303 if (!weston_subsurface_is_synchronized(sub))
4304 weston_subsurface_synchronized_commit(sub);
4305 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004306}
4307
4308static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004309weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4310{
4311 wl_list_remove(&sub->parent_link);
4312 wl_list_remove(&sub->parent_link_pending);
4313 wl_list_remove(&sub->parent_destroy_listener.link);
4314 sub->parent = NULL;
4315}
4316
4317static void
4318weston_subsurface_destroy(struct weston_subsurface *sub);
4319
4320static void
4321subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4322{
4323 struct weston_subsurface *sub =
4324 container_of(listener, struct weston_subsurface,
4325 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004326 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004327
4328 /* The protocol object (wl_resource) is left inert. */
4329 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004330 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004331
4332 weston_subsurface_destroy(sub);
4333}
4334
4335static void
4336subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4337{
4338 struct weston_subsurface *sub =
4339 container_of(listener, struct weston_subsurface,
4340 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004341 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004342 assert(sub->surface != sub->parent);
4343
4344 if (weston_surface_is_mapped(sub->surface))
4345 weston_surface_unmap(sub->surface);
4346
4347 weston_subsurface_unlink_parent(sub);
4348}
4349
4350static void
4351subsurface_resource_destroy(struct wl_resource *resource)
4352{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004353 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004354
4355 if (sub)
4356 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004357}
4358
4359static void
4360subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4361{
4362 wl_resource_destroy(resource);
4363}
4364
4365static void
4366weston_subsurface_link_parent(struct weston_subsurface *sub,
4367 struct weston_surface *parent)
4368{
4369 sub->parent = parent;
4370 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004371 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004372 &sub->parent_destroy_listener);
4373
4374 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4375 wl_list_insert(&parent->subsurface_list_pending,
4376 &sub->parent_link_pending);
4377}
4378
4379static void
4380weston_subsurface_link_surface(struct weston_subsurface *sub,
4381 struct weston_surface *surface)
4382{
4383 sub->surface = surface;
4384 sub->surface_destroy_listener.notify =
4385 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004386 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004387 &sub->surface_destroy_listener);
4388}
4389
4390static void
4391weston_subsurface_destroy(struct weston_subsurface *sub)
4392{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004393 struct weston_view *view, *next;
4394
Pekka Paalanene67858b2013-04-25 13:57:42 +03004395 assert(sub->surface);
4396
4397 if (sub->resource) {
4398 assert(weston_surface_to_subsurface(sub->surface) == sub);
4399 assert(sub->parent_destroy_listener.notify ==
4400 subsurface_handle_parent_destroy);
4401
George Kiagiadakised04d382014-06-13 18:10:26 +02004402 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4403 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004404 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004405 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004406
Pekka Paalanene67858b2013-04-25 13:57:42 +03004407 if (sub->parent)
4408 weston_subsurface_unlink_parent(sub);
4409
Jason Ekstrand7b982072014-05-20 14:33:03 -05004410 weston_surface_state_fini(&sub->cached);
4411 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004412
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004413 sub->surface->committed = NULL;
4414 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004415 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004416 } else {
4417 /* the dummy weston_subsurface for the parent itself */
4418 assert(sub->parent_destroy_listener.notify == NULL);
4419 wl_list_remove(&sub->parent_link);
4420 wl_list_remove(&sub->parent_link_pending);
4421 }
4422
4423 wl_list_remove(&sub->surface_destroy_listener.link);
4424 free(sub);
4425}
4426
4427static const struct wl_subsurface_interface subsurface_implementation = {
4428 subsurface_destroy,
4429 subsurface_set_position,
4430 subsurface_place_above,
4431 subsurface_place_below,
4432 subsurface_set_sync,
4433 subsurface_set_desync
4434};
4435
4436static struct weston_subsurface *
4437weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4438 struct weston_surface *parent)
4439{
4440 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004441 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004442
Bryce Harringtonde16d892014-11-20 22:21:57 -08004443 sub = zalloc(sizeof *sub);
4444 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004445 return NULL;
4446
Jason Ekstranda7af7042013-10-12 22:38:11 -05004447 wl_list_init(&sub->unused_views);
4448
Jason Ekstranda85118c2013-06-27 20:17:02 -05004449 sub->resource =
4450 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004451 if (!sub->resource) {
4452 free(sub);
4453 return NULL;
4454 }
4455
Jason Ekstranda85118c2013-06-27 20:17:02 -05004456 wl_resource_set_implementation(sub->resource,
4457 &subsurface_implementation,
4458 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004459 weston_subsurface_link_surface(sub, surface);
4460 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004461 weston_surface_state_init(&sub->cached);
4462 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004463 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004464
4465 return sub;
4466}
4467
4468/* Create a dummy subsurface for having the parent itself in its
4469 * sub-surface lists. Makes stacking order manipulation easy.
4470 */
4471static struct weston_subsurface *
4472weston_subsurface_create_for_parent(struct weston_surface *parent)
4473{
4474 struct weston_subsurface *sub;
4475
Bryce Harringtonde16d892014-11-20 22:21:57 -08004476 sub = zalloc(sizeof *sub);
4477 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004478 return NULL;
4479
4480 weston_subsurface_link_surface(sub, parent);
4481 sub->parent = parent;
4482 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4483 wl_list_insert(&parent->subsurface_list_pending,
4484 &sub->parent_link_pending);
4485
4486 return sub;
4487}
4488
4489static void
4490subcompositor_get_subsurface(struct wl_client *client,
4491 struct wl_resource *resource,
4492 uint32_t id,
4493 struct wl_resource *surface_resource,
4494 struct wl_resource *parent_resource)
4495{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004496 struct weston_surface *surface =
4497 wl_resource_get_user_data(surface_resource);
4498 struct weston_surface *parent =
4499 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004500 struct weston_subsurface *sub;
4501 static const char where[] = "get_subsurface: wl_subsurface@";
4502
4503 if (surface == parent) {
4504 wl_resource_post_error(resource,
4505 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4506 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004507 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004508 return;
4509 }
4510
4511 if (weston_surface_to_subsurface(surface)) {
4512 wl_resource_post_error(resource,
4513 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4514 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004515 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004516 return;
4517 }
4518
Pekka Paalanen50b67472014-10-01 15:02:41 +03004519 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4520 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004521 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004522
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004523 if (weston_surface_get_main_surface(parent) == surface) {
4524 wl_resource_post_error(resource,
4525 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4526 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004527 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004528 return;
4529 }
4530
Pekka Paalanene67858b2013-04-25 13:57:42 +03004531 /* make sure the parent is in its own list */
4532 if (wl_list_empty(&parent->subsurface_list)) {
4533 if (!weston_subsurface_create_for_parent(parent)) {
4534 wl_resource_post_no_memory(resource);
4535 return;
4536 }
4537 }
4538
4539 sub = weston_subsurface_create(id, surface, parent);
4540 if (!sub) {
4541 wl_resource_post_no_memory(resource);
4542 return;
4543 }
4544
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004545 surface->committed = subsurface_committed;
4546 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004547 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004548}
4549
4550static void
4551subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4552{
4553 wl_resource_destroy(resource);
4554}
4555
4556static const struct wl_subcompositor_interface subcompositor_interface = {
4557 subcompositor_destroy,
4558 subcompositor_get_subsurface
4559};
4560
4561static void
4562bind_subcompositor(struct wl_client *client,
4563 void *data, uint32_t version, uint32_t id)
4564{
4565 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004566 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004567
Jason Ekstranda85118c2013-06-27 20:17:02 -05004568 resource =
4569 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004570 if (resource == NULL) {
4571 wl_client_post_no_memory(client);
4572 return;
4573 }
4574 wl_resource_set_implementation(resource, &subcompositor_interface,
4575 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004576}
4577
Bryce Harrington0795ece2016-08-30 12:04:26 -07004578/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004579 *
4580 * \param compositor The compositor instance
4581 * \param state The DPMS state the outputs will be set to
4582 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004583static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004584weston_compositor_dpms(struct weston_compositor *compositor,
4585 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004586{
4587 struct weston_output *output;
4588
Bryce Harrington08976ac2016-08-30 12:05:16 -07004589 wl_list_for_each(output, &compositor->output_list, link)
4590 if (output->set_dpms)
4591 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004592}
4593
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004594/** Restores the compositor to active status
4595 *
4596 * \param compositor The compositor instance
4597 *
4598 * If the compositor was in a sleeping mode, all outputs are powered
4599 * back on via DPMS. Otherwise if the compositor was inactive
4600 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4601 * signal will fire.
4602 *
4603 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004604 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004605 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004606WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004607weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004608{
Neil Roberts8b62e202013-09-30 13:14:47 +01004609 uint32_t old_state = compositor->state;
4610
4611 /* The state needs to be changed before emitting the wake
4612 * signal because that may try to schedule a repaint which
4613 * will not work if the compositor is still sleeping */
4614 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4615
4616 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004617 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004618 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004619 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004620 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004621 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004622 /* fall through */
4623 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004624 wl_event_source_timer_update(compositor->idle_source,
4625 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004626 }
4627}
4628
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004629/** Turns off rendering and frame events for the compositor.
4630 *
4631 * \param compositor The compositor instance
4632 *
4633 * This is used for example to prevent further rendering while the
4634 * compositor is shutting down.
4635 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004636 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004637 *
4638 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004639 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004640WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004641weston_compositor_offscreen(struct weston_compositor *compositor)
4642{
4643 switch (compositor->state) {
4644 case WESTON_COMPOSITOR_OFFSCREEN:
4645 return;
4646 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004647 default:
4648 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4649 wl_event_source_timer_update(compositor->idle_source, 0);
4650 }
4651}
4652
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004653/** Powers down all attached output devices
4654 *
4655 * \param compositor The compositor instance
4656 *
4657 * Causes rendering to the outputs to cease, and no frame events to be
4658 * sent. Only powers down the outputs if the compositor is not already
4659 * in sleep mode.
4660 *
4661 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004662 *
4663 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004664 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004665WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004666weston_compositor_sleep(struct weston_compositor *compositor)
4667{
4668 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4669 return;
4670
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004671 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004672 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4673 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4674}
4675
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004676/** Sets compositor to idle mode
4677 *
4678 * \param data The compositor instance
4679 *
4680 * This is called when the idle timer fires. Once the compositor is in
4681 * idle mode it requires a wake action (e.g. via
4682 * weston_compositor_wake()) to restore it. The compositor's
4683 * idle_signal will be triggered when the idle event occurs.
4684 *
4685 * Idleness can be inhibited by setting the compositor's idle_inhibit
4686 * property.
4687 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004688static int
4689idle_handler(void *data)
4690{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004691 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004692
4693 if (compositor->idle_inhibit)
4694 return 1;
4695
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004696 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004697 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004698
4699 return 1;
4700}
4701
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004702WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004703weston_plane_init(struct weston_plane *plane,
4704 struct weston_compositor *ec,
4705 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004706{
4707 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004708 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004709 plane->x = x;
4710 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004711 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004712
4713 /* Init the link so that the call to wl_list_remove() when releasing
4714 * the plane without ever stacking doesn't lead to a crash */
4715 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004716}
4717
4718WL_EXPORT void
4719weston_plane_release(struct weston_plane *plane)
4720{
Xiong Zhang97116532013-10-23 13:58:31 +08004721 struct weston_view *view;
4722
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004723 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004724 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004725
Xiong Zhang97116532013-10-23 13:58:31 +08004726 wl_list_for_each(view, &plane->compositor->view_list, link) {
4727 if (view->plane == plane)
4728 view->plane = NULL;
4729 }
4730
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004731 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004732}
4733
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004734/** weston_compositor_stack_plane
4735 * \ingroup compositor
4736 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004737WL_EXPORT void
4738weston_compositor_stack_plane(struct weston_compositor *ec,
4739 struct weston_plane *plane,
4740 struct weston_plane *above)
4741{
4742 if (above)
4743 wl_list_insert(above->link.prev, &plane->link);
4744 else
4745 wl_list_insert(&ec->plane_list, &plane->link);
4746}
4747
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004748static void
4749output_release(struct wl_client *client, struct wl_resource *resource)
4750{
4751 wl_resource_destroy(resource);
4752}
4753
4754static const struct wl_output_interface output_interface = {
4755 output_release,
4756};
4757
4758
Casey Dahlin9074db52012-04-19 22:50:09 -04004759static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004760{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004761 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004762}
4763
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004764static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004765bind_output(struct wl_client *client,
4766 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004767{
Pekka Paalanen05347622017-03-27 12:24:34 +03004768 struct weston_head *head = data;
4769 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004770 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004771 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004772
Jason Ekstranda85118c2013-06-27 20:17:02 -05004773 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004774 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004775 if (resource == NULL) {
4776 wl_client_post_no_memory(client);
4777 return;
4778 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004779
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004780 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004781 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004782 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004783
Pekka Paalanen05347622017-03-27 12:24:34 +03004784 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004785 wl_output_send_geometry(resource,
4786 output->x,
4787 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004788 head->mm_width,
4789 head->mm_height,
4790 head->subpixel,
4791 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004792 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004793 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004794 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004795 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004796
4797 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004798 wl_output_send_mode(resource,
4799 mode->flags,
4800 mode->width,
4801 mode->height,
4802 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004803 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004804
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004805 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004806 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004807}
4808
Pekka Paalanendcac3512017-12-08 14:13:34 +02004809static void
4810weston_head_add_global(struct weston_head *head)
4811{
4812 head->global = wl_global_create(head->compositor->wl_display,
4813 &wl_output_interface, 3,
4814 head, bind_output);
4815}
4816
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004817/** Remove the global wl_output protocol object
4818 *
4819 * \param head The head whose global to remove.
4820 *
4821 * Also orphans the wl_resources for this head (wl_output).
4822 */
4823static void
4824weston_head_remove_global(struct weston_head *head)
4825{
4826 struct wl_resource *resource, *tmp;
4827
4828 if (head->global)
4829 wl_global_destroy(head->global);
4830 head->global = NULL;
4831
4832 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4833 unbind_resource(resource);
4834 wl_resource_set_destructor(resource, NULL);
4835 wl_resource_set_user_data(resource, NULL);
4836 }
Roman Gilge97391c2019-03-29 13:01:06 +01004837
4838 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
4839 /* It's sufficient to unset the destructor, then the list elements
4840 * won't be accessed.
4841 */
4842 wl_resource_set_destructor(resource, NULL);
4843 }
4844 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004845}
4846
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004847/** Get the backing object of wl_output
4848 *
4849 * \param resource A wl_output protocol object.
4850 * \return The backing object (user data) of a wl_resource representing a
4851 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004852 *
4853 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004854 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004855WL_EXPORT struct weston_head *
4856weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004857{
4858 assert(wl_resource_instance_of(resource, &wl_output_interface,
4859 &output_interface));
4860
4861 return wl_resource_get_user_data(resource);
4862}
4863
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004864/** Initialize a pre-allocated weston_head
4865 *
4866 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004867 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004868 *
4869 * The head will be safe to attach, detach and release.
4870 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004871 * The name is used in logs, and can be used by compositors as a configuration
4872 * identifier.
4873 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004874 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004875 * \internal
4876 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004877WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004878weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004879{
4880 /* Add some (in)sane defaults which can be used
4881 * for checking if an output was properly configured
4882 */
4883 memset(head, 0, sizeof *head);
4884
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004885 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004886 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004887 wl_list_init(&head->output_link);
4888 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01004889 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004890 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05304891 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004892}
4893
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004894/** Send output heads changed signal
4895 *
4896 * \param output The output that changed.
4897 *
4898 * Notify that the enabled output gained and/or lost heads, or that the
4899 * associated heads may have changed their connection status. This does not
4900 * include cases where the output becomes enabled or disabled. The registered
4901 * callbacks are called after the change has successfully happened.
4902 *
4903 * If connection status change causes the compositor to attach or detach a head
4904 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03004905 *
4906 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004907 */
4908static void
4909weston_output_emit_heads_changed(struct weston_output *output)
4910{
4911 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4912 output);
4913}
4914
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004915/** Idle task for emitting heads_changed_signal */
4916static void
4917weston_compositor_call_heads_changed(void *data)
4918{
4919 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004920 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004921
4922 compositor->heads_changed_source = NULL;
4923
4924 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004925
4926 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4927 if (head->output && head->output->enabled)
4928 weston_output_emit_heads_changed(head->output);
4929 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004930}
4931
4932/** Schedule a call on idle to heads_changed callback
4933 *
4934 * \param compositor The Compositor.
4935 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004936 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004937 * \internal
4938 */
4939static void
4940weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4941{
4942 struct wl_event_loop *loop;
4943
4944 if (compositor->heads_changed_source)
4945 return;
4946
4947 loop = wl_display_get_event_loop(compositor->wl_display);
4948 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4949 weston_compositor_call_heads_changed, compositor);
4950}
4951
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004952/** Register a new head
4953 *
4954 * \param compositor The compositor.
4955 * \param head The head to register, must not be already registered.
4956 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004957 * This signals the core that a new head has become available, leading to
4958 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004959 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004960 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004961 * \internal
4962 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004963WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004964weston_compositor_add_head(struct weston_compositor *compositor,
4965 struct weston_head *head)
4966{
4967 assert(wl_list_empty(&head->compositor_link));
4968 assert(head->name);
4969
4970 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4971 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004972 weston_compositor_schedule_heads_changed(compositor);
4973}
4974
4975/** Adds a listener to be called when heads change
4976 *
4977 * \param compositor The compositor.
4978 * \param listener The listener to add.
4979 *
Marius Vlada2dace22019-06-12 16:05:44 +03004980 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004981 *
4982 * The listener function will be called after heads are added or their
4983 * connection status has changed. Several changes may be accumulated into a
4984 * single call. The user is expected to iterate over the existing heads and
4985 * check their statuses to find out what changed.
4986 *
4987 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4988 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004989 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004990 */
4991WL_EXPORT void
4992weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4993 struct wl_listener *listener)
4994{
4995 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004996}
4997
4998/** Iterate over available heads
4999 *
5000 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005001 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005002 * \return The next available head in the list.
5003 *
5004 * Returns all available heads, regardless of being connected or enabled.
5005 *
5006 * You can iterate over all heads as follows:
5007 * \code
5008 * struct weston_head *head = NULL;
5009 *
5010 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5011 * ...
5012 * }
5013 * \endcode
5014 *
5015 * If you cause \c iter to be removed from the list, you cannot use it to
5016 * continue iterating. Removing any other item is safe.
5017 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005018 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005019 */
5020WL_EXPORT struct weston_head *
5021weston_compositor_iterate_heads(struct weston_compositor *compositor,
5022 struct weston_head *iter)
5023{
5024 struct wl_list *list = &compositor->head_list;
5025 struct wl_list *node;
5026
5027 assert(compositor);
5028 assert(!iter || iter->compositor == compositor);
5029
5030 if (iter)
5031 node = iter->compositor_link.next;
5032 else
5033 node = list->next;
5034
5035 assert(node);
5036 assert(!iter || node != &iter->compositor_link);
5037
5038 if (node == list)
5039 return NULL;
5040
5041 return container_of(node, struct weston_head, compositor_link);
5042}
5043
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005044/** Iterate over attached heads
5045 *
5046 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005047 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005048 * \return The next attached head in the list.
5049 *
5050 * Returns all heads currently attached to the output.
5051 *
5052 * You can iterate over all heads as follows:
5053 * \code
5054 * struct weston_head *head = NULL;
5055 *
5056 * while ((head = weston_output_iterate_heads(output, head))) {
5057 * ...
5058 * }
5059 * \endcode
5060 *
5061 * If you cause \c iter to be removed from the list, you cannot use it to
5062 * continue iterating. Removing any other item is safe.
5063 *
Marius Vlad55d87362019-06-11 01:15:35 +03005064 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005065 */
5066WL_EXPORT struct weston_head *
5067weston_output_iterate_heads(struct weston_output *output,
5068 struct weston_head *iter)
5069{
5070 struct wl_list *list = &output->head_list;
5071 struct wl_list *node;
5072
5073 assert(output);
5074 assert(!iter || iter->output == output);
5075
5076 if (iter)
5077 node = iter->output_link.next;
5078 else
5079 node = list->next;
5080
5081 assert(node);
5082 assert(!iter || node != &iter->output_link);
5083
5084 if (node == list)
5085 return NULL;
5086
5087 return container_of(node, struct weston_head, output_link);
5088}
5089
Pekka Paalanendcac3512017-12-08 14:13:34 +02005090/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005091 *
5092 * \param output The output to attach to.
5093 * \param head The head that is not yet attached.
5094 * \return 0 on success, -1 on failure.
5095 *
5096 * Attaches the given head to the output. All heads of an output are clones
5097 * and share the resolution and timings.
5098 *
5099 * Cloning heads this way uses less resources than creating an output for
5100 * each head, but is not always possible due to environment, driver and hardware
5101 * limitations.
5102 *
5103 * On failure, the head remains unattached. Success of this function does not
5104 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005105 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005106 *
Marius Vlad55d87362019-06-11 01:15:35 +03005107 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005108 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005109WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005110weston_output_attach_head(struct weston_output *output,
5111 struct weston_head *head)
5112{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005113 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005114
5115 if (!wl_list_empty(&head->output_link))
5116 return -1;
5117
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005118 if (output->attach_head) {
5119 if (output->attach_head(output, head) < 0)
5120 return -1;
5121 } else if (!wl_list_empty(&output->head_list)) {
5122 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005123 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005124 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005125
5126 head->output = output;
5127 wl_list_insert(output->head_list.prev, &head->output_link);
5128
Pekka Paalanendcac3512017-12-08 14:13:34 +02005129 if (output->enabled) {
5130 weston_head_add_global(head);
5131
5132 head_names = weston_output_create_heads_string(output);
5133 weston_log("Output '%s' updated to have head(s) %s\n",
5134 output->name, head_names);
5135 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005136
5137 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005138 }
5139
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005140 return 0;
5141}
5142
5143/** Detach a head from its output
5144 *
5145 * \param head The head to detach.
5146 *
5147 * It is safe to detach a non-attached head.
5148 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005149 * If the head is attached to an enabled output and the output will be left
5150 * with no heads, the output will be disabled.
5151 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005152 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005153 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005154 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005155WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005156weston_head_detach(struct weston_head *head)
5157{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005158 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005159 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005160
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005161 wl_list_remove(&head->output_link);
5162 wl_list_init(&head->output_link);
5163 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005164
5165 if (!output)
5166 return;
5167
5168 if (output->detach_head)
5169 output->detach_head(output, head);
5170
5171 if (output->enabled) {
5172 weston_head_remove_global(head);
5173
Pekka Paalanena0106992017-12-08 16:11:17 +02005174 if (wl_list_empty(&output->head_list)) {
5175 weston_log("Output '%s' no heads left, disabling.\n",
5176 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005177 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005178 } else {
5179 head_names = weston_output_create_heads_string(output);
5180 weston_log("Output '%s' updated to have head(s) %s\n",
5181 output->name, head_names);
5182 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005183
5184 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005185 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005186 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005187}
5188
5189/** Destroy a head
5190 *
5191 * \param head The head to be released.
5192 *
5193 * Destroys the head. The caller is responsible for freeing the memory pointed
5194 * to by \c head.
5195 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005196 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005197 * \internal
5198 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005199WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005200weston_head_release(struct weston_head *head)
5201{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005202 wl_signal_emit(&head->destroy_signal, head);
5203
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005204 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005205
5206 free(head->make);
5207 free(head->model);
5208 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005209 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005210
5211 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005212}
5213
Pekka Paalanene19970f2017-08-28 14:11:02 +03005214static void
5215weston_head_set_device_changed(struct weston_head *head)
5216{
5217 head->device_changed = true;
5218
5219 if (head->compositor)
5220 weston_compositor_schedule_heads_changed(head->compositor);
5221}
5222
5223/** String equal comparison with NULLs being equal */
5224static bool
5225str_null_eq(const char *a, const char *b)
5226{
5227 if (!a && !b)
5228 return true;
5229
5230 if (!!a != !!b)
5231 return false;
5232
5233 return strcmp(a, b) == 0;
5234}
5235
Pekka Paalanen01f60212017-03-24 15:39:24 +02005236/** Store monitor make, model and serial number
5237 *
5238 * \param head The head to modify.
5239 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5240 * any string, or NULL for none.
5241 * \param model The monitor model or name, or a made-up string, or NULL for
5242 * none.
5243 * \param serialno The monitor serial number, a made-up string, or NULL for
5244 * none.
5245 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005246 * This may set the device_changed flag.
5247 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005248 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005249 * \internal
5250 */
5251WL_EXPORT void
5252weston_head_set_monitor_strings(struct weston_head *head,
5253 const char *make,
5254 const char *model,
5255 const char *serialno)
5256{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005257 if (str_null_eq(head->make, make) &&
5258 str_null_eq(head->model, model) &&
5259 str_null_eq(head->serial_number, serialno))
5260 return;
5261
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005262 free(head->make);
5263 free(head->model);
5264 free(head->serial_number);
5265
5266 head->make = make ? strdup(make) : NULL;
5267 head->model = model ? strdup(model) : NULL;
5268 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005269
5270 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005271}
5272
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005273/** Store display non-desktop status
5274 *
5275 * \param head The head to modify.
5276 * \param non_desktop Whether the head connects to a non-desktop display.
5277 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005278 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005279 * \internal
5280 */
5281WL_EXPORT void
5282weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5283{
5284 if (head->non_desktop == non_desktop)
5285 return;
5286
5287 head->non_desktop = non_desktop;
5288
5289 weston_head_set_device_changed(head);
5290}
5291
Pekka Paalanen01f60212017-03-24 15:39:24 +02005292/** Store physical image size
5293 *
5294 * \param head The head to modify.
5295 * \param mm_width Image area width in millimeters.
5296 * \param mm_height Image area height in millimeters.
5297 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005298 * This may set the device_changed flag.
5299 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005300 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005301 * \internal
5302 */
5303WL_EXPORT void
5304weston_head_set_physical_size(struct weston_head *head,
5305 int32_t mm_width, int32_t mm_height)
5306{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005307 if (head->mm_width == mm_width &&
5308 head->mm_height == mm_height)
5309 return;
5310
Pekka Paalanen01f60212017-03-24 15:39:24 +02005311 head->mm_width = mm_width;
5312 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005313
5314 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005315}
5316
5317/** Store monitor sub-pixel layout
5318 *
5319 * \param head The head to modify.
5320 * \param sp Sub-pixel layout. The possible values are:
5321 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5322 * - WL_OUTPUT_SUBPIXEL_NONE,
5323 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5324 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5325 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5326 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5327 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005328 * This may set the device_changed flag.
5329 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005330 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005331 * \internal
5332 */
5333WL_EXPORT void
5334weston_head_set_subpixel(struct weston_head *head,
5335 enum wl_output_subpixel sp)
5336{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005337 if (head->subpixel == sp)
5338 return;
5339
Pekka Paalanen01f60212017-03-24 15:39:24 +02005340 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005341
5342 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005343}
5344
5345/** Mark the monitor as internal
5346 *
5347 * This is used for embedded screens, like laptop panels.
5348 *
5349 * \param head The head to mark as internal.
5350 *
5351 * By default a head is external. The type is often inferred from the physical
5352 * connector type.
5353 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005354 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005355 * \internal
5356 */
5357WL_EXPORT void
5358weston_head_set_internal(struct weston_head *head)
5359{
5360 head->connection_internal = true;
5361}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005362
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005363/** Store connector status
5364 *
5365 * \param head The head to modify.
5366 * \param connected Whether the head is connected.
5367 *
5368 * Connectors are created as disconnected. This function can be used to
5369 * set the connector status.
5370 *
5371 * The status should be set to true when a physical connector is connected to
5372 * a video sink device like a monitor and to false when the connector is
5373 * disconnected. For nested backends, the connection status should reflect the
5374 * connection to the parent display server.
5375 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005376 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005377 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005378 *
5379 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005380 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005381 * \internal
5382 */
5383WL_EXPORT void
5384weston_head_set_connection_status(struct weston_head *head, bool connected)
5385{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005386 if (head->connected == connected)
5387 return;
5388
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005389 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005390
Pekka Paalanene19970f2017-08-28 14:11:02 +03005391 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005392}
5393
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305394static void
5395weston_output_compute_protection(struct weston_output *output)
5396{
5397 struct weston_head *head;
5398 enum weston_hdcp_protection op_protection;
5399 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305400 struct weston_compositor *wc = output->compositor;
5401 struct content_protection *cp = wc->content_protection;
5402
5403 if (!cp)
5404 return;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305405
5406 wl_list_for_each(head, &output->head_list, output_link) {
5407 if (!op_protection_valid) {
5408 op_protection = head->current_protection;
5409 op_protection_valid = true;
5410 }
5411 if (head->current_protection < op_protection)
5412 op_protection = head->current_protection;
5413 }
5414
5415 if (!op_protection_valid)
5416 op_protection = WESTON_HDCP_DISABLE;
5417
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305418 if (output->current_protection != op_protection) {
5419 struct wl_event_loop *loop;
5420
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305421 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305422 weston_output_damage(output);
5423 if (cp->surface_protection_update)
5424 return;
5425 loop = wl_display_get_event_loop(wc->wl_display);
5426 cp->surface_protection_update = wl_event_loop_add_idle(loop,
5427 notify_surface_protection_change,
5428 wc);
5429 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305430}
5431
5432WL_EXPORT void
5433weston_head_set_content_protection_status(struct weston_head *head,
5434 enum weston_hdcp_protection status)
5435{
5436 head->current_protection = status;
5437 if (head->output)
5438 weston_output_compute_protection(head->output);
5439}
5440
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005441/** Is the head currently connected?
5442 *
5443 * \param head The head to query.
5444 * \return Connection status.
5445 *
5446 * Returns true if the head is physically connected to a monitor, or in
5447 * case of a nested backend returns true when there is a connection to the
5448 * parent display server.
5449 *
5450 * This is independent from the head being enabled.
5451 *
5452 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005453 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005454 */
5455WL_EXPORT bool
5456weston_head_is_connected(struct weston_head *head)
5457{
5458 return head->connected;
5459}
5460
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005461/** Is the head currently enabled?
5462 *
5463 * \param head The head to query.
5464 * \return Video status.
5465 *
5466 * Returns true if the head is currently transmitting a video stream.
5467 *
5468 * This is independent of the head being connected.
5469 *
5470 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005471 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005472 */
5473WL_EXPORT bool
5474weston_head_is_enabled(struct weston_head *head)
5475{
5476 if (!head->output)
5477 return false;
5478
5479 return head->output->enabled;
5480}
5481
Pekka Paalanene19970f2017-08-28 14:11:02 +03005482/** Has the device information changed?
5483 *
5484 * \param head The head to query.
5485 * \return True if the device information has changed since last reset.
5486 *
5487 * The information about the connected display device, e.g. a monitor, may
5488 * change without being disconnected in between. Changing information
5489 * causes a call to the heads_changed hook.
5490 *
5491 * The information includes make, model, serial number, physical size,
5492 * and sub-pixel type. The connection status is also included.
5493 *
5494 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005495 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005496 */
5497WL_EXPORT bool
5498weston_head_is_device_changed(struct weston_head *head)
5499{
5500 return head->device_changed;
5501}
5502
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005503/** Does the head represent a non-desktop display?
5504 *
5505 * \param head The head to query.
5506 * \return True if the device is a non-desktop display.
5507 *
5508 * Non-desktop heads are not attached to outputs by default.
5509 * This stops weston from extending the desktop onto head mounted displays.
5510 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005511 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005512 */
5513WL_EXPORT bool
5514weston_head_is_non_desktop(struct weston_head *head)
5515{
5516 return head->non_desktop;
5517}
5518
Pekka Paalanene19970f2017-08-28 14:11:02 +03005519/** Acknowledge device information change
5520 *
5521 * \param head The head to acknowledge.
5522 *
5523 * Clears the device changed flag on this head. When a compositor has processed
5524 * device information, it should call this to be able to notice further
5525 * changes.
5526 *
5527 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005528 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005529 */
5530WL_EXPORT void
5531weston_head_reset_device_changed(struct weston_head *head)
5532{
5533 head->device_changed = false;
5534}
5535
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005536/** Get the name of a head
5537 *
5538 * \param head The head to query.
5539 * \return The head's name, not NULL.
5540 *
5541 * The name depends on the backend. The DRM backend uses connector names,
5542 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005543 *
5544 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005545 */
5546WL_EXPORT const char *
5547weston_head_get_name(struct weston_head *head)
5548{
5549 return head->name;
5550}
5551
5552/** Get the output the head is attached to
5553 *
5554 * \param head The head to query.
5555 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005556 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005557 */
5558WL_EXPORT struct weston_output *
5559weston_head_get_output(struct weston_head *head)
5560{
5561 return head->output;
5562}
5563
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005564/** Add destroy callback for a head
5565 *
5566 * \param head The head to watch for.
5567 * \param listener The listener to add. The \c notify member must be set.
5568 *
5569 * Heads may get destroyed for various reasons by the backends. If a head is
5570 * attached to an output, the compositor should listen for head destruction
5571 * and reconfigure or destroy the output if necessary.
5572 *
5573 * The destroy callbacks will be called on weston_head destruction before any
5574 * automatic detaching from an associated weston_output and before any
5575 * weston_head information is lost.
5576 *
5577 * The \c data argument to the notify callback is the weston_head being
5578 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005579 *
5580 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005581 */
5582WL_EXPORT void
5583weston_head_add_destroy_listener(struct weston_head *head,
5584 struct wl_listener *listener)
5585{
5586 wl_signal_add(&head->destroy_signal, listener);
5587}
5588
5589/** Look up destroy listener for a head
5590 *
5591 * \param head The head to query.
5592 * \param notify The notify function used used for the added destroy listener.
5593 * \return The listener, or NULL if not found.
5594 *
5595 * This looks up the previously added destroy listener struct based on the
5596 * notify function it has. The listener can be used to access user data
5597 * through \c container_of().
5598 *
5599 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005600 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005601 */
5602WL_EXPORT struct wl_listener *
5603weston_head_get_destroy_listener(struct weston_head *head,
5604 wl_notify_func_t notify)
5605{
5606 return wl_signal_get(&head->destroy_signal, notify);
5607}
5608
David Fort0de859e2016-05-27 23:22:57 +02005609/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005610static void
David Fort0de859e2016-05-27 23:22:57 +02005611weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5612 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005613{
5614 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005615 bool start_resizing = false;
5616
5617 if (!delta_width)
5618 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005619
5620 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005621 if (output == resized_output) {
5622 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005623 continue;
5624 }
5625
David Fort0de859e2016-05-27 23:22:57 +02005626 if (start_resizing) {
5627 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005628 output->dirty = 1;
5629 }
5630 }
5631}
5632
Pekka Paalanend72bad22017-03-29 17:01:41 +03005633static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005634weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005635{
Scott Moreau850ca422012-05-21 15:21:25 -06005636 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005637
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005638 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005639 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005640
Scott Moreauccbf29d2012-02-22 14:21:41 -07005641 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005642 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005643 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005644 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005645 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005646 weston_matrix_scale(&output->matrix, magnification,
5647 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005648 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005649
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005650 switch (output->transform) {
5651 case WL_OUTPUT_TRANSFORM_FLIPPED:
5652 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5653 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5654 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5655 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5656 weston_matrix_scale(&output->matrix, -1, 1, 1);
5657 break;
5658 }
5659
5660 switch (output->transform) {
5661 default:
5662 case WL_OUTPUT_TRANSFORM_NORMAL:
5663 case WL_OUTPUT_TRANSFORM_FLIPPED:
5664 break;
5665 case WL_OUTPUT_TRANSFORM_90:
5666 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5667 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5668 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5669 break;
5670 case WL_OUTPUT_TRANSFORM_180:
5671 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5672 weston_matrix_translate(&output->matrix,
5673 -output->width, -output->height, 0);
5674 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5675 break;
5676 case WL_OUTPUT_TRANSFORM_270:
5677 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5678 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5679 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5680 break;
5681 }
5682
5683 if (output->current_scale != 1)
5684 weston_matrix_scale(&output->matrix,
5685 output->current_scale,
5686 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005687
Scott Moreauccbf29d2012-02-22 14:21:41 -07005688 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005689
5690 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005691}
5692
Scott Moreau1bad5db2012-08-18 01:04:05 -06005693static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005694weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005695{
5696 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005697 output->native_scale = scale;
5698 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005699
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005700 convert_size_by_transform_scale(&output->width, &output->height,
5701 output->current_mode->width,
5702 output->current_mode->height,
5703 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005704}
5705
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005706static void
5707weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005708{
5709 output->x = x;
5710 output->y = y;
5711
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005712 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005713 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005714
5715 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005716 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005717 output->width,
5718 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005719}
5720
Marius Vlad55d87362019-06-11 01:15:35 +03005721/**
5722 * \ingroup output
5723 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005724WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005725weston_output_move(struct weston_output *output, int x, int y)
5726{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005727 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005728 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005729 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005730
5731 output->move_x = x - output->x;
5732 output->move_y = y - output->y;
5733
5734 if (output->move_x == 0 && output->move_y == 0)
5735 return;
5736
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005737 weston_output_init_geometry(output, x, y);
5738
5739 output->dirty = 1;
5740
5741 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005742 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005743
5744 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005745 wl_list_for_each(head, &output->head_list, output_link) {
5746 wl_resource_for_each(resource, &head->resource_list) {
5747 wl_output_send_geometry(resource,
5748 output->x,
5749 output->y,
5750 head->mm_width,
5751 head->mm_height,
5752 head->subpixel,
5753 head->make,
5754 head->model,
5755 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005756
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005757 ver = wl_resource_get_version(resource);
5758 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5759 wl_output_send_done(resource);
5760 }
Roman Gilge97391c2019-03-29 13:01:06 +01005761
5762 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5763 zxdg_output_v1_send_logical_position(resource,
5764 output->x,
5765 output->y);
5766 zxdg_output_v1_send_done(resource);
5767 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005768 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005769}
5770
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005771/** Signal that a pending output is taken into use.
5772 *
5773 * Removes the output from the pending list and adds it to the compositor's
5774 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005775 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005776 * The output gets an internal ID assigned, and the wl_output global is
5777 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005778 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005779 * \param compositor The compositor instance.
5780 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005781 *
5782 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005783 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005784 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005785static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005786weston_compositor_add_output(struct weston_compositor *compositor,
5787 struct weston_output *output)
5788{
Armin Krezoviće5403842016-08-05 15:28:29 +02005789 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005790 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005791
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005792 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005793
5794 /* Verify we haven't reached the limit of 32 available output IDs */
5795 assert(ffs(~compositor->output_id_pool) > 0);
5796
5797 /* Invert the output id pool and look for the lowest numbered
5798 * switch (the least significant bit). Take that bit's position
5799 * as our ID, and mark it used in the compositor's output_id_pool.
5800 */
5801 output->id = ffs(~compositor->output_id_pool) - 1;
5802 compositor->output_id_pool |= 1u << output->id;
5803
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005804 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005805 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005806 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005807
Pekka Paalanendcac3512017-12-08 14:13:34 +02005808 wl_list_for_each(head, &output->head_list, output_link)
5809 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005810
Giulio Camuffob1147152015-05-06 21:41:57 +03005811 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005812
5813 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5814 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005815}
5816
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005817/** Transform device coordinates into global coordinates
5818 *
Marius Vlada2dace22019-06-12 16:05:44 +03005819 * \param output the weston_output object
5820 * \param[in] device_x X coordinate in device units.
5821 * \param[in] device_y Y coordinate in device units.
5822 * \param[out] x X coordinate in the global space.
5823 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005824 *
Marius Vlada2dace22019-06-12 16:05:44 +03005825 * Transforms coordinates from the device coordinate space (physical pixel
5826 * units) to the global coordinate space (logical pixel units). This takes
5827 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005828 *
Marius Vlad55d87362019-06-11 01:15:35 +03005829 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005830 * \internal
5831 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005832WL_EXPORT void
5833weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005834 double device_x, double device_y,
5835 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005836{
Derek Foreman0f679412014-10-02 13:41:17 -05005837 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005838 device_x,
5839 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005840 0.0,
5841 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005842
Derek Foreman67a18b92015-03-24 11:36:14 -05005843 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005844
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005845 *x = p.f[0] / p.f[3];
5846 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005847}
5848
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005849/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005850 *
5851 * \param output The weston_output object that is being removed.
5852 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005853 * The following happens:
5854 *
5855 * - The output assignments of all views in the current scenegraph are
5856 * recomputed.
5857 *
5858 * - Presentation feedback is discarded.
5859 *
5860 * - Compositor is notified that outputs were changed and
5861 * applies the necessary changes to re-layout outputs.
5862 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005863 * - The output is put back in the pending outputs list.
5864 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005865 * - Signal is emitted to notify all users of the weston_output
5866 * object that the output is being destroyed.
5867 *
5868 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005869 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005870 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005871 * - The output's internal ID is released.
5872 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005873 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005874 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005875 */
5876static void
5877weston_compositor_remove_output(struct weston_output *output)
5878{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005879 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005880 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005881 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005882
5883 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005884 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005885
Pekka Paalanenbccda712017-03-29 16:16:04 +03005886 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005887 if (view->output_mask & (1u << output->id))
5888 weston_view_assign_output(view);
5889 }
5890
5891 weston_presentation_feedback_discard_list(&output->feedback_list);
5892
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005893 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005894
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005895 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005896 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005897 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005898
Pekka Paalanenbccda712017-03-29 16:16:04 +03005899 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005900 wl_signal_emit(&output->destroy_signal, output);
5901
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005902 wl_list_for_each(head, &output->head_list, output_link)
5903 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005904
5905 compositor->output_id_pool &= ~(1u << output->id);
5906 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005907}
5908
5909/** Sets the output scale for a given output.
5910 *
5911 * \param output The weston_output object that the scale is set for.
5912 * \param scale Scale factor for the given output.
5913 *
5914 * It only supports setting scale for an output that
5915 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005916 *
Marius Vlad55d87362019-06-11 01:15:35 +03005917 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005918 */
5919WL_EXPORT void
5920weston_output_set_scale(struct weston_output *output,
5921 int32_t scale)
5922{
5923 /* We can only set scale on a disabled output */
5924 assert(!output->enabled);
5925
5926 /* We only want to set scale once */
5927 assert(!output->scale);
5928
5929 output->scale = scale;
5930}
5931
5932/** Sets the output transform for a given output.
5933 *
5934 * \param output The weston_output object that the transform is set for.
5935 * \param transform Transform value for the given output.
5936 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005937 * Refer to wl_output::transform section located at
5938 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5939 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005940 *
Marius Vlad55d87362019-06-11 01:15:35 +03005941 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005942 */
5943WL_EXPORT void
5944weston_output_set_transform(struct weston_output *output,
5945 uint32_t transform)
5946{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005947 struct weston_pointer_motion_event ev;
5948 struct wl_resource *resource;
5949 struct weston_seat *seat;
5950 pixman_region32_t old_region;
5951 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005952 struct weston_head *head;
5953 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005954
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005955 if (!output->enabled && output->transform == UINT32_MAX) {
5956 output->transform = transform;
5957 return;
5958 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005959
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005960 weston_output_transform_scale_init(output, transform, output->scale);
5961
5962 pixman_region32_init(&old_region);
5963 pixman_region32_copy(&old_region, &output->region);
5964
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005965 weston_output_init_geometry(output, output->x, output->y);
5966
5967 output->dirty = 1;
5968
5969 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005970 wl_list_for_each(head, &output->head_list, output_link) {
5971 wl_resource_for_each(resource, &head->resource_list) {
5972 wl_output_send_geometry(resource,
5973 output->x,
5974 output->y,
5975 head->mm_width,
5976 head->mm_height,
5977 head->subpixel,
5978 head->make,
5979 head->model,
5980 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005981
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005982 ver = wl_resource_get_version(resource);
5983 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5984 wl_output_send_done(resource);
5985 }
Roman Gilge97391c2019-03-29 13:01:06 +01005986 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5987 zxdg_output_v1_send_logical_position(resource,
5988 output->x,
5989 output->y);
5990 zxdg_output_v1_send_logical_size(resource,
5991 output->width,
5992 output->height);
5993 zxdg_output_v1_send_done(resource);
5994 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005995 }
5996
5997 /* we must ensure that pointers are inside output, otherwise they disappear */
5998 mid_x = output->x + output->width / 2;
5999 mid_y = output->y + output->height / 2;
6000
6001 ev.mask = WESTON_POINTER_MOTION_ABS;
6002 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6003 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6004
6005 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6006 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6007
6008 if (pointer && pixman_region32_contains_point(&old_region,
6009 wl_fixed_to_int(pointer->x),
6010 wl_fixed_to_int(pointer->y),
6011 NULL))
6012 weston_pointer_move(pointer, &ev);
6013 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006014}
6015
6016/** Initializes a weston_output object with enough data so
6017 ** an output can be configured.
6018 *
6019 * \param output The weston_output object to initialize
6020 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006021 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006022 *
6023 * Sets initial values for fields that are expected to be
6024 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006025 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006026 * The name is used in logs, and can be used by compositors as a configuration
6027 * identifier.
6028 *
Marius Vlad55d87362019-06-11 01:15:35 +03006029 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006030 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006031 */
6032WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006033weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006034 struct weston_compositor *compositor,
6035 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006036{
6037 output->compositor = compositor;
6038 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006039 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006040 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006041 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006042 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306043 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306044 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006045
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006046 wl_list_init(&output->head_list);
6047
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006048 /* Add some (in)sane defaults which can be used
6049 * for checking if an output was properly configured
6050 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006051 output->scale = 0;
6052 /* Can't use -1 on uint32_t and 0 is valid enum value */
6053 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006054
6055 pixman_region32_init(&output->previous_damage);
6056 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006057 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006058}
6059
6060/** Adds weston_output object to pending output list.
6061 *
6062 * \param output The weston_output object to add
6063 * \param compositor The compositor instance.
6064 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006065 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006066 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006067 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006068 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006069 */
6070WL_EXPORT void
6071weston_compositor_add_pending_output(struct weston_output *output,
6072 struct weston_compositor *compositor)
6073{
Pekka Paalanene952a012017-03-29 17:14:00 +03006074 assert(output->disable);
6075 assert(output->enable);
6076
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006077 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006078 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006079}
6080
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006081/** Create a string with the attached heads' names.
6082 *
6083 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006084 *
6085 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006086 */
6087static char *
6088weston_output_create_heads_string(struct weston_output *output)
6089{
6090 FILE *fp;
6091 char *str = NULL;
6092 size_t size = 0;
6093 struct weston_head *head;
6094 const char *sep = "";
6095
6096 fp = open_memstream(&str, &size);
6097 if (!fp)
6098 return NULL;
6099
6100 wl_list_for_each(head, &output->head_list, output_link) {
6101 fprintf(fp, "%s%s", sep, head->name);
6102 sep = ", ";
6103 }
6104 fclose(fp);
6105
6106 return str;
6107}
6108
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006109/** Constructs a weston_output object that can be used by the compositor.
6110 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006111 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006112 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006113 *
6114 * Output coordinates are calculated and each new output is by default
6115 * assigned to the right of previous one.
6116 *
6117 * Sets up the transformation, zoom, and geometry of the output using
6118 * the properties that need to be configured by the compositor.
6119 *
6120 * Establishes a repaint timer for the output with the relevant display
6121 * object's event loop. See output_repaint_timer_handler().
6122 *
6123 * The output is assigned an ID. Weston can support up to 32 distinct
6124 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6125 * is referred to and used to find the first available ID number, and
6126 * then this ID is marked as used in output_id_pool.
6127 *
6128 * The output is also assigned a Wayland global with the wl_output
6129 * external interface.
6130 *
6131 * Backend specific function is called to set up the output output.
6132 *
6133 * Output is added to the compositor's output list
6134 *
6135 * If the backend specific function fails, the weston_output object
6136 * is returned to a state it was before calling this function and
6137 * is added to the compositor's pending_output_list in case it needs
6138 * to be reconfigured or just so it can be destroyed at shutdown.
6139 *
6140 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006141 *
6142 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006143 */
6144WL_EXPORT int
6145weston_output_enable(struct weston_output *output)
6146{
Armin Krezović782f5df2016-09-30 14:11:11 +02006147 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006148 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006149 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006150 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006151 int x = 0, y = 0;
6152
Pekka Paalanencc201e42017-03-30 15:11:25 +03006153 if (output->enabled) {
6154 weston_log("Error: attempt to enable an enabled output '%s'\n",
6155 output->name);
6156 return -1;
6157 }
6158
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006159 if (wl_list_empty(&output->head_list)) {
6160 weston_log("Error: cannot enable output '%s' without heads.\n",
6161 output->name);
6162 return -1;
6163 }
6164
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006165 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6166 weston_log("Error: no video mode for output '%s'.\n",
6167 output->name);
6168 return -1;
6169 }
6170
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006171 wl_list_for_each(head, &output->head_list, output_link) {
6172 assert(head->make);
6173 assert(head->model);
6174 }
6175
Armin Krezović782f5df2016-09-30 14:11:11 +02006176 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006177 struct weston_output, link);
6178
Armin Krezović782f5df2016-09-30 14:11:11 +02006179 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006180 x = iterator->x + iterator->width;
6181
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006182 /* Make sure the scale is set up */
6183 assert(output->scale);
6184
6185 /* Make sure we have a transform set */
6186 assert(output->transform != UINT32_MAX);
6187
Armin Krezović782f5df2016-09-30 14:11:11 +02006188 output->x = x;
6189 output->y = y;
6190 output->dirty = 1;
6191 output->original_scale = output->scale;
6192
6193 weston_output_transform_scale_init(output, output->transform, output->scale);
6194 weston_output_init_zoom(output);
6195
6196 weston_output_init_geometry(output, x, y);
6197 weston_output_damage(output);
6198
6199 wl_signal_init(&output->frame_signal);
6200 wl_signal_init(&output->destroy_signal);
6201 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006202 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006203
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006204 /* Enable the output (set up the crtc or create a
6205 * window representing the output, set up the
6206 * renderer, etc)
6207 */
6208 if (output->enable(output) < 0) {
6209 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006210 return -1;
6211 }
6212
6213 weston_compositor_add_output(output->compositor, output);
6214
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006215 head_names = weston_output_create_heads_string(output);
6216 weston_log("Output '%s' enabled with head(s) %s\n",
6217 output->name, head_names);
6218 free(head_names);
6219
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006220 return 0;
6221}
6222
6223/** Converts a weston_output object to a pending output state, so it
6224 ** can be configured again or destroyed.
6225 *
6226 * \param output The weston_output object that needs to be disabled.
6227 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006228 * Calls a backend specific function to disable an output, in case
6229 * such function exists.
6230 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006231 * The backend specific disable function may choose to postpone the disabling
6232 * by returning a negative value, in which case this function returns early.
6233 * In that case the backend will guarantee the output will be disabled soon
6234 * by the backend calling this function again. One must not attempt to re-enable
6235 * the output until that happens.
6236 *
6237 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006238 * from weston's output_list (see weston_compositor_remove_output())
6239 * and is returned to a state it was before weston_output_enable()
6240 * was ran (see weston_output_enable_undo()).
6241 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006242 * See weston_output_init() for more information on the
6243 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006244 *
6245 * If the output has never been enabled yet, this function can still be
6246 * called to ensure that the output is actually turned off rather than left
6247 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006248 *
6249 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006250 */
6251WL_EXPORT void
6252weston_output_disable(struct weston_output *output)
6253{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006254 /* Should we rename this? */
6255 output->destroying = 1;
6256
Pekka Paalanenc65df642017-03-29 15:45:46 +03006257 /* Disable is called unconditionally also for not-enabled outputs,
6258 * because at compositor start-up, if there is an output that is
6259 * already on but the compositor wants to turn it off, we have to
6260 * forward the turn-off to the backend so it knows to do it.
6261 * The backend cannot initially turn off everything, because it
6262 * would cause unnecessary mode-sets for all outputs the compositor
6263 * wants to be on.
6264 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006265 if (output->disable(output) < 0)
6266 return;
6267
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006268 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006269 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006270
6271 output->destroying = 0;
6272}
6273
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006274/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006275 *
6276 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006277 *
6278 * If there are new or changed heads, calls the heads_changed hook and
6279 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006280 *
6281 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006282 */
6283WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006284weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006285{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006286 if (compositor->heads_changed_source) {
6287 wl_event_source_remove(compositor->heads_changed_source);
6288 weston_compositor_call_heads_changed(compositor);
6289 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006290}
6291
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006292/** Add destroy callback for an output
6293 *
6294 * \param output The output to watch.
6295 * \param listener The listener to add. The \c notify member must be set.
6296 *
6297 * The listener callback will be called when user destroys an output. This
6298 * may be delayed by a backend in some cases. The main purpose of the
6299 * listener is to allow hooking up custom data to the output. The custom data
6300 * can be fetched via weston_output_get_destroy_listener() followed by
6301 * container_of().
6302 *
6303 * The \c data argument to the notify callback is the weston_output being
6304 * destroyed.
6305 *
6306 * @note This is for the final destruction of an output, not when it gets
6307 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006308 *
6309 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006310 */
6311WL_EXPORT void
6312weston_output_add_destroy_listener(struct weston_output *output,
6313 struct wl_listener *listener)
6314{
6315 wl_signal_add(&output->user_destroy_signal, listener);
6316}
6317
6318/** Look up destroy listener for an output
6319 *
6320 * \param output The output to query.
6321 * \param notify The notify function used used for the added destroy listener.
6322 * \return The listener, or NULL if not found.
6323 *
6324 * This looks up the previously added destroy listener struct based on the
6325 * notify function it has. The listener can be used to access user data
6326 * through \c container_of().
6327 *
6328 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006329 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006330 */
6331WL_EXPORT struct wl_listener *
6332weston_output_get_destroy_listener(struct weston_output *output,
6333 wl_notify_func_t notify)
6334{
6335 return wl_signal_get(&output->user_destroy_signal, notify);
6336}
6337
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006338/** Uninitialize an output
6339 *
6340 * Removes the output from the list of enabled outputs if necessary, but
6341 * does not call the backend's output disable function. The output will no
6342 * longer be in the list of pending outputs either.
6343 *
6344 * All fields of weston_output become uninitialized, i.e. should not be used
6345 * anymore. The caller can free the memory after this.
6346 *
Marius Vlad55d87362019-06-11 01:15:35 +03006347 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006348 * \internal
6349 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006350WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006351weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006352{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006353 struct weston_head *head, *tmp;
6354
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006355 output->destroying = 1;
6356
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006357 wl_signal_emit(&output->user_destroy_signal, output);
6358
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006359 if (output->idle_repaint_source)
6360 wl_event_source_remove(output->idle_repaint_source);
6361
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006362 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006363 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006364
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006365 pixman_region32_fini(&output->region);
6366 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006367 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006368
6369 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6370 weston_head_detach(head);
6371
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006372 free(output->name);
6373}
6374
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006375/** Find an output by its given name
6376 *
6377 * \param compositor The compositor to search in.
6378 * \param name The output name to search for.
6379 * \return An existing output with the given name, or NULL if not found.
6380 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006381 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006382 */
6383WL_EXPORT struct weston_output *
6384weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6385 const char *name)
6386{
6387 struct weston_output *output;
6388
6389 wl_list_for_each(output, &compositor->output_list, link)
6390 if (strcmp(output->name, name) == 0)
6391 return output;
6392
6393 wl_list_for_each(output, &compositor->pending_output_list, link)
6394 if (strcmp(output->name, name) == 0)
6395 return output;
6396
6397 return NULL;
6398}
6399
6400/** Create a named output
6401 *
6402 * \param compositor The compositor.
6403 * \param name The name for the output.
6404 * \return A new \c weston_output, or NULL on failure.
6405 *
6406 * This creates a new weston_output that starts with no heads attached.
6407 *
6408 * An output must be configured and it must have at least one head before
6409 * it can be enabled.
6410 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006411 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006412 */
6413WL_EXPORT struct weston_output *
6414weston_compositor_create_output(struct weston_compositor *compositor,
6415 const char *name)
6416{
6417 assert(compositor->backend->create_output);
6418
6419 if (weston_compositor_find_output_by_name(compositor, name)) {
6420 weston_log("Warning: attempted to create an output with a "
6421 "duplicate name '%s'.\n", name);
6422 return NULL;
6423 }
6424
6425 return compositor->backend->create_output(compositor, name);
6426}
6427
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006428/** Create an output for an unused head
6429 *
6430 * \param compositor The compositor.
6431 * \param head The head to attach to the output.
6432 * \return A new \c weston_output, or NULL on failure.
6433 *
6434 * This creates a new weston_output that starts with the given head attached.
6435 * The output inherits the name of the head. The head must not be already
6436 * attached to another output.
6437 *
6438 * An output must be configured before it can be enabled.
6439 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006440 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006441 */
6442WL_EXPORT struct weston_output *
6443weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6444 struct weston_head *head)
6445{
6446 struct weston_output *output;
6447
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006448 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006449 if (!output)
6450 return NULL;
6451
6452 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006453 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006454 return NULL;
6455 }
6456
6457 return output;
6458}
6459
6460/** Destroy an output
6461 *
6462 * \param output The output to destroy.
6463 *
6464 * The heads attached to the given output are detached and become unused again.
6465 *
6466 * It is not necessary to explicitly destroy all outputs at compositor exit.
6467 * weston_compositor_destroy() will automatically destroy any remaining
6468 * outputs.
6469 *
Marius Vlad55d87362019-06-11 01:15:35 +03006470 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006471 */
6472WL_EXPORT void
6473weston_output_destroy(struct weston_output *output)
6474{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006475 output->destroy(output);
6476}
6477
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006478/** When you need a head...
6479 *
6480 * This function is a hack, used until all code has been converted to become
6481 * multi-head aware.
6482 *
6483 * \param output The weston_output whose head to get.
6484 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006485 *
6486 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006487 */
6488WL_EXPORT struct weston_head *
6489weston_output_get_first_head(struct weston_output *output)
6490{
6491 if (wl_list_empty(&output->head_list))
6492 return NULL;
6493
6494 return container_of(output->head_list.next,
6495 struct weston_head, output_link);
6496}
6497
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306498/** Allow/Disallow content-protection support for an output
6499 *
6500 * This function sets the allow_protection member for an output. Setting of
6501 * this field will allow the compositor to attempt content-protection for this
6502 * output, for a backend that supports the content-protection protocol.
6503 *
6504 * \param output The weston_output for whom the content-protection is to be
6505 * allowed.
6506 * \param allow_protection The bool value which is to be set.
6507 */
6508WL_EXPORT void
6509weston_output_allow_protection(struct weston_output *output,
6510 bool allow_protection)
6511{
6512 output->allow_protection = allow_protection;
6513}
6514
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006515static void
Roman Gilge97391c2019-03-29 13:01:06 +01006516xdg_output_unlist(struct wl_resource *resource)
6517{
6518 wl_list_remove(wl_resource_get_link(resource));
6519}
6520
6521static void
6522xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6523{
6524 wl_resource_destroy(resource);
6525}
6526
6527static const struct zxdg_output_v1_interface xdg_output_interface = {
6528 xdg_output_destroy
6529};
6530
6531static void
6532xdg_output_manager_destroy(struct wl_client *client,
6533 struct wl_resource *resource)
6534{
6535 wl_resource_destroy(resource);
6536}
6537
6538static void
6539xdg_output_manager_get_xdg_output(struct wl_client *client,
6540 struct wl_resource *manager,
6541 uint32_t id,
6542 struct wl_resource *output_resource)
6543{
6544 int version = wl_resource_get_version(manager);
6545 struct weston_head *head = wl_resource_get_user_data(output_resource);
6546 struct weston_output *output = head->output;
6547 struct wl_resource *resource;
6548
6549 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6550 version, id);
6551 if (resource == NULL) {
6552 wl_client_post_no_memory(client);
6553 return;
6554 }
6555
6556 wl_list_insert(&head->xdg_output_resource_list,
6557 wl_resource_get_link(resource));
6558
6559 wl_resource_set_implementation(resource, &xdg_output_interface,
6560 NULL, xdg_output_unlist);
6561
6562 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6563 zxdg_output_v1_send_logical_size(resource,
6564 output->width,
6565 output->height);
6566 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6567 zxdg_output_v1_send_name(resource, head->name);
6568
6569 zxdg_output_v1_send_done(resource);
6570}
6571
6572static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6573 xdg_output_manager_destroy,
6574 xdg_output_manager_get_xdg_output
6575};
6576
6577static void
6578bind_xdg_output_manager(struct wl_client *client,
6579 void *data, uint32_t version, uint32_t id)
6580{
6581 struct wl_resource *resource;
6582
6583 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6584 version, id);
6585 if (resource == NULL) {
6586 wl_client_post_no_memory(client);
6587 return;
6588 }
6589
6590 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6591 NULL, NULL);
6592}
6593
6594static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006595destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006596{
Jonny Lamb74130762013-11-26 18:19:46 +01006597 struct weston_surface *surface =
6598 wl_resource_get_user_data(resource);
6599
Pekka Paalanen4826f872016-04-22 14:14:38 +03006600 if (!surface)
6601 return;
6602
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006603 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006604 surface->pending.buffer_viewport.buffer.src_width =
6605 wl_fixed_from_int(-1);
6606 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006607 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006608}
6609
6610static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006611viewport_destroy(struct wl_client *client,
6612 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006613{
6614 wl_resource_destroy(resource);
6615}
6616
6617static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006618viewport_set_source(struct wl_client *client,
6619 struct wl_resource *resource,
6620 wl_fixed_t src_x,
6621 wl_fixed_t src_y,
6622 wl_fixed_t src_width,
6623 wl_fixed_t src_height)
6624{
6625 struct weston_surface *surface =
6626 wl_resource_get_user_data(resource);
6627
Pekka Paalanen4826f872016-04-22 14:14:38 +03006628 if (!surface) {
6629 wl_resource_post_error(resource,
6630 WP_VIEWPORT_ERROR_NO_SURFACE,
6631 "wl_surface for this viewport is no longer exists");
6632 return;
6633 }
6634
6635 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006636 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006637
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006638 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006639 src_height == wl_fixed_from_int(-1) &&
6640 src_x == wl_fixed_from_int(-1) &&
6641 src_y == wl_fixed_from_int(-1)) {
6642 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006643 surface->pending.buffer_viewport.buffer.src_width =
6644 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006645 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006646 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006647 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006648
Pekka Paalanen201769a2016-04-26 14:42:11 +03006649 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006650 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006651 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006652 "wl_surface@%d viewport source "
6653 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6654 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006655 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006656 wl_fixed_to_double(src_height),
6657 wl_fixed_to_double(src_x),
6658 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006659 return;
6660 }
6661
6662 surface->pending.buffer_viewport.buffer.src_x = src_x;
6663 surface->pending.buffer_viewport.buffer.src_y = src_y;
6664 surface->pending.buffer_viewport.buffer.src_width = src_width;
6665 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006666 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006667}
6668
6669static void
6670viewport_set_destination(struct wl_client *client,
6671 struct wl_resource *resource,
6672 int32_t dst_width,
6673 int32_t dst_height)
6674{
6675 struct weston_surface *surface =
6676 wl_resource_get_user_data(resource);
6677
Pekka Paalanen4826f872016-04-22 14:14:38 +03006678 if (!surface) {
6679 wl_resource_post_error(resource,
6680 WP_VIEWPORT_ERROR_NO_SURFACE,
6681 "wl_surface for this viewport no longer exists");
6682 return;
6683 }
6684
6685 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006686
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006687 if (dst_width == -1 && dst_height == -1) {
6688 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006689 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006690 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006691 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006692 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006693
6694 if (dst_width <= 0 || dst_height <= 0) {
6695 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006696 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006697 "destination size must be positive (%dx%d)",
6698 dst_width, dst_height);
6699 return;
6700 }
6701
6702 surface->pending.buffer_viewport.surface.width = dst_width;
6703 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006704 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006705}
6706
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006707static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006708 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006709 viewport_set_source,
6710 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006711};
6712
6713static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006714viewporter_destroy(struct wl_client *client,
6715 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006716{
6717 wl_resource_destroy(resource);
6718}
6719
6720static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006721viewporter_get_viewport(struct wl_client *client,
6722 struct wl_resource *viewporter,
6723 uint32_t id,
6724 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006725{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006726 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006727 struct weston_surface *surface =
6728 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006729 struct wl_resource *resource;
6730
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006731 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006732 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006733 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006734 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006735 return;
6736 }
6737
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006738 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006739 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006740 if (resource == NULL) {
6741 wl_client_post_no_memory(client);
6742 return;
6743 }
6744
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006745 wl_resource_set_implementation(resource, &viewport_interface,
6746 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006747
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006748 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006749}
6750
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006751static const struct wp_viewporter_interface viewporter_interface = {
6752 viewporter_destroy,
6753 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006754};
6755
6756static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006757bind_viewporter(struct wl_client *client,
6758 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006759{
6760 struct wl_resource *resource;
6761
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006762 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006763 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006764 if (resource == NULL) {
6765 wl_client_post_no_memory(client);
6766 return;
6767 }
6768
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006769 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006770 NULL, NULL);
6771}
6772
6773static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006774destroy_presentation_feedback(struct wl_resource *feedback_resource)
6775{
6776 struct weston_presentation_feedback *feedback;
6777
6778 feedback = wl_resource_get_user_data(feedback_resource);
6779
6780 wl_list_remove(&feedback->link);
6781 free(feedback);
6782}
6783
6784static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006785presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6786{
6787 wl_resource_destroy(resource);
6788}
6789
6790static void
6791presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006792 struct wl_resource *presentation_resource,
6793 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006794 uint32_t callback)
6795{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006796 struct weston_surface *surface;
6797 struct weston_presentation_feedback *feedback;
6798
6799 surface = wl_resource_get_user_data(surface_resource);
6800
Bryce Harringtonde16d892014-11-20 22:21:57 -08006801 feedback = zalloc(sizeof *feedback);
6802 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006803 goto err_calloc;
6804
6805 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006806 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006807 1, callback);
6808 if (!feedback->resource)
6809 goto err_create;
6810
6811 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6812 destroy_presentation_feedback);
6813
6814 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6815
6816 return;
6817
6818err_create:
6819 free(feedback);
6820
6821err_calloc:
6822 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006823}
6824
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006825static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006826 presentation_destroy,
6827 presentation_feedback
6828};
6829
6830static void
6831bind_presentation(struct wl_client *client,
6832 void *data, uint32_t version, uint32_t id)
6833{
6834 struct weston_compositor *compositor = data;
6835 struct wl_resource *resource;
6836
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006837 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006838 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006839 if (resource == NULL) {
6840 wl_client_post_no_memory(client);
6841 return;
6842 }
6843
6844 wl_resource_set_implementation(resource, &presentation_implementation,
6845 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006846 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006847}
6848
6849static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006850compositor_bind(struct wl_client *client,
6851 void *data, uint32_t version, uint32_t id)
6852{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006853 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006854 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006855
Jason Ekstranda85118c2013-06-27 20:17:02 -05006856 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006857 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006858 if (resource == NULL) {
6859 wl_client_post_no_memory(client);
6860 return;
6861 }
6862
6863 wl_resource_set_implementation(resource, &compositor_interface,
6864 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006865}
6866
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006867WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006868weston_environment_get_fd(const char *env)
6869{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006870 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006871 int fd, flags;
6872
6873 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006874 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006875 return -1;
6876
6877 flags = fcntl(fd, F_GETFD);
6878 if (flags == -1)
6879 return -1;
6880
6881 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6882 unsetenv(env);
6883
6884 return fd;
6885}
6886
Pekka Paalanenb5026542014-11-12 15:09:24 +02006887static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006888timeline_key_binding_handler(struct weston_keyboard *keyboard,
6889 const struct timespec *time, uint32_t key,
6890 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006891{
6892 struct weston_compositor *compositor = data;
6893
6894 if (weston_timeline_enabled_)
6895 weston_timeline_close();
6896 else
6897 weston_timeline_open(compositor);
6898}
6899
Daniel Stonece62cb32018-07-20 09:46:24 +01006900static const char *
6901output_repaint_status_text(struct weston_output *output)
6902{
6903 switch (output->repaint_status) {
6904 case REPAINT_NOT_SCHEDULED:
6905 return "no repaint";
6906 case REPAINT_BEGIN_FROM_IDLE:
6907 return "start_repaint_loop scheduled";
6908 case REPAINT_SCHEDULED:
6909 return "repaint scheduled";
6910 case REPAINT_AWAITING_COMPLETION:
6911 return "awaiting completion";
6912 }
6913
6914 assert(!"output_repaint_status_text missing enum");
6915 return NULL;
6916}
6917
6918static void
6919debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6920{
6921 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6922 struct wl_shm_buffer *shm;
6923 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006924 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006925
6926 if (!buffer) {
6927 fprintf(fp, "\t\t[buffer not available]\n");
6928 return;
6929 }
6930
6931 shm = wl_shm_buffer_get(buffer->resource);
6932 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006933 uint32_t _format = wl_shm_buffer_get_format(shm);
6934 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006935 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006936 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6937 (unsigned long) _format,
6938 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006939 return;
6940 }
6941
6942 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6943 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006944 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006945 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006946 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6947 (unsigned long) dmabuf->attributes.format,
6948 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006949 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6950 (unsigned long long) dmabuf->attributes.modifier[0]);
6951 return;
6952 }
6953
Marius Vlad253ba9a2019-03-17 18:22:21 +02006954 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006955}
6956
6957static void
6958debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6959{
6960 struct weston_compositor *ec = view->surface->compositor;
6961 struct weston_output *output;
6962 char desc[512];
6963 pixman_box32_t *box;
6964 uint32_t surface_id = 0;
6965 pid_t pid = 0;
6966
6967 if (view->surface->resource) {
6968 struct wl_resource *resource = view->surface->resource;
6969 wl_client_get_credentials(wl_resource_get_client(resource),
6970 &pid, NULL, NULL);
6971 surface_id = wl_resource_get_id(view->surface->resource);
6972 }
6973
6974 if (!view->surface->get_label ||
6975 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6976 strcpy(desc, "[no description available]");
6977 }
6978 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6979 view_idx, view->surface->role_name, pid, surface_id,
6980 desc, view);
6981
6982 box = pixman_region32_extents(&view->transform.boundingbox);
6983 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6984 box->x1, box->y1, box->x2, box->y2);
6985 box = pixman_region32_extents(&view->transform.opaque);
6986
6987 if (pixman_region32_equal(&view->transform.opaque,
6988 &view->transform.boundingbox)) {
6989 fprintf(fp, "\t\t[fully opaque]\n");
6990 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6991 fprintf(fp, "\t\t[not opaque]\n");
6992 } else {
6993 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6994 box->x1, box->y1, box->x2, box->y2);
6995 }
6996
6997 if (view->alpha < 1.0)
6998 fprintf(fp, "\t\talpha: %f\n", view->alpha);
6999
7000 if (view->output_mask != 0) {
7001 bool first_output = true;
7002 fprintf(fp, "\t\toutputs: ");
7003 wl_list_for_each(output, &ec->output_list, link) {
7004 if (!(view->output_mask & (1 << output->id)))
7005 continue;
7006 fprintf(fp, "%s%d (%s)%s",
7007 (first_output) ? "" : ", ",
7008 output->id, output->name,
7009 (view->output == output) ? " (primary)" : "");
7010 first_output = false;
7011 }
7012 } else {
7013 fprintf(fp, "\t\t[no outputs]");
7014 }
7015
7016 fprintf(fp, "\n");
7017
7018 debug_scene_view_print_buffer(fp, view);
7019}
7020
Marius Vlad433f4e72019-02-17 22:14:23 +02007021static void
7022debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007023 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007024{
7025 struct weston_subsurface *sub;
7026 struct weston_view *ev;
7027
7028 /*
7029 * print the view first, then we recursively go on printing
7030 * sub-surfaces. We bail out once no more sub-surfaces are available.
7031 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007032 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007033
7034 /* no more sub-surfaces */
7035 if (wl_list_empty(&view->surface->subsurface_list))
7036 return;
7037
7038 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7039 wl_list_for_each(ev, &sub->surface->views, surface_link) {
7040 /* do not print again the parent view */
7041 if (view == ev)
7042 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007043
7044 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007045 debug_scene_view_print_tree(ev, fp, view_idx);
7046 }
7047 }
7048}
7049
Daniel Stonece62cb32018-07-20 09:46:24 +01007050/**
7051 * Output information on how libweston is currently composing the scene
7052 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007053 *
7054 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007055 */
7056WL_EXPORT char *
7057weston_compositor_print_scene_graph(struct weston_compositor *ec)
7058{
7059 struct weston_output *output;
7060 struct weston_layer *layer;
7061 struct timespec now;
7062 int layer_idx = 0;
7063 FILE *fp;
7064 char *ret;
7065 size_t len;
7066 int err;
7067
7068 fp = open_memstream(&ret, &len);
7069 assert(fp);
7070
7071 weston_compositor_read_presentation_clock(ec, &now);
7072 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7073 now.tv_sec, now.tv_nsec);
7074
7075 wl_list_for_each(output, &ec->output_list, link) {
7076 struct weston_head *head;
7077 int head_idx = 0;
7078
7079 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7080 assert(output->enabled);
7081
7082 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7083 output->x, output->y,
7084 output->x + output->width,
7085 output->y + output->height);
7086 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7087 output->current_mode->width,
7088 output->current_mode->height,
7089 output->current_mode->refresh / 1000.0);
7090 fprintf(fp, "\tscale: %d\n", output->scale);
7091
7092 fprintf(fp, "\trepaint status: %s\n",
7093 output_repaint_status_text(output));
7094 if (output->repaint_status == REPAINT_SCHEDULED)
7095 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7096 output->next_repaint.tv_sec,
7097 output->next_repaint.tv_nsec);
7098
7099 wl_list_for_each(head, &output->head_list, output_link) {
7100 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7101 head_idx++, head->name,
7102 (head->connected) ? "" : "not ");
7103 }
7104 }
7105
7106 fprintf(fp, "\n");
7107
7108 wl_list_for_each(layer, &ec->layer_list, link) {
7109 struct weston_view *view;
7110 int view_idx = 0;
7111
7112 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7113 (unsigned long) layer->position);
7114
7115 if (!weston_layer_mask_is_infinite(layer)) {
7116 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7117 layer->mask.x1, layer->mask.y1,
7118 layer->mask.x2, layer->mask.y2);
7119 }
7120
Marius Vlada6acfa82019-03-17 18:10:09 +02007121 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7122 debug_scene_view_print_tree(view, fp, &view_idx);
7123 view_idx++;
7124 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007125
7126 if (wl_list_empty(&layer->view_list.link))
7127 fprintf(fp, "\t[no views]\n");
7128
7129 fprintf(fp, "\n");
7130 }
7131
7132 err = fclose(fp);
7133 assert(err == 0);
7134
7135 return ret;
7136}
7137
7138/**
7139 * Called when the 'scene-graph' debug scope is bound by a client. This
7140 * one-shot weston-debug scope prints the current scene graph when bound,
7141 * and then terminates the stream.
7142 */
7143static void
7144debug_scene_graph_cb(struct weston_debug_stream *stream, void *data)
7145{
7146 struct weston_compositor *ec = data;
7147 char *str = weston_compositor_print_scene_graph(ec);
7148
7149 weston_debug_stream_printf(stream, "%s", str);
7150 free(str);
7151 weston_debug_stream_complete(stream);
7152}
7153
Giulio Camuffo459137b2014-10-11 23:56:24 +03007154/** Create the compositor.
7155 *
7156 * This functions creates and initializes a compositor instance.
7157 *
7158 * \param display The Wayland display to be used.
7159 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007160 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007161 * using the \ref weston_compositor_get_user_data function.
7162 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007163 *
7164 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007165 */
7166WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007167weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007168 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007169 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007170{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007171 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007172 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007173
Giulio Camuffo459137b2014-10-11 23:56:24 +03007174 ec = zalloc(sizeof *ec);
7175 if (!ec)
7176 return NULL;
7177
7178 ec->wl_display = display;
7179 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007180 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007181 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007182 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007183 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007184 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007185 wl_signal_init(&ec->idle_signal);
7186 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007187 wl_signal_init(&ec->show_input_panel_signal);
7188 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007189 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007190 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007191 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007192 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007193 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007194 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007195 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007196 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007197 wl_signal_init(&ec->session_signal);
7198 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007199
Casey Dahlin58ba1372012-04-19 22:50:08 -04007200 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007201 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007202
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007203 ec->activate_serial = 1;
7204
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007205 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7206
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307207 ec->content_protection = NULL;
7208
Derek Foreman152254b2015-11-26 14:17:48 -06007209 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007210 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007211 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007212
Giulio Camuffo954f1832014-10-11 18:27:30 +03007213 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007214 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007215 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007216
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007217 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007218 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007219 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007220
Roman Gilge97391c2019-03-29 13:01:06 +01007221 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7222 ec, bind_xdg_output_manager))
7223 goto fail;
7224
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007225 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007226 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007227 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007228
Marius Vlad3d7d9782019-04-17 12:35:38 +03007229 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007230 goto fail;
7231
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007232 if (weston_input_init(ec) != 0)
7233 goto fail;
7234
Jason Ekstranda7af7042013-10-12 22:38:11 -05007235 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007236 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007237 wl_list_init(&ec->layer_list);
7238 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007239 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007240 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007241 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007242 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007243 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007244 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007245 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007246 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007247 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007248
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007249 wl_list_init(&ec->plugin_api_list);
7250
Xiong Zhang97116532013-10-23 13:58:31 +08007251 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007252 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007253
Giulio Camuffo459137b2014-10-11 23:56:24 +03007254 wl_data_device_manager_init(ec->wl_display);
7255
7256 wl_display_init_shm(ec->wl_display);
7257
7258 loop = wl_display_get_event_loop(ec->wl_display);
7259 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007260 ec->repaint_timer =
7261 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7262 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007263
Quentin Glidic82681572016-12-17 13:40:51 +01007264 weston_layer_init(&ec->fade_layer, ec);
7265 weston_layer_init(&ec->cursor_layer, ec);
7266
7267 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7268 weston_layer_set_position(&ec->cursor_layer,
7269 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007270
7271 weston_compositor_add_debug_binding(ec, KEY_T,
7272 timeline_key_binding_handler, ec);
7273
Daniel Stonece62cb32018-07-20 09:46:24 +01007274 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03007275 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
7276 "Scene graph details\n",
7277 debug_scene_graph_cb,
7278 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007279
Giulio Camuffo459137b2014-10-11 23:56:24 +03007280 return ec;
7281
7282fail:
7283 free(ec);
7284 return NULL;
7285}
7286
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007287/** weston_compositor_shutdown
7288 * \ingroup compositor
7289 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007290WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007291weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007292{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007293 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007294
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007295 wl_event_source_remove(ec->idle_source);
7296
Matt Roper361d2ad2011-08-29 13:52:23 -07007297 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007298 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007299 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007300
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007301 /* Destroy all pending outputs associated with this compositor */
7302 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7303 output->destroy(output);
7304
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007305 if (ec->renderer)
7306 ec->renderer->destroy(ec);
7307
Daniel Stone325fc2d2012-05-30 16:31:58 +01007308 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007309 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007310 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007311 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007312 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007313 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007314
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007315 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007316}
7317
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007318/** weston_compositor_exit_with_code
7319 * \ingroup compositor
7320 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007321WL_EXPORT void
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007322weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007323 int exit_code)
7324{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007325 if (compositor->exit_code == EXIT_SUCCESS)
7326 compositor->exit_code = exit_code;
7327
Giulio Camuffo459137b2014-10-11 23:56:24 +03007328 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007329}
7330
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007331/** weston_compositor_set_default_pointer_grab
7332 * \ingroup compositor
7333 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007334WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007335weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7336 const struct weston_pointer_grab_interface *interface)
7337{
7338 struct weston_seat *seat;
7339
7340 ec->default_pointer_grab = interface;
7341 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007342 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7343
7344 if (pointer)
7345 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007346 }
7347}
7348
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007349/** weston_compositor_set_presentation_clock
7350 * \ingroup compositor
7351 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007352WL_EXPORT int
7353weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7354 clockid_t clk_id)
7355{
7356 struct timespec ts;
7357
7358 if (clock_gettime(clk_id, &ts) < 0)
7359 return -1;
7360
7361 compositor->presentation_clock = clk_id;
7362
7363 return 0;
7364}
7365
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007366/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007367 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007368 *
7369 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007370 */
7371WL_EXPORT int
7372weston_compositor_set_presentation_clock_software(
7373 struct weston_compositor *compositor)
7374{
7375 /* In order of preference */
7376 static const clockid_t clocks[] = {
7377 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7378 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7379 CLOCK_MONOTONIC, /* no jumps, may crawl */
7380 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7381 CLOCK_REALTIME /* may jump and crawl */
7382 };
7383 unsigned i;
7384
7385 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7386 if (weston_compositor_set_presentation_clock(compositor,
7387 clocks[i]) == 0)
7388 return 0;
7389
7390 weston_log("Error: no suitable presentation clock available.\n");
7391
7392 return -1;
7393}
7394
Pekka Paalanen662f3842015-03-18 12:17:26 +02007395/** Read the current time from the Presentation clock
7396 *
7397 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007398 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007399 *
7400 * \note Reading the current time in user space is always imprecise to some
7401 * degree.
7402 *
7403 * This function is never meant to fail. If reading the clock does fail,
7404 * an error message is logged and a zero time is returned. Callers are not
7405 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007406 *
7407 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007408 */
7409WL_EXPORT void
7410weston_compositor_read_presentation_clock(
7411 const struct weston_compositor *compositor,
7412 struct timespec *ts)
7413{
7414 static bool warned;
7415 int ret;
7416
7417 ret = clock_gettime(compositor->presentation_clock, ts);
7418 if (ret < 0) {
7419 ts->tv_sec = 0;
7420 ts->tv_nsec = 0;
7421
7422 if (!warned)
7423 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007424 "the presentation clock %#x: '%s' (%d)\n",
7425 compositor->presentation_clock,
7426 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007427 warned = true;
7428 }
7429}
7430
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007431/** Import dmabuf buffer into current renderer
7432 *
7433 * \param compositor
7434 * \param buffer the dmabuf buffer to import
7435 * \return true on usable buffers, false otherwise
7436 *
7437 * This function tests that the linux_dmabuf_buffer is usable
7438 * for the current renderer. Returns false on unusable buffers. Usually
7439 * usability is tested by importing the dmabufs for composition.
7440 *
7441 * This hook is also used for detecting if the renderer supports
7442 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7443 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007444 *
7445 * \ingroup compositor
7446 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007447WL_EXPORT bool
7448weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7449 struct linux_dmabuf_buffer *buffer)
7450{
7451 struct weston_renderer *renderer;
7452
7453 renderer = compositor->renderer;
7454
7455 if (renderer->import_dmabuf == NULL)
7456 return false;
7457
7458 return renderer->import_dmabuf(compositor, buffer);
7459}
7460
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007461WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007462weston_version(int *major, int *minor, int *micro)
7463{
7464 *major = WESTON_VERSION_MAJOR;
7465 *minor = WESTON_VERSION_MINOR;
7466 *micro = WESTON_VERSION_MICRO;
7467}
7468
Daniel Stonee03c1112016-11-24 20:45:45 +00007469/**
7470 * Attempts to find a module path from the module map specified in the
7471 * environment. If found, writes the full path into the path variable.
7472 *
7473 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7474 * each entry is of the form "name=path" and entries are separated by
7475 * semicolons. Whitespace is significant.
7476 *
7477 * \param name The name to search for.
7478 * \param path Where the path is written to if found.
7479 * \param path_len Allocated bytes at \c path .
7480 * \returns The length of the string written to path on success, or 0 if the
7481 * module was not specified in the environment map or path_len was too small.
7482 */
7483WL_EXPORT size_t
7484weston_module_path_from_env(const char *name, char *path, size_t path_len)
7485{
7486 const char *mapping = getenv("WESTON_MODULE_MAP");
7487 const char *end;
7488 const int name_len = strlen(name);
7489
7490 if (!mapping)
7491 return 0;
7492
7493 end = mapping + strlen(mapping);
7494 while (mapping < end && *mapping) {
7495 const char *filename, *next;
7496
7497 /* early out: impossibly short string */
7498 if (end - mapping < name_len + 1)
7499 return 0;
7500
7501 filename = &mapping[name_len + 1];
7502 next = strchrnul(mapping, ';');
7503
7504 if (strncmp(mapping, name, name_len) == 0 &&
7505 mapping[name_len] == '=') {
7506 size_t file_len = next - filename; /* no trailing NUL */
7507 if (file_len >= path_len)
7508 return 0;
7509 strncpy(path, filename, file_len);
7510 path[file_len] = '\0';
7511 return file_len;
7512 }
7513
7514 mapping = next + 1;
7515 }
7516
7517 return 0;
7518}
7519
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007520WL_EXPORT void *
7521weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007522{
7523 char path[PATH_MAX];
7524 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007525 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007526
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007527 if (name == NULL)
7528 return NULL;
7529
Derek Foreman3f86e502015-06-08 11:46:54 -05007530 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007531 len = weston_module_path_from_env(name, path, sizeof path);
7532 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007533 len = snprintf(path, sizeof path, "%s/%s",
7534 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007535 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007536 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007537 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007538
Daniel Stonebeb97e52016-11-28 12:13:54 +00007539 /* snprintf returns the length of the string it would've written,
7540 * _excluding_ the NUL byte. So even being equal to the size of
7541 * our buffer is an error here. */
7542 if (len >= sizeof path)
7543 return NULL;
7544
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007545 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7546 if (module) {
7547 weston_log("Module '%s' already loaded\n", path);
7548 dlclose(module);
7549 return NULL;
7550 }
7551
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007552 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007553 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007554 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007555 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007556 return NULL;
7557 }
7558
7559 init = dlsym(module, entrypoint);
7560 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007561 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007562 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007563 return NULL;
7564 }
7565
7566 return init;
7567}
7568
Giulio Camuffo459137b2014-10-11 23:56:24 +03007569
7570/** Destroys the compositor.
7571 *
7572 * This function cleans up the compositor state and destroys it.
7573 *
7574 * \param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007575 *
7576 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007577 */
7578WL_EXPORT void
7579weston_compositor_destroy(struct weston_compositor *compositor)
7580{
7581 /* prevent further rendering while shutting down */
7582 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7583
7584 wl_signal_emit(&compositor->destroy_signal, compositor);
7585
7586 weston_compositor_xkb_destroy(compositor);
7587
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007588 if (compositor->backend)
7589 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007590
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007591 /* The backend is responsible for destroying the heads. */
7592 assert(wl_list_empty(&compositor->head_list));
7593
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007594 weston_plugin_api_destroy_list(compositor);
7595
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007596 if (compositor->heads_changed_source)
7597 wl_event_source_remove(compositor->heads_changed_source);
7598
Marius Vlad7e4db952019-04-17 13:47:06 +03007599 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007600 compositor->debug_scene = NULL;
Marius Vlad3d7d9782019-04-17 12:35:38 +03007601 weston_log_ctx_compositor_destroy(compositor);
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007602
Giulio Camuffo459137b2014-10-11 23:56:24 +03007603 free(compositor);
7604}
7605
7606/** Instruct the compositor to exit.
7607 *
7608 * This functions does not directly destroy the compositor object, it merely
7609 * command it to start the tear down process. It is not guaranteed that the
7610 * tear down will happen immediately.
7611 *
7612 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007613 *
7614 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007615 */
7616WL_EXPORT void
7617weston_compositor_exit(struct weston_compositor *compositor)
7618{
7619 compositor->exit(compositor);
7620}
7621
7622/** Return the user data stored in the compositor.
7623 *
7624 * This function returns the user data pointer set with user_data parameter
7625 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007626 *
7627 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007628 */
7629WL_EXPORT void *
7630weston_compositor_get_user_data(struct weston_compositor *compositor)
7631{
7632 return compositor->user_data;
7633}
Pekka Paalanendd186732016-06-03 14:49:54 +03007634
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007635static const char * const backend_map[] = {
7636 [WESTON_BACKEND_DRM] = "drm-backend.so",
7637 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7638 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7639 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7640 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7641 [WESTON_BACKEND_X11] = "x11-backend.so",
7642};
7643
Pekka Paalanendd186732016-06-03 14:49:54 +03007644/** Load a backend into a weston_compositor
7645 *
7646 * A backend must be loaded to make a weston_compositor work. A backend
7647 * provides input and output capabilities, and determines the renderer to use.
7648 *
7649 * \param compositor A compositor that has not had a backend loaded yet.
7650 * \param backend Name of the backend file.
7651 * \param config_base A pointer to a backend-specific configuration
7652 * structure's 'base' member.
7653 *
7654 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007655 *
7656 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007657 */
7658WL_EXPORT int
7659weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007660 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007661 struct weston_backend_config *config_base)
7662{
7663 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007664 struct weston_backend_config *config_base);
7665
Pekka Paalanend7e35112017-08-29 17:04:12 +03007666 if (compositor->backend) {
7667 weston_log("Error: attempt to load a backend when one is already loaded\n");
7668 return -1;
7669 }
7670
Quentin Glidic887c0182016-07-10 11:00:53 +02007671 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007672 return -1;
7673
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007674 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007675 if (!backend_init)
7676 return -1;
7677
Pekka Paalanend7e35112017-08-29 17:04:12 +03007678 if (backend_init(compositor, config_base) < 0) {
7679 compositor->backend = NULL;
7680 return -1;
7681 }
7682
7683 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007684}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007685
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007686/** weston_compositor_load_xwayland
7687 * \ingroup compositor
7688 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007689WL_EXPORT int
7690weston_compositor_load_xwayland(struct weston_compositor *compositor)
7691{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007692 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007693
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007694 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007695 if (!module_init)
7696 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007697 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007698 return -1;
7699 return 0;
7700}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007701
7702/** Resolve an internal compositor error by disconnecting the client.
7703 *
7704 * This function is used in cases when the wl_buffer turns out
7705 * unusable and there is no fallback path.
7706 *
7707 * It is possible the fault is caused by a compositor bug, the underlying
7708 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7709 * In any case, the options are to either composite garbage or nothing,
7710 * or disconnect the client. This is a helper function for the latter.
7711 *
7712 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7713 *
7714 * \param buffer The weston buffer that is unusable.
7715 * \param msg A custom error message attached to the protocol error.
7716 */
7717WL_EXPORT void
7718weston_buffer_send_server_error(struct weston_buffer *buffer,
7719 const char *msg)
7720{
7721 struct wl_client *client;
7722 struct wl_resource *display_resource;
7723 uint32_t id;
7724
7725 assert(buffer->resource);
7726 id = wl_resource_get_id(buffer->resource);
7727 client = wl_resource_get_client(buffer->resource);
7728 display_resource = wl_client_get_object(client, 1);
7729
7730 assert(display_resource);
7731 wl_resource_post_error(display_resource,
7732 WL_DISPLAY_ERROR_INVALID_OBJECT,
7733 "server error with "
7734 "wl_buffer@%u: %s", id, msg);
7735}