blob: 63f3880cf030b5d6eeab885d829652dc32877bae [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 Vlad4e036292019-07-09 00:36:20 +030078#include "weston-log-internal.h"
79
Marius Vlad78984ee2019-06-11 00:05:08 +030080/**
81 * \defgroup head Head
82 * \defgroup output Output
83 * \defgroup compositor Compositor
84 */
85
Pekka Paalanen0513a952014-05-21 16:17:27 +030086#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
87
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030089weston_output_update_matrix(struct weston_output *output);
90
91static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050096weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010097
Pekka Paalanendcac3512017-12-08 14:13:34 +020098static char *
99weston_output_create_heads_string(struct weston_output *output);
100
Pekka Paalanen6528c032017-03-28 15:27:10 +0300101/** Send wl_output events for mode and scale changes
102 *
103 * \param head Send on all resources bound to this head.
104 * \param mode_changed If true, send the current mode.
105 * \param scale_changed If true, send the current scale.
106 */
107static void
108weston_mode_switch_send_events(struct weston_head *head,
109 bool mode_changed, bool scale_changed)
110{
111 struct weston_output *output = head->output;
112 struct wl_resource *resource;
113 int version;
114
115 wl_resource_for_each(resource, &head->resource_list) {
116 if (mode_changed) {
117 wl_output_send_mode(resource,
118 output->current_mode->flags,
119 output->current_mode->width,
120 output->current_mode->height,
121 output->current_mode->refresh);
122 }
123
124 version = wl_resource_get_version(resource);
125 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
126 wl_output_send_scale(resource, output->current_scale);
127
128 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
129 wl_output_send_done(resource);
130 }
Roman Gilge97391c2019-03-29 13:01:06 +0100131 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
132 zxdg_output_v1_send_logical_position(resource,
133 output->x,
134 output->y);
135 zxdg_output_v1_send_logical_size(resource,
136 output->width,
137 output->height);
138 zxdg_output_v1_send_done(resource);
139 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300140}
141
142static void
143weston_mode_switch_finish(struct weston_output *output,
144 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800145{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200146 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300147 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200148 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200149
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200150 pixman_region32_init(&old_output_region);
151 pixman_region32_copy(&old_output_region, &output->region);
152
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200153 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200154 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200155
156 pixman_region32_init(&output->previous_damage);
157 pixman_region32_init_rect(&output->region, output->x, output->y,
158 output->width, output->height);
159
160 weston_output_update_matrix(output);
161
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200162 /* If a pointer falls outside the outputs new geometry, move it to its
163 * lower-right corner */
164 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500165 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200166 int32_t x, y;
167
168 if (!pointer)
169 continue;
170
171 x = wl_fixed_to_int(pointer->x);
172 y = wl_fixed_to_int(pointer->y);
173
174 if (!pixman_region32_contains_point(&old_output_region,
175 x, y, NULL) ||
176 pixman_region32_contains_point(&output->region,
177 x, y, NULL))
178 continue;
179
180 if (x >= output->x + output->width)
181 x = output->x + output->width - 1;
182 if (y >= output->y + output->height)
183 y = output->y + output->height - 1;
184
185 pointer->x = wl_fixed_from_int(x);
186 pointer->y = wl_fixed_from_int(y);
187 }
188
189 pixman_region32_fini(&old_output_region);
190
Derek Foremandd4cd332014-11-10 10:29:59 -0600191 if (!mode_changed && !scale_changed)
192 return;
193
Hardening57388e42013-09-18 23:56:36 +0200194 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300195 wl_list_for_each(head, &output->head_list, output_link)
196 weston_mode_switch_send_events(head,
197 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600198}
199
David Fort0de859e2016-05-27 23:22:57 +0200200static void
201weston_compositor_reflow_outputs(struct weston_compositor *compositor,
202 struct weston_output *resized_output, int delta_width);
203
Marius Vlad55d87362019-06-11 01:15:35 +0300204/**
205 * \ingroup output
206 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600207WL_EXPORT int
208weston_output_mode_set_native(struct weston_output *output,
209 struct weston_mode *mode,
210 int32_t scale)
211{
212 int ret;
213 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200214 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600215
216 if (!output->switch_mode)
217 return -1;
218
219 if (!output->original_mode) {
220 mode_changed = 1;
221 ret = output->switch_mode(output, mode);
222 if (ret < 0)
223 return ret;
224 if (output->current_scale != scale) {
225 scale_changed = 1;
226 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200227 }
228 }
229
David Fort0de859e2016-05-27 23:22:57 +0200230 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600231 output->native_mode = mode;
232 output->native_scale = scale;
233
234 weston_mode_switch_finish(output, mode_changed, scale_changed);
235
David Fort0de859e2016-05-27 23:22:57 +0200236 if (mode_changed || scale_changed) {
237 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
238
239 wl_signal_emit(&output->compositor->output_resized_signal, output);
240 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600241 return 0;
242}
243
Marius Vlad55d87362019-06-11 01:15:35 +0300244/**
245 * \ingroup output
246 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600247WL_EXPORT int
248weston_output_mode_switch_to_native(struct weston_output *output)
249{
250 int ret;
251 int mode_changed = 0, scale_changed = 0;
252
253 if (!output->switch_mode)
254 return -1;
255
256 if (!output->original_mode) {
257 weston_log("already in the native mode\n");
258 return -1;
259 }
260 /* the non fullscreen clients haven't seen a mode set since we
261 * switched into a temporary, so we need to notify them if the
262 * mode at that time is different from the native mode now.
263 */
264 mode_changed = (output->original_mode != output->native_mode);
265 scale_changed = (output->original_scale != output->native_scale);
266
267 ret = output->switch_mode(output, output->native_mode);
268 if (ret < 0)
269 return ret;
270
271 output->current_scale = output->native_scale;
272
273 output->original_mode = NULL;
274 output->original_scale = 0;
275
276 weston_mode_switch_finish(output, mode_changed, scale_changed);
277
278 return 0;
279}
280
Marius Vlad55d87362019-06-11 01:15:35 +0300281/**
282 * \ingroup output
283 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600284WL_EXPORT int
285weston_output_mode_switch_to_temporary(struct weston_output *output,
286 struct weston_mode *mode,
287 int32_t scale)
288{
289 int ret;
290
291 if (!output->switch_mode)
292 return -1;
293
294 /* original_mode is the last mode non full screen clients have seen,
295 * so we shouldn't change it if we already have one set.
296 */
297 if (!output->original_mode) {
298 output->original_mode = output->native_mode;
299 output->original_scale = output->native_scale;
300 }
301 ret = output->switch_mode(output, mode);
302 if (ret < 0)
303 return ret;
304
305 output->current_scale = scale;
306
307 weston_mode_switch_finish(output, 0, 0);
308
309 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800310}
311
Benjamin Franzke06286262011-05-06 19:12:33 +0200312static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300313region_init_infinite(pixman_region32_t *region)
314{
315 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
316 UINT32_MAX, UINT32_MAX);
317}
318
Pekka Paalanene67858b2013-04-25 13:57:42 +0300319static struct weston_subsurface *
320weston_surface_to_subsurface(struct weston_surface *surface);
321
Jason Ekstranda7af7042013-10-12 22:38:11 -0500322WL_EXPORT struct weston_view *
323weston_view_create(struct weston_surface *surface)
324{
325 struct weston_view *view;
326
Bryce Harringtonde16d892014-11-20 22:21:57 -0800327 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500328 if (view == NULL)
329 return NULL;
330
331 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000332 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333
Jason Ekstranda7af7042013-10-12 22:38:11 -0500334 /* Assign to surface */
335 wl_list_insert(&surface->views, &view->surface_link);
336
337 wl_signal_init(&view->destroy_signal);
338 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300339 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340
Jason Ekstranda7af7042013-10-12 22:38:11 -0500341 pixman_region32_init(&view->clip);
342
343 view->alpha = 1.0;
344 pixman_region32_init(&view->transform.opaque);
345
346 wl_list_init(&view->geometry.transformation_list);
347 wl_list_insert(&view->geometry.transformation_list,
348 &view->transform.position.link);
349 weston_matrix_init(&view->transform.position.matrix);
350 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200351 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500352 pixman_region32_init(&view->transform.boundingbox);
353 view->transform.dirty = 1;
354
Jason Ekstranda7af7042013-10-12 22:38:11 -0500355 return view;
356}
357
Jason Ekstrand108865d2014-06-26 10:04:49 -0700358struct weston_frame_callback {
359 struct wl_resource *resource;
360 struct wl_list link;
361};
362
Pekka Paalanen133e4392014-09-23 22:08:46 -0400363struct weston_presentation_feedback {
364 struct wl_resource *resource;
365
366 /* XXX: could use just wl_resource_get_link() instead */
367 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200368
369 /* The per-surface feedback flags */
370 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400371};
372
373static void
374weston_presentation_feedback_discard(
375 struct weston_presentation_feedback *feedback)
376{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200377 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400378 wl_resource_destroy(feedback->resource);
379}
380
381static void
382weston_presentation_feedback_discard_list(struct wl_list *list)
383{
384 struct weston_presentation_feedback *feedback, *tmp;
385
386 wl_list_for_each_safe(feedback, tmp, list, link)
387 weston_presentation_feedback_discard(feedback);
388}
389
390static void
391weston_presentation_feedback_present(
392 struct weston_presentation_feedback *feedback,
393 struct weston_output *output,
394 uint32_t refresh_nsec,
395 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200396 uint64_t seq,
397 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400398{
399 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300400 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400401 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200402 uint32_t tv_sec_hi;
403 uint32_t tv_sec_lo;
404 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300405 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400406
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300407 wl_list_for_each(head, &output->head_list, output_link) {
408 wl_resource_for_each(o, &head->resource_list) {
409 if (wl_resource_get_client(o) != client)
410 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400411
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300412 wp_presentation_feedback_send_sync_output(feedback->resource, o);
413 done = true;
414 }
415
416 /* For clone mode, send it for just one wl_output global,
417 * they are all equivalent anyway.
418 */
419 if (done)
420 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400421 }
422
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200423 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200424 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200425 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200426 refresh_nsec,
427 seq >> 32, seq & 0xffffffff,
428 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400429 wl_resource_destroy(feedback->resource);
430}
431
432static void
433weston_presentation_feedback_present_list(struct wl_list *list,
434 struct weston_output *output,
435 uint32_t refresh_nsec,
436 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200437 uint64_t seq,
438 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400439{
440 struct weston_presentation_feedback *feedback, *tmp;
441
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200442 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200443 wl_list_empty(list));
444
Pekka Paalanen133e4392014-09-23 22:08:46 -0400445 wl_list_for_each_safe(feedback, tmp, list, link)
446 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200447 refresh_nsec, ts, seq,
448 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400449}
450
Jason Ekstrand7b982072014-05-20 14:33:03 -0500451static void
452surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
453{
454 struct weston_surface_state *state =
455 container_of(listener, struct weston_surface_state,
456 buffer_destroy_listener);
457
458 state->buffer = NULL;
459}
460
461static void
462weston_surface_state_init(struct weston_surface_state *state)
463{
464 state->newly_attached = 0;
465 state->buffer = NULL;
466 state->buffer_destroy_listener.notify =
467 surface_state_handle_buffer_destroy;
468 state->sx = 0;
469 state->sy = 0;
470
Derek Foreman152254b2015-11-26 14:17:48 -0600471 pixman_region32_init(&state->damage_surface);
472 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500473 pixman_region32_init(&state->opaque);
474 region_init_infinite(&state->input);
475
476 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400477 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500478
479 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
480 state->buffer_viewport.buffer.scale = 1;
481 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
482 state->buffer_viewport.surface.width = -1;
483 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300484
485 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530486
487 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530488 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500489}
490
491static void
492weston_surface_state_fini(struct weston_surface_state *state)
493{
494 struct weston_frame_callback *cb, *next;
495
496 wl_list_for_each_safe(cb, next,
497 &state->frame_callback_list, link)
498 wl_resource_destroy(cb->resource);
499
Pekka Paalanen133e4392014-09-23 22:08:46 -0400500 weston_presentation_feedback_discard_list(&state->feedback_list);
501
Jason Ekstrand7b982072014-05-20 14:33:03 -0500502 pixman_region32_fini(&state->input);
503 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600504 pixman_region32_fini(&state->damage_surface);
505 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500506
507 if (state->buffer)
508 wl_list_remove(&state->buffer_destroy_listener.link);
509 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300510
511 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300512 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500513}
514
515static void
516weston_surface_state_set_buffer(struct weston_surface_state *state,
517 struct weston_buffer *buffer)
518{
519 if (state->buffer == buffer)
520 return;
521
522 if (state->buffer)
523 wl_list_remove(&state->buffer_destroy_listener.link);
524 state->buffer = buffer;
525 if (state->buffer)
526 wl_signal_add(&state->buffer->destroy_signal,
527 &state->buffer_destroy_listener);
528}
529
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500530WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500531weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500532{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500533 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400534
Bryce Harringtonde16d892014-11-20 22:21:57 -0800535 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400536 if (surface == NULL)
537 return NULL;
538
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500539 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800540 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500541
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500542 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200543 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400544
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200545 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
546 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200547 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
548 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500549
550 weston_surface_state_init(&surface->pending);
551
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400552 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500553 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300554 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400555
Jason Ekstranda7af7042013-10-12 22:38:11 -0500556 wl_list_init(&surface->views);
557
558 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400559 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500560
Pekka Paalanene67858b2013-04-25 13:57:42 +0300561 wl_list_init(&surface->subsurface_list);
562 wl_list_init(&surface->subsurface_list_pending);
563
Jason Ekstrand1e059042014-10-16 10:55:19 -0500564 weston_matrix_init(&surface->buffer_to_surface_matrix);
565 weston_matrix_init(&surface->surface_to_buffer_matrix);
566
Jonas Ådahld3414f22016-07-22 17:56:31 +0800567 wl_list_init(&surface->pointer_constraints);
568
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300569 surface->acquire_fence_fd = -1;
570
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530571 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530572 surface->current_protection = WESTON_HDCP_DISABLE;
573 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530574
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400575 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500576}
577
Alex Wu8811bf92012-02-28 18:07:54 +0800578WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500579weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200580 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500581{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100582 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200583 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500584}
585
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400586WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500587weston_view_to_global_float(struct weston_view *view,
588 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200589{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500590 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200591 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
592
Jason Ekstranda7af7042013-10-12 22:38:11 -0500593 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200594
595 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200596 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700597 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200598 v.f[3]);
599 *x = 0;
600 *y = 0;
601 return;
602 }
603
604 *x = v.f[0] / v.f[3];
605 *y = v.f[1] / v.f[3];
606 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500607 *x = sx + view->geometry.x;
608 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200609 }
610}
611
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500612WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200613weston_transformed_coord(int width, int height,
614 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200615 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200616 float sx, float sy, float *bx, float *by)
617{
618 switch (transform) {
619 case WL_OUTPUT_TRANSFORM_NORMAL:
620 default:
621 *bx = sx;
622 *by = sy;
623 break;
624 case WL_OUTPUT_TRANSFORM_FLIPPED:
625 *bx = width - sx;
626 *by = sy;
627 break;
628 case WL_OUTPUT_TRANSFORM_90:
629 *bx = height - sy;
630 *by = sx;
631 break;
632 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
633 *bx = height - sy;
634 *by = width - sx;
635 break;
636 case WL_OUTPUT_TRANSFORM_180:
637 *bx = width - sx;
638 *by = height - sy;
639 break;
640 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
641 *bx = sx;
642 *by = height - sy;
643 break;
644 case WL_OUTPUT_TRANSFORM_270:
645 *bx = sy;
646 *by = width - sx;
647 break;
648 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
649 *bx = sy;
650 *by = sx;
651 break;
652 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200653
654 *bx *= scale;
655 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200656}
657
658WL_EXPORT pixman_box32_t
659weston_transformed_rect(int width, int height,
660 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200661 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200662 pixman_box32_t rect)
663{
664 float x1, x2, y1, y2;
665
666 pixman_box32_t ret;
667
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.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200670 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200671 rect.x2, rect.y2, &x2, &y2);
672
673 if (x1 <= x2) {
674 ret.x1 = x1;
675 ret.x2 = x2;
676 } else {
677 ret.x1 = x2;
678 ret.x2 = x1;
679 }
680
681 if (y1 <= y2) {
682 ret.y1 = y1;
683 ret.y2 = y2;
684 } else {
685 ret.y1 = y2;
686 ret.y2 = y1;
687 }
688
689 return ret;
690}
691
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600692/** Transform a region by a matrix, restricted to axis-aligned transformations
693 *
694 * Warning: This function does not work for projective, affine, or matrices
695 * that encode arbitrary rotations. Only 90-degree step rotations are
696 * supported.
697 */
698WL_EXPORT void
699weston_matrix_transform_region(pixman_region32_t *dest,
700 struct weston_matrix *matrix,
701 pixman_region32_t *src)
702{
703 pixman_box32_t *src_rects, *dest_rects;
704 int nrects, i;
705
706 src_rects = pixman_region32_rectangles(src, &nrects);
707 dest_rects = malloc(nrects * sizeof(*dest_rects));
708 if (!dest_rects)
709 return;
710
711 for (i = 0; i < nrects; i++) {
712 struct weston_vector vec1 = {{
713 src_rects[i].x1, src_rects[i].y1, 0, 1
714 }};
715 weston_matrix_transform(matrix, &vec1);
716 vec1.f[0] /= vec1.f[3];
717 vec1.f[1] /= vec1.f[3];
718
719 struct weston_vector vec2 = {{
720 src_rects[i].x2, src_rects[i].y2, 0, 1
721 }};
722 weston_matrix_transform(matrix, &vec2);
723 vec2.f[0] /= vec2.f[3];
724 vec2.f[1] /= vec2.f[3];
725
726 if (vec1.f[0] < vec2.f[0]) {
727 dest_rects[i].x1 = floor(vec1.f[0]);
728 dest_rects[i].x2 = ceil(vec2.f[0]);
729 } else {
730 dest_rects[i].x1 = floor(vec2.f[0]);
731 dest_rects[i].x2 = ceil(vec1.f[0]);
732 }
733
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600734 if (vec1.f[1] < vec2.f[1]) {
735 dest_rects[i].y1 = floor(vec1.f[1]);
736 dest_rects[i].y2 = ceil(vec2.f[1]);
737 } else {
738 dest_rects[i].y1 = floor(vec2.f[1]);
739 dest_rects[i].y2 = ceil(vec1.f[1]);
740 }
741 }
742
743 pixman_region32_clear(dest);
744 pixman_region32_init_rects(dest, dest_rects, nrects);
745 free(dest_rects);
746}
747
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200748WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500749weston_transformed_region(int width, int height,
750 enum wl_output_transform transform,
751 int32_t scale,
752 pixman_region32_t *src, pixman_region32_t *dest)
753{
754 pixman_box32_t *src_rects, *dest_rects;
755 int nrects, i;
756
757 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
758 if (src != dest)
759 pixman_region32_copy(dest, src);
760 return;
761 }
762
763 src_rects = pixman_region32_rectangles(src, &nrects);
764 dest_rects = malloc(nrects * sizeof(*dest_rects));
765 if (!dest_rects)
766 return;
767
768 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
769 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
770 } else {
771 for (i = 0; i < nrects; i++) {
772 switch (transform) {
773 default:
774 case WL_OUTPUT_TRANSFORM_NORMAL:
775 dest_rects[i].x1 = src_rects[i].x1;
776 dest_rects[i].y1 = src_rects[i].y1;
777 dest_rects[i].x2 = src_rects[i].x2;
778 dest_rects[i].y2 = src_rects[i].y2;
779 break;
780 case WL_OUTPUT_TRANSFORM_90:
781 dest_rects[i].x1 = height - src_rects[i].y2;
782 dest_rects[i].y1 = src_rects[i].x1;
783 dest_rects[i].x2 = height - src_rects[i].y1;
784 dest_rects[i].y2 = src_rects[i].x2;
785 break;
786 case WL_OUTPUT_TRANSFORM_180:
787 dest_rects[i].x1 = width - src_rects[i].x2;
788 dest_rects[i].y1 = height - src_rects[i].y2;
789 dest_rects[i].x2 = width - src_rects[i].x1;
790 dest_rects[i].y2 = height - src_rects[i].y1;
791 break;
792 case WL_OUTPUT_TRANSFORM_270:
793 dest_rects[i].x1 = src_rects[i].y1;
794 dest_rects[i].y1 = width - src_rects[i].x2;
795 dest_rects[i].x2 = src_rects[i].y2;
796 dest_rects[i].y2 = width - src_rects[i].x1;
797 break;
798 case WL_OUTPUT_TRANSFORM_FLIPPED:
799 dest_rects[i].x1 = width - src_rects[i].x2;
800 dest_rects[i].y1 = src_rects[i].y1;
801 dest_rects[i].x2 = width - src_rects[i].x1;
802 dest_rects[i].y2 = src_rects[i].y2;
803 break;
804 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
805 dest_rects[i].x1 = height - src_rects[i].y2;
806 dest_rects[i].y1 = width - src_rects[i].x2;
807 dest_rects[i].x2 = height - src_rects[i].y1;
808 dest_rects[i].y2 = width - src_rects[i].x1;
809 break;
810 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
811 dest_rects[i].x1 = src_rects[i].x1;
812 dest_rects[i].y1 = height - src_rects[i].y2;
813 dest_rects[i].x2 = src_rects[i].x2;
814 dest_rects[i].y2 = height - src_rects[i].y1;
815 break;
816 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
817 dest_rects[i].x1 = src_rects[i].y1;
818 dest_rects[i].y1 = src_rects[i].x1;
819 dest_rects[i].x2 = src_rects[i].y2;
820 dest_rects[i].y2 = src_rects[i].x2;
821 break;
822 }
823 }
824 }
825
826 if (scale != 1) {
827 for (i = 0; i < nrects; i++) {
828 dest_rects[i].x1 *= scale;
829 dest_rects[i].x2 *= scale;
830 dest_rects[i].y1 *= scale;
831 dest_rects[i].y2 *= scale;
832 }
833 }
834
835 pixman_region32_clear(dest);
836 pixman_region32_init_rects(dest, dest_rects, nrects);
837 free(dest_rects);
838}
839
Jonny Lamb74130762013-11-26 18:19:46 +0100840static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300841viewport_surface_to_buffer(struct weston_surface *surface,
842 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100843{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200844 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200845 double src_width, src_height;
846 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200847
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200848 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
849 if (vp->surface.width == -1) {
850 *bx = sx;
851 *by = sy;
852 return;
853 }
Jonny Lamb74130762013-11-26 18:19:46 +0100854
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200855 src_x = 0.0;
856 src_y = 0.0;
857 src_width = surface->width_from_buffer;
858 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100859 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200860 src_x = wl_fixed_to_double(vp->buffer.src_x);
861 src_y = wl_fixed_to_double(vp->buffer.src_y);
862 src_width = wl_fixed_to_double(vp->buffer.src_width);
863 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100864 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200865
866 *bx = sx * src_width / surface->width + src_x;
867 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100868}
869
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500870WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200871weston_surface_to_buffer_float(struct weston_surface *surface,
872 float sx, float sy, float *bx, float *by)
873{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200874 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
875
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300876 /* first transform coordinates if the viewport is set */
877 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100878
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500879 weston_transformed_coord(surface->width_from_buffer,
880 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200881 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100882 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200883}
884
Derek Foreman99739672015-12-03 16:38:11 -0600885/** Transform a rectangle from surface coordinates to buffer coordinates
886 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300887 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600888 * from.
889 * \param rect The rectangle to transform.
890 * \return The transformed rectangle.
891 *
892 * Viewport and buffer transformations can only do translation, scaling,
893 * and rotations in 90-degree steps. Therefore the only loss in the
894 * conversion is coordinate rounding.
895 *
896 * However, some coordinate rounding takes place as an intermediate
897 * step before the buffer scale factor is applied, so the rectangle
898 * boundary may not be exactly as expected.
899 *
900 * This is OK for damage tracking since a little extra coverage is
901 * not a problem.
902 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200903WL_EXPORT pixman_box32_t
904weston_surface_to_buffer_rect(struct weston_surface *surface,
905 pixman_box32_t rect)
906{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200907 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100908 float xf, yf;
909
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300910 /* first transform box coordinates if the viewport is set */
911 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100912 rect.x1 = floorf(xf);
913 rect.y1 = floorf(yf);
914
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300915 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600916 rect.x2 = ceilf(xf);
917 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100918
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500919 return weston_transformed_rect(surface->width_from_buffer,
920 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200921 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200922 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200923}
924
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200925/** Transform a region from surface coordinates to buffer coordinates
926 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300927 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200928 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300929 * \param[in] surface_region The region in surface coordinates.
930 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200931 *
932 * Buffer_region must be init'd, but will be completely overwritten.
933 *
934 * Viewport and buffer transformations can only do translation, scaling,
935 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600936 * conversion is from the coordinate rounding that takes place in
937 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300938 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200939 */
940WL_EXPORT void
941weston_surface_to_buffer_region(struct weston_surface *surface,
942 pixman_region32_t *surface_region,
943 pixman_region32_t *buffer_region)
944{
945 pixman_box32_t *src_rects, *dest_rects;
946 int nrects, i;
947
948 src_rects = pixman_region32_rectangles(surface_region, &nrects);
949 dest_rects = malloc(nrects * sizeof(*dest_rects));
950 if (!dest_rects)
951 return;
952
953 for (i = 0; i < nrects; i++) {
954 dest_rects[i] = weston_surface_to_buffer_rect(surface,
955 src_rects[i]);
956 }
957
958 pixman_region32_fini(buffer_region);
959 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
960 free(dest_rects);
961}
962
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200963WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500964weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400965 struct weston_plane *plane)
966{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400968 return;
969
Jason Ekstranda7af7042013-10-12 22:38:11 -0500970 weston_view_damage_below(view);
971 view->plane = plane;
972 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400973}
974
Pekka Paalanen51723d52015-02-17 13:10:01 +0200975/** Inflict damage on the plane where the view is visible.
976 *
977 * \param view The view that causes the damage.
978 *
979 * If the view is currently on a plane (including the primary plane),
980 * take the view's boundingbox, subtract all the opaque views that cover it,
981 * and add the remaining region as damage to the plane. This corresponds
982 * to the damage inflicted to the plane if this view disappeared.
983 *
984 * A repaint is scheduled for this view.
985 *
986 * The region of all opaque views covering this view is stored in
987 * weston_view::clip and updated by view_accumulate_damage() during
988 * weston_output_repaint(). Specifically, that region matches the
989 * scenegraph as it was last painted.
990 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400991WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200993{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500994 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200995
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500996 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200997 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800999 if (view->plane)
1000 pixman_region32_union(&view->plane->damage,
1001 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001002 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001003 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001004}
1005
Pekka Paalanen01e00682017-03-24 16:21:06 +02001006/** Send wl_surface.enter/leave events
1007 *
1008 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001009 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001010 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001011 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001012 *
1013 * Send the enter/leave events for all protocol objects bound to the given
1014 * output by the client owning the surface.
1015 */
1016static void
1017weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001018 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001019 bool enter,
1020 bool leave)
1021{
1022 struct wl_resource *wloutput;
1023 struct wl_client *client;
1024
1025 assert(enter != leave);
1026
1027 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001028 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001029 if (wl_resource_get_client(wloutput) != client)
1030 continue;
1031
1032 if (enter)
1033 wl_surface_send_enter(surface->resource, wloutput);
1034 if (leave)
1035 wl_surface_send_leave(surface->resource, wloutput);
1036 }
1037}
1038
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301039static void
1040weston_surface_compute_protection(struct protected_surface *psurface)
1041{
1042 enum weston_hdcp_protection min_protection;
1043 bool min_protection_valid = false;
1044 struct weston_surface *surface = psurface->surface;
1045 struct weston_output *output;
1046
1047 wl_list_for_each(output, &surface->compositor->output_list, link)
1048 if (surface->output_mask & (1u << output->id)) {
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301049 /*
1050 * If the content-protection is enabled with protection
1051 * mode as RELAXED for a surface, and if
1052 * content-recording features like: screen-shooter,
1053 * recorder, screen-sharing, etc are on, then notify the
1054 * client, that the protection is disabled.
1055 *
1056 * Note: If the protection mode is ENFORCED then there
1057 * is no need to bother the client as the renderer takes
1058 * care of censoring the visibility of the protected
1059 * content.
1060 */
1061
1062 if (output->disable_planes > 0 &&
1063 surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_RELAXED) {
1064 min_protection = WESTON_HDCP_DISABLE;
1065 min_protection_valid = true;
1066 break;
1067 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301068 if (!min_protection_valid) {
1069 min_protection = output->current_protection;
1070 min_protection_valid = true;
1071 }
1072 if (output->current_protection < min_protection)
1073 min_protection = output->current_protection;
1074 }
1075 if (!min_protection_valid)
1076 min_protection = WESTON_HDCP_DISABLE;
1077
1078 surface->current_protection = min_protection;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301079
1080 weston_protected_surface_send_event(psurface, surface->current_protection);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301081}
1082
1083static void
1084notify_surface_protection_change(void *data)
1085{
1086 struct weston_compositor *compositor = data;
1087 struct content_protection *cp;
1088 struct protected_surface *psurface;
1089
1090 cp = compositor->content_protection;
1091 cp->surface_protection_update = NULL;
1092
1093 /* Notify the clients, whose surfaces are changed */
1094 wl_list_for_each(psurface, &cp->protected_list, link)
1095 if (psurface && psurface->surface)
1096 weston_surface_compute_protection(psurface);
1097}
1098
Bryce Harrington3f650b82015-12-23 11:01:58 -08001099/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301100 * \param compositor weston_compositor
1101 *
1102 * Schedule an idle task to notify surface about the update in protection,
1103 * if not already scheduled.
1104 */
1105static void
1106weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1107{
1108 struct content_protection *cp = compositor->content_protection;
1109 struct wl_event_loop *loop;
1110
1111 if (!cp || cp->surface_protection_update)
1112 return;
1113 loop = wl_display_get_event_loop(compositor->wl_display);
1114 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1115 notify_surface_protection_change,
1116 compositor);
1117}
1118
1119/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001120 * \param es The surface
1121 * \param mask The new set of outputs for the surface
1122 *
1123 * Sets the surface's set of outputs to the ones specified by
1124 * the new output mask provided. Identifies the outputs that
1125 * have changed, the posts enter and leave events for these
1126 * outputs as appropriate.
1127 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001128static void
1129weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1130{
1131 uint32_t different = es->output_mask ^ mask;
1132 uint32_t entered = mask & different;
1133 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001134 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001135 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001136 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001137
1138 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001139 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001140 return;
1141 if (different == 0)
1142 return;
1143
1144 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001145 output_bit = 1u << output->id;
1146 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001147 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001148
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001149 wl_list_for_each(head, &output->head_list, output_link) {
1150 weston_surface_send_enter_leave(es, head,
1151 output_bit & entered,
1152 output_bit & left);
1153 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001154 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301155 /*
1156 * Change in surfaces' output mask might trigger a change in its
1157 * protection.
1158 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301159 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001160}
1161
Semi Malinene7a52fb2018-04-26 11:08:10 +02001162static void
1163notify_view_output_destroy(struct wl_listener *listener, void *data)
1164{
1165 struct weston_view *view =
1166 container_of(listener,
1167 struct weston_view, output_destroy_listener);
1168
1169 view->output = NULL;
1170 view->output_destroy_listener.notify = NULL;
1171}
1172
1173/** Set the primary output of the view
1174 *
1175 * \param view The view whose primary output to set
1176 * \param output The new primary output for the view
1177 *
1178 * Set \a output to be the primary output of the \a view.
1179 *
1180 * Notice that the assignment may be temporary; the primary output could be
1181 * automatically changed. Hence, one cannot rely on the value persisting.
1182 *
1183 * Passing NULL as /a output will set the primary output to NULL.
1184 */
1185WL_EXPORT void
1186weston_view_set_output(struct weston_view *view, struct weston_output *output)
1187{
1188 if (view->output_destroy_listener.notify) {
1189 wl_list_remove(&view->output_destroy_listener.link);
1190 view->output_destroy_listener.notify = NULL;
1191 }
1192 view->output = output;
1193 if (output) {
1194 view->output_destroy_listener.notify =
1195 notify_view_output_destroy;
1196 wl_signal_add(&output->destroy_signal,
1197 &view->output_destroy_listener);
1198 }
1199}
1200
Bryce Harrington3f650b82015-12-23 11:01:58 -08001201/** Recalculate which output(s) the surface has views displayed on
1202 *
1203 * \param es The surface to remap to outputs
1204 *
1205 * Finds the output that is showing the largest amount of one
1206 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001207 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001208 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001209 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001210 * in the output_mask for the surface.
1211 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001212static void
1213weston_surface_assign_output(struct weston_surface *es)
1214{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 struct weston_output *new_output;
1216 struct weston_view *view;
1217 pixman_region32_t region;
1218 uint32_t max, area, mask;
1219 pixman_box32_t *e;
1220
1221 new_output = NULL;
1222 max = 0;
1223 mask = 0;
1224 pixman_region32_init(&region);
1225 wl_list_for_each(view, &es->views, surface_link) {
1226 if (!view->output)
1227 continue;
1228
1229 pixman_region32_intersect(&region, &view->transform.boundingbox,
1230 &view->output->region);
1231
1232 e = pixman_region32_extents(&region);
1233 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1234
1235 mask |= view->output_mask;
1236
1237 if (area >= max) {
1238 new_output = view->output;
1239 max = area;
1240 }
1241 }
1242 pixman_region32_fini(&region);
1243
1244 es->output = new_output;
1245 weston_surface_update_output_mask(es, mask);
1246}
1247
Bryce Harrington3f650b82015-12-23 11:01:58 -08001248/** Recalculate which output(s) the view is displayed on
1249 *
1250 * \param ev The view to remap to outputs
1251 *
1252 * Identifies the set of outputs that the view is visible on,
1253 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001254 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001255 *
1256 * Also does the same for the view's surface. See
1257 * weston_surface_assign_output().
1258 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259static void
1260weston_view_assign_output(struct weston_view *ev)
1261{
1262 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001263 struct weston_output *output, *new_output;
1264 pixman_region32_t region;
1265 uint32_t max, area, mask;
1266 pixman_box32_t *e;
1267
1268 new_output = NULL;
1269 max = 0;
1270 mask = 0;
1271 pixman_region32_init(&region);
1272 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001273 if (output->destroying)
1274 continue;
1275
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001277 &output->region);
1278
1279 e = pixman_region32_extents(&region);
1280 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1281
1282 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001283 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001284
1285 if (area >= max) {
1286 new_output = output;
1287 max = area;
1288 }
1289 }
1290 pixman_region32_fini(&region);
1291
Semi Malinene7a52fb2018-04-26 11:08:10 +02001292 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 ev->output_mask = mask;
1294
1295 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001296}
1297
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001298static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001299weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1300 int from_x, int from_y, int *to_x, int *to_y)
1301{
1302 float x, y;
1303
1304 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1305 weston_view_from_global_float(to, x, y, &x, &y);
1306
1307 *to_x = round(x);
1308 *to_y = round(y);
1309}
1310
1311static void
1312weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1313{
1314 pixman_box32_t *a;
1315 pixman_box32_t b;
1316
1317 a = pixman_region32_extents(&from->geometry.scissor);
1318
1319 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1320 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1321
1322 pixman_region32_fini(&to->geometry.scissor);
1323 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1324}
1325
1326static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001327view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001329{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001330 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1331 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001332 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001333 { inbox->x1, inbox->y1 },
1334 { inbox->x1, inbox->y2 },
1335 { inbox->x2, inbox->y1 },
1336 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001337 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001338 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001339 int i;
1340
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001341 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001342 /* avoid rounding empty bbox to 1x1 */
1343 pixman_region32_init(bbox);
1344 return;
1345 }
1346
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001347 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001348 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001350 if (x < min_x)
1351 min_x = x;
1352 if (x > max_x)
1353 max_x = x;
1354 if (y < min_y)
1355 min_y = y;
1356 if (y > max_y)
1357 max_y = y;
1358 }
1359
Pekka Paalanen219b9822012-02-08 15:38:37 +02001360 int_x = floorf(min_x);
1361 int_y = floorf(min_y);
1362 pixman_region32_init_rect(bbox, int_x, int_y,
1363 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001364}
1365
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001366static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001368{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001370
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001371 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 view->geometry.x = roundf(view->geometry.x);
1373 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001374
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001375 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001376 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1377 view->transform.position.matrix.d[12] = view->geometry.x;
1378 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001379
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001381
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 view->transform.inverse = view->transform.position.matrix;
1383 view->transform.inverse.d[12] = -view->geometry.x;
1384 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001385
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001387 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001388 view->surface->width,
1389 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001390 if (view->geometry.scissor_enabled)
1391 pixman_region32_intersect(&view->transform.boundingbox,
1392 &view->transform.boundingbox,
1393 &view->geometry.scissor);
1394
1395 pixman_region32_translate(&view->transform.boundingbox,
1396 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001397
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 if (view->alpha == 1.0) {
1399 pixman_region32_copy(&view->transform.opaque,
1400 &view->surface->opaque);
1401 pixman_region32_translate(&view->transform.opaque,
1402 view->geometry.x,
1403 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001404 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001405}
1406
1407static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001409{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 struct weston_view *parent = view->geometry.parent;
1411 struct weston_matrix *matrix = &view->transform.matrix;
1412 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001413 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001414 pixman_region32_t surfregion;
1415 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001416
Jason Ekstranda7af7042013-10-12 22:38:11 -05001417 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001418
1419 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1421 view->transform.position.matrix.d[12] = view->geometry.x;
1422 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001423
1424 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001425 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001426 weston_matrix_multiply(matrix, &tform->matrix);
1427
Pekka Paalanen483243f2013-03-08 14:56:50 +02001428 if (parent)
1429 weston_matrix_multiply(matrix, &parent->transform.matrix);
1430
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001431 if (weston_matrix_invert(inverse, matrix) < 0) {
1432 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 weston_log("error: weston_view %p"
1434 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001435 return -1;
1436 }
1437
Giulio Camuffo148c1992016-09-04 18:50:46 +03001438 if (view->alpha == 1.0 &&
1439 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1440 pixman_region32_copy(&view->transform.opaque,
1441 &view->surface->opaque);
1442 pixman_region32_translate(&view->transform.opaque,
1443 matrix->d[12],
1444 matrix->d[13]);
1445 }
1446
Pekka Paalanen380adf52015-02-16 14:39:11 +02001447 pixman_region32_init_rect(&surfregion, 0, 0,
1448 view->surface->width, view->surface->height);
1449 if (view->geometry.scissor_enabled)
1450 pixman_region32_intersect(&surfregion, &surfregion,
1451 &view->geometry.scissor);
1452 surfbox = pixman_region32_extents(&surfregion);
1453
1454 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1455 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001456
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001457 return 0;
1458}
1459
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001460static struct weston_layer *
1461get_view_layer(struct weston_view *view)
1462{
1463 if (view->parent_view)
1464 return get_view_layer(view->parent_view);
1465 return view->layer_link.layer;
1466}
1467
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001468WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001470{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001472 struct weston_layer *layer;
1473 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001474
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001476 return;
1477
Pekka Paalanen483243f2013-03-08 14:56:50 +02001478 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001479 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001480
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001482
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001484
Jason Ekstranda7af7042013-10-12 22:38:11 -05001485 pixman_region32_fini(&view->transform.boundingbox);
1486 pixman_region32_fini(&view->transform.opaque);
1487 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001488
Pekka Paalanencd403622012-01-25 13:37:39 +02001489 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490 if (view->geometry.transformation_list.next ==
1491 &view->transform.position.link &&
1492 view->geometry.transformation_list.prev ==
1493 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001494 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001495 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001496 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497 if (weston_view_update_transform_enable(view) < 0)
1498 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001499 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001500
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001501 layer = get_view_layer(view);
1502 if (layer) {
1503 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001504 pixman_region32_intersect(&view->transform.boundingbox,
1505 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001506 pixman_region32_intersect(&view->transform.opaque,
1507 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001508 pixman_region32_fini(&mask);
1509 }
1510
Pekka Paalanen380adf52015-02-16 14:39:11 +02001511 if (parent) {
1512 if (parent->geometry.scissor_enabled) {
1513 view->geometry.scissor_enabled = true;
1514 weston_view_transfer_scissor(parent, view);
1515 } else {
1516 view->geometry.scissor_enabled = false;
1517 }
1518 }
1519
Jason Ekstranda7af7042013-10-12 22:38:11 -05001520 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001521
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001523
Jason Ekstranda7af7042013-10-12 22:38:11 -05001524 wl_signal_emit(&view->surface->compositor->transform_signal,
1525 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001526}
1527
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001528WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001530{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001532
1533 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 * The invariant: if view->geometry.dirty, then all views
1535 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001536 * Corollary: if not parent->geometry.dirty, then all ancestors
1537 * are not dirty.
1538 */
1539
Jason Ekstranda7af7042013-10-12 22:38:11 -05001540 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001541 return;
1542
Jason Ekstranda7af7042013-10-12 22:38:11 -05001543 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001544
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001546 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001548}
1549
1550WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551weston_view_to_global_fixed(struct weston_view *view,
1552 wl_fixed_t vx, wl_fixed_t vy,
1553 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001554{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001555 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001556
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 weston_view_to_global_float(view,
1558 wl_fixed_to_double(vx),
1559 wl_fixed_to_double(vy),
1560 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001561 *x = wl_fixed_from_double(xf);
1562 *y = wl_fixed_from_double(yf);
1563}
1564
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001565WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566weston_view_from_global_float(struct weston_view *view,
1567 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001568{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001569 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001570 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1571
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001573
1574 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001575 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001577 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 *vx = 0;
1579 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001580 return;
1581 }
1582
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583 *vx = v.f[0] / v.f[3];
1584 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001585 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 *vx = x - view->geometry.x;
1587 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001588 }
1589}
1590
1591WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592weston_view_from_global_fixed(struct weston_view *view,
1593 wl_fixed_t x, wl_fixed_t y,
1594 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001595{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001596 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001597
Jason Ekstranda7af7042013-10-12 22:38:11 -05001598 weston_view_from_global_float(view,
1599 wl_fixed_to_double(x),
1600 wl_fixed_to_double(y),
1601 &vxf, &vyf);
1602 *vx = wl_fixed_from_double(vxf);
1603 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001604}
1605
1606WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001607weston_view_from_global(struct weston_view *view,
1608 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001609{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001610 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001611
Jason Ekstranda7af7042013-10-12 22:38:11 -05001612 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1613 *vx = floorf(vxf);
1614 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001615}
1616
Bryce Harrington3f650b82015-12-23 11:01:58 -08001617/**
1618 * \param surface The surface to be repainted
1619 *
1620 * Marks the output(s) that the surface is shown on as needing to be
1621 * repainted. See weston_output_schedule_repaint().
1622 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001623WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001624weston_surface_schedule_repaint(struct weston_surface *surface)
1625{
1626 struct weston_output *output;
1627
1628 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001629 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001630 weston_output_schedule_repaint(output);
1631}
1632
Bryce Harrington3f650b82015-12-23 11:01:58 -08001633/**
1634 * \param view The view to be repainted
1635 *
1636 * Marks the output(s) that the view is shown on as needing to be
1637 * repainted. See weston_output_schedule_repaint().
1638 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001639WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001640weston_view_schedule_repaint(struct weston_view *view)
1641{
1642 struct weston_output *output;
1643
1644 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001645 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001646 weston_output_schedule_repaint(output);
1647}
1648
Pekka Paalanene508ce62015-02-19 13:59:55 +02001649/**
1650 * XXX: This function does it the wrong way.
1651 * surface->damage is the damage from the client, and causes
1652 * surface_flush_damage() to copy pixels. No window management action can
1653 * cause damage to the client-provided content, warranting re-upload!
1654 *
1655 * Instead of surface->damage, this function should record the damage
1656 * with all the views for this surface to avoid extraneous texture
1657 * uploads.
1658 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001659WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001660weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001661{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001662 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001663 0, 0, surface->width,
1664 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001665
Kristian Høgsberg98238702012-08-03 16:29:12 -04001666 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001667}
1668
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001669WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001671{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001672 if (view->geometry.x == x && view->geometry.y == y)
1673 return;
1674
Jason Ekstranda7af7042013-10-12 22:38:11 -05001675 view->geometry.x = x;
1676 view->geometry.y = y;
1677 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001678}
1679
Pekka Paalanen483243f2013-03-08 14:56:50 +02001680static void
1681transform_parent_handle_parent_destroy(struct wl_listener *listener,
1682 void *data)
1683{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001684 struct weston_view *view =
1685 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001686 geometry.parent_destroy_listener);
1687
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001689}
1690
1691WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001693 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001694{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001695 if (view->geometry.parent) {
1696 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1697 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001698
1699 if (!parent)
1700 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001701 }
1702
Jason Ekstranda7af7042013-10-12 22:38:11 -05001703 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001704
Jason Ekstranda7af7042013-10-12 22:38:11 -05001705 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001706 transform_parent_handle_parent_destroy;
1707 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001708 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001710 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001711 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001712 }
1713
Jason Ekstranda7af7042013-10-12 22:38:11 -05001714 weston_view_geometry_dirty(view);
1715}
1716
Pekka Paalanen380adf52015-02-16 14:39:11 +02001717/** Set a clip mask rectangle on a view
1718 *
1719 * \param view The view to set the clip mask on.
1720 * \param x Top-left corner X coordinate of the clip rectangle.
1721 * \param y Top-left corner Y coordinate of the clip rectangle.
1722 * \param width Width of the clip rectangle, non-negative.
1723 * \param height Height of the clip rectangle, non-negative.
1724 *
1725 * A shell may set a clip mask rectangle on a view. Everything outside
1726 * the rectangle is cut away for input and output purposes: it is
1727 * not drawn and cannot be hit by hit-test based input like pointer
1728 * motion or touch-downs. Everything inside the rectangle will behave
1729 * normally. Clients are unaware of clipping.
1730 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001731 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001732 * mask rectangle does not affect the view position, the view is positioned
1733 * as it would be without a clip. The clip also does not change
1734 * weston_surface::width,height.
1735 *
1736 * The clip mask rectangle is part of transformation inheritance
1737 * (weston_view_set_transform_parent()). A clip set in the root of the
1738 * transformation inheritance tree will affect all views in the tree.
1739 * A clip can be set only on the root view. Attempting to set a clip
1740 * on view that has a transformation parent will fail. Assigning a parent
1741 * to a view that has a clip set will cause the clip to be forgotten.
1742 *
1743 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1744 * on the additional transformations in the child views. These transformations
1745 * may not rotate the coordinate axes, i.e., only translation and scaling
1746 * are allowed. Violating this restriction causes the clipping to malfunction.
1747 * Furthermore, using scaling may cause rounding errors in child clipping.
1748 *
1749 * The clip mask rectangle is not automatically adjusted based on
1750 * wl_surface.attach dx and dy arguments.
1751 *
1752 * A clip mask rectangle can be set only if the compositor capability
1753 * WESTON_CAP_VIEW_CLIP_MASK is present.
1754 *
1755 * This function sets the clip mask rectangle and schedules a repaint for
1756 * the view.
1757 */
1758WL_EXPORT void
1759weston_view_set_mask(struct weston_view *view,
1760 int x, int y, int width, int height)
1761{
1762 struct weston_compositor *compositor = view->surface->compositor;
1763
1764 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1765 weston_log("%s not allowed without capability!\n", __func__);
1766 return;
1767 }
1768
1769 if (view->geometry.parent) {
1770 weston_log("view %p has a parent, clip forbidden!\n", view);
1771 return;
1772 }
1773
1774 if (width < 0 || height < 0) {
1775 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1776 x, y, width, height);
1777 return;
1778 }
1779
1780 pixman_region32_fini(&view->geometry.scissor);
1781 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1782 view->geometry.scissor_enabled = true;
1783 weston_view_geometry_dirty(view);
1784 weston_view_schedule_repaint(view);
1785}
1786
1787/** Remove the clip mask from a view
1788 *
1789 * \param view The view to remove the clip mask from.
1790 *
1791 * Removed the clip mask rectangle and schedules a repaint.
1792 *
1793 * \sa weston_view_set_mask
1794 */
1795WL_EXPORT void
1796weston_view_set_mask_infinite(struct weston_view *view)
1797{
1798 view->geometry.scissor_enabled = false;
1799 weston_view_geometry_dirty(view);
1800 weston_view_schedule_repaint(view);
1801}
1802
Armin Krezović0da12b82016-06-30 06:04:33 +02001803/* Check if view should be displayed
1804 *
1805 * The indicator is set manually when assigning
1806 * a view to a surface.
1807 *
1808 * This needs reworking. See the thread starting at:
1809 *
1810 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1811 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001812WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813weston_view_is_mapped(struct weston_view *view)
1814{
Armin Krezović0da12b82016-06-30 06:04:33 +02001815 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001816}
1817
Philipp Zabel70decd52018-09-03 20:11:15 +02001818/* Check if view is opaque in specified region
1819 *
1820 * \param view The view to check for opacity.
1821 * \param region The region to check for opacity, in view coordinates.
1822 *
1823 * Returns true if the view is opaque in the specified region, because view
1824 * alpha is 1.0 and either the opaque region set by the client contains the
1825 * specified region, or the buffer pixel format or solid color is opaque.
1826 */
1827WL_EXPORT bool
1828weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1829{
1830 pixman_region32_t r;
1831 bool ret = false;
1832
1833 if (ev->alpha < 1.0)
1834 return false;
1835
1836 if (ev->surface->is_opaque)
1837 return true;
1838
1839 if (ev->transform.dirty) {
1840 weston_log("%s: transform dirty", __func__);
1841 return false;
1842 }
1843
1844 pixman_region32_init(&r);
1845 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1846
1847 if (!pixman_region32_not_empty(&r))
1848 ret = true;
1849
1850 pixman_region32_fini(&r);
1851
1852 return ret;
1853}
1854
Armin Krezović0da12b82016-06-30 06:04:33 +02001855/* Check if a surface has a view assigned to it
1856 *
1857 * The indicator is set manually when mapping
1858 * a surface and creating a view for it.
1859 *
1860 * This needs to go. See the thread starting at:
1861 *
1862 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1863 *
1864 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001865WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001866weston_surface_is_mapped(struct weston_surface *surface)
1867{
Armin Krezović0da12b82016-06-30 06:04:33 +02001868 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001869}
1870
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001871static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001872surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001873{
1874 struct weston_view *view;
1875
1876 if (surface->width == width && surface->height == height)
1877 return;
1878
1879 surface->width = width;
1880 surface->height = height;
1881
1882 wl_list_for_each(view, &surface->views, surface_link)
1883 weston_view_geometry_dirty(view);
1884}
1885
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001886WL_EXPORT void
1887weston_surface_set_size(struct weston_surface *surface,
1888 int32_t width, int32_t height)
1889{
1890 assert(!surface->resource);
1891 surface_set_size(surface, width, height);
1892}
1893
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001894static int
1895fixed_round_up_to_int(wl_fixed_t f)
1896{
1897 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1898}
1899
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001900static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001901convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1902 int32_t width, int32_t height,
1903 uint32_t transform,
1904 int32_t scale)
1905{
1906 assert(scale > 0);
1907
1908 switch (transform) {
1909 case WL_OUTPUT_TRANSFORM_NORMAL:
1910 case WL_OUTPUT_TRANSFORM_180:
1911 case WL_OUTPUT_TRANSFORM_FLIPPED:
1912 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1913 *width_out = width / scale;
1914 *height_out = height / scale;
1915 break;
1916 case WL_OUTPUT_TRANSFORM_90:
1917 case WL_OUTPUT_TRANSFORM_270:
1918 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1919 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1920 *width_out = height / scale;
1921 *height_out = width / scale;
1922 break;
1923 default:
1924 assert(0 && "invalid transform");
1925 }
1926}
1927
1928static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001929weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001930{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001931 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001932
1933 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001934 surface->width_from_buffer = 0;
1935 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001936 return;
1937 }
1938
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001939 convert_size_by_transform_scale(&surface->width_from_buffer,
1940 &surface->height_from_buffer,
1941 surface->buffer_ref.buffer->width,
1942 surface->buffer_ref.buffer->height,
1943 vp->buffer.transform,
1944 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001945}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001946
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001947static void
1948weston_surface_update_size(struct weston_surface *surface)
1949{
1950 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1951 int32_t width, height;
1952
1953 width = surface->width_from_buffer;
1954 height = surface->height_from_buffer;
1955
1956 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001957 surface_set_size(surface,
1958 vp->surface.width, vp->surface.height);
1959 return;
1960 }
1961
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001962 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001963 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1964 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1965
1966 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001967 return;
1968 }
1969
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001970 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001971}
1972
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001973/** weston_compositor_get_time
1974 * \ingroup compositor
1975 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001976WL_EXPORT void
1977weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001978{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001979 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001980}
1981
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001982/** weston_compositor_pick_view
1983 * \ingroup compositor
1984 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001985WL_EXPORT struct weston_view *
1986weston_compositor_pick_view(struct weston_compositor *compositor,
1987 wl_fixed_t x, wl_fixed_t y,
1988 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001989{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001990 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001991 wl_fixed_t view_x, view_y;
1992 int view_ix, view_iy;
1993 int ix = wl_fixed_to_int(x);
1994 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001995
Jason Ekstranda7af7042013-10-12 22:38:11 -05001996 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001997 if (!pixman_region32_contains_point(
1998 &view->transform.boundingbox, ix, iy, NULL))
1999 continue;
2000
2001 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2002 view_ix = wl_fixed_to_int(view_x);
2003 view_iy = wl_fixed_to_int(view_y);
2004
2005 if (!pixman_region32_contains_point(&view->surface->input,
2006 view_ix, view_iy, NULL))
2007 continue;
2008
Pekka Paalanen380adf52015-02-16 14:39:11 +02002009 if (view->geometry.scissor_enabled &&
2010 !pixman_region32_contains_point(&view->geometry.scissor,
2011 view_ix, view_iy, NULL))
2012 continue;
2013
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002014 *vx = view_x;
2015 *vy = view_y;
2016 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002017 }
2018
Derek Foremanf9318d12015-05-11 15:40:11 -05002019 *vx = wl_fixed_from_int(-1000000);
2020 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002021 return NULL;
2022}
2023
2024static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002025weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002026{
Daniel Stone37816df2012-05-16 18:45:18 +01002027 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002028
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002029 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002030 return;
2031
Daniel Stone37816df2012-05-16 18:45:18 +01002032 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002033 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002034}
2035
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002036WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002037weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002038{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002039 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002040
Jason Ekstranda7af7042013-10-12 22:38:11 -05002041 if (!weston_view_is_mapped(view))
2042 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002043
Jason Ekstranda7af7042013-10-12 22:38:11 -05002044 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002045 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002046 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002047 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002048 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049 wl_list_remove(&view->link);
2050 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002051 view->output_mask = 0;
2052 weston_surface_assign_output(view->surface);
2053
2054 if (weston_surface_is_mapped(view->surface))
2055 return;
2056
2057 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002058 struct weston_touch *touch = weston_seat_get_touch(seat);
2059 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2060 struct weston_keyboard *keyboard =
2061 weston_seat_get_keyboard(seat);
2062
2063 if (keyboard && keyboard->focus == view->surface)
2064 weston_keyboard_set_focus(keyboard, NULL);
2065 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002066 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002067 if (touch && touch->focus == view)
2068 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002069 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002071
Jason Ekstranda7af7042013-10-12 22:38:11 -05002072WL_EXPORT void
2073weston_surface_unmap(struct weston_surface *surface)
2074{
2075 struct weston_view *view;
2076
Armin Krezovićf8486c32016-06-30 06:04:28 +02002077 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002078 wl_list_for_each(view, &surface->views, surface_link)
2079 weston_view_unmap(view);
2080 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002081}
2082
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002083static void
2084weston_surface_reset_pending_buffer(struct weston_surface *surface)
2085{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002086 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002087 surface->pending.sx = 0;
2088 surface->pending.sy = 0;
2089 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002090 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002091}
2092
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093WL_EXPORT void
2094weston_view_destroy(struct weston_view *view)
2095{
2096 wl_signal_emit(&view->destroy_signal, view);
2097
2098 assert(wl_list_empty(&view->geometry.child_list));
2099
2100 if (weston_view_is_mapped(view)) {
2101 weston_view_unmap(view);
2102 weston_compositor_build_view_list(view->surface->compositor);
2103 }
2104
2105 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002106 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002107
2108 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002109 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002110 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002111 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112
2113 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002114 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002115
Jason Ekstranda7af7042013-10-12 22:38:11 -05002116 wl_list_remove(&view->surface_link);
2117
2118 free(view);
2119}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002120
2121WL_EXPORT void
2122weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002123{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002124 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002125 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002126 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002127
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002128 if (--surface->ref_count > 0)
2129 return;
2130
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002131 assert(surface->resource == NULL);
2132
Pekka Paalanenca790762015-04-17 14:23:38 +03002133 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002134
Pekka Paalanene67858b2013-04-25 13:57:42 +03002135 assert(wl_list_empty(&surface->subsurface_list_pending));
2136 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002137
Jason Ekstranda7af7042013-10-12 22:38:11 -05002138 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2139 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002140
Jason Ekstrand7b982072014-05-20 14:33:03 -05002141 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002142
Pekka Paalanende685b82012-12-04 15:58:12 +02002143 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002144 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002145
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002146 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002147 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002148 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002149
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002150 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002151 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002152
Pekka Paalanen133e4392014-09-23 22:08:46 -04002153 weston_presentation_feedback_discard_list(&surface->feedback_list);
2154
Jonas Ådahld3414f22016-07-22 17:56:31 +08002155 wl_list_for_each_safe(constraint, next_constraint,
2156 &surface->pointer_constraints,
2157 link)
2158 weston_pointer_constraint_destroy(constraint);
2159
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002160 fd_clear(&surface->acquire_fence_fd);
2161
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002162 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002163}
2164
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002165static void
2166destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002167{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002168 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002169
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002170 assert(surface);
2171
Giulio Camuffo0d379742013-11-15 22:06:15 +01002172 /* Set the resource to NULL, since we don't want to leave a
2173 * dangling pointer if the surface was refcounted and survives
2174 * the weston_surface_destroy() call. */
2175 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002176
2177 if (surface->viewport_resource)
2178 wl_resource_set_user_data(surface->viewport_resource, NULL);
2179
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002180 if (surface->synchronization_resource) {
2181 wl_resource_set_user_data(surface->synchronization_resource,
2182 NULL);
2183 }
2184
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002185 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002186}
2187
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002188static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002189weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2190{
2191 struct weston_buffer *buffer =
2192 container_of(listener, struct weston_buffer, destroy_listener);
2193
2194 wl_signal_emit(&buffer->destroy_signal, buffer);
2195 free(buffer);
2196}
2197
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002198WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002199weston_buffer_from_resource(struct wl_resource *resource)
2200{
2201 struct weston_buffer *buffer;
2202 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002203
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002204 listener = wl_resource_get_destroy_listener(resource,
2205 weston_buffer_destroy_handler);
2206
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002207 if (listener)
2208 return container_of(listener, struct weston_buffer,
2209 destroy_listener);
2210
2211 buffer = zalloc(sizeof *buffer);
2212 if (buffer == NULL)
2213 return NULL;
2214
2215 buffer->resource = resource;
2216 wl_signal_init(&buffer->destroy_signal);
2217 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002218 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002219 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002220
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002221 return buffer;
2222}
2223
2224static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002225weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2226 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002227{
Pekka Paalanende685b82012-12-04 15:58:12 +02002228 struct weston_buffer_reference *ref =
2229 container_of(listener, struct weston_buffer_reference,
2230 destroy_listener);
2231
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002232 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002233 ref->buffer = NULL;
2234}
2235
2236WL_EXPORT void
2237weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002238 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002239{
2240 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002241 ref->buffer->busy_count--;
2242 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002243 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002244 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002245 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002246 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002247 }
2248
Pekka Paalanende685b82012-12-04 15:58:12 +02002249 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002250 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002251 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002252 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002253 }
2254
Pekka Paalanende685b82012-12-04 15:58:12 +02002255 ref->buffer = buffer;
2256 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2257}
2258
2259static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002260weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2261 void *data)
2262{
2263 struct weston_buffer_release_reference *ref =
2264 container_of(listener, struct weston_buffer_release_reference,
2265 destroy_listener);
2266
2267 assert((struct wl_resource *)data == ref->buffer_release->resource);
2268 ref->buffer_release = NULL;
2269}
2270
2271static void
2272weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2273{
2274 struct wl_resource *resource = buffer_release->resource;
2275 int release_fence_fd = buffer_release->fence_fd;
2276
2277 if (release_fence_fd >= 0) {
2278 zwp_linux_buffer_release_v1_send_fenced_release(
2279 resource, release_fence_fd);
2280 } else {
2281 zwp_linux_buffer_release_v1_send_immediate_release(
2282 resource);
2283 }
2284
2285 wl_resource_destroy(resource);
2286}
2287
2288WL_EXPORT void
2289weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2290 struct weston_buffer_release *buffer_release)
2291{
2292 if (buffer_release == ref->buffer_release)
2293 return;
2294
2295 if (ref->buffer_release) {
2296 ref->buffer_release->ref_count--;
2297 wl_list_remove(&ref->destroy_listener.link);
2298 if (ref->buffer_release->ref_count == 0)
2299 weston_buffer_release_destroy(ref->buffer_release);
2300 }
2301
2302 if (buffer_release) {
2303 buffer_release->ref_count++;
2304 wl_resource_add_destroy_listener(buffer_release->resource,
2305 &ref->destroy_listener);
2306 }
2307
2308 ref->buffer_release = buffer_release;
2309 ref->destroy_listener.notify =
2310 weston_buffer_release_reference_handle_destroy;
2311}
2312
2313WL_EXPORT void
2314weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2315 struct weston_buffer_release_reference *src)
2316{
2317 weston_buffer_release_reference(dest, src->buffer_release);
2318 weston_buffer_release_reference(src, NULL);
2319}
2320
2321static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002322weston_surface_attach(struct weston_surface *surface,
2323 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002324{
2325 weston_buffer_reference(&surface->buffer_ref, buffer);
2326
Pekka Paalanena6421c42012-12-04 15:58:10 +02002327 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002328 if (weston_surface_is_mapped(surface))
2329 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002330 }
2331
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002332 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002333
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002334 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002335 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002336}
2337
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002338/** weston_compositor_damage_all
2339 * \ingroup compositor
2340 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002341WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002342weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002343{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002344 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002345
2346 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002347 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002348}
2349
Marius Vlad55d87362019-06-11 01:15:35 +03002350/**
2351 * \ingroup output
2352 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002353WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002354weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002355{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002356 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002357
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002358 pixman_region32_union(&compositor->primary_plane.damage,
2359 &compositor->primary_plane.damage,
2360 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002361 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002362}
2363
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002364static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002365surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002366{
Pekka Paalanende685b82012-12-04 15:58:12 +02002367 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002368 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002369 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002370
Pekka Paalanenb5026542014-11-12 15:09:24 +02002371 if (weston_timeline_enabled_ &&
2372 pixman_region32_not_empty(&surface->damage))
2373 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2374 TLP_OUTPUT(surface->output), TLP_END);
2375
Jason Ekstrandef540082014-06-26 10:37:36 -07002376 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002377}
2378
2379static void
2380view_accumulate_damage(struct weston_view *view,
2381 pixman_region32_t *opaque)
2382{
2383 pixman_region32_t damage;
2384
2385 pixman_region32_init(&damage);
2386 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002387 pixman_box32_t *extents;
2388
Jason Ekstranda7af7042013-10-12 22:38:11 -05002389 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002390 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002391 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002392 pixman_region32_copy(&damage, &view->surface->damage);
2393 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002394 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002395 }
2396
Pekka Paalanen380adf52015-02-16 14:39:11 +02002397 pixman_region32_intersect(&damage, &damage,
2398 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002399 pixman_region32_subtract(&damage, &damage, opaque);
2400 pixman_region32_union(&view->plane->damage,
2401 &view->plane->damage, &damage);
2402 pixman_region32_fini(&damage);
2403 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002404 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002405}
2406
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002407static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002408compositor_accumulate_damage(struct weston_compositor *ec)
2409{
2410 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002411 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002412 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002413
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002414 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002415
2416 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002417 pixman_region32_copy(&plane->clip, &clip);
2418
2419 pixman_region32_init(&opaque);
2420
Jason Ekstranda7af7042013-10-12 22:38:11 -05002421 wl_list_for_each(ev, &ec->view_list, link) {
2422 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002423 continue;
2424
Jason Ekstranda7af7042013-10-12 22:38:11 -05002425 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002426 }
2427
2428 pixman_region32_union(&clip, &clip, &opaque);
2429 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002430 }
2431
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002432 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002433
Jason Ekstranda7af7042013-10-12 22:38:11 -05002434 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002435 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002436
2437 wl_list_for_each(ev, &ec->view_list, link) {
2438 if (ev->surface->touched)
2439 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002440 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002441
2442 surface_flush_damage(ev->surface);
2443
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002444 /* Both the renderer and the backend have seen the buffer
2445 * by now. If renderer needs the buffer, it has its own
2446 * reference set. If the backend wants to keep the buffer
2447 * around for migrating the surface into a non-primary plane
2448 * later, keep_buffer is true. Otherwise, drop the core
2449 * reference now, and allow early buffer release. This enables
2450 * clients to use single-buffering.
2451 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002452 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002453 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002454 weston_buffer_release_reference(
2455 &ev->surface->buffer_release_ref, NULL);
2456 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002457 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002458}
2459
2460static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002461surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002462{
2463 struct weston_subsurface *sub;
2464
Pekka Paalanene67858b2013-04-25 13:57:42 +03002465 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002466 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002467 continue;
2468
Jason Ekstranda7af7042013-10-12 22:38:11 -05002469 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2470 wl_list_init(&sub->surface->views);
2471
2472 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002473 }
2474}
2475
2476static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002477surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002478{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002479 struct weston_subsurface *sub;
2480 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002481
Jason Ekstranda7af7042013-10-12 22:38:11 -05002482 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2483 if (sub->surface == surface)
2484 continue;
2485
George Kiagiadakised04d382014-06-13 18:10:26 +02002486 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2487 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002488 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002489 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002490
2491 surface_free_unused_subsurface_views(sub->surface);
2492 }
2493}
2494
2495static void
2496view_list_add_subsurface_view(struct weston_compositor *compositor,
2497 struct weston_subsurface *sub,
2498 struct weston_view *parent)
2499{
2500 struct weston_subsurface *child;
2501 struct weston_view *view = NULL, *iv;
2502
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002503 if (!weston_surface_is_mapped(sub->surface))
2504 return;
2505
Jason Ekstranda7af7042013-10-12 22:38:11 -05002506 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2507 if (iv->geometry.parent == parent) {
2508 view = iv;
2509 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002510 }
2511 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002512
2513 if (view) {
2514 /* Put it back in the surface's list of views */
2515 wl_list_remove(&view->surface_link);
2516 wl_list_insert(&sub->surface->views, &view->surface_link);
2517 } else {
2518 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002519 weston_view_set_position(view,
2520 sub->position.x,
2521 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002522 weston_view_set_transform_parent(view, parent);
2523 }
2524
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002525 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002526 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002527 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002528
Pekka Paalanenb188e912013-11-19 14:03:35 +02002529 if (wl_list_empty(&sub->surface->subsurface_list)) {
2530 wl_list_insert(compositor->view_list.prev, &view->link);
2531 return;
2532 }
2533
2534 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2535 if (child->surface == sub->surface)
2536 wl_list_insert(compositor->view_list.prev, &view->link);
2537 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002538 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002539 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002540}
2541
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002542/* This recursively adds the sub-surfaces for a view, relying on the
2543 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2544 * change first happens to the sub-surface list, and then automatically
2545 * propagates here. See weston_surface_damage_subsurfaces() for how the
2546 * sub-surfaces receive damage when the client changes the state.
2547 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002548static void
2549view_list_add(struct weston_compositor *compositor,
2550 struct weston_view *view)
2551{
2552 struct weston_subsurface *sub;
2553
2554 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002555
Pekka Paalanenb188e912013-11-19 14:03:35 +02002556 if (wl_list_empty(&view->surface->subsurface_list)) {
2557 wl_list_insert(compositor->view_list.prev, &view->link);
2558 return;
2559 }
2560
2561 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2562 if (sub->surface == view->surface)
2563 wl_list_insert(compositor->view_list.prev, &view->link);
2564 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002565 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002566 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002567}
2568
2569static void
2570weston_compositor_build_view_list(struct weston_compositor *compositor)
2571{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002572 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002573 struct weston_layer *layer;
2574
2575 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002576 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002577 surface_stash_subsurface_views(view->surface);
2578
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002579 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2580 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002581 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002582
Jason Ekstranda7af7042013-10-12 22:38:11 -05002583 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002584 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002585 view_list_add(compositor, view);
2586 }
2587 }
2588
2589 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002590 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002591 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002592}
2593
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002594static void
2595weston_output_take_feedback_list(struct weston_output *output,
2596 struct weston_surface *surface)
2597{
2598 struct weston_view *view;
2599 struct weston_presentation_feedback *feedback;
2600 uint32_t flags = 0xffffffff;
2601
2602 if (wl_list_empty(&surface->feedback_list))
2603 return;
2604
2605 /* All views must have the flag for the flag to survive. */
2606 wl_list_for_each(view, &surface->views, surface_link) {
2607 /* ignore views that are not on this output at all */
2608 if (view->output_mask & (1u << output->id))
2609 flags &= view->psf_flags;
2610 }
2611
2612 wl_list_for_each(feedback, &surface->feedback_list, link)
2613 feedback->psf_flags = flags;
2614
2615 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2616 wl_list_init(&surface->feedback_list);
2617}
2618
David Herrmann1edf44c2013-10-22 17:11:26 +02002619static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002620weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002621{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002622 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002623 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002624 struct weston_animation *animation, *next;
2625 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002626 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002627 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002628 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002629 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302630 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002631
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002632 if (output->destroying)
2633 return 0;
2634
Pekka Paalanenb5026542014-11-12 15:09:24 +02002635 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2636
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002637 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002638 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002639
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302640 /* Find the highest protection desired for an output */
2641 wl_list_for_each(ev, &ec->view_list, link) {
2642 if (ev->surface->output_mask & (1u << output->id)) {
2643 /*
2644 * The desired_protection of the output should be the
2645 * maximum of the desired_protection of the surfaces,
2646 * that are displayed on that output, to avoid
2647 * reducing the protection for existing surfaces.
2648 */
2649 if (ev->surface->desired_protection > highest_requested)
2650 highest_requested =
2651 ev->surface->desired_protection;
2652 }
2653 }
2654
2655 output->desired_protection = highest_requested;
2656
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002657 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002658 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002659 } else {
2660 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002661 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002662 ev->psf_flags = 0;
2663 }
2664 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002665
Pekka Paalanene67858b2013-04-25 13:57:42 +03002666 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667 wl_list_for_each(ev, &ec->view_list, link) {
2668 /* Note: This operation is safe to do multiple times on the
2669 * same surface.
2670 */
2671 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002672 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002673 &ev->surface->frame_callback_list);
2674 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002675
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002676 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002677 }
2678 }
2679
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002680 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002681
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002682 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002683 pixman_region32_intersect(&output_damage,
2684 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002685 pixman_region32_subtract(&output_damage,
2686 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002687
Scott Moreauccbf29d2012-02-22 14:21:41 -07002688 if (output->dirty)
2689 weston_output_update_matrix(output);
2690
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002691 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002692
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002693 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002694
Daniel Stone09a97e22017-03-01 11:34:06 +00002695 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002696 if (r == 0)
2697 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002698
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002699 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002700
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002701 frame_time_msec = timespec_to_msec(&output->frame_time);
2702
Jonas Ådahldb773762012-06-13 00:01:21 +02002703 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002704 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002705 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002706 }
2707
Scott Moreaud64cf212012-06-08 19:40:54 -06002708 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002709 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002710 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002711 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002712
Pekka Paalanenb5026542014-11-12 15:09:24 +02002713 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2714
David Herrmann1edf44c2013-10-22 17:11:26 +02002715 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002716}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002717
Pekka Paalanen82919792014-05-21 13:51:49 +03002718static void
2719weston_output_schedule_repaint_reset(struct weston_output *output)
2720{
Daniel Stone05df8c12017-03-03 16:59:42 +00002721 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002722 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002723}
2724
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002725static int
2726weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2727 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002728{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002729 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002730 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002731 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002732
Daniel Stone6847b852017-03-01 11:34:08 +00002733 /* We're not ready yet; come back to make a decision later. */
2734 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002735 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002736
2737 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2738 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002739 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002740
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002741 /* If we're sleeping, drop the repaint machinery entirely; we will
2742 * explicitly repaint all outputs when we come back. */
2743 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2744 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2745 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002746
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002747 /* We don't actually need to repaint this output; drop it from
2748 * repaint until something causes damage. */
2749 if (!output->repaint_needed)
2750 goto err;
2751
2752 /* If repaint fails, we aren't going to get weston_output_finish_frame
2753 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002754 * something schedules a successful repaint later. As repainting may
2755 * take some time, re-read our clock as a courtesy to the next
2756 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002757 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002758 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002759 if (ret != 0)
2760 goto err;
2761
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002762 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002763 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002764
2765err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002766 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002767 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002768}
2769
2770static void
2771output_repaint_timer_arm(struct weston_compositor *compositor)
2772{
2773 struct weston_output *output;
2774 bool any_should_repaint = false;
2775 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002776 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002777
2778 weston_compositor_read_presentation_clock(compositor, &now);
2779
2780 wl_list_for_each(output, &compositor->output_list, link) {
2781 int64_t msec_to_this;
2782
2783 if (output->repaint_status != REPAINT_SCHEDULED)
2784 continue;
2785
2786 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2787 &now);
2788 if (!any_should_repaint || msec_to_this < msec_to_next)
2789 msec_to_next = msec_to_this;
2790
2791 any_should_repaint = true;
2792 }
2793
2794 if (!any_should_repaint)
2795 return;
2796
2797 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2798 * This is a workaround to allow coalescing multiple output repaints
2799 * particularly from weston_output_finish_frame()
2800 * into the same call, which would not happen if we called
2801 * output_repaint_timer_handler() directly.
2802 */
2803 if (msec_to_next < 1)
2804 msec_to_next = 1;
2805
2806 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2807}
2808
2809static int
2810output_repaint_timer_handler(void *data)
2811{
2812 struct weston_compositor *compositor = data;
2813 struct weston_output *output;
2814 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002815 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002816 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002817
2818 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002819
2820 if (compositor->backend->repaint_begin)
2821 repaint_data = compositor->backend->repaint_begin(compositor);
2822
2823 wl_list_for_each(output, &compositor->output_list, link) {
2824 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2825 if (ret)
2826 break;
2827 }
2828
2829 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002830 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002831 ret = compositor->backend->repaint_flush(compositor,
2832 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002833 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002834 if (compositor->backend->repaint_cancel)
2835 compositor->backend->repaint_cancel(compositor,
2836 repaint_data);
2837 }
2838
2839 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002840 wl_list_for_each(output, &compositor->output_list, link) {
2841 if (output->repainted)
2842 weston_output_schedule_repaint_reset(output);
2843 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002844 }
Daniel Stone6847b852017-03-01 11:34:08 +00002845
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002846 wl_list_for_each(output, &compositor->output_list, link)
2847 output->repainted = false;
2848
Daniel Stone6847b852017-03-01 11:34:08 +00002849 output_repaint_timer_arm(compositor);
2850
Pekka Paalanen0513a952014-05-21 16:17:27 +03002851 return 0;
2852}
2853
Marius Vlad55d87362019-06-11 01:15:35 +03002854/**
2855 * \ingroup output
2856 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002857WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002858weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002859 const struct timespec *stamp,
2860 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002861{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002862 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002863 int32_t refresh_nsec;
2864 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002865 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002866
Pekka Paalanenb5026542014-11-12 15:09:24 +02002867
Daniel Stone05df8c12017-03-03 16:59:42 +00002868 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002869 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2870
Daniel Stone6847b852017-03-01 11:34:08 +00002871 weston_compositor_read_presentation_clock(compositor, &now);
2872
Daniel Stone3615ce12017-03-01 11:34:05 +00002873 /* If we haven't been supplied any timestamp at all, we don't have a
2874 * timebase to work against, so any delay just wastes time. Push a
2875 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002876 if (!stamp) {
2877 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002878 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002879 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002880
Marius Vladdf9278a2018-03-06 18:56:23 +02002881 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2882 TLP_VBLANK(stamp), TLP_END);
2883
Pekka Paalanend7894d02015-07-03 15:08:53 +03002884 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002885 weston_presentation_feedback_present_list(&output->feedback_list,
2886 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002887 output->msc,
2888 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002889
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002890 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002891
Daniel Stone6847b852017-03-01 11:34:08 +00002892 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2893 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2894 -compositor->repaint_msec);
2895 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002896
2897 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002898 static bool warned;
2899
2900 if (!warned)
2901 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002902 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002903 warned = true;
2904
Daniel Stone6847b852017-03-01 11:34:08 +00002905 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002906 }
2907
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002908 /* Called from restart_repaint_loop and restart happens already after
2909 * the deadline given by repaint_msec? In that case we delay until
2910 * the deadline of the next frame, to give clients a more predictable
2911 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002912 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2913 msec_rel < 0) {
2914 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2915 timespec_add_nsec(&output->next_repaint,
2916 &output->next_repaint,
2917 refresh_nsec);
2918 }
2919 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002920
Daniel Stone3615ce12017-03-01 11:34:05 +00002921out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002922 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002923 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002924}
2925
2926static void
2927idle_repaint(void *data)
2928{
2929 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002930 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002931
Daniel Stone05df8c12017-03-03 16:59:42 +00002932 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2933 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002934 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002935 ret = output->start_repaint_loop(output);
2936 if (ret != 0)
2937 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002938}
2939
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002940WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002941weston_layer_entry_insert(struct weston_layer_entry *list,
2942 struct weston_layer_entry *entry)
2943{
2944 wl_list_insert(&list->link, &entry->link);
2945 entry->layer = list->layer;
2946}
2947
2948WL_EXPORT void
2949weston_layer_entry_remove(struct weston_layer_entry *entry)
2950{
2951 wl_list_remove(&entry->link);
2952 wl_list_init(&entry->link);
2953 entry->layer = NULL;
2954}
2955
Quentin Glidic82681572016-12-17 13:40:51 +01002956
2957/** Initialize the weston_layer struct.
2958 *
2959 * \param compositor The compositor instance
2960 * \param layer The layer to initialize
2961 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002962WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002963weston_layer_init(struct weston_layer *layer,
2964 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002965{
Quentin Glidic82681572016-12-17 13:40:51 +01002966 layer->compositor = compositor;
2967 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002968 wl_list_init(&layer->view_list.link);
2969 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002970 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002971}
2972
2973/** Sets the position of the layer in the layer list. The layer will be placed
2974 * below any layer with the same position value, if any.
2975 * This function is safe to call if the layer is already on the list, but the
2976 * layer may be moved below other layers at the same position, if any.
2977 *
2978 * \param layer The layer to modify
2979 * \param position The position the layer will be placed at
2980 */
2981WL_EXPORT void
2982weston_layer_set_position(struct weston_layer *layer,
2983 enum weston_layer_position position)
2984{
2985 struct weston_layer *below;
2986
2987 wl_list_remove(&layer->link);
2988
2989 /* layer_list is ordered from top to bottom, the last layer being the
2990 * background with the smallest position value */
2991
2992 layer->position = position;
2993 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2994 if (below->position >= layer->position) {
2995 wl_list_insert(&below->link, &layer->link);
2996 return;
2997 }
2998 }
2999 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3000}
3001
3002/** Hide a layer by taking it off the layer list.
3003 * This function is safe to call if the layer is not on the list.
3004 *
3005 * \param layer The layer to hide
3006 */
3007WL_EXPORT void
3008weston_layer_unset_position(struct weston_layer *layer)
3009{
3010 wl_list_remove(&layer->link);
3011 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003012}
3013
3014WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003015weston_layer_set_mask(struct weston_layer *layer,
3016 int x, int y, int width, int height)
3017{
3018 struct weston_view *view;
3019
3020 layer->mask.x1 = x;
3021 layer->mask.x2 = x + width;
3022 layer->mask.y1 = y;
3023 layer->mask.y2 = y + height;
3024
3025 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3026 weston_view_geometry_dirty(view);
3027 }
3028}
3029
3030WL_EXPORT void
3031weston_layer_set_mask_infinite(struct weston_layer *layer)
3032{
3033 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
3034 UINT32_MAX, UINT32_MAX);
3035}
3036
Daniel Stone3b775632018-07-20 08:38:25 +01003037WL_EXPORT bool
3038weston_layer_mask_is_infinite(struct weston_layer *layer)
3039{
3040 return layer->mask.x1 == INT32_MIN &&
3041 layer->mask.y1 == INT32_MIN &&
3042 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
3043 layer->mask.y2 == INT32_MIN + UINT32_MAX;
3044}
3045
Marius Vlad55d87362019-06-11 01:15:35 +03003046/**
3047 * \ingroup output
3048 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003049WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003050weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003051{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003052 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003053 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003054
Bryce Harrington08976ac2016-08-30 12:05:16 -07003055 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3056 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003057 return;
3058
Pekka Paalanenb5026542014-11-12 15:09:24 +02003059 if (!output->repaint_needed)
3060 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
3061
Kristian Høgsbergef044142011-06-21 15:02:12 -04003062 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003063 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003064
3065 /* If we already have a repaint scheduled for our idle handler,
3066 * no need to set it again. If the repaint has been called but
3067 * not finished, then weston_output_finish_frame() will notice
3068 * that a repaint is needed and schedule one. */
3069 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003070 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003071
Daniel Stone05df8c12017-03-03 16:59:42 +00003072 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003073 assert(!output->idle_repaint_source);
3074 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3075 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003076 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003077}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003078
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003079/** weston_compositor_schedule_repaint
3080 * \ingroup compositor
3081 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003082WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003083weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3084{
3085 struct weston_output *output;
3086
3087 wl_list_for_each(output, &compositor->output_list, link)
3088 weston_output_schedule_repaint(output);
3089}
3090
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003091static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003092surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003093{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003094 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003095}
3096
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003097static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003098surface_attach(struct wl_client *client,
3099 struct wl_resource *resource,
3100 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3101{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003102 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003103 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003104
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003105 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003106 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003107 if (buffer == NULL) {
3108 wl_client_post_no_memory(client);
3109 return;
3110 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003111 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003112
Pekka Paalanende685b82012-12-04 15:58:12 +02003113 /* Attach, attach, without commit in between does not send
3114 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003115 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003116
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003117 surface->pending.sx = sx;
3118 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003119 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003120}
3121
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003122static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003123surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003124 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003125 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003126{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003127 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003128
Derek Foreman57e92ed2015-11-17 14:11:35 -06003129 if (width <= 0 || height <= 0)
3130 return;
3131
Derek Foreman152254b2015-11-26 14:17:48 -06003132 pixman_region32_union_rect(&surface->pending.damage_surface,
3133 &surface->pending.damage_surface,
3134 x, y, width, height);
3135}
3136
3137static void
3138surface_damage_buffer(struct wl_client *client,
3139 struct wl_resource *resource,
3140 int32_t x, int32_t y, int32_t width, int32_t height)
3141{
3142 struct weston_surface *surface = wl_resource_get_user_data(resource);
3143
3144 if (width <= 0 || height <= 0)
3145 return;
3146
3147 pixman_region32_union_rect(&surface->pending.damage_buffer,
3148 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003149 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003150}
3151
Kristian Høgsberg33418202011-08-16 23:01:28 -04003152static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003153destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003154{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003155 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003156
3157 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003158 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003159}
3160
3161static void
3162surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003163 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003164{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003165 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003166 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003167
3168 cb = malloc(sizeof *cb);
3169 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003170 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003171 return;
3172 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003173
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003174 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3175 callback);
3176 if (cb->resource == NULL) {
3177 free(cb);
3178 wl_resource_post_no_memory(resource);
3179 return;
3180 }
3181
Jason Ekstranda85118c2013-06-27 20:17:02 -05003182 wl_resource_set_implementation(cb->resource, NULL, cb,
3183 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003184
Pekka Paalanenbc106382012-10-10 12:49:31 +03003185 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003186}
3187
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003188static void
3189surface_set_opaque_region(struct wl_client *client,
3190 struct wl_resource *resource,
3191 struct wl_resource *region_resource)
3192{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003193 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003194 struct weston_region *region;
3195
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003196 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003197 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003198 pixman_region32_copy(&surface->pending.opaque,
3199 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003200 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003201 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003202 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003203}
3204
3205static void
3206surface_set_input_region(struct wl_client *client,
3207 struct wl_resource *resource,
3208 struct wl_resource *region_resource)
3209{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003210 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003211 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003212
3213 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003214 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003215 pixman_region32_copy(&surface->pending.input,
3216 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003217 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003218 pixman_region32_fini(&surface->pending.input);
3219 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003220 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003221}
3222
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003223/* Cause damage to this sub-surface and all its children.
3224 *
3225 * This is useful when there are state changes that need an implicit
3226 * damage, e.g. a z-order change.
3227 */
3228static void
3229weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3230{
3231 struct weston_subsurface *child;
3232
3233 weston_surface_damage(sub->surface);
3234 sub->reordered = false;
3235
3236 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3237 if (child != sub)
3238 weston_surface_damage_subsurfaces(child);
3239}
3240
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003241static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003242weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003243{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003244 struct weston_subsurface *sub;
3245
3246 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3247 parent_link_pending) {
3248 wl_list_remove(&sub->parent_link);
3249 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003250
3251 if (sub->reordered)
3252 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003253 }
3254}
3255
3256static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003257weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003258 struct weston_matrix *matrix)
3259{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003260 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003261 double src_width, src_height, dest_width, dest_height;
3262
3263 weston_matrix_init(matrix);
3264
3265 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3266 src_width = surface->width_from_buffer;
3267 src_height = surface->height_from_buffer;
3268 } else {
3269 src_width = wl_fixed_to_double(vp->buffer.src_width);
3270 src_height = wl_fixed_to_double(vp->buffer.src_height);
3271 }
3272
3273 if (vp->surface.width == -1) {
3274 dest_width = src_width;
3275 dest_height = src_height;
3276 } else {
3277 dest_width = vp->surface.width;
3278 dest_height = vp->surface.height;
3279 }
3280
3281 if (src_width != dest_width || src_height != dest_height)
3282 weston_matrix_scale(matrix,
3283 src_width / dest_width,
3284 src_height / dest_height, 1);
3285
3286 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3287 weston_matrix_translate(matrix,
3288 wl_fixed_to_double(vp->buffer.src_x),
3289 wl_fixed_to_double(vp->buffer.src_y),
3290 0);
3291
3292 switch (vp->buffer.transform) {
3293 case WL_OUTPUT_TRANSFORM_FLIPPED:
3294 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3295 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3296 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3297 weston_matrix_scale(matrix, -1, 1, 1);
3298 weston_matrix_translate(matrix,
3299 surface->width_from_buffer, 0, 0);
3300 break;
3301 }
3302
3303 switch (vp->buffer.transform) {
3304 default:
3305 case WL_OUTPUT_TRANSFORM_NORMAL:
3306 case WL_OUTPUT_TRANSFORM_FLIPPED:
3307 break;
3308 case WL_OUTPUT_TRANSFORM_90:
3309 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3310 weston_matrix_rotate_xy(matrix, 0, 1);
3311 weston_matrix_translate(matrix,
3312 surface->height_from_buffer, 0, 0);
3313 break;
3314 case WL_OUTPUT_TRANSFORM_180:
3315 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3316 weston_matrix_rotate_xy(matrix, -1, 0);
3317 weston_matrix_translate(matrix,
3318 surface->width_from_buffer,
3319 surface->height_from_buffer, 0);
3320 break;
3321 case WL_OUTPUT_TRANSFORM_270:
3322 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3323 weston_matrix_rotate_xy(matrix, 0, -1);
3324 weston_matrix_translate(matrix,
3325 0, surface->width_from_buffer, 0);
3326 break;
3327 }
3328
3329 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3330}
3331
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003332/**
3333 * Compute a + b > c while being safe to overflows.
3334 */
3335static bool
3336fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3337{
3338 return (int64_t)a + (int64_t)b > (int64_t)c;
3339}
3340
3341static bool
3342weston_surface_is_pending_viewport_source_valid(
3343 const struct weston_surface *surface)
3344{
3345 const struct weston_surface_state *pend = &surface->pending;
3346 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3347 int width_from_buffer = 0;
3348 int height_from_buffer = 0;
3349 wl_fixed_t w;
3350 wl_fixed_t h;
3351
3352 /* If viewport source rect is not set, it is always ok. */
3353 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3354 return true;
3355
3356 if (pend->newly_attached) {
3357 if (pend->buffer) {
3358 convert_size_by_transform_scale(&width_from_buffer,
3359 &height_from_buffer,
3360 pend->buffer->width,
3361 pend->buffer->height,
3362 vp->buffer.transform,
3363 vp->buffer.scale);
3364 } else {
3365 /* No buffer: viewport is irrelevant. */
3366 return true;
3367 }
3368 } else {
3369 width_from_buffer = surface->width_from_buffer;
3370 height_from_buffer = surface->height_from_buffer;
3371 }
3372
3373 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3374 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3375
3376 /* No buffer: viewport is irrelevant. */
3377 if (width_from_buffer == 0 || height_from_buffer == 0)
3378 return true;
3379
3380 /* overflow checks for wl_fixed_from_int() */
3381 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3382 return false;
3383 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3384 return false;
3385
3386 w = wl_fixed_from_int(width_from_buffer);
3387 h = wl_fixed_from_int(height_from_buffer);
3388
3389 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3390 return false;
3391 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3392 return false;
3393
3394 return true;
3395}
3396
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003397static bool
3398fixed_is_integer(wl_fixed_t v)
3399{
3400 return (v & 0xff) == 0;
3401}
3402
3403static bool
3404weston_surface_is_pending_viewport_dst_size_int(
3405 const struct weston_surface *surface)
3406{
3407 const struct weston_buffer_viewport *vp =
3408 &surface->pending.buffer_viewport;
3409
3410 if (vp->surface.width != -1) {
3411 assert(vp->surface.width > 0 && vp->surface.height > 0);
3412 return true;
3413 }
3414
3415 return fixed_is_integer(vp->buffer.src_width) &&
3416 fixed_is_integer(vp->buffer.src_height);
3417}
3418
Derek Foreman152254b2015-11-26 14:17:48 -06003419/* Translate pending damage in buffer co-ordinates to surface
3420 * co-ordinates and union it with a pixman_region32_t.
3421 * This should only be called after the buffer is attached.
3422 */
3423static void
3424apply_damage_buffer(pixman_region32_t *dest,
3425 struct weston_surface *surface,
3426 struct weston_surface_state *state)
3427{
3428 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3429
3430 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3431 * translated into surface co-ordinates and unioned with
3432 * any other surface damage.
3433 * None of this makes sense if there is no buffer though.
3434 */
3435 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3436 pixman_region32_t buffer_damage;
3437
3438 pixman_region32_intersect_rect(&state->damage_buffer,
3439 &state->damage_buffer,
3440 0, 0, buffer->width,
3441 buffer->height);
3442 pixman_region32_init(&buffer_damage);
3443 weston_matrix_transform_region(&buffer_damage,
3444 &surface->buffer_to_surface_matrix,
3445 &state->damage_buffer);
3446 pixman_region32_union(dest, dest, &buffer_damage);
3447 pixman_region32_fini(&buffer_damage);
3448 }
3449 /* We should clear this on commit even if there was no buffer */
3450 pixman_region32_clear(&state->damage_buffer);
3451}
3452
Jason Ekstrand1e059042014-10-16 10:55:19 -05003453static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303454weston_surface_set_desired_protection(struct weston_surface *surface,
3455 enum weston_hdcp_protection protection)
3456{
3457 if (surface->desired_protection == protection)
3458 return;
3459 surface->desired_protection = protection;
3460 weston_surface_damage(surface);
3461}
3462
3463static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303464weston_surface_set_protection_mode(struct weston_surface *surface,
3465 enum weston_surface_protection_mode p_mode)
3466{
3467 struct content_protection *cp = surface->compositor->content_protection;
3468 struct protected_surface *psurface;
3469
3470 surface->protection_mode = p_mode;
3471 wl_list_for_each(psurface, &cp->protected_list, link) {
3472 if (!psurface || psurface->surface != surface)
3473 continue;
3474 weston_protected_surface_send_event(psurface,
3475 surface->current_protection);
3476 }
3477}
3478
3479static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003480weston_surface_commit_state(struct weston_surface *surface,
3481 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003482{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003483 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003484 pixman_region32_t opaque;
3485
Alexander Larsson4ea95522013-05-22 14:41:37 +02003486 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003487 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003488 /* wp_viewport.set_source */
3489 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003490 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003491
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003492 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003493 if (state->newly_attached) {
3494 /* zwp_surface_synchronization_v1.set_acquire_fence */
3495 fd_move(&surface->acquire_fence_fd,
3496 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003497 /* zwp_surface_synchronization_v1.get_release */
3498 weston_buffer_release_move(&surface->buffer_release_ref,
3499 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003500 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003501 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003502 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003503 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003504 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003505
Jason Ekstrand1e059042014-10-16 10:55:19 -05003506 weston_surface_build_buffer_matrix(surface,
3507 &surface->surface_to_buffer_matrix);
3508 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3509 &surface->surface_to_buffer_matrix);
3510
Jason Ekstrand7b982072014-05-20 14:33:03 -05003511 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003512 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003513 if (surface->committed)
3514 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003515 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003516
Jason Ekstrand7b982072014-05-20 14:33:03 -05003517 state->sx = 0;
3518 state->sy = 0;
3519 state->newly_attached = 0;
3520 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003521
Derek Foreman152254b2015-11-26 14:17:48 -06003522 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003523 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003524 (pixman_region32_not_empty(&state->damage_surface) ||
3525 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003526 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003527
Pekka Paalanen8e159182012-10-10 12:49:25 +03003528 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003529 &state->damage_surface);
3530
3531 apply_damage_buffer(&surface->damage, surface, state);
3532
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003533 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003534 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003535 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003536
3537 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003538 pixman_region32_init(&opaque);
3539 pixman_region32_intersect_rect(&opaque, &state->opaque,
3540 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003541
3542 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3543 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003544 wl_list_for_each(view, &surface->views, surface_link)
3545 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003546 }
3547
3548 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003549
3550 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003551 pixman_region32_intersect_rect(&surface->input, &state->input,
3552 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003553
Pekka Paalanenbc106382012-10-10 12:49:31 +03003554 /* wl_surface.frame */
3555 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003556 &state->frame_callback_list);
3557 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003558
3559 /* XXX:
3560 * What should happen with a feedback request, if there
3561 * is no wl_buffer attached for this commit?
3562 */
3563
3564 /* presentation.feedback */
3565 wl_list_insert_list(&surface->feedback_list,
3566 &state->feedback_list);
3567 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003568
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303569 /* weston_protected_surface.enforced/relaxed */
3570 if (surface->protection_mode != state->protection_mode)
3571 weston_surface_set_protection_mode(surface,
3572 state->protection_mode);
3573
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303574 /* weston_protected_surface.set_type */
3575 weston_surface_set_desired_protection(surface, state->desired_protection);
3576
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003577 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003578}
3579
3580static void
3581weston_surface_commit(struct weston_surface *surface)
3582{
3583 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003584
Pekka Paalanene67858b2013-04-25 13:57:42 +03003585 weston_surface_commit_subsurface_order(surface);
3586
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003587 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003588}
3589
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003590static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003591weston_subsurface_commit(struct weston_subsurface *sub);
3592
3593static void
3594weston_subsurface_parent_commit(struct weston_subsurface *sub,
3595 int parent_is_synchronized);
3596
3597static void
3598surface_commit(struct wl_client *client, struct wl_resource *resource)
3599{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003600 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003601 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3602
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003603 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3604 assert(surface->viewport_resource);
3605
3606 wl_resource_post_error(surface->viewport_resource,
3607 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3608 "wl_surface@%d has viewport source outside buffer",
3609 wl_resource_get_id(resource));
3610 return;
3611 }
3612
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003613 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3614 assert(surface->viewport_resource);
3615
3616 wl_resource_post_error(surface->viewport_resource,
3617 WP_VIEWPORT_ERROR_BAD_SIZE,
3618 "wl_surface@%d viewport dst size not integer",
3619 wl_resource_get_id(resource));
3620 return;
3621 }
3622
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003623 if (surface->pending.acquire_fence_fd >= 0) {
3624 assert(surface->synchronization_resource);
3625
3626 if (!surface->pending.buffer) {
3627 fd_clear(&surface->pending.acquire_fence_fd);
3628 wl_resource_post_error(surface->synchronization_resource,
3629 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3630 "wl_surface@%"PRIu32" no buffer for synchronization",
3631 wl_resource_get_id(resource));
3632 return;
3633 }
3634
3635 /* We support fences for both wp_linux_dmabuf and opaque EGL
3636 * buffers, as mandated by minor version 2 of the
3637 * zwp_linux_explicit_synchronization_v1 protocol. Since
3638 * renderers that support fences currently only support these
3639 * two buffer types plus SHM buffers, we can just check for the
3640 * SHM buffer case here.
3641 */
3642 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3643 fd_clear(&surface->pending.acquire_fence_fd);
3644 wl_resource_post_error(surface->synchronization_resource,
3645 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3646 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3647 wl_resource_get_id(resource));
3648 return;
3649 }
3650 }
3651
Alexandros Frantzis67629672018-10-19 12:14:11 +03003652 if (surface->pending.buffer_release_ref.buffer_release &&
3653 !surface->pending.buffer) {
3654 assert(surface->synchronization_resource);
3655
3656 wl_resource_post_error(surface->synchronization_resource,
3657 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3658 "wl_surface@%"PRIu32" no buffer for synchronization",
3659 wl_resource_get_id(resource));
3660 return;
3661 }
3662
Pekka Paalanene67858b2013-04-25 13:57:42 +03003663 if (sub) {
3664 weston_subsurface_commit(sub);
3665 return;
3666 }
3667
3668 weston_surface_commit(surface);
3669
3670 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3671 if (sub->surface != surface)
3672 weston_subsurface_parent_commit(sub, 0);
3673 }
3674}
3675
3676static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003677surface_set_buffer_transform(struct wl_client *client,
3678 struct wl_resource *resource, int transform)
3679{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003680 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003681
Jonny Lamba55f1392014-05-30 12:07:15 +02003682 /* if wl_output.transform grows more members this will need to be updated. */
3683 if (transform < 0 ||
3684 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3685 wl_resource_post_error(resource,
3686 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3687 "buffer transform must be a valid transform "
3688 "('%d' specified)", transform);
3689 return;
3690 }
3691
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003692 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003693 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003694}
3695
Alexander Larsson4ea95522013-05-22 14:41:37 +02003696static void
3697surface_set_buffer_scale(struct wl_client *client,
3698 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003699 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003700{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003701 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003702
Jonny Lamba55f1392014-05-30 12:07:15 +02003703 if (scale < 1) {
3704 wl_resource_post_error(resource,
3705 WL_SURFACE_ERROR_INVALID_SCALE,
3706 "buffer scale must be at least one "
3707 "('%d' specified)", scale);
3708 return;
3709 }
3710
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003711 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003712 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003713}
3714
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003715static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003716 surface_destroy,
3717 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003718 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003719 surface_frame,
3720 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003721 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003722 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003723 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003724 surface_set_buffer_scale,
3725 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003726};
3727
3728static void
3729compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003730 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003731{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003732 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003733 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003734
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003735 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003736 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003737 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003738 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003739 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003740
Jason Ekstranda85118c2013-06-27 20:17:02 -05003741 surface->resource =
3742 wl_resource_create(client, &wl_surface_interface,
3743 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003744 if (surface->resource == NULL) {
3745 weston_surface_destroy(surface);
3746 wl_resource_post_no_memory(resource);
3747 return;
3748 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003749 wl_resource_set_implementation(surface->resource, &surface_interface,
3750 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003751
3752 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003753}
3754
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003755static void
3756destroy_region(struct wl_resource *resource)
3757{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003758 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003759
3760 pixman_region32_fini(&region->region);
3761 free(region);
3762}
3763
3764static void
3765region_destroy(struct wl_client *client, struct wl_resource *resource)
3766{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003767 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003768}
3769
3770static void
3771region_add(struct wl_client *client, struct wl_resource *resource,
3772 int32_t x, int32_t y, int32_t width, int32_t height)
3773{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003774 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003775
3776 pixman_region32_union_rect(&region->region, &region->region,
3777 x, y, width, height);
3778}
3779
3780static void
3781region_subtract(struct wl_client *client, struct wl_resource *resource,
3782 int32_t x, int32_t y, int32_t width, int32_t height)
3783{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003784 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003785 pixman_region32_t rect;
3786
3787 pixman_region32_init_rect(&rect, x, y, width, height);
3788 pixman_region32_subtract(&region->region, &region->region, &rect);
3789 pixman_region32_fini(&rect);
3790}
3791
3792static const struct wl_region_interface region_interface = {
3793 region_destroy,
3794 region_add,
3795 region_subtract
3796};
3797
3798static void
3799compositor_create_region(struct wl_client *client,
3800 struct wl_resource *resource, uint32_t id)
3801{
3802 struct weston_region *region;
3803
3804 region = malloc(sizeof *region);
3805 if (region == NULL) {
3806 wl_resource_post_no_memory(resource);
3807 return;
3808 }
3809
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003810 pixman_region32_init(&region->region);
3811
Jason Ekstranda85118c2013-06-27 20:17:02 -05003812 region->resource =
3813 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003814 if (region->resource == NULL) {
3815 free(region);
3816 wl_resource_post_no_memory(resource);
3817 return;
3818 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003819 wl_resource_set_implementation(region->resource, &region_interface,
3820 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003821}
3822
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003823static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003824 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003825 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003826};
3827
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003828static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003829weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3830{
3831 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003832
Jason Ekstrand7b982072014-05-20 14:33:03 -05003833 weston_surface_commit_state(surface, &sub->cached);
3834 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003835
3836 weston_surface_commit_subsurface_order(surface);
3837
3838 weston_surface_schedule_repaint(surface);
3839
Jason Ekstrand7b982072014-05-20 14:33:03 -05003840 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003841}
3842
3843static void
3844weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3845{
3846 struct weston_surface *surface = sub->surface;
3847
3848 /*
3849 * If this commit would cause the surface to move by the
3850 * attach(dx, dy) parameters, the old damage region must be
3851 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003852 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003853 */
Derek Foreman152254b2015-11-26 14:17:48 -06003854 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003855 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003856 pixman_region32_union(&sub->cached.damage_surface,
3857 &sub->cached.damage_surface,
3858 &surface->pending.damage_surface);
3859 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003860
3861 if (surface->pending.newly_attached) {
3862 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003863 weston_surface_state_set_buffer(&sub->cached,
3864 surface->pending.buffer);
3865 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003866 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003867 weston_presentation_feedback_discard_list(
3868 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003869 /* zwp_surface_synchronization_v1.set_acquire_fence */
3870 fd_move(&sub->cached.acquire_fence_fd,
3871 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003872 /* zwp_surface_synchronization_v1.get_release */
3873 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3874 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003875 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303876 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303877 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003878 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003879 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003880 sub->cached.sx += surface->pending.sx;
3881 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003882
Derek Foreman152254b2015-11-26 14:17:48 -06003883 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3884
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003885 sub->cached.buffer_viewport.changed |=
3886 surface->pending.buffer_viewport.changed;
3887 sub->cached.buffer_viewport.buffer =
3888 surface->pending.buffer_viewport.buffer;
3889 sub->cached.buffer_viewport.surface =
3890 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003891
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003892 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003893
3894 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3895
3896 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3897
3898 wl_list_insert_list(&sub->cached.frame_callback_list,
3899 &surface->pending.frame_callback_list);
3900 wl_list_init(&surface->pending.frame_callback_list);
3901
Pekka Paalanen133e4392014-09-23 22:08:46 -04003902 wl_list_insert_list(&sub->cached.feedback_list,
3903 &surface->pending.feedback_list);
3904 wl_list_init(&surface->pending.feedback_list);
3905
Jason Ekstrand7b982072014-05-20 14:33:03 -05003906 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003907}
3908
Derek Foreman280e7dd2014-10-03 13:13:42 -05003909static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003910weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3911{
3912 while (sub) {
3913 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003914 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003915
3916 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003917 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003918
3919 sub = weston_surface_to_subsurface(sub->parent);
3920 }
3921
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003922 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003923}
3924
3925static void
3926weston_subsurface_commit(struct weston_subsurface *sub)
3927{
3928 struct weston_surface *surface = sub->surface;
3929 struct weston_subsurface *tmp;
3930
3931 /* Recursive check for effectively synchronized. */
3932 if (weston_subsurface_is_synchronized(sub)) {
3933 weston_subsurface_commit_to_cache(sub);
3934 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003935 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003936 /* flush accumulated state from cache */
3937 weston_subsurface_commit_to_cache(sub);
3938 weston_subsurface_commit_from_cache(sub);
3939 } else {
3940 weston_surface_commit(surface);
3941 }
3942
3943 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3944 if (tmp->surface != surface)
3945 weston_subsurface_parent_commit(tmp, 0);
3946 }
3947 }
3948}
3949
3950static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003951weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003952{
3953 struct weston_surface *surface = sub->surface;
3954 struct weston_subsurface *tmp;
3955
Pekka Paalanene67858b2013-04-25 13:57:42 +03003956 /* From now on, commit_from_cache the whole sub-tree, regardless of
3957 * the synchronized mode of each child. This sub-surface or some
3958 * of its ancestors were synchronized, so we are synchronized
3959 * all the way down.
3960 */
3961
Jason Ekstrand7b982072014-05-20 14:33:03 -05003962 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003963 weston_subsurface_commit_from_cache(sub);
3964
3965 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3966 if (tmp->surface != surface)
3967 weston_subsurface_parent_commit(tmp, 1);
3968 }
3969}
3970
3971static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003972weston_subsurface_parent_commit(struct weston_subsurface *sub,
3973 int parent_is_synchronized)
3974{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003975 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003976 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003977 wl_list_for_each(view, &sub->surface->views, surface_link)
3978 weston_view_set_position(view,
3979 sub->position.x,
3980 sub->position.y);
3981
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003982 sub->position.set = 0;
3983 }
3984
3985 if (parent_is_synchronized || sub->synchronized)
3986 weston_subsurface_synchronized_commit(sub);
3987}
3988
Pekka Paalanen8274d902014-08-06 19:36:51 +03003989static int
3990subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3991{
3992 return snprintf(buf, len, "sub-surface");
3993}
3994
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003995static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003996subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003997{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003998 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003999
Jason Ekstranda7af7042013-10-12 22:38:11 -05004000 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004001 weston_view_set_position(view,
4002 view->geometry.x + dx,
4003 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004004
4005 /* No need to check parent mappedness, because if parent is not
4006 * mapped, parent is not in a visible layer, so this sub-surface
4007 * will not be drawn either.
4008 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004009
Pekka Paalanene67858b2013-04-25 13:57:42 +03004010 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004011 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004012
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004013 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004014 * because that would call it also for the parent surface,
4015 * which might not be mapped yet. That would lead to
4016 * inconsistent state, where the window could never be
4017 * mapped.
4018 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004019 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004020 * weston_surface_is_mapped() return true, so that when the
4021 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004022 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004023 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004024 }
4025}
4026
4027static struct weston_subsurface *
4028weston_surface_to_subsurface(struct weston_surface *surface)
4029{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004030 if (surface->committed == subsurface_committed)
4031 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004032
4033 return NULL;
4034}
4035
Pekka Paalanen01388e22013-04-25 13:57:44 +03004036WL_EXPORT struct weston_surface *
4037weston_surface_get_main_surface(struct weston_surface *surface)
4038{
4039 struct weston_subsurface *sub;
4040
4041 while (surface && (sub = weston_surface_to_subsurface(surface)))
4042 surface = sub->parent;
4043
4044 return surface;
4045}
4046
Pekka Paalanen50b67472014-10-01 15:02:41 +03004047WL_EXPORT int
4048weston_surface_set_role(struct weston_surface *surface,
4049 const char *role_name,
4050 struct wl_resource *error_resource,
4051 uint32_t error_code)
4052{
4053 assert(role_name);
4054
4055 if (surface->role_name == NULL ||
4056 surface->role_name == role_name ||
4057 strcmp(surface->role_name, role_name) == 0) {
4058 surface->role_name = role_name;
4059
4060 return 0;
4061 }
4062
4063 wl_resource_post_error(error_resource, error_code,
4064 "Cannot assign role %s to wl_surface@%d,"
4065 " already has role %s\n",
4066 role_name,
4067 wl_resource_get_id(surface->resource),
4068 surface->role_name);
4069 return -1;
4070}
4071
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004072WL_EXPORT const char *
4073weston_surface_get_role(struct weston_surface *surface)
4074{
4075 return surface->role_name;
4076}
4077
Pekka Paalanen8274d902014-08-06 19:36:51 +03004078WL_EXPORT void
4079weston_surface_set_label_func(struct weston_surface *surface,
4080 int (*desc)(struct weston_surface *,
4081 char *, size_t))
4082{
4083 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02004084 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004085}
4086
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004087/** Get the size of surface contents
4088 *
4089 * \param surface The surface to query.
4090 * \param width Returns the width of raw contents.
4091 * \param height Returns the height of raw contents.
4092 *
4093 * Retrieves the raw surface content size in pixels for the given surface.
4094 * This is the whole content size in buffer pixels. If the surface
4095 * has no content or the renderer does not implement this feature,
4096 * zeroes are returned.
4097 *
4098 * This function is used to determine the buffer size needed for
4099 * a weston_surface_copy_content() call.
4100 */
4101WL_EXPORT void
4102weston_surface_get_content_size(struct weston_surface *surface,
4103 int *width, int *height)
4104{
4105 struct weston_renderer *rer = surface->compositor->renderer;
4106
4107 if (!rer->surface_get_content_size) {
4108 *width = 0;
4109 *height = 0;
4110 return;
4111 }
4112
4113 rer->surface_get_content_size(surface, width, height);
4114}
4115
Quentin Glidic248dd102016-08-12 10:41:34 +02004116/** Get the bounding box of a surface and its subsurfaces
4117 *
4118 * \param surface The surface to query.
4119 * \return The bounding box relative to the surface origin.
4120 *
4121 */
4122WL_EXPORT struct weston_geometry
4123weston_surface_get_bounding_box(struct weston_surface *surface)
4124{
4125 pixman_region32_t region;
4126 pixman_box32_t *box;
4127 struct weston_subsurface *subsurface;
4128
4129 pixman_region32_init_rect(&region,
4130 0, 0,
4131 surface->width, surface->height);
4132
4133 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4134 pixman_region32_union_rect(&region, &region,
4135 subsurface->position.x,
4136 subsurface->position.y,
4137 subsurface->surface->width,
4138 subsurface->surface->height);
4139
4140 box = pixman_region32_extents(&region);
4141 struct weston_geometry geometry = {
4142 .x = box->x1,
4143 .y = box->y1,
4144 .width = box->x2 - box->x1,
4145 .height = box->y2 - box->y1,
4146 };
4147
4148 pixman_region32_fini(&region);
4149
4150 return geometry;
4151}
4152
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004153/** Copy surface contents to system memory.
4154 *
4155 * \param surface The surface to copy from.
4156 * \param target Pointer to the target memory buffer.
4157 * \param size Size of the target buffer in bytes.
4158 * \param src_x X location on contents to copy from.
4159 * \param src_y Y location on contents to copy from.
4160 * \param width Width in pixels of the area to copy.
4161 * \param height Height in pixels of the area to copy.
4162 * \return 0 for success, -1 for failure.
4163 *
4164 * Surface contents are maintained by the renderer. They can be in a
4165 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4166 * else.
4167 *
4168 * Surface contents are copied into memory pointed to by target,
4169 * which has size bytes of space available. The target memory
4170 * may be larger than needed, but being smaller returns an error.
4171 * The extra bytes in target may or may not be written; their content is
4172 * unspecified. Size must be large enough to hold the image.
4173 *
4174 * The image in the target memory will be arranged in rows from
4175 * top to bottom, and pixels on a row from left to right. The pixel
4176 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4177 * width * 4.
4178 *
4179 * Parameters src_x and src_y define the upper-left corner in buffer
4180 * coordinates (pixels) to copy from. Parameters width and height
4181 * define the size of the area to copy in pixels.
4182 *
4183 * The rectangle defined by src_x, src_y, width, height must fit in
4184 * the surface contents. Otherwise an error is returned.
4185 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004186 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004187 * needed target buffer size and rectangle limits.
4188 *
4189 * CURRENT IMPLEMENTATION RESTRICTIONS:
4190 * - the machine must be little-endian due to Pixman formats.
4191 *
4192 * NOTE: Pixman formats are premultiplied.
4193 */
4194WL_EXPORT int
4195weston_surface_copy_content(struct weston_surface *surface,
4196 void *target, size_t size,
4197 int src_x, int src_y,
4198 int width, int height)
4199{
4200 struct weston_renderer *rer = surface->compositor->renderer;
4201 int cw, ch;
4202 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4203
4204 if (!rer->surface_copy_content)
4205 return -1;
4206
4207 weston_surface_get_content_size(surface, &cw, &ch);
4208
4209 if (src_x < 0 || src_y < 0)
4210 return -1;
4211
4212 if (width <= 0 || height <= 0)
4213 return -1;
4214
4215 if (src_x + width > cw || src_y + height > ch)
4216 return -1;
4217
4218 if (width * bytespp * height > size)
4219 return -1;
4220
4221 return rer->surface_copy_content(surface, target, size,
4222 src_x, src_y, width, height);
4223}
4224
Pekka Paalanene67858b2013-04-25 13:57:42 +03004225static void
4226subsurface_set_position(struct wl_client *client,
4227 struct wl_resource *resource, int32_t x, int32_t y)
4228{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004229 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004230
4231 if (!sub)
4232 return;
4233
4234 sub->position.x = x;
4235 sub->position.y = y;
4236 sub->position.set = 1;
4237}
4238
4239static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004240subsurface_find_sibling(struct weston_subsurface *sub,
4241 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004242{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004243 struct weston_surface *parent = sub->parent;
4244 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004245
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004246 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4247 if (sibling->surface == surface && sibling != sub)
4248 return sibling;
4249 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004250
4251 return NULL;
4252}
4253
4254static struct weston_subsurface *
4255subsurface_sibling_check(struct weston_subsurface *sub,
4256 struct weston_surface *surface,
4257 const char *request)
4258{
4259 struct weston_subsurface *sibling;
4260
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004261 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004262 if (!sibling) {
4263 wl_resource_post_error(sub->resource,
4264 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4265 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004266 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004267 return NULL;
4268 }
4269
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004270 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004271
4272 return sibling;
4273}
4274
4275static void
4276subsurface_place_above(struct wl_client *client,
4277 struct wl_resource *resource,
4278 struct wl_resource *sibling_resource)
4279{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004280 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004281 struct weston_surface *surface =
4282 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004283 struct weston_subsurface *sibling;
4284
4285 if (!sub)
4286 return;
4287
4288 sibling = subsurface_sibling_check(sub, surface, "place_above");
4289 if (!sibling)
4290 return;
4291
4292 wl_list_remove(&sub->parent_link_pending);
4293 wl_list_insert(sibling->parent_link_pending.prev,
4294 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004295
4296 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004297}
4298
4299static void
4300subsurface_place_below(struct wl_client *client,
4301 struct wl_resource *resource,
4302 struct wl_resource *sibling_resource)
4303{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004304 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004305 struct weston_surface *surface =
4306 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004307 struct weston_subsurface *sibling;
4308
4309 if (!sub)
4310 return;
4311
4312 sibling = subsurface_sibling_check(sub, surface, "place_below");
4313 if (!sibling)
4314 return;
4315
4316 wl_list_remove(&sub->parent_link_pending);
4317 wl_list_insert(&sibling->parent_link_pending,
4318 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004319
4320 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004321}
4322
4323static void
4324subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4325{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004326 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004327
4328 if (sub)
4329 sub->synchronized = 1;
4330}
4331
4332static void
4333subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4334{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004335 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004336
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004337 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004338 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004339
4340 /* If sub became effectively desynchronized, flush. */
4341 if (!weston_subsurface_is_synchronized(sub))
4342 weston_subsurface_synchronized_commit(sub);
4343 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004344}
4345
4346static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004347weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4348{
4349 wl_list_remove(&sub->parent_link);
4350 wl_list_remove(&sub->parent_link_pending);
4351 wl_list_remove(&sub->parent_destroy_listener.link);
4352 sub->parent = NULL;
4353}
4354
4355static void
4356weston_subsurface_destroy(struct weston_subsurface *sub);
4357
4358static void
4359subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4360{
4361 struct weston_subsurface *sub =
4362 container_of(listener, struct weston_subsurface,
4363 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004364 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004365
4366 /* The protocol object (wl_resource) is left inert. */
4367 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004368 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004369
4370 weston_subsurface_destroy(sub);
4371}
4372
4373static void
4374subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4375{
4376 struct weston_subsurface *sub =
4377 container_of(listener, struct weston_subsurface,
4378 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004379 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004380 assert(sub->surface != sub->parent);
4381
4382 if (weston_surface_is_mapped(sub->surface))
4383 weston_surface_unmap(sub->surface);
4384
4385 weston_subsurface_unlink_parent(sub);
4386}
4387
4388static void
4389subsurface_resource_destroy(struct wl_resource *resource)
4390{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004391 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004392
4393 if (sub)
4394 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004395}
4396
4397static void
4398subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4399{
4400 wl_resource_destroy(resource);
4401}
4402
4403static void
4404weston_subsurface_link_parent(struct weston_subsurface *sub,
4405 struct weston_surface *parent)
4406{
4407 sub->parent = parent;
4408 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004409 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004410 &sub->parent_destroy_listener);
4411
4412 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4413 wl_list_insert(&parent->subsurface_list_pending,
4414 &sub->parent_link_pending);
4415}
4416
4417static void
4418weston_subsurface_link_surface(struct weston_subsurface *sub,
4419 struct weston_surface *surface)
4420{
4421 sub->surface = surface;
4422 sub->surface_destroy_listener.notify =
4423 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004424 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004425 &sub->surface_destroy_listener);
4426}
4427
4428static void
4429weston_subsurface_destroy(struct weston_subsurface *sub)
4430{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004431 struct weston_view *view, *next;
4432
Pekka Paalanene67858b2013-04-25 13:57:42 +03004433 assert(sub->surface);
4434
4435 if (sub->resource) {
4436 assert(weston_surface_to_subsurface(sub->surface) == sub);
4437 assert(sub->parent_destroy_listener.notify ==
4438 subsurface_handle_parent_destroy);
4439
George Kiagiadakised04d382014-06-13 18:10:26 +02004440 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4441 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004442 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004443 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004444
Pekka Paalanene67858b2013-04-25 13:57:42 +03004445 if (sub->parent)
4446 weston_subsurface_unlink_parent(sub);
4447
Jason Ekstrand7b982072014-05-20 14:33:03 -05004448 weston_surface_state_fini(&sub->cached);
4449 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004450
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004451 sub->surface->committed = NULL;
4452 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004453 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004454 } else {
4455 /* the dummy weston_subsurface for the parent itself */
4456 assert(sub->parent_destroy_listener.notify == NULL);
4457 wl_list_remove(&sub->parent_link);
4458 wl_list_remove(&sub->parent_link_pending);
4459 }
4460
4461 wl_list_remove(&sub->surface_destroy_listener.link);
4462 free(sub);
4463}
4464
4465static const struct wl_subsurface_interface subsurface_implementation = {
4466 subsurface_destroy,
4467 subsurface_set_position,
4468 subsurface_place_above,
4469 subsurface_place_below,
4470 subsurface_set_sync,
4471 subsurface_set_desync
4472};
4473
4474static struct weston_subsurface *
4475weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4476 struct weston_surface *parent)
4477{
4478 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004479 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004480
Bryce Harringtonde16d892014-11-20 22:21:57 -08004481 sub = zalloc(sizeof *sub);
4482 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004483 return NULL;
4484
Jason Ekstranda7af7042013-10-12 22:38:11 -05004485 wl_list_init(&sub->unused_views);
4486
Jason Ekstranda85118c2013-06-27 20:17:02 -05004487 sub->resource =
4488 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004489 if (!sub->resource) {
4490 free(sub);
4491 return NULL;
4492 }
4493
Jason Ekstranda85118c2013-06-27 20:17:02 -05004494 wl_resource_set_implementation(sub->resource,
4495 &subsurface_implementation,
4496 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004497 weston_subsurface_link_surface(sub, surface);
4498 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004499 weston_surface_state_init(&sub->cached);
4500 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004501 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004502
4503 return sub;
4504}
4505
4506/* Create a dummy subsurface for having the parent itself in its
4507 * sub-surface lists. Makes stacking order manipulation easy.
4508 */
4509static struct weston_subsurface *
4510weston_subsurface_create_for_parent(struct weston_surface *parent)
4511{
4512 struct weston_subsurface *sub;
4513
Bryce Harringtonde16d892014-11-20 22:21:57 -08004514 sub = zalloc(sizeof *sub);
4515 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004516 return NULL;
4517
4518 weston_subsurface_link_surface(sub, parent);
4519 sub->parent = parent;
4520 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4521 wl_list_insert(&parent->subsurface_list_pending,
4522 &sub->parent_link_pending);
4523
4524 return sub;
4525}
4526
4527static void
4528subcompositor_get_subsurface(struct wl_client *client,
4529 struct wl_resource *resource,
4530 uint32_t id,
4531 struct wl_resource *surface_resource,
4532 struct wl_resource *parent_resource)
4533{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004534 struct weston_surface *surface =
4535 wl_resource_get_user_data(surface_resource);
4536 struct weston_surface *parent =
4537 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004538 struct weston_subsurface *sub;
4539 static const char where[] = "get_subsurface: wl_subsurface@";
4540
4541 if (surface == parent) {
4542 wl_resource_post_error(resource,
4543 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4544 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004545 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004546 return;
4547 }
4548
4549 if (weston_surface_to_subsurface(surface)) {
4550 wl_resource_post_error(resource,
4551 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4552 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004553 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004554 return;
4555 }
4556
Pekka Paalanen50b67472014-10-01 15:02:41 +03004557 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4558 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004559 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004560
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004561 if (weston_surface_get_main_surface(parent) == surface) {
4562 wl_resource_post_error(resource,
4563 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4564 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004565 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004566 return;
4567 }
4568
Pekka Paalanene67858b2013-04-25 13:57:42 +03004569 /* make sure the parent is in its own list */
4570 if (wl_list_empty(&parent->subsurface_list)) {
4571 if (!weston_subsurface_create_for_parent(parent)) {
4572 wl_resource_post_no_memory(resource);
4573 return;
4574 }
4575 }
4576
4577 sub = weston_subsurface_create(id, surface, parent);
4578 if (!sub) {
4579 wl_resource_post_no_memory(resource);
4580 return;
4581 }
4582
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004583 surface->committed = subsurface_committed;
4584 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004585 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004586}
4587
4588static void
4589subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4590{
4591 wl_resource_destroy(resource);
4592}
4593
4594static const struct wl_subcompositor_interface subcompositor_interface = {
4595 subcompositor_destroy,
4596 subcompositor_get_subsurface
4597};
4598
4599static void
4600bind_subcompositor(struct wl_client *client,
4601 void *data, uint32_t version, uint32_t id)
4602{
4603 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004604 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004605
Jason Ekstranda85118c2013-06-27 20:17:02 -05004606 resource =
4607 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004608 if (resource == NULL) {
4609 wl_client_post_no_memory(client);
4610 return;
4611 }
4612 wl_resource_set_implementation(resource, &subcompositor_interface,
4613 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004614}
4615
Bryce Harrington0795ece2016-08-30 12:04:26 -07004616/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004617 *
4618 * \param compositor The compositor instance
4619 * \param state The DPMS state the outputs will be set to
4620 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004621static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004622weston_compositor_dpms(struct weston_compositor *compositor,
4623 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004624{
4625 struct weston_output *output;
4626
Bryce Harrington08976ac2016-08-30 12:05:16 -07004627 wl_list_for_each(output, &compositor->output_list, link)
4628 if (output->set_dpms)
4629 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004630}
4631
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004632/** Restores the compositor to active status
4633 *
4634 * \param compositor The compositor instance
4635 *
4636 * If the compositor was in a sleeping mode, all outputs are powered
4637 * back on via DPMS. Otherwise if the compositor was inactive
4638 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4639 * signal will fire.
4640 *
4641 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004642 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004643 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004644WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004645weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004646{
Neil Roberts8b62e202013-09-30 13:14:47 +01004647 uint32_t old_state = compositor->state;
4648
4649 /* The state needs to be changed before emitting the wake
4650 * signal because that may try to schedule a repaint which
4651 * will not work if the compositor is still sleeping */
4652 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4653
4654 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004655 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004656 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004657 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004658 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004659 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004660 /* fall through */
4661 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004662 wl_event_source_timer_update(compositor->idle_source,
4663 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004664 }
4665}
4666
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004667/** Turns off rendering and frame events for the compositor.
4668 *
4669 * \param compositor The compositor instance
4670 *
4671 * This is used for example to prevent further rendering while the
4672 * compositor is shutting down.
4673 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004674 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004675 *
4676 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004677 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004678WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004679weston_compositor_offscreen(struct weston_compositor *compositor)
4680{
4681 switch (compositor->state) {
4682 case WESTON_COMPOSITOR_OFFSCREEN:
4683 return;
4684 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004685 default:
4686 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4687 wl_event_source_timer_update(compositor->idle_source, 0);
4688 }
4689}
4690
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004691/** Powers down all attached output devices
4692 *
4693 * \param compositor The compositor instance
4694 *
4695 * Causes rendering to the outputs to cease, and no frame events to be
4696 * sent. Only powers down the outputs if the compositor is not already
4697 * in sleep mode.
4698 *
4699 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004700 *
4701 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004702 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004703WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004704weston_compositor_sleep(struct weston_compositor *compositor)
4705{
4706 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4707 return;
4708
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004709 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004710 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4711 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4712}
4713
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004714/** Sets compositor to idle mode
4715 *
4716 * \param data The compositor instance
4717 *
4718 * This is called when the idle timer fires. Once the compositor is in
4719 * idle mode it requires a wake action (e.g. via
4720 * weston_compositor_wake()) to restore it. The compositor's
4721 * idle_signal will be triggered when the idle event occurs.
4722 *
4723 * Idleness can be inhibited by setting the compositor's idle_inhibit
4724 * property.
4725 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004726static int
4727idle_handler(void *data)
4728{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004729 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004730
4731 if (compositor->idle_inhibit)
4732 return 1;
4733
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004734 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004735 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004736
4737 return 1;
4738}
4739
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004740WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004741weston_plane_init(struct weston_plane *plane,
4742 struct weston_compositor *ec,
4743 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004744{
4745 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004746 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004747 plane->x = x;
4748 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004749 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004750
4751 /* Init the link so that the call to wl_list_remove() when releasing
4752 * the plane without ever stacking doesn't lead to a crash */
4753 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004754}
4755
4756WL_EXPORT void
4757weston_plane_release(struct weston_plane *plane)
4758{
Xiong Zhang97116532013-10-23 13:58:31 +08004759 struct weston_view *view;
4760
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004761 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004762 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004763
Xiong Zhang97116532013-10-23 13:58:31 +08004764 wl_list_for_each(view, &plane->compositor->view_list, link) {
4765 if (view->plane == plane)
4766 view->plane = NULL;
4767 }
4768
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004769 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004770}
4771
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004772/** weston_compositor_stack_plane
4773 * \ingroup compositor
4774 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004775WL_EXPORT void
4776weston_compositor_stack_plane(struct weston_compositor *ec,
4777 struct weston_plane *plane,
4778 struct weston_plane *above)
4779{
4780 if (above)
4781 wl_list_insert(above->link.prev, &plane->link);
4782 else
4783 wl_list_insert(&ec->plane_list, &plane->link);
4784}
4785
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004786static void
4787output_release(struct wl_client *client, struct wl_resource *resource)
4788{
4789 wl_resource_destroy(resource);
4790}
4791
4792static const struct wl_output_interface output_interface = {
4793 output_release,
4794};
4795
4796
Casey Dahlin9074db52012-04-19 22:50:09 -04004797static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004798{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004799 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004800}
4801
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004802static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004803bind_output(struct wl_client *client,
4804 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004805{
Pekka Paalanen05347622017-03-27 12:24:34 +03004806 struct weston_head *head = data;
4807 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004808 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004809 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004810
Jason Ekstranda85118c2013-06-27 20:17:02 -05004811 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004812 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004813 if (resource == NULL) {
4814 wl_client_post_no_memory(client);
4815 return;
4816 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004817
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004818 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004819 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004820 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004821
Pekka Paalanen05347622017-03-27 12:24:34 +03004822 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004823 wl_output_send_geometry(resource,
4824 output->x,
4825 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004826 head->mm_width,
4827 head->mm_height,
4828 head->subpixel,
4829 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004830 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004831 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004832 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004833 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004834
4835 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004836 wl_output_send_mode(resource,
4837 mode->flags,
4838 mode->width,
4839 mode->height,
4840 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004841 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004842
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004843 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004844 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004845}
4846
Pekka Paalanendcac3512017-12-08 14:13:34 +02004847static void
4848weston_head_add_global(struct weston_head *head)
4849{
4850 head->global = wl_global_create(head->compositor->wl_display,
4851 &wl_output_interface, 3,
4852 head, bind_output);
4853}
4854
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004855/** Remove the global wl_output protocol object
4856 *
4857 * \param head The head whose global to remove.
4858 *
4859 * Also orphans the wl_resources for this head (wl_output).
4860 */
4861static void
4862weston_head_remove_global(struct weston_head *head)
4863{
4864 struct wl_resource *resource, *tmp;
4865
4866 if (head->global)
4867 wl_global_destroy(head->global);
4868 head->global = NULL;
4869
4870 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4871 unbind_resource(resource);
4872 wl_resource_set_destructor(resource, NULL);
4873 wl_resource_set_user_data(resource, NULL);
4874 }
Roman Gilge97391c2019-03-29 13:01:06 +01004875
4876 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
4877 /* It's sufficient to unset the destructor, then the list elements
4878 * won't be accessed.
4879 */
4880 wl_resource_set_destructor(resource, NULL);
4881 }
4882 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004883}
4884
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004885/** Get the backing object of wl_output
4886 *
4887 * \param resource A wl_output protocol object.
4888 * \return The backing object (user data) of a wl_resource representing a
4889 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004890 *
4891 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004892 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004893WL_EXPORT struct weston_head *
4894weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004895{
4896 assert(wl_resource_instance_of(resource, &wl_output_interface,
4897 &output_interface));
4898
4899 return wl_resource_get_user_data(resource);
4900}
4901
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004902/** Initialize a pre-allocated weston_head
4903 *
4904 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004905 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004906 *
4907 * The head will be safe to attach, detach and release.
4908 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004909 * The name is used in logs, and can be used by compositors as a configuration
4910 * identifier.
4911 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004912 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004913 * \internal
4914 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004915WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004916weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004917{
4918 /* Add some (in)sane defaults which can be used
4919 * for checking if an output was properly configured
4920 */
4921 memset(head, 0, sizeof *head);
4922
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004923 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004924 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004925 wl_list_init(&head->output_link);
4926 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01004927 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004928 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05304929 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004930}
4931
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004932/** Send output heads changed signal
4933 *
4934 * \param output The output that changed.
4935 *
4936 * Notify that the enabled output gained and/or lost heads, or that the
4937 * associated heads may have changed their connection status. This does not
4938 * include cases where the output becomes enabled or disabled. The registered
4939 * callbacks are called after the change has successfully happened.
4940 *
4941 * If connection status change causes the compositor to attach or detach a head
4942 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03004943 *
4944 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004945 */
4946static void
4947weston_output_emit_heads_changed(struct weston_output *output)
4948{
4949 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4950 output);
4951}
4952
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004953/** Idle task for emitting heads_changed_signal */
4954static void
4955weston_compositor_call_heads_changed(void *data)
4956{
4957 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004958 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004959
4960 compositor->heads_changed_source = NULL;
4961
4962 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004963
4964 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4965 if (head->output && head->output->enabled)
4966 weston_output_emit_heads_changed(head->output);
4967 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004968}
4969
4970/** Schedule a call on idle to heads_changed callback
4971 *
4972 * \param compositor The Compositor.
4973 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004974 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004975 * \internal
4976 */
4977static void
4978weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4979{
4980 struct wl_event_loop *loop;
4981
4982 if (compositor->heads_changed_source)
4983 return;
4984
4985 loop = wl_display_get_event_loop(compositor->wl_display);
4986 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4987 weston_compositor_call_heads_changed, compositor);
4988}
4989
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004990/** Register a new head
4991 *
4992 * \param compositor The compositor.
4993 * \param head The head to register, must not be already registered.
4994 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004995 * This signals the core that a new head has become available, leading to
4996 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004997 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004998 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004999 * \internal
5000 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005001WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005002weston_compositor_add_head(struct weston_compositor *compositor,
5003 struct weston_head *head)
5004{
5005 assert(wl_list_empty(&head->compositor_link));
5006 assert(head->name);
5007
5008 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5009 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005010 weston_compositor_schedule_heads_changed(compositor);
5011}
5012
5013/** Adds a listener to be called when heads change
5014 *
5015 * \param compositor The compositor.
5016 * \param listener The listener to add.
5017 *
Marius Vlada2dace22019-06-12 16:05:44 +03005018 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005019 *
5020 * The listener function will be called after heads are added or their
5021 * connection status has changed. Several changes may be accumulated into a
5022 * single call. The user is expected to iterate over the existing heads and
5023 * check their statuses to find out what changed.
5024 *
5025 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5026 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005027 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005028 */
5029WL_EXPORT void
5030weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5031 struct wl_listener *listener)
5032{
5033 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005034}
5035
5036/** Iterate over available heads
5037 *
5038 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005039 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005040 * \return The next available head in the list.
5041 *
5042 * Returns all available heads, regardless of being connected or enabled.
5043 *
5044 * You can iterate over all heads as follows:
5045 * \code
5046 * struct weston_head *head = NULL;
5047 *
5048 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5049 * ...
5050 * }
5051 * \endcode
5052 *
5053 * If you cause \c iter to be removed from the list, you cannot use it to
5054 * continue iterating. Removing any other item is safe.
5055 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005056 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005057 */
5058WL_EXPORT struct weston_head *
5059weston_compositor_iterate_heads(struct weston_compositor *compositor,
5060 struct weston_head *iter)
5061{
5062 struct wl_list *list = &compositor->head_list;
5063 struct wl_list *node;
5064
5065 assert(compositor);
5066 assert(!iter || iter->compositor == compositor);
5067
5068 if (iter)
5069 node = iter->compositor_link.next;
5070 else
5071 node = list->next;
5072
5073 assert(node);
5074 assert(!iter || node != &iter->compositor_link);
5075
5076 if (node == list)
5077 return NULL;
5078
5079 return container_of(node, struct weston_head, compositor_link);
5080}
5081
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005082/** Iterate over attached heads
5083 *
5084 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005085 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005086 * \return The next attached head in the list.
5087 *
5088 * Returns all heads currently attached to the output.
5089 *
5090 * You can iterate over all heads as follows:
5091 * \code
5092 * struct weston_head *head = NULL;
5093 *
5094 * while ((head = weston_output_iterate_heads(output, head))) {
5095 * ...
5096 * }
5097 * \endcode
5098 *
5099 * If you cause \c iter to be removed from the list, you cannot use it to
5100 * continue iterating. Removing any other item is safe.
5101 *
Marius Vlad55d87362019-06-11 01:15:35 +03005102 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005103 */
5104WL_EXPORT struct weston_head *
5105weston_output_iterate_heads(struct weston_output *output,
5106 struct weston_head *iter)
5107{
5108 struct wl_list *list = &output->head_list;
5109 struct wl_list *node;
5110
5111 assert(output);
5112 assert(!iter || iter->output == output);
5113
5114 if (iter)
5115 node = iter->output_link.next;
5116 else
5117 node = list->next;
5118
5119 assert(node);
5120 assert(!iter || node != &iter->output_link);
5121
5122 if (node == list)
5123 return NULL;
5124
5125 return container_of(node, struct weston_head, output_link);
5126}
5127
Pekka Paalanendcac3512017-12-08 14:13:34 +02005128/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005129 *
5130 * \param output The output to attach to.
5131 * \param head The head that is not yet attached.
5132 * \return 0 on success, -1 on failure.
5133 *
5134 * Attaches the given head to the output. All heads of an output are clones
5135 * and share the resolution and timings.
5136 *
5137 * Cloning heads this way uses less resources than creating an output for
5138 * each head, but is not always possible due to environment, driver and hardware
5139 * limitations.
5140 *
5141 * On failure, the head remains unattached. Success of this function does not
5142 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005143 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005144 *
Marius Vlad55d87362019-06-11 01:15:35 +03005145 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005146 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005147WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005148weston_output_attach_head(struct weston_output *output,
5149 struct weston_head *head)
5150{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005151 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005152
5153 if (!wl_list_empty(&head->output_link))
5154 return -1;
5155
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005156 if (output->attach_head) {
5157 if (output->attach_head(output, head) < 0)
5158 return -1;
5159 } else if (!wl_list_empty(&output->head_list)) {
5160 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005161 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005162 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005163
5164 head->output = output;
5165 wl_list_insert(output->head_list.prev, &head->output_link);
5166
Pekka Paalanendcac3512017-12-08 14:13:34 +02005167 if (output->enabled) {
5168 weston_head_add_global(head);
5169
5170 head_names = weston_output_create_heads_string(output);
5171 weston_log("Output '%s' updated to have head(s) %s\n",
5172 output->name, head_names);
5173 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005174
5175 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005176 }
5177
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005178 return 0;
5179}
5180
5181/** Detach a head from its output
5182 *
5183 * \param head The head to detach.
5184 *
5185 * It is safe to detach a non-attached head.
5186 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005187 * If the head is attached to an enabled output and the output will be left
5188 * with no heads, the output will be disabled.
5189 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005190 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005191 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005192 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005193WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005194weston_head_detach(struct weston_head *head)
5195{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005196 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005197 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005198
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005199 wl_list_remove(&head->output_link);
5200 wl_list_init(&head->output_link);
5201 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005202
5203 if (!output)
5204 return;
5205
5206 if (output->detach_head)
5207 output->detach_head(output, head);
5208
5209 if (output->enabled) {
5210 weston_head_remove_global(head);
5211
Pekka Paalanena0106992017-12-08 16:11:17 +02005212 if (wl_list_empty(&output->head_list)) {
5213 weston_log("Output '%s' no heads left, disabling.\n",
5214 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005215 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005216 } else {
5217 head_names = weston_output_create_heads_string(output);
5218 weston_log("Output '%s' updated to have head(s) %s\n",
5219 output->name, head_names);
5220 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005221
5222 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005223 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005224 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005225}
5226
5227/** Destroy a head
5228 *
5229 * \param head The head to be released.
5230 *
5231 * Destroys the head. The caller is responsible for freeing the memory pointed
5232 * to by \c head.
5233 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005234 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005235 * \internal
5236 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005237WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005238weston_head_release(struct weston_head *head)
5239{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005240 wl_signal_emit(&head->destroy_signal, head);
5241
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005242 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005243
5244 free(head->make);
5245 free(head->model);
5246 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005247 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005248
5249 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005250}
5251
Pekka Paalanene19970f2017-08-28 14:11:02 +03005252static void
5253weston_head_set_device_changed(struct weston_head *head)
5254{
5255 head->device_changed = true;
5256
5257 if (head->compositor)
5258 weston_compositor_schedule_heads_changed(head->compositor);
5259}
5260
5261/** String equal comparison with NULLs being equal */
5262static bool
5263str_null_eq(const char *a, const char *b)
5264{
5265 if (!a && !b)
5266 return true;
5267
5268 if (!!a != !!b)
5269 return false;
5270
5271 return strcmp(a, b) == 0;
5272}
5273
Pekka Paalanen01f60212017-03-24 15:39:24 +02005274/** Store monitor make, model and serial number
5275 *
5276 * \param head The head to modify.
5277 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5278 * any string, or NULL for none.
5279 * \param model The monitor model or name, or a made-up string, or NULL for
5280 * none.
5281 * \param serialno The monitor serial number, a made-up string, or NULL for
5282 * none.
5283 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005284 * This may set the device_changed flag.
5285 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005286 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005287 * \internal
5288 */
5289WL_EXPORT void
5290weston_head_set_monitor_strings(struct weston_head *head,
5291 const char *make,
5292 const char *model,
5293 const char *serialno)
5294{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005295 if (str_null_eq(head->make, make) &&
5296 str_null_eq(head->model, model) &&
5297 str_null_eq(head->serial_number, serialno))
5298 return;
5299
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005300 free(head->make);
5301 free(head->model);
5302 free(head->serial_number);
5303
5304 head->make = make ? strdup(make) : NULL;
5305 head->model = model ? strdup(model) : NULL;
5306 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005307
5308 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005309}
5310
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005311/** Store display non-desktop status
5312 *
5313 * \param head The head to modify.
5314 * \param non_desktop Whether the head connects to a non-desktop display.
5315 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005316 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005317 * \internal
5318 */
5319WL_EXPORT void
5320weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5321{
5322 if (head->non_desktop == non_desktop)
5323 return;
5324
5325 head->non_desktop = non_desktop;
5326
5327 weston_head_set_device_changed(head);
5328}
5329
Pekka Paalanen01f60212017-03-24 15:39:24 +02005330/** Store physical image size
5331 *
5332 * \param head The head to modify.
5333 * \param mm_width Image area width in millimeters.
5334 * \param mm_height Image area height in millimeters.
5335 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005336 * This may set the device_changed flag.
5337 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005338 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005339 * \internal
5340 */
5341WL_EXPORT void
5342weston_head_set_physical_size(struct weston_head *head,
5343 int32_t mm_width, int32_t mm_height)
5344{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005345 if (head->mm_width == mm_width &&
5346 head->mm_height == mm_height)
5347 return;
5348
Pekka Paalanen01f60212017-03-24 15:39:24 +02005349 head->mm_width = mm_width;
5350 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005351
5352 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005353}
5354
5355/** Store monitor sub-pixel layout
5356 *
5357 * \param head The head to modify.
5358 * \param sp Sub-pixel layout. The possible values are:
5359 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5360 * - WL_OUTPUT_SUBPIXEL_NONE,
5361 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5362 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5363 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5364 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5365 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005366 * This may set the device_changed flag.
5367 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005368 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005369 * \internal
5370 */
5371WL_EXPORT void
5372weston_head_set_subpixel(struct weston_head *head,
5373 enum wl_output_subpixel sp)
5374{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005375 if (head->subpixel == sp)
5376 return;
5377
Pekka Paalanen01f60212017-03-24 15:39:24 +02005378 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005379
5380 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005381}
5382
5383/** Mark the monitor as internal
5384 *
5385 * This is used for embedded screens, like laptop panels.
5386 *
5387 * \param head The head to mark as internal.
5388 *
5389 * By default a head is external. The type is often inferred from the physical
5390 * connector type.
5391 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005392 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005393 * \internal
5394 */
5395WL_EXPORT void
5396weston_head_set_internal(struct weston_head *head)
5397{
5398 head->connection_internal = true;
5399}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005400
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005401/** Store connector status
5402 *
5403 * \param head The head to modify.
5404 * \param connected Whether the head is connected.
5405 *
5406 * Connectors are created as disconnected. This function can be used to
5407 * set the connector status.
5408 *
5409 * The status should be set to true when a physical connector is connected to
5410 * a video sink device like a monitor and to false when the connector is
5411 * disconnected. For nested backends, the connection status should reflect the
5412 * connection to the parent display server.
5413 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005414 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005415 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005416 *
5417 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005418 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005419 * \internal
5420 */
5421WL_EXPORT void
5422weston_head_set_connection_status(struct weston_head *head, bool connected)
5423{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005424 if (head->connected == connected)
5425 return;
5426
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005427 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005428
Pekka Paalanene19970f2017-08-28 14:11:02 +03005429 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005430}
5431
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305432static void
5433weston_output_compute_protection(struct weston_output *output)
5434{
5435 struct weston_head *head;
5436 enum weston_hdcp_protection op_protection;
5437 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305438 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305439
5440 wl_list_for_each(head, &output->head_list, output_link) {
5441 if (!op_protection_valid) {
5442 op_protection = head->current_protection;
5443 op_protection_valid = true;
5444 }
5445 if (head->current_protection < op_protection)
5446 op_protection = head->current_protection;
5447 }
5448
5449 if (!op_protection_valid)
5450 op_protection = WESTON_HDCP_DISABLE;
5451
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305452 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305453 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305454 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305455 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305456 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305457}
5458
5459WL_EXPORT void
5460weston_head_set_content_protection_status(struct weston_head *head,
5461 enum weston_hdcp_protection status)
5462{
5463 head->current_protection = status;
5464 if (head->output)
5465 weston_output_compute_protection(head->output);
5466}
5467
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005468/** Is the head currently connected?
5469 *
5470 * \param head The head to query.
5471 * \return Connection status.
5472 *
5473 * Returns true if the head is physically connected to a monitor, or in
5474 * case of a nested backend returns true when there is a connection to the
5475 * parent display server.
5476 *
5477 * This is independent from the head being enabled.
5478 *
5479 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005480 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005481 */
5482WL_EXPORT bool
5483weston_head_is_connected(struct weston_head *head)
5484{
5485 return head->connected;
5486}
5487
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005488/** Is the head currently enabled?
5489 *
5490 * \param head The head to query.
5491 * \return Video status.
5492 *
5493 * Returns true if the head is currently transmitting a video stream.
5494 *
5495 * This is independent of the head being connected.
5496 *
5497 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005498 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005499 */
5500WL_EXPORT bool
5501weston_head_is_enabled(struct weston_head *head)
5502{
5503 if (!head->output)
5504 return false;
5505
5506 return head->output->enabled;
5507}
5508
Pekka Paalanene19970f2017-08-28 14:11:02 +03005509/** Has the device information changed?
5510 *
5511 * \param head The head to query.
5512 * \return True if the device information has changed since last reset.
5513 *
5514 * The information about the connected display device, e.g. a monitor, may
5515 * change without being disconnected in between. Changing information
5516 * causes a call to the heads_changed hook.
5517 *
5518 * The information includes make, model, serial number, physical size,
5519 * and sub-pixel type. The connection status is also included.
5520 *
5521 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005522 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005523 */
5524WL_EXPORT bool
5525weston_head_is_device_changed(struct weston_head *head)
5526{
5527 return head->device_changed;
5528}
5529
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005530/** Does the head represent a non-desktop display?
5531 *
5532 * \param head The head to query.
5533 * \return True if the device is a non-desktop display.
5534 *
5535 * Non-desktop heads are not attached to outputs by default.
5536 * This stops weston from extending the desktop onto head mounted displays.
5537 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005538 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005539 */
5540WL_EXPORT bool
5541weston_head_is_non_desktop(struct weston_head *head)
5542{
5543 return head->non_desktop;
5544}
5545
Pekka Paalanene19970f2017-08-28 14:11:02 +03005546/** Acknowledge device information change
5547 *
5548 * \param head The head to acknowledge.
5549 *
5550 * Clears the device changed flag on this head. When a compositor has processed
5551 * device information, it should call this to be able to notice further
5552 * changes.
5553 *
5554 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005555 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005556 */
5557WL_EXPORT void
5558weston_head_reset_device_changed(struct weston_head *head)
5559{
5560 head->device_changed = false;
5561}
5562
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005563/** Get the name of a head
5564 *
5565 * \param head The head to query.
5566 * \return The head's name, not NULL.
5567 *
5568 * The name depends on the backend. The DRM backend uses connector names,
5569 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005570 *
5571 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005572 */
5573WL_EXPORT const char *
5574weston_head_get_name(struct weston_head *head)
5575{
5576 return head->name;
5577}
5578
5579/** Get the output the head is attached to
5580 *
5581 * \param head The head to query.
5582 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005583 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005584 */
5585WL_EXPORT struct weston_output *
5586weston_head_get_output(struct weston_head *head)
5587{
5588 return head->output;
5589}
5590
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005591/** Add destroy callback for a head
5592 *
5593 * \param head The head to watch for.
5594 * \param listener The listener to add. The \c notify member must be set.
5595 *
5596 * Heads may get destroyed for various reasons by the backends. If a head is
5597 * attached to an output, the compositor should listen for head destruction
5598 * and reconfigure or destroy the output if necessary.
5599 *
5600 * The destroy callbacks will be called on weston_head destruction before any
5601 * automatic detaching from an associated weston_output and before any
5602 * weston_head information is lost.
5603 *
5604 * The \c data argument to the notify callback is the weston_head being
5605 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005606 *
5607 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005608 */
5609WL_EXPORT void
5610weston_head_add_destroy_listener(struct weston_head *head,
5611 struct wl_listener *listener)
5612{
5613 wl_signal_add(&head->destroy_signal, listener);
5614}
5615
5616/** Look up destroy listener for a head
5617 *
5618 * \param head The head to query.
5619 * \param notify The notify function used used for the added destroy listener.
5620 * \return The listener, or NULL if not found.
5621 *
5622 * This looks up the previously added destroy listener struct based on the
5623 * notify function it has. The listener can be used to access user data
5624 * through \c container_of().
5625 *
5626 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005627 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005628 */
5629WL_EXPORT struct wl_listener *
5630weston_head_get_destroy_listener(struct weston_head *head,
5631 wl_notify_func_t notify)
5632{
5633 return wl_signal_get(&head->destroy_signal, notify);
5634}
5635
David Fort0de859e2016-05-27 23:22:57 +02005636/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005637static void
David Fort0de859e2016-05-27 23:22:57 +02005638weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5639 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005640{
5641 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005642 bool start_resizing = false;
5643
5644 if (!delta_width)
5645 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005646
5647 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005648 if (output == resized_output) {
5649 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005650 continue;
5651 }
5652
David Fort0de859e2016-05-27 23:22:57 +02005653 if (start_resizing) {
5654 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005655 output->dirty = 1;
5656 }
5657 }
5658}
5659
Pekka Paalanend72bad22017-03-29 17:01:41 +03005660static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005661weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005662{
Scott Moreau850ca422012-05-21 15:21:25 -06005663 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005664
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005665 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005666 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005667
Scott Moreauccbf29d2012-02-22 14:21:41 -07005668 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005669 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005670 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005671 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005672 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005673 weston_matrix_scale(&output->matrix, magnification,
5674 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005675 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005676
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005677 switch (output->transform) {
5678 case WL_OUTPUT_TRANSFORM_FLIPPED:
5679 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5680 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5681 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5682 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5683 weston_matrix_scale(&output->matrix, -1, 1, 1);
5684 break;
5685 }
5686
5687 switch (output->transform) {
5688 default:
5689 case WL_OUTPUT_TRANSFORM_NORMAL:
5690 case WL_OUTPUT_TRANSFORM_FLIPPED:
5691 break;
5692 case WL_OUTPUT_TRANSFORM_90:
5693 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5694 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5695 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5696 break;
5697 case WL_OUTPUT_TRANSFORM_180:
5698 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5699 weston_matrix_translate(&output->matrix,
5700 -output->width, -output->height, 0);
5701 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5702 break;
5703 case WL_OUTPUT_TRANSFORM_270:
5704 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5705 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5706 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5707 break;
5708 }
5709
5710 if (output->current_scale != 1)
5711 weston_matrix_scale(&output->matrix,
5712 output->current_scale,
5713 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005714
Scott Moreauccbf29d2012-02-22 14:21:41 -07005715 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005716
5717 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005718}
5719
Scott Moreau1bad5db2012-08-18 01:04:05 -06005720static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005721weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005722{
5723 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005724 output->native_scale = scale;
5725 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005726
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005727 convert_size_by_transform_scale(&output->width, &output->height,
5728 output->current_mode->width,
5729 output->current_mode->height,
5730 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005731}
5732
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005733static void
5734weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005735{
5736 output->x = x;
5737 output->y = y;
5738
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005739 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005740 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005741
5742 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005743 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005744 output->width,
5745 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005746}
5747
Marius Vlad55d87362019-06-11 01:15:35 +03005748/**
5749 * \ingroup output
5750 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005751WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005752weston_output_move(struct weston_output *output, int x, int y)
5753{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005754 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005755 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005756 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005757
5758 output->move_x = x - output->x;
5759 output->move_y = y - output->y;
5760
5761 if (output->move_x == 0 && output->move_y == 0)
5762 return;
5763
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005764 weston_output_init_geometry(output, x, y);
5765
5766 output->dirty = 1;
5767
5768 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005769 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005770
5771 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005772 wl_list_for_each(head, &output->head_list, output_link) {
5773 wl_resource_for_each(resource, &head->resource_list) {
5774 wl_output_send_geometry(resource,
5775 output->x,
5776 output->y,
5777 head->mm_width,
5778 head->mm_height,
5779 head->subpixel,
5780 head->make,
5781 head->model,
5782 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005783
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005784 ver = wl_resource_get_version(resource);
5785 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5786 wl_output_send_done(resource);
5787 }
Roman Gilge97391c2019-03-29 13:01:06 +01005788
5789 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5790 zxdg_output_v1_send_logical_position(resource,
5791 output->x,
5792 output->y);
5793 zxdg_output_v1_send_done(resource);
5794 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005795 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005796}
5797
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005798/** Signal that a pending output is taken into use.
5799 *
5800 * Removes the output from the pending list and adds it to the compositor's
5801 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005802 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005803 * The output gets an internal ID assigned, and the wl_output global is
5804 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005805 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005806 * \param compositor The compositor instance.
5807 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005808 *
5809 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005810 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005811 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005812static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005813weston_compositor_add_output(struct weston_compositor *compositor,
5814 struct weston_output *output)
5815{
Armin Krezoviće5403842016-08-05 15:28:29 +02005816 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005817 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005818
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005819 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005820
5821 /* Verify we haven't reached the limit of 32 available output IDs */
5822 assert(ffs(~compositor->output_id_pool) > 0);
5823
5824 /* Invert the output id pool and look for the lowest numbered
5825 * switch (the least significant bit). Take that bit's position
5826 * as our ID, and mark it used in the compositor's output_id_pool.
5827 */
5828 output->id = ffs(~compositor->output_id_pool) - 1;
5829 compositor->output_id_pool |= 1u << output->id;
5830
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005831 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005832 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005833 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005834
Pekka Paalanendcac3512017-12-08 14:13:34 +02005835 wl_list_for_each(head, &output->head_list, output_link)
5836 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005837
Giulio Camuffob1147152015-05-06 21:41:57 +03005838 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005839
5840 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5841 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005842}
5843
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005844/** Transform device coordinates into global coordinates
5845 *
Marius Vlada2dace22019-06-12 16:05:44 +03005846 * \param output the weston_output object
5847 * \param[in] device_x X coordinate in device units.
5848 * \param[in] device_y Y coordinate in device units.
5849 * \param[out] x X coordinate in the global space.
5850 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005851 *
Marius Vlada2dace22019-06-12 16:05:44 +03005852 * Transforms coordinates from the device coordinate space (physical pixel
5853 * units) to the global coordinate space (logical pixel units). This takes
5854 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005855 *
Marius Vlad55d87362019-06-11 01:15:35 +03005856 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005857 * \internal
5858 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005859WL_EXPORT void
5860weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005861 double device_x, double device_y,
5862 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005863{
Derek Foreman0f679412014-10-02 13:41:17 -05005864 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005865 device_x,
5866 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005867 0.0,
5868 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005869
Derek Foreman67a18b92015-03-24 11:36:14 -05005870 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005871
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005872 *x = p.f[0] / p.f[3];
5873 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005874}
5875
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005876/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005877 *
5878 * \param output The weston_output object that is being removed.
5879 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005880 * The following happens:
5881 *
5882 * - The output assignments of all views in the current scenegraph are
5883 * recomputed.
5884 *
5885 * - Presentation feedback is discarded.
5886 *
5887 * - Compositor is notified that outputs were changed and
5888 * applies the necessary changes to re-layout outputs.
5889 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005890 * - The output is put back in the pending outputs list.
5891 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005892 * - Signal is emitted to notify all users of the weston_output
5893 * object that the output is being destroyed.
5894 *
5895 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005896 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005897 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005898 * - The output's internal ID is released.
5899 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005900 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005901 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005902 */
5903static void
5904weston_compositor_remove_output(struct weston_output *output)
5905{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005906 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005907 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005908 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005909
5910 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005911 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005912
Pekka Paalanenbccda712017-03-29 16:16:04 +03005913 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005914 if (view->output_mask & (1u << output->id))
5915 weston_view_assign_output(view);
5916 }
5917
5918 weston_presentation_feedback_discard_list(&output->feedback_list);
5919
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005920 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005921
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005922 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005923 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005924 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005925
Pekka Paalanenbccda712017-03-29 16:16:04 +03005926 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005927 wl_signal_emit(&output->destroy_signal, output);
5928
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005929 wl_list_for_each(head, &output->head_list, output_link)
5930 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005931
5932 compositor->output_id_pool &= ~(1u << output->id);
5933 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005934}
5935
5936/** Sets the output scale for a given output.
5937 *
5938 * \param output The weston_output object that the scale is set for.
5939 * \param scale Scale factor for the given output.
5940 *
5941 * It only supports setting scale for an output that
5942 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005943 *
Marius Vlad55d87362019-06-11 01:15:35 +03005944 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005945 */
5946WL_EXPORT void
5947weston_output_set_scale(struct weston_output *output,
5948 int32_t scale)
5949{
5950 /* We can only set scale on a disabled output */
5951 assert(!output->enabled);
5952
5953 /* We only want to set scale once */
5954 assert(!output->scale);
5955
5956 output->scale = scale;
5957}
5958
5959/** Sets the output transform for a given output.
5960 *
5961 * \param output The weston_output object that the transform is set for.
5962 * \param transform Transform value for the given output.
5963 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005964 * Refer to wl_output::transform section located at
5965 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5966 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005967 *
Marius Vlad55d87362019-06-11 01:15:35 +03005968 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005969 */
5970WL_EXPORT void
5971weston_output_set_transform(struct weston_output *output,
5972 uint32_t transform)
5973{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005974 struct weston_pointer_motion_event ev;
5975 struct wl_resource *resource;
5976 struct weston_seat *seat;
5977 pixman_region32_t old_region;
5978 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005979 struct weston_head *head;
5980 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005981
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005982 if (!output->enabled && output->transform == UINT32_MAX) {
5983 output->transform = transform;
5984 return;
5985 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005986
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005987 weston_output_transform_scale_init(output, transform, output->scale);
5988
5989 pixman_region32_init(&old_region);
5990 pixman_region32_copy(&old_region, &output->region);
5991
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005992 weston_output_init_geometry(output, output->x, output->y);
5993
5994 output->dirty = 1;
5995
5996 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005997 wl_list_for_each(head, &output->head_list, output_link) {
5998 wl_resource_for_each(resource, &head->resource_list) {
5999 wl_output_send_geometry(resource,
6000 output->x,
6001 output->y,
6002 head->mm_width,
6003 head->mm_height,
6004 head->subpixel,
6005 head->make,
6006 head->model,
6007 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006008
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006009 ver = wl_resource_get_version(resource);
6010 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6011 wl_output_send_done(resource);
6012 }
Roman Gilge97391c2019-03-29 13:01:06 +01006013 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6014 zxdg_output_v1_send_logical_position(resource,
6015 output->x,
6016 output->y);
6017 zxdg_output_v1_send_logical_size(resource,
6018 output->width,
6019 output->height);
6020 zxdg_output_v1_send_done(resource);
6021 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006022 }
6023
6024 /* we must ensure that pointers are inside output, otherwise they disappear */
6025 mid_x = output->x + output->width / 2;
6026 mid_y = output->y + output->height / 2;
6027
6028 ev.mask = WESTON_POINTER_MOTION_ABS;
6029 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6030 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6031
6032 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6033 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6034
6035 if (pointer && pixman_region32_contains_point(&old_region,
6036 wl_fixed_to_int(pointer->x),
6037 wl_fixed_to_int(pointer->y),
6038 NULL))
6039 weston_pointer_move(pointer, &ev);
6040 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006041}
6042
6043/** Initializes a weston_output object with enough data so
6044 ** an output can be configured.
6045 *
6046 * \param output The weston_output object to initialize
6047 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006048 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006049 *
6050 * Sets initial values for fields that are expected to be
6051 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006052 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006053 * The name is used in logs, and can be used by compositors as a configuration
6054 * identifier.
6055 *
Marius Vlad55d87362019-06-11 01:15:35 +03006056 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006057 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006058 */
6059WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006060weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006061 struct weston_compositor *compositor,
6062 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006063{
6064 output->compositor = compositor;
6065 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006066 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006067 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006068 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006069 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306070 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306071 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006072
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006073 wl_list_init(&output->head_list);
6074
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006075 /* Add some (in)sane defaults which can be used
6076 * for checking if an output was properly configured
6077 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006078 output->scale = 0;
6079 /* Can't use -1 on uint32_t and 0 is valid enum value */
6080 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006081
6082 pixman_region32_init(&output->previous_damage);
6083 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006084 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006085}
6086
6087/** Adds weston_output object to pending output list.
6088 *
6089 * \param output The weston_output object to add
6090 * \param compositor The compositor instance.
6091 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006092 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006093 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006094 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006095 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006096 */
6097WL_EXPORT void
6098weston_compositor_add_pending_output(struct weston_output *output,
6099 struct weston_compositor *compositor)
6100{
Pekka Paalanene952a012017-03-29 17:14:00 +03006101 assert(output->disable);
6102 assert(output->enable);
6103
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006104 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006105 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006106}
6107
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006108/** Create a string with the attached heads' names.
6109 *
6110 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006111 *
6112 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006113 */
6114static char *
6115weston_output_create_heads_string(struct weston_output *output)
6116{
6117 FILE *fp;
6118 char *str = NULL;
6119 size_t size = 0;
6120 struct weston_head *head;
6121 const char *sep = "";
6122
6123 fp = open_memstream(&str, &size);
6124 if (!fp)
6125 return NULL;
6126
6127 wl_list_for_each(head, &output->head_list, output_link) {
6128 fprintf(fp, "%s%s", sep, head->name);
6129 sep = ", ";
6130 }
6131 fclose(fp);
6132
6133 return str;
6134}
6135
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006136/** Constructs a weston_output object that can be used by the compositor.
6137 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006138 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006139 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006140 *
6141 * Output coordinates are calculated and each new output is by default
6142 * assigned to the right of previous one.
6143 *
6144 * Sets up the transformation, zoom, and geometry of the output using
6145 * the properties that need to be configured by the compositor.
6146 *
6147 * Establishes a repaint timer for the output with the relevant display
6148 * object's event loop. See output_repaint_timer_handler().
6149 *
6150 * The output is assigned an ID. Weston can support up to 32 distinct
6151 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6152 * is referred to and used to find the first available ID number, and
6153 * then this ID is marked as used in output_id_pool.
6154 *
6155 * The output is also assigned a Wayland global with the wl_output
6156 * external interface.
6157 *
6158 * Backend specific function is called to set up the output output.
6159 *
6160 * Output is added to the compositor's output list
6161 *
6162 * If the backend specific function fails, the weston_output object
6163 * is returned to a state it was before calling this function and
6164 * is added to the compositor's pending_output_list in case it needs
6165 * to be reconfigured or just so it can be destroyed at shutdown.
6166 *
6167 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006168 *
6169 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006170 */
6171WL_EXPORT int
6172weston_output_enable(struct weston_output *output)
6173{
Armin Krezović782f5df2016-09-30 14:11:11 +02006174 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006175 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006176 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006177 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006178 int x = 0, y = 0;
6179
Pekka Paalanencc201e42017-03-30 15:11:25 +03006180 if (output->enabled) {
6181 weston_log("Error: attempt to enable an enabled output '%s'\n",
6182 output->name);
6183 return -1;
6184 }
6185
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006186 if (wl_list_empty(&output->head_list)) {
6187 weston_log("Error: cannot enable output '%s' without heads.\n",
6188 output->name);
6189 return -1;
6190 }
6191
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006192 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6193 weston_log("Error: no video mode for output '%s'.\n",
6194 output->name);
6195 return -1;
6196 }
6197
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006198 wl_list_for_each(head, &output->head_list, output_link) {
6199 assert(head->make);
6200 assert(head->model);
6201 }
6202
Armin Krezović782f5df2016-09-30 14:11:11 +02006203 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006204 struct weston_output, link);
6205
Armin Krezović782f5df2016-09-30 14:11:11 +02006206 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006207 x = iterator->x + iterator->width;
6208
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006209 /* Make sure the scale is set up */
6210 assert(output->scale);
6211
6212 /* Make sure we have a transform set */
6213 assert(output->transform != UINT32_MAX);
6214
Armin Krezović782f5df2016-09-30 14:11:11 +02006215 output->x = x;
6216 output->y = y;
6217 output->dirty = 1;
6218 output->original_scale = output->scale;
6219
6220 weston_output_transform_scale_init(output, output->transform, output->scale);
6221 weston_output_init_zoom(output);
6222
6223 weston_output_init_geometry(output, x, y);
6224 weston_output_damage(output);
6225
6226 wl_signal_init(&output->frame_signal);
6227 wl_signal_init(&output->destroy_signal);
6228 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006229 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006230
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006231 /* Enable the output (set up the crtc or create a
6232 * window representing the output, set up the
6233 * renderer, etc)
6234 */
6235 if (output->enable(output) < 0) {
6236 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006237 return -1;
6238 }
6239
6240 weston_compositor_add_output(output->compositor, output);
6241
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006242 head_names = weston_output_create_heads_string(output);
6243 weston_log("Output '%s' enabled with head(s) %s\n",
6244 output->name, head_names);
6245 free(head_names);
6246
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006247 return 0;
6248}
6249
6250/** Converts a weston_output object to a pending output state, so it
6251 ** can be configured again or destroyed.
6252 *
6253 * \param output The weston_output object that needs to be disabled.
6254 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006255 * Calls a backend specific function to disable an output, in case
6256 * such function exists.
6257 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006258 * The backend specific disable function may choose to postpone the disabling
6259 * by returning a negative value, in which case this function returns early.
6260 * In that case the backend will guarantee the output will be disabled soon
6261 * by the backend calling this function again. One must not attempt to re-enable
6262 * the output until that happens.
6263 *
6264 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006265 * from weston's output_list (see weston_compositor_remove_output())
6266 * and is returned to a state it was before weston_output_enable()
6267 * was ran (see weston_output_enable_undo()).
6268 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006269 * See weston_output_init() for more information on the
6270 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006271 *
6272 * If the output has never been enabled yet, this function can still be
6273 * called to ensure that the output is actually turned off rather than left
6274 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006275 *
6276 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006277 */
6278WL_EXPORT void
6279weston_output_disable(struct weston_output *output)
6280{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006281 /* Should we rename this? */
6282 output->destroying = 1;
6283
Pekka Paalanenc65df642017-03-29 15:45:46 +03006284 /* Disable is called unconditionally also for not-enabled outputs,
6285 * because at compositor start-up, if there is an output that is
6286 * already on but the compositor wants to turn it off, we have to
6287 * forward the turn-off to the backend so it knows to do it.
6288 * The backend cannot initially turn off everything, because it
6289 * would cause unnecessary mode-sets for all outputs the compositor
6290 * wants to be on.
6291 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006292 if (output->disable(output) < 0)
6293 return;
6294
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006295 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006296 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006297
6298 output->destroying = 0;
6299}
6300
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006301/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006302 *
6303 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006304 *
6305 * If there are new or changed heads, calls the heads_changed hook and
6306 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006307 *
6308 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006309 */
6310WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006311weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006312{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006313 if (compositor->heads_changed_source) {
6314 wl_event_source_remove(compositor->heads_changed_source);
6315 weston_compositor_call_heads_changed(compositor);
6316 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006317}
6318
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006319/** Add destroy callback for an output
6320 *
6321 * \param output The output to watch.
6322 * \param listener The listener to add. The \c notify member must be set.
6323 *
6324 * The listener callback will be called when user destroys an output. This
6325 * may be delayed by a backend in some cases. The main purpose of the
6326 * listener is to allow hooking up custom data to the output. The custom data
6327 * can be fetched via weston_output_get_destroy_listener() followed by
6328 * container_of().
6329 *
6330 * The \c data argument to the notify callback is the weston_output being
6331 * destroyed.
6332 *
6333 * @note This is for the final destruction of an output, not when it gets
6334 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006335 *
6336 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006337 */
6338WL_EXPORT void
6339weston_output_add_destroy_listener(struct weston_output *output,
6340 struct wl_listener *listener)
6341{
6342 wl_signal_add(&output->user_destroy_signal, listener);
6343}
6344
6345/** Look up destroy listener for an output
6346 *
6347 * \param output The output to query.
6348 * \param notify The notify function used used for the added destroy listener.
6349 * \return The listener, or NULL if not found.
6350 *
6351 * This looks up the previously added destroy listener struct based on the
6352 * notify function it has. The listener can be used to access user data
6353 * through \c container_of().
6354 *
6355 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006356 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006357 */
6358WL_EXPORT struct wl_listener *
6359weston_output_get_destroy_listener(struct weston_output *output,
6360 wl_notify_func_t notify)
6361{
6362 return wl_signal_get(&output->user_destroy_signal, notify);
6363}
6364
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006365/** Uninitialize an output
6366 *
6367 * Removes the output from the list of enabled outputs if necessary, but
6368 * does not call the backend's output disable function. The output will no
6369 * longer be in the list of pending outputs either.
6370 *
6371 * All fields of weston_output become uninitialized, i.e. should not be used
6372 * anymore. The caller can free the memory after this.
6373 *
Marius Vlad55d87362019-06-11 01:15:35 +03006374 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006375 * \internal
6376 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006377WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006378weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006379{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006380 struct weston_head *head, *tmp;
6381
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006382 output->destroying = 1;
6383
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006384 wl_signal_emit(&output->user_destroy_signal, output);
6385
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006386 if (output->idle_repaint_source)
6387 wl_event_source_remove(output->idle_repaint_source);
6388
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006389 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006390 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006391
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006392 pixman_region32_fini(&output->region);
6393 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006394 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006395
6396 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6397 weston_head_detach(head);
6398
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006399 free(output->name);
6400}
6401
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006402/** Find an output by its given name
6403 *
6404 * \param compositor The compositor to search in.
6405 * \param name The output name to search for.
6406 * \return An existing output with the given name, or NULL if not found.
6407 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006408 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006409 */
6410WL_EXPORT struct weston_output *
6411weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6412 const char *name)
6413{
6414 struct weston_output *output;
6415
6416 wl_list_for_each(output, &compositor->output_list, link)
6417 if (strcmp(output->name, name) == 0)
6418 return output;
6419
6420 wl_list_for_each(output, &compositor->pending_output_list, link)
6421 if (strcmp(output->name, name) == 0)
6422 return output;
6423
6424 return NULL;
6425}
6426
6427/** Create a named output
6428 *
6429 * \param compositor The compositor.
6430 * \param name The name for the output.
6431 * \return A new \c weston_output, or NULL on failure.
6432 *
6433 * This creates a new weston_output that starts with no heads attached.
6434 *
6435 * An output must be configured and it must have at least one head before
6436 * it can be enabled.
6437 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006438 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006439 */
6440WL_EXPORT struct weston_output *
6441weston_compositor_create_output(struct weston_compositor *compositor,
6442 const char *name)
6443{
6444 assert(compositor->backend->create_output);
6445
6446 if (weston_compositor_find_output_by_name(compositor, name)) {
6447 weston_log("Warning: attempted to create an output with a "
6448 "duplicate name '%s'.\n", name);
6449 return NULL;
6450 }
6451
6452 return compositor->backend->create_output(compositor, name);
6453}
6454
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006455/** Create an output for an unused head
6456 *
6457 * \param compositor The compositor.
6458 * \param head The head to attach to the output.
6459 * \return A new \c weston_output, or NULL on failure.
6460 *
6461 * This creates a new weston_output that starts with the given head attached.
6462 * The output inherits the name of the head. The head must not be already
6463 * attached to another output.
6464 *
6465 * An output must be configured before it can be enabled.
6466 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006467 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006468 */
6469WL_EXPORT struct weston_output *
6470weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6471 struct weston_head *head)
6472{
6473 struct weston_output *output;
6474
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006475 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006476 if (!output)
6477 return NULL;
6478
6479 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006480 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006481 return NULL;
6482 }
6483
6484 return output;
6485}
6486
6487/** Destroy an output
6488 *
6489 * \param output The output to destroy.
6490 *
6491 * The heads attached to the given output are detached and become unused again.
6492 *
6493 * It is not necessary to explicitly destroy all outputs at compositor exit.
6494 * weston_compositor_destroy() will automatically destroy any remaining
6495 * outputs.
6496 *
Marius Vlad55d87362019-06-11 01:15:35 +03006497 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006498 */
6499WL_EXPORT void
6500weston_output_destroy(struct weston_output *output)
6501{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006502 output->destroy(output);
6503}
6504
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006505/** When you need a head...
6506 *
6507 * This function is a hack, used until all code has been converted to become
6508 * multi-head aware.
6509 *
6510 * \param output The weston_output whose head to get.
6511 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006512 *
6513 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006514 */
6515WL_EXPORT struct weston_head *
6516weston_output_get_first_head(struct weston_output *output)
6517{
6518 if (wl_list_empty(&output->head_list))
6519 return NULL;
6520
6521 return container_of(output->head_list.next,
6522 struct weston_head, output_link);
6523}
6524
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306525/** Allow/Disallow content-protection support for an output
6526 *
6527 * This function sets the allow_protection member for an output. Setting of
6528 * this field will allow the compositor to attempt content-protection for this
6529 * output, for a backend that supports the content-protection protocol.
6530 *
6531 * \param output The weston_output for whom the content-protection is to be
6532 * allowed.
6533 * \param allow_protection The bool value which is to be set.
6534 */
6535WL_EXPORT void
6536weston_output_allow_protection(struct weston_output *output,
6537 bool allow_protection)
6538{
6539 output->allow_protection = allow_protection;
6540}
6541
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006542static void
Roman Gilge97391c2019-03-29 13:01:06 +01006543xdg_output_unlist(struct wl_resource *resource)
6544{
6545 wl_list_remove(wl_resource_get_link(resource));
6546}
6547
6548static void
6549xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6550{
6551 wl_resource_destroy(resource);
6552}
6553
6554static const struct zxdg_output_v1_interface xdg_output_interface = {
6555 xdg_output_destroy
6556};
6557
6558static void
6559xdg_output_manager_destroy(struct wl_client *client,
6560 struct wl_resource *resource)
6561{
6562 wl_resource_destroy(resource);
6563}
6564
6565static void
6566xdg_output_manager_get_xdg_output(struct wl_client *client,
6567 struct wl_resource *manager,
6568 uint32_t id,
6569 struct wl_resource *output_resource)
6570{
6571 int version = wl_resource_get_version(manager);
6572 struct weston_head *head = wl_resource_get_user_data(output_resource);
6573 struct weston_output *output = head->output;
6574 struct wl_resource *resource;
6575
6576 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6577 version, id);
6578 if (resource == NULL) {
6579 wl_client_post_no_memory(client);
6580 return;
6581 }
6582
6583 wl_list_insert(&head->xdg_output_resource_list,
6584 wl_resource_get_link(resource));
6585
6586 wl_resource_set_implementation(resource, &xdg_output_interface,
6587 NULL, xdg_output_unlist);
6588
6589 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6590 zxdg_output_v1_send_logical_size(resource,
6591 output->width,
6592 output->height);
6593 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6594 zxdg_output_v1_send_name(resource, head->name);
6595
6596 zxdg_output_v1_send_done(resource);
6597}
6598
6599static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6600 xdg_output_manager_destroy,
6601 xdg_output_manager_get_xdg_output
6602};
6603
6604static void
6605bind_xdg_output_manager(struct wl_client *client,
6606 void *data, uint32_t version, uint32_t id)
6607{
6608 struct wl_resource *resource;
6609
6610 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6611 version, id);
6612 if (resource == NULL) {
6613 wl_client_post_no_memory(client);
6614 return;
6615 }
6616
6617 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6618 NULL, NULL);
6619}
6620
6621static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006622destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006623{
Jonny Lamb74130762013-11-26 18:19:46 +01006624 struct weston_surface *surface =
6625 wl_resource_get_user_data(resource);
6626
Pekka Paalanen4826f872016-04-22 14:14:38 +03006627 if (!surface)
6628 return;
6629
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006630 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006631 surface->pending.buffer_viewport.buffer.src_width =
6632 wl_fixed_from_int(-1);
6633 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006634 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006635}
6636
6637static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006638viewport_destroy(struct wl_client *client,
6639 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006640{
6641 wl_resource_destroy(resource);
6642}
6643
6644static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006645viewport_set_source(struct wl_client *client,
6646 struct wl_resource *resource,
6647 wl_fixed_t src_x,
6648 wl_fixed_t src_y,
6649 wl_fixed_t src_width,
6650 wl_fixed_t src_height)
6651{
6652 struct weston_surface *surface =
6653 wl_resource_get_user_data(resource);
6654
Pekka Paalanen4826f872016-04-22 14:14:38 +03006655 if (!surface) {
6656 wl_resource_post_error(resource,
6657 WP_VIEWPORT_ERROR_NO_SURFACE,
6658 "wl_surface for this viewport is no longer exists");
6659 return;
6660 }
6661
6662 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006663 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006664
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006665 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006666 src_height == wl_fixed_from_int(-1) &&
6667 src_x == wl_fixed_from_int(-1) &&
6668 src_y == wl_fixed_from_int(-1)) {
6669 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006670 surface->pending.buffer_viewport.buffer.src_width =
6671 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006672 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006673 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006674 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006675
Pekka Paalanen201769a2016-04-26 14:42:11 +03006676 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006677 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006678 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006679 "wl_surface@%d viewport source "
6680 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6681 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006682 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006683 wl_fixed_to_double(src_height),
6684 wl_fixed_to_double(src_x),
6685 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006686 return;
6687 }
6688
6689 surface->pending.buffer_viewport.buffer.src_x = src_x;
6690 surface->pending.buffer_viewport.buffer.src_y = src_y;
6691 surface->pending.buffer_viewport.buffer.src_width = src_width;
6692 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006693 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006694}
6695
6696static void
6697viewport_set_destination(struct wl_client *client,
6698 struct wl_resource *resource,
6699 int32_t dst_width,
6700 int32_t dst_height)
6701{
6702 struct weston_surface *surface =
6703 wl_resource_get_user_data(resource);
6704
Pekka Paalanen4826f872016-04-22 14:14:38 +03006705 if (!surface) {
6706 wl_resource_post_error(resource,
6707 WP_VIEWPORT_ERROR_NO_SURFACE,
6708 "wl_surface for this viewport no longer exists");
6709 return;
6710 }
6711
6712 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006713
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006714 if (dst_width == -1 && dst_height == -1) {
6715 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006716 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006717 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006718 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006719 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006720
6721 if (dst_width <= 0 || dst_height <= 0) {
6722 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006723 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006724 "destination size must be positive (%dx%d)",
6725 dst_width, dst_height);
6726 return;
6727 }
6728
6729 surface->pending.buffer_viewport.surface.width = dst_width;
6730 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006731 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006732}
6733
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006734static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006735 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006736 viewport_set_source,
6737 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006738};
6739
6740static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006741viewporter_destroy(struct wl_client *client,
6742 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006743{
6744 wl_resource_destroy(resource);
6745}
6746
6747static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006748viewporter_get_viewport(struct wl_client *client,
6749 struct wl_resource *viewporter,
6750 uint32_t id,
6751 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006752{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006753 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006754 struct weston_surface *surface =
6755 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006756 struct wl_resource *resource;
6757
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006758 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006759 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006760 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006761 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006762 return;
6763 }
6764
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006765 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006766 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006767 if (resource == NULL) {
6768 wl_client_post_no_memory(client);
6769 return;
6770 }
6771
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006772 wl_resource_set_implementation(resource, &viewport_interface,
6773 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006774
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006775 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006776}
6777
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006778static const struct wp_viewporter_interface viewporter_interface = {
6779 viewporter_destroy,
6780 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006781};
6782
6783static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006784bind_viewporter(struct wl_client *client,
6785 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006786{
6787 struct wl_resource *resource;
6788
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006789 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006790 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006791 if (resource == NULL) {
6792 wl_client_post_no_memory(client);
6793 return;
6794 }
6795
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006796 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006797 NULL, NULL);
6798}
6799
6800static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006801destroy_presentation_feedback(struct wl_resource *feedback_resource)
6802{
6803 struct weston_presentation_feedback *feedback;
6804
6805 feedback = wl_resource_get_user_data(feedback_resource);
6806
6807 wl_list_remove(&feedback->link);
6808 free(feedback);
6809}
6810
6811static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006812presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6813{
6814 wl_resource_destroy(resource);
6815}
6816
6817static void
6818presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006819 struct wl_resource *presentation_resource,
6820 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006821 uint32_t callback)
6822{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006823 struct weston_surface *surface;
6824 struct weston_presentation_feedback *feedback;
6825
6826 surface = wl_resource_get_user_data(surface_resource);
6827
Bryce Harringtonde16d892014-11-20 22:21:57 -08006828 feedback = zalloc(sizeof *feedback);
6829 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006830 goto err_calloc;
6831
6832 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006833 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006834 1, callback);
6835 if (!feedback->resource)
6836 goto err_create;
6837
6838 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6839 destroy_presentation_feedback);
6840
6841 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6842
6843 return;
6844
6845err_create:
6846 free(feedback);
6847
6848err_calloc:
6849 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006850}
6851
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006852static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006853 presentation_destroy,
6854 presentation_feedback
6855};
6856
6857static void
6858bind_presentation(struct wl_client *client,
6859 void *data, uint32_t version, uint32_t id)
6860{
6861 struct weston_compositor *compositor = data;
6862 struct wl_resource *resource;
6863
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006864 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006865 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006866 if (resource == NULL) {
6867 wl_client_post_no_memory(client);
6868 return;
6869 }
6870
6871 wl_resource_set_implementation(resource, &presentation_implementation,
6872 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006873 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006874}
6875
6876static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006877compositor_bind(struct wl_client *client,
6878 void *data, uint32_t version, uint32_t id)
6879{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006880 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006881 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006882
Jason Ekstranda85118c2013-06-27 20:17:02 -05006883 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006884 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006885 if (resource == NULL) {
6886 wl_client_post_no_memory(client);
6887 return;
6888 }
6889
6890 wl_resource_set_implementation(resource, &compositor_interface,
6891 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006892}
6893
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006894WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006895weston_environment_get_fd(const char *env)
6896{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006897 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006898 int fd, flags;
6899
6900 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006901 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006902 return -1;
6903
6904 flags = fcntl(fd, F_GETFD);
6905 if (flags == -1)
6906 return -1;
6907
6908 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6909 unsetenv(env);
6910
6911 return fd;
6912}
6913
Pekka Paalanenb5026542014-11-12 15:09:24 +02006914static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006915timeline_key_binding_handler(struct weston_keyboard *keyboard,
6916 const struct timespec *time, uint32_t key,
6917 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006918{
6919 struct weston_compositor *compositor = data;
6920
6921 if (weston_timeline_enabled_)
6922 weston_timeline_close();
6923 else
6924 weston_timeline_open(compositor);
6925}
6926
Daniel Stonece62cb32018-07-20 09:46:24 +01006927static const char *
6928output_repaint_status_text(struct weston_output *output)
6929{
6930 switch (output->repaint_status) {
6931 case REPAINT_NOT_SCHEDULED:
6932 return "no repaint";
6933 case REPAINT_BEGIN_FROM_IDLE:
6934 return "start_repaint_loop scheduled";
6935 case REPAINT_SCHEDULED:
6936 return "repaint scheduled";
6937 case REPAINT_AWAITING_COMPLETION:
6938 return "awaiting completion";
6939 }
6940
6941 assert(!"output_repaint_status_text missing enum");
6942 return NULL;
6943}
6944
6945static void
6946debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6947{
6948 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6949 struct wl_shm_buffer *shm;
6950 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006951 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006952
6953 if (!buffer) {
6954 fprintf(fp, "\t\t[buffer not available]\n");
6955 return;
6956 }
6957
6958 shm = wl_shm_buffer_get(buffer->resource);
6959 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006960 uint32_t _format = wl_shm_buffer_get_format(shm);
6961 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006962 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006963 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6964 (unsigned long) _format,
6965 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006966 return;
6967 }
6968
6969 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6970 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006971 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006972 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006973 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6974 (unsigned long) dmabuf->attributes.format,
6975 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006976 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6977 (unsigned long long) dmabuf->attributes.modifier[0]);
6978 return;
6979 }
6980
Marius Vlad253ba9a2019-03-17 18:22:21 +02006981 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006982}
6983
6984static void
6985debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6986{
6987 struct weston_compositor *ec = view->surface->compositor;
6988 struct weston_output *output;
6989 char desc[512];
6990 pixman_box32_t *box;
6991 uint32_t surface_id = 0;
6992 pid_t pid = 0;
6993
6994 if (view->surface->resource) {
6995 struct wl_resource *resource = view->surface->resource;
6996 wl_client_get_credentials(wl_resource_get_client(resource),
6997 &pid, NULL, NULL);
6998 surface_id = wl_resource_get_id(view->surface->resource);
6999 }
7000
7001 if (!view->surface->get_label ||
7002 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7003 strcpy(desc, "[no description available]");
7004 }
7005 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7006 view_idx, view->surface->role_name, pid, surface_id,
7007 desc, view);
7008
7009 box = pixman_region32_extents(&view->transform.boundingbox);
7010 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7011 box->x1, box->y1, box->x2, box->y2);
7012 box = pixman_region32_extents(&view->transform.opaque);
7013
7014 if (pixman_region32_equal(&view->transform.opaque,
7015 &view->transform.boundingbox)) {
7016 fprintf(fp, "\t\t[fully opaque]\n");
7017 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7018 fprintf(fp, "\t\t[not opaque]\n");
7019 } else {
7020 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7021 box->x1, box->y1, box->x2, box->y2);
7022 }
7023
7024 if (view->alpha < 1.0)
7025 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7026
7027 if (view->output_mask != 0) {
7028 bool first_output = true;
7029 fprintf(fp, "\t\toutputs: ");
7030 wl_list_for_each(output, &ec->output_list, link) {
7031 if (!(view->output_mask & (1 << output->id)))
7032 continue;
7033 fprintf(fp, "%s%d (%s)%s",
7034 (first_output) ? "" : ", ",
7035 output->id, output->name,
7036 (view->output == output) ? " (primary)" : "");
7037 first_output = false;
7038 }
7039 } else {
7040 fprintf(fp, "\t\t[no outputs]");
7041 }
7042
7043 fprintf(fp, "\n");
7044
7045 debug_scene_view_print_buffer(fp, view);
7046}
7047
Marius Vlad433f4e72019-02-17 22:14:23 +02007048static void
7049debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007050 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007051{
7052 struct weston_subsurface *sub;
7053 struct weston_view *ev;
7054
7055 /*
7056 * print the view first, then we recursively go on printing
7057 * sub-surfaces. We bail out once no more sub-surfaces are available.
7058 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007059 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007060
7061 /* no more sub-surfaces */
7062 if (wl_list_empty(&view->surface->subsurface_list))
7063 return;
7064
7065 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7066 wl_list_for_each(ev, &sub->surface->views, surface_link) {
7067 /* do not print again the parent view */
7068 if (view == ev)
7069 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007070
7071 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007072 debug_scene_view_print_tree(ev, fp, view_idx);
7073 }
7074 }
7075}
7076
Daniel Stonece62cb32018-07-20 09:46:24 +01007077/**
7078 * Output information on how libweston is currently composing the scene
7079 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007080 *
7081 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007082 */
7083WL_EXPORT char *
7084weston_compositor_print_scene_graph(struct weston_compositor *ec)
7085{
7086 struct weston_output *output;
7087 struct weston_layer *layer;
7088 struct timespec now;
7089 int layer_idx = 0;
7090 FILE *fp;
7091 char *ret;
7092 size_t len;
7093 int err;
7094
7095 fp = open_memstream(&ret, &len);
7096 assert(fp);
7097
7098 weston_compositor_read_presentation_clock(ec, &now);
7099 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7100 now.tv_sec, now.tv_nsec);
7101
7102 wl_list_for_each(output, &ec->output_list, link) {
7103 struct weston_head *head;
7104 int head_idx = 0;
7105
7106 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7107 assert(output->enabled);
7108
7109 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7110 output->x, output->y,
7111 output->x + output->width,
7112 output->y + output->height);
7113 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7114 output->current_mode->width,
7115 output->current_mode->height,
7116 output->current_mode->refresh / 1000.0);
7117 fprintf(fp, "\tscale: %d\n", output->scale);
7118
7119 fprintf(fp, "\trepaint status: %s\n",
7120 output_repaint_status_text(output));
7121 if (output->repaint_status == REPAINT_SCHEDULED)
7122 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7123 output->next_repaint.tv_sec,
7124 output->next_repaint.tv_nsec);
7125
7126 wl_list_for_each(head, &output->head_list, output_link) {
7127 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7128 head_idx++, head->name,
7129 (head->connected) ? "" : "not ");
7130 }
7131 }
7132
7133 fprintf(fp, "\n");
7134
7135 wl_list_for_each(layer, &ec->layer_list, link) {
7136 struct weston_view *view;
7137 int view_idx = 0;
7138
7139 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7140 (unsigned long) layer->position);
7141
7142 if (!weston_layer_mask_is_infinite(layer)) {
7143 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7144 layer->mask.x1, layer->mask.y1,
7145 layer->mask.x2, layer->mask.y2);
7146 }
7147
Marius Vlada6acfa82019-03-17 18:10:09 +02007148 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7149 debug_scene_view_print_tree(view, fp, &view_idx);
7150 view_idx++;
7151 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007152
7153 if (wl_list_empty(&layer->view_list.link))
7154 fprintf(fp, "\t[no views]\n");
7155
7156 fprintf(fp, "\n");
7157 }
7158
7159 err = fclose(fp);
7160 assert(err == 0);
7161
7162 return ret;
7163}
7164
7165/**
7166 * Called when the 'scene-graph' debug scope is bound by a client. This
7167 * one-shot weston-debug scope prints the current scene graph when bound,
7168 * and then terminates the stream.
7169 */
7170static void
Marius Vladdad882a2019-07-17 15:43:53 +03007171debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007172{
7173 struct weston_compositor *ec = data;
7174 char *str = weston_compositor_print_scene_graph(ec);
7175
Marius Vladdad882a2019-07-17 15:43:53 +03007176 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007177 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007178 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007179}
7180
Giulio Camuffo459137b2014-10-11 23:56:24 +03007181/** Create the compositor.
7182 *
7183 * This functions creates and initializes a compositor instance.
7184 *
7185 * \param display The Wayland display to be used.
7186 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007187 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007188 * using the \ref weston_compositor_get_user_data function.
7189 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007190 *
7191 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007192 */
7193WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007194weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007195 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007196 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007197{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007198 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007199 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007200
Giulio Camuffo459137b2014-10-11 23:56:24 +03007201 ec = zalloc(sizeof *ec);
7202 if (!ec)
7203 return NULL;
7204
7205 ec->wl_display = display;
7206 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007207 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007208 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007209 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007210 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007211 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007212 wl_signal_init(&ec->idle_signal);
7213 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007214 wl_signal_init(&ec->show_input_panel_signal);
7215 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007216 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007217 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007218 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007219 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007220 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007221 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007222 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007223 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007224 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007225 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007226
Casey Dahlin58ba1372012-04-19 22:50:08 -04007227 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007228 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007229
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007230 ec->activate_serial = 1;
7231
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007232 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7233
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307234 ec->content_protection = NULL;
7235
Derek Foreman152254b2015-11-26 14:17:48 -06007236 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007237 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007238 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007239
Giulio Camuffo954f1832014-10-11 18:27:30 +03007240 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007241 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007242 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007243
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007244 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007245 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007246 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007247
Roman Gilge97391c2019-03-29 13:01:06 +01007248 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7249 ec, bind_xdg_output_manager))
7250 goto fail;
7251
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007252 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007253 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007254 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007255
Marius Vlad3d7d9782019-04-17 12:35:38 +03007256 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007257 goto fail;
7258
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007259 if (weston_input_init(ec) != 0)
7260 goto fail;
7261
Jason Ekstranda7af7042013-10-12 22:38:11 -05007262 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007263 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007264 wl_list_init(&ec->layer_list);
7265 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007266 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007267 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007268 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007269 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007270 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007271 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007272 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007273 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007274 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007275
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007276 wl_list_init(&ec->plugin_api_list);
7277
Xiong Zhang97116532013-10-23 13:58:31 +08007278 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007279 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007280
Giulio Camuffo459137b2014-10-11 23:56:24 +03007281 wl_data_device_manager_init(ec->wl_display);
7282
7283 wl_display_init_shm(ec->wl_display);
7284
7285 loop = wl_display_get_event_loop(ec->wl_display);
7286 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007287 ec->repaint_timer =
7288 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7289 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007290
Quentin Glidic82681572016-12-17 13:40:51 +01007291 weston_layer_init(&ec->fade_layer, ec);
7292 weston_layer_init(&ec->cursor_layer, ec);
7293
7294 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7295 weston_layer_set_position(&ec->cursor_layer,
7296 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007297
7298 weston_compositor_add_debug_binding(ec, KEY_T,
7299 timeline_key_binding_handler, ec);
7300
Daniel Stonece62cb32018-07-20 09:46:24 +01007301 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03007302 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
7303 "Scene graph details\n",
7304 debug_scene_graph_cb,
7305 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007306
Giulio Camuffo459137b2014-10-11 23:56:24 +03007307 return ec;
7308
7309fail:
7310 free(ec);
7311 return NULL;
7312}
7313
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007314/** weston_compositor_shutdown
7315 * \ingroup compositor
7316 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007317WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007318weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007319{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007320 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007321
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007322 wl_event_source_remove(ec->idle_source);
7323
Matt Roper361d2ad2011-08-29 13:52:23 -07007324 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007325 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007326 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007327
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007328 /* Destroy all pending outputs associated with this compositor */
7329 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7330 output->destroy(output);
7331
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007332 if (ec->renderer)
7333 ec->renderer->destroy(ec);
7334
Daniel Stone325fc2d2012-05-30 16:31:58 +01007335 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007336 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007337 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007338 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007339 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007340 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007341
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007342 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007343}
7344
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007345/** weston_compositor_exit_with_code
7346 * \ingroup compositor
7347 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007348WL_EXPORT void
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007349weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007350 int exit_code)
7351{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007352 if (compositor->exit_code == EXIT_SUCCESS)
7353 compositor->exit_code = exit_code;
7354
Giulio Camuffo459137b2014-10-11 23:56:24 +03007355 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007356}
7357
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007358/** weston_compositor_set_default_pointer_grab
7359 * \ingroup compositor
7360 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007361WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007362weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7363 const struct weston_pointer_grab_interface *interface)
7364{
7365 struct weston_seat *seat;
7366
7367 ec->default_pointer_grab = interface;
7368 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007369 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7370
7371 if (pointer)
7372 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007373 }
7374}
7375
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007376/** weston_compositor_set_presentation_clock
7377 * \ingroup compositor
7378 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007379WL_EXPORT int
7380weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7381 clockid_t clk_id)
7382{
7383 struct timespec ts;
7384
7385 if (clock_gettime(clk_id, &ts) < 0)
7386 return -1;
7387
7388 compositor->presentation_clock = clk_id;
7389
7390 return 0;
7391}
7392
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007393/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007394 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007395 *
7396 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007397 */
7398WL_EXPORT int
7399weston_compositor_set_presentation_clock_software(
7400 struct weston_compositor *compositor)
7401{
7402 /* In order of preference */
7403 static const clockid_t clocks[] = {
7404 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7405 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7406 CLOCK_MONOTONIC, /* no jumps, may crawl */
7407 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7408 CLOCK_REALTIME /* may jump and crawl */
7409 };
7410 unsigned i;
7411
7412 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7413 if (weston_compositor_set_presentation_clock(compositor,
7414 clocks[i]) == 0)
7415 return 0;
7416
7417 weston_log("Error: no suitable presentation clock available.\n");
7418
7419 return -1;
7420}
7421
Pekka Paalanen662f3842015-03-18 12:17:26 +02007422/** Read the current time from the Presentation clock
7423 *
7424 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007425 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007426 *
7427 * \note Reading the current time in user space is always imprecise to some
7428 * degree.
7429 *
7430 * This function is never meant to fail. If reading the clock does fail,
7431 * an error message is logged and a zero time is returned. Callers are not
7432 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007433 *
7434 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007435 */
7436WL_EXPORT void
7437weston_compositor_read_presentation_clock(
7438 const struct weston_compositor *compositor,
7439 struct timespec *ts)
7440{
7441 static bool warned;
7442 int ret;
7443
7444 ret = clock_gettime(compositor->presentation_clock, ts);
7445 if (ret < 0) {
7446 ts->tv_sec = 0;
7447 ts->tv_nsec = 0;
7448
7449 if (!warned)
7450 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007451 "the presentation clock %#x: '%s' (%d)\n",
7452 compositor->presentation_clock,
7453 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007454 warned = true;
7455 }
7456}
7457
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007458/** Import dmabuf buffer into current renderer
7459 *
7460 * \param compositor
7461 * \param buffer the dmabuf buffer to import
7462 * \return true on usable buffers, false otherwise
7463 *
7464 * This function tests that the linux_dmabuf_buffer is usable
7465 * for the current renderer. Returns false on unusable buffers. Usually
7466 * usability is tested by importing the dmabufs for composition.
7467 *
7468 * This hook is also used for detecting if the renderer supports
7469 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7470 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007471 *
7472 * \ingroup compositor
7473 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007474WL_EXPORT bool
7475weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7476 struct linux_dmabuf_buffer *buffer)
7477{
7478 struct weston_renderer *renderer;
7479
7480 renderer = compositor->renderer;
7481
7482 if (renderer->import_dmabuf == NULL)
7483 return false;
7484
7485 return renderer->import_dmabuf(compositor, buffer);
7486}
7487
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007488WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007489weston_version(int *major, int *minor, int *micro)
7490{
7491 *major = WESTON_VERSION_MAJOR;
7492 *minor = WESTON_VERSION_MINOR;
7493 *micro = WESTON_VERSION_MICRO;
7494}
7495
Daniel Stonee03c1112016-11-24 20:45:45 +00007496/**
7497 * Attempts to find a module path from the module map specified in the
7498 * environment. If found, writes the full path into the path variable.
7499 *
7500 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7501 * each entry is of the form "name=path" and entries are separated by
7502 * semicolons. Whitespace is significant.
7503 *
7504 * \param name The name to search for.
7505 * \param path Where the path is written to if found.
7506 * \param path_len Allocated bytes at \c path .
7507 * \returns The length of the string written to path on success, or 0 if the
7508 * module was not specified in the environment map or path_len was too small.
7509 */
7510WL_EXPORT size_t
7511weston_module_path_from_env(const char *name, char *path, size_t path_len)
7512{
7513 const char *mapping = getenv("WESTON_MODULE_MAP");
7514 const char *end;
7515 const int name_len = strlen(name);
7516
7517 if (!mapping)
7518 return 0;
7519
7520 end = mapping + strlen(mapping);
7521 while (mapping < end && *mapping) {
7522 const char *filename, *next;
7523
7524 /* early out: impossibly short string */
7525 if (end - mapping < name_len + 1)
7526 return 0;
7527
7528 filename = &mapping[name_len + 1];
7529 next = strchrnul(mapping, ';');
7530
7531 if (strncmp(mapping, name, name_len) == 0 &&
7532 mapping[name_len] == '=') {
7533 size_t file_len = next - filename; /* no trailing NUL */
7534 if (file_len >= path_len)
7535 return 0;
7536 strncpy(path, filename, file_len);
7537 path[file_len] = '\0';
7538 return file_len;
7539 }
7540
7541 mapping = next + 1;
7542 }
7543
7544 return 0;
7545}
7546
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007547WL_EXPORT void *
7548weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007549{
7550 char path[PATH_MAX];
7551 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007552 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007553
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007554 if (name == NULL)
7555 return NULL;
7556
Derek Foreman3f86e502015-06-08 11:46:54 -05007557 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007558 len = weston_module_path_from_env(name, path, sizeof path);
7559 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007560 len = snprintf(path, sizeof path, "%s/%s",
7561 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007562 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007563 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007564 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007565
Daniel Stonebeb97e52016-11-28 12:13:54 +00007566 /* snprintf returns the length of the string it would've written,
7567 * _excluding_ the NUL byte. So even being equal to the size of
7568 * our buffer is an error here. */
7569 if (len >= sizeof path)
7570 return NULL;
7571
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007572 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7573 if (module) {
7574 weston_log("Module '%s' already loaded\n", path);
7575 dlclose(module);
7576 return NULL;
7577 }
7578
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007579 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007580 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007581 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007582 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007583 return NULL;
7584 }
7585
7586 init = dlsym(module, entrypoint);
7587 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007588 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007589 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007590 return NULL;
7591 }
7592
7593 return init;
7594}
7595
Giulio Camuffo459137b2014-10-11 23:56:24 +03007596
Marius Vlad284d5342019-06-24 12:00:47 +03007597/** Tear down the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007598 *
Marius Vlad284d5342019-06-24 12:00:47 +03007599 * This function cleans up the compositor state. While the compositor state has
7600 * been cleaned do note that **only** weston_compositor_destroy() can be called
7601 * afterwards, in order to destroy the compositor instance.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007602 *
Marius Vlad284d5342019-06-24 12:00:47 +03007603 * @param compositor The compositor to be tear-down/cleaned.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007604 *
Marius Vlad284d5342019-06-24 12:00:47 +03007605 * @ingroup compositor
7606 * @sa weston_compositor_destroy
Giulio Camuffo459137b2014-10-11 23:56:24 +03007607 */
7608WL_EXPORT void
Marius Vlad284d5342019-06-24 12:00:47 +03007609weston_compositor_tear_down(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03007610{
7611 /* prevent further rendering while shutting down */
7612 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7613
7614 wl_signal_emit(&compositor->destroy_signal, compositor);
7615
7616 weston_compositor_xkb_destroy(compositor);
7617
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007618 if (compositor->backend)
7619 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007620
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007621 /* The backend is responsible for destroying the heads. */
7622 assert(wl_list_empty(&compositor->head_list));
7623
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007624 weston_plugin_api_destroy_list(compositor);
7625
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007626 if (compositor->heads_changed_source)
7627 wl_event_source_remove(compositor->heads_changed_source);
7628
Marius Vlad7e4db952019-04-17 13:47:06 +03007629 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007630 compositor->debug_scene = NULL;
Marius Vlad284d5342019-06-24 12:00:47 +03007631}
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007632
Marius Vlad284d5342019-06-24 12:00:47 +03007633/** Destroys the compositor.
7634 *
7635 * This function destroys the compositor. **Do not** call this before
7636 * calling weston_compositor_tear_down()
7637 *
7638 * @param compositor The compositor to be destroyed.
7639 * @ingroup compositor
7640 * @sa weston_compositor_tear_down()
7641 */
7642WL_EXPORT void
7643weston_compositor_destroy(struct weston_compositor *compositor)
7644{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007645 free(compositor);
7646}
7647
7648/** Instruct the compositor to exit.
7649 *
7650 * This functions does not directly destroy the compositor object, it merely
7651 * command it to start the tear down process. It is not guaranteed that the
7652 * tear down will happen immediately.
7653 *
7654 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007655 *
7656 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007657 */
7658WL_EXPORT void
7659weston_compositor_exit(struct weston_compositor *compositor)
7660{
7661 compositor->exit(compositor);
7662}
7663
7664/** Return the user data stored in the compositor.
7665 *
7666 * This function returns the user data pointer set with user_data parameter
7667 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007668 *
7669 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007670 */
7671WL_EXPORT void *
7672weston_compositor_get_user_data(struct weston_compositor *compositor)
7673{
7674 return compositor->user_data;
7675}
Pekka Paalanendd186732016-06-03 14:49:54 +03007676
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007677static const char * const backend_map[] = {
7678 [WESTON_BACKEND_DRM] = "drm-backend.so",
7679 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7680 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7681 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7682 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7683 [WESTON_BACKEND_X11] = "x11-backend.so",
7684};
7685
Pekka Paalanendd186732016-06-03 14:49:54 +03007686/** Load a backend into a weston_compositor
7687 *
7688 * A backend must be loaded to make a weston_compositor work. A backend
7689 * provides input and output capabilities, and determines the renderer to use.
7690 *
7691 * \param compositor A compositor that has not had a backend loaded yet.
7692 * \param backend Name of the backend file.
7693 * \param config_base A pointer to a backend-specific configuration
7694 * structure's 'base' member.
7695 *
7696 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007697 *
7698 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007699 */
7700WL_EXPORT int
7701weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007702 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007703 struct weston_backend_config *config_base)
7704{
7705 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007706 struct weston_backend_config *config_base);
7707
Pekka Paalanend7e35112017-08-29 17:04:12 +03007708 if (compositor->backend) {
7709 weston_log("Error: attempt to load a backend when one is already loaded\n");
7710 return -1;
7711 }
7712
Quentin Glidic887c0182016-07-10 11:00:53 +02007713 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007714 return -1;
7715
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007716 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007717 if (!backend_init)
7718 return -1;
7719
Pekka Paalanend7e35112017-08-29 17:04:12 +03007720 if (backend_init(compositor, config_base) < 0) {
7721 compositor->backend = NULL;
7722 return -1;
7723 }
7724
7725 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007726}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007727
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007728/** weston_compositor_load_xwayland
7729 * \ingroup compositor
7730 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007731WL_EXPORT int
7732weston_compositor_load_xwayland(struct weston_compositor *compositor)
7733{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007734 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007735
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007736 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007737 if (!module_init)
7738 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007739 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007740 return -1;
7741 return 0;
7742}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007743
7744/** Resolve an internal compositor error by disconnecting the client.
7745 *
7746 * This function is used in cases when the wl_buffer turns out
7747 * unusable and there is no fallback path.
7748 *
7749 * It is possible the fault is caused by a compositor bug, the underlying
7750 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7751 * In any case, the options are to either composite garbage or nothing,
7752 * or disconnect the client. This is a helper function for the latter.
7753 *
7754 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7755 *
7756 * \param buffer The weston buffer that is unusable.
7757 * \param msg A custom error message attached to the protocol error.
7758 */
7759WL_EXPORT void
7760weston_buffer_send_server_error(struct weston_buffer *buffer,
7761 const char *msg)
7762{
7763 struct wl_client *client;
7764 struct wl_resource *display_resource;
7765 uint32_t id;
7766
7767 assert(buffer->resource);
7768 id = wl_resource_get_id(buffer->resource);
7769 client = wl_resource_get_client(buffer->resource);
7770 display_resource = wl_client_get_object(client, 1);
7771
7772 assert(display_resource);
7773 wl_resource_post_error(display_resource,
7774 WL_DISPLAY_ERROR_INVALID_OBJECT,
7775 "server error with "
7776 "wl_buffer@%u: %s", id, msg);
7777}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307778
7779WL_EXPORT void
7780weston_output_disable_planes_incr(struct weston_output *output)
7781{
7782 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05307783 /*
7784 * If disable_planes changes from 0 to non-zero, it means some type of
7785 * recording of content has started, and therefore protection level of
7786 * the protected surfaces must be updated to avoid the recording of
7787 * the protected content.
7788 */
7789 if (output->disable_planes == 1)
7790 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307791}
7792
7793WL_EXPORT void
7794weston_output_disable_planes_decr(struct weston_output *output)
7795{
7796 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05307797 /*
7798 * If disable_planes changes from non-zero to 0, it means no content
7799 * recording is going on any more, and the protected and surfaces can be
7800 * shown without any apprehensions about content being recorded.
7801 */
7802 if (output->disable_planes == 0)
7803 weston_schedule_surface_protection_update(output->compositor);
7804
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307805}