blob: f2ef3f5baaf1b82a505ec27b623d302863a796e4 [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 Vladc901e892019-06-21 22:49:18 +030059#include <libweston/weston-log.h>
Daniel Stonece62cb32018-07-20 09:46:24 +010060#include "linux-dmabuf.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030061#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020062#include "presentation-time-server-protocol.h"
Roman Gilge97391c2019-03-29 13:01:06 +010063#include "xdg-output-unstable-v1-server-protocol.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030064#include "linux-explicit-synchronization-unstable-v1-server-protocol.h"
Alexandros Frantzis67629672018-10-19 12:14:11 +030065#include "linux-explicit-synchronization.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030066#include "shared/fd-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070067#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070068#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070069#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030070#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040071#include "git-version.h"
Pekka Paalanencda14882019-04-04 15:41:02 +030072#include <libweston/version.h>
Pekka Paalanen27b377f2019-03-29 17:07:34 +020073#include <libweston/plugin-registry.h>
Marius Vlad00a6e012018-11-20 17:52:31 +020074#include "pixel-formats.h"
Marius Vlad63ef0782019-07-16 23:34:14 +030075#include "backend.h"
Marius Vlada72e3712019-07-10 13:46:39 +030076#include "libweston-internal.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050077
Marius Vlad78984ee2019-06-11 00:05:08 +030078/**
79 * \defgroup head Head
80 * \defgroup output Output
81 * \defgroup compositor Compositor
82 */
83
Pekka Paalanen0513a952014-05-21 16:17:27 +030084#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
85
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020086static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030087weston_output_update_matrix(struct weston_output *output);
88
89static void
Alexander Larsson0b135062013-05-28 16:23:36 +020090weston_output_transform_scale_init(struct weston_output *output,
91 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020092
Rob Bradford27b17932013-06-26 18:08:46 +010093static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050094weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010095
Pekka Paalanendcac3512017-12-08 14:13:34 +020096static char *
97weston_output_create_heads_string(struct weston_output *output);
98
Pekka Paalanen6528c032017-03-28 15:27:10 +030099/** Send wl_output events for mode and scale changes
100 *
101 * \param head Send on all resources bound to this head.
102 * \param mode_changed If true, send the current mode.
103 * \param scale_changed If true, send the current scale.
104 */
105static void
106weston_mode_switch_send_events(struct weston_head *head,
107 bool mode_changed, bool scale_changed)
108{
109 struct weston_output *output = head->output;
110 struct wl_resource *resource;
111 int version;
112
113 wl_resource_for_each(resource, &head->resource_list) {
114 if (mode_changed) {
115 wl_output_send_mode(resource,
116 output->current_mode->flags,
117 output->current_mode->width,
118 output->current_mode->height,
119 output->current_mode->refresh);
120 }
121
122 version = wl_resource_get_version(resource);
123 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
124 wl_output_send_scale(resource, output->current_scale);
125
126 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
127 wl_output_send_done(resource);
128 }
Roman Gilge97391c2019-03-29 13:01:06 +0100129 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
130 zxdg_output_v1_send_logical_position(resource,
131 output->x,
132 output->y);
133 zxdg_output_v1_send_logical_size(resource,
134 output->width,
135 output->height);
136 zxdg_output_v1_send_done(resource);
137 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300138}
139
140static void
141weston_mode_switch_finish(struct weston_output *output,
142 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800143{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200144 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300145 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200146 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200147
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200148 pixman_region32_init(&old_output_region);
149 pixman_region32_copy(&old_output_region, &output->region);
150
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200151 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200152 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200153
154 pixman_region32_init(&output->previous_damage);
155 pixman_region32_init_rect(&output->region, output->x, output->y,
156 output->width, output->height);
157
158 weston_output_update_matrix(output);
159
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200160 /* If a pointer falls outside the outputs new geometry, move it to its
161 * lower-right corner */
162 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500163 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200164 int32_t x, y;
165
166 if (!pointer)
167 continue;
168
169 x = wl_fixed_to_int(pointer->x);
170 y = wl_fixed_to_int(pointer->y);
171
172 if (!pixman_region32_contains_point(&old_output_region,
173 x, y, NULL) ||
174 pixman_region32_contains_point(&output->region,
175 x, y, NULL))
176 continue;
177
178 if (x >= output->x + output->width)
179 x = output->x + output->width - 1;
180 if (y >= output->y + output->height)
181 y = output->y + output->height - 1;
182
183 pointer->x = wl_fixed_from_int(x);
184 pointer->y = wl_fixed_from_int(y);
185 }
186
187 pixman_region32_fini(&old_output_region);
188
Derek Foremandd4cd332014-11-10 10:29:59 -0600189 if (!mode_changed && !scale_changed)
190 return;
191
Hardening57388e42013-09-18 23:56:36 +0200192 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300193 wl_list_for_each(head, &output->head_list, output_link)
194 weston_mode_switch_send_events(head,
195 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600196}
197
David Fort0de859e2016-05-27 23:22:57 +0200198static void
199weston_compositor_reflow_outputs(struct weston_compositor *compositor,
200 struct weston_output *resized_output, int delta_width);
201
Marius Vlad55d87362019-06-11 01:15:35 +0300202/**
203 * \ingroup output
204 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600205WL_EXPORT int
206weston_output_mode_set_native(struct weston_output *output,
207 struct weston_mode *mode,
208 int32_t scale)
209{
210 int ret;
211 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200212 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600213
214 if (!output->switch_mode)
215 return -1;
216
217 if (!output->original_mode) {
218 mode_changed = 1;
219 ret = output->switch_mode(output, mode);
220 if (ret < 0)
221 return ret;
222 if (output->current_scale != scale) {
223 scale_changed = 1;
224 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200225 }
226 }
227
David Fort0de859e2016-05-27 23:22:57 +0200228 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600229 output->native_mode = mode;
230 output->native_scale = scale;
231
232 weston_mode_switch_finish(output, mode_changed, scale_changed);
233
David Fort0de859e2016-05-27 23:22:57 +0200234 if (mode_changed || scale_changed) {
235 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
236
237 wl_signal_emit(&output->compositor->output_resized_signal, output);
238 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600239 return 0;
240}
241
Marius Vlad55d87362019-06-11 01:15:35 +0300242/**
243 * \ingroup output
244 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600245WL_EXPORT int
246weston_output_mode_switch_to_native(struct weston_output *output)
247{
248 int ret;
249 int mode_changed = 0, scale_changed = 0;
250
251 if (!output->switch_mode)
252 return -1;
253
254 if (!output->original_mode) {
255 weston_log("already in the native mode\n");
256 return -1;
257 }
258 /* the non fullscreen clients haven't seen a mode set since we
259 * switched into a temporary, so we need to notify them if the
260 * mode at that time is different from the native mode now.
261 */
262 mode_changed = (output->original_mode != output->native_mode);
263 scale_changed = (output->original_scale != output->native_scale);
264
265 ret = output->switch_mode(output, output->native_mode);
266 if (ret < 0)
267 return ret;
268
269 output->current_scale = output->native_scale;
270
271 output->original_mode = NULL;
272 output->original_scale = 0;
273
274 weston_mode_switch_finish(output, mode_changed, scale_changed);
275
276 return 0;
277}
278
Marius Vlad55d87362019-06-11 01:15:35 +0300279/**
280 * \ingroup output
281 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600282WL_EXPORT int
283weston_output_mode_switch_to_temporary(struct weston_output *output,
284 struct weston_mode *mode,
285 int32_t scale)
286{
287 int ret;
288
289 if (!output->switch_mode)
290 return -1;
291
292 /* original_mode is the last mode non full screen clients have seen,
293 * so we shouldn't change it if we already have one set.
294 */
295 if (!output->original_mode) {
296 output->original_mode = output->native_mode;
297 output->original_scale = output->native_scale;
298 }
299 ret = output->switch_mode(output, mode);
300 if (ret < 0)
301 return ret;
302
303 output->current_scale = scale;
304
305 weston_mode_switch_finish(output, 0, 0);
306
307 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800308}
309
Benjamin Franzke06286262011-05-06 19:12:33 +0200310static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300311region_init_infinite(pixman_region32_t *region)
312{
313 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
314 UINT32_MAX, UINT32_MAX);
315}
316
Pekka Paalanene67858b2013-04-25 13:57:42 +0300317static struct weston_subsurface *
318weston_surface_to_subsurface(struct weston_surface *surface);
319
Jason Ekstranda7af7042013-10-12 22:38:11 -0500320WL_EXPORT struct weston_view *
321weston_view_create(struct weston_surface *surface)
322{
323 struct weston_view *view;
324
Bryce Harringtonde16d892014-11-20 22:21:57 -0800325 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500326 if (view == NULL)
327 return NULL;
328
329 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000330 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500331
Jason Ekstranda7af7042013-10-12 22:38:11 -0500332 /* Assign to surface */
333 wl_list_insert(&surface->views, &view->surface_link);
334
335 wl_signal_init(&view->destroy_signal);
336 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300337 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500338
Jason Ekstranda7af7042013-10-12 22:38:11 -0500339 pixman_region32_init(&view->clip);
340
341 view->alpha = 1.0;
342 pixman_region32_init(&view->transform.opaque);
343
344 wl_list_init(&view->geometry.transformation_list);
345 wl_list_insert(&view->geometry.transformation_list,
346 &view->transform.position.link);
347 weston_matrix_init(&view->transform.position.matrix);
348 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200349 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500350 pixman_region32_init(&view->transform.boundingbox);
351 view->transform.dirty = 1;
352
Jason Ekstranda7af7042013-10-12 22:38:11 -0500353 return view;
354}
355
Jason Ekstrand108865d2014-06-26 10:04:49 -0700356struct weston_frame_callback {
357 struct wl_resource *resource;
358 struct wl_list link;
359};
360
Pekka Paalanen133e4392014-09-23 22:08:46 -0400361struct weston_presentation_feedback {
362 struct wl_resource *resource;
363
364 /* XXX: could use just wl_resource_get_link() instead */
365 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200366
367 /* The per-surface feedback flags */
368 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400369};
370
371static void
372weston_presentation_feedback_discard(
373 struct weston_presentation_feedback *feedback)
374{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200375 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400376 wl_resource_destroy(feedback->resource);
377}
378
379static void
380weston_presentation_feedback_discard_list(struct wl_list *list)
381{
382 struct weston_presentation_feedback *feedback, *tmp;
383
384 wl_list_for_each_safe(feedback, tmp, list, link)
385 weston_presentation_feedback_discard(feedback);
386}
387
388static void
389weston_presentation_feedback_present(
390 struct weston_presentation_feedback *feedback,
391 struct weston_output *output,
392 uint32_t refresh_nsec,
393 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200394 uint64_t seq,
395 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400396{
397 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300398 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400399 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200400 uint32_t tv_sec_hi;
401 uint32_t tv_sec_lo;
402 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300403 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400404
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300405 wl_list_for_each(head, &output->head_list, output_link) {
406 wl_resource_for_each(o, &head->resource_list) {
407 if (wl_resource_get_client(o) != client)
408 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400409
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300410 wp_presentation_feedback_send_sync_output(feedback->resource, o);
411 done = true;
412 }
413
414 /* For clone mode, send it for just one wl_output global,
415 * they are all equivalent anyway.
416 */
417 if (done)
418 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400419 }
420
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200421 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200422 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200423 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200424 refresh_nsec,
425 seq >> 32, seq & 0xffffffff,
426 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400427 wl_resource_destroy(feedback->resource);
428}
429
430static void
431weston_presentation_feedback_present_list(struct wl_list *list,
432 struct weston_output *output,
433 uint32_t refresh_nsec,
434 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200435 uint64_t seq,
436 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400437{
438 struct weston_presentation_feedback *feedback, *tmp;
439
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200440 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200441 wl_list_empty(list));
442
Pekka Paalanen133e4392014-09-23 22:08:46 -0400443 wl_list_for_each_safe(feedback, tmp, list, link)
444 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200445 refresh_nsec, ts, seq,
446 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400447}
448
Jason Ekstrand7b982072014-05-20 14:33:03 -0500449static void
450surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
451{
452 struct weston_surface_state *state =
453 container_of(listener, struct weston_surface_state,
454 buffer_destroy_listener);
455
456 state->buffer = NULL;
457}
458
459static void
460weston_surface_state_init(struct weston_surface_state *state)
461{
462 state->newly_attached = 0;
463 state->buffer = NULL;
464 state->buffer_destroy_listener.notify =
465 surface_state_handle_buffer_destroy;
466 state->sx = 0;
467 state->sy = 0;
468
Derek Foreman152254b2015-11-26 14:17:48 -0600469 pixman_region32_init(&state->damage_surface);
470 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500471 pixman_region32_init(&state->opaque);
472 region_init_infinite(&state->input);
473
474 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400475 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500476
477 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
478 state->buffer_viewport.buffer.scale = 1;
479 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
480 state->buffer_viewport.surface.width = -1;
481 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300482
483 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530484
485 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530486 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500487}
488
489static void
490weston_surface_state_fini(struct weston_surface_state *state)
491{
492 struct weston_frame_callback *cb, *next;
493
494 wl_list_for_each_safe(cb, next,
495 &state->frame_callback_list, link)
496 wl_resource_destroy(cb->resource);
497
Pekka Paalanen133e4392014-09-23 22:08:46 -0400498 weston_presentation_feedback_discard_list(&state->feedback_list);
499
Jason Ekstrand7b982072014-05-20 14:33:03 -0500500 pixman_region32_fini(&state->input);
501 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600502 pixman_region32_fini(&state->damage_surface);
503 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500504
505 if (state->buffer)
506 wl_list_remove(&state->buffer_destroy_listener.link);
507 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300508
509 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300510 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500511}
512
513static void
514weston_surface_state_set_buffer(struct weston_surface_state *state,
515 struct weston_buffer *buffer)
516{
517 if (state->buffer == buffer)
518 return;
519
520 if (state->buffer)
521 wl_list_remove(&state->buffer_destroy_listener.link);
522 state->buffer = buffer;
523 if (state->buffer)
524 wl_signal_add(&state->buffer->destroy_signal,
525 &state->buffer_destroy_listener);
526}
527
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500528WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500529weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500530{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500531 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400532
Bryce Harringtonde16d892014-11-20 22:21:57 -0800533 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400534 if (surface == NULL)
535 return NULL;
536
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500537 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800538 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500539
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500540 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200541 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400542
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200543 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
544 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200545 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
546 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500547
548 weston_surface_state_init(&surface->pending);
549
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400550 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500551 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300552 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400553
Jason Ekstranda7af7042013-10-12 22:38:11 -0500554 wl_list_init(&surface->views);
555
556 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400557 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500558
Pekka Paalanene67858b2013-04-25 13:57:42 +0300559 wl_list_init(&surface->subsurface_list);
560 wl_list_init(&surface->subsurface_list_pending);
561
Jason Ekstrand1e059042014-10-16 10:55:19 -0500562 weston_matrix_init(&surface->buffer_to_surface_matrix);
563 weston_matrix_init(&surface->surface_to_buffer_matrix);
564
Jonas Ådahld3414f22016-07-22 17:56:31 +0800565 wl_list_init(&surface->pointer_constraints);
566
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300567 surface->acquire_fence_fd = -1;
568
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530569 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530570 surface->current_protection = WESTON_HDCP_DISABLE;
571 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530572
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400573 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500574}
575
Alex Wu8811bf92012-02-28 18:07:54 +0800576WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500577weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200578 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500579{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100580 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200581 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500582}
583
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400584WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500585weston_view_to_global_float(struct weston_view *view,
586 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200587{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500588 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200589 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
590
Jason Ekstranda7af7042013-10-12 22:38:11 -0500591 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200592
593 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200594 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700595 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200596 v.f[3]);
597 *x = 0;
598 *y = 0;
599 return;
600 }
601
602 *x = v.f[0] / v.f[3];
603 *y = v.f[1] / v.f[3];
604 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500605 *x = sx + view->geometry.x;
606 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200607 }
608}
609
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500610WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200611weston_transformed_coord(int width, int height,
612 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200613 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200614 float sx, float sy, float *bx, float *by)
615{
616 switch (transform) {
617 case WL_OUTPUT_TRANSFORM_NORMAL:
618 default:
619 *bx = sx;
620 *by = sy;
621 break;
622 case WL_OUTPUT_TRANSFORM_FLIPPED:
623 *bx = width - sx;
624 *by = sy;
625 break;
626 case WL_OUTPUT_TRANSFORM_90:
627 *bx = height - sy;
628 *by = sx;
629 break;
630 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
631 *bx = height - sy;
632 *by = width - sx;
633 break;
634 case WL_OUTPUT_TRANSFORM_180:
635 *bx = width - sx;
636 *by = height - sy;
637 break;
638 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
639 *bx = sx;
640 *by = height - sy;
641 break;
642 case WL_OUTPUT_TRANSFORM_270:
643 *bx = sy;
644 *by = width - sx;
645 break;
646 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
647 *bx = sy;
648 *by = sx;
649 break;
650 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200651
652 *bx *= scale;
653 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200654}
655
656WL_EXPORT pixman_box32_t
657weston_transformed_rect(int width, int height,
658 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200659 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200660 pixman_box32_t rect)
661{
662 float x1, x2, y1, y2;
663
664 pixman_box32_t ret;
665
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.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200668 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200669 rect.x2, rect.y2, &x2, &y2);
670
671 if (x1 <= x2) {
672 ret.x1 = x1;
673 ret.x2 = x2;
674 } else {
675 ret.x1 = x2;
676 ret.x2 = x1;
677 }
678
679 if (y1 <= y2) {
680 ret.y1 = y1;
681 ret.y2 = y2;
682 } else {
683 ret.y1 = y2;
684 ret.y2 = y1;
685 }
686
687 return ret;
688}
689
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600690/** Transform a region by a matrix, restricted to axis-aligned transformations
691 *
692 * Warning: This function does not work for projective, affine, or matrices
693 * that encode arbitrary rotations. Only 90-degree step rotations are
694 * supported.
695 */
696WL_EXPORT void
697weston_matrix_transform_region(pixman_region32_t *dest,
698 struct weston_matrix *matrix,
699 pixman_region32_t *src)
700{
701 pixman_box32_t *src_rects, *dest_rects;
702 int nrects, i;
703
704 src_rects = pixman_region32_rectangles(src, &nrects);
705 dest_rects = malloc(nrects * sizeof(*dest_rects));
706 if (!dest_rects)
707 return;
708
709 for (i = 0; i < nrects; i++) {
710 struct weston_vector vec1 = {{
711 src_rects[i].x1, src_rects[i].y1, 0, 1
712 }};
713 weston_matrix_transform(matrix, &vec1);
714 vec1.f[0] /= vec1.f[3];
715 vec1.f[1] /= vec1.f[3];
716
717 struct weston_vector vec2 = {{
718 src_rects[i].x2, src_rects[i].y2, 0, 1
719 }};
720 weston_matrix_transform(matrix, &vec2);
721 vec2.f[0] /= vec2.f[3];
722 vec2.f[1] /= vec2.f[3];
723
724 if (vec1.f[0] < vec2.f[0]) {
725 dest_rects[i].x1 = floor(vec1.f[0]);
726 dest_rects[i].x2 = ceil(vec2.f[0]);
727 } else {
728 dest_rects[i].x1 = floor(vec2.f[0]);
729 dest_rects[i].x2 = ceil(vec1.f[0]);
730 }
731
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600732 if (vec1.f[1] < vec2.f[1]) {
733 dest_rects[i].y1 = floor(vec1.f[1]);
734 dest_rects[i].y2 = ceil(vec2.f[1]);
735 } else {
736 dest_rects[i].y1 = floor(vec2.f[1]);
737 dest_rects[i].y2 = ceil(vec1.f[1]);
738 }
739 }
740
741 pixman_region32_clear(dest);
742 pixman_region32_init_rects(dest, dest_rects, nrects);
743 free(dest_rects);
744}
745
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200746WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500747weston_transformed_region(int width, int height,
748 enum wl_output_transform transform,
749 int32_t scale,
750 pixman_region32_t *src, pixman_region32_t *dest)
751{
752 pixman_box32_t *src_rects, *dest_rects;
753 int nrects, i;
754
755 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
756 if (src != dest)
757 pixman_region32_copy(dest, src);
758 return;
759 }
760
761 src_rects = pixman_region32_rectangles(src, &nrects);
762 dest_rects = malloc(nrects * sizeof(*dest_rects));
763 if (!dest_rects)
764 return;
765
766 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
767 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
768 } else {
769 for (i = 0; i < nrects; i++) {
770 switch (transform) {
771 default:
772 case WL_OUTPUT_TRANSFORM_NORMAL:
773 dest_rects[i].x1 = src_rects[i].x1;
774 dest_rects[i].y1 = src_rects[i].y1;
775 dest_rects[i].x2 = src_rects[i].x2;
776 dest_rects[i].y2 = src_rects[i].y2;
777 break;
778 case WL_OUTPUT_TRANSFORM_90:
779 dest_rects[i].x1 = height - src_rects[i].y2;
780 dest_rects[i].y1 = src_rects[i].x1;
781 dest_rects[i].x2 = height - src_rects[i].y1;
782 dest_rects[i].y2 = src_rects[i].x2;
783 break;
784 case WL_OUTPUT_TRANSFORM_180:
785 dest_rects[i].x1 = width - src_rects[i].x2;
786 dest_rects[i].y1 = height - src_rects[i].y2;
787 dest_rects[i].x2 = width - src_rects[i].x1;
788 dest_rects[i].y2 = height - src_rects[i].y1;
789 break;
790 case WL_OUTPUT_TRANSFORM_270:
791 dest_rects[i].x1 = src_rects[i].y1;
792 dest_rects[i].y1 = width - src_rects[i].x2;
793 dest_rects[i].x2 = src_rects[i].y2;
794 dest_rects[i].y2 = width - src_rects[i].x1;
795 break;
796 case WL_OUTPUT_TRANSFORM_FLIPPED:
797 dest_rects[i].x1 = width - src_rects[i].x2;
798 dest_rects[i].y1 = src_rects[i].y1;
799 dest_rects[i].x2 = width - src_rects[i].x1;
800 dest_rects[i].y2 = src_rects[i].y2;
801 break;
802 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
803 dest_rects[i].x1 = height - src_rects[i].y2;
804 dest_rects[i].y1 = width - src_rects[i].x2;
805 dest_rects[i].x2 = height - src_rects[i].y1;
806 dest_rects[i].y2 = width - src_rects[i].x1;
807 break;
808 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
809 dest_rects[i].x1 = src_rects[i].x1;
810 dest_rects[i].y1 = height - src_rects[i].y2;
811 dest_rects[i].x2 = src_rects[i].x2;
812 dest_rects[i].y2 = height - src_rects[i].y1;
813 break;
814 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
815 dest_rects[i].x1 = src_rects[i].y1;
816 dest_rects[i].y1 = src_rects[i].x1;
817 dest_rects[i].x2 = src_rects[i].y2;
818 dest_rects[i].y2 = src_rects[i].x2;
819 break;
820 }
821 }
822 }
823
824 if (scale != 1) {
825 for (i = 0; i < nrects; i++) {
826 dest_rects[i].x1 *= scale;
827 dest_rects[i].x2 *= scale;
828 dest_rects[i].y1 *= scale;
829 dest_rects[i].y2 *= scale;
830 }
831 }
832
833 pixman_region32_clear(dest);
834 pixman_region32_init_rects(dest, dest_rects, nrects);
835 free(dest_rects);
836}
837
Jonny Lamb74130762013-11-26 18:19:46 +0100838static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300839viewport_surface_to_buffer(struct weston_surface *surface,
840 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100841{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200842 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200843 double src_width, src_height;
844 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200845
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200846 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
847 if (vp->surface.width == -1) {
848 *bx = sx;
849 *by = sy;
850 return;
851 }
Jonny Lamb74130762013-11-26 18:19:46 +0100852
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200853 src_x = 0.0;
854 src_y = 0.0;
855 src_width = surface->width_from_buffer;
856 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100857 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200858 src_x = wl_fixed_to_double(vp->buffer.src_x);
859 src_y = wl_fixed_to_double(vp->buffer.src_y);
860 src_width = wl_fixed_to_double(vp->buffer.src_width);
861 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100862 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200863
864 *bx = sx * src_width / surface->width + src_x;
865 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100866}
867
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500868WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200869weston_surface_to_buffer_float(struct weston_surface *surface,
870 float sx, float sy, float *bx, float *by)
871{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200872 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
873
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300874 /* first transform coordinates if the viewport is set */
875 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100876
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500877 weston_transformed_coord(surface->width_from_buffer,
878 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200879 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100880 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200881}
882
Derek Foreman99739672015-12-03 16:38:11 -0600883/** Transform a rectangle from surface coordinates to buffer coordinates
884 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300885 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600886 * from.
887 * \param rect The rectangle to transform.
888 * \return The transformed rectangle.
889 *
890 * Viewport and buffer transformations can only do translation, scaling,
891 * and rotations in 90-degree steps. Therefore the only loss in the
892 * conversion is coordinate rounding.
893 *
894 * However, some coordinate rounding takes place as an intermediate
895 * step before the buffer scale factor is applied, so the rectangle
896 * boundary may not be exactly as expected.
897 *
898 * This is OK for damage tracking since a little extra coverage is
899 * not a problem.
900 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200901WL_EXPORT pixman_box32_t
902weston_surface_to_buffer_rect(struct weston_surface *surface,
903 pixman_box32_t rect)
904{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200905 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100906 float xf, yf;
907
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300908 /* first transform box coordinates if the viewport is set */
909 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100910 rect.x1 = floorf(xf);
911 rect.y1 = floorf(yf);
912
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300913 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600914 rect.x2 = ceilf(xf);
915 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100916
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500917 return weston_transformed_rect(surface->width_from_buffer,
918 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200919 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200920 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200921}
922
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200923/** Transform a region from surface coordinates to buffer coordinates
924 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300925 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200926 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300927 * \param[in] surface_region The region in surface coordinates.
928 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200929 *
930 * Buffer_region must be init'd, but will be completely overwritten.
931 *
932 * Viewport and buffer transformations can only do translation, scaling,
933 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600934 * conversion is from the coordinate rounding that takes place in
935 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300936 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200937 */
938WL_EXPORT void
939weston_surface_to_buffer_region(struct weston_surface *surface,
940 pixman_region32_t *surface_region,
941 pixman_region32_t *buffer_region)
942{
943 pixman_box32_t *src_rects, *dest_rects;
944 int nrects, i;
945
946 src_rects = pixman_region32_rectangles(surface_region, &nrects);
947 dest_rects = malloc(nrects * sizeof(*dest_rects));
948 if (!dest_rects)
949 return;
950
951 for (i = 0; i < nrects; i++) {
952 dest_rects[i] = weston_surface_to_buffer_rect(surface,
953 src_rects[i]);
954 }
955
956 pixman_region32_fini(buffer_region);
957 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
958 free(dest_rects);
959}
960
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200961WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400963 struct weston_plane *plane)
964{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500965 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400966 return;
967
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 weston_view_damage_below(view);
969 view->plane = plane;
970 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400971}
972
Pekka Paalanen51723d52015-02-17 13:10:01 +0200973/** Inflict damage on the plane where the view is visible.
974 *
975 * \param view The view that causes the damage.
976 *
977 * If the view is currently on a plane (including the primary plane),
978 * take the view's boundingbox, subtract all the opaque views that cover it,
979 * and add the remaining region as damage to the plane. This corresponds
980 * to the damage inflicted to the plane if this view disappeared.
981 *
982 * A repaint is scheduled for this view.
983 *
984 * The region of all opaque views covering this view is stored in
985 * weston_view::clip and updated by view_accumulate_damage() during
986 * weston_output_repaint(). Specifically, that region matches the
987 * scenegraph as it was last painted.
988 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400989WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500990weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200991{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500992 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200993
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500994 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200995 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800997 if (view->plane)
998 pixman_region32_union(&view->plane->damage,
999 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001000 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001001 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001002}
1003
Pekka Paalanen01e00682017-03-24 16:21:06 +02001004/** Send wl_surface.enter/leave events
1005 *
1006 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001007 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001008 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001009 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001010 *
1011 * Send the enter/leave events for all protocol objects bound to the given
1012 * output by the client owning the surface.
1013 */
1014static void
1015weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001016 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001017 bool enter,
1018 bool leave)
1019{
1020 struct wl_resource *wloutput;
1021 struct wl_client *client;
1022
1023 assert(enter != leave);
1024
1025 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001026 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001027 if (wl_resource_get_client(wloutput) != client)
1028 continue;
1029
1030 if (enter)
1031 wl_surface_send_enter(surface->resource, wloutput);
1032 if (leave)
1033 wl_surface_send_leave(surface->resource, wloutput);
1034 }
1035}
1036
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301037static void
1038weston_surface_compute_protection(struct protected_surface *psurface)
1039{
1040 enum weston_hdcp_protection min_protection;
1041 bool min_protection_valid = false;
1042 struct weston_surface *surface = psurface->surface;
1043 struct weston_output *output;
1044
1045 wl_list_for_each(output, &surface->compositor->output_list, link)
1046 if (surface->output_mask & (1u << output->id)) {
1047 if (!min_protection_valid) {
1048 min_protection = output->current_protection;
1049 min_protection_valid = true;
1050 }
1051 if (output->current_protection < min_protection)
1052 min_protection = output->current_protection;
1053 }
1054 if (!min_protection_valid)
1055 min_protection = WESTON_HDCP_DISABLE;
1056
1057 surface->current_protection = min_protection;
1058 weston_protected_surface_send_event(psurface,
1059 surface->current_protection);
1060}
1061
1062static void
1063notify_surface_protection_change(void *data)
1064{
1065 struct weston_compositor *compositor = data;
1066 struct content_protection *cp;
1067 struct protected_surface *psurface;
1068
1069 cp = compositor->content_protection;
1070 cp->surface_protection_update = NULL;
1071
1072 /* Notify the clients, whose surfaces are changed */
1073 wl_list_for_each(psurface, &cp->protected_list, link)
1074 if (psurface && psurface->surface)
1075 weston_surface_compute_protection(psurface);
1076}
1077
Bryce Harrington3f650b82015-12-23 11:01:58 -08001078/**
1079 * \param es The surface
1080 * \param mask The new set of outputs for the surface
1081 *
1082 * Sets the surface's set of outputs to the ones specified by
1083 * the new output mask provided. Identifies the outputs that
1084 * have changed, the posts enter and leave events for these
1085 * outputs as appropriate.
1086 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001087static void
1088weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1089{
1090 uint32_t different = es->output_mask ^ mask;
1091 uint32_t entered = mask & different;
1092 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001093 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001094 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001095 struct weston_head *head;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301096 struct content_protection *cp;
1097 struct wl_event_loop *loop;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001098
1099 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001100 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001101 return;
1102 if (different == 0)
1103 return;
1104
1105 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001106 output_bit = 1u << output->id;
1107 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001108 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001109
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001110 wl_list_for_each(head, &output->head_list, output_link) {
1111 weston_surface_send_enter_leave(es, head,
1112 output_bit & entered,
1113 output_bit & left);
1114 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001115 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301116 /*
1117 * Change in surfaces' output mask might trigger a change in its
1118 * protection.
1119 */
1120 loop = wl_display_get_event_loop(es->compositor->wl_display);
1121 cp = es->compositor->content_protection;
1122 if (!cp || cp->surface_protection_update)
1123 return;
1124 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1125 notify_surface_protection_change,
1126 es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001127}
1128
Semi Malinene7a52fb2018-04-26 11:08:10 +02001129static void
1130notify_view_output_destroy(struct wl_listener *listener, void *data)
1131{
1132 struct weston_view *view =
1133 container_of(listener,
1134 struct weston_view, output_destroy_listener);
1135
1136 view->output = NULL;
1137 view->output_destroy_listener.notify = NULL;
1138}
1139
1140/** Set the primary output of the view
1141 *
1142 * \param view The view whose primary output to set
1143 * \param output The new primary output for the view
1144 *
1145 * Set \a output to be the primary output of the \a view.
1146 *
1147 * Notice that the assignment may be temporary; the primary output could be
1148 * automatically changed. Hence, one cannot rely on the value persisting.
1149 *
1150 * Passing NULL as /a output will set the primary output to NULL.
1151 */
1152WL_EXPORT void
1153weston_view_set_output(struct weston_view *view, struct weston_output *output)
1154{
1155 if (view->output_destroy_listener.notify) {
1156 wl_list_remove(&view->output_destroy_listener.link);
1157 view->output_destroy_listener.notify = NULL;
1158 }
1159 view->output = output;
1160 if (output) {
1161 view->output_destroy_listener.notify =
1162 notify_view_output_destroy;
1163 wl_signal_add(&output->destroy_signal,
1164 &view->output_destroy_listener);
1165 }
1166}
1167
Bryce Harrington3f650b82015-12-23 11:01:58 -08001168/** Recalculate which output(s) the surface has views displayed on
1169 *
1170 * \param es The surface to remap to outputs
1171 *
1172 * Finds the output that is showing the largest amount of one
1173 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001174 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001175 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001176 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001177 * in the output_mask for the surface.
1178 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001179static void
1180weston_surface_assign_output(struct weston_surface *es)
1181{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 struct weston_output *new_output;
1183 struct weston_view *view;
1184 pixman_region32_t region;
1185 uint32_t max, area, mask;
1186 pixman_box32_t *e;
1187
1188 new_output = NULL;
1189 max = 0;
1190 mask = 0;
1191 pixman_region32_init(&region);
1192 wl_list_for_each(view, &es->views, surface_link) {
1193 if (!view->output)
1194 continue;
1195
1196 pixman_region32_intersect(&region, &view->transform.boundingbox,
1197 &view->output->region);
1198
1199 e = pixman_region32_extents(&region);
1200 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1201
1202 mask |= view->output_mask;
1203
1204 if (area >= max) {
1205 new_output = view->output;
1206 max = area;
1207 }
1208 }
1209 pixman_region32_fini(&region);
1210
1211 es->output = new_output;
1212 weston_surface_update_output_mask(es, mask);
1213}
1214
Bryce Harrington3f650b82015-12-23 11:01:58 -08001215/** Recalculate which output(s) the view is displayed on
1216 *
1217 * \param ev The view to remap to outputs
1218 *
1219 * Identifies the set of outputs that the view is visible on,
1220 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001221 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001222 *
1223 * Also does the same for the view's surface. See
1224 * weston_surface_assign_output().
1225 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226static void
1227weston_view_assign_output(struct weston_view *ev)
1228{
1229 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001230 struct weston_output *output, *new_output;
1231 pixman_region32_t region;
1232 uint32_t max, area, mask;
1233 pixman_box32_t *e;
1234
1235 new_output = NULL;
1236 max = 0;
1237 mask = 0;
1238 pixman_region32_init(&region);
1239 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001240 if (output->destroying)
1241 continue;
1242
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001244 &output->region);
1245
1246 e = pixman_region32_extents(&region);
1247 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1248
1249 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001250 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001251
1252 if (area >= max) {
1253 new_output = output;
1254 max = area;
1255 }
1256 }
1257 pixman_region32_fini(&region);
1258
Semi Malinene7a52fb2018-04-26 11:08:10 +02001259 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 ev->output_mask = mask;
1261
1262 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001263}
1264
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001265static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001266weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1267 int from_x, int from_y, int *to_x, int *to_y)
1268{
1269 float x, y;
1270
1271 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1272 weston_view_from_global_float(to, x, y, &x, &y);
1273
1274 *to_x = round(x);
1275 *to_y = round(y);
1276}
1277
1278static void
1279weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1280{
1281 pixman_box32_t *a;
1282 pixman_box32_t b;
1283
1284 a = pixman_region32_extents(&from->geometry.scissor);
1285
1286 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1287 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1288
1289 pixman_region32_fini(&to->geometry.scissor);
1290 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1291}
1292
1293static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001294view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001296{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001297 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1298 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001299 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001300 { inbox->x1, inbox->y1 },
1301 { inbox->x1, inbox->y2 },
1302 { inbox->x2, inbox->y1 },
1303 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001304 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001305 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001306 int i;
1307
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001308 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001309 /* avoid rounding empty bbox to 1x1 */
1310 pixman_region32_init(bbox);
1311 return;
1312 }
1313
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001314 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001315 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001317 if (x < min_x)
1318 min_x = x;
1319 if (x > max_x)
1320 max_x = x;
1321 if (y < min_y)
1322 min_y = y;
1323 if (y > max_y)
1324 max_y = y;
1325 }
1326
Pekka Paalanen219b9822012-02-08 15:38:37 +02001327 int_x = floorf(min_x);
1328 int_y = floorf(min_y);
1329 pixman_region32_init_rect(bbox, int_x, int_y,
1330 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001331}
1332
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001333static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001335{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001337
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001338 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339 view->geometry.x = roundf(view->geometry.x);
1340 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001341
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001342 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1344 view->transform.position.matrix.d[12] = view->geometry.x;
1345 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001346
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001348
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349 view->transform.inverse = view->transform.position.matrix;
1350 view->transform.inverse.d[12] = -view->geometry.x;
1351 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001352
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001354 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001355 view->surface->width,
1356 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001357 if (view->geometry.scissor_enabled)
1358 pixman_region32_intersect(&view->transform.boundingbox,
1359 &view->transform.boundingbox,
1360 &view->geometry.scissor);
1361
1362 pixman_region32_translate(&view->transform.boundingbox,
1363 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001364
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 if (view->alpha == 1.0) {
1366 pixman_region32_copy(&view->transform.opaque,
1367 &view->surface->opaque);
1368 pixman_region32_translate(&view->transform.opaque,
1369 view->geometry.x,
1370 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001371 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001372}
1373
1374static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001376{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 struct weston_view *parent = view->geometry.parent;
1378 struct weston_matrix *matrix = &view->transform.matrix;
1379 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001380 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001381 pixman_region32_t surfregion;
1382 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001383
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001385
1386 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1388 view->transform.position.matrix.d[12] = view->geometry.x;
1389 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001390
1391 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001393 weston_matrix_multiply(matrix, &tform->matrix);
1394
Pekka Paalanen483243f2013-03-08 14:56:50 +02001395 if (parent)
1396 weston_matrix_multiply(matrix, &parent->transform.matrix);
1397
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001398 if (weston_matrix_invert(inverse, matrix) < 0) {
1399 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400 weston_log("error: weston_view %p"
1401 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001402 return -1;
1403 }
1404
Giulio Camuffo148c1992016-09-04 18:50:46 +03001405 if (view->alpha == 1.0 &&
1406 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1407 pixman_region32_copy(&view->transform.opaque,
1408 &view->surface->opaque);
1409 pixman_region32_translate(&view->transform.opaque,
1410 matrix->d[12],
1411 matrix->d[13]);
1412 }
1413
Pekka Paalanen380adf52015-02-16 14:39:11 +02001414 pixman_region32_init_rect(&surfregion, 0, 0,
1415 view->surface->width, view->surface->height);
1416 if (view->geometry.scissor_enabled)
1417 pixman_region32_intersect(&surfregion, &surfregion,
1418 &view->geometry.scissor);
1419 surfbox = pixman_region32_extents(&surfregion);
1420
1421 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1422 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001423
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001424 return 0;
1425}
1426
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001427static struct weston_layer *
1428get_view_layer(struct weston_view *view)
1429{
1430 if (view->parent_view)
1431 return get_view_layer(view->parent_view);
1432 return view->layer_link.layer;
1433}
1434
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001435WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001437{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001439 struct weston_layer *layer;
1440 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001441
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001443 return;
1444
Pekka Paalanen483243f2013-03-08 14:56:50 +02001445 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001447
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001451
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 pixman_region32_fini(&view->transform.boundingbox);
1453 pixman_region32_fini(&view->transform.opaque);
1454 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001455
Pekka Paalanencd403622012-01-25 13:37:39 +02001456 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 if (view->geometry.transformation_list.next ==
1458 &view->transform.position.link &&
1459 view->geometry.transformation_list.prev ==
1460 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001461 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001463 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 if (weston_view_update_transform_enable(view) < 0)
1465 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001466 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001467
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001468 layer = get_view_layer(view);
1469 if (layer) {
1470 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001471 pixman_region32_intersect(&view->transform.boundingbox,
1472 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001473 pixman_region32_intersect(&view->transform.opaque,
1474 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001475 pixman_region32_fini(&mask);
1476 }
1477
Pekka Paalanen380adf52015-02-16 14:39:11 +02001478 if (parent) {
1479 if (parent->geometry.scissor_enabled) {
1480 view->geometry.scissor_enabled = true;
1481 weston_view_transfer_scissor(parent, view);
1482 } else {
1483 view->geometry.scissor_enabled = false;
1484 }
1485 }
1486
Jason Ekstranda7af7042013-10-12 22:38:11 -05001487 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001488
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001490
Jason Ekstranda7af7042013-10-12 22:38:11 -05001491 wl_signal_emit(&view->surface->compositor->transform_signal,
1492 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001493}
1494
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001495WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001497{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001499
1500 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501 * The invariant: if view->geometry.dirty, then all views
1502 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001503 * Corollary: if not parent->geometry.dirty, then all ancestors
1504 * are not dirty.
1505 */
1506
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001508 return;
1509
Jason Ekstranda7af7042013-10-12 22:38:11 -05001510 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001511
Jason Ekstranda7af7042013-10-12 22:38:11 -05001512 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001513 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001515}
1516
1517WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518weston_view_to_global_fixed(struct weston_view *view,
1519 wl_fixed_t vx, wl_fixed_t vy,
1520 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001521{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001522 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001523
Jason Ekstranda7af7042013-10-12 22:38:11 -05001524 weston_view_to_global_float(view,
1525 wl_fixed_to_double(vx),
1526 wl_fixed_to_double(vy),
1527 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001528 *x = wl_fixed_from_double(xf);
1529 *y = wl_fixed_from_double(yf);
1530}
1531
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001532WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533weston_view_from_global_float(struct weston_view *view,
1534 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001535{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001536 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001537 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1538
Jason Ekstranda7af7042013-10-12 22:38:11 -05001539 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001540
1541 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001542 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001543 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001544 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545 *vx = 0;
1546 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001547 return;
1548 }
1549
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550 *vx = v.f[0] / v.f[3];
1551 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001552 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553 *vx = x - view->geometry.x;
1554 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001555 }
1556}
1557
1558WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559weston_view_from_global_fixed(struct weston_view *view,
1560 wl_fixed_t x, wl_fixed_t y,
1561 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001562{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001563 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001564
Jason Ekstranda7af7042013-10-12 22:38:11 -05001565 weston_view_from_global_float(view,
1566 wl_fixed_to_double(x),
1567 wl_fixed_to_double(y),
1568 &vxf, &vyf);
1569 *vx = wl_fixed_from_double(vxf);
1570 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001571}
1572
1573WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574weston_view_from_global(struct weston_view *view,
1575 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001576{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001578
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1580 *vx = floorf(vxf);
1581 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001582}
1583
Bryce Harrington3f650b82015-12-23 11:01:58 -08001584/**
1585 * \param surface The surface to be repainted
1586 *
1587 * Marks the output(s) that the surface is shown on as needing to be
1588 * repainted. See weston_output_schedule_repaint().
1589 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001590WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001591weston_surface_schedule_repaint(struct weston_surface *surface)
1592{
1593 struct weston_output *output;
1594
1595 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001596 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001597 weston_output_schedule_repaint(output);
1598}
1599
Bryce Harrington3f650b82015-12-23 11:01:58 -08001600/**
1601 * \param view The view to be repainted
1602 *
1603 * Marks the output(s) that the view is shown on as needing to be
1604 * repainted. See weston_output_schedule_repaint().
1605 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001606WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001607weston_view_schedule_repaint(struct weston_view *view)
1608{
1609 struct weston_output *output;
1610
1611 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001612 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001613 weston_output_schedule_repaint(output);
1614}
1615
Pekka Paalanene508ce62015-02-19 13:59:55 +02001616/**
1617 * XXX: This function does it the wrong way.
1618 * surface->damage is the damage from the client, and causes
1619 * surface_flush_damage() to copy pixels. No window management action can
1620 * cause damage to the client-provided content, warranting re-upload!
1621 *
1622 * Instead of surface->damage, this function should record the damage
1623 * with all the views for this surface to avoid extraneous texture
1624 * uploads.
1625 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001626WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001627weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001628{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001629 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001630 0, 0, surface->width,
1631 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001632
Kristian Høgsberg98238702012-08-03 16:29:12 -04001633 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001634}
1635
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001636WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001637weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001638{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001639 if (view->geometry.x == x && view->geometry.y == y)
1640 return;
1641
Jason Ekstranda7af7042013-10-12 22:38:11 -05001642 view->geometry.x = x;
1643 view->geometry.y = y;
1644 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001645}
1646
Pekka Paalanen483243f2013-03-08 14:56:50 +02001647static void
1648transform_parent_handle_parent_destroy(struct wl_listener *listener,
1649 void *data)
1650{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001651 struct weston_view *view =
1652 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001653 geometry.parent_destroy_listener);
1654
Jason Ekstranda7af7042013-10-12 22:38:11 -05001655 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001656}
1657
1658WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001659weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001660 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001661{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001662 if (view->geometry.parent) {
1663 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1664 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001665
1666 if (!parent)
1667 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001668 }
1669
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001671
Jason Ekstranda7af7042013-10-12 22:38:11 -05001672 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001673 transform_parent_handle_parent_destroy;
1674 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001675 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001677 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001678 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001679 }
1680
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681 weston_view_geometry_dirty(view);
1682}
1683
Pekka Paalanen380adf52015-02-16 14:39:11 +02001684/** Set a clip mask rectangle on a view
1685 *
1686 * \param view The view to set the clip mask on.
1687 * \param x Top-left corner X coordinate of the clip rectangle.
1688 * \param y Top-left corner Y coordinate of the clip rectangle.
1689 * \param width Width of the clip rectangle, non-negative.
1690 * \param height Height of the clip rectangle, non-negative.
1691 *
1692 * A shell may set a clip mask rectangle on a view. Everything outside
1693 * the rectangle is cut away for input and output purposes: it is
1694 * not drawn and cannot be hit by hit-test based input like pointer
1695 * motion or touch-downs. Everything inside the rectangle will behave
1696 * normally. Clients are unaware of clipping.
1697 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001698 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001699 * mask rectangle does not affect the view position, the view is positioned
1700 * as it would be without a clip. The clip also does not change
1701 * weston_surface::width,height.
1702 *
1703 * The clip mask rectangle is part of transformation inheritance
1704 * (weston_view_set_transform_parent()). A clip set in the root of the
1705 * transformation inheritance tree will affect all views in the tree.
1706 * A clip can be set only on the root view. Attempting to set a clip
1707 * on view that has a transformation parent will fail. Assigning a parent
1708 * to a view that has a clip set will cause the clip to be forgotten.
1709 *
1710 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1711 * on the additional transformations in the child views. These transformations
1712 * may not rotate the coordinate axes, i.e., only translation and scaling
1713 * are allowed. Violating this restriction causes the clipping to malfunction.
1714 * Furthermore, using scaling may cause rounding errors in child clipping.
1715 *
1716 * The clip mask rectangle is not automatically adjusted based on
1717 * wl_surface.attach dx and dy arguments.
1718 *
1719 * A clip mask rectangle can be set only if the compositor capability
1720 * WESTON_CAP_VIEW_CLIP_MASK is present.
1721 *
1722 * This function sets the clip mask rectangle and schedules a repaint for
1723 * the view.
1724 */
1725WL_EXPORT void
1726weston_view_set_mask(struct weston_view *view,
1727 int x, int y, int width, int height)
1728{
1729 struct weston_compositor *compositor = view->surface->compositor;
1730
1731 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1732 weston_log("%s not allowed without capability!\n", __func__);
1733 return;
1734 }
1735
1736 if (view->geometry.parent) {
1737 weston_log("view %p has a parent, clip forbidden!\n", view);
1738 return;
1739 }
1740
1741 if (width < 0 || height < 0) {
1742 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1743 x, y, width, height);
1744 return;
1745 }
1746
1747 pixman_region32_fini(&view->geometry.scissor);
1748 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1749 view->geometry.scissor_enabled = true;
1750 weston_view_geometry_dirty(view);
1751 weston_view_schedule_repaint(view);
1752}
1753
1754/** Remove the clip mask from a view
1755 *
1756 * \param view The view to remove the clip mask from.
1757 *
1758 * Removed the clip mask rectangle and schedules a repaint.
1759 *
1760 * \sa weston_view_set_mask
1761 */
1762WL_EXPORT void
1763weston_view_set_mask_infinite(struct weston_view *view)
1764{
1765 view->geometry.scissor_enabled = false;
1766 weston_view_geometry_dirty(view);
1767 weston_view_schedule_repaint(view);
1768}
1769
Armin Krezović0da12b82016-06-30 06:04:33 +02001770/* Check if view should be displayed
1771 *
1772 * The indicator is set manually when assigning
1773 * a view to a surface.
1774 *
1775 * This needs reworking. See the thread starting at:
1776 *
1777 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1778 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001779WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780weston_view_is_mapped(struct weston_view *view)
1781{
Armin Krezović0da12b82016-06-30 06:04:33 +02001782 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001783}
1784
Philipp Zabel70decd52018-09-03 20:11:15 +02001785/* Check if view is opaque in specified region
1786 *
1787 * \param view The view to check for opacity.
1788 * \param region The region to check for opacity, in view coordinates.
1789 *
1790 * Returns true if the view is opaque in the specified region, because view
1791 * alpha is 1.0 and either the opaque region set by the client contains the
1792 * specified region, or the buffer pixel format or solid color is opaque.
1793 */
1794WL_EXPORT bool
1795weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1796{
1797 pixman_region32_t r;
1798 bool ret = false;
1799
1800 if (ev->alpha < 1.0)
1801 return false;
1802
1803 if (ev->surface->is_opaque)
1804 return true;
1805
1806 if (ev->transform.dirty) {
1807 weston_log("%s: transform dirty", __func__);
1808 return false;
1809 }
1810
1811 pixman_region32_init(&r);
1812 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1813
1814 if (!pixman_region32_not_empty(&r))
1815 ret = true;
1816
1817 pixman_region32_fini(&r);
1818
1819 return ret;
1820}
1821
Armin Krezović0da12b82016-06-30 06:04:33 +02001822/* Check if a surface has a view assigned to it
1823 *
1824 * The indicator is set manually when mapping
1825 * a surface and creating a view for it.
1826 *
1827 * This needs to go. See the thread starting at:
1828 *
1829 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1830 *
1831 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001832WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001833weston_surface_is_mapped(struct weston_surface *surface)
1834{
Armin Krezović0da12b82016-06-30 06:04:33 +02001835 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001836}
1837
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001838static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001839surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001840{
1841 struct weston_view *view;
1842
1843 if (surface->width == width && surface->height == height)
1844 return;
1845
1846 surface->width = width;
1847 surface->height = height;
1848
1849 wl_list_for_each(view, &surface->views, surface_link)
1850 weston_view_geometry_dirty(view);
1851}
1852
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001853WL_EXPORT void
1854weston_surface_set_size(struct weston_surface *surface,
1855 int32_t width, int32_t height)
1856{
1857 assert(!surface->resource);
1858 surface_set_size(surface, width, height);
1859}
1860
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001861static int
1862fixed_round_up_to_int(wl_fixed_t f)
1863{
1864 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1865}
1866
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001867static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001868convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1869 int32_t width, int32_t height,
1870 uint32_t transform,
1871 int32_t scale)
1872{
1873 assert(scale > 0);
1874
1875 switch (transform) {
1876 case WL_OUTPUT_TRANSFORM_NORMAL:
1877 case WL_OUTPUT_TRANSFORM_180:
1878 case WL_OUTPUT_TRANSFORM_FLIPPED:
1879 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1880 *width_out = width / scale;
1881 *height_out = height / scale;
1882 break;
1883 case WL_OUTPUT_TRANSFORM_90:
1884 case WL_OUTPUT_TRANSFORM_270:
1885 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1886 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1887 *width_out = height / scale;
1888 *height_out = width / scale;
1889 break;
1890 default:
1891 assert(0 && "invalid transform");
1892 }
1893}
1894
1895static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001896weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001897{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001898 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001899
1900 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001901 surface->width_from_buffer = 0;
1902 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001903 return;
1904 }
1905
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001906 convert_size_by_transform_scale(&surface->width_from_buffer,
1907 &surface->height_from_buffer,
1908 surface->buffer_ref.buffer->width,
1909 surface->buffer_ref.buffer->height,
1910 vp->buffer.transform,
1911 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001912}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001913
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001914static void
1915weston_surface_update_size(struct weston_surface *surface)
1916{
1917 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1918 int32_t width, height;
1919
1920 width = surface->width_from_buffer;
1921 height = surface->height_from_buffer;
1922
1923 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001924 surface_set_size(surface,
1925 vp->surface.width, vp->surface.height);
1926 return;
1927 }
1928
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001929 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001930 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1931 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1932
1933 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001934 return;
1935 }
1936
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001937 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001938}
1939
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001940/** weston_compositor_get_time
1941 * \ingroup compositor
1942 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001943WL_EXPORT void
1944weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001945{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001946 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001947}
1948
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001949/** weston_compositor_pick_view
1950 * \ingroup compositor
1951 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001952WL_EXPORT struct weston_view *
1953weston_compositor_pick_view(struct weston_compositor *compositor,
1954 wl_fixed_t x, wl_fixed_t y,
1955 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001956{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001957 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001958 wl_fixed_t view_x, view_y;
1959 int view_ix, view_iy;
1960 int ix = wl_fixed_to_int(x);
1961 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001962
Jason Ekstranda7af7042013-10-12 22:38:11 -05001963 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001964 if (!pixman_region32_contains_point(
1965 &view->transform.boundingbox, ix, iy, NULL))
1966 continue;
1967
1968 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1969 view_ix = wl_fixed_to_int(view_x);
1970 view_iy = wl_fixed_to_int(view_y);
1971
1972 if (!pixman_region32_contains_point(&view->surface->input,
1973 view_ix, view_iy, NULL))
1974 continue;
1975
Pekka Paalanen380adf52015-02-16 14:39:11 +02001976 if (view->geometry.scissor_enabled &&
1977 !pixman_region32_contains_point(&view->geometry.scissor,
1978 view_ix, view_iy, NULL))
1979 continue;
1980
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001981 *vx = view_x;
1982 *vy = view_y;
1983 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001984 }
1985
Derek Foremanf9318d12015-05-11 15:40:11 -05001986 *vx = wl_fixed_from_int(-1000000);
1987 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001988 return NULL;
1989}
1990
1991static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001992weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001993{
Daniel Stone37816df2012-05-16 18:45:18 +01001994 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001995
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001996 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001997 return;
1998
Daniel Stone37816df2012-05-16 18:45:18 +01001999 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002000 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002001}
2002
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002003WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002004weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002005{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002006 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002007
Jason Ekstranda7af7042013-10-12 22:38:11 -05002008 if (!weston_view_is_mapped(view))
2009 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002010
Jason Ekstranda7af7042013-10-12 22:38:11 -05002011 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002012 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002013 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002014 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002015 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002016 wl_list_remove(&view->link);
2017 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018 view->output_mask = 0;
2019 weston_surface_assign_output(view->surface);
2020
2021 if (weston_surface_is_mapped(view->surface))
2022 return;
2023
2024 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002025 struct weston_touch *touch = weston_seat_get_touch(seat);
2026 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2027 struct weston_keyboard *keyboard =
2028 weston_seat_get_keyboard(seat);
2029
2030 if (keyboard && keyboard->focus == view->surface)
2031 weston_keyboard_set_focus(keyboard, NULL);
2032 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002033 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002034 if (touch && touch->focus == view)
2035 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002036 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002037}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002038
Jason Ekstranda7af7042013-10-12 22:38:11 -05002039WL_EXPORT void
2040weston_surface_unmap(struct weston_surface *surface)
2041{
2042 struct weston_view *view;
2043
Armin Krezovićf8486c32016-06-30 06:04:28 +02002044 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002045 wl_list_for_each(view, &surface->views, surface_link)
2046 weston_view_unmap(view);
2047 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002048}
2049
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002050static void
2051weston_surface_reset_pending_buffer(struct weston_surface *surface)
2052{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002053 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002054 surface->pending.sx = 0;
2055 surface->pending.sy = 0;
2056 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002057 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002058}
2059
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060WL_EXPORT void
2061weston_view_destroy(struct weston_view *view)
2062{
2063 wl_signal_emit(&view->destroy_signal, view);
2064
2065 assert(wl_list_empty(&view->geometry.child_list));
2066
2067 if (weston_view_is_mapped(view)) {
2068 weston_view_unmap(view);
2069 weston_compositor_build_view_list(view->surface->compositor);
2070 }
2071
2072 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002073 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002074
2075 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002076 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002077 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002078 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079
2080 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002081 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083 wl_list_remove(&view->surface_link);
2084
2085 free(view);
2086}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002087
2088WL_EXPORT void
2089weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002090{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002091 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002092 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002093 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002094
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002095 if (--surface->ref_count > 0)
2096 return;
2097
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002098 assert(surface->resource == NULL);
2099
Pekka Paalanenca790762015-04-17 14:23:38 +03002100 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002101
Pekka Paalanene67858b2013-04-25 13:57:42 +03002102 assert(wl_list_empty(&surface->subsurface_list_pending));
2103 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002104
Jason Ekstranda7af7042013-10-12 22:38:11 -05002105 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2106 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002107
Jason Ekstrand7b982072014-05-20 14:33:03 -05002108 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002109
Pekka Paalanende685b82012-12-04 15:58:12 +02002110 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002111 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002112
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002113 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002114 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002115 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002116
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002117 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002118 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002119
Pekka Paalanen133e4392014-09-23 22:08:46 -04002120 weston_presentation_feedback_discard_list(&surface->feedback_list);
2121
Jonas Ådahld3414f22016-07-22 17:56:31 +08002122 wl_list_for_each_safe(constraint, next_constraint,
2123 &surface->pointer_constraints,
2124 link)
2125 weston_pointer_constraint_destroy(constraint);
2126
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002127 fd_clear(&surface->acquire_fence_fd);
2128
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002129 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002130}
2131
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002132static void
2133destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002134{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002135 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002136
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002137 assert(surface);
2138
Giulio Camuffo0d379742013-11-15 22:06:15 +01002139 /* Set the resource to NULL, since we don't want to leave a
2140 * dangling pointer if the surface was refcounted and survives
2141 * the weston_surface_destroy() call. */
2142 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002143
2144 if (surface->viewport_resource)
2145 wl_resource_set_user_data(surface->viewport_resource, NULL);
2146
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002147 if (surface->synchronization_resource) {
2148 wl_resource_set_user_data(surface->synchronization_resource,
2149 NULL);
2150 }
2151
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002152 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002153}
2154
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002155static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002156weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2157{
2158 struct weston_buffer *buffer =
2159 container_of(listener, struct weston_buffer, destroy_listener);
2160
2161 wl_signal_emit(&buffer->destroy_signal, buffer);
2162 free(buffer);
2163}
2164
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002165WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002166weston_buffer_from_resource(struct wl_resource *resource)
2167{
2168 struct weston_buffer *buffer;
2169 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002170
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002171 listener = wl_resource_get_destroy_listener(resource,
2172 weston_buffer_destroy_handler);
2173
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002174 if (listener)
2175 return container_of(listener, struct weston_buffer,
2176 destroy_listener);
2177
2178 buffer = zalloc(sizeof *buffer);
2179 if (buffer == NULL)
2180 return NULL;
2181
2182 buffer->resource = resource;
2183 wl_signal_init(&buffer->destroy_signal);
2184 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002185 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002186 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002187
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002188 return buffer;
2189}
2190
2191static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002192weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2193 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002194{
Pekka Paalanende685b82012-12-04 15:58:12 +02002195 struct weston_buffer_reference *ref =
2196 container_of(listener, struct weston_buffer_reference,
2197 destroy_listener);
2198
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002199 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002200 ref->buffer = NULL;
2201}
2202
2203WL_EXPORT void
2204weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002205 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002206{
2207 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002208 ref->buffer->busy_count--;
2209 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002210 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002211 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002212 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002213 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002214 }
2215
Pekka Paalanende685b82012-12-04 15:58:12 +02002216 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002217 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002218 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002219 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002220 }
2221
Pekka Paalanende685b82012-12-04 15:58:12 +02002222 ref->buffer = buffer;
2223 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2224}
2225
2226static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002227weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2228 void *data)
2229{
2230 struct weston_buffer_release_reference *ref =
2231 container_of(listener, struct weston_buffer_release_reference,
2232 destroy_listener);
2233
2234 assert((struct wl_resource *)data == ref->buffer_release->resource);
2235 ref->buffer_release = NULL;
2236}
2237
2238static void
2239weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2240{
2241 struct wl_resource *resource = buffer_release->resource;
2242 int release_fence_fd = buffer_release->fence_fd;
2243
2244 if (release_fence_fd >= 0) {
2245 zwp_linux_buffer_release_v1_send_fenced_release(
2246 resource, release_fence_fd);
2247 } else {
2248 zwp_linux_buffer_release_v1_send_immediate_release(
2249 resource);
2250 }
2251
2252 wl_resource_destroy(resource);
2253}
2254
2255WL_EXPORT void
2256weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2257 struct weston_buffer_release *buffer_release)
2258{
2259 if (buffer_release == ref->buffer_release)
2260 return;
2261
2262 if (ref->buffer_release) {
2263 ref->buffer_release->ref_count--;
2264 wl_list_remove(&ref->destroy_listener.link);
2265 if (ref->buffer_release->ref_count == 0)
2266 weston_buffer_release_destroy(ref->buffer_release);
2267 }
2268
2269 if (buffer_release) {
2270 buffer_release->ref_count++;
2271 wl_resource_add_destroy_listener(buffer_release->resource,
2272 &ref->destroy_listener);
2273 }
2274
2275 ref->buffer_release = buffer_release;
2276 ref->destroy_listener.notify =
2277 weston_buffer_release_reference_handle_destroy;
2278}
2279
2280WL_EXPORT void
2281weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2282 struct weston_buffer_release_reference *src)
2283{
2284 weston_buffer_release_reference(dest, src->buffer_release);
2285 weston_buffer_release_reference(src, NULL);
2286}
2287
2288static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002289weston_surface_attach(struct weston_surface *surface,
2290 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002291{
2292 weston_buffer_reference(&surface->buffer_ref, buffer);
2293
Pekka Paalanena6421c42012-12-04 15:58:10 +02002294 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002295 if (weston_surface_is_mapped(surface))
2296 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002297 }
2298
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002299 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002300
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002301 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002302 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002303}
2304
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002305/** weston_compositor_damage_all
2306 * \ingroup compositor
2307 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002308WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002309weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002310{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002311 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002312
2313 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002314 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002315}
2316
Marius Vlad55d87362019-06-11 01:15:35 +03002317/**
2318 * \ingroup output
2319 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002320WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002321weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002322{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002323 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002324
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002325 pixman_region32_union(&compositor->primary_plane.damage,
2326 &compositor->primary_plane.damage,
2327 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002328 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002329}
2330
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002331static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002332surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002333{
Pekka Paalanende685b82012-12-04 15:58:12 +02002334 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002335 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002336 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002337
Pekka Paalanenb5026542014-11-12 15:09:24 +02002338 if (weston_timeline_enabled_ &&
2339 pixman_region32_not_empty(&surface->damage))
2340 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2341 TLP_OUTPUT(surface->output), TLP_END);
2342
Jason Ekstrandef540082014-06-26 10:37:36 -07002343 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002344}
2345
2346static void
2347view_accumulate_damage(struct weston_view *view,
2348 pixman_region32_t *opaque)
2349{
2350 pixman_region32_t damage;
2351
2352 pixman_region32_init(&damage);
2353 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002354 pixman_box32_t *extents;
2355
Jason Ekstranda7af7042013-10-12 22:38:11 -05002356 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002357 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002358 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002359 pixman_region32_copy(&damage, &view->surface->damage);
2360 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002361 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002362 }
2363
Pekka Paalanen380adf52015-02-16 14:39:11 +02002364 pixman_region32_intersect(&damage, &damage,
2365 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002366 pixman_region32_subtract(&damage, &damage, opaque);
2367 pixman_region32_union(&view->plane->damage,
2368 &view->plane->damage, &damage);
2369 pixman_region32_fini(&damage);
2370 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002371 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002372}
2373
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002374static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002375compositor_accumulate_damage(struct weston_compositor *ec)
2376{
2377 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002378 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002379 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002380
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002381 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002382
2383 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002384 pixman_region32_copy(&plane->clip, &clip);
2385
2386 pixman_region32_init(&opaque);
2387
Jason Ekstranda7af7042013-10-12 22:38:11 -05002388 wl_list_for_each(ev, &ec->view_list, link) {
2389 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002390 continue;
2391
Jason Ekstranda7af7042013-10-12 22:38:11 -05002392 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002393 }
2394
2395 pixman_region32_union(&clip, &clip, &opaque);
2396 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002397 }
2398
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002399 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002400
Jason Ekstranda7af7042013-10-12 22:38:11 -05002401 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002402 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002403
2404 wl_list_for_each(ev, &ec->view_list, link) {
2405 if (ev->surface->touched)
2406 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002407 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002408
2409 surface_flush_damage(ev->surface);
2410
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002411 /* Both the renderer and the backend have seen the buffer
2412 * by now. If renderer needs the buffer, it has its own
2413 * reference set. If the backend wants to keep the buffer
2414 * around for migrating the surface into a non-primary plane
2415 * later, keep_buffer is true. Otherwise, drop the core
2416 * reference now, and allow early buffer release. This enables
2417 * clients to use single-buffering.
2418 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002419 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002420 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002421 weston_buffer_release_reference(
2422 &ev->surface->buffer_release_ref, NULL);
2423 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002424 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002425}
2426
2427static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002428surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002429{
2430 struct weston_subsurface *sub;
2431
Pekka Paalanene67858b2013-04-25 13:57:42 +03002432 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002433 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002434 continue;
2435
Jason Ekstranda7af7042013-10-12 22:38:11 -05002436 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2437 wl_list_init(&sub->surface->views);
2438
2439 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002440 }
2441}
2442
2443static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002444surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002445{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002446 struct weston_subsurface *sub;
2447 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002448
Jason Ekstranda7af7042013-10-12 22:38:11 -05002449 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2450 if (sub->surface == surface)
2451 continue;
2452
George Kiagiadakised04d382014-06-13 18:10:26 +02002453 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2454 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002455 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002456 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002457
2458 surface_free_unused_subsurface_views(sub->surface);
2459 }
2460}
2461
2462static void
2463view_list_add_subsurface_view(struct weston_compositor *compositor,
2464 struct weston_subsurface *sub,
2465 struct weston_view *parent)
2466{
2467 struct weston_subsurface *child;
2468 struct weston_view *view = NULL, *iv;
2469
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002470 if (!weston_surface_is_mapped(sub->surface))
2471 return;
2472
Jason Ekstranda7af7042013-10-12 22:38:11 -05002473 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2474 if (iv->geometry.parent == parent) {
2475 view = iv;
2476 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002477 }
2478 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002479
2480 if (view) {
2481 /* Put it back in the surface's list of views */
2482 wl_list_remove(&view->surface_link);
2483 wl_list_insert(&sub->surface->views, &view->surface_link);
2484 } else {
2485 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002486 weston_view_set_position(view,
2487 sub->position.x,
2488 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002489 weston_view_set_transform_parent(view, parent);
2490 }
2491
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002492 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002493 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002494 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002495
Pekka Paalanenb188e912013-11-19 14:03:35 +02002496 if (wl_list_empty(&sub->surface->subsurface_list)) {
2497 wl_list_insert(compositor->view_list.prev, &view->link);
2498 return;
2499 }
2500
2501 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2502 if (child->surface == sub->surface)
2503 wl_list_insert(compositor->view_list.prev, &view->link);
2504 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002505 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002506 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002507}
2508
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002509/* This recursively adds the sub-surfaces for a view, relying on the
2510 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2511 * change first happens to the sub-surface list, and then automatically
2512 * propagates here. See weston_surface_damage_subsurfaces() for how the
2513 * sub-surfaces receive damage when the client changes the state.
2514 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002515static void
2516view_list_add(struct weston_compositor *compositor,
2517 struct weston_view *view)
2518{
2519 struct weston_subsurface *sub;
2520
2521 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002522
Pekka Paalanenb188e912013-11-19 14:03:35 +02002523 if (wl_list_empty(&view->surface->subsurface_list)) {
2524 wl_list_insert(compositor->view_list.prev, &view->link);
2525 return;
2526 }
2527
2528 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2529 if (sub->surface == view->surface)
2530 wl_list_insert(compositor->view_list.prev, &view->link);
2531 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002532 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002533 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002534}
2535
2536static void
2537weston_compositor_build_view_list(struct weston_compositor *compositor)
2538{
2539 struct weston_view *view;
2540 struct weston_layer *layer;
2541
2542 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002543 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002544 surface_stash_subsurface_views(view->surface);
2545
2546 wl_list_init(&compositor->view_list);
2547 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002548 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002549 view_list_add(compositor, view);
2550 }
2551 }
2552
2553 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002554 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002555 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002556}
2557
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002558static void
2559weston_output_take_feedback_list(struct weston_output *output,
2560 struct weston_surface *surface)
2561{
2562 struct weston_view *view;
2563 struct weston_presentation_feedback *feedback;
2564 uint32_t flags = 0xffffffff;
2565
2566 if (wl_list_empty(&surface->feedback_list))
2567 return;
2568
2569 /* All views must have the flag for the flag to survive. */
2570 wl_list_for_each(view, &surface->views, surface_link) {
2571 /* ignore views that are not on this output at all */
2572 if (view->output_mask & (1u << output->id))
2573 flags &= view->psf_flags;
2574 }
2575
2576 wl_list_for_each(feedback, &surface->feedback_list, link)
2577 feedback->psf_flags = flags;
2578
2579 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2580 wl_list_init(&surface->feedback_list);
2581}
2582
David Herrmann1edf44c2013-10-22 17:11:26 +02002583static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002584weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002585{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002586 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002587 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002588 struct weston_animation *animation, *next;
2589 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002590 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002591 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002592 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002593 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302594 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002595
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002596 if (output->destroying)
2597 return 0;
2598
Pekka Paalanenb5026542014-11-12 15:09:24 +02002599 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2600
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002601 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002602 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002603
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302604 /* Find the highest protection desired for an output */
2605 wl_list_for_each(ev, &ec->view_list, link) {
2606 if (ev->surface->output_mask & (1u << output->id)) {
2607 /*
2608 * The desired_protection of the output should be the
2609 * maximum of the desired_protection of the surfaces,
2610 * that are displayed on that output, to avoid
2611 * reducing the protection for existing surfaces.
2612 */
2613 if (ev->surface->desired_protection > highest_requested)
2614 highest_requested =
2615 ev->surface->desired_protection;
2616 }
2617 }
2618
2619 output->desired_protection = highest_requested;
2620
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002621 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002622 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002623 } else {
2624 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002625 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002626 ev->psf_flags = 0;
2627 }
2628 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002629
Pekka Paalanene67858b2013-04-25 13:57:42 +03002630 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002631 wl_list_for_each(ev, &ec->view_list, link) {
2632 /* Note: This operation is safe to do multiple times on the
2633 * same surface.
2634 */
2635 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002636 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002637 &ev->surface->frame_callback_list);
2638 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002639
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002640 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002641 }
2642 }
2643
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002644 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002645
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002646 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002647 pixman_region32_intersect(&output_damage,
2648 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002649 pixman_region32_subtract(&output_damage,
2650 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002651
Scott Moreauccbf29d2012-02-22 14:21:41 -07002652 if (output->dirty)
2653 weston_output_update_matrix(output);
2654
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002655 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002656
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002657 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002658
Daniel Stone09a97e22017-03-01 11:34:06 +00002659 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002660 if (r == 0)
2661 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002662
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002663 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002664
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002665 frame_time_msec = timespec_to_msec(&output->frame_time);
2666
Jonas Ådahldb773762012-06-13 00:01:21 +02002667 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002668 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002669 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002670 }
2671
Scott Moreaud64cf212012-06-08 19:40:54 -06002672 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002673 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002674 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002675 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002676
Pekka Paalanenb5026542014-11-12 15:09:24 +02002677 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2678
David Herrmann1edf44c2013-10-22 17:11:26 +02002679 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002680}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002681
Pekka Paalanen82919792014-05-21 13:51:49 +03002682static void
2683weston_output_schedule_repaint_reset(struct weston_output *output)
2684{
Daniel Stone05df8c12017-03-03 16:59:42 +00002685 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002686 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002687}
2688
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002689static int
2690weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2691 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002692{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002693 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002694 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002695 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002696
Daniel Stone6847b852017-03-01 11:34:08 +00002697 /* We're not ready yet; come back to make a decision later. */
2698 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002699 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002700
2701 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2702 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002703 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002704
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002705 /* If we're sleeping, drop the repaint machinery entirely; we will
2706 * explicitly repaint all outputs when we come back. */
2707 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2708 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2709 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002710
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002711 /* We don't actually need to repaint this output; drop it from
2712 * repaint until something causes damage. */
2713 if (!output->repaint_needed)
2714 goto err;
2715
2716 /* If repaint fails, we aren't going to get weston_output_finish_frame
2717 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002718 * something schedules a successful repaint later. As repainting may
2719 * take some time, re-read our clock as a courtesy to the next
2720 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002721 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002722 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002723 if (ret != 0)
2724 goto err;
2725
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002726 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002727 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002728
2729err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002730 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002731 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002732}
2733
2734static void
2735output_repaint_timer_arm(struct weston_compositor *compositor)
2736{
2737 struct weston_output *output;
2738 bool any_should_repaint = false;
2739 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002740 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002741
2742 weston_compositor_read_presentation_clock(compositor, &now);
2743
2744 wl_list_for_each(output, &compositor->output_list, link) {
2745 int64_t msec_to_this;
2746
2747 if (output->repaint_status != REPAINT_SCHEDULED)
2748 continue;
2749
2750 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2751 &now);
2752 if (!any_should_repaint || msec_to_this < msec_to_next)
2753 msec_to_next = msec_to_this;
2754
2755 any_should_repaint = true;
2756 }
2757
2758 if (!any_should_repaint)
2759 return;
2760
2761 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2762 * This is a workaround to allow coalescing multiple output repaints
2763 * particularly from weston_output_finish_frame()
2764 * into the same call, which would not happen if we called
2765 * output_repaint_timer_handler() directly.
2766 */
2767 if (msec_to_next < 1)
2768 msec_to_next = 1;
2769
2770 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2771}
2772
2773static int
2774output_repaint_timer_handler(void *data)
2775{
2776 struct weston_compositor *compositor = data;
2777 struct weston_output *output;
2778 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002779 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002780 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002781
2782 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002783
2784 if (compositor->backend->repaint_begin)
2785 repaint_data = compositor->backend->repaint_begin(compositor);
2786
2787 wl_list_for_each(output, &compositor->output_list, link) {
2788 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2789 if (ret)
2790 break;
2791 }
2792
2793 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002794 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002795 ret = compositor->backend->repaint_flush(compositor,
2796 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002797 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002798 if (compositor->backend->repaint_cancel)
2799 compositor->backend->repaint_cancel(compositor,
2800 repaint_data);
2801 }
2802
2803 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002804 wl_list_for_each(output, &compositor->output_list, link) {
2805 if (output->repainted)
2806 weston_output_schedule_repaint_reset(output);
2807 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002808 }
Daniel Stone6847b852017-03-01 11:34:08 +00002809
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002810 wl_list_for_each(output, &compositor->output_list, link)
2811 output->repainted = false;
2812
Daniel Stone6847b852017-03-01 11:34:08 +00002813 output_repaint_timer_arm(compositor);
2814
Pekka Paalanen0513a952014-05-21 16:17:27 +03002815 return 0;
2816}
2817
Marius Vlad55d87362019-06-11 01:15:35 +03002818/**
2819 * \ingroup output
2820 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002821WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002822weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002823 const struct timespec *stamp,
2824 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002825{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002826 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002827 int32_t refresh_nsec;
2828 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002829 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002830
Pekka Paalanenb5026542014-11-12 15:09:24 +02002831
Daniel Stone05df8c12017-03-03 16:59:42 +00002832 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002833 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2834
Daniel Stone6847b852017-03-01 11:34:08 +00002835 weston_compositor_read_presentation_clock(compositor, &now);
2836
Daniel Stone3615ce12017-03-01 11:34:05 +00002837 /* If we haven't been supplied any timestamp at all, we don't have a
2838 * timebase to work against, so any delay just wastes time. Push a
2839 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002840 if (!stamp) {
2841 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002842 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002843 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002844
Marius Vladdf9278a2018-03-06 18:56:23 +02002845 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2846 TLP_VBLANK(stamp), TLP_END);
2847
Pekka Paalanend7894d02015-07-03 15:08:53 +03002848 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002849 weston_presentation_feedback_present_list(&output->feedback_list,
2850 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002851 output->msc,
2852 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002853
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002854 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002855
Daniel Stone6847b852017-03-01 11:34:08 +00002856 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2857 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2858 -compositor->repaint_msec);
2859 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002860
2861 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002862 static bool warned;
2863
2864 if (!warned)
2865 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002866 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002867 warned = true;
2868
Daniel Stone6847b852017-03-01 11:34:08 +00002869 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002870 }
2871
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002872 /* Called from restart_repaint_loop and restart happens already after
2873 * the deadline given by repaint_msec? In that case we delay until
2874 * the deadline of the next frame, to give clients a more predictable
2875 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002876 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2877 msec_rel < 0) {
2878 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2879 timespec_add_nsec(&output->next_repaint,
2880 &output->next_repaint,
2881 refresh_nsec);
2882 }
2883 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002884
Daniel Stone3615ce12017-03-01 11:34:05 +00002885out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002886 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002887 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002888}
2889
2890static void
2891idle_repaint(void *data)
2892{
2893 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002894 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002895
Daniel Stone05df8c12017-03-03 16:59:42 +00002896 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2897 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002898 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002899 ret = output->start_repaint_loop(output);
2900 if (ret != 0)
2901 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002902}
2903
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002904WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002905weston_layer_entry_insert(struct weston_layer_entry *list,
2906 struct weston_layer_entry *entry)
2907{
2908 wl_list_insert(&list->link, &entry->link);
2909 entry->layer = list->layer;
2910}
2911
2912WL_EXPORT void
2913weston_layer_entry_remove(struct weston_layer_entry *entry)
2914{
2915 wl_list_remove(&entry->link);
2916 wl_list_init(&entry->link);
2917 entry->layer = NULL;
2918}
2919
Quentin Glidic82681572016-12-17 13:40:51 +01002920
2921/** Initialize the weston_layer struct.
2922 *
2923 * \param compositor The compositor instance
2924 * \param layer The layer to initialize
2925 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002926WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002927weston_layer_init(struct weston_layer *layer,
2928 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002929{
Quentin Glidic82681572016-12-17 13:40:51 +01002930 layer->compositor = compositor;
2931 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002932 wl_list_init(&layer->view_list.link);
2933 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002934 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002935}
2936
2937/** Sets the position of the layer in the layer list. The layer will be placed
2938 * below any layer with the same position value, if any.
2939 * This function is safe to call if the layer is already on the list, but the
2940 * layer may be moved below other layers at the same position, if any.
2941 *
2942 * \param layer The layer to modify
2943 * \param position The position the layer will be placed at
2944 */
2945WL_EXPORT void
2946weston_layer_set_position(struct weston_layer *layer,
2947 enum weston_layer_position position)
2948{
2949 struct weston_layer *below;
2950
2951 wl_list_remove(&layer->link);
2952
2953 /* layer_list is ordered from top to bottom, the last layer being the
2954 * background with the smallest position value */
2955
2956 layer->position = position;
2957 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2958 if (below->position >= layer->position) {
2959 wl_list_insert(&below->link, &layer->link);
2960 return;
2961 }
2962 }
2963 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2964}
2965
2966/** Hide a layer by taking it off the layer list.
2967 * This function is safe to call if the layer is not on the list.
2968 *
2969 * \param layer The layer to hide
2970 */
2971WL_EXPORT void
2972weston_layer_unset_position(struct weston_layer *layer)
2973{
2974 wl_list_remove(&layer->link);
2975 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002976}
2977
2978WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002979weston_layer_set_mask(struct weston_layer *layer,
2980 int x, int y, int width, int height)
2981{
2982 struct weston_view *view;
2983
2984 layer->mask.x1 = x;
2985 layer->mask.x2 = x + width;
2986 layer->mask.y1 = y;
2987 layer->mask.y2 = y + height;
2988
2989 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2990 weston_view_geometry_dirty(view);
2991 }
2992}
2993
2994WL_EXPORT void
2995weston_layer_set_mask_infinite(struct weston_layer *layer)
2996{
2997 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2998 UINT32_MAX, UINT32_MAX);
2999}
3000
Daniel Stone3b775632018-07-20 08:38:25 +01003001WL_EXPORT bool
3002weston_layer_mask_is_infinite(struct weston_layer *layer)
3003{
3004 return layer->mask.x1 == INT32_MIN &&
3005 layer->mask.y1 == INT32_MIN &&
3006 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
3007 layer->mask.y2 == INT32_MIN + UINT32_MAX;
3008}
3009
Marius Vlad55d87362019-06-11 01:15:35 +03003010/**
3011 * \ingroup output
3012 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003013WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003014weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003015{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003016 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003017 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003018
Bryce Harrington08976ac2016-08-30 12:05:16 -07003019 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3020 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003021 return;
3022
Pekka Paalanenb5026542014-11-12 15:09:24 +02003023 if (!output->repaint_needed)
3024 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
3025
Kristian Høgsbergef044142011-06-21 15:02:12 -04003026 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003027 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003028
3029 /* If we already have a repaint scheduled for our idle handler,
3030 * no need to set it again. If the repaint has been called but
3031 * not finished, then weston_output_finish_frame() will notice
3032 * that a repaint is needed and schedule one. */
3033 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003034 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003035
Daniel Stone05df8c12017-03-03 16:59:42 +00003036 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003037 assert(!output->idle_repaint_source);
3038 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3039 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003040 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003041}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003042
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003043/** weston_compositor_schedule_repaint
3044 * \ingroup compositor
3045 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003046WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003047weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3048{
3049 struct weston_output *output;
3050
3051 wl_list_for_each(output, &compositor->output_list, link)
3052 weston_output_schedule_repaint(output);
3053}
3054
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003055static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003056surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003057{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003058 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003059}
3060
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003061static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003062surface_attach(struct wl_client *client,
3063 struct wl_resource *resource,
3064 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3065{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003066 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003067 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003068
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003069 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003070 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003071 if (buffer == NULL) {
3072 wl_client_post_no_memory(client);
3073 return;
3074 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003075 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003076
Pekka Paalanende685b82012-12-04 15:58:12 +02003077 /* Attach, attach, without commit in between does not send
3078 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003079 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003080
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003081 surface->pending.sx = sx;
3082 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003083 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003084}
3085
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003086static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003087surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003088 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003089 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003090{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003091 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003092
Derek Foreman57e92ed2015-11-17 14:11:35 -06003093 if (width <= 0 || height <= 0)
3094 return;
3095
Derek Foreman152254b2015-11-26 14:17:48 -06003096 pixman_region32_union_rect(&surface->pending.damage_surface,
3097 &surface->pending.damage_surface,
3098 x, y, width, height);
3099}
3100
3101static void
3102surface_damage_buffer(struct wl_client *client,
3103 struct wl_resource *resource,
3104 int32_t x, int32_t y, int32_t width, int32_t height)
3105{
3106 struct weston_surface *surface = wl_resource_get_user_data(resource);
3107
3108 if (width <= 0 || height <= 0)
3109 return;
3110
3111 pixman_region32_union_rect(&surface->pending.damage_buffer,
3112 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003113 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003114}
3115
Kristian Høgsberg33418202011-08-16 23:01:28 -04003116static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003117destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003118{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003119 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003120
3121 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003122 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003123}
3124
3125static void
3126surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003127 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003128{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003129 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003130 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003131
3132 cb = malloc(sizeof *cb);
3133 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003134 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003135 return;
3136 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003137
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003138 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3139 callback);
3140 if (cb->resource == NULL) {
3141 free(cb);
3142 wl_resource_post_no_memory(resource);
3143 return;
3144 }
3145
Jason Ekstranda85118c2013-06-27 20:17:02 -05003146 wl_resource_set_implementation(cb->resource, NULL, cb,
3147 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003148
Pekka Paalanenbc106382012-10-10 12:49:31 +03003149 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003150}
3151
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003152static void
3153surface_set_opaque_region(struct wl_client *client,
3154 struct wl_resource *resource,
3155 struct wl_resource *region_resource)
3156{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003157 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003158 struct weston_region *region;
3159
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003160 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003161 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003162 pixman_region32_copy(&surface->pending.opaque,
3163 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003164 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003165 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003166 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003167}
3168
3169static void
3170surface_set_input_region(struct wl_client *client,
3171 struct wl_resource *resource,
3172 struct wl_resource *region_resource)
3173{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003174 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003175 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003176
3177 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003178 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003179 pixman_region32_copy(&surface->pending.input,
3180 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003181 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003182 pixman_region32_fini(&surface->pending.input);
3183 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003184 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003185}
3186
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003187/* Cause damage to this sub-surface and all its children.
3188 *
3189 * This is useful when there are state changes that need an implicit
3190 * damage, e.g. a z-order change.
3191 */
3192static void
3193weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3194{
3195 struct weston_subsurface *child;
3196
3197 weston_surface_damage(sub->surface);
3198 sub->reordered = false;
3199
3200 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3201 if (child != sub)
3202 weston_surface_damage_subsurfaces(child);
3203}
3204
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003205static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003206weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003207{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003208 struct weston_subsurface *sub;
3209
3210 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3211 parent_link_pending) {
3212 wl_list_remove(&sub->parent_link);
3213 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003214
3215 if (sub->reordered)
3216 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003217 }
3218}
3219
3220static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003221weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003222 struct weston_matrix *matrix)
3223{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003224 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003225 double src_width, src_height, dest_width, dest_height;
3226
3227 weston_matrix_init(matrix);
3228
3229 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3230 src_width = surface->width_from_buffer;
3231 src_height = surface->height_from_buffer;
3232 } else {
3233 src_width = wl_fixed_to_double(vp->buffer.src_width);
3234 src_height = wl_fixed_to_double(vp->buffer.src_height);
3235 }
3236
3237 if (vp->surface.width == -1) {
3238 dest_width = src_width;
3239 dest_height = src_height;
3240 } else {
3241 dest_width = vp->surface.width;
3242 dest_height = vp->surface.height;
3243 }
3244
3245 if (src_width != dest_width || src_height != dest_height)
3246 weston_matrix_scale(matrix,
3247 src_width / dest_width,
3248 src_height / dest_height, 1);
3249
3250 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3251 weston_matrix_translate(matrix,
3252 wl_fixed_to_double(vp->buffer.src_x),
3253 wl_fixed_to_double(vp->buffer.src_y),
3254 0);
3255
3256 switch (vp->buffer.transform) {
3257 case WL_OUTPUT_TRANSFORM_FLIPPED:
3258 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3259 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3260 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3261 weston_matrix_scale(matrix, -1, 1, 1);
3262 weston_matrix_translate(matrix,
3263 surface->width_from_buffer, 0, 0);
3264 break;
3265 }
3266
3267 switch (vp->buffer.transform) {
3268 default:
3269 case WL_OUTPUT_TRANSFORM_NORMAL:
3270 case WL_OUTPUT_TRANSFORM_FLIPPED:
3271 break;
3272 case WL_OUTPUT_TRANSFORM_90:
3273 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3274 weston_matrix_rotate_xy(matrix, 0, 1);
3275 weston_matrix_translate(matrix,
3276 surface->height_from_buffer, 0, 0);
3277 break;
3278 case WL_OUTPUT_TRANSFORM_180:
3279 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3280 weston_matrix_rotate_xy(matrix, -1, 0);
3281 weston_matrix_translate(matrix,
3282 surface->width_from_buffer,
3283 surface->height_from_buffer, 0);
3284 break;
3285 case WL_OUTPUT_TRANSFORM_270:
3286 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3287 weston_matrix_rotate_xy(matrix, 0, -1);
3288 weston_matrix_translate(matrix,
3289 0, surface->width_from_buffer, 0);
3290 break;
3291 }
3292
3293 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3294}
3295
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003296/**
3297 * Compute a + b > c while being safe to overflows.
3298 */
3299static bool
3300fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3301{
3302 return (int64_t)a + (int64_t)b > (int64_t)c;
3303}
3304
3305static bool
3306weston_surface_is_pending_viewport_source_valid(
3307 const struct weston_surface *surface)
3308{
3309 const struct weston_surface_state *pend = &surface->pending;
3310 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3311 int width_from_buffer = 0;
3312 int height_from_buffer = 0;
3313 wl_fixed_t w;
3314 wl_fixed_t h;
3315
3316 /* If viewport source rect is not set, it is always ok. */
3317 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3318 return true;
3319
3320 if (pend->newly_attached) {
3321 if (pend->buffer) {
3322 convert_size_by_transform_scale(&width_from_buffer,
3323 &height_from_buffer,
3324 pend->buffer->width,
3325 pend->buffer->height,
3326 vp->buffer.transform,
3327 vp->buffer.scale);
3328 } else {
3329 /* No buffer: viewport is irrelevant. */
3330 return true;
3331 }
3332 } else {
3333 width_from_buffer = surface->width_from_buffer;
3334 height_from_buffer = surface->height_from_buffer;
3335 }
3336
3337 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3338 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3339
3340 /* No buffer: viewport is irrelevant. */
3341 if (width_from_buffer == 0 || height_from_buffer == 0)
3342 return true;
3343
3344 /* overflow checks for wl_fixed_from_int() */
3345 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3346 return false;
3347 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3348 return false;
3349
3350 w = wl_fixed_from_int(width_from_buffer);
3351 h = wl_fixed_from_int(height_from_buffer);
3352
3353 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3354 return false;
3355 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3356 return false;
3357
3358 return true;
3359}
3360
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003361static bool
3362fixed_is_integer(wl_fixed_t v)
3363{
3364 return (v & 0xff) == 0;
3365}
3366
3367static bool
3368weston_surface_is_pending_viewport_dst_size_int(
3369 const struct weston_surface *surface)
3370{
3371 const struct weston_buffer_viewport *vp =
3372 &surface->pending.buffer_viewport;
3373
3374 if (vp->surface.width != -1) {
3375 assert(vp->surface.width > 0 && vp->surface.height > 0);
3376 return true;
3377 }
3378
3379 return fixed_is_integer(vp->buffer.src_width) &&
3380 fixed_is_integer(vp->buffer.src_height);
3381}
3382
Derek Foreman152254b2015-11-26 14:17:48 -06003383/* Translate pending damage in buffer co-ordinates to surface
3384 * co-ordinates and union it with a pixman_region32_t.
3385 * This should only be called after the buffer is attached.
3386 */
3387static void
3388apply_damage_buffer(pixman_region32_t *dest,
3389 struct weston_surface *surface,
3390 struct weston_surface_state *state)
3391{
3392 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3393
3394 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3395 * translated into surface co-ordinates and unioned with
3396 * any other surface damage.
3397 * None of this makes sense if there is no buffer though.
3398 */
3399 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3400 pixman_region32_t buffer_damage;
3401
3402 pixman_region32_intersect_rect(&state->damage_buffer,
3403 &state->damage_buffer,
3404 0, 0, buffer->width,
3405 buffer->height);
3406 pixman_region32_init(&buffer_damage);
3407 weston_matrix_transform_region(&buffer_damage,
3408 &surface->buffer_to_surface_matrix,
3409 &state->damage_buffer);
3410 pixman_region32_union(dest, dest, &buffer_damage);
3411 pixman_region32_fini(&buffer_damage);
3412 }
3413 /* We should clear this on commit even if there was no buffer */
3414 pixman_region32_clear(&state->damage_buffer);
3415}
3416
Jason Ekstrand1e059042014-10-16 10:55:19 -05003417static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303418weston_surface_set_desired_protection(struct weston_surface *surface,
3419 enum weston_hdcp_protection protection)
3420{
3421 if (surface->desired_protection == protection)
3422 return;
3423 surface->desired_protection = protection;
3424 weston_surface_damage(surface);
3425}
3426
3427static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303428weston_surface_set_protection_mode(struct weston_surface *surface,
3429 enum weston_surface_protection_mode p_mode)
3430{
3431 struct content_protection *cp = surface->compositor->content_protection;
3432 struct protected_surface *psurface;
3433
3434 surface->protection_mode = p_mode;
3435 wl_list_for_each(psurface, &cp->protected_list, link) {
3436 if (!psurface || psurface->surface != surface)
3437 continue;
3438 weston_protected_surface_send_event(psurface,
3439 surface->current_protection);
3440 }
3441}
3442
3443static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003444weston_surface_commit_state(struct weston_surface *surface,
3445 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003446{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003447 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003448 pixman_region32_t opaque;
3449
Alexander Larsson4ea95522013-05-22 14:41:37 +02003450 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003451 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003452 /* wp_viewport.set_source */
3453 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003454 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003455
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003456 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003457 if (state->newly_attached) {
3458 /* zwp_surface_synchronization_v1.set_acquire_fence */
3459 fd_move(&surface->acquire_fence_fd,
3460 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003461 /* zwp_surface_synchronization_v1.get_release */
3462 weston_buffer_release_move(&surface->buffer_release_ref,
3463 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003464 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003465 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003466 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003467 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003468 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003469
Jason Ekstrand1e059042014-10-16 10:55:19 -05003470 weston_surface_build_buffer_matrix(surface,
3471 &surface->surface_to_buffer_matrix);
3472 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3473 &surface->surface_to_buffer_matrix);
3474
Jason Ekstrand7b982072014-05-20 14:33:03 -05003475 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003476 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003477 if (surface->committed)
3478 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003479 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003480
Jason Ekstrand7b982072014-05-20 14:33:03 -05003481 state->sx = 0;
3482 state->sy = 0;
3483 state->newly_attached = 0;
3484 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003485
Derek Foreman152254b2015-11-26 14:17:48 -06003486 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003487 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003488 (pixman_region32_not_empty(&state->damage_surface) ||
3489 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003490 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003491
Pekka Paalanen8e159182012-10-10 12:49:25 +03003492 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003493 &state->damage_surface);
3494
3495 apply_damage_buffer(&surface->damage, surface, state);
3496
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003497 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003498 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003499 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003500
3501 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003502 pixman_region32_init(&opaque);
3503 pixman_region32_intersect_rect(&opaque, &state->opaque,
3504 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003505
3506 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3507 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003508 wl_list_for_each(view, &surface->views, surface_link)
3509 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003510 }
3511
3512 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003513
3514 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003515 pixman_region32_intersect_rect(&surface->input, &state->input,
3516 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003517
Pekka Paalanenbc106382012-10-10 12:49:31 +03003518 /* wl_surface.frame */
3519 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003520 &state->frame_callback_list);
3521 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003522
3523 /* XXX:
3524 * What should happen with a feedback request, if there
3525 * is no wl_buffer attached for this commit?
3526 */
3527
3528 /* presentation.feedback */
3529 wl_list_insert_list(&surface->feedback_list,
3530 &state->feedback_list);
3531 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003532
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303533 /* weston_protected_surface.enforced/relaxed */
3534 if (surface->protection_mode != state->protection_mode)
3535 weston_surface_set_protection_mode(surface,
3536 state->protection_mode);
3537
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303538 /* weston_protected_surface.set_type */
3539 weston_surface_set_desired_protection(surface, state->desired_protection);
3540
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003541 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003542}
3543
3544static void
3545weston_surface_commit(struct weston_surface *surface)
3546{
3547 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003548
Pekka Paalanene67858b2013-04-25 13:57:42 +03003549 weston_surface_commit_subsurface_order(surface);
3550
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003551 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003552}
3553
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003554static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003555weston_subsurface_commit(struct weston_subsurface *sub);
3556
3557static void
3558weston_subsurface_parent_commit(struct weston_subsurface *sub,
3559 int parent_is_synchronized);
3560
3561static void
3562surface_commit(struct wl_client *client, struct wl_resource *resource)
3563{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003564 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003565 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3566
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003567 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3568 assert(surface->viewport_resource);
3569
3570 wl_resource_post_error(surface->viewport_resource,
3571 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3572 "wl_surface@%d has viewport source outside buffer",
3573 wl_resource_get_id(resource));
3574 return;
3575 }
3576
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003577 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3578 assert(surface->viewport_resource);
3579
3580 wl_resource_post_error(surface->viewport_resource,
3581 WP_VIEWPORT_ERROR_BAD_SIZE,
3582 "wl_surface@%d viewport dst size not integer",
3583 wl_resource_get_id(resource));
3584 return;
3585 }
3586
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003587 if (surface->pending.acquire_fence_fd >= 0) {
3588 assert(surface->synchronization_resource);
3589
3590 if (!surface->pending.buffer) {
3591 fd_clear(&surface->pending.acquire_fence_fd);
3592 wl_resource_post_error(surface->synchronization_resource,
3593 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3594 "wl_surface@%"PRIu32" no buffer for synchronization",
3595 wl_resource_get_id(resource));
3596 return;
3597 }
3598
3599 /* We support fences for both wp_linux_dmabuf and opaque EGL
3600 * buffers, as mandated by minor version 2 of the
3601 * zwp_linux_explicit_synchronization_v1 protocol. Since
3602 * renderers that support fences currently only support these
3603 * two buffer types plus SHM buffers, we can just check for the
3604 * SHM buffer case here.
3605 */
3606 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3607 fd_clear(&surface->pending.acquire_fence_fd);
3608 wl_resource_post_error(surface->synchronization_resource,
3609 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3610 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3611 wl_resource_get_id(resource));
3612 return;
3613 }
3614 }
3615
Alexandros Frantzis67629672018-10-19 12:14:11 +03003616 if (surface->pending.buffer_release_ref.buffer_release &&
3617 !surface->pending.buffer) {
3618 assert(surface->synchronization_resource);
3619
3620 wl_resource_post_error(surface->synchronization_resource,
3621 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3622 "wl_surface@%"PRIu32" no buffer for synchronization",
3623 wl_resource_get_id(resource));
3624 return;
3625 }
3626
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627 if (sub) {
3628 weston_subsurface_commit(sub);
3629 return;
3630 }
3631
3632 weston_surface_commit(surface);
3633
3634 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3635 if (sub->surface != surface)
3636 weston_subsurface_parent_commit(sub, 0);
3637 }
3638}
3639
3640static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003641surface_set_buffer_transform(struct wl_client *client,
3642 struct wl_resource *resource, int transform)
3643{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003644 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003645
Jonny Lamba55f1392014-05-30 12:07:15 +02003646 /* if wl_output.transform grows more members this will need to be updated. */
3647 if (transform < 0 ||
3648 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3649 wl_resource_post_error(resource,
3650 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3651 "buffer transform must be a valid transform "
3652 "('%d' specified)", transform);
3653 return;
3654 }
3655
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003656 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003657 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003658}
3659
Alexander Larsson4ea95522013-05-22 14:41:37 +02003660static void
3661surface_set_buffer_scale(struct wl_client *client,
3662 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003663 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003664{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003665 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003666
Jonny Lamba55f1392014-05-30 12:07:15 +02003667 if (scale < 1) {
3668 wl_resource_post_error(resource,
3669 WL_SURFACE_ERROR_INVALID_SCALE,
3670 "buffer scale must be at least one "
3671 "('%d' specified)", scale);
3672 return;
3673 }
3674
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003675 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003676 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003677}
3678
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003679static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003680 surface_destroy,
3681 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003682 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003683 surface_frame,
3684 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003685 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003686 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003687 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003688 surface_set_buffer_scale,
3689 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003690};
3691
3692static void
3693compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003694 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003695{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003696 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003697 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003698
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003699 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003700 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003701 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003702 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003703 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003704
Jason Ekstranda85118c2013-06-27 20:17:02 -05003705 surface->resource =
3706 wl_resource_create(client, &wl_surface_interface,
3707 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003708 if (surface->resource == NULL) {
3709 weston_surface_destroy(surface);
3710 wl_resource_post_no_memory(resource);
3711 return;
3712 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003713 wl_resource_set_implementation(surface->resource, &surface_interface,
3714 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003715
3716 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003717}
3718
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003719static void
3720destroy_region(struct wl_resource *resource)
3721{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003722 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003723
3724 pixman_region32_fini(&region->region);
3725 free(region);
3726}
3727
3728static void
3729region_destroy(struct wl_client *client, struct wl_resource *resource)
3730{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003731 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003732}
3733
3734static void
3735region_add(struct wl_client *client, struct wl_resource *resource,
3736 int32_t x, int32_t y, int32_t width, int32_t height)
3737{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003738 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003739
3740 pixman_region32_union_rect(&region->region, &region->region,
3741 x, y, width, height);
3742}
3743
3744static void
3745region_subtract(struct wl_client *client, struct wl_resource *resource,
3746 int32_t x, int32_t y, int32_t width, int32_t height)
3747{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003748 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003749 pixman_region32_t rect;
3750
3751 pixman_region32_init_rect(&rect, x, y, width, height);
3752 pixman_region32_subtract(&region->region, &region->region, &rect);
3753 pixman_region32_fini(&rect);
3754}
3755
3756static const struct wl_region_interface region_interface = {
3757 region_destroy,
3758 region_add,
3759 region_subtract
3760};
3761
3762static void
3763compositor_create_region(struct wl_client *client,
3764 struct wl_resource *resource, uint32_t id)
3765{
3766 struct weston_region *region;
3767
3768 region = malloc(sizeof *region);
3769 if (region == NULL) {
3770 wl_resource_post_no_memory(resource);
3771 return;
3772 }
3773
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003774 pixman_region32_init(&region->region);
3775
Jason Ekstranda85118c2013-06-27 20:17:02 -05003776 region->resource =
3777 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003778 if (region->resource == NULL) {
3779 free(region);
3780 wl_resource_post_no_memory(resource);
3781 return;
3782 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003783 wl_resource_set_implementation(region->resource, &region_interface,
3784 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003785}
3786
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003787static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003788 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003789 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003790};
3791
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003792static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003793weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3794{
3795 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003796
Jason Ekstrand7b982072014-05-20 14:33:03 -05003797 weston_surface_commit_state(surface, &sub->cached);
3798 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003799
3800 weston_surface_commit_subsurface_order(surface);
3801
3802 weston_surface_schedule_repaint(surface);
3803
Jason Ekstrand7b982072014-05-20 14:33:03 -05003804 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003805}
3806
3807static void
3808weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3809{
3810 struct weston_surface *surface = sub->surface;
3811
3812 /*
3813 * If this commit would cause the surface to move by the
3814 * attach(dx, dy) parameters, the old damage region must be
3815 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003816 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003817 */
Derek Foreman152254b2015-11-26 14:17:48 -06003818 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003819 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003820 pixman_region32_union(&sub->cached.damage_surface,
3821 &sub->cached.damage_surface,
3822 &surface->pending.damage_surface);
3823 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003824
3825 if (surface->pending.newly_attached) {
3826 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003827 weston_surface_state_set_buffer(&sub->cached,
3828 surface->pending.buffer);
3829 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003830 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003831 weston_presentation_feedback_discard_list(
3832 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003833 /* zwp_surface_synchronization_v1.set_acquire_fence */
3834 fd_move(&sub->cached.acquire_fence_fd,
3835 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003836 /* zwp_surface_synchronization_v1.get_release */
3837 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3838 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003839 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303840 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303841 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003842 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003843 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003844 sub->cached.sx += surface->pending.sx;
3845 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003846
Derek Foreman152254b2015-11-26 14:17:48 -06003847 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3848
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003849 sub->cached.buffer_viewport.changed |=
3850 surface->pending.buffer_viewport.changed;
3851 sub->cached.buffer_viewport.buffer =
3852 surface->pending.buffer_viewport.buffer;
3853 sub->cached.buffer_viewport.surface =
3854 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003855
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003856 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003857
3858 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3859
3860 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3861
3862 wl_list_insert_list(&sub->cached.frame_callback_list,
3863 &surface->pending.frame_callback_list);
3864 wl_list_init(&surface->pending.frame_callback_list);
3865
Pekka Paalanen133e4392014-09-23 22:08:46 -04003866 wl_list_insert_list(&sub->cached.feedback_list,
3867 &surface->pending.feedback_list);
3868 wl_list_init(&surface->pending.feedback_list);
3869
Jason Ekstrand7b982072014-05-20 14:33:03 -05003870 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003871}
3872
Derek Foreman280e7dd2014-10-03 13:13:42 -05003873static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003874weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3875{
3876 while (sub) {
3877 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003878 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003879
3880 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003881 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003882
3883 sub = weston_surface_to_subsurface(sub->parent);
3884 }
3885
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003886 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003887}
3888
3889static void
3890weston_subsurface_commit(struct weston_subsurface *sub)
3891{
3892 struct weston_surface *surface = sub->surface;
3893 struct weston_subsurface *tmp;
3894
3895 /* Recursive check for effectively synchronized. */
3896 if (weston_subsurface_is_synchronized(sub)) {
3897 weston_subsurface_commit_to_cache(sub);
3898 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003899 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003900 /* flush accumulated state from cache */
3901 weston_subsurface_commit_to_cache(sub);
3902 weston_subsurface_commit_from_cache(sub);
3903 } else {
3904 weston_surface_commit(surface);
3905 }
3906
3907 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3908 if (tmp->surface != surface)
3909 weston_subsurface_parent_commit(tmp, 0);
3910 }
3911 }
3912}
3913
3914static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003915weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003916{
3917 struct weston_surface *surface = sub->surface;
3918 struct weston_subsurface *tmp;
3919
Pekka Paalanene67858b2013-04-25 13:57:42 +03003920 /* From now on, commit_from_cache the whole sub-tree, regardless of
3921 * the synchronized mode of each child. This sub-surface or some
3922 * of its ancestors were synchronized, so we are synchronized
3923 * all the way down.
3924 */
3925
Jason Ekstrand7b982072014-05-20 14:33:03 -05003926 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003927 weston_subsurface_commit_from_cache(sub);
3928
3929 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3930 if (tmp->surface != surface)
3931 weston_subsurface_parent_commit(tmp, 1);
3932 }
3933}
3934
3935static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003936weston_subsurface_parent_commit(struct weston_subsurface *sub,
3937 int parent_is_synchronized)
3938{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003939 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003940 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003941 wl_list_for_each(view, &sub->surface->views, surface_link)
3942 weston_view_set_position(view,
3943 sub->position.x,
3944 sub->position.y);
3945
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003946 sub->position.set = 0;
3947 }
3948
3949 if (parent_is_synchronized || sub->synchronized)
3950 weston_subsurface_synchronized_commit(sub);
3951}
3952
Pekka Paalanen8274d902014-08-06 19:36:51 +03003953static int
3954subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3955{
3956 return snprintf(buf, len, "sub-surface");
3957}
3958
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003959static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003960subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003961{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003962 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003963
Jason Ekstranda7af7042013-10-12 22:38:11 -05003964 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003965 weston_view_set_position(view,
3966 view->geometry.x + dx,
3967 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003968
3969 /* No need to check parent mappedness, because if parent is not
3970 * mapped, parent is not in a visible layer, so this sub-surface
3971 * will not be drawn either.
3972 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003973
Pekka Paalanene67858b2013-04-25 13:57:42 +03003974 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003975 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003976
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003977 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003978 * because that would call it also for the parent surface,
3979 * which might not be mapped yet. That would lead to
3980 * inconsistent state, where the window could never be
3981 * mapped.
3982 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003983 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003984 * weston_surface_is_mapped() return true, so that when the
3985 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003986 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003987 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003988 }
3989}
3990
3991static struct weston_subsurface *
3992weston_surface_to_subsurface(struct weston_surface *surface)
3993{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003994 if (surface->committed == subsurface_committed)
3995 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003996
3997 return NULL;
3998}
3999
Pekka Paalanen01388e22013-04-25 13:57:44 +03004000WL_EXPORT struct weston_surface *
4001weston_surface_get_main_surface(struct weston_surface *surface)
4002{
4003 struct weston_subsurface *sub;
4004
4005 while (surface && (sub = weston_surface_to_subsurface(surface)))
4006 surface = sub->parent;
4007
4008 return surface;
4009}
4010
Pekka Paalanen50b67472014-10-01 15:02:41 +03004011WL_EXPORT int
4012weston_surface_set_role(struct weston_surface *surface,
4013 const char *role_name,
4014 struct wl_resource *error_resource,
4015 uint32_t error_code)
4016{
4017 assert(role_name);
4018
4019 if (surface->role_name == NULL ||
4020 surface->role_name == role_name ||
4021 strcmp(surface->role_name, role_name) == 0) {
4022 surface->role_name = role_name;
4023
4024 return 0;
4025 }
4026
4027 wl_resource_post_error(error_resource, error_code,
4028 "Cannot assign role %s to wl_surface@%d,"
4029 " already has role %s\n",
4030 role_name,
4031 wl_resource_get_id(surface->resource),
4032 surface->role_name);
4033 return -1;
4034}
4035
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004036WL_EXPORT const char *
4037weston_surface_get_role(struct weston_surface *surface)
4038{
4039 return surface->role_name;
4040}
4041
Pekka Paalanen8274d902014-08-06 19:36:51 +03004042WL_EXPORT void
4043weston_surface_set_label_func(struct weston_surface *surface,
4044 int (*desc)(struct weston_surface *,
4045 char *, size_t))
4046{
4047 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02004048 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004049}
4050
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004051/** Get the size of surface contents
4052 *
4053 * \param surface The surface to query.
4054 * \param width Returns the width of raw contents.
4055 * \param height Returns the height of raw contents.
4056 *
4057 * Retrieves the raw surface content size in pixels for the given surface.
4058 * This is the whole content size in buffer pixels. If the surface
4059 * has no content or the renderer does not implement this feature,
4060 * zeroes are returned.
4061 *
4062 * This function is used to determine the buffer size needed for
4063 * a weston_surface_copy_content() call.
4064 */
4065WL_EXPORT void
4066weston_surface_get_content_size(struct weston_surface *surface,
4067 int *width, int *height)
4068{
4069 struct weston_renderer *rer = surface->compositor->renderer;
4070
4071 if (!rer->surface_get_content_size) {
4072 *width = 0;
4073 *height = 0;
4074 return;
4075 }
4076
4077 rer->surface_get_content_size(surface, width, height);
4078}
4079
Quentin Glidic248dd102016-08-12 10:41:34 +02004080/** Get the bounding box of a surface and its subsurfaces
4081 *
4082 * \param surface The surface to query.
4083 * \return The bounding box relative to the surface origin.
4084 *
4085 */
4086WL_EXPORT struct weston_geometry
4087weston_surface_get_bounding_box(struct weston_surface *surface)
4088{
4089 pixman_region32_t region;
4090 pixman_box32_t *box;
4091 struct weston_subsurface *subsurface;
4092
4093 pixman_region32_init_rect(&region,
4094 0, 0,
4095 surface->width, surface->height);
4096
4097 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4098 pixman_region32_union_rect(&region, &region,
4099 subsurface->position.x,
4100 subsurface->position.y,
4101 subsurface->surface->width,
4102 subsurface->surface->height);
4103
4104 box = pixman_region32_extents(&region);
4105 struct weston_geometry geometry = {
4106 .x = box->x1,
4107 .y = box->y1,
4108 .width = box->x2 - box->x1,
4109 .height = box->y2 - box->y1,
4110 };
4111
4112 pixman_region32_fini(&region);
4113
4114 return geometry;
4115}
4116
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004117/** Copy surface contents to system memory.
4118 *
4119 * \param surface The surface to copy from.
4120 * \param target Pointer to the target memory buffer.
4121 * \param size Size of the target buffer in bytes.
4122 * \param src_x X location on contents to copy from.
4123 * \param src_y Y location on contents to copy from.
4124 * \param width Width in pixels of the area to copy.
4125 * \param height Height in pixels of the area to copy.
4126 * \return 0 for success, -1 for failure.
4127 *
4128 * Surface contents are maintained by the renderer. They can be in a
4129 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4130 * else.
4131 *
4132 * Surface contents are copied into memory pointed to by target,
4133 * which has size bytes of space available. The target memory
4134 * may be larger than needed, but being smaller returns an error.
4135 * The extra bytes in target may or may not be written; their content is
4136 * unspecified. Size must be large enough to hold the image.
4137 *
4138 * The image in the target memory will be arranged in rows from
4139 * top to bottom, and pixels on a row from left to right. The pixel
4140 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4141 * width * 4.
4142 *
4143 * Parameters src_x and src_y define the upper-left corner in buffer
4144 * coordinates (pixels) to copy from. Parameters width and height
4145 * define the size of the area to copy in pixels.
4146 *
4147 * The rectangle defined by src_x, src_y, width, height must fit in
4148 * the surface contents. Otherwise an error is returned.
4149 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004150 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004151 * needed target buffer size and rectangle limits.
4152 *
4153 * CURRENT IMPLEMENTATION RESTRICTIONS:
4154 * - the machine must be little-endian due to Pixman formats.
4155 *
4156 * NOTE: Pixman formats are premultiplied.
4157 */
4158WL_EXPORT int
4159weston_surface_copy_content(struct weston_surface *surface,
4160 void *target, size_t size,
4161 int src_x, int src_y,
4162 int width, int height)
4163{
4164 struct weston_renderer *rer = surface->compositor->renderer;
4165 int cw, ch;
4166 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4167
4168 if (!rer->surface_copy_content)
4169 return -1;
4170
4171 weston_surface_get_content_size(surface, &cw, &ch);
4172
4173 if (src_x < 0 || src_y < 0)
4174 return -1;
4175
4176 if (width <= 0 || height <= 0)
4177 return -1;
4178
4179 if (src_x + width > cw || src_y + height > ch)
4180 return -1;
4181
4182 if (width * bytespp * height > size)
4183 return -1;
4184
4185 return rer->surface_copy_content(surface, target, size,
4186 src_x, src_y, width, height);
4187}
4188
Pekka Paalanene67858b2013-04-25 13:57:42 +03004189static void
4190subsurface_set_position(struct wl_client *client,
4191 struct wl_resource *resource, int32_t x, int32_t y)
4192{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004193 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004194
4195 if (!sub)
4196 return;
4197
4198 sub->position.x = x;
4199 sub->position.y = y;
4200 sub->position.set = 1;
4201}
4202
4203static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004204subsurface_find_sibling(struct weston_subsurface *sub,
4205 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004206{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004207 struct weston_surface *parent = sub->parent;
4208 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004209
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004210 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4211 if (sibling->surface == surface && sibling != sub)
4212 return sibling;
4213 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004214
4215 return NULL;
4216}
4217
4218static struct weston_subsurface *
4219subsurface_sibling_check(struct weston_subsurface *sub,
4220 struct weston_surface *surface,
4221 const char *request)
4222{
4223 struct weston_subsurface *sibling;
4224
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004225 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004226 if (!sibling) {
4227 wl_resource_post_error(sub->resource,
4228 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4229 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004230 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004231 return NULL;
4232 }
4233
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004234 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004235
4236 return sibling;
4237}
4238
4239static void
4240subsurface_place_above(struct wl_client *client,
4241 struct wl_resource *resource,
4242 struct wl_resource *sibling_resource)
4243{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004244 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004245 struct weston_surface *surface =
4246 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004247 struct weston_subsurface *sibling;
4248
4249 if (!sub)
4250 return;
4251
4252 sibling = subsurface_sibling_check(sub, surface, "place_above");
4253 if (!sibling)
4254 return;
4255
4256 wl_list_remove(&sub->parent_link_pending);
4257 wl_list_insert(sibling->parent_link_pending.prev,
4258 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004259
4260 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004261}
4262
4263static void
4264subsurface_place_below(struct wl_client *client,
4265 struct wl_resource *resource,
4266 struct wl_resource *sibling_resource)
4267{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004268 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004269 struct weston_surface *surface =
4270 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004271 struct weston_subsurface *sibling;
4272
4273 if (!sub)
4274 return;
4275
4276 sibling = subsurface_sibling_check(sub, surface, "place_below");
4277 if (!sibling)
4278 return;
4279
4280 wl_list_remove(&sub->parent_link_pending);
4281 wl_list_insert(&sibling->parent_link_pending,
4282 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004283
4284 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004285}
4286
4287static void
4288subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4289{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004290 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004291
4292 if (sub)
4293 sub->synchronized = 1;
4294}
4295
4296static void
4297subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4298{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004299 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004300
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004301 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004302 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004303
4304 /* If sub became effectively desynchronized, flush. */
4305 if (!weston_subsurface_is_synchronized(sub))
4306 weston_subsurface_synchronized_commit(sub);
4307 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004308}
4309
4310static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004311weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4312{
4313 wl_list_remove(&sub->parent_link);
4314 wl_list_remove(&sub->parent_link_pending);
4315 wl_list_remove(&sub->parent_destroy_listener.link);
4316 sub->parent = NULL;
4317}
4318
4319static void
4320weston_subsurface_destroy(struct weston_subsurface *sub);
4321
4322static void
4323subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4324{
4325 struct weston_subsurface *sub =
4326 container_of(listener, struct weston_subsurface,
4327 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004328 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004329
4330 /* The protocol object (wl_resource) is left inert. */
4331 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004332 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004333
4334 weston_subsurface_destroy(sub);
4335}
4336
4337static void
4338subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4339{
4340 struct weston_subsurface *sub =
4341 container_of(listener, struct weston_subsurface,
4342 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004343 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004344 assert(sub->surface != sub->parent);
4345
4346 if (weston_surface_is_mapped(sub->surface))
4347 weston_surface_unmap(sub->surface);
4348
4349 weston_subsurface_unlink_parent(sub);
4350}
4351
4352static void
4353subsurface_resource_destroy(struct wl_resource *resource)
4354{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004355 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004356
4357 if (sub)
4358 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004359}
4360
4361static void
4362subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4363{
4364 wl_resource_destroy(resource);
4365}
4366
4367static void
4368weston_subsurface_link_parent(struct weston_subsurface *sub,
4369 struct weston_surface *parent)
4370{
4371 sub->parent = parent;
4372 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004373 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004374 &sub->parent_destroy_listener);
4375
4376 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4377 wl_list_insert(&parent->subsurface_list_pending,
4378 &sub->parent_link_pending);
4379}
4380
4381static void
4382weston_subsurface_link_surface(struct weston_subsurface *sub,
4383 struct weston_surface *surface)
4384{
4385 sub->surface = surface;
4386 sub->surface_destroy_listener.notify =
4387 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004388 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004389 &sub->surface_destroy_listener);
4390}
4391
4392static void
4393weston_subsurface_destroy(struct weston_subsurface *sub)
4394{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004395 struct weston_view *view, *next;
4396
Pekka Paalanene67858b2013-04-25 13:57:42 +03004397 assert(sub->surface);
4398
4399 if (sub->resource) {
4400 assert(weston_surface_to_subsurface(sub->surface) == sub);
4401 assert(sub->parent_destroy_listener.notify ==
4402 subsurface_handle_parent_destroy);
4403
George Kiagiadakised04d382014-06-13 18:10:26 +02004404 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4405 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004406 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004407 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004408
Pekka Paalanene67858b2013-04-25 13:57:42 +03004409 if (sub->parent)
4410 weston_subsurface_unlink_parent(sub);
4411
Jason Ekstrand7b982072014-05-20 14:33:03 -05004412 weston_surface_state_fini(&sub->cached);
4413 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004414
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004415 sub->surface->committed = NULL;
4416 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004417 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004418 } else {
4419 /* the dummy weston_subsurface for the parent itself */
4420 assert(sub->parent_destroy_listener.notify == NULL);
4421 wl_list_remove(&sub->parent_link);
4422 wl_list_remove(&sub->parent_link_pending);
4423 }
4424
4425 wl_list_remove(&sub->surface_destroy_listener.link);
4426 free(sub);
4427}
4428
4429static const struct wl_subsurface_interface subsurface_implementation = {
4430 subsurface_destroy,
4431 subsurface_set_position,
4432 subsurface_place_above,
4433 subsurface_place_below,
4434 subsurface_set_sync,
4435 subsurface_set_desync
4436};
4437
4438static struct weston_subsurface *
4439weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4440 struct weston_surface *parent)
4441{
4442 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004443 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004444
Bryce Harringtonde16d892014-11-20 22:21:57 -08004445 sub = zalloc(sizeof *sub);
4446 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004447 return NULL;
4448
Jason Ekstranda7af7042013-10-12 22:38:11 -05004449 wl_list_init(&sub->unused_views);
4450
Jason Ekstranda85118c2013-06-27 20:17:02 -05004451 sub->resource =
4452 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004453 if (!sub->resource) {
4454 free(sub);
4455 return NULL;
4456 }
4457
Jason Ekstranda85118c2013-06-27 20:17:02 -05004458 wl_resource_set_implementation(sub->resource,
4459 &subsurface_implementation,
4460 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004461 weston_subsurface_link_surface(sub, surface);
4462 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004463 weston_surface_state_init(&sub->cached);
4464 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004465 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004466
4467 return sub;
4468}
4469
4470/* Create a dummy subsurface for having the parent itself in its
4471 * sub-surface lists. Makes stacking order manipulation easy.
4472 */
4473static struct weston_subsurface *
4474weston_subsurface_create_for_parent(struct weston_surface *parent)
4475{
4476 struct weston_subsurface *sub;
4477
Bryce Harringtonde16d892014-11-20 22:21:57 -08004478 sub = zalloc(sizeof *sub);
4479 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004480 return NULL;
4481
4482 weston_subsurface_link_surface(sub, parent);
4483 sub->parent = parent;
4484 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4485 wl_list_insert(&parent->subsurface_list_pending,
4486 &sub->parent_link_pending);
4487
4488 return sub;
4489}
4490
4491static void
4492subcompositor_get_subsurface(struct wl_client *client,
4493 struct wl_resource *resource,
4494 uint32_t id,
4495 struct wl_resource *surface_resource,
4496 struct wl_resource *parent_resource)
4497{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004498 struct weston_surface *surface =
4499 wl_resource_get_user_data(surface_resource);
4500 struct weston_surface *parent =
4501 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004502 struct weston_subsurface *sub;
4503 static const char where[] = "get_subsurface: wl_subsurface@";
4504
4505 if (surface == parent) {
4506 wl_resource_post_error(resource,
4507 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4508 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004509 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004510 return;
4511 }
4512
4513 if (weston_surface_to_subsurface(surface)) {
4514 wl_resource_post_error(resource,
4515 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4516 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004517 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004518 return;
4519 }
4520
Pekka Paalanen50b67472014-10-01 15:02:41 +03004521 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4522 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004523 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004524
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004525 if (weston_surface_get_main_surface(parent) == surface) {
4526 wl_resource_post_error(resource,
4527 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4528 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004529 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004530 return;
4531 }
4532
Pekka Paalanene67858b2013-04-25 13:57:42 +03004533 /* make sure the parent is in its own list */
4534 if (wl_list_empty(&parent->subsurface_list)) {
4535 if (!weston_subsurface_create_for_parent(parent)) {
4536 wl_resource_post_no_memory(resource);
4537 return;
4538 }
4539 }
4540
4541 sub = weston_subsurface_create(id, surface, parent);
4542 if (!sub) {
4543 wl_resource_post_no_memory(resource);
4544 return;
4545 }
4546
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004547 surface->committed = subsurface_committed;
4548 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004549 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004550}
4551
4552static void
4553subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4554{
4555 wl_resource_destroy(resource);
4556}
4557
4558static const struct wl_subcompositor_interface subcompositor_interface = {
4559 subcompositor_destroy,
4560 subcompositor_get_subsurface
4561};
4562
4563static void
4564bind_subcompositor(struct wl_client *client,
4565 void *data, uint32_t version, uint32_t id)
4566{
4567 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004568 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004569
Jason Ekstranda85118c2013-06-27 20:17:02 -05004570 resource =
4571 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004572 if (resource == NULL) {
4573 wl_client_post_no_memory(client);
4574 return;
4575 }
4576 wl_resource_set_implementation(resource, &subcompositor_interface,
4577 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004578}
4579
Bryce Harrington0795ece2016-08-30 12:04:26 -07004580/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004581 *
4582 * \param compositor The compositor instance
4583 * \param state The DPMS state the outputs will be set to
4584 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004585static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004586weston_compositor_dpms(struct weston_compositor *compositor,
4587 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004588{
4589 struct weston_output *output;
4590
Bryce Harrington08976ac2016-08-30 12:05:16 -07004591 wl_list_for_each(output, &compositor->output_list, link)
4592 if (output->set_dpms)
4593 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004594}
4595
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004596/** Restores the compositor to active status
4597 *
4598 * \param compositor The compositor instance
4599 *
4600 * If the compositor was in a sleeping mode, all outputs are powered
4601 * back on via DPMS. Otherwise if the compositor was inactive
4602 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4603 * signal will fire.
4604 *
4605 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004606 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004607 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004608WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004609weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004610{
Neil Roberts8b62e202013-09-30 13:14:47 +01004611 uint32_t old_state = compositor->state;
4612
4613 /* The state needs to be changed before emitting the wake
4614 * signal because that may try to schedule a repaint which
4615 * will not work if the compositor is still sleeping */
4616 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4617
4618 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004619 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004620 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004621 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004622 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004623 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004624 /* fall through */
4625 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004626 wl_event_source_timer_update(compositor->idle_source,
4627 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004628 }
4629}
4630
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004631/** Turns off rendering and frame events for the compositor.
4632 *
4633 * \param compositor The compositor instance
4634 *
4635 * This is used for example to prevent further rendering while the
4636 * compositor is shutting down.
4637 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004638 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004639 *
4640 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004641 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004642WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004643weston_compositor_offscreen(struct weston_compositor *compositor)
4644{
4645 switch (compositor->state) {
4646 case WESTON_COMPOSITOR_OFFSCREEN:
4647 return;
4648 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004649 default:
4650 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4651 wl_event_source_timer_update(compositor->idle_source, 0);
4652 }
4653}
4654
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004655/** Powers down all attached output devices
4656 *
4657 * \param compositor The compositor instance
4658 *
4659 * Causes rendering to the outputs to cease, and no frame events to be
4660 * sent. Only powers down the outputs if the compositor is not already
4661 * in sleep mode.
4662 *
4663 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004664 *
4665 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004666 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004667WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004668weston_compositor_sleep(struct weston_compositor *compositor)
4669{
4670 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4671 return;
4672
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004673 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004674 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4675 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4676}
4677
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004678/** Sets compositor to idle mode
4679 *
4680 * \param data The compositor instance
4681 *
4682 * This is called when the idle timer fires. Once the compositor is in
4683 * idle mode it requires a wake action (e.g. via
4684 * weston_compositor_wake()) to restore it. The compositor's
4685 * idle_signal will be triggered when the idle event occurs.
4686 *
4687 * Idleness can be inhibited by setting the compositor's idle_inhibit
4688 * property.
4689 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004690static int
4691idle_handler(void *data)
4692{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004693 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004694
4695 if (compositor->idle_inhibit)
4696 return 1;
4697
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004698 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004699 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004700
4701 return 1;
4702}
4703
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004704WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004705weston_plane_init(struct weston_plane *plane,
4706 struct weston_compositor *ec,
4707 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004708{
4709 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004710 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004711 plane->x = x;
4712 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004713 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004714
4715 /* Init the link so that the call to wl_list_remove() when releasing
4716 * the plane without ever stacking doesn't lead to a crash */
4717 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004718}
4719
4720WL_EXPORT void
4721weston_plane_release(struct weston_plane *plane)
4722{
Xiong Zhang97116532013-10-23 13:58:31 +08004723 struct weston_view *view;
4724
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004725 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004726 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004727
Xiong Zhang97116532013-10-23 13:58:31 +08004728 wl_list_for_each(view, &plane->compositor->view_list, link) {
4729 if (view->plane == plane)
4730 view->plane = NULL;
4731 }
4732
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004733 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004734}
4735
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004736/** weston_compositor_stack_plane
4737 * \ingroup compositor
4738 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004739WL_EXPORT void
4740weston_compositor_stack_plane(struct weston_compositor *ec,
4741 struct weston_plane *plane,
4742 struct weston_plane *above)
4743{
4744 if (above)
4745 wl_list_insert(above->link.prev, &plane->link);
4746 else
4747 wl_list_insert(&ec->plane_list, &plane->link);
4748}
4749
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004750static void
4751output_release(struct wl_client *client, struct wl_resource *resource)
4752{
4753 wl_resource_destroy(resource);
4754}
4755
4756static const struct wl_output_interface output_interface = {
4757 output_release,
4758};
4759
4760
Casey Dahlin9074db52012-04-19 22:50:09 -04004761static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004762{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004763 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004764}
4765
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004766static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004767bind_output(struct wl_client *client,
4768 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004769{
Pekka Paalanen05347622017-03-27 12:24:34 +03004770 struct weston_head *head = data;
4771 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004772 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004773 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004774
Jason Ekstranda85118c2013-06-27 20:17:02 -05004775 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004776 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004777 if (resource == NULL) {
4778 wl_client_post_no_memory(client);
4779 return;
4780 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004781
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004782 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004783 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004784 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004785
Pekka Paalanen05347622017-03-27 12:24:34 +03004786 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004787 wl_output_send_geometry(resource,
4788 output->x,
4789 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004790 head->mm_width,
4791 head->mm_height,
4792 head->subpixel,
4793 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004794 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004795 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004796 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004797 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004798
4799 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004800 wl_output_send_mode(resource,
4801 mode->flags,
4802 mode->width,
4803 mode->height,
4804 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004805 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004806
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004807 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004808 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004809}
4810
Pekka Paalanendcac3512017-12-08 14:13:34 +02004811static void
4812weston_head_add_global(struct weston_head *head)
4813{
4814 head->global = wl_global_create(head->compositor->wl_display,
4815 &wl_output_interface, 3,
4816 head, bind_output);
4817}
4818
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004819/** Remove the global wl_output protocol object
4820 *
4821 * \param head The head whose global to remove.
4822 *
4823 * Also orphans the wl_resources for this head (wl_output).
4824 */
4825static void
4826weston_head_remove_global(struct weston_head *head)
4827{
4828 struct wl_resource *resource, *tmp;
4829
4830 if (head->global)
4831 wl_global_destroy(head->global);
4832 head->global = NULL;
4833
4834 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4835 unbind_resource(resource);
4836 wl_resource_set_destructor(resource, NULL);
4837 wl_resource_set_user_data(resource, NULL);
4838 }
Roman Gilge97391c2019-03-29 13:01:06 +01004839
4840 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
4841 /* It's sufficient to unset the destructor, then the list elements
4842 * won't be accessed.
4843 */
4844 wl_resource_set_destructor(resource, NULL);
4845 }
4846 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004847}
4848
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004849/** Get the backing object of wl_output
4850 *
4851 * \param resource A wl_output protocol object.
4852 * \return The backing object (user data) of a wl_resource representing a
4853 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004854 *
4855 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004856 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004857WL_EXPORT struct weston_head *
4858weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004859{
4860 assert(wl_resource_instance_of(resource, &wl_output_interface,
4861 &output_interface));
4862
4863 return wl_resource_get_user_data(resource);
4864}
4865
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004866/** Initialize a pre-allocated weston_head
4867 *
4868 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004869 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004870 *
4871 * The head will be safe to attach, detach and release.
4872 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004873 * The name is used in logs, and can be used by compositors as a configuration
4874 * identifier.
4875 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004876 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004877 * \internal
4878 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004879WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004880weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004881{
4882 /* Add some (in)sane defaults which can be used
4883 * for checking if an output was properly configured
4884 */
4885 memset(head, 0, sizeof *head);
4886
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004887 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004888 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004889 wl_list_init(&head->output_link);
4890 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01004891 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004892 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05304893 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004894}
4895
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004896/** Send output heads changed signal
4897 *
4898 * \param output The output that changed.
4899 *
4900 * Notify that the enabled output gained and/or lost heads, or that the
4901 * associated heads may have changed their connection status. This does not
4902 * include cases where the output becomes enabled or disabled. The registered
4903 * callbacks are called after the change has successfully happened.
4904 *
4905 * If connection status change causes the compositor to attach or detach a head
4906 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03004907 *
4908 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004909 */
4910static void
4911weston_output_emit_heads_changed(struct weston_output *output)
4912{
4913 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4914 output);
4915}
4916
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004917/** Idle task for emitting heads_changed_signal */
4918static void
4919weston_compositor_call_heads_changed(void *data)
4920{
4921 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004922 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004923
4924 compositor->heads_changed_source = NULL;
4925
4926 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004927
4928 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4929 if (head->output && head->output->enabled)
4930 weston_output_emit_heads_changed(head->output);
4931 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004932}
4933
4934/** Schedule a call on idle to heads_changed callback
4935 *
4936 * \param compositor The Compositor.
4937 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004938 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004939 * \internal
4940 */
4941static void
4942weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4943{
4944 struct wl_event_loop *loop;
4945
4946 if (compositor->heads_changed_source)
4947 return;
4948
4949 loop = wl_display_get_event_loop(compositor->wl_display);
4950 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4951 weston_compositor_call_heads_changed, compositor);
4952}
4953
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004954/** Register a new head
4955 *
4956 * \param compositor The compositor.
4957 * \param head The head to register, must not be already registered.
4958 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004959 * This signals the core that a new head has become available, leading to
4960 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004961 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004962 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004963 * \internal
4964 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004965WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004966weston_compositor_add_head(struct weston_compositor *compositor,
4967 struct weston_head *head)
4968{
4969 assert(wl_list_empty(&head->compositor_link));
4970 assert(head->name);
4971
4972 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4973 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004974 weston_compositor_schedule_heads_changed(compositor);
4975}
4976
4977/** Adds a listener to be called when heads change
4978 *
4979 * \param compositor The compositor.
4980 * \param listener The listener to add.
4981 *
Marius Vlada2dace22019-06-12 16:05:44 +03004982 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004983 *
4984 * The listener function will be called after heads are added or their
4985 * connection status has changed. Several changes may be accumulated into a
4986 * single call. The user is expected to iterate over the existing heads and
4987 * check their statuses to find out what changed.
4988 *
4989 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4990 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004991 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004992 */
4993WL_EXPORT void
4994weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4995 struct wl_listener *listener)
4996{
4997 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004998}
4999
5000/** Iterate over available heads
5001 *
5002 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005003 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005004 * \return The next available head in the list.
5005 *
5006 * Returns all available heads, regardless of being connected or enabled.
5007 *
5008 * You can iterate over all heads as follows:
5009 * \code
5010 * struct weston_head *head = NULL;
5011 *
5012 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5013 * ...
5014 * }
5015 * \endcode
5016 *
5017 * If you cause \c iter to be removed from the list, you cannot use it to
5018 * continue iterating. Removing any other item is safe.
5019 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005020 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005021 */
5022WL_EXPORT struct weston_head *
5023weston_compositor_iterate_heads(struct weston_compositor *compositor,
5024 struct weston_head *iter)
5025{
5026 struct wl_list *list = &compositor->head_list;
5027 struct wl_list *node;
5028
5029 assert(compositor);
5030 assert(!iter || iter->compositor == compositor);
5031
5032 if (iter)
5033 node = iter->compositor_link.next;
5034 else
5035 node = list->next;
5036
5037 assert(node);
5038 assert(!iter || node != &iter->compositor_link);
5039
5040 if (node == list)
5041 return NULL;
5042
5043 return container_of(node, struct weston_head, compositor_link);
5044}
5045
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005046/** Iterate over attached heads
5047 *
5048 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005049 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005050 * \return The next attached head in the list.
5051 *
5052 * Returns all heads currently attached to the output.
5053 *
5054 * You can iterate over all heads as follows:
5055 * \code
5056 * struct weston_head *head = NULL;
5057 *
5058 * while ((head = weston_output_iterate_heads(output, head))) {
5059 * ...
5060 * }
5061 * \endcode
5062 *
5063 * If you cause \c iter to be removed from the list, you cannot use it to
5064 * continue iterating. Removing any other item is safe.
5065 *
Marius Vlad55d87362019-06-11 01:15:35 +03005066 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005067 */
5068WL_EXPORT struct weston_head *
5069weston_output_iterate_heads(struct weston_output *output,
5070 struct weston_head *iter)
5071{
5072 struct wl_list *list = &output->head_list;
5073 struct wl_list *node;
5074
5075 assert(output);
5076 assert(!iter || iter->output == output);
5077
5078 if (iter)
5079 node = iter->output_link.next;
5080 else
5081 node = list->next;
5082
5083 assert(node);
5084 assert(!iter || node != &iter->output_link);
5085
5086 if (node == list)
5087 return NULL;
5088
5089 return container_of(node, struct weston_head, output_link);
5090}
5091
Pekka Paalanendcac3512017-12-08 14:13:34 +02005092/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005093 *
5094 * \param output The output to attach to.
5095 * \param head The head that is not yet attached.
5096 * \return 0 on success, -1 on failure.
5097 *
5098 * Attaches the given head to the output. All heads of an output are clones
5099 * and share the resolution and timings.
5100 *
5101 * Cloning heads this way uses less resources than creating an output for
5102 * each head, but is not always possible due to environment, driver and hardware
5103 * limitations.
5104 *
5105 * On failure, the head remains unattached. Success of this function does not
5106 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005107 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005108 *
Marius Vlad55d87362019-06-11 01:15:35 +03005109 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005110 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005111WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005112weston_output_attach_head(struct weston_output *output,
5113 struct weston_head *head)
5114{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005115 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005116
5117 if (!wl_list_empty(&head->output_link))
5118 return -1;
5119
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005120 if (output->attach_head) {
5121 if (output->attach_head(output, head) < 0)
5122 return -1;
5123 } else if (!wl_list_empty(&output->head_list)) {
5124 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005125 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005126 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005127
5128 head->output = output;
5129 wl_list_insert(output->head_list.prev, &head->output_link);
5130
Pekka Paalanendcac3512017-12-08 14:13:34 +02005131 if (output->enabled) {
5132 weston_head_add_global(head);
5133
5134 head_names = weston_output_create_heads_string(output);
5135 weston_log("Output '%s' updated to have head(s) %s\n",
5136 output->name, head_names);
5137 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005138
5139 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005140 }
5141
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005142 return 0;
5143}
5144
5145/** Detach a head from its output
5146 *
5147 * \param head The head to detach.
5148 *
5149 * It is safe to detach a non-attached head.
5150 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005151 * If the head is attached to an enabled output and the output will be left
5152 * with no heads, the output will be disabled.
5153 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005154 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005155 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005156 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005157WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005158weston_head_detach(struct weston_head *head)
5159{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005160 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005161 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005162
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005163 wl_list_remove(&head->output_link);
5164 wl_list_init(&head->output_link);
5165 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005166
5167 if (!output)
5168 return;
5169
5170 if (output->detach_head)
5171 output->detach_head(output, head);
5172
5173 if (output->enabled) {
5174 weston_head_remove_global(head);
5175
Pekka Paalanena0106992017-12-08 16:11:17 +02005176 if (wl_list_empty(&output->head_list)) {
5177 weston_log("Output '%s' no heads left, disabling.\n",
5178 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005179 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005180 } else {
5181 head_names = weston_output_create_heads_string(output);
5182 weston_log("Output '%s' updated to have head(s) %s\n",
5183 output->name, head_names);
5184 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005185
5186 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005187 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005188 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005189}
5190
5191/** Destroy a head
5192 *
5193 * \param head The head to be released.
5194 *
5195 * Destroys the head. The caller is responsible for freeing the memory pointed
5196 * to by \c head.
5197 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005198 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005199 * \internal
5200 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005201WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005202weston_head_release(struct weston_head *head)
5203{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005204 wl_signal_emit(&head->destroy_signal, head);
5205
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005206 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005207
5208 free(head->make);
5209 free(head->model);
5210 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005211 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005212
5213 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005214}
5215
Pekka Paalanene19970f2017-08-28 14:11:02 +03005216static void
5217weston_head_set_device_changed(struct weston_head *head)
5218{
5219 head->device_changed = true;
5220
5221 if (head->compositor)
5222 weston_compositor_schedule_heads_changed(head->compositor);
5223}
5224
5225/** String equal comparison with NULLs being equal */
5226static bool
5227str_null_eq(const char *a, const char *b)
5228{
5229 if (!a && !b)
5230 return true;
5231
5232 if (!!a != !!b)
5233 return false;
5234
5235 return strcmp(a, b) == 0;
5236}
5237
Pekka Paalanen01f60212017-03-24 15:39:24 +02005238/** Store monitor make, model and serial number
5239 *
5240 * \param head The head to modify.
5241 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5242 * any string, or NULL for none.
5243 * \param model The monitor model or name, or a made-up string, or NULL for
5244 * none.
5245 * \param serialno The monitor serial number, a made-up string, or NULL for
5246 * none.
5247 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005248 * This may set the device_changed flag.
5249 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005250 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005251 * \internal
5252 */
5253WL_EXPORT void
5254weston_head_set_monitor_strings(struct weston_head *head,
5255 const char *make,
5256 const char *model,
5257 const char *serialno)
5258{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005259 if (str_null_eq(head->make, make) &&
5260 str_null_eq(head->model, model) &&
5261 str_null_eq(head->serial_number, serialno))
5262 return;
5263
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005264 free(head->make);
5265 free(head->model);
5266 free(head->serial_number);
5267
5268 head->make = make ? strdup(make) : NULL;
5269 head->model = model ? strdup(model) : NULL;
5270 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005271
5272 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005273}
5274
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005275/** Store display non-desktop status
5276 *
5277 * \param head The head to modify.
5278 * \param non_desktop Whether the head connects to a non-desktop display.
5279 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005280 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005281 * \internal
5282 */
5283WL_EXPORT void
5284weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5285{
5286 if (head->non_desktop == non_desktop)
5287 return;
5288
5289 head->non_desktop = non_desktop;
5290
5291 weston_head_set_device_changed(head);
5292}
5293
Pekka Paalanen01f60212017-03-24 15:39:24 +02005294/** Store physical image size
5295 *
5296 * \param head The head to modify.
5297 * \param mm_width Image area width in millimeters.
5298 * \param mm_height Image area height in millimeters.
5299 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005300 * This may set the device_changed flag.
5301 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005302 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005303 * \internal
5304 */
5305WL_EXPORT void
5306weston_head_set_physical_size(struct weston_head *head,
5307 int32_t mm_width, int32_t mm_height)
5308{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005309 if (head->mm_width == mm_width &&
5310 head->mm_height == mm_height)
5311 return;
5312
Pekka Paalanen01f60212017-03-24 15:39:24 +02005313 head->mm_width = mm_width;
5314 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005315
5316 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005317}
5318
5319/** Store monitor sub-pixel layout
5320 *
5321 * \param head The head to modify.
5322 * \param sp Sub-pixel layout. The possible values are:
5323 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5324 * - WL_OUTPUT_SUBPIXEL_NONE,
5325 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5326 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5327 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5328 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5329 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005330 * This may set the device_changed flag.
5331 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005332 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005333 * \internal
5334 */
5335WL_EXPORT void
5336weston_head_set_subpixel(struct weston_head *head,
5337 enum wl_output_subpixel sp)
5338{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005339 if (head->subpixel == sp)
5340 return;
5341
Pekka Paalanen01f60212017-03-24 15:39:24 +02005342 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005343
5344 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005345}
5346
5347/** Mark the monitor as internal
5348 *
5349 * This is used for embedded screens, like laptop panels.
5350 *
5351 * \param head The head to mark as internal.
5352 *
5353 * By default a head is external. The type is often inferred from the physical
5354 * connector type.
5355 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005356 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005357 * \internal
5358 */
5359WL_EXPORT void
5360weston_head_set_internal(struct weston_head *head)
5361{
5362 head->connection_internal = true;
5363}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005364
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005365/** Store connector status
5366 *
5367 * \param head The head to modify.
5368 * \param connected Whether the head is connected.
5369 *
5370 * Connectors are created as disconnected. This function can be used to
5371 * set the connector status.
5372 *
5373 * The status should be set to true when a physical connector is connected to
5374 * a video sink device like a monitor and to false when the connector is
5375 * disconnected. For nested backends, the connection status should reflect the
5376 * connection to the parent display server.
5377 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005378 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005379 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005380 *
5381 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005382 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005383 * \internal
5384 */
5385WL_EXPORT void
5386weston_head_set_connection_status(struct weston_head *head, bool connected)
5387{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005388 if (head->connected == connected)
5389 return;
5390
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005391 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005392
Pekka Paalanene19970f2017-08-28 14:11:02 +03005393 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005394}
5395
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305396static void
5397weston_output_compute_protection(struct weston_output *output)
5398{
5399 struct weston_head *head;
5400 enum weston_hdcp_protection op_protection;
5401 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305402 struct weston_compositor *wc = output->compositor;
5403 struct content_protection *cp = wc->content_protection;
5404
5405 if (!cp)
5406 return;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305407
5408 wl_list_for_each(head, &output->head_list, output_link) {
5409 if (!op_protection_valid) {
5410 op_protection = head->current_protection;
5411 op_protection_valid = true;
5412 }
5413 if (head->current_protection < op_protection)
5414 op_protection = head->current_protection;
5415 }
5416
5417 if (!op_protection_valid)
5418 op_protection = WESTON_HDCP_DISABLE;
5419
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305420 if (output->current_protection != op_protection) {
5421 struct wl_event_loop *loop;
5422
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305423 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305424 weston_output_damage(output);
5425 if (cp->surface_protection_update)
5426 return;
5427 loop = wl_display_get_event_loop(wc->wl_display);
5428 cp->surface_protection_update = wl_event_loop_add_idle(loop,
5429 notify_surface_protection_change,
5430 wc);
5431 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305432}
5433
5434WL_EXPORT void
5435weston_head_set_content_protection_status(struct weston_head *head,
5436 enum weston_hdcp_protection status)
5437{
5438 head->current_protection = status;
5439 if (head->output)
5440 weston_output_compute_protection(head->output);
5441}
5442
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005443/** Is the head currently connected?
5444 *
5445 * \param head The head to query.
5446 * \return Connection status.
5447 *
5448 * Returns true if the head is physically connected to a monitor, or in
5449 * case of a nested backend returns true when there is a connection to the
5450 * parent display server.
5451 *
5452 * This is independent from the head being enabled.
5453 *
5454 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005455 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005456 */
5457WL_EXPORT bool
5458weston_head_is_connected(struct weston_head *head)
5459{
5460 return head->connected;
5461}
5462
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005463/** Is the head currently enabled?
5464 *
5465 * \param head The head to query.
5466 * \return Video status.
5467 *
5468 * Returns true if the head is currently transmitting a video stream.
5469 *
5470 * This is independent of the head being connected.
5471 *
5472 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005473 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005474 */
5475WL_EXPORT bool
5476weston_head_is_enabled(struct weston_head *head)
5477{
5478 if (!head->output)
5479 return false;
5480
5481 return head->output->enabled;
5482}
5483
Pekka Paalanene19970f2017-08-28 14:11:02 +03005484/** Has the device information changed?
5485 *
5486 * \param head The head to query.
5487 * \return True if the device information has changed since last reset.
5488 *
5489 * The information about the connected display device, e.g. a monitor, may
5490 * change without being disconnected in between. Changing information
5491 * causes a call to the heads_changed hook.
5492 *
5493 * The information includes make, model, serial number, physical size,
5494 * and sub-pixel type. The connection status is also included.
5495 *
5496 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005497 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005498 */
5499WL_EXPORT bool
5500weston_head_is_device_changed(struct weston_head *head)
5501{
5502 return head->device_changed;
5503}
5504
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005505/** Does the head represent a non-desktop display?
5506 *
5507 * \param head The head to query.
5508 * \return True if the device is a non-desktop display.
5509 *
5510 * Non-desktop heads are not attached to outputs by default.
5511 * This stops weston from extending the desktop onto head mounted displays.
5512 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005513 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005514 */
5515WL_EXPORT bool
5516weston_head_is_non_desktop(struct weston_head *head)
5517{
5518 return head->non_desktop;
5519}
5520
Pekka Paalanene19970f2017-08-28 14:11:02 +03005521/** Acknowledge device information change
5522 *
5523 * \param head The head to acknowledge.
5524 *
5525 * Clears the device changed flag on this head. When a compositor has processed
5526 * device information, it should call this to be able to notice further
5527 * changes.
5528 *
5529 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005530 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005531 */
5532WL_EXPORT void
5533weston_head_reset_device_changed(struct weston_head *head)
5534{
5535 head->device_changed = false;
5536}
5537
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005538/** Get the name of a head
5539 *
5540 * \param head The head to query.
5541 * \return The head's name, not NULL.
5542 *
5543 * The name depends on the backend. The DRM backend uses connector names,
5544 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005545 *
5546 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005547 */
5548WL_EXPORT const char *
5549weston_head_get_name(struct weston_head *head)
5550{
5551 return head->name;
5552}
5553
5554/** Get the output the head is attached to
5555 *
5556 * \param head The head to query.
5557 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005558 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005559 */
5560WL_EXPORT struct weston_output *
5561weston_head_get_output(struct weston_head *head)
5562{
5563 return head->output;
5564}
5565
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005566/** Add destroy callback for a head
5567 *
5568 * \param head The head to watch for.
5569 * \param listener The listener to add. The \c notify member must be set.
5570 *
5571 * Heads may get destroyed for various reasons by the backends. If a head is
5572 * attached to an output, the compositor should listen for head destruction
5573 * and reconfigure or destroy the output if necessary.
5574 *
5575 * The destroy callbacks will be called on weston_head destruction before any
5576 * automatic detaching from an associated weston_output and before any
5577 * weston_head information is lost.
5578 *
5579 * The \c data argument to the notify callback is the weston_head being
5580 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005581 *
5582 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005583 */
5584WL_EXPORT void
5585weston_head_add_destroy_listener(struct weston_head *head,
5586 struct wl_listener *listener)
5587{
5588 wl_signal_add(&head->destroy_signal, listener);
5589}
5590
5591/** Look up destroy listener for a head
5592 *
5593 * \param head The head to query.
5594 * \param notify The notify function used used for the added destroy listener.
5595 * \return The listener, or NULL if not found.
5596 *
5597 * This looks up the previously added destroy listener struct based on the
5598 * notify function it has. The listener can be used to access user data
5599 * through \c container_of().
5600 *
5601 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005602 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005603 */
5604WL_EXPORT struct wl_listener *
5605weston_head_get_destroy_listener(struct weston_head *head,
5606 wl_notify_func_t notify)
5607{
5608 return wl_signal_get(&head->destroy_signal, notify);
5609}
5610
David Fort0de859e2016-05-27 23:22:57 +02005611/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005612static void
David Fort0de859e2016-05-27 23:22:57 +02005613weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5614 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005615{
5616 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005617 bool start_resizing = false;
5618
5619 if (!delta_width)
5620 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005621
5622 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005623 if (output == resized_output) {
5624 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005625 continue;
5626 }
5627
David Fort0de859e2016-05-27 23:22:57 +02005628 if (start_resizing) {
5629 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005630 output->dirty = 1;
5631 }
5632 }
5633}
5634
Pekka Paalanend72bad22017-03-29 17:01:41 +03005635static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005636weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005637{
Scott Moreau850ca422012-05-21 15:21:25 -06005638 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005639
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005640 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005641 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005642
Scott Moreauccbf29d2012-02-22 14:21:41 -07005643 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005644 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005645 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005646 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005647 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005648 weston_matrix_scale(&output->matrix, magnification,
5649 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005650 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005651
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005652 switch (output->transform) {
5653 case WL_OUTPUT_TRANSFORM_FLIPPED:
5654 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5655 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5656 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5657 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5658 weston_matrix_scale(&output->matrix, -1, 1, 1);
5659 break;
5660 }
5661
5662 switch (output->transform) {
5663 default:
5664 case WL_OUTPUT_TRANSFORM_NORMAL:
5665 case WL_OUTPUT_TRANSFORM_FLIPPED:
5666 break;
5667 case WL_OUTPUT_TRANSFORM_90:
5668 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5669 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5670 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5671 break;
5672 case WL_OUTPUT_TRANSFORM_180:
5673 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5674 weston_matrix_translate(&output->matrix,
5675 -output->width, -output->height, 0);
5676 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5677 break;
5678 case WL_OUTPUT_TRANSFORM_270:
5679 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5680 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5681 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5682 break;
5683 }
5684
5685 if (output->current_scale != 1)
5686 weston_matrix_scale(&output->matrix,
5687 output->current_scale,
5688 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005689
Scott Moreauccbf29d2012-02-22 14:21:41 -07005690 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005691
5692 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005693}
5694
Scott Moreau1bad5db2012-08-18 01:04:05 -06005695static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005696weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005697{
5698 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005699 output->native_scale = scale;
5700 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005701
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005702 convert_size_by_transform_scale(&output->width, &output->height,
5703 output->current_mode->width,
5704 output->current_mode->height,
5705 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005706}
5707
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005708static void
5709weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005710{
5711 output->x = x;
5712 output->y = y;
5713
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005714 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005715 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005716
5717 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005718 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005719 output->width,
5720 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005721}
5722
Marius Vlad55d87362019-06-11 01:15:35 +03005723/**
5724 * \ingroup output
5725 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005726WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005727weston_output_move(struct weston_output *output, int x, int y)
5728{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005729 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005730 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005731 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005732
5733 output->move_x = x - output->x;
5734 output->move_y = y - output->y;
5735
5736 if (output->move_x == 0 && output->move_y == 0)
5737 return;
5738
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005739 weston_output_init_geometry(output, x, y);
5740
5741 output->dirty = 1;
5742
5743 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005744 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005745
5746 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005747 wl_list_for_each(head, &output->head_list, output_link) {
5748 wl_resource_for_each(resource, &head->resource_list) {
5749 wl_output_send_geometry(resource,
5750 output->x,
5751 output->y,
5752 head->mm_width,
5753 head->mm_height,
5754 head->subpixel,
5755 head->make,
5756 head->model,
5757 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005758
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005759 ver = wl_resource_get_version(resource);
5760 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5761 wl_output_send_done(resource);
5762 }
Roman Gilge97391c2019-03-29 13:01:06 +01005763
5764 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5765 zxdg_output_v1_send_logical_position(resource,
5766 output->x,
5767 output->y);
5768 zxdg_output_v1_send_done(resource);
5769 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005770 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005771}
5772
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005773/** Signal that a pending output is taken into use.
5774 *
5775 * Removes the output from the pending list and adds it to the compositor's
5776 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005777 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005778 * The output gets an internal ID assigned, and the wl_output global is
5779 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005780 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005781 * \param compositor The compositor instance.
5782 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005783 *
5784 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005785 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005786 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005787static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005788weston_compositor_add_output(struct weston_compositor *compositor,
5789 struct weston_output *output)
5790{
Armin Krezoviće5403842016-08-05 15:28:29 +02005791 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005792 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005793
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005794 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005795
5796 /* Verify we haven't reached the limit of 32 available output IDs */
5797 assert(ffs(~compositor->output_id_pool) > 0);
5798
5799 /* Invert the output id pool and look for the lowest numbered
5800 * switch (the least significant bit). Take that bit's position
5801 * as our ID, and mark it used in the compositor's output_id_pool.
5802 */
5803 output->id = ffs(~compositor->output_id_pool) - 1;
5804 compositor->output_id_pool |= 1u << output->id;
5805
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005806 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005807 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005808 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005809
Pekka Paalanendcac3512017-12-08 14:13:34 +02005810 wl_list_for_each(head, &output->head_list, output_link)
5811 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005812
Giulio Camuffob1147152015-05-06 21:41:57 +03005813 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005814
5815 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5816 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005817}
5818
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005819/** Transform device coordinates into global coordinates
5820 *
Marius Vlada2dace22019-06-12 16:05:44 +03005821 * \param output the weston_output object
5822 * \param[in] device_x X coordinate in device units.
5823 * \param[in] device_y Y coordinate in device units.
5824 * \param[out] x X coordinate in the global space.
5825 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005826 *
Marius Vlada2dace22019-06-12 16:05:44 +03005827 * Transforms coordinates from the device coordinate space (physical pixel
5828 * units) to the global coordinate space (logical pixel units). This takes
5829 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005830 *
Marius Vlad55d87362019-06-11 01:15:35 +03005831 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005832 * \internal
5833 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005834WL_EXPORT void
5835weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005836 double device_x, double device_y,
5837 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005838{
Derek Foreman0f679412014-10-02 13:41:17 -05005839 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005840 device_x,
5841 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005842 0.0,
5843 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005844
Derek Foreman67a18b92015-03-24 11:36:14 -05005845 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005846
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005847 *x = p.f[0] / p.f[3];
5848 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005849}
5850
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005851/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005852 *
5853 * \param output The weston_output object that is being removed.
5854 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005855 * The following happens:
5856 *
5857 * - The output assignments of all views in the current scenegraph are
5858 * recomputed.
5859 *
5860 * - Presentation feedback is discarded.
5861 *
5862 * - Compositor is notified that outputs were changed and
5863 * applies the necessary changes to re-layout outputs.
5864 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005865 * - The output is put back in the pending outputs list.
5866 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005867 * - Signal is emitted to notify all users of the weston_output
5868 * object that the output is being destroyed.
5869 *
5870 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005871 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005872 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005873 * - The output's internal ID is released.
5874 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005875 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005876 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005877 */
5878static void
5879weston_compositor_remove_output(struct weston_output *output)
5880{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005881 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005882 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005883 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005884
5885 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005886 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005887
Pekka Paalanenbccda712017-03-29 16:16:04 +03005888 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005889 if (view->output_mask & (1u << output->id))
5890 weston_view_assign_output(view);
5891 }
5892
5893 weston_presentation_feedback_discard_list(&output->feedback_list);
5894
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005895 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005896
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005897 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005898 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005899 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005900
Pekka Paalanenbccda712017-03-29 16:16:04 +03005901 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005902 wl_signal_emit(&output->destroy_signal, output);
5903
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005904 wl_list_for_each(head, &output->head_list, output_link)
5905 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005906
5907 compositor->output_id_pool &= ~(1u << output->id);
5908 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005909}
5910
5911/** Sets the output scale for a given output.
5912 *
5913 * \param output The weston_output object that the scale is set for.
5914 * \param scale Scale factor for the given output.
5915 *
5916 * It only supports setting scale for an output that
5917 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005918 *
Marius Vlad55d87362019-06-11 01:15:35 +03005919 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005920 */
5921WL_EXPORT void
5922weston_output_set_scale(struct weston_output *output,
5923 int32_t scale)
5924{
5925 /* We can only set scale on a disabled output */
5926 assert(!output->enabled);
5927
5928 /* We only want to set scale once */
5929 assert(!output->scale);
5930
5931 output->scale = scale;
5932}
5933
5934/** Sets the output transform for a given output.
5935 *
5936 * \param output The weston_output object that the transform is set for.
5937 * \param transform Transform value for the given output.
5938 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005939 * Refer to wl_output::transform section located at
5940 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5941 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005942 *
Marius Vlad55d87362019-06-11 01:15:35 +03005943 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005944 */
5945WL_EXPORT void
5946weston_output_set_transform(struct weston_output *output,
5947 uint32_t transform)
5948{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005949 struct weston_pointer_motion_event ev;
5950 struct wl_resource *resource;
5951 struct weston_seat *seat;
5952 pixman_region32_t old_region;
5953 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005954 struct weston_head *head;
5955 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005956
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005957 if (!output->enabled && output->transform == UINT32_MAX) {
5958 output->transform = transform;
5959 return;
5960 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005961
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005962 weston_output_transform_scale_init(output, transform, output->scale);
5963
5964 pixman_region32_init(&old_region);
5965 pixman_region32_copy(&old_region, &output->region);
5966
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005967 weston_output_init_geometry(output, output->x, output->y);
5968
5969 output->dirty = 1;
5970
5971 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005972 wl_list_for_each(head, &output->head_list, output_link) {
5973 wl_resource_for_each(resource, &head->resource_list) {
5974 wl_output_send_geometry(resource,
5975 output->x,
5976 output->y,
5977 head->mm_width,
5978 head->mm_height,
5979 head->subpixel,
5980 head->make,
5981 head->model,
5982 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005983
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005984 ver = wl_resource_get_version(resource);
5985 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5986 wl_output_send_done(resource);
5987 }
Roman Gilge97391c2019-03-29 13:01:06 +01005988 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5989 zxdg_output_v1_send_logical_position(resource,
5990 output->x,
5991 output->y);
5992 zxdg_output_v1_send_logical_size(resource,
5993 output->width,
5994 output->height);
5995 zxdg_output_v1_send_done(resource);
5996 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005997 }
5998
5999 /* we must ensure that pointers are inside output, otherwise they disappear */
6000 mid_x = output->x + output->width / 2;
6001 mid_y = output->y + output->height / 2;
6002
6003 ev.mask = WESTON_POINTER_MOTION_ABS;
6004 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6005 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6006
6007 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6008 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6009
6010 if (pointer && pixman_region32_contains_point(&old_region,
6011 wl_fixed_to_int(pointer->x),
6012 wl_fixed_to_int(pointer->y),
6013 NULL))
6014 weston_pointer_move(pointer, &ev);
6015 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006016}
6017
6018/** Initializes a weston_output object with enough data so
6019 ** an output can be configured.
6020 *
6021 * \param output The weston_output object to initialize
6022 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006023 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006024 *
6025 * Sets initial values for fields that are expected to be
6026 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006027 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006028 * The name is used in logs, and can be used by compositors as a configuration
6029 * identifier.
6030 *
Marius Vlad55d87362019-06-11 01:15:35 +03006031 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006032 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006033 */
6034WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006035weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006036 struct weston_compositor *compositor,
6037 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006038{
6039 output->compositor = compositor;
6040 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006041 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006042 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006043 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006044 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306045 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306046 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006047
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006048 wl_list_init(&output->head_list);
6049
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006050 /* Add some (in)sane defaults which can be used
6051 * for checking if an output was properly configured
6052 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006053 output->scale = 0;
6054 /* Can't use -1 on uint32_t and 0 is valid enum value */
6055 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006056
6057 pixman_region32_init(&output->previous_damage);
6058 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006059 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006060}
6061
6062/** Adds weston_output object to pending output list.
6063 *
6064 * \param output The weston_output object to add
6065 * \param compositor The compositor instance.
6066 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006067 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006068 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006069 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006070 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006071 */
6072WL_EXPORT void
6073weston_compositor_add_pending_output(struct weston_output *output,
6074 struct weston_compositor *compositor)
6075{
Pekka Paalanene952a012017-03-29 17:14:00 +03006076 assert(output->disable);
6077 assert(output->enable);
6078
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006079 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006080 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006081}
6082
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006083/** Create a string with the attached heads' names.
6084 *
6085 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006086 *
6087 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006088 */
6089static char *
6090weston_output_create_heads_string(struct weston_output *output)
6091{
6092 FILE *fp;
6093 char *str = NULL;
6094 size_t size = 0;
6095 struct weston_head *head;
6096 const char *sep = "";
6097
6098 fp = open_memstream(&str, &size);
6099 if (!fp)
6100 return NULL;
6101
6102 wl_list_for_each(head, &output->head_list, output_link) {
6103 fprintf(fp, "%s%s", sep, head->name);
6104 sep = ", ";
6105 }
6106 fclose(fp);
6107
6108 return str;
6109}
6110
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006111/** Constructs a weston_output object that can be used by the compositor.
6112 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006113 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006114 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006115 *
6116 * Output coordinates are calculated and each new output is by default
6117 * assigned to the right of previous one.
6118 *
6119 * Sets up the transformation, zoom, and geometry of the output using
6120 * the properties that need to be configured by the compositor.
6121 *
6122 * Establishes a repaint timer for the output with the relevant display
6123 * object's event loop. See output_repaint_timer_handler().
6124 *
6125 * The output is assigned an ID. Weston can support up to 32 distinct
6126 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6127 * is referred to and used to find the first available ID number, and
6128 * then this ID is marked as used in output_id_pool.
6129 *
6130 * The output is also assigned a Wayland global with the wl_output
6131 * external interface.
6132 *
6133 * Backend specific function is called to set up the output output.
6134 *
6135 * Output is added to the compositor's output list
6136 *
6137 * If the backend specific function fails, the weston_output object
6138 * is returned to a state it was before calling this function and
6139 * is added to the compositor's pending_output_list in case it needs
6140 * to be reconfigured or just so it can be destroyed at shutdown.
6141 *
6142 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006143 *
6144 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006145 */
6146WL_EXPORT int
6147weston_output_enable(struct weston_output *output)
6148{
Armin Krezović782f5df2016-09-30 14:11:11 +02006149 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006150 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006151 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006152 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006153 int x = 0, y = 0;
6154
Pekka Paalanencc201e42017-03-30 15:11:25 +03006155 if (output->enabled) {
6156 weston_log("Error: attempt to enable an enabled output '%s'\n",
6157 output->name);
6158 return -1;
6159 }
6160
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006161 if (wl_list_empty(&output->head_list)) {
6162 weston_log("Error: cannot enable output '%s' without heads.\n",
6163 output->name);
6164 return -1;
6165 }
6166
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006167 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6168 weston_log("Error: no video mode for output '%s'.\n",
6169 output->name);
6170 return -1;
6171 }
6172
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006173 wl_list_for_each(head, &output->head_list, output_link) {
6174 assert(head->make);
6175 assert(head->model);
6176 }
6177
Armin Krezović782f5df2016-09-30 14:11:11 +02006178 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006179 struct weston_output, link);
6180
Armin Krezović782f5df2016-09-30 14:11:11 +02006181 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006182 x = iterator->x + iterator->width;
6183
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006184 /* Make sure the scale is set up */
6185 assert(output->scale);
6186
6187 /* Make sure we have a transform set */
6188 assert(output->transform != UINT32_MAX);
6189
Armin Krezović782f5df2016-09-30 14:11:11 +02006190 output->x = x;
6191 output->y = y;
6192 output->dirty = 1;
6193 output->original_scale = output->scale;
6194
6195 weston_output_transform_scale_init(output, output->transform, output->scale);
6196 weston_output_init_zoom(output);
6197
6198 weston_output_init_geometry(output, x, y);
6199 weston_output_damage(output);
6200
6201 wl_signal_init(&output->frame_signal);
6202 wl_signal_init(&output->destroy_signal);
6203 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006204 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006205
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006206 /* Enable the output (set up the crtc or create a
6207 * window representing the output, set up the
6208 * renderer, etc)
6209 */
6210 if (output->enable(output) < 0) {
6211 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006212 return -1;
6213 }
6214
6215 weston_compositor_add_output(output->compositor, output);
6216
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006217 head_names = weston_output_create_heads_string(output);
6218 weston_log("Output '%s' enabled with head(s) %s\n",
6219 output->name, head_names);
6220 free(head_names);
6221
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006222 return 0;
6223}
6224
6225/** Converts a weston_output object to a pending output state, so it
6226 ** can be configured again or destroyed.
6227 *
6228 * \param output The weston_output object that needs to be disabled.
6229 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006230 * Calls a backend specific function to disable an output, in case
6231 * such function exists.
6232 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006233 * The backend specific disable function may choose to postpone the disabling
6234 * by returning a negative value, in which case this function returns early.
6235 * In that case the backend will guarantee the output will be disabled soon
6236 * by the backend calling this function again. One must not attempt to re-enable
6237 * the output until that happens.
6238 *
6239 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006240 * from weston's output_list (see weston_compositor_remove_output())
6241 * and is returned to a state it was before weston_output_enable()
6242 * was ran (see weston_output_enable_undo()).
6243 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006244 * See weston_output_init() for more information on the
6245 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006246 *
6247 * If the output has never been enabled yet, this function can still be
6248 * called to ensure that the output is actually turned off rather than left
6249 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006250 *
6251 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006252 */
6253WL_EXPORT void
6254weston_output_disable(struct weston_output *output)
6255{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006256 /* Should we rename this? */
6257 output->destroying = 1;
6258
Pekka Paalanenc65df642017-03-29 15:45:46 +03006259 /* Disable is called unconditionally also for not-enabled outputs,
6260 * because at compositor start-up, if there is an output that is
6261 * already on but the compositor wants to turn it off, we have to
6262 * forward the turn-off to the backend so it knows to do it.
6263 * The backend cannot initially turn off everything, because it
6264 * would cause unnecessary mode-sets for all outputs the compositor
6265 * wants to be on.
6266 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006267 if (output->disable(output) < 0)
6268 return;
6269
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006270 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006271 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006272
6273 output->destroying = 0;
6274}
6275
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006276/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006277 *
6278 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006279 *
6280 * If there are new or changed heads, calls the heads_changed hook and
6281 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006282 *
6283 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006284 */
6285WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006286weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006287{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006288 if (compositor->heads_changed_source) {
6289 wl_event_source_remove(compositor->heads_changed_source);
6290 weston_compositor_call_heads_changed(compositor);
6291 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006292}
6293
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006294/** Add destroy callback for an output
6295 *
6296 * \param output The output to watch.
6297 * \param listener The listener to add. The \c notify member must be set.
6298 *
6299 * The listener callback will be called when user destroys an output. This
6300 * may be delayed by a backend in some cases. The main purpose of the
6301 * listener is to allow hooking up custom data to the output. The custom data
6302 * can be fetched via weston_output_get_destroy_listener() followed by
6303 * container_of().
6304 *
6305 * The \c data argument to the notify callback is the weston_output being
6306 * destroyed.
6307 *
6308 * @note This is for the final destruction of an output, not when it gets
6309 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006310 *
6311 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006312 */
6313WL_EXPORT void
6314weston_output_add_destroy_listener(struct weston_output *output,
6315 struct wl_listener *listener)
6316{
6317 wl_signal_add(&output->user_destroy_signal, listener);
6318}
6319
6320/** Look up destroy listener for an output
6321 *
6322 * \param output The output to query.
6323 * \param notify The notify function used used for the added destroy listener.
6324 * \return The listener, or NULL if not found.
6325 *
6326 * This looks up the previously added destroy listener struct based on the
6327 * notify function it has. The listener can be used to access user data
6328 * through \c container_of().
6329 *
6330 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006331 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006332 */
6333WL_EXPORT struct wl_listener *
6334weston_output_get_destroy_listener(struct weston_output *output,
6335 wl_notify_func_t notify)
6336{
6337 return wl_signal_get(&output->user_destroy_signal, notify);
6338}
6339
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006340/** Uninitialize an output
6341 *
6342 * Removes the output from the list of enabled outputs if necessary, but
6343 * does not call the backend's output disable function. The output will no
6344 * longer be in the list of pending outputs either.
6345 *
6346 * All fields of weston_output become uninitialized, i.e. should not be used
6347 * anymore. The caller can free the memory after this.
6348 *
Marius Vlad55d87362019-06-11 01:15:35 +03006349 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006350 * \internal
6351 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006352WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006353weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006354{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006355 struct weston_head *head, *tmp;
6356
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006357 output->destroying = 1;
6358
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006359 wl_signal_emit(&output->user_destroy_signal, output);
6360
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006361 if (output->idle_repaint_source)
6362 wl_event_source_remove(output->idle_repaint_source);
6363
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006364 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006365 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006366
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006367 pixman_region32_fini(&output->region);
6368 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006369 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006370
6371 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6372 weston_head_detach(head);
6373
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006374 free(output->name);
6375}
6376
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006377/** Find an output by its given name
6378 *
6379 * \param compositor The compositor to search in.
6380 * \param name The output name to search for.
6381 * \return An existing output with the given name, or NULL if not found.
6382 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006383 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006384 */
6385WL_EXPORT struct weston_output *
6386weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6387 const char *name)
6388{
6389 struct weston_output *output;
6390
6391 wl_list_for_each(output, &compositor->output_list, link)
6392 if (strcmp(output->name, name) == 0)
6393 return output;
6394
6395 wl_list_for_each(output, &compositor->pending_output_list, link)
6396 if (strcmp(output->name, name) == 0)
6397 return output;
6398
6399 return NULL;
6400}
6401
6402/** Create a named output
6403 *
6404 * \param compositor The compositor.
6405 * \param name The name for the output.
6406 * \return A new \c weston_output, or NULL on failure.
6407 *
6408 * This creates a new weston_output that starts with no heads attached.
6409 *
6410 * An output must be configured and it must have at least one head before
6411 * it can be enabled.
6412 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006413 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006414 */
6415WL_EXPORT struct weston_output *
6416weston_compositor_create_output(struct weston_compositor *compositor,
6417 const char *name)
6418{
6419 assert(compositor->backend->create_output);
6420
6421 if (weston_compositor_find_output_by_name(compositor, name)) {
6422 weston_log("Warning: attempted to create an output with a "
6423 "duplicate name '%s'.\n", name);
6424 return NULL;
6425 }
6426
6427 return compositor->backend->create_output(compositor, name);
6428}
6429
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006430/** Create an output for an unused head
6431 *
6432 * \param compositor The compositor.
6433 * \param head The head to attach to the output.
6434 * \return A new \c weston_output, or NULL on failure.
6435 *
6436 * This creates a new weston_output that starts with the given head attached.
6437 * The output inherits the name of the head. The head must not be already
6438 * attached to another output.
6439 *
6440 * An output must be configured before it can be enabled.
6441 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006442 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006443 */
6444WL_EXPORT struct weston_output *
6445weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6446 struct weston_head *head)
6447{
6448 struct weston_output *output;
6449
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006450 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006451 if (!output)
6452 return NULL;
6453
6454 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006455 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006456 return NULL;
6457 }
6458
6459 return output;
6460}
6461
6462/** Destroy an output
6463 *
6464 * \param output The output to destroy.
6465 *
6466 * The heads attached to the given output are detached and become unused again.
6467 *
6468 * It is not necessary to explicitly destroy all outputs at compositor exit.
6469 * weston_compositor_destroy() will automatically destroy any remaining
6470 * outputs.
6471 *
Marius Vlad55d87362019-06-11 01:15:35 +03006472 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006473 */
6474WL_EXPORT void
6475weston_output_destroy(struct weston_output *output)
6476{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006477 output->destroy(output);
6478}
6479
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006480/** When you need a head...
6481 *
6482 * This function is a hack, used until all code has been converted to become
6483 * multi-head aware.
6484 *
6485 * \param output The weston_output whose head to get.
6486 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006487 *
6488 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006489 */
6490WL_EXPORT struct weston_head *
6491weston_output_get_first_head(struct weston_output *output)
6492{
6493 if (wl_list_empty(&output->head_list))
6494 return NULL;
6495
6496 return container_of(output->head_list.next,
6497 struct weston_head, output_link);
6498}
6499
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306500/** Allow/Disallow content-protection support for an output
6501 *
6502 * This function sets the allow_protection member for an output. Setting of
6503 * this field will allow the compositor to attempt content-protection for this
6504 * output, for a backend that supports the content-protection protocol.
6505 *
6506 * \param output The weston_output for whom the content-protection is to be
6507 * allowed.
6508 * \param allow_protection The bool value which is to be set.
6509 */
6510WL_EXPORT void
6511weston_output_allow_protection(struct weston_output *output,
6512 bool allow_protection)
6513{
6514 output->allow_protection = allow_protection;
6515}
6516
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006517static void
Roman Gilge97391c2019-03-29 13:01:06 +01006518xdg_output_unlist(struct wl_resource *resource)
6519{
6520 wl_list_remove(wl_resource_get_link(resource));
6521}
6522
6523static void
6524xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6525{
6526 wl_resource_destroy(resource);
6527}
6528
6529static const struct zxdg_output_v1_interface xdg_output_interface = {
6530 xdg_output_destroy
6531};
6532
6533static void
6534xdg_output_manager_destroy(struct wl_client *client,
6535 struct wl_resource *resource)
6536{
6537 wl_resource_destroy(resource);
6538}
6539
6540static void
6541xdg_output_manager_get_xdg_output(struct wl_client *client,
6542 struct wl_resource *manager,
6543 uint32_t id,
6544 struct wl_resource *output_resource)
6545{
6546 int version = wl_resource_get_version(manager);
6547 struct weston_head *head = wl_resource_get_user_data(output_resource);
6548 struct weston_output *output = head->output;
6549 struct wl_resource *resource;
6550
6551 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6552 version, id);
6553 if (resource == NULL) {
6554 wl_client_post_no_memory(client);
6555 return;
6556 }
6557
6558 wl_list_insert(&head->xdg_output_resource_list,
6559 wl_resource_get_link(resource));
6560
6561 wl_resource_set_implementation(resource, &xdg_output_interface,
6562 NULL, xdg_output_unlist);
6563
6564 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6565 zxdg_output_v1_send_logical_size(resource,
6566 output->width,
6567 output->height);
6568 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6569 zxdg_output_v1_send_name(resource, head->name);
6570
6571 zxdg_output_v1_send_done(resource);
6572}
6573
6574static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6575 xdg_output_manager_destroy,
6576 xdg_output_manager_get_xdg_output
6577};
6578
6579static void
6580bind_xdg_output_manager(struct wl_client *client,
6581 void *data, uint32_t version, uint32_t id)
6582{
6583 struct wl_resource *resource;
6584
6585 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6586 version, id);
6587 if (resource == NULL) {
6588 wl_client_post_no_memory(client);
6589 return;
6590 }
6591
6592 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6593 NULL, NULL);
6594}
6595
6596static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006597destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006598{
Jonny Lamb74130762013-11-26 18:19:46 +01006599 struct weston_surface *surface =
6600 wl_resource_get_user_data(resource);
6601
Pekka Paalanen4826f872016-04-22 14:14:38 +03006602 if (!surface)
6603 return;
6604
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006605 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006606 surface->pending.buffer_viewport.buffer.src_width =
6607 wl_fixed_from_int(-1);
6608 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006609 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006610}
6611
6612static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006613viewport_destroy(struct wl_client *client,
6614 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006615{
6616 wl_resource_destroy(resource);
6617}
6618
6619static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006620viewport_set_source(struct wl_client *client,
6621 struct wl_resource *resource,
6622 wl_fixed_t src_x,
6623 wl_fixed_t src_y,
6624 wl_fixed_t src_width,
6625 wl_fixed_t src_height)
6626{
6627 struct weston_surface *surface =
6628 wl_resource_get_user_data(resource);
6629
Pekka Paalanen4826f872016-04-22 14:14:38 +03006630 if (!surface) {
6631 wl_resource_post_error(resource,
6632 WP_VIEWPORT_ERROR_NO_SURFACE,
6633 "wl_surface for this viewport is no longer exists");
6634 return;
6635 }
6636
6637 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006638 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006639
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006640 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006641 src_height == wl_fixed_from_int(-1) &&
6642 src_x == wl_fixed_from_int(-1) &&
6643 src_y == wl_fixed_from_int(-1)) {
6644 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006645 surface->pending.buffer_viewport.buffer.src_width =
6646 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006647 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006648 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006649 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006650
Pekka Paalanen201769a2016-04-26 14:42:11 +03006651 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006652 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006653 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006654 "wl_surface@%d viewport source "
6655 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6656 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006657 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006658 wl_fixed_to_double(src_height),
6659 wl_fixed_to_double(src_x),
6660 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006661 return;
6662 }
6663
6664 surface->pending.buffer_viewport.buffer.src_x = src_x;
6665 surface->pending.buffer_viewport.buffer.src_y = src_y;
6666 surface->pending.buffer_viewport.buffer.src_width = src_width;
6667 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006668 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006669}
6670
6671static void
6672viewport_set_destination(struct wl_client *client,
6673 struct wl_resource *resource,
6674 int32_t dst_width,
6675 int32_t dst_height)
6676{
6677 struct weston_surface *surface =
6678 wl_resource_get_user_data(resource);
6679
Pekka Paalanen4826f872016-04-22 14:14:38 +03006680 if (!surface) {
6681 wl_resource_post_error(resource,
6682 WP_VIEWPORT_ERROR_NO_SURFACE,
6683 "wl_surface for this viewport no longer exists");
6684 return;
6685 }
6686
6687 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006688
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006689 if (dst_width == -1 && dst_height == -1) {
6690 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006691 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006692 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006693 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006694 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006695
6696 if (dst_width <= 0 || dst_height <= 0) {
6697 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006698 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006699 "destination size must be positive (%dx%d)",
6700 dst_width, dst_height);
6701 return;
6702 }
6703
6704 surface->pending.buffer_viewport.surface.width = dst_width;
6705 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006706 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006707}
6708
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006709static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006710 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006711 viewport_set_source,
6712 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006713};
6714
6715static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006716viewporter_destroy(struct wl_client *client,
6717 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006718{
6719 wl_resource_destroy(resource);
6720}
6721
6722static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006723viewporter_get_viewport(struct wl_client *client,
6724 struct wl_resource *viewporter,
6725 uint32_t id,
6726 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006727{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006728 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006729 struct weston_surface *surface =
6730 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006731 struct wl_resource *resource;
6732
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006733 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006734 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006735 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006736 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006737 return;
6738 }
6739
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006740 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006741 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006742 if (resource == NULL) {
6743 wl_client_post_no_memory(client);
6744 return;
6745 }
6746
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006747 wl_resource_set_implementation(resource, &viewport_interface,
6748 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006749
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006750 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006751}
6752
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006753static const struct wp_viewporter_interface viewporter_interface = {
6754 viewporter_destroy,
6755 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006756};
6757
6758static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006759bind_viewporter(struct wl_client *client,
6760 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006761{
6762 struct wl_resource *resource;
6763
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006764 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006765 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006766 if (resource == NULL) {
6767 wl_client_post_no_memory(client);
6768 return;
6769 }
6770
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006771 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006772 NULL, NULL);
6773}
6774
6775static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006776destroy_presentation_feedback(struct wl_resource *feedback_resource)
6777{
6778 struct weston_presentation_feedback *feedback;
6779
6780 feedback = wl_resource_get_user_data(feedback_resource);
6781
6782 wl_list_remove(&feedback->link);
6783 free(feedback);
6784}
6785
6786static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006787presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6788{
6789 wl_resource_destroy(resource);
6790}
6791
6792static void
6793presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006794 struct wl_resource *presentation_resource,
6795 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006796 uint32_t callback)
6797{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006798 struct weston_surface *surface;
6799 struct weston_presentation_feedback *feedback;
6800
6801 surface = wl_resource_get_user_data(surface_resource);
6802
Bryce Harringtonde16d892014-11-20 22:21:57 -08006803 feedback = zalloc(sizeof *feedback);
6804 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006805 goto err_calloc;
6806
6807 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006808 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006809 1, callback);
6810 if (!feedback->resource)
6811 goto err_create;
6812
6813 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6814 destroy_presentation_feedback);
6815
6816 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6817
6818 return;
6819
6820err_create:
6821 free(feedback);
6822
6823err_calloc:
6824 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006825}
6826
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006827static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006828 presentation_destroy,
6829 presentation_feedback
6830};
6831
6832static void
6833bind_presentation(struct wl_client *client,
6834 void *data, uint32_t version, uint32_t id)
6835{
6836 struct weston_compositor *compositor = data;
6837 struct wl_resource *resource;
6838
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006839 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006840 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006841 if (resource == NULL) {
6842 wl_client_post_no_memory(client);
6843 return;
6844 }
6845
6846 wl_resource_set_implementation(resource, &presentation_implementation,
6847 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006848 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006849}
6850
6851static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006852compositor_bind(struct wl_client *client,
6853 void *data, uint32_t version, uint32_t id)
6854{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006855 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006856 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006857
Jason Ekstranda85118c2013-06-27 20:17:02 -05006858 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006859 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006860 if (resource == NULL) {
6861 wl_client_post_no_memory(client);
6862 return;
6863 }
6864
6865 wl_resource_set_implementation(resource, &compositor_interface,
6866 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006867}
6868
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006869WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006870weston_environment_get_fd(const char *env)
6871{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006872 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006873 int fd, flags;
6874
6875 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006876 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006877 return -1;
6878
6879 flags = fcntl(fd, F_GETFD);
6880 if (flags == -1)
6881 return -1;
6882
6883 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6884 unsetenv(env);
6885
6886 return fd;
6887}
6888
Pekka Paalanenb5026542014-11-12 15:09:24 +02006889static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006890timeline_key_binding_handler(struct weston_keyboard *keyboard,
6891 const struct timespec *time, uint32_t key,
6892 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006893{
6894 struct weston_compositor *compositor = data;
6895
6896 if (weston_timeline_enabled_)
6897 weston_timeline_close();
6898 else
6899 weston_timeline_open(compositor);
6900}
6901
Daniel Stonece62cb32018-07-20 09:46:24 +01006902static const char *
6903output_repaint_status_text(struct weston_output *output)
6904{
6905 switch (output->repaint_status) {
6906 case REPAINT_NOT_SCHEDULED:
6907 return "no repaint";
6908 case REPAINT_BEGIN_FROM_IDLE:
6909 return "start_repaint_loop scheduled";
6910 case REPAINT_SCHEDULED:
6911 return "repaint scheduled";
6912 case REPAINT_AWAITING_COMPLETION:
6913 return "awaiting completion";
6914 }
6915
6916 assert(!"output_repaint_status_text missing enum");
6917 return NULL;
6918}
6919
6920static void
6921debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6922{
6923 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6924 struct wl_shm_buffer *shm;
6925 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006926 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006927
6928 if (!buffer) {
6929 fprintf(fp, "\t\t[buffer not available]\n");
6930 return;
6931 }
6932
6933 shm = wl_shm_buffer_get(buffer->resource);
6934 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006935 uint32_t _format = wl_shm_buffer_get_format(shm);
6936 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006937 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006938 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6939 (unsigned long) _format,
6940 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006941 return;
6942 }
6943
6944 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6945 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006946 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006947 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006948 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6949 (unsigned long) dmabuf->attributes.format,
6950 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006951 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6952 (unsigned long long) dmabuf->attributes.modifier[0]);
6953 return;
6954 }
6955
Marius Vlad253ba9a2019-03-17 18:22:21 +02006956 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006957}
6958
6959static void
6960debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6961{
6962 struct weston_compositor *ec = view->surface->compositor;
6963 struct weston_output *output;
6964 char desc[512];
6965 pixman_box32_t *box;
6966 uint32_t surface_id = 0;
6967 pid_t pid = 0;
6968
6969 if (view->surface->resource) {
6970 struct wl_resource *resource = view->surface->resource;
6971 wl_client_get_credentials(wl_resource_get_client(resource),
6972 &pid, NULL, NULL);
6973 surface_id = wl_resource_get_id(view->surface->resource);
6974 }
6975
6976 if (!view->surface->get_label ||
6977 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6978 strcpy(desc, "[no description available]");
6979 }
6980 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6981 view_idx, view->surface->role_name, pid, surface_id,
6982 desc, view);
6983
6984 box = pixman_region32_extents(&view->transform.boundingbox);
6985 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6986 box->x1, box->y1, box->x2, box->y2);
6987 box = pixman_region32_extents(&view->transform.opaque);
6988
6989 if (pixman_region32_equal(&view->transform.opaque,
6990 &view->transform.boundingbox)) {
6991 fprintf(fp, "\t\t[fully opaque]\n");
6992 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6993 fprintf(fp, "\t\t[not opaque]\n");
6994 } else {
6995 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6996 box->x1, box->y1, box->x2, box->y2);
6997 }
6998
6999 if (view->alpha < 1.0)
7000 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7001
7002 if (view->output_mask != 0) {
7003 bool first_output = true;
7004 fprintf(fp, "\t\toutputs: ");
7005 wl_list_for_each(output, &ec->output_list, link) {
7006 if (!(view->output_mask & (1 << output->id)))
7007 continue;
7008 fprintf(fp, "%s%d (%s)%s",
7009 (first_output) ? "" : ", ",
7010 output->id, output->name,
7011 (view->output == output) ? " (primary)" : "");
7012 first_output = false;
7013 }
7014 } else {
7015 fprintf(fp, "\t\t[no outputs]");
7016 }
7017
7018 fprintf(fp, "\n");
7019
7020 debug_scene_view_print_buffer(fp, view);
7021}
7022
Marius Vlad433f4e72019-02-17 22:14:23 +02007023static void
7024debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007025 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007026{
7027 struct weston_subsurface *sub;
7028 struct weston_view *ev;
7029
7030 /*
7031 * print the view first, then we recursively go on printing
7032 * sub-surfaces. We bail out once no more sub-surfaces are available.
7033 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007034 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007035
7036 /* no more sub-surfaces */
7037 if (wl_list_empty(&view->surface->subsurface_list))
7038 return;
7039
7040 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7041 wl_list_for_each(ev, &sub->surface->views, surface_link) {
7042 /* do not print again the parent view */
7043 if (view == ev)
7044 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007045
7046 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007047 debug_scene_view_print_tree(ev, fp, view_idx);
7048 }
7049 }
7050}
7051
Daniel Stonece62cb32018-07-20 09:46:24 +01007052/**
7053 * Output information on how libweston is currently composing the scene
7054 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007055 *
7056 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007057 */
7058WL_EXPORT char *
7059weston_compositor_print_scene_graph(struct weston_compositor *ec)
7060{
7061 struct weston_output *output;
7062 struct weston_layer *layer;
7063 struct timespec now;
7064 int layer_idx = 0;
7065 FILE *fp;
7066 char *ret;
7067 size_t len;
7068 int err;
7069
7070 fp = open_memstream(&ret, &len);
7071 assert(fp);
7072
7073 weston_compositor_read_presentation_clock(ec, &now);
7074 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7075 now.tv_sec, now.tv_nsec);
7076
7077 wl_list_for_each(output, &ec->output_list, link) {
7078 struct weston_head *head;
7079 int head_idx = 0;
7080
7081 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7082 assert(output->enabled);
7083
7084 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7085 output->x, output->y,
7086 output->x + output->width,
7087 output->y + output->height);
7088 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7089 output->current_mode->width,
7090 output->current_mode->height,
7091 output->current_mode->refresh / 1000.0);
7092 fprintf(fp, "\tscale: %d\n", output->scale);
7093
7094 fprintf(fp, "\trepaint status: %s\n",
7095 output_repaint_status_text(output));
7096 if (output->repaint_status == REPAINT_SCHEDULED)
7097 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7098 output->next_repaint.tv_sec,
7099 output->next_repaint.tv_nsec);
7100
7101 wl_list_for_each(head, &output->head_list, output_link) {
7102 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7103 head_idx++, head->name,
7104 (head->connected) ? "" : "not ");
7105 }
7106 }
7107
7108 fprintf(fp, "\n");
7109
7110 wl_list_for_each(layer, &ec->layer_list, link) {
7111 struct weston_view *view;
7112 int view_idx = 0;
7113
7114 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7115 (unsigned long) layer->position);
7116
7117 if (!weston_layer_mask_is_infinite(layer)) {
7118 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7119 layer->mask.x1, layer->mask.y1,
7120 layer->mask.x2, layer->mask.y2);
7121 }
7122
Marius Vlada6acfa82019-03-17 18:10:09 +02007123 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7124 debug_scene_view_print_tree(view, fp, &view_idx);
7125 view_idx++;
7126 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007127
7128 if (wl_list_empty(&layer->view_list.link))
7129 fprintf(fp, "\t[no views]\n");
7130
7131 fprintf(fp, "\n");
7132 }
7133
7134 err = fclose(fp);
7135 assert(err == 0);
7136
7137 return ret;
7138}
7139
7140/**
7141 * Called when the 'scene-graph' debug scope is bound by a client. This
7142 * one-shot weston-debug scope prints the current scene graph when bound,
7143 * and then terminates the stream.
7144 */
7145static void
Marius Vladdad882a2019-07-17 15:43:53 +03007146debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007147{
7148 struct weston_compositor *ec = data;
7149 char *str = weston_compositor_print_scene_graph(ec);
7150
Marius Vladdad882a2019-07-17 15:43:53 +03007151 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007152 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007153 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007154}
7155
Giulio Camuffo459137b2014-10-11 23:56:24 +03007156/** Create the compositor.
7157 *
7158 * This functions creates and initializes a compositor instance.
7159 *
7160 * \param display The Wayland display to be used.
7161 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007162 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007163 * using the \ref weston_compositor_get_user_data function.
7164 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007165 *
7166 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007167 */
7168WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007169weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007170 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007171 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007172{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007173 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007174 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007175
Giulio Camuffo459137b2014-10-11 23:56:24 +03007176 ec = zalloc(sizeof *ec);
7177 if (!ec)
7178 return NULL;
7179
7180 ec->wl_display = display;
7181 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007182 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007183 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007184 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007185 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007186 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007187 wl_signal_init(&ec->idle_signal);
7188 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007189 wl_signal_init(&ec->show_input_panel_signal);
7190 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007191 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007192 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007193 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007194 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007195 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007196 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007197 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007198 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007199 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007200 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007201
Casey Dahlin58ba1372012-04-19 22:50:08 -04007202 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007203 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007204
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007205 ec->activate_serial = 1;
7206
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007207 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7208
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307209 ec->content_protection = NULL;
7210
Derek Foreman152254b2015-11-26 14:17:48 -06007211 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007212 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007213 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007214
Giulio Camuffo954f1832014-10-11 18:27:30 +03007215 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007216 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007217 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007218
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007219 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007220 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007221 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007222
Roman Gilge97391c2019-03-29 13:01:06 +01007223 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7224 ec, bind_xdg_output_manager))
7225 goto fail;
7226
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007227 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007228 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007229 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007230
Marius Vlad3d7d9782019-04-17 12:35:38 +03007231 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007232 goto fail;
7233
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007234 if (weston_input_init(ec) != 0)
7235 goto fail;
7236
Jason Ekstranda7af7042013-10-12 22:38:11 -05007237 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007238 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007239 wl_list_init(&ec->layer_list);
7240 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007241 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007242 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007243 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007244 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007245 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007246 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007247 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007248 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007249 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007250
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007251 wl_list_init(&ec->plugin_api_list);
7252
Xiong Zhang97116532013-10-23 13:58:31 +08007253 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007254 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007255
Giulio Camuffo459137b2014-10-11 23:56:24 +03007256 wl_data_device_manager_init(ec->wl_display);
7257
7258 wl_display_init_shm(ec->wl_display);
7259
7260 loop = wl_display_get_event_loop(ec->wl_display);
7261 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007262 ec->repaint_timer =
7263 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7264 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007265
Quentin Glidic82681572016-12-17 13:40:51 +01007266 weston_layer_init(&ec->fade_layer, ec);
7267 weston_layer_init(&ec->cursor_layer, ec);
7268
7269 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7270 weston_layer_set_position(&ec->cursor_layer,
7271 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007272
7273 weston_compositor_add_debug_binding(ec, KEY_T,
7274 timeline_key_binding_handler, ec);
7275
Daniel Stonece62cb32018-07-20 09:46:24 +01007276 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03007277 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
7278 "Scene graph details\n",
7279 debug_scene_graph_cb,
7280 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007281
Giulio Camuffo459137b2014-10-11 23:56:24 +03007282 return ec;
7283
7284fail:
7285 free(ec);
7286 return NULL;
7287}
7288
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007289/** weston_compositor_shutdown
7290 * \ingroup compositor
7291 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007292WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007293weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007294{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007295 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007296
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007297 wl_event_source_remove(ec->idle_source);
7298
Matt Roper361d2ad2011-08-29 13:52:23 -07007299 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007300 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007301 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007302
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007303 /* Destroy all pending outputs associated with this compositor */
7304 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7305 output->destroy(output);
7306
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007307 if (ec->renderer)
7308 ec->renderer->destroy(ec);
7309
Daniel Stone325fc2d2012-05-30 16:31:58 +01007310 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007311 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007312 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007313 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007314 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007315 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007316
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007317 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007318}
7319
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007320/** weston_compositor_exit_with_code
7321 * \ingroup compositor
7322 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007323WL_EXPORT void
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007324weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007325 int exit_code)
7326{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007327 if (compositor->exit_code == EXIT_SUCCESS)
7328 compositor->exit_code = exit_code;
7329
Giulio Camuffo459137b2014-10-11 23:56:24 +03007330 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007331}
7332
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007333/** weston_compositor_set_default_pointer_grab
7334 * \ingroup compositor
7335 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007336WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007337weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7338 const struct weston_pointer_grab_interface *interface)
7339{
7340 struct weston_seat *seat;
7341
7342 ec->default_pointer_grab = interface;
7343 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007344 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7345
7346 if (pointer)
7347 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007348 }
7349}
7350
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007351/** weston_compositor_set_presentation_clock
7352 * \ingroup compositor
7353 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007354WL_EXPORT int
7355weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7356 clockid_t clk_id)
7357{
7358 struct timespec ts;
7359
7360 if (clock_gettime(clk_id, &ts) < 0)
7361 return -1;
7362
7363 compositor->presentation_clock = clk_id;
7364
7365 return 0;
7366}
7367
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007368/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007369 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007370 *
7371 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007372 */
7373WL_EXPORT int
7374weston_compositor_set_presentation_clock_software(
7375 struct weston_compositor *compositor)
7376{
7377 /* In order of preference */
7378 static const clockid_t clocks[] = {
7379 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7380 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7381 CLOCK_MONOTONIC, /* no jumps, may crawl */
7382 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7383 CLOCK_REALTIME /* may jump and crawl */
7384 };
7385 unsigned i;
7386
7387 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7388 if (weston_compositor_set_presentation_clock(compositor,
7389 clocks[i]) == 0)
7390 return 0;
7391
7392 weston_log("Error: no suitable presentation clock available.\n");
7393
7394 return -1;
7395}
7396
Pekka Paalanen662f3842015-03-18 12:17:26 +02007397/** Read the current time from the Presentation clock
7398 *
7399 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007400 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007401 *
7402 * \note Reading the current time in user space is always imprecise to some
7403 * degree.
7404 *
7405 * This function is never meant to fail. If reading the clock does fail,
7406 * an error message is logged and a zero time is returned. Callers are not
7407 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007408 *
7409 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007410 */
7411WL_EXPORT void
7412weston_compositor_read_presentation_clock(
7413 const struct weston_compositor *compositor,
7414 struct timespec *ts)
7415{
7416 static bool warned;
7417 int ret;
7418
7419 ret = clock_gettime(compositor->presentation_clock, ts);
7420 if (ret < 0) {
7421 ts->tv_sec = 0;
7422 ts->tv_nsec = 0;
7423
7424 if (!warned)
7425 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007426 "the presentation clock %#x: '%s' (%d)\n",
7427 compositor->presentation_clock,
7428 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007429 warned = true;
7430 }
7431}
7432
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007433/** Import dmabuf buffer into current renderer
7434 *
7435 * \param compositor
7436 * \param buffer the dmabuf buffer to import
7437 * \return true on usable buffers, false otherwise
7438 *
7439 * This function tests that the linux_dmabuf_buffer is usable
7440 * for the current renderer. Returns false on unusable buffers. Usually
7441 * usability is tested by importing the dmabufs for composition.
7442 *
7443 * This hook is also used for detecting if the renderer supports
7444 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7445 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007446 *
7447 * \ingroup compositor
7448 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007449WL_EXPORT bool
7450weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7451 struct linux_dmabuf_buffer *buffer)
7452{
7453 struct weston_renderer *renderer;
7454
7455 renderer = compositor->renderer;
7456
7457 if (renderer->import_dmabuf == NULL)
7458 return false;
7459
7460 return renderer->import_dmabuf(compositor, buffer);
7461}
7462
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007463WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007464weston_version(int *major, int *minor, int *micro)
7465{
7466 *major = WESTON_VERSION_MAJOR;
7467 *minor = WESTON_VERSION_MINOR;
7468 *micro = WESTON_VERSION_MICRO;
7469}
7470
Daniel Stonee03c1112016-11-24 20:45:45 +00007471/**
7472 * Attempts to find a module path from the module map specified in the
7473 * environment. If found, writes the full path into the path variable.
7474 *
7475 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7476 * each entry is of the form "name=path" and entries are separated by
7477 * semicolons. Whitespace is significant.
7478 *
7479 * \param name The name to search for.
7480 * \param path Where the path is written to if found.
7481 * \param path_len Allocated bytes at \c path .
7482 * \returns The length of the string written to path on success, or 0 if the
7483 * module was not specified in the environment map or path_len was too small.
7484 */
7485WL_EXPORT size_t
7486weston_module_path_from_env(const char *name, char *path, size_t path_len)
7487{
7488 const char *mapping = getenv("WESTON_MODULE_MAP");
7489 const char *end;
7490 const int name_len = strlen(name);
7491
7492 if (!mapping)
7493 return 0;
7494
7495 end = mapping + strlen(mapping);
7496 while (mapping < end && *mapping) {
7497 const char *filename, *next;
7498
7499 /* early out: impossibly short string */
7500 if (end - mapping < name_len + 1)
7501 return 0;
7502
7503 filename = &mapping[name_len + 1];
7504 next = strchrnul(mapping, ';');
7505
7506 if (strncmp(mapping, name, name_len) == 0 &&
7507 mapping[name_len] == '=') {
7508 size_t file_len = next - filename; /* no trailing NUL */
7509 if (file_len >= path_len)
7510 return 0;
7511 strncpy(path, filename, file_len);
7512 path[file_len] = '\0';
7513 return file_len;
7514 }
7515
7516 mapping = next + 1;
7517 }
7518
7519 return 0;
7520}
7521
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007522WL_EXPORT void *
7523weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007524{
7525 char path[PATH_MAX];
7526 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007527 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007528
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007529 if (name == NULL)
7530 return NULL;
7531
Derek Foreman3f86e502015-06-08 11:46:54 -05007532 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007533 len = weston_module_path_from_env(name, path, sizeof path);
7534 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007535 len = snprintf(path, sizeof path, "%s/%s",
7536 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007537 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007538 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007539 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007540
Daniel Stonebeb97e52016-11-28 12:13:54 +00007541 /* snprintf returns the length of the string it would've written,
7542 * _excluding_ the NUL byte. So even being equal to the size of
7543 * our buffer is an error here. */
7544 if (len >= sizeof path)
7545 return NULL;
7546
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007547 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7548 if (module) {
7549 weston_log("Module '%s' already loaded\n", path);
7550 dlclose(module);
7551 return NULL;
7552 }
7553
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007554 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007555 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007556 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007557 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007558 return NULL;
7559 }
7560
7561 init = dlsym(module, entrypoint);
7562 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007563 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007564 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007565 return NULL;
7566 }
7567
7568 return init;
7569}
7570
Giulio Camuffo459137b2014-10-11 23:56:24 +03007571
7572/** Destroys the compositor.
7573 *
7574 * This function cleans up the compositor state and destroys it.
7575 *
7576 * \param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007577 *
7578 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007579 */
7580WL_EXPORT void
7581weston_compositor_destroy(struct weston_compositor *compositor)
7582{
7583 /* prevent further rendering while shutting down */
7584 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7585
7586 wl_signal_emit(&compositor->destroy_signal, compositor);
7587
7588 weston_compositor_xkb_destroy(compositor);
7589
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007590 if (compositor->backend)
7591 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007592
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007593 /* The backend is responsible for destroying the heads. */
7594 assert(wl_list_empty(&compositor->head_list));
7595
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007596 weston_plugin_api_destroy_list(compositor);
7597
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007598 if (compositor->heads_changed_source)
7599 wl_event_source_remove(compositor->heads_changed_source);
7600
Marius Vlad7e4db952019-04-17 13:47:06 +03007601 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007602 compositor->debug_scene = NULL;
Marius Vlad3d7d9782019-04-17 12:35:38 +03007603 weston_log_ctx_compositor_destroy(compositor);
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007604
Giulio Camuffo459137b2014-10-11 23:56:24 +03007605 free(compositor);
7606}
7607
7608/** Instruct the compositor to exit.
7609 *
7610 * This functions does not directly destroy the compositor object, it merely
7611 * command it to start the tear down process. It is not guaranteed that the
7612 * tear down will happen immediately.
7613 *
7614 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007615 *
7616 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007617 */
7618WL_EXPORT void
7619weston_compositor_exit(struct weston_compositor *compositor)
7620{
7621 compositor->exit(compositor);
7622}
7623
7624/** Return the user data stored in the compositor.
7625 *
7626 * This function returns the user data pointer set with user_data parameter
7627 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007628 *
7629 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007630 */
7631WL_EXPORT void *
7632weston_compositor_get_user_data(struct weston_compositor *compositor)
7633{
7634 return compositor->user_data;
7635}
Pekka Paalanendd186732016-06-03 14:49:54 +03007636
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007637static const char * const backend_map[] = {
7638 [WESTON_BACKEND_DRM] = "drm-backend.so",
7639 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7640 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7641 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7642 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7643 [WESTON_BACKEND_X11] = "x11-backend.so",
7644};
7645
Pekka Paalanendd186732016-06-03 14:49:54 +03007646/** Load a backend into a weston_compositor
7647 *
7648 * A backend must be loaded to make a weston_compositor work. A backend
7649 * provides input and output capabilities, and determines the renderer to use.
7650 *
7651 * \param compositor A compositor that has not had a backend loaded yet.
7652 * \param backend Name of the backend file.
7653 * \param config_base A pointer to a backend-specific configuration
7654 * structure's 'base' member.
7655 *
7656 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007657 *
7658 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007659 */
7660WL_EXPORT int
7661weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007662 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007663 struct weston_backend_config *config_base)
7664{
7665 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007666 struct weston_backend_config *config_base);
7667
Pekka Paalanend7e35112017-08-29 17:04:12 +03007668 if (compositor->backend) {
7669 weston_log("Error: attempt to load a backend when one is already loaded\n");
7670 return -1;
7671 }
7672
Quentin Glidic887c0182016-07-10 11:00:53 +02007673 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007674 return -1;
7675
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007676 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007677 if (!backend_init)
7678 return -1;
7679
Pekka Paalanend7e35112017-08-29 17:04:12 +03007680 if (backend_init(compositor, config_base) < 0) {
7681 compositor->backend = NULL;
7682 return -1;
7683 }
7684
7685 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007686}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007687
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007688/** weston_compositor_load_xwayland
7689 * \ingroup compositor
7690 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007691WL_EXPORT int
7692weston_compositor_load_xwayland(struct weston_compositor *compositor)
7693{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007694 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007695
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007696 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007697 if (!module_init)
7698 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007699 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007700 return -1;
7701 return 0;
7702}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007703
7704/** Resolve an internal compositor error by disconnecting the client.
7705 *
7706 * This function is used in cases when the wl_buffer turns out
7707 * unusable and there is no fallback path.
7708 *
7709 * It is possible the fault is caused by a compositor bug, the underlying
7710 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7711 * In any case, the options are to either composite garbage or nothing,
7712 * or disconnect the client. This is a helper function for the latter.
7713 *
7714 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7715 *
7716 * \param buffer The weston buffer that is unusable.
7717 * \param msg A custom error message attached to the protocol error.
7718 */
7719WL_EXPORT void
7720weston_buffer_send_server_error(struct weston_buffer *buffer,
7721 const char *msg)
7722{
7723 struct wl_client *client;
7724 struct wl_resource *display_resource;
7725 uint32_t id;
7726
7727 assert(buffer->resource);
7728 id = wl_resource_get_id(buffer->resource);
7729 client = wl_resource_get_client(buffer->resource);
7730 display_resource = wl_client_get_object(client, 1);
7731
7732 assert(display_resource);
7733 wl_resource_post_error(display_resource,
7734 WL_DISPLAY_ERROR_INVALID_OBJECT,
7735 "server error with "
7736 "wl_buffer@%u: %s", id, msg);
7737}