blob: d417d7ac1625269ff72262cf3a477c9d30830bca [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)
2793 compositor->backend->repaint_flush(compositor,
2794 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002795 } else {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002796 wl_list_for_each(output, &compositor->output_list, link) {
2797 if (output->repainted)
2798 weston_output_schedule_repaint_reset(output);
2799 }
2800
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002801 if (compositor->backend->repaint_cancel)
2802 compositor->backend->repaint_cancel(compositor,
2803 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002804 }
Daniel Stone6847b852017-03-01 11:34:08 +00002805
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002806 wl_list_for_each(output, &compositor->output_list, link)
2807 output->repainted = false;
2808
Daniel Stone6847b852017-03-01 11:34:08 +00002809 output_repaint_timer_arm(compositor);
2810
Pekka Paalanen0513a952014-05-21 16:17:27 +03002811 return 0;
2812}
2813
Marius Vlad55d87362019-06-11 01:15:35 +03002814/**
2815 * \ingroup output
2816 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002817WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002818weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002819 const struct timespec *stamp,
2820 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002821{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002822 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002823 int32_t refresh_nsec;
2824 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002825 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002826
Pekka Paalanenb5026542014-11-12 15:09:24 +02002827
Daniel Stone05df8c12017-03-03 16:59:42 +00002828 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002829 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2830
Daniel Stone6847b852017-03-01 11:34:08 +00002831 weston_compositor_read_presentation_clock(compositor, &now);
2832
Daniel Stone3615ce12017-03-01 11:34:05 +00002833 /* If we haven't been supplied any timestamp at all, we don't have a
2834 * timebase to work against, so any delay just wastes time. Push a
2835 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002836 if (!stamp) {
2837 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002838 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002839 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002840
Marius Vladdf9278a2018-03-06 18:56:23 +02002841 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2842 TLP_VBLANK(stamp), TLP_END);
2843
Pekka Paalanend7894d02015-07-03 15:08:53 +03002844 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002845 weston_presentation_feedback_present_list(&output->feedback_list,
2846 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002847 output->msc,
2848 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002849
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002850 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002851
Daniel Stone6847b852017-03-01 11:34:08 +00002852 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2853 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2854 -compositor->repaint_msec);
2855 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002856
2857 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002858 static bool warned;
2859
2860 if (!warned)
2861 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002862 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002863 warned = true;
2864
Daniel Stone6847b852017-03-01 11:34:08 +00002865 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002866 }
2867
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002868 /* Called from restart_repaint_loop and restart happens already after
2869 * the deadline given by repaint_msec? In that case we delay until
2870 * the deadline of the next frame, to give clients a more predictable
2871 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002872 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2873 msec_rel < 0) {
2874 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2875 timespec_add_nsec(&output->next_repaint,
2876 &output->next_repaint,
2877 refresh_nsec);
2878 }
2879 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002880
Daniel Stone3615ce12017-03-01 11:34:05 +00002881out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002882 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002883 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002884}
2885
2886static void
2887idle_repaint(void *data)
2888{
2889 struct weston_output *output = data;
2890
Daniel Stone05df8c12017-03-03 16:59:42 +00002891 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2892 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002893 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002894 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002895}
2896
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002897WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002898weston_layer_entry_insert(struct weston_layer_entry *list,
2899 struct weston_layer_entry *entry)
2900{
2901 wl_list_insert(&list->link, &entry->link);
2902 entry->layer = list->layer;
2903}
2904
2905WL_EXPORT void
2906weston_layer_entry_remove(struct weston_layer_entry *entry)
2907{
2908 wl_list_remove(&entry->link);
2909 wl_list_init(&entry->link);
2910 entry->layer = NULL;
2911}
2912
Quentin Glidic82681572016-12-17 13:40:51 +01002913
2914/** Initialize the weston_layer struct.
2915 *
2916 * \param compositor The compositor instance
2917 * \param layer The layer to initialize
2918 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002919WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002920weston_layer_init(struct weston_layer *layer,
2921 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002922{
Quentin Glidic82681572016-12-17 13:40:51 +01002923 layer->compositor = compositor;
2924 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002925 wl_list_init(&layer->view_list.link);
2926 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002927 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002928}
2929
2930/** Sets the position of the layer in the layer list. The layer will be placed
2931 * below any layer with the same position value, if any.
2932 * This function is safe to call if the layer is already on the list, but the
2933 * layer may be moved below other layers at the same position, if any.
2934 *
2935 * \param layer The layer to modify
2936 * \param position The position the layer will be placed at
2937 */
2938WL_EXPORT void
2939weston_layer_set_position(struct weston_layer *layer,
2940 enum weston_layer_position position)
2941{
2942 struct weston_layer *below;
2943
2944 wl_list_remove(&layer->link);
2945
2946 /* layer_list is ordered from top to bottom, the last layer being the
2947 * background with the smallest position value */
2948
2949 layer->position = position;
2950 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2951 if (below->position >= layer->position) {
2952 wl_list_insert(&below->link, &layer->link);
2953 return;
2954 }
2955 }
2956 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2957}
2958
2959/** Hide a layer by taking it off the layer list.
2960 * This function is safe to call if the layer is not on the list.
2961 *
2962 * \param layer The layer to hide
2963 */
2964WL_EXPORT void
2965weston_layer_unset_position(struct weston_layer *layer)
2966{
2967 wl_list_remove(&layer->link);
2968 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002969}
2970
2971WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002972weston_layer_set_mask(struct weston_layer *layer,
2973 int x, int y, int width, int height)
2974{
2975 struct weston_view *view;
2976
2977 layer->mask.x1 = x;
2978 layer->mask.x2 = x + width;
2979 layer->mask.y1 = y;
2980 layer->mask.y2 = y + height;
2981
2982 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2983 weston_view_geometry_dirty(view);
2984 }
2985}
2986
2987WL_EXPORT void
2988weston_layer_set_mask_infinite(struct weston_layer *layer)
2989{
2990 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2991 UINT32_MAX, UINT32_MAX);
2992}
2993
Daniel Stone3b775632018-07-20 08:38:25 +01002994WL_EXPORT bool
2995weston_layer_mask_is_infinite(struct weston_layer *layer)
2996{
2997 return layer->mask.x1 == INT32_MIN &&
2998 layer->mask.y1 == INT32_MIN &&
2999 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
3000 layer->mask.y2 == INT32_MIN + UINT32_MAX;
3001}
3002
Marius Vlad55d87362019-06-11 01:15:35 +03003003/**
3004 * \ingroup output
3005 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003006WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003007weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003008{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003009 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003010 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003011
Bryce Harrington08976ac2016-08-30 12:05:16 -07003012 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3013 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003014 return;
3015
Pekka Paalanenb5026542014-11-12 15:09:24 +02003016 if (!output->repaint_needed)
3017 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
3018
Kristian Høgsbergef044142011-06-21 15:02:12 -04003019 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003020 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003021
3022 /* If we already have a repaint scheduled for our idle handler,
3023 * no need to set it again. If the repaint has been called but
3024 * not finished, then weston_output_finish_frame() will notice
3025 * that a repaint is needed and schedule one. */
3026 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003027 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003028
Daniel Stone05df8c12017-03-03 16:59:42 +00003029 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003030 assert(!output->idle_repaint_source);
3031 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3032 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003033 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003034}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003035
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003036/** weston_compositor_schedule_repaint
3037 * \ingroup compositor
3038 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003039WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003040weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3041{
3042 struct weston_output *output;
3043
3044 wl_list_for_each(output, &compositor->output_list, link)
3045 weston_output_schedule_repaint(output);
3046}
3047
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003048static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003049surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003050{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003051 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003052}
3053
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003054static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003055surface_attach(struct wl_client *client,
3056 struct wl_resource *resource,
3057 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3058{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003059 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003060 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003061
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003062 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003063 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003064 if (buffer == NULL) {
3065 wl_client_post_no_memory(client);
3066 return;
3067 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003068 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003069
Pekka Paalanende685b82012-12-04 15:58:12 +02003070 /* Attach, attach, without commit in between does not send
3071 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003072 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003073
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003074 surface->pending.sx = sx;
3075 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003076 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003077}
3078
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003079static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003080surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003081 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003082 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003083{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003084 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003085
Derek Foreman57e92ed2015-11-17 14:11:35 -06003086 if (width <= 0 || height <= 0)
3087 return;
3088
Derek Foreman152254b2015-11-26 14:17:48 -06003089 pixman_region32_union_rect(&surface->pending.damage_surface,
3090 &surface->pending.damage_surface,
3091 x, y, width, height);
3092}
3093
3094static void
3095surface_damage_buffer(struct wl_client *client,
3096 struct wl_resource *resource,
3097 int32_t x, int32_t y, int32_t width, int32_t height)
3098{
3099 struct weston_surface *surface = wl_resource_get_user_data(resource);
3100
3101 if (width <= 0 || height <= 0)
3102 return;
3103
3104 pixman_region32_union_rect(&surface->pending.damage_buffer,
3105 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003106 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003107}
3108
Kristian Høgsberg33418202011-08-16 23:01:28 -04003109static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003110destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003111{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003112 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003113
3114 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003115 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003116}
3117
3118static void
3119surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003120 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003121{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003122 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003123 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003124
3125 cb = malloc(sizeof *cb);
3126 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003127 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003128 return;
3129 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003130
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003131 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3132 callback);
3133 if (cb->resource == NULL) {
3134 free(cb);
3135 wl_resource_post_no_memory(resource);
3136 return;
3137 }
3138
Jason Ekstranda85118c2013-06-27 20:17:02 -05003139 wl_resource_set_implementation(cb->resource, NULL, cb,
3140 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003141
Pekka Paalanenbc106382012-10-10 12:49:31 +03003142 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003143}
3144
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003145static void
3146surface_set_opaque_region(struct wl_client *client,
3147 struct wl_resource *resource,
3148 struct wl_resource *region_resource)
3149{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003150 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003151 struct weston_region *region;
3152
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003153 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003154 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003155 pixman_region32_copy(&surface->pending.opaque,
3156 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003157 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003158 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003159 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003160}
3161
3162static void
3163surface_set_input_region(struct wl_client *client,
3164 struct wl_resource *resource,
3165 struct wl_resource *region_resource)
3166{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003167 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003168 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003169
3170 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003171 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003172 pixman_region32_copy(&surface->pending.input,
3173 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003174 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003175 pixman_region32_fini(&surface->pending.input);
3176 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003177 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003178}
3179
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003180/* Cause damage to this sub-surface and all its children.
3181 *
3182 * This is useful when there are state changes that need an implicit
3183 * damage, e.g. a z-order change.
3184 */
3185static void
3186weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3187{
3188 struct weston_subsurface *child;
3189
3190 weston_surface_damage(sub->surface);
3191 sub->reordered = false;
3192
3193 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3194 if (child != sub)
3195 weston_surface_damage_subsurfaces(child);
3196}
3197
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003198static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003199weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003200{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003201 struct weston_subsurface *sub;
3202
3203 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3204 parent_link_pending) {
3205 wl_list_remove(&sub->parent_link);
3206 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003207
3208 if (sub->reordered)
3209 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003210 }
3211}
3212
3213static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003214weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003215 struct weston_matrix *matrix)
3216{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003217 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003218 double src_width, src_height, dest_width, dest_height;
3219
3220 weston_matrix_init(matrix);
3221
3222 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3223 src_width = surface->width_from_buffer;
3224 src_height = surface->height_from_buffer;
3225 } else {
3226 src_width = wl_fixed_to_double(vp->buffer.src_width);
3227 src_height = wl_fixed_to_double(vp->buffer.src_height);
3228 }
3229
3230 if (vp->surface.width == -1) {
3231 dest_width = src_width;
3232 dest_height = src_height;
3233 } else {
3234 dest_width = vp->surface.width;
3235 dest_height = vp->surface.height;
3236 }
3237
3238 if (src_width != dest_width || src_height != dest_height)
3239 weston_matrix_scale(matrix,
3240 src_width / dest_width,
3241 src_height / dest_height, 1);
3242
3243 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3244 weston_matrix_translate(matrix,
3245 wl_fixed_to_double(vp->buffer.src_x),
3246 wl_fixed_to_double(vp->buffer.src_y),
3247 0);
3248
3249 switch (vp->buffer.transform) {
3250 case WL_OUTPUT_TRANSFORM_FLIPPED:
3251 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3252 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3253 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3254 weston_matrix_scale(matrix, -1, 1, 1);
3255 weston_matrix_translate(matrix,
3256 surface->width_from_buffer, 0, 0);
3257 break;
3258 }
3259
3260 switch (vp->buffer.transform) {
3261 default:
3262 case WL_OUTPUT_TRANSFORM_NORMAL:
3263 case WL_OUTPUT_TRANSFORM_FLIPPED:
3264 break;
3265 case WL_OUTPUT_TRANSFORM_90:
3266 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3267 weston_matrix_rotate_xy(matrix, 0, 1);
3268 weston_matrix_translate(matrix,
3269 surface->height_from_buffer, 0, 0);
3270 break;
3271 case WL_OUTPUT_TRANSFORM_180:
3272 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3273 weston_matrix_rotate_xy(matrix, -1, 0);
3274 weston_matrix_translate(matrix,
3275 surface->width_from_buffer,
3276 surface->height_from_buffer, 0);
3277 break;
3278 case WL_OUTPUT_TRANSFORM_270:
3279 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3280 weston_matrix_rotate_xy(matrix, 0, -1);
3281 weston_matrix_translate(matrix,
3282 0, surface->width_from_buffer, 0);
3283 break;
3284 }
3285
3286 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3287}
3288
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003289/**
3290 * Compute a + b > c while being safe to overflows.
3291 */
3292static bool
3293fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3294{
3295 return (int64_t)a + (int64_t)b > (int64_t)c;
3296}
3297
3298static bool
3299weston_surface_is_pending_viewport_source_valid(
3300 const struct weston_surface *surface)
3301{
3302 const struct weston_surface_state *pend = &surface->pending;
3303 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3304 int width_from_buffer = 0;
3305 int height_from_buffer = 0;
3306 wl_fixed_t w;
3307 wl_fixed_t h;
3308
3309 /* If viewport source rect is not set, it is always ok. */
3310 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3311 return true;
3312
3313 if (pend->newly_attached) {
3314 if (pend->buffer) {
3315 convert_size_by_transform_scale(&width_from_buffer,
3316 &height_from_buffer,
3317 pend->buffer->width,
3318 pend->buffer->height,
3319 vp->buffer.transform,
3320 vp->buffer.scale);
3321 } else {
3322 /* No buffer: viewport is irrelevant. */
3323 return true;
3324 }
3325 } else {
3326 width_from_buffer = surface->width_from_buffer;
3327 height_from_buffer = surface->height_from_buffer;
3328 }
3329
3330 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3331 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3332
3333 /* No buffer: viewport is irrelevant. */
3334 if (width_from_buffer == 0 || height_from_buffer == 0)
3335 return true;
3336
3337 /* overflow checks for wl_fixed_from_int() */
3338 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3339 return false;
3340 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3341 return false;
3342
3343 w = wl_fixed_from_int(width_from_buffer);
3344 h = wl_fixed_from_int(height_from_buffer);
3345
3346 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3347 return false;
3348 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3349 return false;
3350
3351 return true;
3352}
3353
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003354static bool
3355fixed_is_integer(wl_fixed_t v)
3356{
3357 return (v & 0xff) == 0;
3358}
3359
3360static bool
3361weston_surface_is_pending_viewport_dst_size_int(
3362 const struct weston_surface *surface)
3363{
3364 const struct weston_buffer_viewport *vp =
3365 &surface->pending.buffer_viewport;
3366
3367 if (vp->surface.width != -1) {
3368 assert(vp->surface.width > 0 && vp->surface.height > 0);
3369 return true;
3370 }
3371
3372 return fixed_is_integer(vp->buffer.src_width) &&
3373 fixed_is_integer(vp->buffer.src_height);
3374}
3375
Derek Foreman152254b2015-11-26 14:17:48 -06003376/* Translate pending damage in buffer co-ordinates to surface
3377 * co-ordinates and union it with a pixman_region32_t.
3378 * This should only be called after the buffer is attached.
3379 */
3380static void
3381apply_damage_buffer(pixman_region32_t *dest,
3382 struct weston_surface *surface,
3383 struct weston_surface_state *state)
3384{
3385 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3386
3387 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3388 * translated into surface co-ordinates and unioned with
3389 * any other surface damage.
3390 * None of this makes sense if there is no buffer though.
3391 */
3392 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3393 pixman_region32_t buffer_damage;
3394
3395 pixman_region32_intersect_rect(&state->damage_buffer,
3396 &state->damage_buffer,
3397 0, 0, buffer->width,
3398 buffer->height);
3399 pixman_region32_init(&buffer_damage);
3400 weston_matrix_transform_region(&buffer_damage,
3401 &surface->buffer_to_surface_matrix,
3402 &state->damage_buffer);
3403 pixman_region32_union(dest, dest, &buffer_damage);
3404 pixman_region32_fini(&buffer_damage);
3405 }
3406 /* We should clear this on commit even if there was no buffer */
3407 pixman_region32_clear(&state->damage_buffer);
3408}
3409
Jason Ekstrand1e059042014-10-16 10:55:19 -05003410static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303411weston_surface_set_desired_protection(struct weston_surface *surface,
3412 enum weston_hdcp_protection protection)
3413{
3414 if (surface->desired_protection == protection)
3415 return;
3416 surface->desired_protection = protection;
3417 weston_surface_damage(surface);
3418}
3419
3420static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303421weston_surface_set_protection_mode(struct weston_surface *surface,
3422 enum weston_surface_protection_mode p_mode)
3423{
3424 struct content_protection *cp = surface->compositor->content_protection;
3425 struct protected_surface *psurface;
3426
3427 surface->protection_mode = p_mode;
3428 wl_list_for_each(psurface, &cp->protected_list, link) {
3429 if (!psurface || psurface->surface != surface)
3430 continue;
3431 weston_protected_surface_send_event(psurface,
3432 surface->current_protection);
3433 }
3434}
3435
3436static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003437weston_surface_commit_state(struct weston_surface *surface,
3438 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003439{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003440 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003441 pixman_region32_t opaque;
3442
Alexander Larsson4ea95522013-05-22 14:41:37 +02003443 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003444 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003445 /* wp_viewport.set_source */
3446 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003447 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003448
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003449 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003450 if (state->newly_attached) {
3451 /* zwp_surface_synchronization_v1.set_acquire_fence */
3452 fd_move(&surface->acquire_fence_fd,
3453 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003454 /* zwp_surface_synchronization_v1.get_release */
3455 weston_buffer_release_move(&surface->buffer_release_ref,
3456 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003457 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003458 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003459 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003460 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003461 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003462
Jason Ekstrand1e059042014-10-16 10:55:19 -05003463 weston_surface_build_buffer_matrix(surface,
3464 &surface->surface_to_buffer_matrix);
3465 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3466 &surface->surface_to_buffer_matrix);
3467
Jason Ekstrand7b982072014-05-20 14:33:03 -05003468 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003469 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003470 if (surface->committed)
3471 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003472 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003473
Jason Ekstrand7b982072014-05-20 14:33:03 -05003474 state->sx = 0;
3475 state->sy = 0;
3476 state->newly_attached = 0;
3477 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003478
Derek Foreman152254b2015-11-26 14:17:48 -06003479 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003480 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003481 (pixman_region32_not_empty(&state->damage_surface) ||
3482 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003483 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003484
Pekka Paalanen8e159182012-10-10 12:49:25 +03003485 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003486 &state->damage_surface);
3487
3488 apply_damage_buffer(&surface->damage, surface, state);
3489
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003490 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003491 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003492 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003493
3494 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003495 pixman_region32_init(&opaque);
3496 pixman_region32_intersect_rect(&opaque, &state->opaque,
3497 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003498
3499 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3500 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003501 wl_list_for_each(view, &surface->views, surface_link)
3502 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003503 }
3504
3505 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003506
3507 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003508 pixman_region32_intersect_rect(&surface->input, &state->input,
3509 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003510
Pekka Paalanenbc106382012-10-10 12:49:31 +03003511 /* wl_surface.frame */
3512 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003513 &state->frame_callback_list);
3514 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003515
3516 /* XXX:
3517 * What should happen with a feedback request, if there
3518 * is no wl_buffer attached for this commit?
3519 */
3520
3521 /* presentation.feedback */
3522 wl_list_insert_list(&surface->feedback_list,
3523 &state->feedback_list);
3524 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003525
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303526 /* weston_protected_surface.enforced/relaxed */
3527 if (surface->protection_mode != state->protection_mode)
3528 weston_surface_set_protection_mode(surface,
3529 state->protection_mode);
3530
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303531 /* weston_protected_surface.set_type */
3532 weston_surface_set_desired_protection(surface, state->desired_protection);
3533
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003534 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003535}
3536
3537static void
3538weston_surface_commit(struct weston_surface *surface)
3539{
3540 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003541
Pekka Paalanene67858b2013-04-25 13:57:42 +03003542 weston_surface_commit_subsurface_order(surface);
3543
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003544 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003545}
3546
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003547static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003548weston_subsurface_commit(struct weston_subsurface *sub);
3549
3550static void
3551weston_subsurface_parent_commit(struct weston_subsurface *sub,
3552 int parent_is_synchronized);
3553
3554static void
3555surface_commit(struct wl_client *client, struct wl_resource *resource)
3556{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003557 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003558 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3559
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003560 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3561 assert(surface->viewport_resource);
3562
3563 wl_resource_post_error(surface->viewport_resource,
3564 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3565 "wl_surface@%d has viewport source outside buffer",
3566 wl_resource_get_id(resource));
3567 return;
3568 }
3569
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003570 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3571 assert(surface->viewport_resource);
3572
3573 wl_resource_post_error(surface->viewport_resource,
3574 WP_VIEWPORT_ERROR_BAD_SIZE,
3575 "wl_surface@%d viewport dst size not integer",
3576 wl_resource_get_id(resource));
3577 return;
3578 }
3579
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003580 if (surface->pending.acquire_fence_fd >= 0) {
3581 assert(surface->synchronization_resource);
3582
3583 if (!surface->pending.buffer) {
3584 fd_clear(&surface->pending.acquire_fence_fd);
3585 wl_resource_post_error(surface->synchronization_resource,
3586 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3587 "wl_surface@%"PRIu32" no buffer for synchronization",
3588 wl_resource_get_id(resource));
3589 return;
3590 }
3591
3592 /* We support fences for both wp_linux_dmabuf and opaque EGL
3593 * buffers, as mandated by minor version 2 of the
3594 * zwp_linux_explicit_synchronization_v1 protocol. Since
3595 * renderers that support fences currently only support these
3596 * two buffer types plus SHM buffers, we can just check for the
3597 * SHM buffer case here.
3598 */
3599 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3600 fd_clear(&surface->pending.acquire_fence_fd);
3601 wl_resource_post_error(surface->synchronization_resource,
3602 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3603 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3604 wl_resource_get_id(resource));
3605 return;
3606 }
3607 }
3608
Alexandros Frantzis67629672018-10-19 12:14:11 +03003609 if (surface->pending.buffer_release_ref.buffer_release &&
3610 !surface->pending.buffer) {
3611 assert(surface->synchronization_resource);
3612
3613 wl_resource_post_error(surface->synchronization_resource,
3614 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3615 "wl_surface@%"PRIu32" no buffer for synchronization",
3616 wl_resource_get_id(resource));
3617 return;
3618 }
3619
Pekka Paalanene67858b2013-04-25 13:57:42 +03003620 if (sub) {
3621 weston_subsurface_commit(sub);
3622 return;
3623 }
3624
3625 weston_surface_commit(surface);
3626
3627 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3628 if (sub->surface != surface)
3629 weston_subsurface_parent_commit(sub, 0);
3630 }
3631}
3632
3633static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003634surface_set_buffer_transform(struct wl_client *client,
3635 struct wl_resource *resource, int transform)
3636{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003637 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003638
Jonny Lamba55f1392014-05-30 12:07:15 +02003639 /* if wl_output.transform grows more members this will need to be updated. */
3640 if (transform < 0 ||
3641 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3642 wl_resource_post_error(resource,
3643 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3644 "buffer transform must be a valid transform "
3645 "('%d' specified)", transform);
3646 return;
3647 }
3648
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003649 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003650 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003651}
3652
Alexander Larsson4ea95522013-05-22 14:41:37 +02003653static void
3654surface_set_buffer_scale(struct wl_client *client,
3655 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003656 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003657{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003658 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003659
Jonny Lamba55f1392014-05-30 12:07:15 +02003660 if (scale < 1) {
3661 wl_resource_post_error(resource,
3662 WL_SURFACE_ERROR_INVALID_SCALE,
3663 "buffer scale must be at least one "
3664 "('%d' specified)", scale);
3665 return;
3666 }
3667
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003668 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003669 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003670}
3671
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003672static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003673 surface_destroy,
3674 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003675 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003676 surface_frame,
3677 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003678 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003679 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003680 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003681 surface_set_buffer_scale,
3682 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003683};
3684
3685static void
3686compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003687 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003688{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003689 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003690 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003691
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003692 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003693 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003694 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003695 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003696 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003697
Jason Ekstranda85118c2013-06-27 20:17:02 -05003698 surface->resource =
3699 wl_resource_create(client, &wl_surface_interface,
3700 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003701 if (surface->resource == NULL) {
3702 weston_surface_destroy(surface);
3703 wl_resource_post_no_memory(resource);
3704 return;
3705 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003706 wl_resource_set_implementation(surface->resource, &surface_interface,
3707 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003708
3709 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003710}
3711
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003712static void
3713destroy_region(struct wl_resource *resource)
3714{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003715 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003716
3717 pixman_region32_fini(&region->region);
3718 free(region);
3719}
3720
3721static void
3722region_destroy(struct wl_client *client, struct wl_resource *resource)
3723{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003724 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003725}
3726
3727static void
3728region_add(struct wl_client *client, struct wl_resource *resource,
3729 int32_t x, int32_t y, int32_t width, int32_t height)
3730{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003731 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003732
3733 pixman_region32_union_rect(&region->region, &region->region,
3734 x, y, width, height);
3735}
3736
3737static void
3738region_subtract(struct wl_client *client, struct wl_resource *resource,
3739 int32_t x, int32_t y, int32_t width, int32_t height)
3740{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003741 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003742 pixman_region32_t rect;
3743
3744 pixman_region32_init_rect(&rect, x, y, width, height);
3745 pixman_region32_subtract(&region->region, &region->region, &rect);
3746 pixman_region32_fini(&rect);
3747}
3748
3749static const struct wl_region_interface region_interface = {
3750 region_destroy,
3751 region_add,
3752 region_subtract
3753};
3754
3755static void
3756compositor_create_region(struct wl_client *client,
3757 struct wl_resource *resource, uint32_t id)
3758{
3759 struct weston_region *region;
3760
3761 region = malloc(sizeof *region);
3762 if (region == NULL) {
3763 wl_resource_post_no_memory(resource);
3764 return;
3765 }
3766
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003767 pixman_region32_init(&region->region);
3768
Jason Ekstranda85118c2013-06-27 20:17:02 -05003769 region->resource =
3770 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003771 if (region->resource == NULL) {
3772 free(region);
3773 wl_resource_post_no_memory(resource);
3774 return;
3775 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003776 wl_resource_set_implementation(region->resource, &region_interface,
3777 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003778}
3779
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003780static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003781 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003782 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003783};
3784
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003785static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003786weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3787{
3788 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003789
Jason Ekstrand7b982072014-05-20 14:33:03 -05003790 weston_surface_commit_state(surface, &sub->cached);
3791 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003792
3793 weston_surface_commit_subsurface_order(surface);
3794
3795 weston_surface_schedule_repaint(surface);
3796
Jason Ekstrand7b982072014-05-20 14:33:03 -05003797 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003798}
3799
3800static void
3801weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3802{
3803 struct weston_surface *surface = sub->surface;
3804
3805 /*
3806 * If this commit would cause the surface to move by the
3807 * attach(dx, dy) parameters, the old damage region must be
3808 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003809 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003810 */
Derek Foreman152254b2015-11-26 14:17:48 -06003811 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003812 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003813 pixman_region32_union(&sub->cached.damage_surface,
3814 &sub->cached.damage_surface,
3815 &surface->pending.damage_surface);
3816 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003817
3818 if (surface->pending.newly_attached) {
3819 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003820 weston_surface_state_set_buffer(&sub->cached,
3821 surface->pending.buffer);
3822 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003823 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003824 weston_presentation_feedback_discard_list(
3825 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003826 /* zwp_surface_synchronization_v1.set_acquire_fence */
3827 fd_move(&sub->cached.acquire_fence_fd,
3828 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003829 /* zwp_surface_synchronization_v1.get_release */
3830 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3831 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003832 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303833 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303834 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003835 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003836 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003837 sub->cached.sx += surface->pending.sx;
3838 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003839
Derek Foreman152254b2015-11-26 14:17:48 -06003840 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3841
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003842 sub->cached.buffer_viewport.changed |=
3843 surface->pending.buffer_viewport.changed;
3844 sub->cached.buffer_viewport.buffer =
3845 surface->pending.buffer_viewport.buffer;
3846 sub->cached.buffer_viewport.surface =
3847 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003848
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003849 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003850
3851 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3852
3853 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3854
3855 wl_list_insert_list(&sub->cached.frame_callback_list,
3856 &surface->pending.frame_callback_list);
3857 wl_list_init(&surface->pending.frame_callback_list);
3858
Pekka Paalanen133e4392014-09-23 22:08:46 -04003859 wl_list_insert_list(&sub->cached.feedback_list,
3860 &surface->pending.feedback_list);
3861 wl_list_init(&surface->pending.feedback_list);
3862
Jason Ekstrand7b982072014-05-20 14:33:03 -05003863 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003864}
3865
Derek Foreman280e7dd2014-10-03 13:13:42 -05003866static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003867weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3868{
3869 while (sub) {
3870 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003871 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003872
3873 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003874 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003875
3876 sub = weston_surface_to_subsurface(sub->parent);
3877 }
3878
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003879 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003880}
3881
3882static void
3883weston_subsurface_commit(struct weston_subsurface *sub)
3884{
3885 struct weston_surface *surface = sub->surface;
3886 struct weston_subsurface *tmp;
3887
3888 /* Recursive check for effectively synchronized. */
3889 if (weston_subsurface_is_synchronized(sub)) {
3890 weston_subsurface_commit_to_cache(sub);
3891 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003892 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003893 /* flush accumulated state from cache */
3894 weston_subsurface_commit_to_cache(sub);
3895 weston_subsurface_commit_from_cache(sub);
3896 } else {
3897 weston_surface_commit(surface);
3898 }
3899
3900 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3901 if (tmp->surface != surface)
3902 weston_subsurface_parent_commit(tmp, 0);
3903 }
3904 }
3905}
3906
3907static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003908weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003909{
3910 struct weston_surface *surface = sub->surface;
3911 struct weston_subsurface *tmp;
3912
Pekka Paalanene67858b2013-04-25 13:57:42 +03003913 /* From now on, commit_from_cache the whole sub-tree, regardless of
3914 * the synchronized mode of each child. This sub-surface or some
3915 * of its ancestors were synchronized, so we are synchronized
3916 * all the way down.
3917 */
3918
Jason Ekstrand7b982072014-05-20 14:33:03 -05003919 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003920 weston_subsurface_commit_from_cache(sub);
3921
3922 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3923 if (tmp->surface != surface)
3924 weston_subsurface_parent_commit(tmp, 1);
3925 }
3926}
3927
3928static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003929weston_subsurface_parent_commit(struct weston_subsurface *sub,
3930 int parent_is_synchronized)
3931{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003932 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003933 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003934 wl_list_for_each(view, &sub->surface->views, surface_link)
3935 weston_view_set_position(view,
3936 sub->position.x,
3937 sub->position.y);
3938
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003939 sub->position.set = 0;
3940 }
3941
3942 if (parent_is_synchronized || sub->synchronized)
3943 weston_subsurface_synchronized_commit(sub);
3944}
3945
Pekka Paalanen8274d902014-08-06 19:36:51 +03003946static int
3947subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3948{
3949 return snprintf(buf, len, "sub-surface");
3950}
3951
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003952static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003953subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003954{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003955 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003956
Jason Ekstranda7af7042013-10-12 22:38:11 -05003957 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003958 weston_view_set_position(view,
3959 view->geometry.x + dx,
3960 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003961
3962 /* No need to check parent mappedness, because if parent is not
3963 * mapped, parent is not in a visible layer, so this sub-surface
3964 * will not be drawn either.
3965 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003966
Pekka Paalanene67858b2013-04-25 13:57:42 +03003967 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003968 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003969
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003970 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003971 * because that would call it also for the parent surface,
3972 * which might not be mapped yet. That would lead to
3973 * inconsistent state, where the window could never be
3974 * mapped.
3975 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003976 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003977 * weston_surface_is_mapped() return true, so that when the
3978 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003979 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003980 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003981 }
3982}
3983
3984static struct weston_subsurface *
3985weston_surface_to_subsurface(struct weston_surface *surface)
3986{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003987 if (surface->committed == subsurface_committed)
3988 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003989
3990 return NULL;
3991}
3992
Pekka Paalanen01388e22013-04-25 13:57:44 +03003993WL_EXPORT struct weston_surface *
3994weston_surface_get_main_surface(struct weston_surface *surface)
3995{
3996 struct weston_subsurface *sub;
3997
3998 while (surface && (sub = weston_surface_to_subsurface(surface)))
3999 surface = sub->parent;
4000
4001 return surface;
4002}
4003
Pekka Paalanen50b67472014-10-01 15:02:41 +03004004WL_EXPORT int
4005weston_surface_set_role(struct weston_surface *surface,
4006 const char *role_name,
4007 struct wl_resource *error_resource,
4008 uint32_t error_code)
4009{
4010 assert(role_name);
4011
4012 if (surface->role_name == NULL ||
4013 surface->role_name == role_name ||
4014 strcmp(surface->role_name, role_name) == 0) {
4015 surface->role_name = role_name;
4016
4017 return 0;
4018 }
4019
4020 wl_resource_post_error(error_resource, error_code,
4021 "Cannot assign role %s to wl_surface@%d,"
4022 " already has role %s\n",
4023 role_name,
4024 wl_resource_get_id(surface->resource),
4025 surface->role_name);
4026 return -1;
4027}
4028
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004029WL_EXPORT const char *
4030weston_surface_get_role(struct weston_surface *surface)
4031{
4032 return surface->role_name;
4033}
4034
Pekka Paalanen8274d902014-08-06 19:36:51 +03004035WL_EXPORT void
4036weston_surface_set_label_func(struct weston_surface *surface,
4037 int (*desc)(struct weston_surface *,
4038 char *, size_t))
4039{
4040 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02004041 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004042}
4043
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004044/** Get the size of surface contents
4045 *
4046 * \param surface The surface to query.
4047 * \param width Returns the width of raw contents.
4048 * \param height Returns the height of raw contents.
4049 *
4050 * Retrieves the raw surface content size in pixels for the given surface.
4051 * This is the whole content size in buffer pixels. If the surface
4052 * has no content or the renderer does not implement this feature,
4053 * zeroes are returned.
4054 *
4055 * This function is used to determine the buffer size needed for
4056 * a weston_surface_copy_content() call.
4057 */
4058WL_EXPORT void
4059weston_surface_get_content_size(struct weston_surface *surface,
4060 int *width, int *height)
4061{
4062 struct weston_renderer *rer = surface->compositor->renderer;
4063
4064 if (!rer->surface_get_content_size) {
4065 *width = 0;
4066 *height = 0;
4067 return;
4068 }
4069
4070 rer->surface_get_content_size(surface, width, height);
4071}
4072
Quentin Glidic248dd102016-08-12 10:41:34 +02004073/** Get the bounding box of a surface and its subsurfaces
4074 *
4075 * \param surface The surface to query.
4076 * \return The bounding box relative to the surface origin.
4077 *
4078 */
4079WL_EXPORT struct weston_geometry
4080weston_surface_get_bounding_box(struct weston_surface *surface)
4081{
4082 pixman_region32_t region;
4083 pixman_box32_t *box;
4084 struct weston_subsurface *subsurface;
4085
4086 pixman_region32_init_rect(&region,
4087 0, 0,
4088 surface->width, surface->height);
4089
4090 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4091 pixman_region32_union_rect(&region, &region,
4092 subsurface->position.x,
4093 subsurface->position.y,
4094 subsurface->surface->width,
4095 subsurface->surface->height);
4096
4097 box = pixman_region32_extents(&region);
4098 struct weston_geometry geometry = {
4099 .x = box->x1,
4100 .y = box->y1,
4101 .width = box->x2 - box->x1,
4102 .height = box->y2 - box->y1,
4103 };
4104
4105 pixman_region32_fini(&region);
4106
4107 return geometry;
4108}
4109
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004110/** Copy surface contents to system memory.
4111 *
4112 * \param surface The surface to copy from.
4113 * \param target Pointer to the target memory buffer.
4114 * \param size Size of the target buffer in bytes.
4115 * \param src_x X location on contents to copy from.
4116 * \param src_y Y location on contents to copy from.
4117 * \param width Width in pixels of the area to copy.
4118 * \param height Height in pixels of the area to copy.
4119 * \return 0 for success, -1 for failure.
4120 *
4121 * Surface contents are maintained by the renderer. They can be in a
4122 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4123 * else.
4124 *
4125 * Surface contents are copied into memory pointed to by target,
4126 * which has size bytes of space available. The target memory
4127 * may be larger than needed, but being smaller returns an error.
4128 * The extra bytes in target may or may not be written; their content is
4129 * unspecified. Size must be large enough to hold the image.
4130 *
4131 * The image in the target memory will be arranged in rows from
4132 * top to bottom, and pixels on a row from left to right. The pixel
4133 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4134 * width * 4.
4135 *
4136 * Parameters src_x and src_y define the upper-left corner in buffer
4137 * coordinates (pixels) to copy from. Parameters width and height
4138 * define the size of the area to copy in pixels.
4139 *
4140 * The rectangle defined by src_x, src_y, width, height must fit in
4141 * the surface contents. Otherwise an error is returned.
4142 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004143 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004144 * needed target buffer size and rectangle limits.
4145 *
4146 * CURRENT IMPLEMENTATION RESTRICTIONS:
4147 * - the machine must be little-endian due to Pixman formats.
4148 *
4149 * NOTE: Pixman formats are premultiplied.
4150 */
4151WL_EXPORT int
4152weston_surface_copy_content(struct weston_surface *surface,
4153 void *target, size_t size,
4154 int src_x, int src_y,
4155 int width, int height)
4156{
4157 struct weston_renderer *rer = surface->compositor->renderer;
4158 int cw, ch;
4159 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4160
4161 if (!rer->surface_copy_content)
4162 return -1;
4163
4164 weston_surface_get_content_size(surface, &cw, &ch);
4165
4166 if (src_x < 0 || src_y < 0)
4167 return -1;
4168
4169 if (width <= 0 || height <= 0)
4170 return -1;
4171
4172 if (src_x + width > cw || src_y + height > ch)
4173 return -1;
4174
4175 if (width * bytespp * height > size)
4176 return -1;
4177
4178 return rer->surface_copy_content(surface, target, size,
4179 src_x, src_y, width, height);
4180}
4181
Pekka Paalanene67858b2013-04-25 13:57:42 +03004182static void
4183subsurface_set_position(struct wl_client *client,
4184 struct wl_resource *resource, int32_t x, int32_t y)
4185{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004186 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004187
4188 if (!sub)
4189 return;
4190
4191 sub->position.x = x;
4192 sub->position.y = y;
4193 sub->position.set = 1;
4194}
4195
4196static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004197subsurface_find_sibling(struct weston_subsurface *sub,
4198 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004199{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004200 struct weston_surface *parent = sub->parent;
4201 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004202
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004203 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4204 if (sibling->surface == surface && sibling != sub)
4205 return sibling;
4206 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004207
4208 return NULL;
4209}
4210
4211static struct weston_subsurface *
4212subsurface_sibling_check(struct weston_subsurface *sub,
4213 struct weston_surface *surface,
4214 const char *request)
4215{
4216 struct weston_subsurface *sibling;
4217
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004218 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004219 if (!sibling) {
4220 wl_resource_post_error(sub->resource,
4221 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4222 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004223 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004224 return NULL;
4225 }
4226
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004227 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004228
4229 return sibling;
4230}
4231
4232static void
4233subsurface_place_above(struct wl_client *client,
4234 struct wl_resource *resource,
4235 struct wl_resource *sibling_resource)
4236{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004237 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004238 struct weston_surface *surface =
4239 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004240 struct weston_subsurface *sibling;
4241
4242 if (!sub)
4243 return;
4244
4245 sibling = subsurface_sibling_check(sub, surface, "place_above");
4246 if (!sibling)
4247 return;
4248
4249 wl_list_remove(&sub->parent_link_pending);
4250 wl_list_insert(sibling->parent_link_pending.prev,
4251 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004252
4253 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004254}
4255
4256static void
4257subsurface_place_below(struct wl_client *client,
4258 struct wl_resource *resource,
4259 struct wl_resource *sibling_resource)
4260{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004261 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004262 struct weston_surface *surface =
4263 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004264 struct weston_subsurface *sibling;
4265
4266 if (!sub)
4267 return;
4268
4269 sibling = subsurface_sibling_check(sub, surface, "place_below");
4270 if (!sibling)
4271 return;
4272
4273 wl_list_remove(&sub->parent_link_pending);
4274 wl_list_insert(&sibling->parent_link_pending,
4275 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004276
4277 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004278}
4279
4280static void
4281subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4282{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004283 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004284
4285 if (sub)
4286 sub->synchronized = 1;
4287}
4288
4289static void
4290subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4291{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004292 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004293
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004294 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004295 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004296
4297 /* If sub became effectively desynchronized, flush. */
4298 if (!weston_subsurface_is_synchronized(sub))
4299 weston_subsurface_synchronized_commit(sub);
4300 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004301}
4302
4303static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004304weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4305{
4306 wl_list_remove(&sub->parent_link);
4307 wl_list_remove(&sub->parent_link_pending);
4308 wl_list_remove(&sub->parent_destroy_listener.link);
4309 sub->parent = NULL;
4310}
4311
4312static void
4313weston_subsurface_destroy(struct weston_subsurface *sub);
4314
4315static void
4316subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4317{
4318 struct weston_subsurface *sub =
4319 container_of(listener, struct weston_subsurface,
4320 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004321 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004322
4323 /* The protocol object (wl_resource) is left inert. */
4324 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004325 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004326
4327 weston_subsurface_destroy(sub);
4328}
4329
4330static void
4331subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4332{
4333 struct weston_subsurface *sub =
4334 container_of(listener, struct weston_subsurface,
4335 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004336 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004337 assert(sub->surface != sub->parent);
4338
4339 if (weston_surface_is_mapped(sub->surface))
4340 weston_surface_unmap(sub->surface);
4341
4342 weston_subsurface_unlink_parent(sub);
4343}
4344
4345static void
4346subsurface_resource_destroy(struct wl_resource *resource)
4347{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004348 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004349
4350 if (sub)
4351 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004352}
4353
4354static void
4355subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4356{
4357 wl_resource_destroy(resource);
4358}
4359
4360static void
4361weston_subsurface_link_parent(struct weston_subsurface *sub,
4362 struct weston_surface *parent)
4363{
4364 sub->parent = parent;
4365 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004366 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004367 &sub->parent_destroy_listener);
4368
4369 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4370 wl_list_insert(&parent->subsurface_list_pending,
4371 &sub->parent_link_pending);
4372}
4373
4374static void
4375weston_subsurface_link_surface(struct weston_subsurface *sub,
4376 struct weston_surface *surface)
4377{
4378 sub->surface = surface;
4379 sub->surface_destroy_listener.notify =
4380 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004381 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004382 &sub->surface_destroy_listener);
4383}
4384
4385static void
4386weston_subsurface_destroy(struct weston_subsurface *sub)
4387{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004388 struct weston_view *view, *next;
4389
Pekka Paalanene67858b2013-04-25 13:57:42 +03004390 assert(sub->surface);
4391
4392 if (sub->resource) {
4393 assert(weston_surface_to_subsurface(sub->surface) == sub);
4394 assert(sub->parent_destroy_listener.notify ==
4395 subsurface_handle_parent_destroy);
4396
George Kiagiadakised04d382014-06-13 18:10:26 +02004397 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4398 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004399 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004400 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004401
Pekka Paalanene67858b2013-04-25 13:57:42 +03004402 if (sub->parent)
4403 weston_subsurface_unlink_parent(sub);
4404
Jason Ekstrand7b982072014-05-20 14:33:03 -05004405 weston_surface_state_fini(&sub->cached);
4406 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004407
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004408 sub->surface->committed = NULL;
4409 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004410 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004411 } else {
4412 /* the dummy weston_subsurface for the parent itself */
4413 assert(sub->parent_destroy_listener.notify == NULL);
4414 wl_list_remove(&sub->parent_link);
4415 wl_list_remove(&sub->parent_link_pending);
4416 }
4417
4418 wl_list_remove(&sub->surface_destroy_listener.link);
4419 free(sub);
4420}
4421
4422static const struct wl_subsurface_interface subsurface_implementation = {
4423 subsurface_destroy,
4424 subsurface_set_position,
4425 subsurface_place_above,
4426 subsurface_place_below,
4427 subsurface_set_sync,
4428 subsurface_set_desync
4429};
4430
4431static struct weston_subsurface *
4432weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4433 struct weston_surface *parent)
4434{
4435 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004436 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004437
Bryce Harringtonde16d892014-11-20 22:21:57 -08004438 sub = zalloc(sizeof *sub);
4439 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004440 return NULL;
4441
Jason Ekstranda7af7042013-10-12 22:38:11 -05004442 wl_list_init(&sub->unused_views);
4443
Jason Ekstranda85118c2013-06-27 20:17:02 -05004444 sub->resource =
4445 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004446 if (!sub->resource) {
4447 free(sub);
4448 return NULL;
4449 }
4450
Jason Ekstranda85118c2013-06-27 20:17:02 -05004451 wl_resource_set_implementation(sub->resource,
4452 &subsurface_implementation,
4453 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004454 weston_subsurface_link_surface(sub, surface);
4455 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004456 weston_surface_state_init(&sub->cached);
4457 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004458 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004459
4460 return sub;
4461}
4462
4463/* Create a dummy subsurface for having the parent itself in its
4464 * sub-surface lists. Makes stacking order manipulation easy.
4465 */
4466static struct weston_subsurface *
4467weston_subsurface_create_for_parent(struct weston_surface *parent)
4468{
4469 struct weston_subsurface *sub;
4470
Bryce Harringtonde16d892014-11-20 22:21:57 -08004471 sub = zalloc(sizeof *sub);
4472 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004473 return NULL;
4474
4475 weston_subsurface_link_surface(sub, parent);
4476 sub->parent = parent;
4477 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4478 wl_list_insert(&parent->subsurface_list_pending,
4479 &sub->parent_link_pending);
4480
4481 return sub;
4482}
4483
4484static void
4485subcompositor_get_subsurface(struct wl_client *client,
4486 struct wl_resource *resource,
4487 uint32_t id,
4488 struct wl_resource *surface_resource,
4489 struct wl_resource *parent_resource)
4490{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004491 struct weston_surface *surface =
4492 wl_resource_get_user_data(surface_resource);
4493 struct weston_surface *parent =
4494 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004495 struct weston_subsurface *sub;
4496 static const char where[] = "get_subsurface: wl_subsurface@";
4497
4498 if (surface == parent) {
4499 wl_resource_post_error(resource,
4500 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4501 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004502 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004503 return;
4504 }
4505
4506 if (weston_surface_to_subsurface(surface)) {
4507 wl_resource_post_error(resource,
4508 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4509 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004510 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004511 return;
4512 }
4513
Pekka Paalanen50b67472014-10-01 15:02:41 +03004514 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4515 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004516 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004517
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004518 if (weston_surface_get_main_surface(parent) == surface) {
4519 wl_resource_post_error(resource,
4520 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4521 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004522 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004523 return;
4524 }
4525
Pekka Paalanene67858b2013-04-25 13:57:42 +03004526 /* make sure the parent is in its own list */
4527 if (wl_list_empty(&parent->subsurface_list)) {
4528 if (!weston_subsurface_create_for_parent(parent)) {
4529 wl_resource_post_no_memory(resource);
4530 return;
4531 }
4532 }
4533
4534 sub = weston_subsurface_create(id, surface, parent);
4535 if (!sub) {
4536 wl_resource_post_no_memory(resource);
4537 return;
4538 }
4539
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004540 surface->committed = subsurface_committed;
4541 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004542 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004543}
4544
4545static void
4546subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4547{
4548 wl_resource_destroy(resource);
4549}
4550
4551static const struct wl_subcompositor_interface subcompositor_interface = {
4552 subcompositor_destroy,
4553 subcompositor_get_subsurface
4554};
4555
4556static void
4557bind_subcompositor(struct wl_client *client,
4558 void *data, uint32_t version, uint32_t id)
4559{
4560 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004561 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004562
Jason Ekstranda85118c2013-06-27 20:17:02 -05004563 resource =
4564 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004565 if (resource == NULL) {
4566 wl_client_post_no_memory(client);
4567 return;
4568 }
4569 wl_resource_set_implementation(resource, &subcompositor_interface,
4570 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004571}
4572
Bryce Harrington0795ece2016-08-30 12:04:26 -07004573/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004574 *
4575 * \param compositor The compositor instance
4576 * \param state The DPMS state the outputs will be set to
4577 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004578static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004579weston_compositor_dpms(struct weston_compositor *compositor,
4580 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004581{
4582 struct weston_output *output;
4583
Bryce Harrington08976ac2016-08-30 12:05:16 -07004584 wl_list_for_each(output, &compositor->output_list, link)
4585 if (output->set_dpms)
4586 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004587}
4588
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004589/** Restores the compositor to active status
4590 *
4591 * \param compositor The compositor instance
4592 *
4593 * If the compositor was in a sleeping mode, all outputs are powered
4594 * back on via DPMS. Otherwise if the compositor was inactive
4595 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4596 * signal will fire.
4597 *
4598 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004599 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004600 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004601WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004602weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004603{
Neil Roberts8b62e202013-09-30 13:14:47 +01004604 uint32_t old_state = compositor->state;
4605
4606 /* The state needs to be changed before emitting the wake
4607 * signal because that may try to schedule a repaint which
4608 * will not work if the compositor is still sleeping */
4609 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4610
4611 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004612 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004613 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004614 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004615 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004616 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004617 /* fall through */
4618 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004619 wl_event_source_timer_update(compositor->idle_source,
4620 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004621 }
4622}
4623
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004624/** Turns off rendering and frame events for the compositor.
4625 *
4626 * \param compositor The compositor instance
4627 *
4628 * This is used for example to prevent further rendering while the
4629 * compositor is shutting down.
4630 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004631 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004632 *
4633 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004634 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004635WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004636weston_compositor_offscreen(struct weston_compositor *compositor)
4637{
4638 switch (compositor->state) {
4639 case WESTON_COMPOSITOR_OFFSCREEN:
4640 return;
4641 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004642 default:
4643 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4644 wl_event_source_timer_update(compositor->idle_source, 0);
4645 }
4646}
4647
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004648/** Powers down all attached output devices
4649 *
4650 * \param compositor The compositor instance
4651 *
4652 * Causes rendering to the outputs to cease, and no frame events to be
4653 * sent. Only powers down the outputs if the compositor is not already
4654 * in sleep mode.
4655 *
4656 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004657 *
4658 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004659 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004660WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004661weston_compositor_sleep(struct weston_compositor *compositor)
4662{
4663 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4664 return;
4665
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004666 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004667 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4668 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4669}
4670
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004671/** Sets compositor to idle mode
4672 *
4673 * \param data The compositor instance
4674 *
4675 * This is called when the idle timer fires. Once the compositor is in
4676 * idle mode it requires a wake action (e.g. via
4677 * weston_compositor_wake()) to restore it. The compositor's
4678 * idle_signal will be triggered when the idle event occurs.
4679 *
4680 * Idleness can be inhibited by setting the compositor's idle_inhibit
4681 * property.
4682 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004683static int
4684idle_handler(void *data)
4685{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004686 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004687
4688 if (compositor->idle_inhibit)
4689 return 1;
4690
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004691 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004692 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004693
4694 return 1;
4695}
4696
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004697WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004698weston_plane_init(struct weston_plane *plane,
4699 struct weston_compositor *ec,
4700 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004701{
4702 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004703 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004704 plane->x = x;
4705 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004706 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004707
4708 /* Init the link so that the call to wl_list_remove() when releasing
4709 * the plane without ever stacking doesn't lead to a crash */
4710 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004711}
4712
4713WL_EXPORT void
4714weston_plane_release(struct weston_plane *plane)
4715{
Xiong Zhang97116532013-10-23 13:58:31 +08004716 struct weston_view *view;
4717
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004718 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004719 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004720
Xiong Zhang97116532013-10-23 13:58:31 +08004721 wl_list_for_each(view, &plane->compositor->view_list, link) {
4722 if (view->plane == plane)
4723 view->plane = NULL;
4724 }
4725
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004726 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004727}
4728
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004729/** weston_compositor_stack_plane
4730 * \ingroup compositor
4731 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004732WL_EXPORT void
4733weston_compositor_stack_plane(struct weston_compositor *ec,
4734 struct weston_plane *plane,
4735 struct weston_plane *above)
4736{
4737 if (above)
4738 wl_list_insert(above->link.prev, &plane->link);
4739 else
4740 wl_list_insert(&ec->plane_list, &plane->link);
4741}
4742
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004743static void
4744output_release(struct wl_client *client, struct wl_resource *resource)
4745{
4746 wl_resource_destroy(resource);
4747}
4748
4749static const struct wl_output_interface output_interface = {
4750 output_release,
4751};
4752
4753
Casey Dahlin9074db52012-04-19 22:50:09 -04004754static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004755{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004756 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004757}
4758
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004759static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004760bind_output(struct wl_client *client,
4761 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004762{
Pekka Paalanen05347622017-03-27 12:24:34 +03004763 struct weston_head *head = data;
4764 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004765 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004766 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004767
Jason Ekstranda85118c2013-06-27 20:17:02 -05004768 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004769 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004770 if (resource == NULL) {
4771 wl_client_post_no_memory(client);
4772 return;
4773 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004774
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004775 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004776 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004777 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004778
Pekka Paalanen05347622017-03-27 12:24:34 +03004779 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004780 wl_output_send_geometry(resource,
4781 output->x,
4782 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004783 head->mm_width,
4784 head->mm_height,
4785 head->subpixel,
4786 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004787 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004788 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004789 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004790 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004791
4792 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004793 wl_output_send_mode(resource,
4794 mode->flags,
4795 mode->width,
4796 mode->height,
4797 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004798 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004799
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004800 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004801 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004802}
4803
Pekka Paalanendcac3512017-12-08 14:13:34 +02004804static void
4805weston_head_add_global(struct weston_head *head)
4806{
4807 head->global = wl_global_create(head->compositor->wl_display,
4808 &wl_output_interface, 3,
4809 head, bind_output);
4810}
4811
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004812/** Remove the global wl_output protocol object
4813 *
4814 * \param head The head whose global to remove.
4815 *
4816 * Also orphans the wl_resources for this head (wl_output).
4817 */
4818static void
4819weston_head_remove_global(struct weston_head *head)
4820{
4821 struct wl_resource *resource, *tmp;
4822
4823 if (head->global)
4824 wl_global_destroy(head->global);
4825 head->global = NULL;
4826
4827 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4828 unbind_resource(resource);
4829 wl_resource_set_destructor(resource, NULL);
4830 wl_resource_set_user_data(resource, NULL);
4831 }
Roman Gilge97391c2019-03-29 13:01:06 +01004832
4833 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
4834 /* It's sufficient to unset the destructor, then the list elements
4835 * won't be accessed.
4836 */
4837 wl_resource_set_destructor(resource, NULL);
4838 }
4839 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004840}
4841
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004842/** Get the backing object of wl_output
4843 *
4844 * \param resource A wl_output protocol object.
4845 * \return The backing object (user data) of a wl_resource representing a
4846 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004847 *
4848 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004849 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004850WL_EXPORT struct weston_head *
4851weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004852{
4853 assert(wl_resource_instance_of(resource, &wl_output_interface,
4854 &output_interface));
4855
4856 return wl_resource_get_user_data(resource);
4857}
4858
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004859/** Initialize a pre-allocated weston_head
4860 *
4861 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004862 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004863 *
4864 * The head will be safe to attach, detach and release.
4865 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004866 * The name is used in logs, and can be used by compositors as a configuration
4867 * identifier.
4868 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004869 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004870 * \internal
4871 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004872WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004873weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004874{
4875 /* Add some (in)sane defaults which can be used
4876 * for checking if an output was properly configured
4877 */
4878 memset(head, 0, sizeof *head);
4879
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004880 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004881 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004882 wl_list_init(&head->output_link);
4883 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01004884 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004885 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05304886 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004887}
4888
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004889/** Send output heads changed signal
4890 *
4891 * \param output The output that changed.
4892 *
4893 * Notify that the enabled output gained and/or lost heads, or that the
4894 * associated heads may have changed their connection status. This does not
4895 * include cases where the output becomes enabled or disabled. The registered
4896 * callbacks are called after the change has successfully happened.
4897 *
4898 * If connection status change causes the compositor to attach or detach a head
4899 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03004900 *
4901 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004902 */
4903static void
4904weston_output_emit_heads_changed(struct weston_output *output)
4905{
4906 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4907 output);
4908}
4909
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004910/** Idle task for emitting heads_changed_signal */
4911static void
4912weston_compositor_call_heads_changed(void *data)
4913{
4914 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004915 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004916
4917 compositor->heads_changed_source = NULL;
4918
4919 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004920
4921 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4922 if (head->output && head->output->enabled)
4923 weston_output_emit_heads_changed(head->output);
4924 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004925}
4926
4927/** Schedule a call on idle to heads_changed callback
4928 *
4929 * \param compositor The Compositor.
4930 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004931 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004932 * \internal
4933 */
4934static void
4935weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4936{
4937 struct wl_event_loop *loop;
4938
4939 if (compositor->heads_changed_source)
4940 return;
4941
4942 loop = wl_display_get_event_loop(compositor->wl_display);
4943 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4944 weston_compositor_call_heads_changed, compositor);
4945}
4946
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004947/** Register a new head
4948 *
4949 * \param compositor The compositor.
4950 * \param head The head to register, must not be already registered.
4951 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004952 * This signals the core that a new head has become available, leading to
4953 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004954 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004955 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004956 * \internal
4957 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004958WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004959weston_compositor_add_head(struct weston_compositor *compositor,
4960 struct weston_head *head)
4961{
4962 assert(wl_list_empty(&head->compositor_link));
4963 assert(head->name);
4964
4965 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4966 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004967 weston_compositor_schedule_heads_changed(compositor);
4968}
4969
4970/** Adds a listener to be called when heads change
4971 *
4972 * \param compositor The compositor.
4973 * \param listener The listener to add.
4974 *
Marius Vlada2dace22019-06-12 16:05:44 +03004975 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004976 *
4977 * The listener function will be called after heads are added or their
4978 * connection status has changed. Several changes may be accumulated into a
4979 * single call. The user is expected to iterate over the existing heads and
4980 * check their statuses to find out what changed.
4981 *
4982 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4983 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004984 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004985 */
4986WL_EXPORT void
4987weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4988 struct wl_listener *listener)
4989{
4990 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004991}
4992
4993/** Iterate over available heads
4994 *
4995 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03004996 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004997 * \return The next available head in the list.
4998 *
4999 * Returns all available heads, regardless of being connected or enabled.
5000 *
5001 * You can iterate over all heads as follows:
5002 * \code
5003 * struct weston_head *head = NULL;
5004 *
5005 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5006 * ...
5007 * }
5008 * \endcode
5009 *
5010 * If you cause \c iter to be removed from the list, you cannot use it to
5011 * continue iterating. Removing any other item is safe.
5012 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005013 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005014 */
5015WL_EXPORT struct weston_head *
5016weston_compositor_iterate_heads(struct weston_compositor *compositor,
5017 struct weston_head *iter)
5018{
5019 struct wl_list *list = &compositor->head_list;
5020 struct wl_list *node;
5021
5022 assert(compositor);
5023 assert(!iter || iter->compositor == compositor);
5024
5025 if (iter)
5026 node = iter->compositor_link.next;
5027 else
5028 node = list->next;
5029
5030 assert(node);
5031 assert(!iter || node != &iter->compositor_link);
5032
5033 if (node == list)
5034 return NULL;
5035
5036 return container_of(node, struct weston_head, compositor_link);
5037}
5038
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005039/** Iterate over attached heads
5040 *
5041 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005042 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005043 * \return The next attached head in the list.
5044 *
5045 * Returns all heads currently attached to the output.
5046 *
5047 * You can iterate over all heads as follows:
5048 * \code
5049 * struct weston_head *head = NULL;
5050 *
5051 * while ((head = weston_output_iterate_heads(output, head))) {
5052 * ...
5053 * }
5054 * \endcode
5055 *
5056 * If you cause \c iter to be removed from the list, you cannot use it to
5057 * continue iterating. Removing any other item is safe.
5058 *
Marius Vlad55d87362019-06-11 01:15:35 +03005059 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005060 */
5061WL_EXPORT struct weston_head *
5062weston_output_iterate_heads(struct weston_output *output,
5063 struct weston_head *iter)
5064{
5065 struct wl_list *list = &output->head_list;
5066 struct wl_list *node;
5067
5068 assert(output);
5069 assert(!iter || iter->output == output);
5070
5071 if (iter)
5072 node = iter->output_link.next;
5073 else
5074 node = list->next;
5075
5076 assert(node);
5077 assert(!iter || node != &iter->output_link);
5078
5079 if (node == list)
5080 return NULL;
5081
5082 return container_of(node, struct weston_head, output_link);
5083}
5084
Pekka Paalanendcac3512017-12-08 14:13:34 +02005085/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005086 *
5087 * \param output The output to attach to.
5088 * \param head The head that is not yet attached.
5089 * \return 0 on success, -1 on failure.
5090 *
5091 * Attaches the given head to the output. All heads of an output are clones
5092 * and share the resolution and timings.
5093 *
5094 * Cloning heads this way uses less resources than creating an output for
5095 * each head, but is not always possible due to environment, driver and hardware
5096 * limitations.
5097 *
5098 * On failure, the head remains unattached. Success of this function does not
5099 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005100 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005101 *
Marius Vlad55d87362019-06-11 01:15:35 +03005102 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005103 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005104WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005105weston_output_attach_head(struct weston_output *output,
5106 struct weston_head *head)
5107{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005108 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005109
5110 if (!wl_list_empty(&head->output_link))
5111 return -1;
5112
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005113 if (output->attach_head) {
5114 if (output->attach_head(output, head) < 0)
5115 return -1;
5116 } else if (!wl_list_empty(&output->head_list)) {
5117 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005118 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005119 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005120
5121 head->output = output;
5122 wl_list_insert(output->head_list.prev, &head->output_link);
5123
Pekka Paalanendcac3512017-12-08 14:13:34 +02005124 if (output->enabled) {
5125 weston_head_add_global(head);
5126
5127 head_names = weston_output_create_heads_string(output);
5128 weston_log("Output '%s' updated to have head(s) %s\n",
5129 output->name, head_names);
5130 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005131
5132 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005133 }
5134
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005135 return 0;
5136}
5137
5138/** Detach a head from its output
5139 *
5140 * \param head The head to detach.
5141 *
5142 * It is safe to detach a non-attached head.
5143 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005144 * If the head is attached to an enabled output and the output will be left
5145 * with no heads, the output will be disabled.
5146 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005147 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005148 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005149 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005150WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005151weston_head_detach(struct weston_head *head)
5152{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005153 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005154 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005155
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005156 wl_list_remove(&head->output_link);
5157 wl_list_init(&head->output_link);
5158 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005159
5160 if (!output)
5161 return;
5162
5163 if (output->detach_head)
5164 output->detach_head(output, head);
5165
5166 if (output->enabled) {
5167 weston_head_remove_global(head);
5168
Pekka Paalanena0106992017-12-08 16:11:17 +02005169 if (wl_list_empty(&output->head_list)) {
5170 weston_log("Output '%s' no heads left, disabling.\n",
5171 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005172 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005173 } else {
5174 head_names = weston_output_create_heads_string(output);
5175 weston_log("Output '%s' updated to have head(s) %s\n",
5176 output->name, head_names);
5177 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005178
5179 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005180 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005181 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005182}
5183
5184/** Destroy a head
5185 *
5186 * \param head The head to be released.
5187 *
5188 * Destroys the head. The caller is responsible for freeing the memory pointed
5189 * to by \c head.
5190 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005191 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005192 * \internal
5193 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005194WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005195weston_head_release(struct weston_head *head)
5196{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005197 wl_signal_emit(&head->destroy_signal, head);
5198
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005199 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005200
5201 free(head->make);
5202 free(head->model);
5203 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005204 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005205
5206 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005207}
5208
Pekka Paalanene19970f2017-08-28 14:11:02 +03005209static void
5210weston_head_set_device_changed(struct weston_head *head)
5211{
5212 head->device_changed = true;
5213
5214 if (head->compositor)
5215 weston_compositor_schedule_heads_changed(head->compositor);
5216}
5217
5218/** String equal comparison with NULLs being equal */
5219static bool
5220str_null_eq(const char *a, const char *b)
5221{
5222 if (!a && !b)
5223 return true;
5224
5225 if (!!a != !!b)
5226 return false;
5227
5228 return strcmp(a, b) == 0;
5229}
5230
Pekka Paalanen01f60212017-03-24 15:39:24 +02005231/** Store monitor make, model and serial number
5232 *
5233 * \param head The head to modify.
5234 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5235 * any string, or NULL for none.
5236 * \param model The monitor model or name, or a made-up string, or NULL for
5237 * none.
5238 * \param serialno The monitor serial number, a made-up string, or NULL for
5239 * none.
5240 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005241 * This may set the device_changed flag.
5242 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005243 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005244 * \internal
5245 */
5246WL_EXPORT void
5247weston_head_set_monitor_strings(struct weston_head *head,
5248 const char *make,
5249 const char *model,
5250 const char *serialno)
5251{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005252 if (str_null_eq(head->make, make) &&
5253 str_null_eq(head->model, model) &&
5254 str_null_eq(head->serial_number, serialno))
5255 return;
5256
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005257 free(head->make);
5258 free(head->model);
5259 free(head->serial_number);
5260
5261 head->make = make ? strdup(make) : NULL;
5262 head->model = model ? strdup(model) : NULL;
5263 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005264
5265 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005266}
5267
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005268/** Store display non-desktop status
5269 *
5270 * \param head The head to modify.
5271 * \param non_desktop Whether the head connects to a non-desktop display.
5272 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005273 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005274 * \internal
5275 */
5276WL_EXPORT void
5277weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5278{
5279 if (head->non_desktop == non_desktop)
5280 return;
5281
5282 head->non_desktop = non_desktop;
5283
5284 weston_head_set_device_changed(head);
5285}
5286
Pekka Paalanen01f60212017-03-24 15:39:24 +02005287/** Store physical image size
5288 *
5289 * \param head The head to modify.
5290 * \param mm_width Image area width in millimeters.
5291 * \param mm_height Image area height in millimeters.
5292 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005293 * This may set the device_changed flag.
5294 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005295 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005296 * \internal
5297 */
5298WL_EXPORT void
5299weston_head_set_physical_size(struct weston_head *head,
5300 int32_t mm_width, int32_t mm_height)
5301{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005302 if (head->mm_width == mm_width &&
5303 head->mm_height == mm_height)
5304 return;
5305
Pekka Paalanen01f60212017-03-24 15:39:24 +02005306 head->mm_width = mm_width;
5307 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005308
5309 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005310}
5311
5312/** Store monitor sub-pixel layout
5313 *
5314 * \param head The head to modify.
5315 * \param sp Sub-pixel layout. The possible values are:
5316 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5317 * - WL_OUTPUT_SUBPIXEL_NONE,
5318 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5319 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5320 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5321 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5322 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005323 * This may set the device_changed flag.
5324 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005325 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005326 * \internal
5327 */
5328WL_EXPORT void
5329weston_head_set_subpixel(struct weston_head *head,
5330 enum wl_output_subpixel sp)
5331{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005332 if (head->subpixel == sp)
5333 return;
5334
Pekka Paalanen01f60212017-03-24 15:39:24 +02005335 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005336
5337 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005338}
5339
5340/** Mark the monitor as internal
5341 *
5342 * This is used for embedded screens, like laptop panels.
5343 *
5344 * \param head The head to mark as internal.
5345 *
5346 * By default a head is external. The type is often inferred from the physical
5347 * connector type.
5348 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005349 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005350 * \internal
5351 */
5352WL_EXPORT void
5353weston_head_set_internal(struct weston_head *head)
5354{
5355 head->connection_internal = true;
5356}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005357
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005358/** Store connector status
5359 *
5360 * \param head The head to modify.
5361 * \param connected Whether the head is connected.
5362 *
5363 * Connectors are created as disconnected. This function can be used to
5364 * set the connector status.
5365 *
5366 * The status should be set to true when a physical connector is connected to
5367 * a video sink device like a monitor and to false when the connector is
5368 * disconnected. For nested backends, the connection status should reflect the
5369 * connection to the parent display server.
5370 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005371 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005372 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005373 *
5374 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005375 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005376 * \internal
5377 */
5378WL_EXPORT void
5379weston_head_set_connection_status(struct weston_head *head, bool connected)
5380{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005381 if (head->connected == connected)
5382 return;
5383
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005384 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005385
Pekka Paalanene19970f2017-08-28 14:11:02 +03005386 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005387}
5388
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305389static void
5390weston_output_compute_protection(struct weston_output *output)
5391{
5392 struct weston_head *head;
5393 enum weston_hdcp_protection op_protection;
5394 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305395 struct weston_compositor *wc = output->compositor;
5396 struct content_protection *cp = wc->content_protection;
5397
5398 if (!cp)
5399 return;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305400
5401 wl_list_for_each(head, &output->head_list, output_link) {
5402 if (!op_protection_valid) {
5403 op_protection = head->current_protection;
5404 op_protection_valid = true;
5405 }
5406 if (head->current_protection < op_protection)
5407 op_protection = head->current_protection;
5408 }
5409
5410 if (!op_protection_valid)
5411 op_protection = WESTON_HDCP_DISABLE;
5412
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305413 if (output->current_protection != op_protection) {
5414 struct wl_event_loop *loop;
5415
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305416 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305417 weston_output_damage(output);
5418 if (cp->surface_protection_update)
5419 return;
5420 loop = wl_display_get_event_loop(wc->wl_display);
5421 cp->surface_protection_update = wl_event_loop_add_idle(loop,
5422 notify_surface_protection_change,
5423 wc);
5424 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305425}
5426
5427WL_EXPORT void
5428weston_head_set_content_protection_status(struct weston_head *head,
5429 enum weston_hdcp_protection status)
5430{
5431 head->current_protection = status;
5432 if (head->output)
5433 weston_output_compute_protection(head->output);
5434}
5435
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005436/** Is the head currently connected?
5437 *
5438 * \param head The head to query.
5439 * \return Connection status.
5440 *
5441 * Returns true if the head is physically connected to a monitor, or in
5442 * case of a nested backend returns true when there is a connection to the
5443 * parent display server.
5444 *
5445 * This is independent from the head being enabled.
5446 *
5447 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005448 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005449 */
5450WL_EXPORT bool
5451weston_head_is_connected(struct weston_head *head)
5452{
5453 return head->connected;
5454}
5455
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005456/** Is the head currently enabled?
5457 *
5458 * \param head The head to query.
5459 * \return Video status.
5460 *
5461 * Returns true if the head is currently transmitting a video stream.
5462 *
5463 * This is independent of the head being connected.
5464 *
5465 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005466 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005467 */
5468WL_EXPORT bool
5469weston_head_is_enabled(struct weston_head *head)
5470{
5471 if (!head->output)
5472 return false;
5473
5474 return head->output->enabled;
5475}
5476
Pekka Paalanene19970f2017-08-28 14:11:02 +03005477/** Has the device information changed?
5478 *
5479 * \param head The head to query.
5480 * \return True if the device information has changed since last reset.
5481 *
5482 * The information about the connected display device, e.g. a monitor, may
5483 * change without being disconnected in between. Changing information
5484 * causes a call to the heads_changed hook.
5485 *
5486 * The information includes make, model, serial number, physical size,
5487 * and sub-pixel type. The connection status is also included.
5488 *
5489 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005490 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005491 */
5492WL_EXPORT bool
5493weston_head_is_device_changed(struct weston_head *head)
5494{
5495 return head->device_changed;
5496}
5497
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005498/** Does the head represent a non-desktop display?
5499 *
5500 * \param head The head to query.
5501 * \return True if the device is a non-desktop display.
5502 *
5503 * Non-desktop heads are not attached to outputs by default.
5504 * This stops weston from extending the desktop onto head mounted displays.
5505 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005506 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005507 */
5508WL_EXPORT bool
5509weston_head_is_non_desktop(struct weston_head *head)
5510{
5511 return head->non_desktop;
5512}
5513
Pekka Paalanene19970f2017-08-28 14:11:02 +03005514/** Acknowledge device information change
5515 *
5516 * \param head The head to acknowledge.
5517 *
5518 * Clears the device changed flag on this head. When a compositor has processed
5519 * device information, it should call this to be able to notice further
5520 * changes.
5521 *
5522 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005523 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005524 */
5525WL_EXPORT void
5526weston_head_reset_device_changed(struct weston_head *head)
5527{
5528 head->device_changed = false;
5529}
5530
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005531/** Get the name of a head
5532 *
5533 * \param head The head to query.
5534 * \return The head's name, not NULL.
5535 *
5536 * The name depends on the backend. The DRM backend uses connector names,
5537 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005538 *
5539 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005540 */
5541WL_EXPORT const char *
5542weston_head_get_name(struct weston_head *head)
5543{
5544 return head->name;
5545}
5546
5547/** Get the output the head is attached to
5548 *
5549 * \param head The head to query.
5550 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005551 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005552 */
5553WL_EXPORT struct weston_output *
5554weston_head_get_output(struct weston_head *head)
5555{
5556 return head->output;
5557}
5558
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005559/** Add destroy callback for a head
5560 *
5561 * \param head The head to watch for.
5562 * \param listener The listener to add. The \c notify member must be set.
5563 *
5564 * Heads may get destroyed for various reasons by the backends. If a head is
5565 * attached to an output, the compositor should listen for head destruction
5566 * and reconfigure or destroy the output if necessary.
5567 *
5568 * The destroy callbacks will be called on weston_head destruction before any
5569 * automatic detaching from an associated weston_output and before any
5570 * weston_head information is lost.
5571 *
5572 * The \c data argument to the notify callback is the weston_head being
5573 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005574 *
5575 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005576 */
5577WL_EXPORT void
5578weston_head_add_destroy_listener(struct weston_head *head,
5579 struct wl_listener *listener)
5580{
5581 wl_signal_add(&head->destroy_signal, listener);
5582}
5583
5584/** Look up destroy listener for a head
5585 *
5586 * \param head The head to query.
5587 * \param notify The notify function used used for the added destroy listener.
5588 * \return The listener, or NULL if not found.
5589 *
5590 * This looks up the previously added destroy listener struct based on the
5591 * notify function it has. The listener can be used to access user data
5592 * through \c container_of().
5593 *
5594 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005595 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005596 */
5597WL_EXPORT struct wl_listener *
5598weston_head_get_destroy_listener(struct weston_head *head,
5599 wl_notify_func_t notify)
5600{
5601 return wl_signal_get(&head->destroy_signal, notify);
5602}
5603
David Fort0de859e2016-05-27 23:22:57 +02005604/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005605static void
David Fort0de859e2016-05-27 23:22:57 +02005606weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5607 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005608{
5609 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005610 bool start_resizing = false;
5611
5612 if (!delta_width)
5613 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005614
5615 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005616 if (output == resized_output) {
5617 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005618 continue;
5619 }
5620
David Fort0de859e2016-05-27 23:22:57 +02005621 if (start_resizing) {
5622 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005623 output->dirty = 1;
5624 }
5625 }
5626}
5627
Pekka Paalanend72bad22017-03-29 17:01:41 +03005628static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005629weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005630{
Scott Moreau850ca422012-05-21 15:21:25 -06005631 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005632
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005633 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005634 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005635
Scott Moreauccbf29d2012-02-22 14:21:41 -07005636 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005637 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005638 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005639 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005640 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005641 weston_matrix_scale(&output->matrix, magnification,
5642 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005643 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005644
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005645 switch (output->transform) {
5646 case WL_OUTPUT_TRANSFORM_FLIPPED:
5647 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5648 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5649 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5650 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5651 weston_matrix_scale(&output->matrix, -1, 1, 1);
5652 break;
5653 }
5654
5655 switch (output->transform) {
5656 default:
5657 case WL_OUTPUT_TRANSFORM_NORMAL:
5658 case WL_OUTPUT_TRANSFORM_FLIPPED:
5659 break;
5660 case WL_OUTPUT_TRANSFORM_90:
5661 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5662 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5663 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5664 break;
5665 case WL_OUTPUT_TRANSFORM_180:
5666 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5667 weston_matrix_translate(&output->matrix,
5668 -output->width, -output->height, 0);
5669 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5670 break;
5671 case WL_OUTPUT_TRANSFORM_270:
5672 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5673 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5674 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5675 break;
5676 }
5677
5678 if (output->current_scale != 1)
5679 weston_matrix_scale(&output->matrix,
5680 output->current_scale,
5681 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005682
Scott Moreauccbf29d2012-02-22 14:21:41 -07005683 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005684
5685 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005686}
5687
Scott Moreau1bad5db2012-08-18 01:04:05 -06005688static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005689weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005690{
5691 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005692 output->native_scale = scale;
5693 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005694
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005695 convert_size_by_transform_scale(&output->width, &output->height,
5696 output->current_mode->width,
5697 output->current_mode->height,
5698 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005699}
5700
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005701static void
5702weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005703{
5704 output->x = x;
5705 output->y = y;
5706
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005707 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005708 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005709
5710 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005711 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005712 output->width,
5713 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005714}
5715
Marius Vlad55d87362019-06-11 01:15:35 +03005716/**
5717 * \ingroup output
5718 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005719WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005720weston_output_move(struct weston_output *output, int x, int y)
5721{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005722 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005723 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005724 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005725
5726 output->move_x = x - output->x;
5727 output->move_y = y - output->y;
5728
5729 if (output->move_x == 0 && output->move_y == 0)
5730 return;
5731
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005732 weston_output_init_geometry(output, x, y);
5733
5734 output->dirty = 1;
5735
5736 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005737 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005738
5739 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005740 wl_list_for_each(head, &output->head_list, output_link) {
5741 wl_resource_for_each(resource, &head->resource_list) {
5742 wl_output_send_geometry(resource,
5743 output->x,
5744 output->y,
5745 head->mm_width,
5746 head->mm_height,
5747 head->subpixel,
5748 head->make,
5749 head->model,
5750 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005751
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005752 ver = wl_resource_get_version(resource);
5753 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5754 wl_output_send_done(resource);
5755 }
Roman Gilge97391c2019-03-29 13:01:06 +01005756
5757 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5758 zxdg_output_v1_send_logical_position(resource,
5759 output->x,
5760 output->y);
5761 zxdg_output_v1_send_done(resource);
5762 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005763 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005764}
5765
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005766/** Signal that a pending output is taken into use.
5767 *
5768 * Removes the output from the pending list and adds it to the compositor's
5769 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005770 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005771 * The output gets an internal ID assigned, and the wl_output global is
5772 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005773 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005774 * \param compositor The compositor instance.
5775 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005776 *
5777 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005778 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005779 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005780static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005781weston_compositor_add_output(struct weston_compositor *compositor,
5782 struct weston_output *output)
5783{
Armin Krezoviće5403842016-08-05 15:28:29 +02005784 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005785 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005786
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005787 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005788
5789 /* Verify we haven't reached the limit of 32 available output IDs */
5790 assert(ffs(~compositor->output_id_pool) > 0);
5791
5792 /* Invert the output id pool and look for the lowest numbered
5793 * switch (the least significant bit). Take that bit's position
5794 * as our ID, and mark it used in the compositor's output_id_pool.
5795 */
5796 output->id = ffs(~compositor->output_id_pool) - 1;
5797 compositor->output_id_pool |= 1u << output->id;
5798
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005799 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005800 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005801 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005802
Pekka Paalanendcac3512017-12-08 14:13:34 +02005803 wl_list_for_each(head, &output->head_list, output_link)
5804 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005805
Giulio Camuffob1147152015-05-06 21:41:57 +03005806 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005807
5808 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5809 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005810}
5811
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005812/** Transform device coordinates into global coordinates
5813 *
Marius Vlada2dace22019-06-12 16:05:44 +03005814 * \param output the weston_output object
5815 * \param[in] device_x X coordinate in device units.
5816 * \param[in] device_y Y coordinate in device units.
5817 * \param[out] x X coordinate in the global space.
5818 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005819 *
Marius Vlada2dace22019-06-12 16:05:44 +03005820 * Transforms coordinates from the device coordinate space (physical pixel
5821 * units) to the global coordinate space (logical pixel units). This takes
5822 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005823 *
Marius Vlad55d87362019-06-11 01:15:35 +03005824 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005825 * \internal
5826 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005827WL_EXPORT void
5828weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005829 double device_x, double device_y,
5830 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005831{
Derek Foreman0f679412014-10-02 13:41:17 -05005832 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005833 device_x,
5834 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005835 0.0,
5836 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005837
Derek Foreman67a18b92015-03-24 11:36:14 -05005838 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005839
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005840 *x = p.f[0] / p.f[3];
5841 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005842}
5843
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005844/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005845 *
5846 * \param output The weston_output object that is being removed.
5847 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005848 * The following happens:
5849 *
5850 * - The output assignments of all views in the current scenegraph are
5851 * recomputed.
5852 *
5853 * - Presentation feedback is discarded.
5854 *
5855 * - Compositor is notified that outputs were changed and
5856 * applies the necessary changes to re-layout outputs.
5857 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005858 * - The output is put back in the pending outputs list.
5859 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005860 * - Signal is emitted to notify all users of the weston_output
5861 * object that the output is being destroyed.
5862 *
5863 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005864 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005865 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005866 * - The output's internal ID is released.
5867 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005868 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005869 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005870 */
5871static void
5872weston_compositor_remove_output(struct weston_output *output)
5873{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005874 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005875 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005876 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005877
5878 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005879 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005880
Pekka Paalanenbccda712017-03-29 16:16:04 +03005881 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005882 if (view->output_mask & (1u << output->id))
5883 weston_view_assign_output(view);
5884 }
5885
5886 weston_presentation_feedback_discard_list(&output->feedback_list);
5887
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005888 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005889
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005890 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005891 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005892 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005893
Pekka Paalanenbccda712017-03-29 16:16:04 +03005894 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005895 wl_signal_emit(&output->destroy_signal, output);
5896
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005897 wl_list_for_each(head, &output->head_list, output_link)
5898 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005899
5900 compositor->output_id_pool &= ~(1u << output->id);
5901 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005902}
5903
5904/** Sets the output scale for a given output.
5905 *
5906 * \param output The weston_output object that the scale is set for.
5907 * \param scale Scale factor for the given output.
5908 *
5909 * It only supports setting scale for an output that
5910 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005911 *
Marius Vlad55d87362019-06-11 01:15:35 +03005912 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005913 */
5914WL_EXPORT void
5915weston_output_set_scale(struct weston_output *output,
5916 int32_t scale)
5917{
5918 /* We can only set scale on a disabled output */
5919 assert(!output->enabled);
5920
5921 /* We only want to set scale once */
5922 assert(!output->scale);
5923
5924 output->scale = scale;
5925}
5926
5927/** Sets the output transform for a given output.
5928 *
5929 * \param output The weston_output object that the transform is set for.
5930 * \param transform Transform value for the given output.
5931 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005932 * Refer to wl_output::transform section located at
5933 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5934 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005935 *
Marius Vlad55d87362019-06-11 01:15:35 +03005936 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005937 */
5938WL_EXPORT void
5939weston_output_set_transform(struct weston_output *output,
5940 uint32_t transform)
5941{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005942 struct weston_pointer_motion_event ev;
5943 struct wl_resource *resource;
5944 struct weston_seat *seat;
5945 pixman_region32_t old_region;
5946 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005947 struct weston_head *head;
5948 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005949
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005950 if (!output->enabled && output->transform == UINT32_MAX) {
5951 output->transform = transform;
5952 return;
5953 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005954
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005955 weston_output_transform_scale_init(output, transform, output->scale);
5956
5957 pixman_region32_init(&old_region);
5958 pixman_region32_copy(&old_region, &output->region);
5959
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005960 weston_output_init_geometry(output, output->x, output->y);
5961
5962 output->dirty = 1;
5963
5964 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005965 wl_list_for_each(head, &output->head_list, output_link) {
5966 wl_resource_for_each(resource, &head->resource_list) {
5967 wl_output_send_geometry(resource,
5968 output->x,
5969 output->y,
5970 head->mm_width,
5971 head->mm_height,
5972 head->subpixel,
5973 head->make,
5974 head->model,
5975 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005976
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005977 ver = wl_resource_get_version(resource);
5978 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5979 wl_output_send_done(resource);
5980 }
Roman Gilge97391c2019-03-29 13:01:06 +01005981 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5982 zxdg_output_v1_send_logical_position(resource,
5983 output->x,
5984 output->y);
5985 zxdg_output_v1_send_logical_size(resource,
5986 output->width,
5987 output->height);
5988 zxdg_output_v1_send_done(resource);
5989 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005990 }
5991
5992 /* we must ensure that pointers are inside output, otherwise they disappear */
5993 mid_x = output->x + output->width / 2;
5994 mid_y = output->y + output->height / 2;
5995
5996 ev.mask = WESTON_POINTER_MOTION_ABS;
5997 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5998 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5999
6000 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6001 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6002
6003 if (pointer && pixman_region32_contains_point(&old_region,
6004 wl_fixed_to_int(pointer->x),
6005 wl_fixed_to_int(pointer->y),
6006 NULL))
6007 weston_pointer_move(pointer, &ev);
6008 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006009}
6010
6011/** Initializes a weston_output object with enough data so
6012 ** an output can be configured.
6013 *
6014 * \param output The weston_output object to initialize
6015 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006016 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006017 *
6018 * Sets initial values for fields that are expected to be
6019 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006020 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006021 * The name is used in logs, and can be used by compositors as a configuration
6022 * identifier.
6023 *
Marius Vlad55d87362019-06-11 01:15:35 +03006024 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006025 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006026 */
6027WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006028weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006029 struct weston_compositor *compositor,
6030 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006031{
6032 output->compositor = compositor;
6033 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006034 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006035 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006036 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006037 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306038 output->desired_protection = WESTON_HDCP_DISABLE;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006039
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006040 wl_list_init(&output->head_list);
6041
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006042 /* Add some (in)sane defaults which can be used
6043 * for checking if an output was properly configured
6044 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006045 output->scale = 0;
6046 /* Can't use -1 on uint32_t and 0 is valid enum value */
6047 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006048
6049 pixman_region32_init(&output->previous_damage);
6050 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006051 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006052}
6053
6054/** Adds weston_output object to pending output list.
6055 *
6056 * \param output The weston_output object to add
6057 * \param compositor The compositor instance.
6058 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006059 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006060 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006061 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006062 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006063 */
6064WL_EXPORT void
6065weston_compositor_add_pending_output(struct weston_output *output,
6066 struct weston_compositor *compositor)
6067{
Pekka Paalanene952a012017-03-29 17:14:00 +03006068 assert(output->disable);
6069 assert(output->enable);
6070
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006071 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006072 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006073}
6074
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006075/** Create a string with the attached heads' names.
6076 *
6077 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006078 *
6079 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006080 */
6081static char *
6082weston_output_create_heads_string(struct weston_output *output)
6083{
6084 FILE *fp;
6085 char *str = NULL;
6086 size_t size = 0;
6087 struct weston_head *head;
6088 const char *sep = "";
6089
6090 fp = open_memstream(&str, &size);
6091 if (!fp)
6092 return NULL;
6093
6094 wl_list_for_each(head, &output->head_list, output_link) {
6095 fprintf(fp, "%s%s", sep, head->name);
6096 sep = ", ";
6097 }
6098 fclose(fp);
6099
6100 return str;
6101}
6102
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006103/** Constructs a weston_output object that can be used by the compositor.
6104 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006105 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006106 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006107 *
6108 * Output coordinates are calculated and each new output is by default
6109 * assigned to the right of previous one.
6110 *
6111 * Sets up the transformation, zoom, and geometry of the output using
6112 * the properties that need to be configured by the compositor.
6113 *
6114 * Establishes a repaint timer for the output with the relevant display
6115 * object's event loop. See output_repaint_timer_handler().
6116 *
6117 * The output is assigned an ID. Weston can support up to 32 distinct
6118 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6119 * is referred to and used to find the first available ID number, and
6120 * then this ID is marked as used in output_id_pool.
6121 *
6122 * The output is also assigned a Wayland global with the wl_output
6123 * external interface.
6124 *
6125 * Backend specific function is called to set up the output output.
6126 *
6127 * Output is added to the compositor's output list
6128 *
6129 * If the backend specific function fails, the weston_output object
6130 * is returned to a state it was before calling this function and
6131 * is added to the compositor's pending_output_list in case it needs
6132 * to be reconfigured or just so it can be destroyed at shutdown.
6133 *
6134 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006135 *
6136 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006137 */
6138WL_EXPORT int
6139weston_output_enable(struct weston_output *output)
6140{
Armin Krezović782f5df2016-09-30 14:11:11 +02006141 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006142 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006143 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006144 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006145 int x = 0, y = 0;
6146
Pekka Paalanencc201e42017-03-30 15:11:25 +03006147 if (output->enabled) {
6148 weston_log("Error: attempt to enable an enabled output '%s'\n",
6149 output->name);
6150 return -1;
6151 }
6152
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006153 if (wl_list_empty(&output->head_list)) {
6154 weston_log("Error: cannot enable output '%s' without heads.\n",
6155 output->name);
6156 return -1;
6157 }
6158
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006159 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6160 weston_log("Error: no video mode for output '%s'.\n",
6161 output->name);
6162 return -1;
6163 }
6164
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006165 wl_list_for_each(head, &output->head_list, output_link) {
6166 assert(head->make);
6167 assert(head->model);
6168 }
6169
Armin Krezović782f5df2016-09-30 14:11:11 +02006170 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006171 struct weston_output, link);
6172
Armin Krezović782f5df2016-09-30 14:11:11 +02006173 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006174 x = iterator->x + iterator->width;
6175
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006176 /* Make sure the scale is set up */
6177 assert(output->scale);
6178
6179 /* Make sure we have a transform set */
6180 assert(output->transform != UINT32_MAX);
6181
Armin Krezović782f5df2016-09-30 14:11:11 +02006182 output->x = x;
6183 output->y = y;
6184 output->dirty = 1;
6185 output->original_scale = output->scale;
6186
6187 weston_output_transform_scale_init(output, output->transform, output->scale);
6188 weston_output_init_zoom(output);
6189
6190 weston_output_init_geometry(output, x, y);
6191 weston_output_damage(output);
6192
6193 wl_signal_init(&output->frame_signal);
6194 wl_signal_init(&output->destroy_signal);
6195 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006196 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006197
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006198 /* Enable the output (set up the crtc or create a
6199 * window representing the output, set up the
6200 * renderer, etc)
6201 */
6202 if (output->enable(output) < 0) {
6203 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006204 return -1;
6205 }
6206
6207 weston_compositor_add_output(output->compositor, output);
6208
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006209 head_names = weston_output_create_heads_string(output);
6210 weston_log("Output '%s' enabled with head(s) %s\n",
6211 output->name, head_names);
6212 free(head_names);
6213
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006214 return 0;
6215}
6216
6217/** Converts a weston_output object to a pending output state, so it
6218 ** can be configured again or destroyed.
6219 *
6220 * \param output The weston_output object that needs to be disabled.
6221 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006222 * Calls a backend specific function to disable an output, in case
6223 * such function exists.
6224 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006225 * The backend specific disable function may choose to postpone the disabling
6226 * by returning a negative value, in which case this function returns early.
6227 * In that case the backend will guarantee the output will be disabled soon
6228 * by the backend calling this function again. One must not attempt to re-enable
6229 * the output until that happens.
6230 *
6231 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006232 * from weston's output_list (see weston_compositor_remove_output())
6233 * and is returned to a state it was before weston_output_enable()
6234 * was ran (see weston_output_enable_undo()).
6235 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006236 * See weston_output_init() for more information on the
6237 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006238 *
6239 * If the output has never been enabled yet, this function can still be
6240 * called to ensure that the output is actually turned off rather than left
6241 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006242 *
6243 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006244 */
6245WL_EXPORT void
6246weston_output_disable(struct weston_output *output)
6247{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006248 /* Should we rename this? */
6249 output->destroying = 1;
6250
Pekka Paalanenc65df642017-03-29 15:45:46 +03006251 /* Disable is called unconditionally also for not-enabled outputs,
6252 * because at compositor start-up, if there is an output that is
6253 * already on but the compositor wants to turn it off, we have to
6254 * forward the turn-off to the backend so it knows to do it.
6255 * The backend cannot initially turn off everything, because it
6256 * would cause unnecessary mode-sets for all outputs the compositor
6257 * wants to be on.
6258 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006259 if (output->disable(output) < 0)
6260 return;
6261
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006262 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006263 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006264
6265 output->destroying = 0;
6266}
6267
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006268/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006269 *
6270 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006271 *
6272 * If there are new or changed heads, calls the heads_changed hook and
6273 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006274 *
6275 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006276 */
6277WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006278weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006279{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006280 if (compositor->heads_changed_source) {
6281 wl_event_source_remove(compositor->heads_changed_source);
6282 weston_compositor_call_heads_changed(compositor);
6283 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006284}
6285
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006286/** Add destroy callback for an output
6287 *
6288 * \param output The output to watch.
6289 * \param listener The listener to add. The \c notify member must be set.
6290 *
6291 * The listener callback will be called when user destroys an output. This
6292 * may be delayed by a backend in some cases. The main purpose of the
6293 * listener is to allow hooking up custom data to the output. The custom data
6294 * can be fetched via weston_output_get_destroy_listener() followed by
6295 * container_of().
6296 *
6297 * The \c data argument to the notify callback is the weston_output being
6298 * destroyed.
6299 *
6300 * @note This is for the final destruction of an output, not when it gets
6301 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006302 *
6303 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006304 */
6305WL_EXPORT void
6306weston_output_add_destroy_listener(struct weston_output *output,
6307 struct wl_listener *listener)
6308{
6309 wl_signal_add(&output->user_destroy_signal, listener);
6310}
6311
6312/** Look up destroy listener for an output
6313 *
6314 * \param output The output to query.
6315 * \param notify The notify function used used for the added destroy listener.
6316 * \return The listener, or NULL if not found.
6317 *
6318 * This looks up the previously added destroy listener struct based on the
6319 * notify function it has. The listener can be used to access user data
6320 * through \c container_of().
6321 *
6322 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006323 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006324 */
6325WL_EXPORT struct wl_listener *
6326weston_output_get_destroy_listener(struct weston_output *output,
6327 wl_notify_func_t notify)
6328{
6329 return wl_signal_get(&output->user_destroy_signal, notify);
6330}
6331
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006332/** Uninitialize an output
6333 *
6334 * Removes the output from the list of enabled outputs if necessary, but
6335 * does not call the backend's output disable function. The output will no
6336 * longer be in the list of pending outputs either.
6337 *
6338 * All fields of weston_output become uninitialized, i.e. should not be used
6339 * anymore. The caller can free the memory after this.
6340 *
Marius Vlad55d87362019-06-11 01:15:35 +03006341 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006342 * \internal
6343 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006344WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006345weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006346{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006347 struct weston_head *head, *tmp;
6348
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006349 output->destroying = 1;
6350
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006351 wl_signal_emit(&output->user_destroy_signal, output);
6352
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006353 if (output->idle_repaint_source)
6354 wl_event_source_remove(output->idle_repaint_source);
6355
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006356 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006357 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006358
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006359 pixman_region32_fini(&output->region);
6360 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006361 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006362
6363 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6364 weston_head_detach(head);
6365
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006366 free(output->name);
6367}
6368
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006369/** Find an output by its given name
6370 *
6371 * \param compositor The compositor to search in.
6372 * \param name The output name to search for.
6373 * \return An existing output with the given name, or NULL if not found.
6374 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006375 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006376 */
6377WL_EXPORT struct weston_output *
6378weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6379 const char *name)
6380{
6381 struct weston_output *output;
6382
6383 wl_list_for_each(output, &compositor->output_list, link)
6384 if (strcmp(output->name, name) == 0)
6385 return output;
6386
6387 wl_list_for_each(output, &compositor->pending_output_list, link)
6388 if (strcmp(output->name, name) == 0)
6389 return output;
6390
6391 return NULL;
6392}
6393
6394/** Create a named output
6395 *
6396 * \param compositor The compositor.
6397 * \param name The name for the output.
6398 * \return A new \c weston_output, or NULL on failure.
6399 *
6400 * This creates a new weston_output that starts with no heads attached.
6401 *
6402 * An output must be configured and it must have at least one head before
6403 * it can be enabled.
6404 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006405 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006406 */
6407WL_EXPORT struct weston_output *
6408weston_compositor_create_output(struct weston_compositor *compositor,
6409 const char *name)
6410{
6411 assert(compositor->backend->create_output);
6412
6413 if (weston_compositor_find_output_by_name(compositor, name)) {
6414 weston_log("Warning: attempted to create an output with a "
6415 "duplicate name '%s'.\n", name);
6416 return NULL;
6417 }
6418
6419 return compositor->backend->create_output(compositor, name);
6420}
6421
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006422/** Create an output for an unused head
6423 *
6424 * \param compositor The compositor.
6425 * \param head The head to attach to the output.
6426 * \return A new \c weston_output, or NULL on failure.
6427 *
6428 * This creates a new weston_output that starts with the given head attached.
6429 * The output inherits the name of the head. The head must not be already
6430 * attached to another output.
6431 *
6432 * An output must be configured before it can be enabled.
6433 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006434 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006435 */
6436WL_EXPORT struct weston_output *
6437weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6438 struct weston_head *head)
6439{
6440 struct weston_output *output;
6441
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006442 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006443 if (!output)
6444 return NULL;
6445
6446 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006447 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006448 return NULL;
6449 }
6450
6451 return output;
6452}
6453
6454/** Destroy an output
6455 *
6456 * \param output The output to destroy.
6457 *
6458 * The heads attached to the given output are detached and become unused again.
6459 *
6460 * It is not necessary to explicitly destroy all outputs at compositor exit.
6461 * weston_compositor_destroy() will automatically destroy any remaining
6462 * outputs.
6463 *
Marius Vlad55d87362019-06-11 01:15:35 +03006464 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006465 */
6466WL_EXPORT void
6467weston_output_destroy(struct weston_output *output)
6468{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006469 output->destroy(output);
6470}
6471
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006472/** When you need a head...
6473 *
6474 * This function is a hack, used until all code has been converted to become
6475 * multi-head aware.
6476 *
6477 * \param output The weston_output whose head to get.
6478 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006479 *
6480 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006481 */
6482WL_EXPORT struct weston_head *
6483weston_output_get_first_head(struct weston_output *output)
6484{
6485 if (wl_list_empty(&output->head_list))
6486 return NULL;
6487
6488 return container_of(output->head_list.next,
6489 struct weston_head, output_link);
6490}
6491
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006492static void
Roman Gilge97391c2019-03-29 13:01:06 +01006493xdg_output_unlist(struct wl_resource *resource)
6494{
6495 wl_list_remove(wl_resource_get_link(resource));
6496}
6497
6498static void
6499xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6500{
6501 wl_resource_destroy(resource);
6502}
6503
6504static const struct zxdg_output_v1_interface xdg_output_interface = {
6505 xdg_output_destroy
6506};
6507
6508static void
6509xdg_output_manager_destroy(struct wl_client *client,
6510 struct wl_resource *resource)
6511{
6512 wl_resource_destroy(resource);
6513}
6514
6515static void
6516xdg_output_manager_get_xdg_output(struct wl_client *client,
6517 struct wl_resource *manager,
6518 uint32_t id,
6519 struct wl_resource *output_resource)
6520{
6521 int version = wl_resource_get_version(manager);
6522 struct weston_head *head = wl_resource_get_user_data(output_resource);
6523 struct weston_output *output = head->output;
6524 struct wl_resource *resource;
6525
6526 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6527 version, id);
6528 if (resource == NULL) {
6529 wl_client_post_no_memory(client);
6530 return;
6531 }
6532
6533 wl_list_insert(&head->xdg_output_resource_list,
6534 wl_resource_get_link(resource));
6535
6536 wl_resource_set_implementation(resource, &xdg_output_interface,
6537 NULL, xdg_output_unlist);
6538
6539 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6540 zxdg_output_v1_send_logical_size(resource,
6541 output->width,
6542 output->height);
6543 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6544 zxdg_output_v1_send_name(resource, head->name);
6545
6546 zxdg_output_v1_send_done(resource);
6547}
6548
6549static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6550 xdg_output_manager_destroy,
6551 xdg_output_manager_get_xdg_output
6552};
6553
6554static void
6555bind_xdg_output_manager(struct wl_client *client,
6556 void *data, uint32_t version, uint32_t id)
6557{
6558 struct wl_resource *resource;
6559
6560 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6561 version, id);
6562 if (resource == NULL) {
6563 wl_client_post_no_memory(client);
6564 return;
6565 }
6566
6567 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6568 NULL, NULL);
6569}
6570
6571static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006572destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006573{
Jonny Lamb74130762013-11-26 18:19:46 +01006574 struct weston_surface *surface =
6575 wl_resource_get_user_data(resource);
6576
Pekka Paalanen4826f872016-04-22 14:14:38 +03006577 if (!surface)
6578 return;
6579
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006580 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006581 surface->pending.buffer_viewport.buffer.src_width =
6582 wl_fixed_from_int(-1);
6583 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006584 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006585}
6586
6587static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006588viewport_destroy(struct wl_client *client,
6589 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006590{
6591 wl_resource_destroy(resource);
6592}
6593
6594static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006595viewport_set_source(struct wl_client *client,
6596 struct wl_resource *resource,
6597 wl_fixed_t src_x,
6598 wl_fixed_t src_y,
6599 wl_fixed_t src_width,
6600 wl_fixed_t src_height)
6601{
6602 struct weston_surface *surface =
6603 wl_resource_get_user_data(resource);
6604
Pekka Paalanen4826f872016-04-22 14:14:38 +03006605 if (!surface) {
6606 wl_resource_post_error(resource,
6607 WP_VIEWPORT_ERROR_NO_SURFACE,
6608 "wl_surface for this viewport is no longer exists");
6609 return;
6610 }
6611
6612 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006613 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006614
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006615 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006616 src_height == wl_fixed_from_int(-1) &&
6617 src_x == wl_fixed_from_int(-1) &&
6618 src_y == wl_fixed_from_int(-1)) {
6619 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006620 surface->pending.buffer_viewport.buffer.src_width =
6621 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006622 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006623 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006624 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006625
Pekka Paalanen201769a2016-04-26 14:42:11 +03006626 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006627 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006628 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006629 "wl_surface@%d viewport source "
6630 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6631 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006632 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006633 wl_fixed_to_double(src_height),
6634 wl_fixed_to_double(src_x),
6635 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006636 return;
6637 }
6638
6639 surface->pending.buffer_viewport.buffer.src_x = src_x;
6640 surface->pending.buffer_viewport.buffer.src_y = src_y;
6641 surface->pending.buffer_viewport.buffer.src_width = src_width;
6642 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006643 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006644}
6645
6646static void
6647viewport_set_destination(struct wl_client *client,
6648 struct wl_resource *resource,
6649 int32_t dst_width,
6650 int32_t dst_height)
6651{
6652 struct weston_surface *surface =
6653 wl_resource_get_user_data(resource);
6654
Pekka Paalanen4826f872016-04-22 14:14:38 +03006655 if (!surface) {
6656 wl_resource_post_error(resource,
6657 WP_VIEWPORT_ERROR_NO_SURFACE,
6658 "wl_surface for this viewport no longer exists");
6659 return;
6660 }
6661
6662 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006663
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006664 if (dst_width == -1 && dst_height == -1) {
6665 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006666 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006667 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006668 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006669 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006670
6671 if (dst_width <= 0 || dst_height <= 0) {
6672 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006673 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006674 "destination size must be positive (%dx%d)",
6675 dst_width, dst_height);
6676 return;
6677 }
6678
6679 surface->pending.buffer_viewport.surface.width = dst_width;
6680 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006681 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006682}
6683
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006684static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006685 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006686 viewport_set_source,
6687 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006688};
6689
6690static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006691viewporter_destroy(struct wl_client *client,
6692 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006693{
6694 wl_resource_destroy(resource);
6695}
6696
6697static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006698viewporter_get_viewport(struct wl_client *client,
6699 struct wl_resource *viewporter,
6700 uint32_t id,
6701 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006702{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006703 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006704 struct weston_surface *surface =
6705 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006706 struct wl_resource *resource;
6707
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006708 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006709 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006710 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006711 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006712 return;
6713 }
6714
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006715 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006716 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006717 if (resource == NULL) {
6718 wl_client_post_no_memory(client);
6719 return;
6720 }
6721
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006722 wl_resource_set_implementation(resource, &viewport_interface,
6723 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006724
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006725 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006726}
6727
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006728static const struct wp_viewporter_interface viewporter_interface = {
6729 viewporter_destroy,
6730 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006731};
6732
6733static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006734bind_viewporter(struct wl_client *client,
6735 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006736{
6737 struct wl_resource *resource;
6738
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006739 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006740 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006741 if (resource == NULL) {
6742 wl_client_post_no_memory(client);
6743 return;
6744 }
6745
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006746 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006747 NULL, NULL);
6748}
6749
6750static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006751destroy_presentation_feedback(struct wl_resource *feedback_resource)
6752{
6753 struct weston_presentation_feedback *feedback;
6754
6755 feedback = wl_resource_get_user_data(feedback_resource);
6756
6757 wl_list_remove(&feedback->link);
6758 free(feedback);
6759}
6760
6761static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006762presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6763{
6764 wl_resource_destroy(resource);
6765}
6766
6767static void
6768presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006769 struct wl_resource *presentation_resource,
6770 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006771 uint32_t callback)
6772{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006773 struct weston_surface *surface;
6774 struct weston_presentation_feedback *feedback;
6775
6776 surface = wl_resource_get_user_data(surface_resource);
6777
Bryce Harringtonde16d892014-11-20 22:21:57 -08006778 feedback = zalloc(sizeof *feedback);
6779 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006780 goto err_calloc;
6781
6782 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006783 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006784 1, callback);
6785 if (!feedback->resource)
6786 goto err_create;
6787
6788 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6789 destroy_presentation_feedback);
6790
6791 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6792
6793 return;
6794
6795err_create:
6796 free(feedback);
6797
6798err_calloc:
6799 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006800}
6801
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006802static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006803 presentation_destroy,
6804 presentation_feedback
6805};
6806
6807static void
6808bind_presentation(struct wl_client *client,
6809 void *data, uint32_t version, uint32_t id)
6810{
6811 struct weston_compositor *compositor = data;
6812 struct wl_resource *resource;
6813
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006814 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006815 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006816 if (resource == NULL) {
6817 wl_client_post_no_memory(client);
6818 return;
6819 }
6820
6821 wl_resource_set_implementation(resource, &presentation_implementation,
6822 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006823 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006824}
6825
6826static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006827compositor_bind(struct wl_client *client,
6828 void *data, uint32_t version, uint32_t id)
6829{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006830 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006831 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006832
Jason Ekstranda85118c2013-06-27 20:17:02 -05006833 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006834 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006835 if (resource == NULL) {
6836 wl_client_post_no_memory(client);
6837 return;
6838 }
6839
6840 wl_resource_set_implementation(resource, &compositor_interface,
6841 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006842}
6843
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006844WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006845weston_environment_get_fd(const char *env)
6846{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006847 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006848 int fd, flags;
6849
6850 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006851 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006852 return -1;
6853
6854 flags = fcntl(fd, F_GETFD);
6855 if (flags == -1)
6856 return -1;
6857
6858 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6859 unsetenv(env);
6860
6861 return fd;
6862}
6863
Pekka Paalanenb5026542014-11-12 15:09:24 +02006864static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006865timeline_key_binding_handler(struct weston_keyboard *keyboard,
6866 const struct timespec *time, uint32_t key,
6867 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006868{
6869 struct weston_compositor *compositor = data;
6870
6871 if (weston_timeline_enabled_)
6872 weston_timeline_close();
6873 else
6874 weston_timeline_open(compositor);
6875}
6876
Daniel Stonece62cb32018-07-20 09:46:24 +01006877static const char *
6878output_repaint_status_text(struct weston_output *output)
6879{
6880 switch (output->repaint_status) {
6881 case REPAINT_NOT_SCHEDULED:
6882 return "no repaint";
6883 case REPAINT_BEGIN_FROM_IDLE:
6884 return "start_repaint_loop scheduled";
6885 case REPAINT_SCHEDULED:
6886 return "repaint scheduled";
6887 case REPAINT_AWAITING_COMPLETION:
6888 return "awaiting completion";
6889 }
6890
6891 assert(!"output_repaint_status_text missing enum");
6892 return NULL;
6893}
6894
6895static void
6896debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6897{
6898 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6899 struct wl_shm_buffer *shm;
6900 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006901 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006902
6903 if (!buffer) {
6904 fprintf(fp, "\t\t[buffer not available]\n");
6905 return;
6906 }
6907
6908 shm = wl_shm_buffer_get(buffer->resource);
6909 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006910 uint32_t _format = wl_shm_buffer_get_format(shm);
6911 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006912 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006913 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6914 (unsigned long) _format,
6915 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006916 return;
6917 }
6918
6919 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6920 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006921 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006922 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006923 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6924 (unsigned long) dmabuf->attributes.format,
6925 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006926 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6927 (unsigned long long) dmabuf->attributes.modifier[0]);
6928 return;
6929 }
6930
Marius Vlad253ba9a2019-03-17 18:22:21 +02006931 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006932}
6933
6934static void
6935debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6936{
6937 struct weston_compositor *ec = view->surface->compositor;
6938 struct weston_output *output;
6939 char desc[512];
6940 pixman_box32_t *box;
6941 uint32_t surface_id = 0;
6942 pid_t pid = 0;
6943
6944 if (view->surface->resource) {
6945 struct wl_resource *resource = view->surface->resource;
6946 wl_client_get_credentials(wl_resource_get_client(resource),
6947 &pid, NULL, NULL);
6948 surface_id = wl_resource_get_id(view->surface->resource);
6949 }
6950
6951 if (!view->surface->get_label ||
6952 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6953 strcpy(desc, "[no description available]");
6954 }
6955 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6956 view_idx, view->surface->role_name, pid, surface_id,
6957 desc, view);
6958
6959 box = pixman_region32_extents(&view->transform.boundingbox);
6960 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6961 box->x1, box->y1, box->x2, box->y2);
6962 box = pixman_region32_extents(&view->transform.opaque);
6963
6964 if (pixman_region32_equal(&view->transform.opaque,
6965 &view->transform.boundingbox)) {
6966 fprintf(fp, "\t\t[fully opaque]\n");
6967 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6968 fprintf(fp, "\t\t[not opaque]\n");
6969 } else {
6970 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6971 box->x1, box->y1, box->x2, box->y2);
6972 }
6973
6974 if (view->alpha < 1.0)
6975 fprintf(fp, "\t\talpha: %f\n", view->alpha);
6976
6977 if (view->output_mask != 0) {
6978 bool first_output = true;
6979 fprintf(fp, "\t\toutputs: ");
6980 wl_list_for_each(output, &ec->output_list, link) {
6981 if (!(view->output_mask & (1 << output->id)))
6982 continue;
6983 fprintf(fp, "%s%d (%s)%s",
6984 (first_output) ? "" : ", ",
6985 output->id, output->name,
6986 (view->output == output) ? " (primary)" : "");
6987 first_output = false;
6988 }
6989 } else {
6990 fprintf(fp, "\t\t[no outputs]");
6991 }
6992
6993 fprintf(fp, "\n");
6994
6995 debug_scene_view_print_buffer(fp, view);
6996}
6997
Marius Vlad433f4e72019-02-17 22:14:23 +02006998static void
6999debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007000 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007001{
7002 struct weston_subsurface *sub;
7003 struct weston_view *ev;
7004
7005 /*
7006 * print the view first, then we recursively go on printing
7007 * sub-surfaces. We bail out once no more sub-surfaces are available.
7008 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007009 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007010
7011 /* no more sub-surfaces */
7012 if (wl_list_empty(&view->surface->subsurface_list))
7013 return;
7014
7015 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7016 wl_list_for_each(ev, &sub->surface->views, surface_link) {
7017 /* do not print again the parent view */
7018 if (view == ev)
7019 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007020
7021 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007022 debug_scene_view_print_tree(ev, fp, view_idx);
7023 }
7024 }
7025}
7026
Daniel Stonece62cb32018-07-20 09:46:24 +01007027/**
7028 * Output information on how libweston is currently composing the scene
7029 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007030 *
7031 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007032 */
7033WL_EXPORT char *
7034weston_compositor_print_scene_graph(struct weston_compositor *ec)
7035{
7036 struct weston_output *output;
7037 struct weston_layer *layer;
7038 struct timespec now;
7039 int layer_idx = 0;
7040 FILE *fp;
7041 char *ret;
7042 size_t len;
7043 int err;
7044
7045 fp = open_memstream(&ret, &len);
7046 assert(fp);
7047
7048 weston_compositor_read_presentation_clock(ec, &now);
7049 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7050 now.tv_sec, now.tv_nsec);
7051
7052 wl_list_for_each(output, &ec->output_list, link) {
7053 struct weston_head *head;
7054 int head_idx = 0;
7055
7056 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7057 assert(output->enabled);
7058
7059 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7060 output->x, output->y,
7061 output->x + output->width,
7062 output->y + output->height);
7063 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7064 output->current_mode->width,
7065 output->current_mode->height,
7066 output->current_mode->refresh / 1000.0);
7067 fprintf(fp, "\tscale: %d\n", output->scale);
7068
7069 fprintf(fp, "\trepaint status: %s\n",
7070 output_repaint_status_text(output));
7071 if (output->repaint_status == REPAINT_SCHEDULED)
7072 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7073 output->next_repaint.tv_sec,
7074 output->next_repaint.tv_nsec);
7075
7076 wl_list_for_each(head, &output->head_list, output_link) {
7077 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7078 head_idx++, head->name,
7079 (head->connected) ? "" : "not ");
7080 }
7081 }
7082
7083 fprintf(fp, "\n");
7084
7085 wl_list_for_each(layer, &ec->layer_list, link) {
7086 struct weston_view *view;
7087 int view_idx = 0;
7088
7089 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7090 (unsigned long) layer->position);
7091
7092 if (!weston_layer_mask_is_infinite(layer)) {
7093 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7094 layer->mask.x1, layer->mask.y1,
7095 layer->mask.x2, layer->mask.y2);
7096 }
7097
Marius Vlada6acfa82019-03-17 18:10:09 +02007098 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7099 debug_scene_view_print_tree(view, fp, &view_idx);
7100 view_idx++;
7101 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007102
7103 if (wl_list_empty(&layer->view_list.link))
7104 fprintf(fp, "\t[no views]\n");
7105
7106 fprintf(fp, "\n");
7107 }
7108
7109 err = fclose(fp);
7110 assert(err == 0);
7111
7112 return ret;
7113}
7114
7115/**
7116 * Called when the 'scene-graph' debug scope is bound by a client. This
7117 * one-shot weston-debug scope prints the current scene graph when bound,
7118 * and then terminates the stream.
7119 */
7120static void
7121debug_scene_graph_cb(struct weston_debug_stream *stream, void *data)
7122{
7123 struct weston_compositor *ec = data;
7124 char *str = weston_compositor_print_scene_graph(ec);
7125
7126 weston_debug_stream_printf(stream, "%s", str);
7127 free(str);
7128 weston_debug_stream_complete(stream);
7129}
7130
Giulio Camuffo459137b2014-10-11 23:56:24 +03007131/** Create the compositor.
7132 *
7133 * This functions creates and initializes a compositor instance.
7134 *
7135 * \param display The Wayland display to be used.
7136 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007137 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007138 * using the \ref weston_compositor_get_user_data function.
7139 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007140 *
7141 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007142 */
7143WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007144weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007145 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007146 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007147{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007148 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007149 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007150
Giulio Camuffo459137b2014-10-11 23:56:24 +03007151 ec = zalloc(sizeof *ec);
7152 if (!ec)
7153 return NULL;
7154
7155 ec->wl_display = display;
7156 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007157 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007158 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007159 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007160 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007161 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007162 wl_signal_init(&ec->idle_signal);
7163 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007164 wl_signal_init(&ec->show_input_panel_signal);
7165 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007166 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007167 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007168 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007169 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007170 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007171 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007172 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007173 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007174 wl_signal_init(&ec->session_signal);
7175 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007176
Casey Dahlin58ba1372012-04-19 22:50:08 -04007177 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007178 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007179
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007180 ec->activate_serial = 1;
7181
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007182 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7183
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307184 ec->content_protection = NULL;
7185
Derek Foreman152254b2015-11-26 14:17:48 -06007186 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007187 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007188 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007189
Giulio Camuffo954f1832014-10-11 18:27:30 +03007190 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007191 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007192 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007193
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007194 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007195 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007196 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007197
Roman Gilge97391c2019-03-29 13:01:06 +01007198 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7199 ec, bind_xdg_output_manager))
7200 goto fail;
7201
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007202 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007203 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007204 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007205
Marius Vlad3d7d9782019-04-17 12:35:38 +03007206 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007207 goto fail;
7208
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007209 if (weston_input_init(ec) != 0)
7210 goto fail;
7211
Jason Ekstranda7af7042013-10-12 22:38:11 -05007212 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007213 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007214 wl_list_init(&ec->layer_list);
7215 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007216 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007217 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007218 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007219 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007220 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007221 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007222 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007223 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007224 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007225
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007226 wl_list_init(&ec->plugin_api_list);
7227
Xiong Zhang97116532013-10-23 13:58:31 +08007228 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007229 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007230
Giulio Camuffo459137b2014-10-11 23:56:24 +03007231 wl_data_device_manager_init(ec->wl_display);
7232
7233 wl_display_init_shm(ec->wl_display);
7234
7235 loop = wl_display_get_event_loop(ec->wl_display);
7236 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007237 ec->repaint_timer =
7238 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7239 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007240
Quentin Glidic82681572016-12-17 13:40:51 +01007241 weston_layer_init(&ec->fade_layer, ec);
7242 weston_layer_init(&ec->cursor_layer, ec);
7243
7244 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7245 weston_layer_set_position(&ec->cursor_layer,
7246 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007247
7248 weston_compositor_add_debug_binding(ec, KEY_T,
7249 timeline_key_binding_handler, ec);
7250
Daniel Stonece62cb32018-07-20 09:46:24 +01007251 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03007252 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
7253 "Scene graph details\n",
7254 debug_scene_graph_cb,
7255 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007256
Giulio Camuffo459137b2014-10-11 23:56:24 +03007257 return ec;
7258
7259fail:
7260 free(ec);
7261 return NULL;
7262}
7263
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007264/** weston_compositor_shutdown
7265 * \ingroup compositor
7266 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007267WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007268weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007269{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007270 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007271
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007272 wl_event_source_remove(ec->idle_source);
7273
Matt Roper361d2ad2011-08-29 13:52:23 -07007274 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007275 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007276 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007277
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007278 /* Destroy all pending outputs associated with this compositor */
7279 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7280 output->destroy(output);
7281
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007282 if (ec->renderer)
7283 ec->renderer->destroy(ec);
7284
Daniel Stone325fc2d2012-05-30 16:31:58 +01007285 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007286 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007287 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007288 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007289 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007290 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007291
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007292 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007293}
7294
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007295/** weston_compositor_exit_with_code
7296 * \ingroup compositor
7297 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007298WL_EXPORT void
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007299weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007300 int exit_code)
7301{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007302 if (compositor->exit_code == EXIT_SUCCESS)
7303 compositor->exit_code = exit_code;
7304
Giulio Camuffo459137b2014-10-11 23:56:24 +03007305 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007306}
7307
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007308/** weston_compositor_set_default_pointer_grab
7309 * \ingroup compositor
7310 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007311WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007312weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7313 const struct weston_pointer_grab_interface *interface)
7314{
7315 struct weston_seat *seat;
7316
7317 ec->default_pointer_grab = interface;
7318 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007319 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7320
7321 if (pointer)
7322 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007323 }
7324}
7325
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007326/** weston_compositor_set_presentation_clock
7327 * \ingroup compositor
7328 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007329WL_EXPORT int
7330weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7331 clockid_t clk_id)
7332{
7333 struct timespec ts;
7334
7335 if (clock_gettime(clk_id, &ts) < 0)
7336 return -1;
7337
7338 compositor->presentation_clock = clk_id;
7339
7340 return 0;
7341}
7342
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007343/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007344 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007345 *
7346 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007347 */
7348WL_EXPORT int
7349weston_compositor_set_presentation_clock_software(
7350 struct weston_compositor *compositor)
7351{
7352 /* In order of preference */
7353 static const clockid_t clocks[] = {
7354 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7355 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7356 CLOCK_MONOTONIC, /* no jumps, may crawl */
7357 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7358 CLOCK_REALTIME /* may jump and crawl */
7359 };
7360 unsigned i;
7361
7362 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7363 if (weston_compositor_set_presentation_clock(compositor,
7364 clocks[i]) == 0)
7365 return 0;
7366
7367 weston_log("Error: no suitable presentation clock available.\n");
7368
7369 return -1;
7370}
7371
Pekka Paalanen662f3842015-03-18 12:17:26 +02007372/** Read the current time from the Presentation clock
7373 *
7374 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007375 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007376 *
7377 * \note Reading the current time in user space is always imprecise to some
7378 * degree.
7379 *
7380 * This function is never meant to fail. If reading the clock does fail,
7381 * an error message is logged and a zero time is returned. Callers are not
7382 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007383 *
7384 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007385 */
7386WL_EXPORT void
7387weston_compositor_read_presentation_clock(
7388 const struct weston_compositor *compositor,
7389 struct timespec *ts)
7390{
7391 static bool warned;
7392 int ret;
7393
7394 ret = clock_gettime(compositor->presentation_clock, ts);
7395 if (ret < 0) {
7396 ts->tv_sec = 0;
7397 ts->tv_nsec = 0;
7398
7399 if (!warned)
7400 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007401 "the presentation clock %#x: '%s' (%d)\n",
7402 compositor->presentation_clock,
7403 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007404 warned = true;
7405 }
7406}
7407
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007408/** Import dmabuf buffer into current renderer
7409 *
7410 * \param compositor
7411 * \param buffer the dmabuf buffer to import
7412 * \return true on usable buffers, false otherwise
7413 *
7414 * This function tests that the linux_dmabuf_buffer is usable
7415 * for the current renderer. Returns false on unusable buffers. Usually
7416 * usability is tested by importing the dmabufs for composition.
7417 *
7418 * This hook is also used for detecting if the renderer supports
7419 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7420 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007421 *
7422 * \ingroup compositor
7423 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007424WL_EXPORT bool
7425weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7426 struct linux_dmabuf_buffer *buffer)
7427{
7428 struct weston_renderer *renderer;
7429
7430 renderer = compositor->renderer;
7431
7432 if (renderer->import_dmabuf == NULL)
7433 return false;
7434
7435 return renderer->import_dmabuf(compositor, buffer);
7436}
7437
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007438WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007439weston_version(int *major, int *minor, int *micro)
7440{
7441 *major = WESTON_VERSION_MAJOR;
7442 *minor = WESTON_VERSION_MINOR;
7443 *micro = WESTON_VERSION_MICRO;
7444}
7445
Daniel Stonee03c1112016-11-24 20:45:45 +00007446/**
7447 * Attempts to find a module path from the module map specified in the
7448 * environment. If found, writes the full path into the path variable.
7449 *
7450 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7451 * each entry is of the form "name=path" and entries are separated by
7452 * semicolons. Whitespace is significant.
7453 *
7454 * \param name The name to search for.
7455 * \param path Where the path is written to if found.
7456 * \param path_len Allocated bytes at \c path .
7457 * \returns The length of the string written to path on success, or 0 if the
7458 * module was not specified in the environment map or path_len was too small.
7459 */
7460WL_EXPORT size_t
7461weston_module_path_from_env(const char *name, char *path, size_t path_len)
7462{
7463 const char *mapping = getenv("WESTON_MODULE_MAP");
7464 const char *end;
7465 const int name_len = strlen(name);
7466
7467 if (!mapping)
7468 return 0;
7469
7470 end = mapping + strlen(mapping);
7471 while (mapping < end && *mapping) {
7472 const char *filename, *next;
7473
7474 /* early out: impossibly short string */
7475 if (end - mapping < name_len + 1)
7476 return 0;
7477
7478 filename = &mapping[name_len + 1];
7479 next = strchrnul(mapping, ';');
7480
7481 if (strncmp(mapping, name, name_len) == 0 &&
7482 mapping[name_len] == '=') {
7483 size_t file_len = next - filename; /* no trailing NUL */
7484 if (file_len >= path_len)
7485 return 0;
7486 strncpy(path, filename, file_len);
7487 path[file_len] = '\0';
7488 return file_len;
7489 }
7490
7491 mapping = next + 1;
7492 }
7493
7494 return 0;
7495}
7496
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007497WL_EXPORT void *
7498weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007499{
7500 char path[PATH_MAX];
7501 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007502 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007503
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007504 if (name == NULL)
7505 return NULL;
7506
Derek Foreman3f86e502015-06-08 11:46:54 -05007507 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007508 len = weston_module_path_from_env(name, path, sizeof path);
7509 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007510 len = snprintf(path, sizeof path, "%s/%s",
7511 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007512 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007513 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007514 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007515
Daniel Stonebeb97e52016-11-28 12:13:54 +00007516 /* snprintf returns the length of the string it would've written,
7517 * _excluding_ the NUL byte. So even being equal to the size of
7518 * our buffer is an error here. */
7519 if (len >= sizeof path)
7520 return NULL;
7521
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007522 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7523 if (module) {
7524 weston_log("Module '%s' already loaded\n", path);
7525 dlclose(module);
7526 return NULL;
7527 }
7528
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007529 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007530 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007531 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007532 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007533 return NULL;
7534 }
7535
7536 init = dlsym(module, entrypoint);
7537 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007538 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007539 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007540 return NULL;
7541 }
7542
7543 return init;
7544}
7545
Giulio Camuffo459137b2014-10-11 23:56:24 +03007546
7547/** Destroys the compositor.
7548 *
7549 * This function cleans up the compositor state and destroys it.
7550 *
7551 * \param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007552 *
7553 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007554 */
7555WL_EXPORT void
7556weston_compositor_destroy(struct weston_compositor *compositor)
7557{
7558 /* prevent further rendering while shutting down */
7559 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7560
7561 wl_signal_emit(&compositor->destroy_signal, compositor);
7562
7563 weston_compositor_xkb_destroy(compositor);
7564
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007565 if (compositor->backend)
7566 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007567
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007568 /* The backend is responsible for destroying the heads. */
7569 assert(wl_list_empty(&compositor->head_list));
7570
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007571 weston_plugin_api_destroy_list(compositor);
7572
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007573 if (compositor->heads_changed_source)
7574 wl_event_source_remove(compositor->heads_changed_source);
7575
Marius Vlad7e4db952019-04-17 13:47:06 +03007576 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007577 compositor->debug_scene = NULL;
Marius Vlad3d7d9782019-04-17 12:35:38 +03007578 weston_log_ctx_compositor_destroy(compositor);
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007579
Giulio Camuffo459137b2014-10-11 23:56:24 +03007580 free(compositor);
7581}
7582
7583/** Instruct the compositor to exit.
7584 *
7585 * This functions does not directly destroy the compositor object, it merely
7586 * command it to start the tear down process. It is not guaranteed that the
7587 * tear down will happen immediately.
7588 *
7589 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007590 *
7591 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007592 */
7593WL_EXPORT void
7594weston_compositor_exit(struct weston_compositor *compositor)
7595{
7596 compositor->exit(compositor);
7597}
7598
7599/** Return the user data stored in the compositor.
7600 *
7601 * This function returns the user data pointer set with user_data parameter
7602 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007603 *
7604 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007605 */
7606WL_EXPORT void *
7607weston_compositor_get_user_data(struct weston_compositor *compositor)
7608{
7609 return compositor->user_data;
7610}
Pekka Paalanendd186732016-06-03 14:49:54 +03007611
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007612static const char * const backend_map[] = {
7613 [WESTON_BACKEND_DRM] = "drm-backend.so",
7614 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7615 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7616 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7617 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7618 [WESTON_BACKEND_X11] = "x11-backend.so",
7619};
7620
Pekka Paalanendd186732016-06-03 14:49:54 +03007621/** Load a backend into a weston_compositor
7622 *
7623 * A backend must be loaded to make a weston_compositor work. A backend
7624 * provides input and output capabilities, and determines the renderer to use.
7625 *
7626 * \param compositor A compositor that has not had a backend loaded yet.
7627 * \param backend Name of the backend file.
7628 * \param config_base A pointer to a backend-specific configuration
7629 * structure's 'base' member.
7630 *
7631 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007632 *
7633 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007634 */
7635WL_EXPORT int
7636weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007637 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007638 struct weston_backend_config *config_base)
7639{
7640 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007641 struct weston_backend_config *config_base);
7642
Pekka Paalanend7e35112017-08-29 17:04:12 +03007643 if (compositor->backend) {
7644 weston_log("Error: attempt to load a backend when one is already loaded\n");
7645 return -1;
7646 }
7647
Quentin Glidic887c0182016-07-10 11:00:53 +02007648 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007649 return -1;
7650
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007651 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007652 if (!backend_init)
7653 return -1;
7654
Pekka Paalanend7e35112017-08-29 17:04:12 +03007655 if (backend_init(compositor, config_base) < 0) {
7656 compositor->backend = NULL;
7657 return -1;
7658 }
7659
7660 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007661}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007662
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007663/** weston_compositor_load_xwayland
7664 * \ingroup compositor
7665 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007666WL_EXPORT int
7667weston_compositor_load_xwayland(struct weston_compositor *compositor)
7668{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007669 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007670
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007671 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007672 if (!module_init)
7673 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007674 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007675 return -1;
7676 return 0;
7677}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007678
7679/** Resolve an internal compositor error by disconnecting the client.
7680 *
7681 * This function is used in cases when the wl_buffer turns out
7682 * unusable and there is no fallback path.
7683 *
7684 * It is possible the fault is caused by a compositor bug, the underlying
7685 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7686 * In any case, the options are to either composite garbage or nothing,
7687 * or disconnect the client. This is a helper function for the latter.
7688 *
7689 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7690 *
7691 * \param buffer The weston buffer that is unusable.
7692 * \param msg A custom error message attached to the protocol error.
7693 */
7694WL_EXPORT void
7695weston_buffer_send_server_error(struct weston_buffer *buffer,
7696 const char *msg)
7697{
7698 struct wl_client *client;
7699 struct wl_resource *display_resource;
7700 uint32_t id;
7701
7702 assert(buffer->resource);
7703 id = wl_resource_get_id(buffer->resource);
7704 client = wl_resource_get_client(buffer->resource);
7705 display_resource = wl_client_get_object(client, 1);
7706
7707 assert(display_resource);
7708 wl_resource_post_error(display_resource,
7709 WL_DISPLAY_ERROR_INVALID_OBJECT,
7710 "server error with "
7711 "wl_buffer@%u: %s", id, msg);
7712}