blob: 794253f672ac51a0eaa289022cc575412a0f9a38 [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
Marius Vlad5f6bee42019-09-11 16:41:04 +03001855/** Check if the view has a valid buffer available
1856 *
1857 * @param ev The view to check if it has a valid buffer.
1858 *
1859 * Returns true if the view has a valid buffer or false otherwise.
1860 */
1861WL_EXPORT bool
1862weston_view_has_valid_buffer(struct weston_view *ev)
1863{
1864 return ev->surface->buffer_ref.buffer != NULL;
1865}
1866
Armin Krezović0da12b82016-06-30 06:04:33 +02001867/* Check if a surface has a view assigned to it
1868 *
1869 * The indicator is set manually when mapping
1870 * a surface and creating a view for it.
1871 *
1872 * This needs to go. See the thread starting at:
1873 *
1874 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1875 *
1876 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001877WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001878weston_surface_is_mapped(struct weston_surface *surface)
1879{
Armin Krezović0da12b82016-06-30 06:04:33 +02001880 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001881}
1882
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001883static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001884surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001885{
1886 struct weston_view *view;
1887
1888 if (surface->width == width && surface->height == height)
1889 return;
1890
1891 surface->width = width;
1892 surface->height = height;
1893
1894 wl_list_for_each(view, &surface->views, surface_link)
1895 weston_view_geometry_dirty(view);
1896}
1897
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001898WL_EXPORT void
1899weston_surface_set_size(struct weston_surface *surface,
1900 int32_t width, int32_t height)
1901{
1902 assert(!surface->resource);
1903 surface_set_size(surface, width, height);
1904}
1905
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001906static int
1907fixed_round_up_to_int(wl_fixed_t f)
1908{
1909 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1910}
1911
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001912static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001913convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1914 int32_t width, int32_t height,
1915 uint32_t transform,
1916 int32_t scale)
1917{
1918 assert(scale > 0);
1919
1920 switch (transform) {
1921 case WL_OUTPUT_TRANSFORM_NORMAL:
1922 case WL_OUTPUT_TRANSFORM_180:
1923 case WL_OUTPUT_TRANSFORM_FLIPPED:
1924 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1925 *width_out = width / scale;
1926 *height_out = height / scale;
1927 break;
1928 case WL_OUTPUT_TRANSFORM_90:
1929 case WL_OUTPUT_TRANSFORM_270:
1930 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1931 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1932 *width_out = height / scale;
1933 *height_out = width / scale;
1934 break;
1935 default:
1936 assert(0 && "invalid transform");
1937 }
1938}
1939
1940static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001941weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001942{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001943 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001944
1945 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001946 surface->width_from_buffer = 0;
1947 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001948 return;
1949 }
1950
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001951 convert_size_by_transform_scale(&surface->width_from_buffer,
1952 &surface->height_from_buffer,
1953 surface->buffer_ref.buffer->width,
1954 surface->buffer_ref.buffer->height,
1955 vp->buffer.transform,
1956 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001957}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001958
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001959static void
1960weston_surface_update_size(struct weston_surface *surface)
1961{
1962 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1963 int32_t width, height;
1964
1965 width = surface->width_from_buffer;
1966 height = surface->height_from_buffer;
1967
1968 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001969 surface_set_size(surface,
1970 vp->surface.width, vp->surface.height);
1971 return;
1972 }
1973
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001974 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001975 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1976 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1977
1978 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001979 return;
1980 }
1981
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001982 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001983}
1984
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001985/** weston_compositor_get_time
1986 * \ingroup compositor
1987 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001988WL_EXPORT void
1989weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001990{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001991 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001992}
1993
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001994/** weston_compositor_pick_view
1995 * \ingroup compositor
1996 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997WL_EXPORT struct weston_view *
1998weston_compositor_pick_view(struct weston_compositor *compositor,
1999 wl_fixed_t x, wl_fixed_t y,
2000 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002001{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002002 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002003 wl_fixed_t view_x, view_y;
2004 int view_ix, view_iy;
2005 int ix = wl_fixed_to_int(x);
2006 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002007
Jason Ekstranda7af7042013-10-12 22:38:11 -05002008 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002009 if (!pixman_region32_contains_point(
2010 &view->transform.boundingbox, ix, iy, NULL))
2011 continue;
2012
2013 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2014 view_ix = wl_fixed_to_int(view_x);
2015 view_iy = wl_fixed_to_int(view_y);
2016
2017 if (!pixman_region32_contains_point(&view->surface->input,
2018 view_ix, view_iy, NULL))
2019 continue;
2020
Pekka Paalanen380adf52015-02-16 14:39:11 +02002021 if (view->geometry.scissor_enabled &&
2022 !pixman_region32_contains_point(&view->geometry.scissor,
2023 view_ix, view_iy, NULL))
2024 continue;
2025
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002026 *vx = view_x;
2027 *vy = view_y;
2028 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002029 }
2030
Derek Foremanf9318d12015-05-11 15:40:11 -05002031 *vx = wl_fixed_from_int(-1000000);
2032 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002033 return NULL;
2034}
2035
2036static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002037weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002038{
Daniel Stone37816df2012-05-16 18:45:18 +01002039 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002040
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002041 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002042 return;
2043
Daniel Stone37816df2012-05-16 18:45:18 +01002044 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002045 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002046}
2047
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002048WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002050{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002051 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002052
Jason Ekstranda7af7042013-10-12 22:38:11 -05002053 if (!weston_view_is_mapped(view))
2054 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002055
Jason Ekstranda7af7042013-10-12 22:38:11 -05002056 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002057 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002058 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002059 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002060 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002061 wl_list_remove(&view->link);
2062 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002063 view->output_mask = 0;
2064 weston_surface_assign_output(view->surface);
2065
2066 if (weston_surface_is_mapped(view->surface))
2067 return;
2068
2069 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002070 struct weston_touch *touch = weston_seat_get_touch(seat);
2071 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2072 struct weston_keyboard *keyboard =
2073 weston_seat_get_keyboard(seat);
2074
2075 if (keyboard && keyboard->focus == view->surface)
2076 weston_keyboard_set_focus(keyboard, NULL);
2077 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002078 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002079 if (touch && touch->focus == view)
2080 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002081 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002083
Jason Ekstranda7af7042013-10-12 22:38:11 -05002084WL_EXPORT void
2085weston_surface_unmap(struct weston_surface *surface)
2086{
2087 struct weston_view *view;
2088
Armin Krezovićf8486c32016-06-30 06:04:28 +02002089 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090 wl_list_for_each(view, &surface->views, surface_link)
2091 weston_view_unmap(view);
2092 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002093}
2094
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002095static void
2096weston_surface_reset_pending_buffer(struct weston_surface *surface)
2097{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002098 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002099 surface->pending.sx = 0;
2100 surface->pending.sy = 0;
2101 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002102 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002103}
2104
Jason Ekstranda7af7042013-10-12 22:38:11 -05002105WL_EXPORT void
2106weston_view_destroy(struct weston_view *view)
2107{
2108 wl_signal_emit(&view->destroy_signal, view);
2109
2110 assert(wl_list_empty(&view->geometry.child_list));
2111
2112 if (weston_view_is_mapped(view)) {
2113 weston_view_unmap(view);
2114 weston_compositor_build_view_list(view->surface->compositor);
2115 }
2116
2117 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002118 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002119
2120 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002121 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002122 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002123 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124
2125 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002126 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127
Jason Ekstranda7af7042013-10-12 22:38:11 -05002128 wl_list_remove(&view->surface_link);
2129
2130 free(view);
2131}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002132
2133WL_EXPORT void
2134weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002135{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002136 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002137 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002138 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002139
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002140 if (--surface->ref_count > 0)
2141 return;
2142
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002143 assert(surface->resource == NULL);
2144
Pekka Paalanenca790762015-04-17 14:23:38 +03002145 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002146
Pekka Paalanene67858b2013-04-25 13:57:42 +03002147 assert(wl_list_empty(&surface->subsurface_list_pending));
2148 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002149
Jason Ekstranda7af7042013-10-12 22:38:11 -05002150 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2151 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002152
Jason Ekstrand7b982072014-05-20 14:33:03 -05002153 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002154
Pekka Paalanende685b82012-12-04 15:58:12 +02002155 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002156 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002157
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002158 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002159 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002160 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002161
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002162 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002163 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002164
Pekka Paalanen133e4392014-09-23 22:08:46 -04002165 weston_presentation_feedback_discard_list(&surface->feedback_list);
2166
Jonas Ådahld3414f22016-07-22 17:56:31 +08002167 wl_list_for_each_safe(constraint, next_constraint,
2168 &surface->pointer_constraints,
2169 link)
2170 weston_pointer_constraint_destroy(constraint);
2171
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002172 fd_clear(&surface->acquire_fence_fd);
2173
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002174 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002175}
2176
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002177static void
2178destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002179{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002180 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002181
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002182 assert(surface);
2183
Giulio Camuffo0d379742013-11-15 22:06:15 +01002184 /* Set the resource to NULL, since we don't want to leave a
2185 * dangling pointer if the surface was refcounted and survives
2186 * the weston_surface_destroy() call. */
2187 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002188
2189 if (surface->viewport_resource)
2190 wl_resource_set_user_data(surface->viewport_resource, NULL);
2191
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002192 if (surface->synchronization_resource) {
2193 wl_resource_set_user_data(surface->synchronization_resource,
2194 NULL);
2195 }
2196
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002197 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002198}
2199
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002200static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002201weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2202{
2203 struct weston_buffer *buffer =
2204 container_of(listener, struct weston_buffer, destroy_listener);
2205
2206 wl_signal_emit(&buffer->destroy_signal, buffer);
2207 free(buffer);
2208}
2209
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002210WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002211weston_buffer_from_resource(struct wl_resource *resource)
2212{
2213 struct weston_buffer *buffer;
2214 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002215
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002216 listener = wl_resource_get_destroy_listener(resource,
2217 weston_buffer_destroy_handler);
2218
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002219 if (listener)
2220 return container_of(listener, struct weston_buffer,
2221 destroy_listener);
2222
2223 buffer = zalloc(sizeof *buffer);
2224 if (buffer == NULL)
2225 return NULL;
2226
2227 buffer->resource = resource;
2228 wl_signal_init(&buffer->destroy_signal);
2229 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002230 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002231 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002232
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002233 return buffer;
2234}
2235
2236static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002237weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2238 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002239{
Pekka Paalanende685b82012-12-04 15:58:12 +02002240 struct weston_buffer_reference *ref =
2241 container_of(listener, struct weston_buffer_reference,
2242 destroy_listener);
2243
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002244 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002245 ref->buffer = NULL;
2246}
2247
2248WL_EXPORT void
2249weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002250 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002251{
2252 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002253 ref->buffer->busy_count--;
2254 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002255 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002256 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002257 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002258 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002259 }
2260
Pekka Paalanende685b82012-12-04 15:58:12 +02002261 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002262 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002263 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002264 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002265 }
2266
Pekka Paalanende685b82012-12-04 15:58:12 +02002267 ref->buffer = buffer;
2268 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2269}
2270
2271static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002272weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2273 void *data)
2274{
2275 struct weston_buffer_release_reference *ref =
2276 container_of(listener, struct weston_buffer_release_reference,
2277 destroy_listener);
2278
2279 assert((struct wl_resource *)data == ref->buffer_release->resource);
2280 ref->buffer_release = NULL;
2281}
2282
2283static void
2284weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2285{
2286 struct wl_resource *resource = buffer_release->resource;
2287 int release_fence_fd = buffer_release->fence_fd;
2288
2289 if (release_fence_fd >= 0) {
2290 zwp_linux_buffer_release_v1_send_fenced_release(
2291 resource, release_fence_fd);
2292 } else {
2293 zwp_linux_buffer_release_v1_send_immediate_release(
2294 resource);
2295 }
2296
2297 wl_resource_destroy(resource);
2298}
2299
2300WL_EXPORT void
2301weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2302 struct weston_buffer_release *buffer_release)
2303{
2304 if (buffer_release == ref->buffer_release)
2305 return;
2306
2307 if (ref->buffer_release) {
2308 ref->buffer_release->ref_count--;
2309 wl_list_remove(&ref->destroy_listener.link);
2310 if (ref->buffer_release->ref_count == 0)
2311 weston_buffer_release_destroy(ref->buffer_release);
2312 }
2313
2314 if (buffer_release) {
2315 buffer_release->ref_count++;
2316 wl_resource_add_destroy_listener(buffer_release->resource,
2317 &ref->destroy_listener);
2318 }
2319
2320 ref->buffer_release = buffer_release;
2321 ref->destroy_listener.notify =
2322 weston_buffer_release_reference_handle_destroy;
2323}
2324
2325WL_EXPORT void
2326weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2327 struct weston_buffer_release_reference *src)
2328{
2329 weston_buffer_release_reference(dest, src->buffer_release);
2330 weston_buffer_release_reference(src, NULL);
2331}
2332
2333static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002334weston_surface_attach(struct weston_surface *surface,
2335 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002336{
2337 weston_buffer_reference(&surface->buffer_ref, buffer);
2338
Pekka Paalanena6421c42012-12-04 15:58:10 +02002339 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002340 if (weston_surface_is_mapped(surface))
2341 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002342 }
2343
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002344 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002345
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002346 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002347 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002348}
2349
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002350/** weston_compositor_damage_all
2351 * \ingroup compositor
2352 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002353WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002354weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002355{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002356 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002357
2358 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002359 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002360}
2361
Marius Vlad55d87362019-06-11 01:15:35 +03002362/**
2363 * \ingroup output
2364 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002365WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002366weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002367{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002368 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002369
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002370 pixman_region32_union(&compositor->primary_plane.damage,
2371 &compositor->primary_plane.damage,
2372 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002373 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002374}
2375
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002376static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002377surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002378{
Pekka Paalanende685b82012-12-04 15:58:12 +02002379 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002380 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002381 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002382
Marius Vlad2a1b7862019-09-05 13:12:18 +03002383 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002384 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002385 TLP_OUTPUT(surface->output), TLP_END);
2386
Jason Ekstrandef540082014-06-26 10:37:36 -07002387 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002388}
2389
2390static void
2391view_accumulate_damage(struct weston_view *view,
2392 pixman_region32_t *opaque)
2393{
2394 pixman_region32_t damage;
2395
2396 pixman_region32_init(&damage);
2397 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002398 pixman_box32_t *extents;
2399
Jason Ekstranda7af7042013-10-12 22:38:11 -05002400 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002401 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002402 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002403 pixman_region32_copy(&damage, &view->surface->damage);
2404 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002405 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002406 }
2407
Pekka Paalanen380adf52015-02-16 14:39:11 +02002408 pixman_region32_intersect(&damage, &damage,
2409 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002410 pixman_region32_subtract(&damage, &damage, opaque);
2411 pixman_region32_union(&view->plane->damage,
2412 &view->plane->damage, &damage);
2413 pixman_region32_fini(&damage);
2414 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002415 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002416}
2417
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002418static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002419compositor_accumulate_damage(struct weston_compositor *ec)
2420{
2421 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002422 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002423 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002424
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002425 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002426
2427 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002428 pixman_region32_copy(&plane->clip, &clip);
2429
2430 pixman_region32_init(&opaque);
2431
Jason Ekstranda7af7042013-10-12 22:38:11 -05002432 wl_list_for_each(ev, &ec->view_list, link) {
2433 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002434 continue;
2435
Jason Ekstranda7af7042013-10-12 22:38:11 -05002436 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002437 }
2438
2439 pixman_region32_union(&clip, &clip, &opaque);
2440 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002441 }
2442
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002443 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002444
Jason Ekstranda7af7042013-10-12 22:38:11 -05002445 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002446 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002447
2448 wl_list_for_each(ev, &ec->view_list, link) {
2449 if (ev->surface->touched)
2450 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002451 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002452
2453 surface_flush_damage(ev->surface);
2454
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002455 /* Both the renderer and the backend have seen the buffer
2456 * by now. If renderer needs the buffer, it has its own
2457 * reference set. If the backend wants to keep the buffer
2458 * around for migrating the surface into a non-primary plane
2459 * later, keep_buffer is true. Otherwise, drop the core
2460 * reference now, and allow early buffer release. This enables
2461 * clients to use single-buffering.
2462 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002463 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002464 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002465 weston_buffer_release_reference(
2466 &ev->surface->buffer_release_ref, NULL);
2467 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002468 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002469}
2470
2471static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002472surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002473{
2474 struct weston_subsurface *sub;
2475
Pekka Paalanene67858b2013-04-25 13:57:42 +03002476 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002477 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002478 continue;
2479
Jason Ekstranda7af7042013-10-12 22:38:11 -05002480 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2481 wl_list_init(&sub->surface->views);
2482
2483 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002484 }
2485}
2486
2487static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002488surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002489{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002490 struct weston_subsurface *sub;
2491 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002492
Jason Ekstranda7af7042013-10-12 22:38:11 -05002493 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2494 if (sub->surface == surface)
2495 continue;
2496
George Kiagiadakised04d382014-06-13 18:10:26 +02002497 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2498 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002499 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002500 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002501
2502 surface_free_unused_subsurface_views(sub->surface);
2503 }
2504}
2505
2506static void
2507view_list_add_subsurface_view(struct weston_compositor *compositor,
2508 struct weston_subsurface *sub,
2509 struct weston_view *parent)
2510{
2511 struct weston_subsurface *child;
2512 struct weston_view *view = NULL, *iv;
2513
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002514 if (!weston_surface_is_mapped(sub->surface))
2515 return;
2516
Jason Ekstranda7af7042013-10-12 22:38:11 -05002517 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2518 if (iv->geometry.parent == parent) {
2519 view = iv;
2520 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002521 }
2522 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002523
2524 if (view) {
2525 /* Put it back in the surface's list of views */
2526 wl_list_remove(&view->surface_link);
2527 wl_list_insert(&sub->surface->views, &view->surface_link);
2528 } else {
2529 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002530 weston_view_set_position(view,
2531 sub->position.x,
2532 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002533 weston_view_set_transform_parent(view, parent);
2534 }
2535
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002536 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002537 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002538 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002539
Pekka Paalanenb188e912013-11-19 14:03:35 +02002540 if (wl_list_empty(&sub->surface->subsurface_list)) {
2541 wl_list_insert(compositor->view_list.prev, &view->link);
2542 return;
2543 }
2544
2545 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2546 if (child->surface == sub->surface)
2547 wl_list_insert(compositor->view_list.prev, &view->link);
2548 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002549 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002550 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002551}
2552
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002553/* This recursively adds the sub-surfaces for a view, relying on the
2554 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2555 * change first happens to the sub-surface list, and then automatically
2556 * propagates here. See weston_surface_damage_subsurfaces() for how the
2557 * sub-surfaces receive damage when the client changes the state.
2558 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002559static void
2560view_list_add(struct weston_compositor *compositor,
2561 struct weston_view *view)
2562{
2563 struct weston_subsurface *sub;
2564
2565 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002566
Pekka Paalanenb188e912013-11-19 14:03:35 +02002567 if (wl_list_empty(&view->surface->subsurface_list)) {
2568 wl_list_insert(compositor->view_list.prev, &view->link);
2569 return;
2570 }
2571
2572 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2573 if (sub->surface == view->surface)
2574 wl_list_insert(compositor->view_list.prev, &view->link);
2575 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002576 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002577 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002578}
2579
2580static void
2581weston_compositor_build_view_list(struct weston_compositor *compositor)
2582{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002583 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002584 struct weston_layer *layer;
2585
2586 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002587 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002588 surface_stash_subsurface_views(view->surface);
2589
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002590 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2591 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002592 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002593
Jason Ekstranda7af7042013-10-12 22:38:11 -05002594 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002595 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002596 view_list_add(compositor, view);
2597 }
2598 }
2599
2600 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002601 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002602 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002603}
2604
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002605static void
2606weston_output_take_feedback_list(struct weston_output *output,
2607 struct weston_surface *surface)
2608{
2609 struct weston_view *view;
2610 struct weston_presentation_feedback *feedback;
2611 uint32_t flags = 0xffffffff;
2612
2613 if (wl_list_empty(&surface->feedback_list))
2614 return;
2615
2616 /* All views must have the flag for the flag to survive. */
2617 wl_list_for_each(view, &surface->views, surface_link) {
2618 /* ignore views that are not on this output at all */
2619 if (view->output_mask & (1u << output->id))
2620 flags &= view->psf_flags;
2621 }
2622
2623 wl_list_for_each(feedback, &surface->feedback_list, link)
2624 feedback->psf_flags = flags;
2625
2626 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2627 wl_list_init(&surface->feedback_list);
2628}
2629
David Herrmann1edf44c2013-10-22 17:11:26 +02002630static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002631weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002632{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002633 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002634 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002635 struct weston_animation *animation, *next;
2636 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002637 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002638 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002639 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002640 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302641 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002642
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002643 if (output->destroying)
2644 return 0;
2645
Marius Vlad3203ff62019-09-05 14:56:12 +03002646 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002647
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002648 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002649 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002650
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302651 /* Find the highest protection desired for an output */
2652 wl_list_for_each(ev, &ec->view_list, link) {
2653 if (ev->surface->output_mask & (1u << output->id)) {
2654 /*
2655 * The desired_protection of the output should be the
2656 * maximum of the desired_protection of the surfaces,
2657 * that are displayed on that output, to avoid
2658 * reducing the protection for existing surfaces.
2659 */
2660 if (ev->surface->desired_protection > highest_requested)
2661 highest_requested =
2662 ev->surface->desired_protection;
2663 }
2664 }
2665
2666 output->desired_protection = highest_requested;
2667
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002668 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002669 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002670 } else {
2671 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002672 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002673 ev->psf_flags = 0;
2674 }
2675 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002676
Pekka Paalanene67858b2013-04-25 13:57:42 +03002677 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002678 wl_list_for_each(ev, &ec->view_list, link) {
2679 /* Note: This operation is safe to do multiple times on the
2680 * same surface.
2681 */
2682 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002683 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002684 &ev->surface->frame_callback_list);
2685 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002686
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002687 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002688 }
2689 }
2690
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002691 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002692
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002693 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002694 pixman_region32_intersect(&output_damage,
2695 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002696 pixman_region32_subtract(&output_damage,
2697 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002698
Scott Moreauccbf29d2012-02-22 14:21:41 -07002699 if (output->dirty)
2700 weston_output_update_matrix(output);
2701
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002702 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002703
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002704 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002705
Daniel Stone09a97e22017-03-01 11:34:06 +00002706 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002707 if (r == 0)
2708 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002709
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002710 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002711
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002712 frame_time_msec = timespec_to_msec(&output->frame_time);
2713
Jonas Ådahldb773762012-06-13 00:01:21 +02002714 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002715 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002716 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002717 }
2718
Scott Moreaud64cf212012-06-08 19:40:54 -06002719 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002720 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002721 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002722 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002723
Marius Vlad3203ff62019-09-05 14:56:12 +03002724 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002725
David Herrmann1edf44c2013-10-22 17:11:26 +02002726 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002727}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002728
Pekka Paalanen82919792014-05-21 13:51:49 +03002729static void
2730weston_output_schedule_repaint_reset(struct weston_output *output)
2731{
Daniel Stone05df8c12017-03-03 16:59:42 +00002732 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002733 TL_POINT(output->compositor, "core_repaint_exit_loop",
2734 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002735}
2736
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002737static int
2738weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2739 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002740{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002741 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002742 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002743 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002744
Daniel Stone6847b852017-03-01 11:34:08 +00002745 /* We're not ready yet; come back to make a decision later. */
2746 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002747 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002748
2749 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2750 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002751 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002752
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002753 /* If we're sleeping, drop the repaint machinery entirely; we will
2754 * explicitly repaint all outputs when we come back. */
2755 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2756 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2757 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002758
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002759 /* We don't actually need to repaint this output; drop it from
2760 * repaint until something causes damage. */
2761 if (!output->repaint_needed)
2762 goto err;
2763
2764 /* If repaint fails, we aren't going to get weston_output_finish_frame
2765 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002766 * something schedules a successful repaint later. As repainting may
2767 * take some time, re-read our clock as a courtesy to the next
2768 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002769 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002770 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002771 if (ret != 0)
2772 goto err;
2773
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002774 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002775 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002776
2777err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002778 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002779 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002780}
2781
2782static void
2783output_repaint_timer_arm(struct weston_compositor *compositor)
2784{
2785 struct weston_output *output;
2786 bool any_should_repaint = false;
2787 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002788 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002789
2790 weston_compositor_read_presentation_clock(compositor, &now);
2791
2792 wl_list_for_each(output, &compositor->output_list, link) {
2793 int64_t msec_to_this;
2794
2795 if (output->repaint_status != REPAINT_SCHEDULED)
2796 continue;
2797
2798 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2799 &now);
2800 if (!any_should_repaint || msec_to_this < msec_to_next)
2801 msec_to_next = msec_to_this;
2802
2803 any_should_repaint = true;
2804 }
2805
2806 if (!any_should_repaint)
2807 return;
2808
2809 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2810 * This is a workaround to allow coalescing multiple output repaints
2811 * particularly from weston_output_finish_frame()
2812 * into the same call, which would not happen if we called
2813 * output_repaint_timer_handler() directly.
2814 */
2815 if (msec_to_next < 1)
2816 msec_to_next = 1;
2817
2818 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2819}
2820
2821static int
2822output_repaint_timer_handler(void *data)
2823{
2824 struct weston_compositor *compositor = data;
2825 struct weston_output *output;
2826 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002827 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002828 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002829
2830 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002831
2832 if (compositor->backend->repaint_begin)
2833 repaint_data = compositor->backend->repaint_begin(compositor);
2834
2835 wl_list_for_each(output, &compositor->output_list, link) {
2836 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2837 if (ret)
2838 break;
2839 }
2840
2841 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002842 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002843 ret = compositor->backend->repaint_flush(compositor,
2844 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002845 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002846 if (compositor->backend->repaint_cancel)
2847 compositor->backend->repaint_cancel(compositor,
2848 repaint_data);
2849 }
2850
2851 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002852 wl_list_for_each(output, &compositor->output_list, link) {
2853 if (output->repainted)
2854 weston_output_schedule_repaint_reset(output);
2855 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002856 }
Daniel Stone6847b852017-03-01 11:34:08 +00002857
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002858 wl_list_for_each(output, &compositor->output_list, link)
2859 output->repainted = false;
2860
Daniel Stone6847b852017-03-01 11:34:08 +00002861 output_repaint_timer_arm(compositor);
2862
Pekka Paalanen0513a952014-05-21 16:17:27 +03002863 return 0;
2864}
2865
Marius Vlad55d87362019-06-11 01:15:35 +03002866/**
2867 * \ingroup output
2868 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002869WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002870weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002871 const struct timespec *stamp,
2872 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002873{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002874 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002875 int32_t refresh_nsec;
2876 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002877 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002878
Pekka Paalanenb5026542014-11-12 15:09:24 +02002879
Daniel Stone05df8c12017-03-03 16:59:42 +00002880 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002881 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2882
Daniel Stone6847b852017-03-01 11:34:08 +00002883 weston_compositor_read_presentation_clock(compositor, &now);
2884
Daniel Stone3615ce12017-03-01 11:34:05 +00002885 /* If we haven't been supplied any timestamp at all, we don't have a
2886 * timebase to work against, so any delay just wastes time. Push a
2887 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002888 if (!stamp) {
2889 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002890 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002891 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002892
Marius Vlad3203ff62019-09-05 14:56:12 +03002893 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Marius Vladdf9278a2018-03-06 18:56:23 +02002894 TLP_VBLANK(stamp), TLP_END);
2895
Pekka Paalanend7894d02015-07-03 15:08:53 +03002896 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002897 weston_presentation_feedback_present_list(&output->feedback_list,
2898 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002899 output->msc,
2900 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002901
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002902 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002903
Daniel Stone6847b852017-03-01 11:34:08 +00002904 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2905 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2906 -compositor->repaint_msec);
2907 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002908
2909 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002910 static bool warned;
2911
2912 if (!warned)
2913 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002914 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002915 warned = true;
2916
Daniel Stone6847b852017-03-01 11:34:08 +00002917 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002918 }
2919
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002920 /* Called from restart_repaint_loop and restart happens already after
2921 * the deadline given by repaint_msec? In that case we delay until
2922 * the deadline of the next frame, to give clients a more predictable
2923 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002924 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2925 msec_rel < 0) {
2926 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2927 timespec_add_nsec(&output->next_repaint,
2928 &output->next_repaint,
2929 refresh_nsec);
2930 }
2931 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002932
Daniel Stone3615ce12017-03-01 11:34:05 +00002933out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002934 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002935 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002936}
2937
2938static void
2939idle_repaint(void *data)
2940{
2941 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002942 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002943
Daniel Stone05df8c12017-03-03 16:59:42 +00002944 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2945 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002946 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002947 ret = output->start_repaint_loop(output);
2948 if (ret != 0)
2949 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002950}
2951
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002952WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002953weston_layer_entry_insert(struct weston_layer_entry *list,
2954 struct weston_layer_entry *entry)
2955{
2956 wl_list_insert(&list->link, &entry->link);
2957 entry->layer = list->layer;
2958}
2959
2960WL_EXPORT void
2961weston_layer_entry_remove(struct weston_layer_entry *entry)
2962{
2963 wl_list_remove(&entry->link);
2964 wl_list_init(&entry->link);
2965 entry->layer = NULL;
2966}
2967
Quentin Glidic82681572016-12-17 13:40:51 +01002968
2969/** Initialize the weston_layer struct.
2970 *
2971 * \param compositor The compositor instance
2972 * \param layer The layer to initialize
2973 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002974WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002975weston_layer_init(struct weston_layer *layer,
2976 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002977{
Quentin Glidic82681572016-12-17 13:40:51 +01002978 layer->compositor = compositor;
2979 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002980 wl_list_init(&layer->view_list.link);
2981 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002982 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002983}
2984
2985/** Sets the position of the layer in the layer list. The layer will be placed
2986 * below any layer with the same position value, if any.
2987 * This function is safe to call if the layer is already on the list, but the
2988 * layer may be moved below other layers at the same position, if any.
2989 *
2990 * \param layer The layer to modify
2991 * \param position The position the layer will be placed at
2992 */
2993WL_EXPORT void
2994weston_layer_set_position(struct weston_layer *layer,
2995 enum weston_layer_position position)
2996{
2997 struct weston_layer *below;
2998
2999 wl_list_remove(&layer->link);
3000
3001 /* layer_list is ordered from top to bottom, the last layer being the
3002 * background with the smallest position value */
3003
3004 layer->position = position;
3005 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3006 if (below->position >= layer->position) {
3007 wl_list_insert(&below->link, &layer->link);
3008 return;
3009 }
3010 }
3011 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3012}
3013
3014/** Hide a layer by taking it off the layer list.
3015 * This function is safe to call if the layer is not on the list.
3016 *
3017 * \param layer The layer to hide
3018 */
3019WL_EXPORT void
3020weston_layer_unset_position(struct weston_layer *layer)
3021{
3022 wl_list_remove(&layer->link);
3023 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003024}
3025
3026WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003027weston_layer_set_mask(struct weston_layer *layer,
3028 int x, int y, int width, int height)
3029{
3030 struct weston_view *view;
3031
3032 layer->mask.x1 = x;
3033 layer->mask.x2 = x + width;
3034 layer->mask.y1 = y;
3035 layer->mask.y2 = y + height;
3036
3037 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3038 weston_view_geometry_dirty(view);
3039 }
3040}
3041
3042WL_EXPORT void
3043weston_layer_set_mask_infinite(struct weston_layer *layer)
3044{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003045 struct weston_view *view;
3046
3047 layer->mask.x1 = INT32_MIN;
3048 layer->mask.x2 = INT32_MAX;
3049 layer->mask.y1 = INT32_MIN;
3050 layer->mask.y2 = INT32_MAX;
3051
3052 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3053 weston_view_geometry_dirty(view);
3054 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003055}
3056
Daniel Stone3b775632018-07-20 08:38:25 +01003057WL_EXPORT bool
3058weston_layer_mask_is_infinite(struct weston_layer *layer)
3059{
3060 return layer->mask.x1 == INT32_MIN &&
3061 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003062 layer->mask.x2 == INT32_MAX &&
3063 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003064}
3065
Marius Vlad55d87362019-06-11 01:15:35 +03003066/**
3067 * \ingroup output
3068 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003069WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003070weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003071{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003072 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003073 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003074
Bryce Harrington08976ac2016-08-30 12:05:16 -07003075 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3076 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003077 return;
3078
Pekka Paalanenb5026542014-11-12 15:09:24 +02003079 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003080 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003081
Kristian Høgsbergef044142011-06-21 15:02:12 -04003082 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003083 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003084
3085 /* If we already have a repaint scheduled for our idle handler,
3086 * no need to set it again. If the repaint has been called but
3087 * not finished, then weston_output_finish_frame() will notice
3088 * that a repaint is needed and schedule one. */
3089 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003090 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003091
Daniel Stone05df8c12017-03-03 16:59:42 +00003092 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003093 assert(!output->idle_repaint_source);
3094 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3095 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003096 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003097}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003098
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003099/** weston_compositor_schedule_repaint
3100 * \ingroup compositor
3101 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003102WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003103weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3104{
3105 struct weston_output *output;
3106
3107 wl_list_for_each(output, &compositor->output_list, link)
3108 weston_output_schedule_repaint(output);
3109}
3110
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003111static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003112surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003113{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003114 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003115}
3116
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003117static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003118surface_attach(struct wl_client *client,
3119 struct wl_resource *resource,
3120 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3121{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003122 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003123 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003124
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003125 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003126 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003127 if (buffer == NULL) {
3128 wl_client_post_no_memory(client);
3129 return;
3130 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003131 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003132
Pekka Paalanende685b82012-12-04 15:58:12 +02003133 /* Attach, attach, without commit in between does not send
3134 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003135 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003136
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003137 surface->pending.sx = sx;
3138 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003139 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003140}
3141
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003142static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003143surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003144 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003145 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003146{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003147 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003148
Derek Foreman57e92ed2015-11-17 14:11:35 -06003149 if (width <= 0 || height <= 0)
3150 return;
3151
Derek Foreman152254b2015-11-26 14:17:48 -06003152 pixman_region32_union_rect(&surface->pending.damage_surface,
3153 &surface->pending.damage_surface,
3154 x, y, width, height);
3155}
3156
3157static void
3158surface_damage_buffer(struct wl_client *client,
3159 struct wl_resource *resource,
3160 int32_t x, int32_t y, int32_t width, int32_t height)
3161{
3162 struct weston_surface *surface = wl_resource_get_user_data(resource);
3163
3164 if (width <= 0 || height <= 0)
3165 return;
3166
3167 pixman_region32_union_rect(&surface->pending.damage_buffer,
3168 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003169 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003170}
3171
Kristian Høgsberg33418202011-08-16 23:01:28 -04003172static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003173destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003174{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003175 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003176
3177 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003178 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003179}
3180
3181static void
3182surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003183 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003184{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003185 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003186 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003187
3188 cb = malloc(sizeof *cb);
3189 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003190 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003191 return;
3192 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003193
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003194 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3195 callback);
3196 if (cb->resource == NULL) {
3197 free(cb);
3198 wl_resource_post_no_memory(resource);
3199 return;
3200 }
3201
Jason Ekstranda85118c2013-06-27 20:17:02 -05003202 wl_resource_set_implementation(cb->resource, NULL, cb,
3203 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003204
Pekka Paalanenbc106382012-10-10 12:49:31 +03003205 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003206}
3207
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003208static void
3209surface_set_opaque_region(struct wl_client *client,
3210 struct wl_resource *resource,
3211 struct wl_resource *region_resource)
3212{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003213 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003214 struct weston_region *region;
3215
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003216 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003217 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003218 pixman_region32_copy(&surface->pending.opaque,
3219 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003220 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003221 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003222 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003223}
3224
3225static void
3226surface_set_input_region(struct wl_client *client,
3227 struct wl_resource *resource,
3228 struct wl_resource *region_resource)
3229{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003230 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003231 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003232
3233 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003234 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003235 pixman_region32_copy(&surface->pending.input,
3236 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003237 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003238 pixman_region32_fini(&surface->pending.input);
3239 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003240 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003241}
3242
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003243/* Cause damage to this sub-surface and all its children.
3244 *
3245 * This is useful when there are state changes that need an implicit
3246 * damage, e.g. a z-order change.
3247 */
3248static void
3249weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3250{
3251 struct weston_subsurface *child;
3252
3253 weston_surface_damage(sub->surface);
3254 sub->reordered = false;
3255
3256 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3257 if (child != sub)
3258 weston_surface_damage_subsurfaces(child);
3259}
3260
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003261static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003262weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003263{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003264 struct weston_subsurface *sub;
3265
3266 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3267 parent_link_pending) {
3268 wl_list_remove(&sub->parent_link);
3269 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003270
3271 if (sub->reordered)
3272 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003273 }
3274}
3275
3276static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003277weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003278 struct weston_matrix *matrix)
3279{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003280 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003281 double src_width, src_height, dest_width, dest_height;
3282
3283 weston_matrix_init(matrix);
3284
3285 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3286 src_width = surface->width_from_buffer;
3287 src_height = surface->height_from_buffer;
3288 } else {
3289 src_width = wl_fixed_to_double(vp->buffer.src_width);
3290 src_height = wl_fixed_to_double(vp->buffer.src_height);
3291 }
3292
3293 if (vp->surface.width == -1) {
3294 dest_width = src_width;
3295 dest_height = src_height;
3296 } else {
3297 dest_width = vp->surface.width;
3298 dest_height = vp->surface.height;
3299 }
3300
3301 if (src_width != dest_width || src_height != dest_height)
3302 weston_matrix_scale(matrix,
3303 src_width / dest_width,
3304 src_height / dest_height, 1);
3305
3306 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3307 weston_matrix_translate(matrix,
3308 wl_fixed_to_double(vp->buffer.src_x),
3309 wl_fixed_to_double(vp->buffer.src_y),
3310 0);
3311
3312 switch (vp->buffer.transform) {
3313 case WL_OUTPUT_TRANSFORM_FLIPPED:
3314 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3315 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3316 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3317 weston_matrix_scale(matrix, -1, 1, 1);
3318 weston_matrix_translate(matrix,
3319 surface->width_from_buffer, 0, 0);
3320 break;
3321 }
3322
3323 switch (vp->buffer.transform) {
3324 default:
3325 case WL_OUTPUT_TRANSFORM_NORMAL:
3326 case WL_OUTPUT_TRANSFORM_FLIPPED:
3327 break;
3328 case WL_OUTPUT_TRANSFORM_90:
3329 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3330 weston_matrix_rotate_xy(matrix, 0, 1);
3331 weston_matrix_translate(matrix,
3332 surface->height_from_buffer, 0, 0);
3333 break;
3334 case WL_OUTPUT_TRANSFORM_180:
3335 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3336 weston_matrix_rotate_xy(matrix, -1, 0);
3337 weston_matrix_translate(matrix,
3338 surface->width_from_buffer,
3339 surface->height_from_buffer, 0);
3340 break;
3341 case WL_OUTPUT_TRANSFORM_270:
3342 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3343 weston_matrix_rotate_xy(matrix, 0, -1);
3344 weston_matrix_translate(matrix,
3345 0, surface->width_from_buffer, 0);
3346 break;
3347 }
3348
3349 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3350}
3351
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003352/**
3353 * Compute a + b > c while being safe to overflows.
3354 */
3355static bool
3356fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3357{
3358 return (int64_t)a + (int64_t)b > (int64_t)c;
3359}
3360
3361static bool
3362weston_surface_is_pending_viewport_source_valid(
3363 const struct weston_surface *surface)
3364{
3365 const struct weston_surface_state *pend = &surface->pending;
3366 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3367 int width_from_buffer = 0;
3368 int height_from_buffer = 0;
3369 wl_fixed_t w;
3370 wl_fixed_t h;
3371
3372 /* If viewport source rect is not set, it is always ok. */
3373 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3374 return true;
3375
3376 if (pend->newly_attached) {
3377 if (pend->buffer) {
3378 convert_size_by_transform_scale(&width_from_buffer,
3379 &height_from_buffer,
3380 pend->buffer->width,
3381 pend->buffer->height,
3382 vp->buffer.transform,
3383 vp->buffer.scale);
3384 } else {
3385 /* No buffer: viewport is irrelevant. */
3386 return true;
3387 }
3388 } else {
3389 width_from_buffer = surface->width_from_buffer;
3390 height_from_buffer = surface->height_from_buffer;
3391 }
3392
3393 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3394 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3395
3396 /* No buffer: viewport is irrelevant. */
3397 if (width_from_buffer == 0 || height_from_buffer == 0)
3398 return true;
3399
3400 /* overflow checks for wl_fixed_from_int() */
3401 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3402 return false;
3403 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3404 return false;
3405
3406 w = wl_fixed_from_int(width_from_buffer);
3407 h = wl_fixed_from_int(height_from_buffer);
3408
3409 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3410 return false;
3411 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3412 return false;
3413
3414 return true;
3415}
3416
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003417static bool
3418fixed_is_integer(wl_fixed_t v)
3419{
3420 return (v & 0xff) == 0;
3421}
3422
3423static bool
3424weston_surface_is_pending_viewport_dst_size_int(
3425 const struct weston_surface *surface)
3426{
3427 const struct weston_buffer_viewport *vp =
3428 &surface->pending.buffer_viewport;
3429
3430 if (vp->surface.width != -1) {
3431 assert(vp->surface.width > 0 && vp->surface.height > 0);
3432 return true;
3433 }
3434
3435 return fixed_is_integer(vp->buffer.src_width) &&
3436 fixed_is_integer(vp->buffer.src_height);
3437}
3438
Derek Foreman152254b2015-11-26 14:17:48 -06003439/* Translate pending damage in buffer co-ordinates to surface
3440 * co-ordinates and union it with a pixman_region32_t.
3441 * This should only be called after the buffer is attached.
3442 */
3443static void
3444apply_damage_buffer(pixman_region32_t *dest,
3445 struct weston_surface *surface,
3446 struct weston_surface_state *state)
3447{
3448 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3449
3450 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3451 * translated into surface co-ordinates and unioned with
3452 * any other surface damage.
3453 * None of this makes sense if there is no buffer though.
3454 */
3455 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3456 pixman_region32_t buffer_damage;
3457
3458 pixman_region32_intersect_rect(&state->damage_buffer,
3459 &state->damage_buffer,
3460 0, 0, buffer->width,
3461 buffer->height);
3462 pixman_region32_init(&buffer_damage);
3463 weston_matrix_transform_region(&buffer_damage,
3464 &surface->buffer_to_surface_matrix,
3465 &state->damage_buffer);
3466 pixman_region32_union(dest, dest, &buffer_damage);
3467 pixman_region32_fini(&buffer_damage);
3468 }
3469 /* We should clear this on commit even if there was no buffer */
3470 pixman_region32_clear(&state->damage_buffer);
3471}
3472
Jason Ekstrand1e059042014-10-16 10:55:19 -05003473static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303474weston_surface_set_desired_protection(struct weston_surface *surface,
3475 enum weston_hdcp_protection protection)
3476{
3477 if (surface->desired_protection == protection)
3478 return;
3479 surface->desired_protection = protection;
3480 weston_surface_damage(surface);
3481}
3482
3483static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303484weston_surface_set_protection_mode(struct weston_surface *surface,
3485 enum weston_surface_protection_mode p_mode)
3486{
3487 struct content_protection *cp = surface->compositor->content_protection;
3488 struct protected_surface *psurface;
3489
3490 surface->protection_mode = p_mode;
3491 wl_list_for_each(psurface, &cp->protected_list, link) {
3492 if (!psurface || psurface->surface != surface)
3493 continue;
3494 weston_protected_surface_send_event(psurface,
3495 surface->current_protection);
3496 }
3497}
3498
3499static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003500weston_surface_commit_state(struct weston_surface *surface,
3501 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003502{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003503 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003504 pixman_region32_t opaque;
3505
Alexander Larsson4ea95522013-05-22 14:41:37 +02003506 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003507 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003508 /* wp_viewport.set_source */
3509 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003510 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003511
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003512 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003513 if (state->newly_attached) {
3514 /* zwp_surface_synchronization_v1.set_acquire_fence */
3515 fd_move(&surface->acquire_fence_fd,
3516 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003517 /* zwp_surface_synchronization_v1.get_release */
3518 weston_buffer_release_move(&surface->buffer_release_ref,
3519 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003520 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003521 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003522 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003523 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003524 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003525
Jason Ekstrand1e059042014-10-16 10:55:19 -05003526 weston_surface_build_buffer_matrix(surface,
3527 &surface->surface_to_buffer_matrix);
3528 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3529 &surface->surface_to_buffer_matrix);
3530
Jason Ekstrand7b982072014-05-20 14:33:03 -05003531 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003532 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003533 if (surface->committed)
3534 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003535 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003536
Jason Ekstrand7b982072014-05-20 14:33:03 -05003537 state->sx = 0;
3538 state->sy = 0;
3539 state->newly_attached = 0;
3540 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003541
Derek Foreman152254b2015-11-26 14:17:48 -06003542 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003543 if (pixman_region32_not_empty(&state->damage_surface) ||
3544 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003545 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003546
Pekka Paalanen8e159182012-10-10 12:49:25 +03003547 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003548 &state->damage_surface);
3549
3550 apply_damage_buffer(&surface->damage, surface, state);
3551
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003552 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003553 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003554 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003555
3556 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003557 pixman_region32_init(&opaque);
3558 pixman_region32_intersect_rect(&opaque, &state->opaque,
3559 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003560
3561 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3562 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003563 wl_list_for_each(view, &surface->views, surface_link)
3564 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003565 }
3566
3567 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003568
3569 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003570 pixman_region32_intersect_rect(&surface->input, &state->input,
3571 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003572
Pekka Paalanenbc106382012-10-10 12:49:31 +03003573 /* wl_surface.frame */
3574 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003575 &state->frame_callback_list);
3576 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003577
3578 /* XXX:
3579 * What should happen with a feedback request, if there
3580 * is no wl_buffer attached for this commit?
3581 */
3582
3583 /* presentation.feedback */
3584 wl_list_insert_list(&surface->feedback_list,
3585 &state->feedback_list);
3586 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003587
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303588 /* weston_protected_surface.enforced/relaxed */
3589 if (surface->protection_mode != state->protection_mode)
3590 weston_surface_set_protection_mode(surface,
3591 state->protection_mode);
3592
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303593 /* weston_protected_surface.set_type */
3594 weston_surface_set_desired_protection(surface, state->desired_protection);
3595
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003596 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003597}
3598
3599static void
3600weston_surface_commit(struct weston_surface *surface)
3601{
3602 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003603
Pekka Paalanene67858b2013-04-25 13:57:42 +03003604 weston_surface_commit_subsurface_order(surface);
3605
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003606 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003607}
3608
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003609static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003610weston_subsurface_commit(struct weston_subsurface *sub);
3611
3612static void
3613weston_subsurface_parent_commit(struct weston_subsurface *sub,
3614 int parent_is_synchronized);
3615
3616static void
3617surface_commit(struct wl_client *client, struct wl_resource *resource)
3618{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003619 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003620 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3621
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003622 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3623 assert(surface->viewport_resource);
3624
3625 wl_resource_post_error(surface->viewport_resource,
3626 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3627 "wl_surface@%d has viewport source outside buffer",
3628 wl_resource_get_id(resource));
3629 return;
3630 }
3631
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003632 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3633 assert(surface->viewport_resource);
3634
3635 wl_resource_post_error(surface->viewport_resource,
3636 WP_VIEWPORT_ERROR_BAD_SIZE,
3637 "wl_surface@%d viewport dst size not integer",
3638 wl_resource_get_id(resource));
3639 return;
3640 }
3641
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003642 if (surface->pending.acquire_fence_fd >= 0) {
3643 assert(surface->synchronization_resource);
3644
3645 if (!surface->pending.buffer) {
3646 fd_clear(&surface->pending.acquire_fence_fd);
3647 wl_resource_post_error(surface->synchronization_resource,
3648 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3649 "wl_surface@%"PRIu32" no buffer for synchronization",
3650 wl_resource_get_id(resource));
3651 return;
3652 }
3653
3654 /* We support fences for both wp_linux_dmabuf and opaque EGL
3655 * buffers, as mandated by minor version 2 of the
3656 * zwp_linux_explicit_synchronization_v1 protocol. Since
3657 * renderers that support fences currently only support these
3658 * two buffer types plus SHM buffers, we can just check for the
3659 * SHM buffer case here.
3660 */
3661 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3662 fd_clear(&surface->pending.acquire_fence_fd);
3663 wl_resource_post_error(surface->synchronization_resource,
3664 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3665 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3666 wl_resource_get_id(resource));
3667 return;
3668 }
3669 }
3670
Alexandros Frantzis67629672018-10-19 12:14:11 +03003671 if (surface->pending.buffer_release_ref.buffer_release &&
3672 !surface->pending.buffer) {
3673 assert(surface->synchronization_resource);
3674
3675 wl_resource_post_error(surface->synchronization_resource,
3676 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3677 "wl_surface@%"PRIu32" no buffer for synchronization",
3678 wl_resource_get_id(resource));
3679 return;
3680 }
3681
Pekka Paalanene67858b2013-04-25 13:57:42 +03003682 if (sub) {
3683 weston_subsurface_commit(sub);
3684 return;
3685 }
3686
3687 weston_surface_commit(surface);
3688
3689 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3690 if (sub->surface != surface)
3691 weston_subsurface_parent_commit(sub, 0);
3692 }
3693}
3694
3695static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003696surface_set_buffer_transform(struct wl_client *client,
3697 struct wl_resource *resource, int transform)
3698{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003699 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003700
Jonny Lamba55f1392014-05-30 12:07:15 +02003701 /* if wl_output.transform grows more members this will need to be updated. */
3702 if (transform < 0 ||
3703 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3704 wl_resource_post_error(resource,
3705 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3706 "buffer transform must be a valid transform "
3707 "('%d' specified)", transform);
3708 return;
3709 }
3710
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003711 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003712 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003713}
3714
Alexander Larsson4ea95522013-05-22 14:41:37 +02003715static void
3716surface_set_buffer_scale(struct wl_client *client,
3717 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003718 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003719{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003720 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003721
Jonny Lamba55f1392014-05-30 12:07:15 +02003722 if (scale < 1) {
3723 wl_resource_post_error(resource,
3724 WL_SURFACE_ERROR_INVALID_SCALE,
3725 "buffer scale must be at least one "
3726 "('%d' specified)", scale);
3727 return;
3728 }
3729
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003730 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003731 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003732}
3733
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003734static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003735 surface_destroy,
3736 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003737 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003738 surface_frame,
3739 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003740 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003741 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003742 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003743 surface_set_buffer_scale,
3744 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003745};
3746
3747static void
3748compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003749 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003750{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003751 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003752 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003753
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003754 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003755 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003756 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003757 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003758 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003759
Jason Ekstranda85118c2013-06-27 20:17:02 -05003760 surface->resource =
3761 wl_resource_create(client, &wl_surface_interface,
3762 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003763 if (surface->resource == NULL) {
3764 weston_surface_destroy(surface);
3765 wl_resource_post_no_memory(resource);
3766 return;
3767 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003768 wl_resource_set_implementation(surface->resource, &surface_interface,
3769 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003770
3771 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003772}
3773
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003774static void
3775destroy_region(struct wl_resource *resource)
3776{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003777 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003778
3779 pixman_region32_fini(&region->region);
3780 free(region);
3781}
3782
3783static void
3784region_destroy(struct wl_client *client, struct wl_resource *resource)
3785{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003786 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003787}
3788
3789static void
3790region_add(struct wl_client *client, struct wl_resource *resource,
3791 int32_t x, int32_t y, int32_t width, int32_t height)
3792{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003793 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003794
3795 pixman_region32_union_rect(&region->region, &region->region,
3796 x, y, width, height);
3797}
3798
3799static void
3800region_subtract(struct wl_client *client, struct wl_resource *resource,
3801 int32_t x, int32_t y, int32_t width, int32_t height)
3802{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003803 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003804 pixman_region32_t rect;
3805
3806 pixman_region32_init_rect(&rect, x, y, width, height);
3807 pixman_region32_subtract(&region->region, &region->region, &rect);
3808 pixman_region32_fini(&rect);
3809}
3810
3811static const struct wl_region_interface region_interface = {
3812 region_destroy,
3813 region_add,
3814 region_subtract
3815};
3816
3817static void
3818compositor_create_region(struct wl_client *client,
3819 struct wl_resource *resource, uint32_t id)
3820{
3821 struct weston_region *region;
3822
3823 region = malloc(sizeof *region);
3824 if (region == NULL) {
3825 wl_resource_post_no_memory(resource);
3826 return;
3827 }
3828
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003829 pixman_region32_init(&region->region);
3830
Jason Ekstranda85118c2013-06-27 20:17:02 -05003831 region->resource =
3832 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003833 if (region->resource == NULL) {
3834 free(region);
3835 wl_resource_post_no_memory(resource);
3836 return;
3837 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003838 wl_resource_set_implementation(region->resource, &region_interface,
3839 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003840}
3841
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003842static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003843 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003844 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003845};
3846
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003847static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003848weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3849{
3850 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003851
Jason Ekstrand7b982072014-05-20 14:33:03 -05003852 weston_surface_commit_state(surface, &sub->cached);
3853 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003854
3855 weston_surface_commit_subsurface_order(surface);
3856
3857 weston_surface_schedule_repaint(surface);
3858
Jason Ekstrand7b982072014-05-20 14:33:03 -05003859 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003860}
3861
3862static void
3863weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3864{
3865 struct weston_surface *surface = sub->surface;
3866
3867 /*
3868 * If this commit would cause the surface to move by the
3869 * attach(dx, dy) parameters, the old damage region must be
3870 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003871 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003872 */
Derek Foreman152254b2015-11-26 14:17:48 -06003873 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003874 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003875 pixman_region32_union(&sub->cached.damage_surface,
3876 &sub->cached.damage_surface,
3877 &surface->pending.damage_surface);
3878 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003879
3880 if (surface->pending.newly_attached) {
3881 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003882 weston_surface_state_set_buffer(&sub->cached,
3883 surface->pending.buffer);
3884 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003885 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003886 weston_presentation_feedback_discard_list(
3887 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003888 /* zwp_surface_synchronization_v1.set_acquire_fence */
3889 fd_move(&sub->cached.acquire_fence_fd,
3890 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003891 /* zwp_surface_synchronization_v1.get_release */
3892 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3893 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003894 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303895 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303896 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003897 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003898 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003899 sub->cached.sx += surface->pending.sx;
3900 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003901
Derek Foreman152254b2015-11-26 14:17:48 -06003902 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3903
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003904 sub->cached.buffer_viewport.changed |=
3905 surface->pending.buffer_viewport.changed;
3906 sub->cached.buffer_viewport.buffer =
3907 surface->pending.buffer_viewport.buffer;
3908 sub->cached.buffer_viewport.surface =
3909 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003910
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003911 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003912
3913 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3914
3915 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3916
3917 wl_list_insert_list(&sub->cached.frame_callback_list,
3918 &surface->pending.frame_callback_list);
3919 wl_list_init(&surface->pending.frame_callback_list);
3920
Pekka Paalanen133e4392014-09-23 22:08:46 -04003921 wl_list_insert_list(&sub->cached.feedback_list,
3922 &surface->pending.feedback_list);
3923 wl_list_init(&surface->pending.feedback_list);
3924
Jason Ekstrand7b982072014-05-20 14:33:03 -05003925 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003926}
3927
Derek Foreman280e7dd2014-10-03 13:13:42 -05003928static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003929weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3930{
3931 while (sub) {
3932 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003933 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003934
3935 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003936 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003937
3938 sub = weston_surface_to_subsurface(sub->parent);
3939 }
3940
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003941 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003942}
3943
3944static void
3945weston_subsurface_commit(struct weston_subsurface *sub)
3946{
3947 struct weston_surface *surface = sub->surface;
3948 struct weston_subsurface *tmp;
3949
3950 /* Recursive check for effectively synchronized. */
3951 if (weston_subsurface_is_synchronized(sub)) {
3952 weston_subsurface_commit_to_cache(sub);
3953 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003954 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003955 /* flush accumulated state from cache */
3956 weston_subsurface_commit_to_cache(sub);
3957 weston_subsurface_commit_from_cache(sub);
3958 } else {
3959 weston_surface_commit(surface);
3960 }
3961
3962 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3963 if (tmp->surface != surface)
3964 weston_subsurface_parent_commit(tmp, 0);
3965 }
3966 }
3967}
3968
3969static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003970weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003971{
3972 struct weston_surface *surface = sub->surface;
3973 struct weston_subsurface *tmp;
3974
Pekka Paalanene67858b2013-04-25 13:57:42 +03003975 /* From now on, commit_from_cache the whole sub-tree, regardless of
3976 * the synchronized mode of each child. This sub-surface or some
3977 * of its ancestors were synchronized, so we are synchronized
3978 * all the way down.
3979 */
3980
Jason Ekstrand7b982072014-05-20 14:33:03 -05003981 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003982 weston_subsurface_commit_from_cache(sub);
3983
3984 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3985 if (tmp->surface != surface)
3986 weston_subsurface_parent_commit(tmp, 1);
3987 }
3988}
3989
3990static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003991weston_subsurface_parent_commit(struct weston_subsurface *sub,
3992 int parent_is_synchronized)
3993{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003994 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003995 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003996 wl_list_for_each(view, &sub->surface->views, surface_link)
3997 weston_view_set_position(view,
3998 sub->position.x,
3999 sub->position.y);
4000
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004001 sub->position.set = 0;
4002 }
4003
4004 if (parent_is_synchronized || sub->synchronized)
4005 weston_subsurface_synchronized_commit(sub);
4006}
4007
Pekka Paalanen8274d902014-08-06 19:36:51 +03004008static int
4009subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4010{
4011 return snprintf(buf, len, "sub-surface");
4012}
4013
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004014static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004015subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004016{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004017 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004018
Jason Ekstranda7af7042013-10-12 22:38:11 -05004019 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004020 weston_view_set_position(view,
4021 view->geometry.x + dx,
4022 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004023
4024 /* No need to check parent mappedness, because if parent is not
4025 * mapped, parent is not in a visible layer, so this sub-surface
4026 * will not be drawn either.
4027 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004028
Pekka Paalanene67858b2013-04-25 13:57:42 +03004029 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004030 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004031
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004032 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004033 * because that would call it also for the parent surface,
4034 * which might not be mapped yet. That would lead to
4035 * inconsistent state, where the window could never be
4036 * mapped.
4037 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004038 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004039 * weston_surface_is_mapped() return true, so that when the
4040 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004041 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004042 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004043 }
4044}
4045
4046static struct weston_subsurface *
4047weston_surface_to_subsurface(struct weston_surface *surface)
4048{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004049 if (surface->committed == subsurface_committed)
4050 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004051
4052 return NULL;
4053}
4054
Pekka Paalanen01388e22013-04-25 13:57:44 +03004055WL_EXPORT struct weston_surface *
4056weston_surface_get_main_surface(struct weston_surface *surface)
4057{
4058 struct weston_subsurface *sub;
4059
4060 while (surface && (sub = weston_surface_to_subsurface(surface)))
4061 surface = sub->parent;
4062
4063 return surface;
4064}
4065
Pekka Paalanen50b67472014-10-01 15:02:41 +03004066WL_EXPORT int
4067weston_surface_set_role(struct weston_surface *surface,
4068 const char *role_name,
4069 struct wl_resource *error_resource,
4070 uint32_t error_code)
4071{
4072 assert(role_name);
4073
4074 if (surface->role_name == NULL ||
4075 surface->role_name == role_name ||
4076 strcmp(surface->role_name, role_name) == 0) {
4077 surface->role_name = role_name;
4078
4079 return 0;
4080 }
4081
4082 wl_resource_post_error(error_resource, error_code,
4083 "Cannot assign role %s to wl_surface@%d,"
4084 " already has role %s\n",
4085 role_name,
4086 wl_resource_get_id(surface->resource),
4087 surface->role_name);
4088 return -1;
4089}
4090
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004091WL_EXPORT const char *
4092weston_surface_get_role(struct weston_surface *surface)
4093{
4094 return surface->role_name;
4095}
4096
Pekka Paalanen8274d902014-08-06 19:36:51 +03004097WL_EXPORT void
4098weston_surface_set_label_func(struct weston_surface *surface,
4099 int (*desc)(struct weston_surface *,
4100 char *, size_t))
4101{
4102 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004103 weston_timeline_refresh_subscription_objects(surface->compositor,
4104 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004105}
4106
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004107/** Get the size of surface contents
4108 *
4109 * \param surface The surface to query.
4110 * \param width Returns the width of raw contents.
4111 * \param height Returns the height of raw contents.
4112 *
4113 * Retrieves the raw surface content size in pixels for the given surface.
4114 * This is the whole content size in buffer pixels. If the surface
4115 * has no content or the renderer does not implement this feature,
4116 * zeroes are returned.
4117 *
4118 * This function is used to determine the buffer size needed for
4119 * a weston_surface_copy_content() call.
4120 */
4121WL_EXPORT void
4122weston_surface_get_content_size(struct weston_surface *surface,
4123 int *width, int *height)
4124{
4125 struct weston_renderer *rer = surface->compositor->renderer;
4126
4127 if (!rer->surface_get_content_size) {
4128 *width = 0;
4129 *height = 0;
4130 return;
4131 }
4132
4133 rer->surface_get_content_size(surface, width, height);
4134}
4135
Quentin Glidic248dd102016-08-12 10:41:34 +02004136/** Get the bounding box of a surface and its subsurfaces
4137 *
4138 * \param surface The surface to query.
4139 * \return The bounding box relative to the surface origin.
4140 *
4141 */
4142WL_EXPORT struct weston_geometry
4143weston_surface_get_bounding_box(struct weston_surface *surface)
4144{
4145 pixman_region32_t region;
4146 pixman_box32_t *box;
4147 struct weston_subsurface *subsurface;
4148
4149 pixman_region32_init_rect(&region,
4150 0, 0,
4151 surface->width, surface->height);
4152
4153 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4154 pixman_region32_union_rect(&region, &region,
4155 subsurface->position.x,
4156 subsurface->position.y,
4157 subsurface->surface->width,
4158 subsurface->surface->height);
4159
4160 box = pixman_region32_extents(&region);
4161 struct weston_geometry geometry = {
4162 .x = box->x1,
4163 .y = box->y1,
4164 .width = box->x2 - box->x1,
4165 .height = box->y2 - box->y1,
4166 };
4167
4168 pixman_region32_fini(&region);
4169
4170 return geometry;
4171}
4172
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004173/** Copy surface contents to system memory.
4174 *
4175 * \param surface The surface to copy from.
4176 * \param target Pointer to the target memory buffer.
4177 * \param size Size of the target buffer in bytes.
4178 * \param src_x X location on contents to copy from.
4179 * \param src_y Y location on contents to copy from.
4180 * \param width Width in pixels of the area to copy.
4181 * \param height Height in pixels of the area to copy.
4182 * \return 0 for success, -1 for failure.
4183 *
4184 * Surface contents are maintained by the renderer. They can be in a
4185 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4186 * else.
4187 *
4188 * Surface contents are copied into memory pointed to by target,
4189 * which has size bytes of space available. The target memory
4190 * may be larger than needed, but being smaller returns an error.
4191 * The extra bytes in target may or may not be written; their content is
4192 * unspecified. Size must be large enough to hold the image.
4193 *
4194 * The image in the target memory will be arranged in rows from
4195 * top to bottom, and pixels on a row from left to right. The pixel
4196 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4197 * width * 4.
4198 *
4199 * Parameters src_x and src_y define the upper-left corner in buffer
4200 * coordinates (pixels) to copy from. Parameters width and height
4201 * define the size of the area to copy in pixels.
4202 *
4203 * The rectangle defined by src_x, src_y, width, height must fit in
4204 * the surface contents. Otherwise an error is returned.
4205 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004206 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004207 * needed target buffer size and rectangle limits.
4208 *
4209 * CURRENT IMPLEMENTATION RESTRICTIONS:
4210 * - the machine must be little-endian due to Pixman formats.
4211 *
4212 * NOTE: Pixman formats are premultiplied.
4213 */
4214WL_EXPORT int
4215weston_surface_copy_content(struct weston_surface *surface,
4216 void *target, size_t size,
4217 int src_x, int src_y,
4218 int width, int height)
4219{
4220 struct weston_renderer *rer = surface->compositor->renderer;
4221 int cw, ch;
4222 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4223
4224 if (!rer->surface_copy_content)
4225 return -1;
4226
4227 weston_surface_get_content_size(surface, &cw, &ch);
4228
4229 if (src_x < 0 || src_y < 0)
4230 return -1;
4231
4232 if (width <= 0 || height <= 0)
4233 return -1;
4234
4235 if (src_x + width > cw || src_y + height > ch)
4236 return -1;
4237
4238 if (width * bytespp * height > size)
4239 return -1;
4240
4241 return rer->surface_copy_content(surface, target, size,
4242 src_x, src_y, width, height);
4243}
4244
Pekka Paalanene67858b2013-04-25 13:57:42 +03004245static void
4246subsurface_set_position(struct wl_client *client,
4247 struct wl_resource *resource, int32_t x, int32_t y)
4248{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004249 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004250
4251 if (!sub)
4252 return;
4253
4254 sub->position.x = x;
4255 sub->position.y = y;
4256 sub->position.set = 1;
4257}
4258
4259static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004260subsurface_find_sibling(struct weston_subsurface *sub,
4261 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004262{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004263 struct weston_surface *parent = sub->parent;
4264 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004265
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004266 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4267 if (sibling->surface == surface && sibling != sub)
4268 return sibling;
4269 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004270
4271 return NULL;
4272}
4273
4274static struct weston_subsurface *
4275subsurface_sibling_check(struct weston_subsurface *sub,
4276 struct weston_surface *surface,
4277 const char *request)
4278{
4279 struct weston_subsurface *sibling;
4280
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004281 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004282 if (!sibling) {
4283 wl_resource_post_error(sub->resource,
4284 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4285 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004286 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004287 return NULL;
4288 }
4289
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004290 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004291
4292 return sibling;
4293}
4294
4295static void
4296subsurface_place_above(struct wl_client *client,
4297 struct wl_resource *resource,
4298 struct wl_resource *sibling_resource)
4299{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004300 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004301 struct weston_surface *surface =
4302 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004303 struct weston_subsurface *sibling;
4304
4305 if (!sub)
4306 return;
4307
4308 sibling = subsurface_sibling_check(sub, surface, "place_above");
4309 if (!sibling)
4310 return;
4311
4312 wl_list_remove(&sub->parent_link_pending);
4313 wl_list_insert(sibling->parent_link_pending.prev,
4314 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004315
4316 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004317}
4318
4319static void
4320subsurface_place_below(struct wl_client *client,
4321 struct wl_resource *resource,
4322 struct wl_resource *sibling_resource)
4323{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004324 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004325 struct weston_surface *surface =
4326 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004327 struct weston_subsurface *sibling;
4328
4329 if (!sub)
4330 return;
4331
4332 sibling = subsurface_sibling_check(sub, surface, "place_below");
4333 if (!sibling)
4334 return;
4335
4336 wl_list_remove(&sub->parent_link_pending);
4337 wl_list_insert(&sibling->parent_link_pending,
4338 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004339
4340 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004341}
4342
4343static void
4344subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4345{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004346 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004347
4348 if (sub)
4349 sub->synchronized = 1;
4350}
4351
4352static void
4353subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4354{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004355 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004356
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004357 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004358 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004359
4360 /* If sub became effectively desynchronized, flush. */
4361 if (!weston_subsurface_is_synchronized(sub))
4362 weston_subsurface_synchronized_commit(sub);
4363 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004364}
4365
4366static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004367weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4368{
4369 wl_list_remove(&sub->parent_link);
4370 wl_list_remove(&sub->parent_link_pending);
4371 wl_list_remove(&sub->parent_destroy_listener.link);
4372 sub->parent = NULL;
4373}
4374
4375static void
4376weston_subsurface_destroy(struct weston_subsurface *sub);
4377
4378static void
4379subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4380{
4381 struct weston_subsurface *sub =
4382 container_of(listener, struct weston_subsurface,
4383 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004384 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004385
4386 /* The protocol object (wl_resource) is left inert. */
4387 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004388 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004389
4390 weston_subsurface_destroy(sub);
4391}
4392
4393static void
4394subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4395{
4396 struct weston_subsurface *sub =
4397 container_of(listener, struct weston_subsurface,
4398 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004399 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004400 assert(sub->surface != sub->parent);
4401
4402 if (weston_surface_is_mapped(sub->surface))
4403 weston_surface_unmap(sub->surface);
4404
4405 weston_subsurface_unlink_parent(sub);
4406}
4407
4408static void
4409subsurface_resource_destroy(struct wl_resource *resource)
4410{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004411 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004412
4413 if (sub)
4414 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004415}
4416
4417static void
4418subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4419{
4420 wl_resource_destroy(resource);
4421}
4422
4423static void
4424weston_subsurface_link_parent(struct weston_subsurface *sub,
4425 struct weston_surface *parent)
4426{
4427 sub->parent = parent;
4428 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004429 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004430 &sub->parent_destroy_listener);
4431
4432 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4433 wl_list_insert(&parent->subsurface_list_pending,
4434 &sub->parent_link_pending);
4435}
4436
4437static void
4438weston_subsurface_link_surface(struct weston_subsurface *sub,
4439 struct weston_surface *surface)
4440{
4441 sub->surface = surface;
4442 sub->surface_destroy_listener.notify =
4443 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004444 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004445 &sub->surface_destroy_listener);
4446}
4447
4448static void
4449weston_subsurface_destroy(struct weston_subsurface *sub)
4450{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004451 struct weston_view *view, *next;
4452
Pekka Paalanene67858b2013-04-25 13:57:42 +03004453 assert(sub->surface);
4454
4455 if (sub->resource) {
4456 assert(weston_surface_to_subsurface(sub->surface) == sub);
4457 assert(sub->parent_destroy_listener.notify ==
4458 subsurface_handle_parent_destroy);
4459
George Kiagiadakised04d382014-06-13 18:10:26 +02004460 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4461 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004462 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004463 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004464
Pekka Paalanene67858b2013-04-25 13:57:42 +03004465 if (sub->parent)
4466 weston_subsurface_unlink_parent(sub);
4467
Jason Ekstrand7b982072014-05-20 14:33:03 -05004468 weston_surface_state_fini(&sub->cached);
4469 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004470
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004471 sub->surface->committed = NULL;
4472 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004473 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004474 } else {
4475 /* the dummy weston_subsurface for the parent itself */
4476 assert(sub->parent_destroy_listener.notify == NULL);
4477 wl_list_remove(&sub->parent_link);
4478 wl_list_remove(&sub->parent_link_pending);
4479 }
4480
4481 wl_list_remove(&sub->surface_destroy_listener.link);
4482 free(sub);
4483}
4484
4485static const struct wl_subsurface_interface subsurface_implementation = {
4486 subsurface_destroy,
4487 subsurface_set_position,
4488 subsurface_place_above,
4489 subsurface_place_below,
4490 subsurface_set_sync,
4491 subsurface_set_desync
4492};
4493
4494static struct weston_subsurface *
4495weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4496 struct weston_surface *parent)
4497{
4498 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004499 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004500
Bryce Harringtonde16d892014-11-20 22:21:57 -08004501 sub = zalloc(sizeof *sub);
4502 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004503 return NULL;
4504
Jason Ekstranda7af7042013-10-12 22:38:11 -05004505 wl_list_init(&sub->unused_views);
4506
Jason Ekstranda85118c2013-06-27 20:17:02 -05004507 sub->resource =
4508 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004509 if (!sub->resource) {
4510 free(sub);
4511 return NULL;
4512 }
4513
Jason Ekstranda85118c2013-06-27 20:17:02 -05004514 wl_resource_set_implementation(sub->resource,
4515 &subsurface_implementation,
4516 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004517 weston_subsurface_link_surface(sub, surface);
4518 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004519 weston_surface_state_init(&sub->cached);
4520 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004521 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004522
4523 return sub;
4524}
4525
4526/* Create a dummy subsurface for having the parent itself in its
4527 * sub-surface lists. Makes stacking order manipulation easy.
4528 */
4529static struct weston_subsurface *
4530weston_subsurface_create_for_parent(struct weston_surface *parent)
4531{
4532 struct weston_subsurface *sub;
4533
Bryce Harringtonde16d892014-11-20 22:21:57 -08004534 sub = zalloc(sizeof *sub);
4535 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004536 return NULL;
4537
4538 weston_subsurface_link_surface(sub, parent);
4539 sub->parent = parent;
4540 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4541 wl_list_insert(&parent->subsurface_list_pending,
4542 &sub->parent_link_pending);
4543
4544 return sub;
4545}
4546
4547static void
4548subcompositor_get_subsurface(struct wl_client *client,
4549 struct wl_resource *resource,
4550 uint32_t id,
4551 struct wl_resource *surface_resource,
4552 struct wl_resource *parent_resource)
4553{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004554 struct weston_surface *surface =
4555 wl_resource_get_user_data(surface_resource);
4556 struct weston_surface *parent =
4557 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004558 struct weston_subsurface *sub;
4559 static const char where[] = "get_subsurface: wl_subsurface@";
4560
4561 if (surface == parent) {
4562 wl_resource_post_error(resource,
4563 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4564 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004565 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004566 return;
4567 }
4568
4569 if (weston_surface_to_subsurface(surface)) {
4570 wl_resource_post_error(resource,
4571 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4572 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004573 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004574 return;
4575 }
4576
Pekka Paalanen50b67472014-10-01 15:02:41 +03004577 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4578 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004579 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004580
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004581 if (weston_surface_get_main_surface(parent) == surface) {
4582 wl_resource_post_error(resource,
4583 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4584 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004585 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004586 return;
4587 }
4588
Pekka Paalanene67858b2013-04-25 13:57:42 +03004589 /* make sure the parent is in its own list */
4590 if (wl_list_empty(&parent->subsurface_list)) {
4591 if (!weston_subsurface_create_for_parent(parent)) {
4592 wl_resource_post_no_memory(resource);
4593 return;
4594 }
4595 }
4596
4597 sub = weston_subsurface_create(id, surface, parent);
4598 if (!sub) {
4599 wl_resource_post_no_memory(resource);
4600 return;
4601 }
4602
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004603 surface->committed = subsurface_committed;
4604 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004605 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004606}
4607
4608static void
4609subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4610{
4611 wl_resource_destroy(resource);
4612}
4613
4614static const struct wl_subcompositor_interface subcompositor_interface = {
4615 subcompositor_destroy,
4616 subcompositor_get_subsurface
4617};
4618
4619static void
4620bind_subcompositor(struct wl_client *client,
4621 void *data, uint32_t version, uint32_t id)
4622{
4623 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004624 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004625
Jason Ekstranda85118c2013-06-27 20:17:02 -05004626 resource =
4627 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004628 if (resource == NULL) {
4629 wl_client_post_no_memory(client);
4630 return;
4631 }
4632 wl_resource_set_implementation(resource, &subcompositor_interface,
4633 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004634}
4635
Bryce Harrington0795ece2016-08-30 12:04:26 -07004636/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004637 *
4638 * \param compositor The compositor instance
4639 * \param state The DPMS state the outputs will be set to
4640 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004641static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004642weston_compositor_dpms(struct weston_compositor *compositor,
4643 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004644{
4645 struct weston_output *output;
4646
Bryce Harrington08976ac2016-08-30 12:05:16 -07004647 wl_list_for_each(output, &compositor->output_list, link)
4648 if (output->set_dpms)
4649 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004650}
4651
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004652/** Restores the compositor to active status
4653 *
4654 * \param compositor The compositor instance
4655 *
4656 * If the compositor was in a sleeping mode, all outputs are powered
4657 * back on via DPMS. Otherwise if the compositor was inactive
4658 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4659 * signal will fire.
4660 *
4661 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004662 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004663 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004664WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004665weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004666{
Neil Roberts8b62e202013-09-30 13:14:47 +01004667 uint32_t old_state = compositor->state;
4668
4669 /* The state needs to be changed before emitting the wake
4670 * signal because that may try to schedule a repaint which
4671 * will not work if the compositor is still sleeping */
4672 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4673
4674 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004675 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004676 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004677 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004678 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004679 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004680 /* fall through */
4681 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004682 wl_event_source_timer_update(compositor->idle_source,
4683 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004684 }
4685}
4686
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004687/** Turns off rendering and frame events for the compositor.
4688 *
4689 * \param compositor The compositor instance
4690 *
4691 * This is used for example to prevent further rendering while the
4692 * compositor is shutting down.
4693 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004694 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004695 *
4696 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004697 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004698WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004699weston_compositor_offscreen(struct weston_compositor *compositor)
4700{
4701 switch (compositor->state) {
4702 case WESTON_COMPOSITOR_OFFSCREEN:
4703 return;
4704 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004705 default:
4706 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4707 wl_event_source_timer_update(compositor->idle_source, 0);
4708 }
4709}
4710
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004711/** Powers down all attached output devices
4712 *
4713 * \param compositor The compositor instance
4714 *
4715 * Causes rendering to the outputs to cease, and no frame events to be
4716 * sent. Only powers down the outputs if the compositor is not already
4717 * in sleep mode.
4718 *
4719 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004720 *
4721 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004722 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004723WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004724weston_compositor_sleep(struct weston_compositor *compositor)
4725{
4726 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4727 return;
4728
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004729 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004730 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4731 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4732}
4733
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004734/** Sets compositor to idle mode
4735 *
4736 * \param data The compositor instance
4737 *
4738 * This is called when the idle timer fires. Once the compositor is in
4739 * idle mode it requires a wake action (e.g. via
4740 * weston_compositor_wake()) to restore it. The compositor's
4741 * idle_signal will be triggered when the idle event occurs.
4742 *
4743 * Idleness can be inhibited by setting the compositor's idle_inhibit
4744 * property.
4745 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004746static int
4747idle_handler(void *data)
4748{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004749 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004750
4751 if (compositor->idle_inhibit)
4752 return 1;
4753
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004754 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004755 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004756
4757 return 1;
4758}
4759
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004760WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004761weston_plane_init(struct weston_plane *plane,
4762 struct weston_compositor *ec,
4763 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004764{
4765 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004766 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004767 plane->x = x;
4768 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004769 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004770
4771 /* Init the link so that the call to wl_list_remove() when releasing
4772 * the plane without ever stacking doesn't lead to a crash */
4773 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004774}
4775
4776WL_EXPORT void
4777weston_plane_release(struct weston_plane *plane)
4778{
Xiong Zhang97116532013-10-23 13:58:31 +08004779 struct weston_view *view;
4780
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004781 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004782 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004783
Xiong Zhang97116532013-10-23 13:58:31 +08004784 wl_list_for_each(view, &plane->compositor->view_list, link) {
4785 if (view->plane == plane)
4786 view->plane = NULL;
4787 }
4788
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004789 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004790}
4791
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004792/** weston_compositor_stack_plane
4793 * \ingroup compositor
4794 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004795WL_EXPORT void
4796weston_compositor_stack_plane(struct weston_compositor *ec,
4797 struct weston_plane *plane,
4798 struct weston_plane *above)
4799{
4800 if (above)
4801 wl_list_insert(above->link.prev, &plane->link);
4802 else
4803 wl_list_insert(&ec->plane_list, &plane->link);
4804}
4805
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004806static void
4807output_release(struct wl_client *client, struct wl_resource *resource)
4808{
4809 wl_resource_destroy(resource);
4810}
4811
4812static const struct wl_output_interface output_interface = {
4813 output_release,
4814};
4815
4816
Casey Dahlin9074db52012-04-19 22:50:09 -04004817static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004818{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004819 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004820}
4821
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004822static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004823bind_output(struct wl_client *client,
4824 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004825{
Pekka Paalanen05347622017-03-27 12:24:34 +03004826 struct weston_head *head = data;
4827 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004828 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004829 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004830
Jason Ekstranda85118c2013-06-27 20:17:02 -05004831 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004832 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004833 if (resource == NULL) {
4834 wl_client_post_no_memory(client);
4835 return;
4836 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004837
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004838 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004839 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004840 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004841
Pekka Paalanen05347622017-03-27 12:24:34 +03004842 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004843 wl_output_send_geometry(resource,
4844 output->x,
4845 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004846 head->mm_width,
4847 head->mm_height,
4848 head->subpixel,
4849 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004850 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004851 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004852 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004853 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004854
4855 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004856 wl_output_send_mode(resource,
4857 mode->flags,
4858 mode->width,
4859 mode->height,
4860 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004861 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004862
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004863 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004864 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004865}
4866
Pekka Paalanendcac3512017-12-08 14:13:34 +02004867static void
4868weston_head_add_global(struct weston_head *head)
4869{
4870 head->global = wl_global_create(head->compositor->wl_display,
4871 &wl_output_interface, 3,
4872 head, bind_output);
4873}
4874
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004875/** Remove the global wl_output protocol object
4876 *
4877 * \param head The head whose global to remove.
4878 *
4879 * Also orphans the wl_resources for this head (wl_output).
4880 */
4881static void
4882weston_head_remove_global(struct weston_head *head)
4883{
4884 struct wl_resource *resource, *tmp;
4885
4886 if (head->global)
4887 wl_global_destroy(head->global);
4888 head->global = NULL;
4889
4890 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4891 unbind_resource(resource);
4892 wl_resource_set_destructor(resource, NULL);
4893 wl_resource_set_user_data(resource, NULL);
4894 }
Roman Gilge97391c2019-03-29 13:01:06 +01004895
4896 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
4897 /* It's sufficient to unset the destructor, then the list elements
4898 * won't be accessed.
4899 */
4900 wl_resource_set_destructor(resource, NULL);
4901 }
4902 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004903}
4904
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004905/** Get the backing object of wl_output
4906 *
4907 * \param resource A wl_output protocol object.
4908 * \return The backing object (user data) of a wl_resource representing a
4909 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004910 *
4911 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004912 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004913WL_EXPORT struct weston_head *
4914weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004915{
4916 assert(wl_resource_instance_of(resource, &wl_output_interface,
4917 &output_interface));
4918
4919 return wl_resource_get_user_data(resource);
4920}
4921
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004922/** Initialize a pre-allocated weston_head
4923 *
4924 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004925 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004926 *
4927 * The head will be safe to attach, detach and release.
4928 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004929 * The name is used in logs, and can be used by compositors as a configuration
4930 * identifier.
4931 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004932 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004933 * \internal
4934 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004935WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004936weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004937{
4938 /* Add some (in)sane defaults which can be used
4939 * for checking if an output was properly configured
4940 */
4941 memset(head, 0, sizeof *head);
4942
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004943 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004944 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004945 wl_list_init(&head->output_link);
4946 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01004947 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004948 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05304949 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004950}
4951
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004952/** Send output heads changed signal
4953 *
4954 * \param output The output that changed.
4955 *
4956 * Notify that the enabled output gained and/or lost heads, or that the
4957 * associated heads may have changed their connection status. This does not
4958 * include cases where the output becomes enabled or disabled. The registered
4959 * callbacks are called after the change has successfully happened.
4960 *
4961 * If connection status change causes the compositor to attach or detach a head
4962 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03004963 *
4964 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004965 */
4966static void
4967weston_output_emit_heads_changed(struct weston_output *output)
4968{
4969 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4970 output);
4971}
4972
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004973/** Idle task for emitting heads_changed_signal */
4974static void
4975weston_compositor_call_heads_changed(void *data)
4976{
4977 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004978 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004979
4980 compositor->heads_changed_source = NULL;
4981
4982 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004983
4984 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4985 if (head->output && head->output->enabled)
4986 weston_output_emit_heads_changed(head->output);
4987 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004988}
4989
4990/** Schedule a call on idle to heads_changed callback
4991 *
4992 * \param compositor The Compositor.
4993 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004994 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004995 * \internal
4996 */
4997static void
4998weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4999{
5000 struct wl_event_loop *loop;
5001
5002 if (compositor->heads_changed_source)
5003 return;
5004
5005 loop = wl_display_get_event_loop(compositor->wl_display);
5006 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5007 weston_compositor_call_heads_changed, compositor);
5008}
5009
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005010/** Register a new head
5011 *
5012 * \param compositor The compositor.
5013 * \param head The head to register, must not be already registered.
5014 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005015 * This signals the core that a new head has become available, leading to
5016 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005017 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005018 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005019 * \internal
5020 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005021WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005022weston_compositor_add_head(struct weston_compositor *compositor,
5023 struct weston_head *head)
5024{
5025 assert(wl_list_empty(&head->compositor_link));
5026 assert(head->name);
5027
5028 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5029 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005030 weston_compositor_schedule_heads_changed(compositor);
5031}
5032
5033/** Adds a listener to be called when heads change
5034 *
5035 * \param compositor The compositor.
5036 * \param listener The listener to add.
5037 *
Marius Vlada2dace22019-06-12 16:05:44 +03005038 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005039 *
5040 * The listener function will be called after heads are added or their
5041 * connection status has changed. Several changes may be accumulated into a
5042 * single call. The user is expected to iterate over the existing heads and
5043 * check their statuses to find out what changed.
5044 *
5045 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5046 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005047 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005048 */
5049WL_EXPORT void
5050weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5051 struct wl_listener *listener)
5052{
5053 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005054}
5055
5056/** Iterate over available heads
5057 *
5058 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005059 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005060 * \return The next available head in the list.
5061 *
5062 * Returns all available heads, regardless of being connected or enabled.
5063 *
5064 * You can iterate over all heads as follows:
5065 * \code
5066 * struct weston_head *head = NULL;
5067 *
5068 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5069 * ...
5070 * }
5071 * \endcode
5072 *
5073 * If you cause \c iter to be removed from the list, you cannot use it to
5074 * continue iterating. Removing any other item is safe.
5075 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005076 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005077 */
5078WL_EXPORT struct weston_head *
5079weston_compositor_iterate_heads(struct weston_compositor *compositor,
5080 struct weston_head *iter)
5081{
5082 struct wl_list *list = &compositor->head_list;
5083 struct wl_list *node;
5084
5085 assert(compositor);
5086 assert(!iter || iter->compositor == compositor);
5087
5088 if (iter)
5089 node = iter->compositor_link.next;
5090 else
5091 node = list->next;
5092
5093 assert(node);
5094 assert(!iter || node != &iter->compositor_link);
5095
5096 if (node == list)
5097 return NULL;
5098
5099 return container_of(node, struct weston_head, compositor_link);
5100}
5101
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005102/** Iterate over attached heads
5103 *
5104 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005105 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005106 * \return The next attached head in the list.
5107 *
5108 * Returns all heads currently attached to the output.
5109 *
5110 * You can iterate over all heads as follows:
5111 * \code
5112 * struct weston_head *head = NULL;
5113 *
5114 * while ((head = weston_output_iterate_heads(output, head))) {
5115 * ...
5116 * }
5117 * \endcode
5118 *
5119 * If you cause \c iter to be removed from the list, you cannot use it to
5120 * continue iterating. Removing any other item is safe.
5121 *
Marius Vlad55d87362019-06-11 01:15:35 +03005122 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005123 */
5124WL_EXPORT struct weston_head *
5125weston_output_iterate_heads(struct weston_output *output,
5126 struct weston_head *iter)
5127{
5128 struct wl_list *list = &output->head_list;
5129 struct wl_list *node;
5130
5131 assert(output);
5132 assert(!iter || iter->output == output);
5133
5134 if (iter)
5135 node = iter->output_link.next;
5136 else
5137 node = list->next;
5138
5139 assert(node);
5140 assert(!iter || node != &iter->output_link);
5141
5142 if (node == list)
5143 return NULL;
5144
5145 return container_of(node, struct weston_head, output_link);
5146}
5147
Pekka Paalanendcac3512017-12-08 14:13:34 +02005148/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005149 *
5150 * \param output The output to attach to.
5151 * \param head The head that is not yet attached.
5152 * \return 0 on success, -1 on failure.
5153 *
5154 * Attaches the given head to the output. All heads of an output are clones
5155 * and share the resolution and timings.
5156 *
5157 * Cloning heads this way uses less resources than creating an output for
5158 * each head, but is not always possible due to environment, driver and hardware
5159 * limitations.
5160 *
5161 * On failure, the head remains unattached. Success of this function does not
5162 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005163 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005164 *
Marius Vlad55d87362019-06-11 01:15:35 +03005165 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005166 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005167WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005168weston_output_attach_head(struct weston_output *output,
5169 struct weston_head *head)
5170{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005171 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005172
5173 if (!wl_list_empty(&head->output_link))
5174 return -1;
5175
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005176 if (output->attach_head) {
5177 if (output->attach_head(output, head) < 0)
5178 return -1;
5179 } else if (!wl_list_empty(&output->head_list)) {
5180 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005181 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005182 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005183
5184 head->output = output;
5185 wl_list_insert(output->head_list.prev, &head->output_link);
5186
Pekka Paalanendcac3512017-12-08 14:13:34 +02005187 if (output->enabled) {
5188 weston_head_add_global(head);
5189
5190 head_names = weston_output_create_heads_string(output);
5191 weston_log("Output '%s' updated to have head(s) %s\n",
5192 output->name, head_names);
5193 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005194
5195 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005196 }
5197
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005198 return 0;
5199}
5200
5201/** Detach a head from its output
5202 *
5203 * \param head The head to detach.
5204 *
5205 * It is safe to detach a non-attached head.
5206 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005207 * If the head is attached to an enabled output and the output will be left
5208 * with no heads, the output will be disabled.
5209 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005210 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005211 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005212 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005213WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005214weston_head_detach(struct weston_head *head)
5215{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005216 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005217 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005218
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005219 wl_list_remove(&head->output_link);
5220 wl_list_init(&head->output_link);
5221 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005222
5223 if (!output)
5224 return;
5225
5226 if (output->detach_head)
5227 output->detach_head(output, head);
5228
5229 if (output->enabled) {
5230 weston_head_remove_global(head);
5231
Pekka Paalanena0106992017-12-08 16:11:17 +02005232 if (wl_list_empty(&output->head_list)) {
5233 weston_log("Output '%s' no heads left, disabling.\n",
5234 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005235 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005236 } else {
5237 head_names = weston_output_create_heads_string(output);
5238 weston_log("Output '%s' updated to have head(s) %s\n",
5239 output->name, head_names);
5240 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005241
5242 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005243 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005244 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005245}
5246
5247/** Destroy a head
5248 *
5249 * \param head The head to be released.
5250 *
5251 * Destroys the head. The caller is responsible for freeing the memory pointed
5252 * to by \c head.
5253 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005254 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005255 * \internal
5256 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005257WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005258weston_head_release(struct weston_head *head)
5259{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005260 wl_signal_emit(&head->destroy_signal, head);
5261
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005262 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005263
5264 free(head->make);
5265 free(head->model);
5266 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005267 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005268
5269 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005270}
5271
Pekka Paalanene19970f2017-08-28 14:11:02 +03005272static void
5273weston_head_set_device_changed(struct weston_head *head)
5274{
5275 head->device_changed = true;
5276
5277 if (head->compositor)
5278 weston_compositor_schedule_heads_changed(head->compositor);
5279}
5280
5281/** String equal comparison with NULLs being equal */
5282static bool
5283str_null_eq(const char *a, const char *b)
5284{
5285 if (!a && !b)
5286 return true;
5287
5288 if (!!a != !!b)
5289 return false;
5290
5291 return strcmp(a, b) == 0;
5292}
5293
Pekka Paalanen01f60212017-03-24 15:39:24 +02005294/** Store monitor make, model and serial number
5295 *
5296 * \param head The head to modify.
5297 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5298 * any string, or NULL for none.
5299 * \param model The monitor model or name, or a made-up string, or NULL for
5300 * none.
5301 * \param serialno The monitor serial number, a made-up string, or NULL for
5302 * none.
5303 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005304 * This may set the device_changed flag.
5305 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005306 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005307 * \internal
5308 */
5309WL_EXPORT void
5310weston_head_set_monitor_strings(struct weston_head *head,
5311 const char *make,
5312 const char *model,
5313 const char *serialno)
5314{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005315 if (str_null_eq(head->make, make) &&
5316 str_null_eq(head->model, model) &&
5317 str_null_eq(head->serial_number, serialno))
5318 return;
5319
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005320 free(head->make);
5321 free(head->model);
5322 free(head->serial_number);
5323
5324 head->make = make ? strdup(make) : NULL;
5325 head->model = model ? strdup(model) : NULL;
5326 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005327
5328 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005329}
5330
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005331/** Store display non-desktop status
5332 *
5333 * \param head The head to modify.
5334 * \param non_desktop Whether the head connects to a non-desktop display.
5335 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005336 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005337 * \internal
5338 */
5339WL_EXPORT void
5340weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5341{
5342 if (head->non_desktop == non_desktop)
5343 return;
5344
5345 head->non_desktop = non_desktop;
5346
5347 weston_head_set_device_changed(head);
5348}
5349
Pekka Paalanen01f60212017-03-24 15:39:24 +02005350/** Store physical image size
5351 *
5352 * \param head The head to modify.
5353 * \param mm_width Image area width in millimeters.
5354 * \param mm_height Image area height in millimeters.
5355 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005356 * This may set the device_changed flag.
5357 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005358 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005359 * \internal
5360 */
5361WL_EXPORT void
5362weston_head_set_physical_size(struct weston_head *head,
5363 int32_t mm_width, int32_t mm_height)
5364{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005365 if (head->mm_width == mm_width &&
5366 head->mm_height == mm_height)
5367 return;
5368
Pekka Paalanen01f60212017-03-24 15:39:24 +02005369 head->mm_width = mm_width;
5370 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005371
5372 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005373}
5374
5375/** Store monitor sub-pixel layout
5376 *
5377 * \param head The head to modify.
5378 * \param sp Sub-pixel layout. The possible values are:
5379 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5380 * - WL_OUTPUT_SUBPIXEL_NONE,
5381 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5382 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5383 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5384 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5385 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005386 * This may set the device_changed flag.
5387 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005388 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005389 * \internal
5390 */
5391WL_EXPORT void
5392weston_head_set_subpixel(struct weston_head *head,
5393 enum wl_output_subpixel sp)
5394{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005395 if (head->subpixel == sp)
5396 return;
5397
Pekka Paalanen01f60212017-03-24 15:39:24 +02005398 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005399
5400 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005401}
5402
5403/** Mark the monitor as internal
5404 *
5405 * This is used for embedded screens, like laptop panels.
5406 *
5407 * \param head The head to mark as internal.
5408 *
5409 * By default a head is external. The type is often inferred from the physical
5410 * connector type.
5411 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005412 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005413 * \internal
5414 */
5415WL_EXPORT void
5416weston_head_set_internal(struct weston_head *head)
5417{
5418 head->connection_internal = true;
5419}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005420
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005421/** Store connector status
5422 *
5423 * \param head The head to modify.
5424 * \param connected Whether the head is connected.
5425 *
5426 * Connectors are created as disconnected. This function can be used to
5427 * set the connector status.
5428 *
5429 * The status should be set to true when a physical connector is connected to
5430 * a video sink device like a monitor and to false when the connector is
5431 * disconnected. For nested backends, the connection status should reflect the
5432 * connection to the parent display server.
5433 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005434 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005435 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005436 *
5437 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005438 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005439 * \internal
5440 */
5441WL_EXPORT void
5442weston_head_set_connection_status(struct weston_head *head, bool connected)
5443{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005444 if (head->connected == connected)
5445 return;
5446
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005447 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005448
Pekka Paalanene19970f2017-08-28 14:11:02 +03005449 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005450}
5451
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305452static void
5453weston_output_compute_protection(struct weston_output *output)
5454{
5455 struct weston_head *head;
5456 enum weston_hdcp_protection op_protection;
5457 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305458 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305459
5460 wl_list_for_each(head, &output->head_list, output_link) {
5461 if (!op_protection_valid) {
5462 op_protection = head->current_protection;
5463 op_protection_valid = true;
5464 }
5465 if (head->current_protection < op_protection)
5466 op_protection = head->current_protection;
5467 }
5468
5469 if (!op_protection_valid)
5470 op_protection = WESTON_HDCP_DISABLE;
5471
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305472 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305473 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305474 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305475 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305476 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305477}
5478
5479WL_EXPORT void
5480weston_head_set_content_protection_status(struct weston_head *head,
5481 enum weston_hdcp_protection status)
5482{
5483 head->current_protection = status;
5484 if (head->output)
5485 weston_output_compute_protection(head->output);
5486}
5487
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005488/** Is the head currently connected?
5489 *
5490 * \param head The head to query.
5491 * \return Connection status.
5492 *
5493 * Returns true if the head is physically connected to a monitor, or in
5494 * case of a nested backend returns true when there is a connection to the
5495 * parent display server.
5496 *
5497 * This is independent from the head being enabled.
5498 *
5499 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005500 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005501 */
5502WL_EXPORT bool
5503weston_head_is_connected(struct weston_head *head)
5504{
5505 return head->connected;
5506}
5507
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005508/** Is the head currently enabled?
5509 *
5510 * \param head The head to query.
5511 * \return Video status.
5512 *
5513 * Returns true if the head is currently transmitting a video stream.
5514 *
5515 * This is independent of the head being connected.
5516 *
5517 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005518 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005519 */
5520WL_EXPORT bool
5521weston_head_is_enabled(struct weston_head *head)
5522{
5523 if (!head->output)
5524 return false;
5525
5526 return head->output->enabled;
5527}
5528
Pekka Paalanene19970f2017-08-28 14:11:02 +03005529/** Has the device information changed?
5530 *
5531 * \param head The head to query.
5532 * \return True if the device information has changed since last reset.
5533 *
5534 * The information about the connected display device, e.g. a monitor, may
5535 * change without being disconnected in between. Changing information
5536 * causes a call to the heads_changed hook.
5537 *
5538 * The information includes make, model, serial number, physical size,
5539 * and sub-pixel type. The connection status is also included.
5540 *
5541 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005542 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005543 */
5544WL_EXPORT bool
5545weston_head_is_device_changed(struct weston_head *head)
5546{
5547 return head->device_changed;
5548}
5549
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005550/** Does the head represent a non-desktop display?
5551 *
5552 * \param head The head to query.
5553 * \return True if the device is a non-desktop display.
5554 *
5555 * Non-desktop heads are not attached to outputs by default.
5556 * This stops weston from extending the desktop onto head mounted displays.
5557 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005558 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005559 */
5560WL_EXPORT bool
5561weston_head_is_non_desktop(struct weston_head *head)
5562{
5563 return head->non_desktop;
5564}
5565
Pekka Paalanene19970f2017-08-28 14:11:02 +03005566/** Acknowledge device information change
5567 *
5568 * \param head The head to acknowledge.
5569 *
5570 * Clears the device changed flag on this head. When a compositor has processed
5571 * device information, it should call this to be able to notice further
5572 * changes.
5573 *
5574 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005575 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005576 */
5577WL_EXPORT void
5578weston_head_reset_device_changed(struct weston_head *head)
5579{
5580 head->device_changed = false;
5581}
5582
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005583/** Get the name of a head
5584 *
5585 * \param head The head to query.
5586 * \return The head's name, not NULL.
5587 *
5588 * The name depends on the backend. The DRM backend uses connector names,
5589 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005590 *
5591 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005592 */
5593WL_EXPORT const char *
5594weston_head_get_name(struct weston_head *head)
5595{
5596 return head->name;
5597}
5598
5599/** Get the output the head is attached to
5600 *
5601 * \param head The head to query.
5602 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005603 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005604 */
5605WL_EXPORT struct weston_output *
5606weston_head_get_output(struct weston_head *head)
5607{
5608 return head->output;
5609}
5610
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005611/** Add destroy callback for a head
5612 *
5613 * \param head The head to watch for.
5614 * \param listener The listener to add. The \c notify member must be set.
5615 *
5616 * Heads may get destroyed for various reasons by the backends. If a head is
5617 * attached to an output, the compositor should listen for head destruction
5618 * and reconfigure or destroy the output if necessary.
5619 *
5620 * The destroy callbacks will be called on weston_head destruction before any
5621 * automatic detaching from an associated weston_output and before any
5622 * weston_head information is lost.
5623 *
5624 * The \c data argument to the notify callback is the weston_head being
5625 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005626 *
5627 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005628 */
5629WL_EXPORT void
5630weston_head_add_destroy_listener(struct weston_head *head,
5631 struct wl_listener *listener)
5632{
5633 wl_signal_add(&head->destroy_signal, listener);
5634}
5635
5636/** Look up destroy listener for a head
5637 *
5638 * \param head The head to query.
5639 * \param notify The notify function used used for the added destroy listener.
5640 * \return The listener, or NULL if not found.
5641 *
5642 * This looks up the previously added destroy listener struct based on the
5643 * notify function it has. The listener can be used to access user data
5644 * through \c container_of().
5645 *
5646 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005647 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005648 */
5649WL_EXPORT struct wl_listener *
5650weston_head_get_destroy_listener(struct weston_head *head,
5651 wl_notify_func_t notify)
5652{
5653 return wl_signal_get(&head->destroy_signal, notify);
5654}
5655
David Fort0de859e2016-05-27 23:22:57 +02005656/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005657static void
David Fort0de859e2016-05-27 23:22:57 +02005658weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5659 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005660{
5661 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005662 bool start_resizing = false;
5663
5664 if (!delta_width)
5665 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005666
5667 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005668 if (output == resized_output) {
5669 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005670 continue;
5671 }
5672
David Fort0de859e2016-05-27 23:22:57 +02005673 if (start_resizing) {
5674 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005675 output->dirty = 1;
5676 }
5677 }
5678}
5679
Pekka Paalanend72bad22017-03-29 17:01:41 +03005680static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005681weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005682{
Scott Moreau850ca422012-05-21 15:21:25 -06005683 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005684
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005685 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005686 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005687
Scott Moreauccbf29d2012-02-22 14:21:41 -07005688 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005689 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005690 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005691 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005692 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005693 weston_matrix_scale(&output->matrix, magnification,
5694 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005695 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005696
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005697 switch (output->transform) {
5698 case WL_OUTPUT_TRANSFORM_FLIPPED:
5699 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5700 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5701 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5702 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5703 weston_matrix_scale(&output->matrix, -1, 1, 1);
5704 break;
5705 }
5706
5707 switch (output->transform) {
5708 default:
5709 case WL_OUTPUT_TRANSFORM_NORMAL:
5710 case WL_OUTPUT_TRANSFORM_FLIPPED:
5711 break;
5712 case WL_OUTPUT_TRANSFORM_90:
5713 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5714 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5715 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5716 break;
5717 case WL_OUTPUT_TRANSFORM_180:
5718 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5719 weston_matrix_translate(&output->matrix,
5720 -output->width, -output->height, 0);
5721 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5722 break;
5723 case WL_OUTPUT_TRANSFORM_270:
5724 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5725 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5726 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5727 break;
5728 }
5729
5730 if (output->current_scale != 1)
5731 weston_matrix_scale(&output->matrix,
5732 output->current_scale,
5733 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005734
Scott Moreauccbf29d2012-02-22 14:21:41 -07005735 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005736
5737 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005738}
5739
Scott Moreau1bad5db2012-08-18 01:04:05 -06005740static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005741weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005742{
5743 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005744 output->native_scale = scale;
5745 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005746
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005747 convert_size_by_transform_scale(&output->width, &output->height,
5748 output->current_mode->width,
5749 output->current_mode->height,
5750 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005751}
5752
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005753static void
5754weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005755{
5756 output->x = x;
5757 output->y = y;
5758
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005759 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005760 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005761
5762 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005763 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005764 output->width,
5765 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005766}
5767
Marius Vlad55d87362019-06-11 01:15:35 +03005768/**
5769 * \ingroup output
5770 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005771WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005772weston_output_move(struct weston_output *output, int x, int y)
5773{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005774 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005775 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005776 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005777
5778 output->move_x = x - output->x;
5779 output->move_y = y - output->y;
5780
5781 if (output->move_x == 0 && output->move_y == 0)
5782 return;
5783
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005784 weston_output_init_geometry(output, x, y);
5785
5786 output->dirty = 1;
5787
5788 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005789 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005790
5791 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005792 wl_list_for_each(head, &output->head_list, output_link) {
5793 wl_resource_for_each(resource, &head->resource_list) {
5794 wl_output_send_geometry(resource,
5795 output->x,
5796 output->y,
5797 head->mm_width,
5798 head->mm_height,
5799 head->subpixel,
5800 head->make,
5801 head->model,
5802 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005803
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005804 ver = wl_resource_get_version(resource);
5805 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5806 wl_output_send_done(resource);
5807 }
Roman Gilge97391c2019-03-29 13:01:06 +01005808
5809 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5810 zxdg_output_v1_send_logical_position(resource,
5811 output->x,
5812 output->y);
5813 zxdg_output_v1_send_done(resource);
5814 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005815 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005816}
5817
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005818/** Signal that a pending output is taken into use.
5819 *
5820 * Removes the output from the pending list and adds it to the compositor's
5821 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005822 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005823 * The output gets an internal ID assigned, and the wl_output global is
5824 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005825 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005826 * \param compositor The compositor instance.
5827 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005828 *
5829 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005830 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005831 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005832static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005833weston_compositor_add_output(struct weston_compositor *compositor,
5834 struct weston_output *output)
5835{
Armin Krezoviće5403842016-08-05 15:28:29 +02005836 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005837 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005838
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005839 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005840
5841 /* Verify we haven't reached the limit of 32 available output IDs */
5842 assert(ffs(~compositor->output_id_pool) > 0);
5843
5844 /* Invert the output id pool and look for the lowest numbered
5845 * switch (the least significant bit). Take that bit's position
5846 * as our ID, and mark it used in the compositor's output_id_pool.
5847 */
5848 output->id = ffs(~compositor->output_id_pool) - 1;
5849 compositor->output_id_pool |= 1u << output->id;
5850
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005851 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005852 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005853 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005854
Pekka Paalanendcac3512017-12-08 14:13:34 +02005855 wl_list_for_each(head, &output->head_list, output_link)
5856 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005857
Giulio Camuffob1147152015-05-06 21:41:57 +03005858 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005859
5860 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5861 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005862}
5863
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005864/** Transform device coordinates into global coordinates
5865 *
Marius Vlada2dace22019-06-12 16:05:44 +03005866 * \param output the weston_output object
5867 * \param[in] device_x X coordinate in device units.
5868 * \param[in] device_y Y coordinate in device units.
5869 * \param[out] x X coordinate in the global space.
5870 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005871 *
Marius Vlada2dace22019-06-12 16:05:44 +03005872 * Transforms coordinates from the device coordinate space (physical pixel
5873 * units) to the global coordinate space (logical pixel units). This takes
5874 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005875 *
Marius Vlad55d87362019-06-11 01:15:35 +03005876 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005877 * \internal
5878 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005879WL_EXPORT void
5880weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005881 double device_x, double device_y,
5882 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005883{
Derek Foreman0f679412014-10-02 13:41:17 -05005884 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005885 device_x,
5886 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005887 0.0,
5888 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005889
Derek Foreman67a18b92015-03-24 11:36:14 -05005890 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005891
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005892 *x = p.f[0] / p.f[3];
5893 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005894}
5895
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005896/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005897 *
5898 * \param output The weston_output object that is being removed.
5899 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005900 * The following happens:
5901 *
5902 * - The output assignments of all views in the current scenegraph are
5903 * recomputed.
5904 *
5905 * - Presentation feedback is discarded.
5906 *
5907 * - Compositor is notified that outputs were changed and
5908 * applies the necessary changes to re-layout outputs.
5909 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005910 * - The output is put back in the pending outputs list.
5911 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005912 * - Signal is emitted to notify all users of the weston_output
5913 * object that the output is being destroyed.
5914 *
5915 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005916 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005917 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005918 * - The output's internal ID is released.
5919 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005920 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005921 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005922 */
5923static void
5924weston_compositor_remove_output(struct weston_output *output)
5925{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005926 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005927 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005928 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005929
5930 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005931 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005932
Pekka Paalanenbccda712017-03-29 16:16:04 +03005933 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005934 if (view->output_mask & (1u << output->id))
5935 weston_view_assign_output(view);
5936 }
5937
5938 weston_presentation_feedback_discard_list(&output->feedback_list);
5939
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005940 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005941
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005942 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005943 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005944 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005945
Pekka Paalanenbccda712017-03-29 16:16:04 +03005946 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005947 wl_signal_emit(&output->destroy_signal, output);
5948
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005949 wl_list_for_each(head, &output->head_list, output_link)
5950 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005951
5952 compositor->output_id_pool &= ~(1u << output->id);
5953 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005954}
5955
5956/** Sets the output scale for a given output.
5957 *
5958 * \param output The weston_output object that the scale is set for.
5959 * \param scale Scale factor for the given output.
5960 *
5961 * It only supports setting scale for an output that
5962 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005963 *
Marius Vlad55d87362019-06-11 01:15:35 +03005964 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005965 */
5966WL_EXPORT void
5967weston_output_set_scale(struct weston_output *output,
5968 int32_t scale)
5969{
5970 /* We can only set scale on a disabled output */
5971 assert(!output->enabled);
5972
5973 /* We only want to set scale once */
5974 assert(!output->scale);
5975
5976 output->scale = scale;
5977}
5978
5979/** Sets the output transform for a given output.
5980 *
5981 * \param output The weston_output object that the transform is set for.
5982 * \param transform Transform value for the given output.
5983 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005984 * Refer to wl_output::transform section located at
5985 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5986 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005987 *
Marius Vlad55d87362019-06-11 01:15:35 +03005988 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005989 */
5990WL_EXPORT void
5991weston_output_set_transform(struct weston_output *output,
5992 uint32_t transform)
5993{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005994 struct weston_pointer_motion_event ev;
5995 struct wl_resource *resource;
5996 struct weston_seat *seat;
5997 pixman_region32_t old_region;
5998 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005999 struct weston_head *head;
6000 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006001
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006002 if (!output->enabled && output->transform == UINT32_MAX) {
6003 output->transform = transform;
6004 return;
6005 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006006
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006007 weston_output_transform_scale_init(output, transform, output->scale);
6008
6009 pixman_region32_init(&old_region);
6010 pixman_region32_copy(&old_region, &output->region);
6011
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006012 weston_output_init_geometry(output, output->x, output->y);
6013
6014 output->dirty = 1;
6015
6016 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006017 wl_list_for_each(head, &output->head_list, output_link) {
6018 wl_resource_for_each(resource, &head->resource_list) {
6019 wl_output_send_geometry(resource,
6020 output->x,
6021 output->y,
6022 head->mm_width,
6023 head->mm_height,
6024 head->subpixel,
6025 head->make,
6026 head->model,
6027 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006028
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006029 ver = wl_resource_get_version(resource);
6030 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6031 wl_output_send_done(resource);
6032 }
Roman Gilge97391c2019-03-29 13:01:06 +01006033 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6034 zxdg_output_v1_send_logical_position(resource,
6035 output->x,
6036 output->y);
6037 zxdg_output_v1_send_logical_size(resource,
6038 output->width,
6039 output->height);
6040 zxdg_output_v1_send_done(resource);
6041 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006042 }
6043
6044 /* we must ensure that pointers are inside output, otherwise they disappear */
6045 mid_x = output->x + output->width / 2;
6046 mid_y = output->y + output->height / 2;
6047
6048 ev.mask = WESTON_POINTER_MOTION_ABS;
6049 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6050 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6051
6052 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6053 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6054
6055 if (pointer && pixman_region32_contains_point(&old_region,
6056 wl_fixed_to_int(pointer->x),
6057 wl_fixed_to_int(pointer->y),
6058 NULL))
6059 weston_pointer_move(pointer, &ev);
6060 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006061}
6062
6063/** Initializes a weston_output object with enough data so
6064 ** an output can be configured.
6065 *
6066 * \param output The weston_output object to initialize
6067 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006068 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006069 *
6070 * Sets initial values for fields that are expected to be
6071 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006072 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006073 * The name is used in logs, and can be used by compositors as a configuration
6074 * identifier.
6075 *
Marius Vlad55d87362019-06-11 01:15:35 +03006076 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006077 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006078 */
6079WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006080weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006081 struct weston_compositor *compositor,
6082 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006083{
6084 output->compositor = compositor;
6085 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006086 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006087 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006088 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006089 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306090 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306091 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006092
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006093 wl_list_init(&output->head_list);
6094
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006095 /* Add some (in)sane defaults which can be used
6096 * for checking if an output was properly configured
6097 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006098 output->scale = 0;
6099 /* Can't use -1 on uint32_t and 0 is valid enum value */
6100 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006101
6102 pixman_region32_init(&output->previous_damage);
6103 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006104 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006105}
6106
6107/** Adds weston_output object to pending output list.
6108 *
6109 * \param output The weston_output object to add
6110 * \param compositor The compositor instance.
6111 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006112 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006113 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006114 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006115 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006116 */
6117WL_EXPORT void
6118weston_compositor_add_pending_output(struct weston_output *output,
6119 struct weston_compositor *compositor)
6120{
Pekka Paalanene952a012017-03-29 17:14:00 +03006121 assert(output->disable);
6122 assert(output->enable);
6123
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006124 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006125 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006126}
6127
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006128/** Create a string with the attached heads' names.
6129 *
6130 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006131 *
6132 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006133 */
6134static char *
6135weston_output_create_heads_string(struct weston_output *output)
6136{
6137 FILE *fp;
6138 char *str = NULL;
6139 size_t size = 0;
6140 struct weston_head *head;
6141 const char *sep = "";
6142
6143 fp = open_memstream(&str, &size);
6144 if (!fp)
6145 return NULL;
6146
6147 wl_list_for_each(head, &output->head_list, output_link) {
6148 fprintf(fp, "%s%s", sep, head->name);
6149 sep = ", ";
6150 }
6151 fclose(fp);
6152
6153 return str;
6154}
6155
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006156/** Constructs a weston_output object that can be used by the compositor.
6157 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006158 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006159 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006160 *
6161 * Output coordinates are calculated and each new output is by default
6162 * assigned to the right of previous one.
6163 *
6164 * Sets up the transformation, zoom, and geometry of the output using
6165 * the properties that need to be configured by the compositor.
6166 *
6167 * Establishes a repaint timer for the output with the relevant display
6168 * object's event loop. See output_repaint_timer_handler().
6169 *
6170 * The output is assigned an ID. Weston can support up to 32 distinct
6171 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6172 * is referred to and used to find the first available ID number, and
6173 * then this ID is marked as used in output_id_pool.
6174 *
6175 * The output is also assigned a Wayland global with the wl_output
6176 * external interface.
6177 *
6178 * Backend specific function is called to set up the output output.
6179 *
6180 * Output is added to the compositor's output list
6181 *
6182 * If the backend specific function fails, the weston_output object
6183 * is returned to a state it was before calling this function and
6184 * is added to the compositor's pending_output_list in case it needs
6185 * to be reconfigured or just so it can be destroyed at shutdown.
6186 *
6187 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006188 *
6189 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006190 */
6191WL_EXPORT int
6192weston_output_enable(struct weston_output *output)
6193{
Armin Krezović782f5df2016-09-30 14:11:11 +02006194 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006195 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006196 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006197 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006198 int x = 0, y = 0;
6199
Pekka Paalanencc201e42017-03-30 15:11:25 +03006200 if (output->enabled) {
6201 weston_log("Error: attempt to enable an enabled output '%s'\n",
6202 output->name);
6203 return -1;
6204 }
6205
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006206 if (wl_list_empty(&output->head_list)) {
6207 weston_log("Error: cannot enable output '%s' without heads.\n",
6208 output->name);
6209 return -1;
6210 }
6211
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006212 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6213 weston_log("Error: no video mode for output '%s'.\n",
6214 output->name);
6215 return -1;
6216 }
6217
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006218 wl_list_for_each(head, &output->head_list, output_link) {
6219 assert(head->make);
6220 assert(head->model);
6221 }
6222
Armin Krezović782f5df2016-09-30 14:11:11 +02006223 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006224 struct weston_output, link);
6225
Armin Krezović782f5df2016-09-30 14:11:11 +02006226 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006227 x = iterator->x + iterator->width;
6228
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006229 /* Make sure the scale is set up */
6230 assert(output->scale);
6231
6232 /* Make sure we have a transform set */
6233 assert(output->transform != UINT32_MAX);
6234
Armin Krezović782f5df2016-09-30 14:11:11 +02006235 output->x = x;
6236 output->y = y;
6237 output->dirty = 1;
6238 output->original_scale = output->scale;
6239
Marius Vlad3a2f8292019-11-04 17:53:46 +02006240 wl_signal_init(&output->frame_signal);
6241 wl_signal_init(&output->destroy_signal);
6242
Armin Krezović782f5df2016-09-30 14:11:11 +02006243 weston_output_transform_scale_init(output, output->transform, output->scale);
6244 weston_output_init_zoom(output);
6245
6246 weston_output_init_geometry(output, x, y);
6247 weston_output_damage(output);
6248
Armin Krezović782f5df2016-09-30 14:11:11 +02006249 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006250 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006251
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006252 /* Enable the output (set up the crtc or create a
6253 * window representing the output, set up the
6254 * renderer, etc)
6255 */
6256 if (output->enable(output) < 0) {
6257 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006258 return -1;
6259 }
6260
6261 weston_compositor_add_output(output->compositor, output);
6262
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006263 head_names = weston_output_create_heads_string(output);
6264 weston_log("Output '%s' enabled with head(s) %s\n",
6265 output->name, head_names);
6266 free(head_names);
6267
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006268 return 0;
6269}
6270
6271/** Converts a weston_output object to a pending output state, so it
6272 ** can be configured again or destroyed.
6273 *
6274 * \param output The weston_output object that needs to be disabled.
6275 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006276 * Calls a backend specific function to disable an output, in case
6277 * such function exists.
6278 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006279 * The backend specific disable function may choose to postpone the disabling
6280 * by returning a negative value, in which case this function returns early.
6281 * In that case the backend will guarantee the output will be disabled soon
6282 * by the backend calling this function again. One must not attempt to re-enable
6283 * the output until that happens.
6284 *
6285 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006286 * from weston's output_list (see weston_compositor_remove_output())
6287 * and is returned to a state it was before weston_output_enable()
6288 * was ran (see weston_output_enable_undo()).
6289 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006290 * See weston_output_init() for more information on the
6291 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006292 *
6293 * If the output has never been enabled yet, this function can still be
6294 * called to ensure that the output is actually turned off rather than left
6295 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006296 *
6297 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006298 */
6299WL_EXPORT void
6300weston_output_disable(struct weston_output *output)
6301{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006302 /* Should we rename this? */
6303 output->destroying = 1;
6304
Pekka Paalanenc65df642017-03-29 15:45:46 +03006305 /* Disable is called unconditionally also for not-enabled outputs,
6306 * because at compositor start-up, if there is an output that is
6307 * already on but the compositor wants to turn it off, we have to
6308 * forward the turn-off to the backend so it knows to do it.
6309 * The backend cannot initially turn off everything, because it
6310 * would cause unnecessary mode-sets for all outputs the compositor
6311 * wants to be on.
6312 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006313 if (output->disable(output) < 0)
6314 return;
6315
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006316 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006317 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006318
6319 output->destroying = 0;
6320}
6321
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006322/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006323 *
6324 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006325 *
6326 * If there are new or changed heads, calls the heads_changed hook and
6327 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006328 *
6329 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006330 */
6331WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006332weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006333{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006334 if (compositor->heads_changed_source) {
6335 wl_event_source_remove(compositor->heads_changed_source);
6336 weston_compositor_call_heads_changed(compositor);
6337 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006338}
6339
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006340/** Add destroy callback for an output
6341 *
6342 * \param output The output to watch.
6343 * \param listener The listener to add. The \c notify member must be set.
6344 *
6345 * The listener callback will be called when user destroys an output. This
6346 * may be delayed by a backend in some cases. The main purpose of the
6347 * listener is to allow hooking up custom data to the output. The custom data
6348 * can be fetched via weston_output_get_destroy_listener() followed by
6349 * container_of().
6350 *
6351 * The \c data argument to the notify callback is the weston_output being
6352 * destroyed.
6353 *
6354 * @note This is for the final destruction of an output, not when it gets
6355 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006356 *
6357 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006358 */
6359WL_EXPORT void
6360weston_output_add_destroy_listener(struct weston_output *output,
6361 struct wl_listener *listener)
6362{
6363 wl_signal_add(&output->user_destroy_signal, listener);
6364}
6365
6366/** Look up destroy listener for an output
6367 *
6368 * \param output The output to query.
6369 * \param notify The notify function used used for the added destroy listener.
6370 * \return The listener, or NULL if not found.
6371 *
6372 * This looks up the previously added destroy listener struct based on the
6373 * notify function it has. The listener can be used to access user data
6374 * through \c container_of().
6375 *
6376 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006377 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006378 */
6379WL_EXPORT struct wl_listener *
6380weston_output_get_destroy_listener(struct weston_output *output,
6381 wl_notify_func_t notify)
6382{
6383 return wl_signal_get(&output->user_destroy_signal, notify);
6384}
6385
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006386/** Uninitialize an output
6387 *
6388 * Removes the output from the list of enabled outputs if necessary, but
6389 * does not call the backend's output disable function. The output will no
6390 * longer be in the list of pending outputs either.
6391 *
6392 * All fields of weston_output become uninitialized, i.e. should not be used
6393 * anymore. The caller can free the memory after this.
6394 *
Marius Vlad55d87362019-06-11 01:15:35 +03006395 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006396 * \internal
6397 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006398WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006399weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006400{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006401 struct weston_head *head, *tmp;
6402
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006403 output->destroying = 1;
6404
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006405 wl_signal_emit(&output->user_destroy_signal, output);
6406
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006407 if (output->idle_repaint_source)
6408 wl_event_source_remove(output->idle_repaint_source);
6409
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006410 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006411 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006412
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006413 pixman_region32_fini(&output->region);
6414 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006415 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006416
6417 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6418 weston_head_detach(head);
6419
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006420 free(output->name);
6421}
6422
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006423/** Find an output by its given name
6424 *
6425 * \param compositor The compositor to search in.
6426 * \param name The output name to search for.
6427 * \return An existing output with the given name, or NULL if not found.
6428 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006429 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006430 */
6431WL_EXPORT struct weston_output *
6432weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6433 const char *name)
6434{
6435 struct weston_output *output;
6436
6437 wl_list_for_each(output, &compositor->output_list, link)
6438 if (strcmp(output->name, name) == 0)
6439 return output;
6440
6441 wl_list_for_each(output, &compositor->pending_output_list, link)
6442 if (strcmp(output->name, name) == 0)
6443 return output;
6444
6445 return NULL;
6446}
6447
6448/** Create a named output
6449 *
6450 * \param compositor The compositor.
6451 * \param name The name for the output.
6452 * \return A new \c weston_output, or NULL on failure.
6453 *
6454 * This creates a new weston_output that starts with no heads attached.
6455 *
6456 * An output must be configured and it must have at least one head before
6457 * it can be enabled.
6458 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006459 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006460 */
6461WL_EXPORT struct weston_output *
6462weston_compositor_create_output(struct weston_compositor *compositor,
6463 const char *name)
6464{
6465 assert(compositor->backend->create_output);
6466
6467 if (weston_compositor_find_output_by_name(compositor, name)) {
6468 weston_log("Warning: attempted to create an output with a "
6469 "duplicate name '%s'.\n", name);
6470 return NULL;
6471 }
6472
6473 return compositor->backend->create_output(compositor, name);
6474}
6475
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006476/** Create an output for an unused head
6477 *
6478 * \param compositor The compositor.
6479 * \param head The head to attach to the output.
6480 * \return A new \c weston_output, or NULL on failure.
6481 *
6482 * This creates a new weston_output that starts with the given head attached.
6483 * The output inherits the name of the head. The head must not be already
6484 * attached to another output.
6485 *
6486 * An output must be configured before it can be enabled.
6487 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006488 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006489 */
6490WL_EXPORT struct weston_output *
6491weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6492 struct weston_head *head)
6493{
6494 struct weston_output *output;
6495
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006496 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006497 if (!output)
6498 return NULL;
6499
6500 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006501 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006502 return NULL;
6503 }
6504
6505 return output;
6506}
6507
6508/** Destroy an output
6509 *
6510 * \param output The output to destroy.
6511 *
6512 * The heads attached to the given output are detached and become unused again.
6513 *
6514 * It is not necessary to explicitly destroy all outputs at compositor exit.
Marius Vlad413cda52019-08-23 17:42:40 +03006515 * weston_compositor_tear_down() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006516 * outputs.
6517 *
Marius Vlad55d87362019-06-11 01:15:35 +03006518 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006519 */
6520WL_EXPORT void
6521weston_output_destroy(struct weston_output *output)
6522{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006523 output->destroy(output);
6524}
6525
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006526/** When you need a head...
6527 *
6528 * This function is a hack, used until all code has been converted to become
6529 * multi-head aware.
6530 *
6531 * \param output The weston_output whose head to get.
6532 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006533 *
6534 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006535 */
6536WL_EXPORT struct weston_head *
6537weston_output_get_first_head(struct weston_output *output)
6538{
6539 if (wl_list_empty(&output->head_list))
6540 return NULL;
6541
6542 return container_of(output->head_list.next,
6543 struct weston_head, output_link);
6544}
6545
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306546/** Allow/Disallow content-protection support for an output
6547 *
6548 * This function sets the allow_protection member for an output. Setting of
6549 * this field will allow the compositor to attempt content-protection for this
6550 * output, for a backend that supports the content-protection protocol.
6551 *
6552 * \param output The weston_output for whom the content-protection is to be
6553 * allowed.
6554 * \param allow_protection The bool value which is to be set.
6555 */
6556WL_EXPORT void
6557weston_output_allow_protection(struct weston_output *output,
6558 bool allow_protection)
6559{
6560 output->allow_protection = allow_protection;
6561}
6562
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006563static void
Roman Gilge97391c2019-03-29 13:01:06 +01006564xdg_output_unlist(struct wl_resource *resource)
6565{
6566 wl_list_remove(wl_resource_get_link(resource));
6567}
6568
6569static void
6570xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6571{
6572 wl_resource_destroy(resource);
6573}
6574
6575static const struct zxdg_output_v1_interface xdg_output_interface = {
6576 xdg_output_destroy
6577};
6578
6579static void
6580xdg_output_manager_destroy(struct wl_client *client,
6581 struct wl_resource *resource)
6582{
6583 wl_resource_destroy(resource);
6584}
6585
6586static void
6587xdg_output_manager_get_xdg_output(struct wl_client *client,
6588 struct wl_resource *manager,
6589 uint32_t id,
6590 struct wl_resource *output_resource)
6591{
6592 int version = wl_resource_get_version(manager);
6593 struct weston_head *head = wl_resource_get_user_data(output_resource);
6594 struct weston_output *output = head->output;
6595 struct wl_resource *resource;
6596
6597 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6598 version, id);
6599 if (resource == NULL) {
6600 wl_client_post_no_memory(client);
6601 return;
6602 }
6603
6604 wl_list_insert(&head->xdg_output_resource_list,
6605 wl_resource_get_link(resource));
6606
6607 wl_resource_set_implementation(resource, &xdg_output_interface,
6608 NULL, xdg_output_unlist);
6609
6610 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6611 zxdg_output_v1_send_logical_size(resource,
6612 output->width,
6613 output->height);
6614 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6615 zxdg_output_v1_send_name(resource, head->name);
6616
6617 zxdg_output_v1_send_done(resource);
6618}
6619
6620static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6621 xdg_output_manager_destroy,
6622 xdg_output_manager_get_xdg_output
6623};
6624
6625static void
6626bind_xdg_output_manager(struct wl_client *client,
6627 void *data, uint32_t version, uint32_t id)
6628{
6629 struct wl_resource *resource;
6630
6631 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6632 version, id);
6633 if (resource == NULL) {
6634 wl_client_post_no_memory(client);
6635 return;
6636 }
6637
6638 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6639 NULL, NULL);
6640}
6641
6642static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006643destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006644{
Jonny Lamb74130762013-11-26 18:19:46 +01006645 struct weston_surface *surface =
6646 wl_resource_get_user_data(resource);
6647
Pekka Paalanen4826f872016-04-22 14:14:38 +03006648 if (!surface)
6649 return;
6650
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006651 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006652 surface->pending.buffer_viewport.buffer.src_width =
6653 wl_fixed_from_int(-1);
6654 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006655 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006656}
6657
6658static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006659viewport_destroy(struct wl_client *client,
6660 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006661{
6662 wl_resource_destroy(resource);
6663}
6664
6665static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006666viewport_set_source(struct wl_client *client,
6667 struct wl_resource *resource,
6668 wl_fixed_t src_x,
6669 wl_fixed_t src_y,
6670 wl_fixed_t src_width,
6671 wl_fixed_t src_height)
6672{
6673 struct weston_surface *surface =
6674 wl_resource_get_user_data(resource);
6675
Pekka Paalanen4826f872016-04-22 14:14:38 +03006676 if (!surface) {
6677 wl_resource_post_error(resource,
6678 WP_VIEWPORT_ERROR_NO_SURFACE,
6679 "wl_surface for this viewport is no longer exists");
6680 return;
6681 }
6682
6683 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006684 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006685
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006686 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006687 src_height == wl_fixed_from_int(-1) &&
6688 src_x == wl_fixed_from_int(-1) &&
6689 src_y == wl_fixed_from_int(-1)) {
6690 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006691 surface->pending.buffer_viewport.buffer.src_width =
6692 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006693 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006694 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006695 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006696
Pekka Paalanen201769a2016-04-26 14:42:11 +03006697 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006698 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006699 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006700 "wl_surface@%d viewport source "
6701 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6702 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006703 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006704 wl_fixed_to_double(src_height),
6705 wl_fixed_to_double(src_x),
6706 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006707 return;
6708 }
6709
6710 surface->pending.buffer_viewport.buffer.src_x = src_x;
6711 surface->pending.buffer_viewport.buffer.src_y = src_y;
6712 surface->pending.buffer_viewport.buffer.src_width = src_width;
6713 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006714 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006715}
6716
6717static void
6718viewport_set_destination(struct wl_client *client,
6719 struct wl_resource *resource,
6720 int32_t dst_width,
6721 int32_t dst_height)
6722{
6723 struct weston_surface *surface =
6724 wl_resource_get_user_data(resource);
6725
Pekka Paalanen4826f872016-04-22 14:14:38 +03006726 if (!surface) {
6727 wl_resource_post_error(resource,
6728 WP_VIEWPORT_ERROR_NO_SURFACE,
6729 "wl_surface for this viewport no longer exists");
6730 return;
6731 }
6732
6733 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006734
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006735 if (dst_width == -1 && dst_height == -1) {
6736 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006737 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006738 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006739 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006740 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006741
6742 if (dst_width <= 0 || dst_height <= 0) {
6743 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006744 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006745 "destination size must be positive (%dx%d)",
6746 dst_width, dst_height);
6747 return;
6748 }
6749
6750 surface->pending.buffer_viewport.surface.width = dst_width;
6751 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006752 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006753}
6754
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006755static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006756 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006757 viewport_set_source,
6758 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006759};
6760
6761static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006762viewporter_destroy(struct wl_client *client,
6763 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006764{
6765 wl_resource_destroy(resource);
6766}
6767
6768static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006769viewporter_get_viewport(struct wl_client *client,
6770 struct wl_resource *viewporter,
6771 uint32_t id,
6772 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006773{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006774 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006775 struct weston_surface *surface =
6776 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006777 struct wl_resource *resource;
6778
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006779 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006780 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006781 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006782 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006783 return;
6784 }
6785
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006786 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006787 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006788 if (resource == NULL) {
6789 wl_client_post_no_memory(client);
6790 return;
6791 }
6792
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006793 wl_resource_set_implementation(resource, &viewport_interface,
6794 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006795
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006796 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006797}
6798
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006799static const struct wp_viewporter_interface viewporter_interface = {
6800 viewporter_destroy,
6801 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006802};
6803
6804static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006805bind_viewporter(struct wl_client *client,
6806 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006807{
6808 struct wl_resource *resource;
6809
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006810 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006811 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006812 if (resource == NULL) {
6813 wl_client_post_no_memory(client);
6814 return;
6815 }
6816
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006817 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006818 NULL, NULL);
6819}
6820
6821static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006822destroy_presentation_feedback(struct wl_resource *feedback_resource)
6823{
6824 struct weston_presentation_feedback *feedback;
6825
6826 feedback = wl_resource_get_user_data(feedback_resource);
6827
6828 wl_list_remove(&feedback->link);
6829 free(feedback);
6830}
6831
6832static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006833presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6834{
6835 wl_resource_destroy(resource);
6836}
6837
6838static void
6839presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006840 struct wl_resource *presentation_resource,
6841 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006842 uint32_t callback)
6843{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006844 struct weston_surface *surface;
6845 struct weston_presentation_feedback *feedback;
6846
6847 surface = wl_resource_get_user_data(surface_resource);
6848
Bryce Harringtonde16d892014-11-20 22:21:57 -08006849 feedback = zalloc(sizeof *feedback);
6850 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006851 goto err_calloc;
6852
6853 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006854 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006855 1, callback);
6856 if (!feedback->resource)
6857 goto err_create;
6858
6859 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6860 destroy_presentation_feedback);
6861
6862 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6863
6864 return;
6865
6866err_create:
6867 free(feedback);
6868
6869err_calloc:
6870 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006871}
6872
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006873static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006874 presentation_destroy,
6875 presentation_feedback
6876};
6877
6878static void
6879bind_presentation(struct wl_client *client,
6880 void *data, uint32_t version, uint32_t id)
6881{
6882 struct weston_compositor *compositor = data;
6883 struct wl_resource *resource;
6884
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006885 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006886 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006887 if (resource == NULL) {
6888 wl_client_post_no_memory(client);
6889 return;
6890 }
6891
6892 wl_resource_set_implementation(resource, &presentation_implementation,
6893 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006894 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006895}
6896
6897static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006898compositor_bind(struct wl_client *client,
6899 void *data, uint32_t version, uint32_t id)
6900{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006901 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006902 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006903
Jason Ekstranda85118c2013-06-27 20:17:02 -05006904 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006905 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006906 if (resource == NULL) {
6907 wl_client_post_no_memory(client);
6908 return;
6909 }
6910
6911 wl_resource_set_implementation(resource, &compositor_interface,
6912 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006913}
6914
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006915WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006916weston_environment_get_fd(const char *env)
6917{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006918 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006919 int fd, flags;
6920
6921 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006922 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006923 return -1;
6924
6925 flags = fcntl(fd, F_GETFD);
6926 if (flags == -1)
6927 return -1;
6928
6929 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6930 unsetenv(env);
6931
6932 return fd;
6933}
6934
Daniel Stonece62cb32018-07-20 09:46:24 +01006935static const char *
6936output_repaint_status_text(struct weston_output *output)
6937{
6938 switch (output->repaint_status) {
6939 case REPAINT_NOT_SCHEDULED:
6940 return "no repaint";
6941 case REPAINT_BEGIN_FROM_IDLE:
6942 return "start_repaint_loop scheduled";
6943 case REPAINT_SCHEDULED:
6944 return "repaint scheduled";
6945 case REPAINT_AWAITING_COMPLETION:
6946 return "awaiting completion";
6947 }
6948
6949 assert(!"output_repaint_status_text missing enum");
6950 return NULL;
6951}
6952
6953static void
6954debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6955{
6956 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6957 struct wl_shm_buffer *shm;
6958 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006959 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006960
6961 if (!buffer) {
6962 fprintf(fp, "\t\t[buffer not available]\n");
6963 return;
6964 }
6965
6966 shm = wl_shm_buffer_get(buffer->resource);
6967 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006968 uint32_t _format = wl_shm_buffer_get_format(shm);
6969 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006970 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006971 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6972 (unsigned long) _format,
6973 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006974 return;
6975 }
6976
6977 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6978 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006979 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006980 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006981 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6982 (unsigned long) dmabuf->attributes.format,
6983 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006984 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6985 (unsigned long long) dmabuf->attributes.modifier[0]);
6986 return;
6987 }
6988
Marius Vlad253ba9a2019-03-17 18:22:21 +02006989 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006990}
6991
6992static void
6993debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6994{
6995 struct weston_compositor *ec = view->surface->compositor;
6996 struct weston_output *output;
6997 char desc[512];
6998 pixman_box32_t *box;
6999 uint32_t surface_id = 0;
7000 pid_t pid = 0;
7001
7002 if (view->surface->resource) {
7003 struct wl_resource *resource = view->surface->resource;
7004 wl_client_get_credentials(wl_resource_get_client(resource),
7005 &pid, NULL, NULL);
7006 surface_id = wl_resource_get_id(view->surface->resource);
7007 }
7008
7009 if (!view->surface->get_label ||
7010 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7011 strcpy(desc, "[no description available]");
7012 }
7013 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7014 view_idx, view->surface->role_name, pid, surface_id,
7015 desc, view);
7016
7017 box = pixman_region32_extents(&view->transform.boundingbox);
7018 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7019 box->x1, box->y1, box->x2, box->y2);
7020 box = pixman_region32_extents(&view->transform.opaque);
7021
7022 if (pixman_region32_equal(&view->transform.opaque,
7023 &view->transform.boundingbox)) {
7024 fprintf(fp, "\t\t[fully opaque]\n");
7025 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7026 fprintf(fp, "\t\t[not opaque]\n");
7027 } else {
7028 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7029 box->x1, box->y1, box->x2, box->y2);
7030 }
7031
7032 if (view->alpha < 1.0)
7033 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7034
7035 if (view->output_mask != 0) {
7036 bool first_output = true;
7037 fprintf(fp, "\t\toutputs: ");
7038 wl_list_for_each(output, &ec->output_list, link) {
7039 if (!(view->output_mask & (1 << output->id)))
7040 continue;
7041 fprintf(fp, "%s%d (%s)%s",
7042 (first_output) ? "" : ", ",
7043 output->id, output->name,
7044 (view->output == output) ? " (primary)" : "");
7045 first_output = false;
7046 }
7047 } else {
7048 fprintf(fp, "\t\t[no outputs]");
7049 }
7050
7051 fprintf(fp, "\n");
7052
7053 debug_scene_view_print_buffer(fp, view);
7054}
7055
Marius Vlad433f4e72019-02-17 22:14:23 +02007056static void
7057debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007058 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007059{
7060 struct weston_subsurface *sub;
7061 struct weston_view *ev;
7062
7063 /*
7064 * print the view first, then we recursively go on printing
7065 * sub-surfaces. We bail out once no more sub-surfaces are available.
7066 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007067 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007068
7069 /* no more sub-surfaces */
7070 if (wl_list_empty(&view->surface->subsurface_list))
7071 return;
7072
7073 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7074 wl_list_for_each(ev, &sub->surface->views, surface_link) {
7075 /* do not print again the parent view */
7076 if (view == ev)
7077 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007078
7079 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007080 debug_scene_view_print_tree(ev, fp, view_idx);
7081 }
7082 }
7083}
7084
Daniel Stonece62cb32018-07-20 09:46:24 +01007085/**
7086 * Output information on how libweston is currently composing the scene
7087 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007088 *
7089 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007090 */
7091WL_EXPORT char *
7092weston_compositor_print_scene_graph(struct weston_compositor *ec)
7093{
7094 struct weston_output *output;
7095 struct weston_layer *layer;
7096 struct timespec now;
7097 int layer_idx = 0;
7098 FILE *fp;
7099 char *ret;
7100 size_t len;
7101 int err;
7102
7103 fp = open_memstream(&ret, &len);
7104 assert(fp);
7105
7106 weston_compositor_read_presentation_clock(ec, &now);
7107 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7108 now.tv_sec, now.tv_nsec);
7109
7110 wl_list_for_each(output, &ec->output_list, link) {
7111 struct weston_head *head;
7112 int head_idx = 0;
7113
7114 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7115 assert(output->enabled);
7116
7117 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7118 output->x, output->y,
7119 output->x + output->width,
7120 output->y + output->height);
7121 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7122 output->current_mode->width,
7123 output->current_mode->height,
7124 output->current_mode->refresh / 1000.0);
7125 fprintf(fp, "\tscale: %d\n", output->scale);
7126
7127 fprintf(fp, "\trepaint status: %s\n",
7128 output_repaint_status_text(output));
7129 if (output->repaint_status == REPAINT_SCHEDULED)
7130 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7131 output->next_repaint.tv_sec,
7132 output->next_repaint.tv_nsec);
7133
7134 wl_list_for_each(head, &output->head_list, output_link) {
7135 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7136 head_idx++, head->name,
7137 (head->connected) ? "" : "not ");
7138 }
7139 }
7140
7141 fprintf(fp, "\n");
7142
7143 wl_list_for_each(layer, &ec->layer_list, link) {
7144 struct weston_view *view;
7145 int view_idx = 0;
7146
7147 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7148 (unsigned long) layer->position);
7149
7150 if (!weston_layer_mask_is_infinite(layer)) {
7151 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7152 layer->mask.x1, layer->mask.y1,
7153 layer->mask.x2, layer->mask.y2);
7154 }
7155
Marius Vlada6acfa82019-03-17 18:10:09 +02007156 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7157 debug_scene_view_print_tree(view, fp, &view_idx);
7158 view_idx++;
7159 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007160
7161 if (wl_list_empty(&layer->view_list.link))
7162 fprintf(fp, "\t[no views]\n");
7163
7164 fprintf(fp, "\n");
7165 }
7166
7167 err = fclose(fp);
7168 assert(err == 0);
7169
7170 return ret;
7171}
7172
7173/**
7174 * Called when the 'scene-graph' debug scope is bound by a client. This
7175 * one-shot weston-debug scope prints the current scene graph when bound,
7176 * and then terminates the stream.
7177 */
7178static void
Marius Vladdad882a2019-07-17 15:43:53 +03007179debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007180{
7181 struct weston_compositor *ec = data;
7182 char *str = weston_compositor_print_scene_graph(ec);
7183
Marius Vladdad882a2019-07-17 15:43:53 +03007184 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007185 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007186 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007187}
7188
Giulio Camuffo459137b2014-10-11 23:56:24 +03007189/** Create the compositor.
7190 *
7191 * This functions creates and initializes a compositor instance.
7192 *
7193 * \param display The Wayland display to be used.
7194 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007195 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007196 * using the \ref weston_compositor_get_user_data function.
7197 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007198 *
7199 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007200 */
7201WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007202weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007203 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007204 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007205{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007206 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007207 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007208
Giulio Camuffo459137b2014-10-11 23:56:24 +03007209 ec = zalloc(sizeof *ec);
7210 if (!ec)
7211 return NULL;
7212
7213 ec->wl_display = display;
7214 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007215 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007216 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007217 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007218 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007219 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007220 wl_signal_init(&ec->idle_signal);
7221 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007222 wl_signal_init(&ec->show_input_panel_signal);
7223 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007224 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007225 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007226 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007227 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007228 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007229 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007230 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007231 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007232 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007233 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007234
Casey Dahlin58ba1372012-04-19 22:50:08 -04007235 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007236 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007237
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007238 ec->activate_serial = 1;
7239
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007240 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7241
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307242 ec->content_protection = NULL;
7243
Derek Foreman152254b2015-11-26 14:17:48 -06007244 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007245 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007246 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007247
Giulio Camuffo954f1832014-10-11 18:27:30 +03007248 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007249 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007250 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007251
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007252 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007253 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007254 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007255
Roman Gilge97391c2019-03-29 13:01:06 +01007256 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7257 ec, bind_xdg_output_manager))
7258 goto fail;
7259
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007260 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007261 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007262 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007263
Marius Vlad3d7d9782019-04-17 12:35:38 +03007264 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007265 goto fail;
7266
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007267 if (weston_input_init(ec) != 0)
7268 goto fail;
7269
Jason Ekstranda7af7042013-10-12 22:38:11 -05007270 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007271 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007272 wl_list_init(&ec->layer_list);
7273 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007274 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007275 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007276 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007277 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007278 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007279 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007280 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007281 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007282 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007283
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007284 wl_list_init(&ec->plugin_api_list);
7285
Xiong Zhang97116532013-10-23 13:58:31 +08007286 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007287 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007288
Giulio Camuffo459137b2014-10-11 23:56:24 +03007289 wl_data_device_manager_init(ec->wl_display);
7290
7291 wl_display_init_shm(ec->wl_display);
7292
7293 loop = wl_display_get_event_loop(ec->wl_display);
7294 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007295 ec->repaint_timer =
7296 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7297 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007298
Quentin Glidic82681572016-12-17 13:40:51 +01007299 weston_layer_init(&ec->fade_layer, ec);
7300 weston_layer_init(&ec->cursor_layer, ec);
7301
7302 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7303 weston_layer_set_position(&ec->cursor_layer,
7304 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007305
Daniel Stonece62cb32018-07-20 09:46:24 +01007306 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03007307 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
7308 "Scene graph details\n",
Marius Vlad9bb1c3a2019-07-29 12:02:47 +03007309 debug_scene_graph_cb, NULL,
Marius Vlad7e4db952019-04-17 13:47:06 +03007310 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007311
Marius Vladda104eb2019-09-05 14:31:01 +03007312 ec->timeline =
7313 weston_compositor_add_log_scope(ec->weston_log_ctx, "timeline",
7314 "Timeline event points\n",
7315 weston_timeline_create_subscription,
7316 weston_timeline_destroy_subscription,
7317 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007318 return ec;
7319
7320fail:
7321 free(ec);
7322 return NULL;
7323}
7324
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007325/** weston_compositor_shutdown
7326 * \ingroup compositor
7327 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007328WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007329weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007330{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007331 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007332
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007333 wl_event_source_remove(ec->idle_source);
7334
Matt Roper361d2ad2011-08-29 13:52:23 -07007335 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007336 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007337 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007338
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007339 /* Destroy all pending outputs associated with this compositor */
7340 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7341 output->destroy(output);
7342
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007343 if (ec->renderer)
7344 ec->renderer->destroy(ec);
7345
Daniel Stone325fc2d2012-05-30 16:31:58 +01007346 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007347 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007348 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007349 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007350 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007351 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007352
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007353 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007354}
7355
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007356/** weston_compositor_exit_with_code
7357 * \ingroup compositor
7358 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007359WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007360weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007361 int exit_code)
7362{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007363 if (compositor->exit_code == EXIT_SUCCESS)
7364 compositor->exit_code = exit_code;
7365
Giulio Camuffo459137b2014-10-11 23:56:24 +03007366 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007367}
7368
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007369/** weston_compositor_set_default_pointer_grab
7370 * \ingroup compositor
7371 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007372WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007373weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7374 const struct weston_pointer_grab_interface *interface)
7375{
7376 struct weston_seat *seat;
7377
7378 ec->default_pointer_grab = interface;
7379 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007380 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7381
7382 if (pointer)
7383 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007384 }
7385}
7386
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007387/** weston_compositor_set_presentation_clock
7388 * \ingroup compositor
7389 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007390WL_EXPORT int
7391weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7392 clockid_t clk_id)
7393{
7394 struct timespec ts;
7395
7396 if (clock_gettime(clk_id, &ts) < 0)
7397 return -1;
7398
7399 compositor->presentation_clock = clk_id;
7400
7401 return 0;
7402}
7403
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007404/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007405 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007406 *
7407 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007408 */
7409WL_EXPORT int
7410weston_compositor_set_presentation_clock_software(
7411 struct weston_compositor *compositor)
7412{
7413 /* In order of preference */
7414 static const clockid_t clocks[] = {
7415 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7416 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7417 CLOCK_MONOTONIC, /* no jumps, may crawl */
7418 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7419 CLOCK_REALTIME /* may jump and crawl */
7420 };
7421 unsigned i;
7422
7423 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7424 if (weston_compositor_set_presentation_clock(compositor,
7425 clocks[i]) == 0)
7426 return 0;
7427
7428 weston_log("Error: no suitable presentation clock available.\n");
7429
7430 return -1;
7431}
7432
Pekka Paalanen662f3842015-03-18 12:17:26 +02007433/** Read the current time from the Presentation clock
7434 *
7435 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007436 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007437 *
7438 * \note Reading the current time in user space is always imprecise to some
7439 * degree.
7440 *
7441 * This function is never meant to fail. If reading the clock does fail,
7442 * an error message is logged and a zero time is returned. Callers are not
7443 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007444 *
7445 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007446 */
7447WL_EXPORT void
7448weston_compositor_read_presentation_clock(
7449 const struct weston_compositor *compositor,
7450 struct timespec *ts)
7451{
7452 static bool warned;
7453 int ret;
7454
7455 ret = clock_gettime(compositor->presentation_clock, ts);
7456 if (ret < 0) {
7457 ts->tv_sec = 0;
7458 ts->tv_nsec = 0;
7459
7460 if (!warned)
7461 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007462 "the presentation clock %#x: '%s' (%d)\n",
7463 compositor->presentation_clock,
7464 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007465 warned = true;
7466 }
7467}
7468
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007469/** Import dmabuf buffer into current renderer
7470 *
7471 * \param compositor
7472 * \param buffer the dmabuf buffer to import
7473 * \return true on usable buffers, false otherwise
7474 *
7475 * This function tests that the linux_dmabuf_buffer is usable
7476 * for the current renderer. Returns false on unusable buffers. Usually
7477 * usability is tested by importing the dmabufs for composition.
7478 *
7479 * This hook is also used for detecting if the renderer supports
7480 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7481 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007482 *
7483 * \ingroup compositor
7484 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007485WL_EXPORT bool
7486weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7487 struct linux_dmabuf_buffer *buffer)
7488{
7489 struct weston_renderer *renderer;
7490
7491 renderer = compositor->renderer;
7492
7493 if (renderer->import_dmabuf == NULL)
7494 return false;
7495
7496 return renderer->import_dmabuf(compositor, buffer);
7497}
7498
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007499WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007500weston_version(int *major, int *minor, int *micro)
7501{
7502 *major = WESTON_VERSION_MAJOR;
7503 *minor = WESTON_VERSION_MINOR;
7504 *micro = WESTON_VERSION_MICRO;
7505}
7506
Daniel Stonee03c1112016-11-24 20:45:45 +00007507/**
7508 * Attempts to find a module path from the module map specified in the
7509 * environment. If found, writes the full path into the path variable.
7510 *
7511 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7512 * each entry is of the form "name=path" and entries are separated by
7513 * semicolons. Whitespace is significant.
7514 *
7515 * \param name The name to search for.
7516 * \param path Where the path is written to if found.
7517 * \param path_len Allocated bytes at \c path .
7518 * \returns The length of the string written to path on success, or 0 if the
7519 * module was not specified in the environment map or path_len was too small.
7520 */
7521WL_EXPORT size_t
7522weston_module_path_from_env(const char *name, char *path, size_t path_len)
7523{
7524 const char *mapping = getenv("WESTON_MODULE_MAP");
7525 const char *end;
7526 const int name_len = strlen(name);
7527
7528 if (!mapping)
7529 return 0;
7530
7531 end = mapping + strlen(mapping);
7532 while (mapping < end && *mapping) {
7533 const char *filename, *next;
7534
7535 /* early out: impossibly short string */
7536 if (end - mapping < name_len + 1)
7537 return 0;
7538
7539 filename = &mapping[name_len + 1];
7540 next = strchrnul(mapping, ';');
7541
7542 if (strncmp(mapping, name, name_len) == 0 &&
7543 mapping[name_len] == '=') {
7544 size_t file_len = next - filename; /* no trailing NUL */
7545 if (file_len >= path_len)
7546 return 0;
7547 strncpy(path, filename, file_len);
7548 path[file_len] = '\0';
7549 return file_len;
7550 }
7551
7552 mapping = next + 1;
7553 }
7554
7555 return 0;
7556}
7557
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007558WL_EXPORT void *
7559weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007560{
7561 char path[PATH_MAX];
7562 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007563 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007564
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007565 if (name == NULL)
7566 return NULL;
7567
Derek Foreman3f86e502015-06-08 11:46:54 -05007568 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007569 len = weston_module_path_from_env(name, path, sizeof path);
7570 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007571 len = snprintf(path, sizeof path, "%s/%s",
7572 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007573 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007574 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007575 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007576
Daniel Stonebeb97e52016-11-28 12:13:54 +00007577 /* snprintf returns the length of the string it would've written,
7578 * _excluding_ the NUL byte. So even being equal to the size of
7579 * our buffer is an error here. */
7580 if (len >= sizeof path)
7581 return NULL;
7582
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007583 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7584 if (module) {
7585 weston_log("Module '%s' already loaded\n", path);
7586 dlclose(module);
7587 return NULL;
7588 }
7589
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007590 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007591 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007592 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007593 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007594 return NULL;
7595 }
7596
7597 init = dlsym(module, entrypoint);
7598 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007599 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007600 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007601 return NULL;
7602 }
7603
7604 return init;
7605}
7606
Giulio Camuffo459137b2014-10-11 23:56:24 +03007607
Marius Vlad284d5342019-06-24 12:00:47 +03007608/** Tear down the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007609 *
Marius Vlad284d5342019-06-24 12:00:47 +03007610 * This function cleans up the compositor state. While the compositor state has
7611 * been cleaned do note that **only** weston_compositor_destroy() can be called
7612 * afterwards, in order to destroy the compositor instance.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007613 *
Marius Vlad284d5342019-06-24 12:00:47 +03007614 * @param compositor The compositor to be tear-down/cleaned.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007615 *
Marius Vlad284d5342019-06-24 12:00:47 +03007616 * @ingroup compositor
7617 * @sa weston_compositor_destroy
Giulio Camuffo459137b2014-10-11 23:56:24 +03007618 */
7619WL_EXPORT void
Marius Vlad284d5342019-06-24 12:00:47 +03007620weston_compositor_tear_down(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03007621{
7622 /* prevent further rendering while shutting down */
7623 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7624
7625 wl_signal_emit(&compositor->destroy_signal, compositor);
7626
7627 weston_compositor_xkb_destroy(compositor);
7628
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007629 if (compositor->backend)
7630 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007631
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007632 /* The backend is responsible for destroying the heads. */
7633 assert(wl_list_empty(&compositor->head_list));
7634
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007635 weston_plugin_api_destroy_list(compositor);
7636
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007637 if (compositor->heads_changed_source)
7638 wl_event_source_remove(compositor->heads_changed_source);
7639
Marius Vlad7e4db952019-04-17 13:47:06 +03007640 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007641 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03007642
7643 weston_compositor_log_scope_destroy(compositor->timeline);
7644 compositor->timeline = NULL;
Marius Vlad284d5342019-06-24 12:00:47 +03007645}
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007646
Marius Vlad284d5342019-06-24 12:00:47 +03007647/** Destroys the compositor.
7648 *
7649 * This function destroys the compositor. **Do not** call this before
7650 * calling weston_compositor_tear_down()
7651 *
7652 * @param compositor The compositor to be destroyed.
7653 * @ingroup compositor
7654 * @sa weston_compositor_tear_down()
7655 */
7656WL_EXPORT void
7657weston_compositor_destroy(struct weston_compositor *compositor)
7658{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007659 free(compositor);
7660}
7661
7662/** Instruct the compositor to exit.
7663 *
7664 * This functions does not directly destroy the compositor object, it merely
7665 * command it to start the tear down process. It is not guaranteed that the
7666 * tear down will happen immediately.
7667 *
7668 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007669 *
7670 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007671 */
7672WL_EXPORT void
7673weston_compositor_exit(struct weston_compositor *compositor)
7674{
7675 compositor->exit(compositor);
7676}
7677
7678/** Return the user data stored in the compositor.
7679 *
7680 * This function returns the user data pointer set with user_data parameter
7681 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007682 *
7683 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007684 */
7685WL_EXPORT void *
7686weston_compositor_get_user_data(struct weston_compositor *compositor)
7687{
7688 return compositor->user_data;
7689}
Pekka Paalanendd186732016-06-03 14:49:54 +03007690
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007691static const char * const backend_map[] = {
7692 [WESTON_BACKEND_DRM] = "drm-backend.so",
7693 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7694 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7695 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7696 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7697 [WESTON_BACKEND_X11] = "x11-backend.so",
7698};
7699
Pekka Paalanendd186732016-06-03 14:49:54 +03007700/** Load a backend into a weston_compositor
7701 *
7702 * A backend must be loaded to make a weston_compositor work. A backend
7703 * provides input and output capabilities, and determines the renderer to use.
7704 *
7705 * \param compositor A compositor that has not had a backend loaded yet.
7706 * \param backend Name of the backend file.
7707 * \param config_base A pointer to a backend-specific configuration
7708 * structure's 'base' member.
7709 *
7710 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007711 *
7712 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007713 */
7714WL_EXPORT int
7715weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007716 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007717 struct weston_backend_config *config_base)
7718{
7719 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007720 struct weston_backend_config *config_base);
7721
Pekka Paalanend7e35112017-08-29 17:04:12 +03007722 if (compositor->backend) {
7723 weston_log("Error: attempt to load a backend when one is already loaded\n");
7724 return -1;
7725 }
7726
Quentin Glidic887c0182016-07-10 11:00:53 +02007727 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007728 return -1;
7729
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007730 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007731 if (!backend_init)
7732 return -1;
7733
Pekka Paalanend7e35112017-08-29 17:04:12 +03007734 if (backend_init(compositor, config_base) < 0) {
7735 compositor->backend = NULL;
7736 return -1;
7737 }
7738
7739 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007740}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007741
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007742/** weston_compositor_load_xwayland
7743 * \ingroup compositor
7744 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007745WL_EXPORT int
7746weston_compositor_load_xwayland(struct weston_compositor *compositor)
7747{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007748 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007749
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007750 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007751 if (!module_init)
7752 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007753 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007754 return -1;
7755 return 0;
7756}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007757
7758/** Resolve an internal compositor error by disconnecting the client.
7759 *
7760 * This function is used in cases when the wl_buffer turns out
7761 * unusable and there is no fallback path.
7762 *
7763 * It is possible the fault is caused by a compositor bug, the underlying
7764 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7765 * In any case, the options are to either composite garbage or nothing,
7766 * or disconnect the client. This is a helper function for the latter.
7767 *
7768 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7769 *
7770 * \param buffer The weston buffer that is unusable.
7771 * \param msg A custom error message attached to the protocol error.
7772 */
7773WL_EXPORT void
7774weston_buffer_send_server_error(struct weston_buffer *buffer,
7775 const char *msg)
7776{
7777 struct wl_client *client;
7778 struct wl_resource *display_resource;
7779 uint32_t id;
7780
7781 assert(buffer->resource);
7782 id = wl_resource_get_id(buffer->resource);
7783 client = wl_resource_get_client(buffer->resource);
7784 display_resource = wl_client_get_object(client, 1);
7785
7786 assert(display_resource);
7787 wl_resource_post_error(display_resource,
7788 WL_DISPLAY_ERROR_INVALID_OBJECT,
7789 "server error with "
7790 "wl_buffer@%u: %s", id, msg);
7791}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307792
7793WL_EXPORT void
7794weston_output_disable_planes_incr(struct weston_output *output)
7795{
7796 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05307797 /*
7798 * If disable_planes changes from 0 to non-zero, it means some type of
7799 * recording of content has started, and therefore protection level of
7800 * the protected surfaces must be updated to avoid the recording of
7801 * the protected content.
7802 */
7803 if (output->disable_planes == 1)
7804 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307805}
7806
7807WL_EXPORT void
7808weston_output_disable_planes_decr(struct weston_output *output)
7809{
7810 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05307811 /*
7812 * If disable_planes changes from non-zero to 0, it means no content
7813 * recording is going on any more, and the protected and surfaces can be
7814 * shown without any apprehensions about content being recorded.
7815 */
7816 if (output->disable_planes == 0)
7817 weston_schedule_surface_protection_update(output->compositor);
7818
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307819}