blob: 35da1e658a02bd7340badd4338948c78a7c3963c [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
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200156 pixman_region32_init_rect(&output->region, output->x, output->y,
157 output->width, output->height);
158
159 weston_output_update_matrix(output);
160
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200161 /* If a pointer falls outside the outputs new geometry, move it to its
162 * lower-right corner */
163 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500164 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200165 int32_t x, y;
166
167 if (!pointer)
168 continue;
169
170 x = wl_fixed_to_int(pointer->x);
171 y = wl_fixed_to_int(pointer->y);
172
173 if (!pixman_region32_contains_point(&old_output_region,
174 x, y, NULL) ||
175 pixman_region32_contains_point(&output->region,
176 x, y, NULL))
177 continue;
178
179 if (x >= output->x + output->width)
180 x = output->x + output->width - 1;
181 if (y >= output->y + output->height)
182 y = output->y + output->height - 1;
183
184 pointer->x = wl_fixed_from_int(x);
185 pointer->y = wl_fixed_from_int(y);
186 }
187
188 pixman_region32_fini(&old_output_region);
189
Derek Foremandd4cd332014-11-10 10:29:59 -0600190 if (!mode_changed && !scale_changed)
191 return;
192
Hardening57388e42013-09-18 23:56:36 +0200193 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300194 wl_list_for_each(head, &output->head_list, output_link)
195 weston_mode_switch_send_events(head,
196 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600197}
198
David Fort0de859e2016-05-27 23:22:57 +0200199static void
200weston_compositor_reflow_outputs(struct weston_compositor *compositor,
201 struct weston_output *resized_output, int delta_width);
202
Marius Vlad55d87362019-06-11 01:15:35 +0300203/**
204 * \ingroup output
205 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600206WL_EXPORT int
207weston_output_mode_set_native(struct weston_output *output,
208 struct weston_mode *mode,
209 int32_t scale)
210{
211 int ret;
212 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200213 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600214
215 if (!output->switch_mode)
216 return -1;
217
218 if (!output->original_mode) {
219 mode_changed = 1;
220 ret = output->switch_mode(output, mode);
221 if (ret < 0)
222 return ret;
223 if (output->current_scale != scale) {
224 scale_changed = 1;
225 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200226 }
227 }
228
David Fort0de859e2016-05-27 23:22:57 +0200229 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600230 output->native_mode = mode;
231 output->native_scale = scale;
232
233 weston_mode_switch_finish(output, mode_changed, scale_changed);
234
David Fort0de859e2016-05-27 23:22:57 +0200235 if (mode_changed || scale_changed) {
236 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
237
238 wl_signal_emit(&output->compositor->output_resized_signal, output);
239 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600240 return 0;
241}
242
Marius Vlad55d87362019-06-11 01:15:35 +0300243/**
244 * \ingroup output
245 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600246WL_EXPORT int
247weston_output_mode_switch_to_native(struct weston_output *output)
248{
249 int ret;
250 int mode_changed = 0, scale_changed = 0;
251
252 if (!output->switch_mode)
253 return -1;
254
255 if (!output->original_mode) {
256 weston_log("already in the native mode\n");
257 return -1;
258 }
259 /* the non fullscreen clients haven't seen a mode set since we
260 * switched into a temporary, so we need to notify them if the
261 * mode at that time is different from the native mode now.
262 */
263 mode_changed = (output->original_mode != output->native_mode);
264 scale_changed = (output->original_scale != output->native_scale);
265
266 ret = output->switch_mode(output, output->native_mode);
267 if (ret < 0)
268 return ret;
269
270 output->current_scale = output->native_scale;
271
272 output->original_mode = NULL;
273 output->original_scale = 0;
274
275 weston_mode_switch_finish(output, mode_changed, scale_changed);
276
277 return 0;
278}
279
Marius Vlad55d87362019-06-11 01:15:35 +0300280/**
281 * \ingroup output
282 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600283WL_EXPORT int
284weston_output_mode_switch_to_temporary(struct weston_output *output,
285 struct weston_mode *mode,
286 int32_t scale)
287{
288 int ret;
289
290 if (!output->switch_mode)
291 return -1;
292
293 /* original_mode is the last mode non full screen clients have seen,
294 * so we shouldn't change it if we already have one set.
295 */
296 if (!output->original_mode) {
297 output->original_mode = output->native_mode;
298 output->original_scale = output->native_scale;
299 }
300 ret = output->switch_mode(output, mode);
301 if (ret < 0)
302 return ret;
303
304 output->current_scale = scale;
305
306 weston_mode_switch_finish(output, 0, 0);
307
308 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800309}
310
Benjamin Franzke06286262011-05-06 19:12:33 +0200311static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300312region_init_infinite(pixman_region32_t *region)
313{
314 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
315 UINT32_MAX, UINT32_MAX);
316}
317
Pekka Paalanene67858b2013-04-25 13:57:42 +0300318static struct weston_subsurface *
319weston_surface_to_subsurface(struct weston_surface *surface);
320
Jason Ekstranda7af7042013-10-12 22:38:11 -0500321WL_EXPORT struct weston_view *
322weston_view_create(struct weston_surface *surface)
323{
324 struct weston_view *view;
325
Bryce Harringtonde16d892014-11-20 22:21:57 -0800326 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500327 if (view == NULL)
328 return NULL;
329
330 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000331 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500332
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333 /* Assign to surface */
334 wl_list_insert(&surface->views, &view->surface_link);
335
336 wl_signal_init(&view->destroy_signal);
337 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300338 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500339
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340 pixman_region32_init(&view->clip);
341
342 view->alpha = 1.0;
343 pixman_region32_init(&view->transform.opaque);
344
345 wl_list_init(&view->geometry.transformation_list);
346 wl_list_insert(&view->geometry.transformation_list,
347 &view->transform.position.link);
348 weston_matrix_init(&view->transform.position.matrix);
349 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200350 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500351 pixman_region32_init(&view->transform.boundingbox);
352 view->transform.dirty = 1;
353
Jason Ekstranda7af7042013-10-12 22:38:11 -0500354 return view;
355}
356
Jason Ekstrand108865d2014-06-26 10:04:49 -0700357struct weston_frame_callback {
358 struct wl_resource *resource;
359 struct wl_list link;
360};
361
Pekka Paalanen133e4392014-09-23 22:08:46 -0400362struct weston_presentation_feedback {
363 struct wl_resource *resource;
364
365 /* XXX: could use just wl_resource_get_link() instead */
366 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200367
368 /* The per-surface feedback flags */
369 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400370};
371
372static void
373weston_presentation_feedback_discard(
374 struct weston_presentation_feedback *feedback)
375{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200376 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400377 wl_resource_destroy(feedback->resource);
378}
379
380static void
381weston_presentation_feedback_discard_list(struct wl_list *list)
382{
383 struct weston_presentation_feedback *feedback, *tmp;
384
385 wl_list_for_each_safe(feedback, tmp, list, link)
386 weston_presentation_feedback_discard(feedback);
387}
388
389static void
390weston_presentation_feedback_present(
391 struct weston_presentation_feedback *feedback,
392 struct weston_output *output,
393 uint32_t refresh_nsec,
394 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200395 uint64_t seq,
396 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400397{
398 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300399 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400400 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200401 uint32_t tv_sec_hi;
402 uint32_t tv_sec_lo;
403 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300404 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400405
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300406 wl_list_for_each(head, &output->head_list, output_link) {
407 wl_resource_for_each(o, &head->resource_list) {
408 if (wl_resource_get_client(o) != client)
409 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400410
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300411 wp_presentation_feedback_send_sync_output(feedback->resource, o);
412 done = true;
413 }
414
415 /* For clone mode, send it for just one wl_output global,
416 * they are all equivalent anyway.
417 */
418 if (done)
419 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400420 }
421
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200422 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200423 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200424 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200425 refresh_nsec,
426 seq >> 32, seq & 0xffffffff,
427 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400428 wl_resource_destroy(feedback->resource);
429}
430
431static void
432weston_presentation_feedback_present_list(struct wl_list *list,
433 struct weston_output *output,
434 uint32_t refresh_nsec,
435 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200436 uint64_t seq,
437 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400438{
439 struct weston_presentation_feedback *feedback, *tmp;
440
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200441 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200442 wl_list_empty(list));
443
Pekka Paalanen133e4392014-09-23 22:08:46 -0400444 wl_list_for_each_safe(feedback, tmp, list, link)
445 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200446 refresh_nsec, ts, seq,
447 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400448}
449
Jason Ekstrand7b982072014-05-20 14:33:03 -0500450static void
451surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
452{
453 struct weston_surface_state *state =
454 container_of(listener, struct weston_surface_state,
455 buffer_destroy_listener);
456
457 state->buffer = NULL;
458}
459
460static void
461weston_surface_state_init(struct weston_surface_state *state)
462{
463 state->newly_attached = 0;
464 state->buffer = NULL;
465 state->buffer_destroy_listener.notify =
466 surface_state_handle_buffer_destroy;
467 state->sx = 0;
468 state->sy = 0;
469
Derek Foreman152254b2015-11-26 14:17:48 -0600470 pixman_region32_init(&state->damage_surface);
471 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500472 pixman_region32_init(&state->opaque);
473 region_init_infinite(&state->input);
474
475 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400476 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500477
478 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
479 state->buffer_viewport.buffer.scale = 1;
480 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
481 state->buffer_viewport.surface.width = -1;
482 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300483
484 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530485
486 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530487 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500488}
489
490static void
491weston_surface_state_fini(struct weston_surface_state *state)
492{
493 struct weston_frame_callback *cb, *next;
494
495 wl_list_for_each_safe(cb, next,
496 &state->frame_callback_list, link)
497 wl_resource_destroy(cb->resource);
498
Pekka Paalanen133e4392014-09-23 22:08:46 -0400499 weston_presentation_feedback_discard_list(&state->feedback_list);
500
Jason Ekstrand7b982072014-05-20 14:33:03 -0500501 pixman_region32_fini(&state->input);
502 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600503 pixman_region32_fini(&state->damage_surface);
504 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500505
506 if (state->buffer)
507 wl_list_remove(&state->buffer_destroy_listener.link);
508 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300509
510 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300511 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500512}
513
514static void
515weston_surface_state_set_buffer(struct weston_surface_state *state,
516 struct weston_buffer *buffer)
517{
518 if (state->buffer == buffer)
519 return;
520
521 if (state->buffer)
522 wl_list_remove(&state->buffer_destroy_listener.link);
523 state->buffer = buffer;
524 if (state->buffer)
525 wl_signal_add(&state->buffer->destroy_signal,
526 &state->buffer_destroy_listener);
527}
528
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500529WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500530weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500531{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500532 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400533
Bryce Harringtonde16d892014-11-20 22:21:57 -0800534 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400535 if (surface == NULL)
536 return NULL;
537
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500538 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800539 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500540
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500541 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200542 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400543
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200544 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
545 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200546 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
547 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500548
549 weston_surface_state_init(&surface->pending);
550
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400551 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500552 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300553 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400554
Jason Ekstranda7af7042013-10-12 22:38:11 -0500555 wl_list_init(&surface->views);
556
557 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400558 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500559
Pekka Paalanene67858b2013-04-25 13:57:42 +0300560 wl_list_init(&surface->subsurface_list);
561 wl_list_init(&surface->subsurface_list_pending);
562
Jason Ekstrand1e059042014-10-16 10:55:19 -0500563 weston_matrix_init(&surface->buffer_to_surface_matrix);
564 weston_matrix_init(&surface->surface_to_buffer_matrix);
565
Jonas Ådahld3414f22016-07-22 17:56:31 +0800566 wl_list_init(&surface->pointer_constraints);
567
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300568 surface->acquire_fence_fd = -1;
569
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530570 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530571 surface->current_protection = WESTON_HDCP_DISABLE;
572 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530573
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400574 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500575}
576
Alex Wu8811bf92012-02-28 18:07:54 +0800577WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500578weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200579 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500580{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100581 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200582 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500583}
584
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400585WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586weston_view_to_global_float(struct weston_view *view,
587 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200588{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500589 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200590 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
591
Jason Ekstranda7af7042013-10-12 22:38:11 -0500592 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200593
594 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200595 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700596 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200597 v.f[3]);
598 *x = 0;
599 *y = 0;
600 return;
601 }
602
603 *x = v.f[0] / v.f[3];
604 *y = v.f[1] / v.f[3];
605 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500606 *x = sx + view->geometry.x;
607 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200608 }
609}
610
Pekka Paalanen0df44772020-02-14 14:50:03 +0200611/** Transform a point to buffer coordinates
612 *
613 * \param width Surface width.
614 * \param height Surface height.
615 * \param transform Buffer transform.
616 * \param scale Buffer scale.
617 * \param sx Surface x coordinate of a point.
618 * \param sy Surface y coordinate of a point.
619 * \param[out] bx Buffer x coordinate of the point.
620 * \param[out] by Buffer Y coordinate of the point.
621 *
622 * Converts the given surface-local coordinates to buffer coordinates
623 * according to the given buffer transform and scale.
624 * This ignores wp_viewport.
625 *
626 * The given width and height must be the result of inverse scaled and
627 * inverse transformed buffer size.
628 */
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500629WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200630weston_transformed_coord(int width, int height,
631 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200632 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200633 float sx, float sy, float *bx, float *by)
634{
635 switch (transform) {
636 case WL_OUTPUT_TRANSFORM_NORMAL:
637 default:
638 *bx = sx;
639 *by = sy;
640 break;
641 case WL_OUTPUT_TRANSFORM_FLIPPED:
642 *bx = width - sx;
643 *by = sy;
644 break;
645 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200646 *bx = sy;
647 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200648 break;
649 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200650 *bx = sy;
651 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200652 break;
653 case WL_OUTPUT_TRANSFORM_180:
654 *bx = width - sx;
655 *by = height - sy;
656 break;
657 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
658 *bx = sx;
659 *by = height - sy;
660 break;
661 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200662 *bx = height - sy;
663 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200664 break;
665 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200666 *bx = height - sy;
667 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200668 break;
669 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200670
671 *bx *= scale;
672 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200673}
674
Pekka Paalanen0df44772020-02-14 14:50:03 +0200675/** Transform a rectangle to buffer coordinates
676 *
677 * \param width Surface width.
678 * \param height Surface height.
679 * \param transform Buffer transform.
680 * \param scale Buffer scale.
681 * \param rect Rectangle in surface coordinates.
682 * \return Rectangle in buffer coordinates.
683 *
684 * Converts the given surface-local rectangle to buffer coordinates
685 * according to the given buffer transform and scale. The resulting
686 * rectangle is guaranteed to be well-formed.
687 * This ignores wp_viewport.
688 *
689 * The given width and height must be the result of inverse scaled and
690 * inverse transformed buffer size.
691 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200692WL_EXPORT pixman_box32_t
693weston_transformed_rect(int width, int height,
694 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200695 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200696 pixman_box32_t rect)
697{
698 float x1, x2, y1, y2;
699
700 pixman_box32_t ret;
701
Alexander Larsson4ea95522013-05-22 14:41:37 +0200702 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200703 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200704 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200705 rect.x2, rect.y2, &x2, &y2);
706
707 if (x1 <= x2) {
708 ret.x1 = x1;
709 ret.x2 = x2;
710 } else {
711 ret.x1 = x2;
712 ret.x2 = x1;
713 }
714
715 if (y1 <= y2) {
716 ret.y1 = y1;
717 ret.y2 = y2;
718 } else {
719 ret.y1 = y2;
720 ret.y2 = y1;
721 }
722
723 return ret;
724}
725
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600726/** Transform a region by a matrix, restricted to axis-aligned transformations
727 *
728 * Warning: This function does not work for projective, affine, or matrices
729 * that encode arbitrary rotations. Only 90-degree step rotations are
730 * supported.
731 */
732WL_EXPORT void
733weston_matrix_transform_region(pixman_region32_t *dest,
734 struct weston_matrix *matrix,
735 pixman_region32_t *src)
736{
737 pixman_box32_t *src_rects, *dest_rects;
738 int nrects, i;
739
740 src_rects = pixman_region32_rectangles(src, &nrects);
741 dest_rects = malloc(nrects * sizeof(*dest_rects));
742 if (!dest_rects)
743 return;
744
745 for (i = 0; i < nrects; i++) {
746 struct weston_vector vec1 = {{
747 src_rects[i].x1, src_rects[i].y1, 0, 1
748 }};
749 weston_matrix_transform(matrix, &vec1);
750 vec1.f[0] /= vec1.f[3];
751 vec1.f[1] /= vec1.f[3];
752
753 struct weston_vector vec2 = {{
754 src_rects[i].x2, src_rects[i].y2, 0, 1
755 }};
756 weston_matrix_transform(matrix, &vec2);
757 vec2.f[0] /= vec2.f[3];
758 vec2.f[1] /= vec2.f[3];
759
760 if (vec1.f[0] < vec2.f[0]) {
761 dest_rects[i].x1 = floor(vec1.f[0]);
762 dest_rects[i].x2 = ceil(vec2.f[0]);
763 } else {
764 dest_rects[i].x1 = floor(vec2.f[0]);
765 dest_rects[i].x2 = ceil(vec1.f[0]);
766 }
767
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600768 if (vec1.f[1] < vec2.f[1]) {
769 dest_rects[i].y1 = floor(vec1.f[1]);
770 dest_rects[i].y2 = ceil(vec2.f[1]);
771 } else {
772 dest_rects[i].y1 = floor(vec2.f[1]);
773 dest_rects[i].y2 = ceil(vec1.f[1]);
774 }
775 }
776
777 pixman_region32_clear(dest);
778 pixman_region32_init_rects(dest, dest_rects, nrects);
779 free(dest_rects);
780}
781
Pekka Paalanen0df44772020-02-14 14:50:03 +0200782/** Transform a region to buffer coordinates
783 *
784 * \param width Surface width.
785 * \param height Surface height.
786 * \param transform Buffer transform.
787 * \param scale Buffer scale.
788 * \param[in] src Region in surface coordinates.
789 * \param[out] dest Resulting region in buffer coordinates.
790 *
791 * Converts the given surface-local region to buffer coordinates
792 * according to the given buffer transform and scale.
793 * This ignores wp_viewport.
794 *
795 * The given width and height must be the result of inverse scaled and
796 * inverse transformed buffer size.
797 *
798 * src and dest are allowed to point to the same memory for in-place conversion.
799 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200800WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500801weston_transformed_region(int width, int height,
802 enum wl_output_transform transform,
803 int32_t scale,
804 pixman_region32_t *src, pixman_region32_t *dest)
805{
806 pixman_box32_t *src_rects, *dest_rects;
807 int nrects, i;
808
809 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
810 if (src != dest)
811 pixman_region32_copy(dest, src);
812 return;
813 }
814
815 src_rects = pixman_region32_rectangles(src, &nrects);
816 dest_rects = malloc(nrects * sizeof(*dest_rects));
817 if (!dest_rects)
818 return;
819
820 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
821 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
822 } else {
823 for (i = 0; i < nrects; i++) {
824 switch (transform) {
825 default:
826 case WL_OUTPUT_TRANSFORM_NORMAL:
827 dest_rects[i].x1 = src_rects[i].x1;
828 dest_rects[i].y1 = src_rects[i].y1;
829 dest_rects[i].x2 = src_rects[i].x2;
830 dest_rects[i].y2 = src_rects[i].y2;
831 break;
832 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200833 dest_rects[i].x1 = src_rects[i].y1;
834 dest_rects[i].y1 = width - src_rects[i].x2;
835 dest_rects[i].x2 = src_rects[i].y2;
836 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500837 break;
838 case WL_OUTPUT_TRANSFORM_180:
839 dest_rects[i].x1 = width - src_rects[i].x2;
840 dest_rects[i].y1 = height - src_rects[i].y2;
841 dest_rects[i].x2 = width - src_rects[i].x1;
842 dest_rects[i].y2 = height - src_rects[i].y1;
843 break;
844 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200845 dest_rects[i].x1 = height - src_rects[i].y2;
846 dest_rects[i].y1 = src_rects[i].x1;
847 dest_rects[i].x2 = height - src_rects[i].y1;
848 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500849 break;
850 case WL_OUTPUT_TRANSFORM_FLIPPED:
851 dest_rects[i].x1 = width - src_rects[i].x2;
852 dest_rects[i].y1 = src_rects[i].y1;
853 dest_rects[i].x2 = width - src_rects[i].x1;
854 dest_rects[i].y2 = src_rects[i].y2;
855 break;
856 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200857 dest_rects[i].x1 = src_rects[i].y1;
858 dest_rects[i].y1 = src_rects[i].x1;
859 dest_rects[i].x2 = src_rects[i].y2;
860 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500861 break;
862 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
863 dest_rects[i].x1 = src_rects[i].x1;
864 dest_rects[i].y1 = height - src_rects[i].y2;
865 dest_rects[i].x2 = src_rects[i].x2;
866 dest_rects[i].y2 = height - src_rects[i].y1;
867 break;
868 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200869 dest_rects[i].x1 = height - src_rects[i].y2;
870 dest_rects[i].y1 = width - src_rects[i].x2;
871 dest_rects[i].x2 = height - src_rects[i].y1;
872 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500873 break;
874 }
875 }
876 }
877
878 if (scale != 1) {
879 for (i = 0; i < nrects; i++) {
880 dest_rects[i].x1 *= scale;
881 dest_rects[i].x2 *= scale;
882 dest_rects[i].y1 *= scale;
883 dest_rects[i].y2 *= scale;
884 }
885 }
886
887 pixman_region32_clear(dest);
888 pixman_region32_init_rects(dest, dest_rects, nrects);
889 free(dest_rects);
890}
891
Jonny Lamb74130762013-11-26 18:19:46 +0100892static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300893viewport_surface_to_buffer(struct weston_surface *surface,
894 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100895{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200896 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200897 double src_width, src_height;
898 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200899
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200900 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
901 if (vp->surface.width == -1) {
902 *bx = sx;
903 *by = sy;
904 return;
905 }
Jonny Lamb74130762013-11-26 18:19:46 +0100906
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200907 src_x = 0.0;
908 src_y = 0.0;
909 src_width = surface->width_from_buffer;
910 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100911 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200912 src_x = wl_fixed_to_double(vp->buffer.src_x);
913 src_y = wl_fixed_to_double(vp->buffer.src_y);
914 src_width = wl_fixed_to_double(vp->buffer.src_width);
915 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100916 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200917
918 *bx = sx * src_width / surface->width + src_x;
919 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100920}
921
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500922WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200923weston_surface_to_buffer_float(struct weston_surface *surface,
924 float sx, float sy, float *bx, float *by)
925{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200926 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
927
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300928 /* first transform coordinates if the viewport is set */
929 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100930
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500931 weston_transformed_coord(surface->width_from_buffer,
932 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200933 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100934 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200935}
936
Derek Foreman99739672015-12-03 16:38:11 -0600937/** Transform a rectangle from surface coordinates to buffer coordinates
938 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300939 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600940 * from.
941 * \param rect The rectangle to transform.
942 * \return The transformed rectangle.
943 *
944 * Viewport and buffer transformations can only do translation, scaling,
945 * and rotations in 90-degree steps. Therefore the only loss in the
946 * conversion is coordinate rounding.
947 *
948 * However, some coordinate rounding takes place as an intermediate
949 * step before the buffer scale factor is applied, so the rectangle
950 * boundary may not be exactly as expected.
951 *
952 * This is OK for damage tracking since a little extra coverage is
953 * not a problem.
954 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200955WL_EXPORT pixman_box32_t
956weston_surface_to_buffer_rect(struct weston_surface *surface,
957 pixman_box32_t rect)
958{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200959 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100960 float xf, yf;
961
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300962 /* first transform box coordinates if the viewport is set */
963 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100964 rect.x1 = floorf(xf);
965 rect.y1 = floorf(yf);
966
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300967 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600968 rect.x2 = ceilf(xf);
969 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100970
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500971 return weston_transformed_rect(surface->width_from_buffer,
972 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200973 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200974 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200975}
976
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200977/** Transform a region from surface coordinates to buffer coordinates
978 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300979 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200980 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300981 * \param[in] surface_region The region in surface coordinates.
982 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200983 *
984 * Buffer_region must be init'd, but will be completely overwritten.
985 *
986 * Viewport and buffer transformations can only do translation, scaling,
987 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600988 * conversion is from the coordinate rounding that takes place in
989 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300990 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200991 */
992WL_EXPORT void
993weston_surface_to_buffer_region(struct weston_surface *surface,
994 pixman_region32_t *surface_region,
995 pixman_region32_t *buffer_region)
996{
997 pixman_box32_t *src_rects, *dest_rects;
998 int nrects, i;
999
1000 src_rects = pixman_region32_rectangles(surface_region, &nrects);
1001 dest_rects = malloc(nrects * sizeof(*dest_rects));
1002 if (!dest_rects)
1003 return;
1004
1005 for (i = 0; i < nrects; i++) {
1006 dest_rects[i] = weston_surface_to_buffer_rect(surface,
1007 src_rects[i]);
1008 }
1009
1010 pixman_region32_fini(buffer_region);
1011 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
1012 free(dest_rects);
1013}
1014
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001015WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001017 struct weston_plane *plane)
1018{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001020 return;
1021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 weston_view_damage_below(view);
1023 view->plane = plane;
1024 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001025}
1026
Pekka Paalanen51723d52015-02-17 13:10:01 +02001027/** Inflict damage on the plane where the view is visible.
1028 *
1029 * \param view The view that causes the damage.
1030 *
1031 * If the view is currently on a plane (including the primary plane),
1032 * take the view's boundingbox, subtract all the opaque views that cover it,
1033 * and add the remaining region as damage to the plane. This corresponds
1034 * to the damage inflicted to the plane if this view disappeared.
1035 *
1036 * A repaint is scheduled for this view.
1037 *
1038 * The region of all opaque views covering this view is stored in
1039 * weston_view::clip and updated by view_accumulate_damage() during
1040 * weston_output_repaint(). Specifically, that region matches the
1041 * scenegraph as it was last painted.
1042 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001043WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001045{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001046 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001047
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001048 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001049 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001051 if (view->plane)
1052 pixman_region32_union(&view->plane->damage,
1053 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001054 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001055 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001056}
1057
Pekka Paalanen01e00682017-03-24 16:21:06 +02001058/** Send wl_surface.enter/leave events
1059 *
1060 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001061 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001062 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001063 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001064 *
1065 * Send the enter/leave events for all protocol objects bound to the given
1066 * output by the client owning the surface.
1067 */
1068static void
1069weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001070 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001071 bool enter,
1072 bool leave)
1073{
1074 struct wl_resource *wloutput;
1075 struct wl_client *client;
1076
1077 assert(enter != leave);
1078
1079 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001080 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001081 if (wl_resource_get_client(wloutput) != client)
1082 continue;
1083
1084 if (enter)
1085 wl_surface_send_enter(surface->resource, wloutput);
1086 if (leave)
1087 wl_surface_send_leave(surface->resource, wloutput);
1088 }
1089}
1090
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301091static void
1092weston_surface_compute_protection(struct protected_surface *psurface)
1093{
1094 enum weston_hdcp_protection min_protection;
1095 bool min_protection_valid = false;
1096 struct weston_surface *surface = psurface->surface;
1097 struct weston_output *output;
1098
1099 wl_list_for_each(output, &surface->compositor->output_list, link)
1100 if (surface->output_mask & (1u << output->id)) {
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301101 /*
1102 * If the content-protection is enabled with protection
1103 * mode as RELAXED for a surface, and if
1104 * content-recording features like: screen-shooter,
1105 * recorder, screen-sharing, etc are on, then notify the
1106 * client, that the protection is disabled.
1107 *
1108 * Note: If the protection mode is ENFORCED then there
1109 * is no need to bother the client as the renderer takes
1110 * care of censoring the visibility of the protected
1111 * content.
1112 */
1113
1114 if (output->disable_planes > 0 &&
1115 surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_RELAXED) {
1116 min_protection = WESTON_HDCP_DISABLE;
1117 min_protection_valid = true;
1118 break;
1119 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301120 if (!min_protection_valid) {
1121 min_protection = output->current_protection;
1122 min_protection_valid = true;
1123 }
1124 if (output->current_protection < min_protection)
1125 min_protection = output->current_protection;
1126 }
1127 if (!min_protection_valid)
1128 min_protection = WESTON_HDCP_DISABLE;
1129
1130 surface->current_protection = min_protection;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301131
1132 weston_protected_surface_send_event(psurface, surface->current_protection);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301133}
1134
1135static void
1136notify_surface_protection_change(void *data)
1137{
1138 struct weston_compositor *compositor = data;
1139 struct content_protection *cp;
1140 struct protected_surface *psurface;
1141
1142 cp = compositor->content_protection;
1143 cp->surface_protection_update = NULL;
1144
1145 /* Notify the clients, whose surfaces are changed */
1146 wl_list_for_each(psurface, &cp->protected_list, link)
1147 if (psurface && psurface->surface)
1148 weston_surface_compute_protection(psurface);
1149}
1150
Bryce Harrington3f650b82015-12-23 11:01:58 -08001151/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301152 * \param compositor weston_compositor
1153 *
1154 * Schedule an idle task to notify surface about the update in protection,
1155 * if not already scheduled.
1156 */
1157static void
1158weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1159{
1160 struct content_protection *cp = compositor->content_protection;
1161 struct wl_event_loop *loop;
1162
1163 if (!cp || cp->surface_protection_update)
1164 return;
1165 loop = wl_display_get_event_loop(compositor->wl_display);
1166 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1167 notify_surface_protection_change,
1168 compositor);
1169}
1170
1171/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001172 * \param es The surface
1173 * \param mask The new set of outputs for the surface
1174 *
1175 * Sets the surface's set of outputs to the ones specified by
1176 * the new output mask provided. Identifies the outputs that
1177 * have changed, the posts enter and leave events for these
1178 * outputs as appropriate.
1179 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001180static void
1181weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1182{
1183 uint32_t different = es->output_mask ^ mask;
1184 uint32_t entered = mask & different;
1185 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001186 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001187 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001188 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001189
1190 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001191 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001192 return;
1193 if (different == 0)
1194 return;
1195
1196 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001197 output_bit = 1u << output->id;
1198 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001199 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001200
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001201 wl_list_for_each(head, &output->head_list, output_link) {
1202 weston_surface_send_enter_leave(es, head,
1203 output_bit & entered,
1204 output_bit & left);
1205 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001206 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301207 /*
1208 * Change in surfaces' output mask might trigger a change in its
1209 * protection.
1210 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301211 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001212}
1213
Semi Malinene7a52fb2018-04-26 11:08:10 +02001214static void
1215notify_view_output_destroy(struct wl_listener *listener, void *data)
1216{
1217 struct weston_view *view =
1218 container_of(listener,
1219 struct weston_view, output_destroy_listener);
1220
1221 view->output = NULL;
1222 view->output_destroy_listener.notify = NULL;
1223}
1224
1225/** Set the primary output of the view
1226 *
1227 * \param view The view whose primary output to set
1228 * \param output The new primary output for the view
1229 *
1230 * Set \a output to be the primary output of the \a view.
1231 *
1232 * Notice that the assignment may be temporary; the primary output could be
1233 * automatically changed. Hence, one cannot rely on the value persisting.
1234 *
1235 * Passing NULL as /a output will set the primary output to NULL.
1236 */
1237WL_EXPORT void
1238weston_view_set_output(struct weston_view *view, struct weston_output *output)
1239{
1240 if (view->output_destroy_listener.notify) {
1241 wl_list_remove(&view->output_destroy_listener.link);
1242 view->output_destroy_listener.notify = NULL;
1243 }
1244 view->output = output;
1245 if (output) {
1246 view->output_destroy_listener.notify =
1247 notify_view_output_destroy;
1248 wl_signal_add(&output->destroy_signal,
1249 &view->output_destroy_listener);
1250 }
1251}
1252
Bryce Harrington3f650b82015-12-23 11:01:58 -08001253/** Recalculate which output(s) the surface has views displayed on
1254 *
1255 * \param es The surface to remap to outputs
1256 *
1257 * Finds the output that is showing the largest amount of one
1258 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001259 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001260 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001261 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001262 * in the output_mask for the surface.
1263 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001264static void
1265weston_surface_assign_output(struct weston_surface *es)
1266{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 struct weston_output *new_output;
1268 struct weston_view *view;
1269 pixman_region32_t region;
1270 uint32_t max, area, mask;
1271 pixman_box32_t *e;
1272
1273 new_output = NULL;
1274 max = 0;
1275 mask = 0;
1276 pixman_region32_init(&region);
1277 wl_list_for_each(view, &es->views, surface_link) {
1278 if (!view->output)
1279 continue;
1280
1281 pixman_region32_intersect(&region, &view->transform.boundingbox,
1282 &view->output->region);
1283
1284 e = pixman_region32_extents(&region);
1285 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1286
1287 mask |= view->output_mask;
1288
1289 if (area >= max) {
1290 new_output = view->output;
1291 max = area;
1292 }
1293 }
1294 pixman_region32_fini(&region);
1295
1296 es->output = new_output;
1297 weston_surface_update_output_mask(es, mask);
1298}
1299
Bryce Harrington3f650b82015-12-23 11:01:58 -08001300/** Recalculate which output(s) the view is displayed on
1301 *
1302 * \param ev The view to remap to outputs
1303 *
1304 * Identifies the set of outputs that the view is visible on,
1305 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001306 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001307 *
1308 * Also does the same for the view's surface. See
1309 * weston_surface_assign_output().
1310 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311static void
1312weston_view_assign_output(struct weston_view *ev)
1313{
1314 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001315 struct weston_output *output, *new_output;
1316 pixman_region32_t region;
1317 uint32_t max, area, mask;
1318 pixman_box32_t *e;
1319
1320 new_output = NULL;
1321 max = 0;
1322 mask = 0;
1323 pixman_region32_init(&region);
1324 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001325 if (output->destroying)
1326 continue;
1327
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001329 &output->region);
1330
1331 e = pixman_region32_extents(&region);
1332 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1333
1334 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001335 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001336
1337 if (area >= max) {
1338 new_output = output;
1339 max = area;
1340 }
1341 }
1342 pixman_region32_fini(&region);
1343
Semi Malinene7a52fb2018-04-26 11:08:10 +02001344 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 ev->output_mask = mask;
1346
1347 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001348}
1349
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001350static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001351weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1352 int from_x, int from_y, int *to_x, int *to_y)
1353{
1354 float x, y;
1355
1356 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1357 weston_view_from_global_float(to, x, y, &x, &y);
1358
1359 *to_x = round(x);
1360 *to_y = round(y);
1361}
1362
1363static void
1364weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1365{
1366 pixman_box32_t *a;
1367 pixman_box32_t b;
1368
1369 a = pixman_region32_extents(&from->geometry.scissor);
1370
1371 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1372 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1373
1374 pixman_region32_fini(&to->geometry.scissor);
1375 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1376}
1377
1378static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001379view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001381{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001382 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1383 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001384 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001385 { inbox->x1, inbox->y1 },
1386 { inbox->x1, inbox->y2 },
1387 { inbox->x2, inbox->y1 },
1388 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001389 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001390 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001391 int i;
1392
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001393 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001394 /* avoid rounding empty bbox to 1x1 */
1395 pixman_region32_init(bbox);
1396 return;
1397 }
1398
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001399 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001400 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001402 if (x < min_x)
1403 min_x = x;
1404 if (x > max_x)
1405 max_x = x;
1406 if (y < min_y)
1407 min_y = y;
1408 if (y > max_y)
1409 max_y = y;
1410 }
1411
Pekka Paalanen219b9822012-02-08 15:38:37 +02001412 int_x = floorf(min_x);
1413 int_y = floorf(min_y);
1414 pixman_region32_init_rect(bbox, int_x, int_y,
1415 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001416}
1417
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001418static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001420{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001422
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001423 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 view->geometry.x = roundf(view->geometry.x);
1425 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001426
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001427 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1429 view->transform.position.matrix.d[12] = view->geometry.x;
1430 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001433
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 view->transform.inverse = view->transform.position.matrix;
1435 view->transform.inverse.d[12] = -view->geometry.x;
1436 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001437
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001439 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001440 view->surface->width,
1441 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001442 if (view->geometry.scissor_enabled)
1443 pixman_region32_intersect(&view->transform.boundingbox,
1444 &view->transform.boundingbox,
1445 &view->geometry.scissor);
1446
1447 pixman_region32_translate(&view->transform.boundingbox,
1448 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 if (view->alpha == 1.0) {
1451 pixman_region32_copy(&view->transform.opaque,
1452 &view->surface->opaque);
1453 pixman_region32_translate(&view->transform.opaque,
1454 view->geometry.x,
1455 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001456 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001457}
1458
1459static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001461{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 struct weston_view *parent = view->geometry.parent;
1463 struct weston_matrix *matrix = &view->transform.matrix;
1464 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001465 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001466 pixman_region32_t surfregion;
1467 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001468
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001470
1471 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1473 view->transform.position.matrix.d[12] = view->geometry.x;
1474 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001475
1476 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001478 weston_matrix_multiply(matrix, &tform->matrix);
1479
Pekka Paalanen483243f2013-03-08 14:56:50 +02001480 if (parent)
1481 weston_matrix_multiply(matrix, &parent->transform.matrix);
1482
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001483 if (weston_matrix_invert(inverse, matrix) < 0) {
1484 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001485 weston_log("error: weston_view %p"
1486 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001487 return -1;
1488 }
1489
Giulio Camuffo148c1992016-09-04 18:50:46 +03001490 if (view->alpha == 1.0 &&
1491 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1492 pixman_region32_copy(&view->transform.opaque,
1493 &view->surface->opaque);
1494 pixman_region32_translate(&view->transform.opaque,
1495 matrix->d[12],
1496 matrix->d[13]);
1497 }
1498
Pekka Paalanen380adf52015-02-16 14:39:11 +02001499 pixman_region32_init_rect(&surfregion, 0, 0,
1500 view->surface->width, view->surface->height);
1501 if (view->geometry.scissor_enabled)
1502 pixman_region32_intersect(&surfregion, &surfregion,
1503 &view->geometry.scissor);
1504 surfbox = pixman_region32_extents(&surfregion);
1505
1506 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1507 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001508
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001509 return 0;
1510}
1511
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001512static struct weston_layer *
1513get_view_layer(struct weston_view *view)
1514{
1515 if (view->parent_view)
1516 return get_view_layer(view->parent_view);
1517 return view->layer_link.layer;
1518}
1519
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001520WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001522{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001524 struct weston_layer *layer;
1525 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001526
Jason Ekstranda7af7042013-10-12 22:38:11 -05001527 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001528 return;
1529
Pekka Paalanen483243f2013-03-08 14:56:50 +02001530 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001532
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001534
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001536
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 pixman_region32_fini(&view->transform.boundingbox);
1538 pixman_region32_fini(&view->transform.opaque);
1539 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001540
Pekka Paalanencd403622012-01-25 13:37:39 +02001541 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001542 if (view->geometry.transformation_list.next ==
1543 &view->transform.position.link &&
1544 view->geometry.transformation_list.prev ==
1545 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001546 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001548 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001549 if (weston_view_update_transform_enable(view) < 0)
1550 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001551 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001552
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001553 layer = get_view_layer(view);
1554 if (layer) {
1555 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001556 pixman_region32_intersect(&view->transform.boundingbox,
1557 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001558 pixman_region32_intersect(&view->transform.opaque,
1559 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001560 pixman_region32_fini(&mask);
1561 }
1562
Pekka Paalanen380adf52015-02-16 14:39:11 +02001563 if (parent) {
1564 if (parent->geometry.scissor_enabled) {
1565 view->geometry.scissor_enabled = true;
1566 weston_view_transfer_scissor(parent, view);
1567 } else {
1568 view->geometry.scissor_enabled = false;
1569 }
1570 }
1571
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001573
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001575
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 wl_signal_emit(&view->surface->compositor->transform_signal,
1577 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001578}
1579
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001580WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001581weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001582{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001584
1585 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 * The invariant: if view->geometry.dirty, then all views
1587 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001588 * Corollary: if not parent->geometry.dirty, then all ancestors
1589 * are not dirty.
1590 */
1591
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001593 return;
1594
Jason Ekstranda7af7042013-10-12 22:38:11 -05001595 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001596
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001598 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001600}
1601
1602WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603weston_view_to_global_fixed(struct weston_view *view,
1604 wl_fixed_t vx, wl_fixed_t vy,
1605 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001606{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001607 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001608
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 weston_view_to_global_float(view,
1610 wl_fixed_to_double(vx),
1611 wl_fixed_to_double(vy),
1612 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001613 *x = wl_fixed_from_double(xf);
1614 *y = wl_fixed_from_double(yf);
1615}
1616
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001617WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001618weston_view_from_global_float(struct weston_view *view,
1619 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001620{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001621 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001622 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1623
Jason Ekstranda7af7042013-10-12 22:38:11 -05001624 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001625
1626 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001627 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001628 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001629 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001630 *vx = 0;
1631 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001632 return;
1633 }
1634
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635 *vx = v.f[0] / v.f[3];
1636 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001637 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 *vx = x - view->geometry.x;
1639 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001640 }
1641}
1642
1643WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644weston_view_from_global_fixed(struct weston_view *view,
1645 wl_fixed_t x, wl_fixed_t y,
1646 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001647{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001648 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001649
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 weston_view_from_global_float(view,
1651 wl_fixed_to_double(x),
1652 wl_fixed_to_double(y),
1653 &vxf, &vyf);
1654 *vx = wl_fixed_from_double(vxf);
1655 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001656}
1657
1658WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001659weston_view_from_global(struct weston_view *view,
1660 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001661{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001662 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001663
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1665 *vx = floorf(vxf);
1666 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001667}
1668
Bryce Harrington3f650b82015-12-23 11:01:58 -08001669/**
1670 * \param surface The surface to be repainted
1671 *
1672 * Marks the output(s) that the surface is shown on as needing to be
1673 * repainted. See weston_output_schedule_repaint().
1674 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001675WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001676weston_surface_schedule_repaint(struct weston_surface *surface)
1677{
1678 struct weston_output *output;
1679
1680 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001681 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001682 weston_output_schedule_repaint(output);
1683}
1684
Bryce Harrington3f650b82015-12-23 11:01:58 -08001685/**
1686 * \param view The view to be repainted
1687 *
1688 * Marks the output(s) that the view is shown on as needing to be
1689 * repainted. See weston_output_schedule_repaint().
1690 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001691WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692weston_view_schedule_repaint(struct weston_view *view)
1693{
1694 struct weston_output *output;
1695
1696 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001697 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698 weston_output_schedule_repaint(output);
1699}
1700
Pekka Paalanene508ce62015-02-19 13:59:55 +02001701/**
1702 * XXX: This function does it the wrong way.
1703 * surface->damage is the damage from the client, and causes
1704 * surface_flush_damage() to copy pixels. No window management action can
1705 * cause damage to the client-provided content, warranting re-upload!
1706 *
1707 * Instead of surface->damage, this function should record the damage
1708 * with all the views for this surface to avoid extraneous texture
1709 * uploads.
1710 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001711WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001712weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001713{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001714 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715 0, 0, surface->width,
1716 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001717
Kristian Høgsberg98238702012-08-03 16:29:12 -04001718 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001719}
1720
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001721WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001722weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001723{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001724 if (view->geometry.x == x && view->geometry.y == y)
1725 return;
1726
Jason Ekstranda7af7042013-10-12 22:38:11 -05001727 view->geometry.x = x;
1728 view->geometry.y = y;
1729 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001730}
1731
Pekka Paalanen483243f2013-03-08 14:56:50 +02001732static void
1733transform_parent_handle_parent_destroy(struct wl_listener *listener,
1734 void *data)
1735{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736 struct weston_view *view =
1737 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001738 geometry.parent_destroy_listener);
1739
Jason Ekstranda7af7042013-10-12 22:38:11 -05001740 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001741}
1742
1743WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001745 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001746{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747 if (view->geometry.parent) {
1748 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1749 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001750
1751 if (!parent)
1752 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001753 }
1754
Jason Ekstranda7af7042013-10-12 22:38:11 -05001755 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001756
Jason Ekstranda7af7042013-10-12 22:38:11 -05001757 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001758 transform_parent_handle_parent_destroy;
1759 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001760 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001761 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001762 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001764 }
1765
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766 weston_view_geometry_dirty(view);
1767}
1768
Pekka Paalanen380adf52015-02-16 14:39:11 +02001769/** Set a clip mask rectangle on a view
1770 *
1771 * \param view The view to set the clip mask on.
1772 * \param x Top-left corner X coordinate of the clip rectangle.
1773 * \param y Top-left corner Y coordinate of the clip rectangle.
1774 * \param width Width of the clip rectangle, non-negative.
1775 * \param height Height of the clip rectangle, non-negative.
1776 *
1777 * A shell may set a clip mask rectangle on a view. Everything outside
1778 * the rectangle is cut away for input and output purposes: it is
1779 * not drawn and cannot be hit by hit-test based input like pointer
1780 * motion or touch-downs. Everything inside the rectangle will behave
1781 * normally. Clients are unaware of clipping.
1782 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001783 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001784 * mask rectangle does not affect the view position, the view is positioned
1785 * as it would be without a clip. The clip also does not change
1786 * weston_surface::width,height.
1787 *
1788 * The clip mask rectangle is part of transformation inheritance
1789 * (weston_view_set_transform_parent()). A clip set in the root of the
1790 * transformation inheritance tree will affect all views in the tree.
1791 * A clip can be set only on the root view. Attempting to set a clip
1792 * on view that has a transformation parent will fail. Assigning a parent
1793 * to a view that has a clip set will cause the clip to be forgotten.
1794 *
1795 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1796 * on the additional transformations in the child views. These transformations
1797 * may not rotate the coordinate axes, i.e., only translation and scaling
1798 * are allowed. Violating this restriction causes the clipping to malfunction.
1799 * Furthermore, using scaling may cause rounding errors in child clipping.
1800 *
1801 * The clip mask rectangle is not automatically adjusted based on
1802 * wl_surface.attach dx and dy arguments.
1803 *
1804 * A clip mask rectangle can be set only if the compositor capability
1805 * WESTON_CAP_VIEW_CLIP_MASK is present.
1806 *
1807 * This function sets the clip mask rectangle and schedules a repaint for
1808 * the view.
1809 */
1810WL_EXPORT void
1811weston_view_set_mask(struct weston_view *view,
1812 int x, int y, int width, int height)
1813{
1814 struct weston_compositor *compositor = view->surface->compositor;
1815
1816 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1817 weston_log("%s not allowed without capability!\n", __func__);
1818 return;
1819 }
1820
1821 if (view->geometry.parent) {
1822 weston_log("view %p has a parent, clip forbidden!\n", view);
1823 return;
1824 }
1825
1826 if (width < 0 || height < 0) {
1827 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1828 x, y, width, height);
1829 return;
1830 }
1831
1832 pixman_region32_fini(&view->geometry.scissor);
1833 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1834 view->geometry.scissor_enabled = true;
1835 weston_view_geometry_dirty(view);
1836 weston_view_schedule_repaint(view);
1837}
1838
1839/** Remove the clip mask from a view
1840 *
1841 * \param view The view to remove the clip mask from.
1842 *
1843 * Removed the clip mask rectangle and schedules a repaint.
1844 *
1845 * \sa weston_view_set_mask
1846 */
1847WL_EXPORT void
1848weston_view_set_mask_infinite(struct weston_view *view)
1849{
1850 view->geometry.scissor_enabled = false;
1851 weston_view_geometry_dirty(view);
1852 weston_view_schedule_repaint(view);
1853}
1854
Armin Krezović0da12b82016-06-30 06:04:33 +02001855/* Check if view should be displayed
1856 *
1857 * The indicator is set manually when assigning
1858 * a view to a surface.
1859 *
1860 * This needs reworking. See the thread starting at:
1861 *
1862 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1863 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001864WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001865weston_view_is_mapped(struct weston_view *view)
1866{
Armin Krezović0da12b82016-06-30 06:04:33 +02001867 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001868}
1869
Philipp Zabel70decd52018-09-03 20:11:15 +02001870/* Check if view is opaque in specified region
1871 *
1872 * \param view The view to check for opacity.
1873 * \param region The region to check for opacity, in view coordinates.
1874 *
1875 * Returns true if the view is opaque in the specified region, because view
1876 * alpha is 1.0 and either the opaque region set by the client contains the
1877 * specified region, or the buffer pixel format or solid color is opaque.
1878 */
1879WL_EXPORT bool
1880weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1881{
1882 pixman_region32_t r;
1883 bool ret = false;
1884
1885 if (ev->alpha < 1.0)
1886 return false;
1887
1888 if (ev->surface->is_opaque)
1889 return true;
1890
1891 if (ev->transform.dirty) {
1892 weston_log("%s: transform dirty", __func__);
1893 return false;
1894 }
1895
1896 pixman_region32_init(&r);
1897 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1898
1899 if (!pixman_region32_not_empty(&r))
1900 ret = true;
1901
1902 pixman_region32_fini(&r);
1903
1904 return ret;
1905}
1906
Marius Vlad5f6bee42019-09-11 16:41:04 +03001907/** Check if the view has a valid buffer available
1908 *
1909 * @param ev The view to check if it has a valid buffer.
1910 *
1911 * Returns true if the view has a valid buffer or false otherwise.
1912 */
1913WL_EXPORT bool
1914weston_view_has_valid_buffer(struct weston_view *ev)
1915{
1916 return ev->surface->buffer_ref.buffer != NULL;
1917}
1918
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001919/** Check if the view matches the entire output
1920 *
1921 * @param ev The view to check.
1922 * @param output The output to check against.
1923 *
1924 * Returns true if the view does indeed matches the entire output.
1925 */
1926WL_EXPORT bool
1927weston_view_matches_output_entirely(struct weston_view *ev,
1928 struct weston_output *output)
1929{
1930 pixman_box32_t *extents =
1931 pixman_region32_extents(&ev->transform.boundingbox);
1932
1933 if (extents->x1 != output->x ||
1934 extents->y1 != output->y ||
1935 extents->x2 != output->x + output->width ||
1936 extents->y2 != output->y + output->height)
1937 return false;
1938
1939 return true;
1940}
1941
Armin Krezović0da12b82016-06-30 06:04:33 +02001942/* Check if a surface has a view assigned to it
1943 *
1944 * The indicator is set manually when mapping
1945 * a surface and creating a view for it.
1946 *
1947 * This needs to go. See the thread starting at:
1948 *
1949 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1950 *
1951 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001952WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001953weston_surface_is_mapped(struct weston_surface *surface)
1954{
Armin Krezović0da12b82016-06-30 06:04:33 +02001955 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001956}
1957
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001958static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001959surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001960{
1961 struct weston_view *view;
1962
1963 if (surface->width == width && surface->height == height)
1964 return;
1965
1966 surface->width = width;
1967 surface->height = height;
1968
1969 wl_list_for_each(view, &surface->views, surface_link)
1970 weston_view_geometry_dirty(view);
1971}
1972
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001973WL_EXPORT void
1974weston_surface_set_size(struct weston_surface *surface,
1975 int32_t width, int32_t height)
1976{
1977 assert(!surface->resource);
1978 surface_set_size(surface, width, height);
1979}
1980
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001981static int
1982fixed_round_up_to_int(wl_fixed_t f)
1983{
1984 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1985}
1986
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001987static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001988convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1989 int32_t width, int32_t height,
1990 uint32_t transform,
1991 int32_t scale)
1992{
1993 assert(scale > 0);
1994
1995 switch (transform) {
1996 case WL_OUTPUT_TRANSFORM_NORMAL:
1997 case WL_OUTPUT_TRANSFORM_180:
1998 case WL_OUTPUT_TRANSFORM_FLIPPED:
1999 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2000 *width_out = width / scale;
2001 *height_out = height / scale;
2002 break;
2003 case WL_OUTPUT_TRANSFORM_90:
2004 case WL_OUTPUT_TRANSFORM_270:
2005 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2006 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2007 *width_out = height / scale;
2008 *height_out = width / scale;
2009 break;
2010 default:
2011 assert(0 && "invalid transform");
2012 }
2013}
2014
2015static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002016weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002017{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002018 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002019
2020 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002021 surface->width_from_buffer = 0;
2022 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01002023 return;
2024 }
2025
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002026 convert_size_by_transform_scale(&surface->width_from_buffer,
2027 &surface->height_from_buffer,
2028 surface->buffer_ref.buffer->width,
2029 surface->buffer_ref.buffer->height,
2030 vp->buffer.transform,
2031 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002032}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002033
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002034static void
2035weston_surface_update_size(struct weston_surface *surface)
2036{
2037 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2038 int32_t width, height;
2039
2040 width = surface->width_from_buffer;
2041 height = surface->height_from_buffer;
2042
2043 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002044 surface_set_size(surface,
2045 vp->surface.width, vp->surface.height);
2046 return;
2047 }
2048
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002049 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03002050 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
2051 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
2052
2053 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002054 return;
2055 }
2056
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002057 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002058}
2059
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002060/** weston_compositor_get_time
2061 * \ingroup compositor
2062 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002063WL_EXPORT void
2064weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002065{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002066 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002067}
2068
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002069/** weston_compositor_pick_view
2070 * \ingroup compositor
2071 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002072WL_EXPORT struct weston_view *
2073weston_compositor_pick_view(struct weston_compositor *compositor,
2074 wl_fixed_t x, wl_fixed_t y,
2075 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002076{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002077 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002078 wl_fixed_t view_x, view_y;
2079 int view_ix, view_iy;
2080 int ix = wl_fixed_to_int(x);
2081 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002082
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002084 if (!pixman_region32_contains_point(
2085 &view->transform.boundingbox, ix, iy, NULL))
2086 continue;
2087
2088 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2089 view_ix = wl_fixed_to_int(view_x);
2090 view_iy = wl_fixed_to_int(view_y);
2091
2092 if (!pixman_region32_contains_point(&view->surface->input,
2093 view_ix, view_iy, NULL))
2094 continue;
2095
Pekka Paalanen380adf52015-02-16 14:39:11 +02002096 if (view->geometry.scissor_enabled &&
2097 !pixman_region32_contains_point(&view->geometry.scissor,
2098 view_ix, view_iy, NULL))
2099 continue;
2100
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002101 *vx = view_x;
2102 *vy = view_y;
2103 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002104 }
2105
Derek Foremanf9318d12015-05-11 15:40:11 -05002106 *vx = wl_fixed_from_int(-1000000);
2107 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002108 return NULL;
2109}
2110
2111static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002112weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002113{
Daniel Stone37816df2012-05-16 18:45:18 +01002114 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002115
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002116 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002117 return;
2118
Daniel Stone37816df2012-05-16 18:45:18 +01002119 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002120 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002121}
2122
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002123WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002125{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002126 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002127
Jason Ekstranda7af7042013-10-12 22:38:11 -05002128 if (!weston_view_is_mapped(view))
2129 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002130
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002132 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002133 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002134 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002135 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136 wl_list_remove(&view->link);
2137 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002138 view->output_mask = 0;
2139 weston_surface_assign_output(view->surface);
2140
2141 if (weston_surface_is_mapped(view->surface))
2142 return;
2143
2144 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002145 struct weston_touch *touch = weston_seat_get_touch(seat);
2146 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2147 struct weston_keyboard *keyboard =
2148 weston_seat_get_keyboard(seat);
2149
2150 if (keyboard && keyboard->focus == view->surface)
2151 weston_keyboard_set_focus(keyboard, NULL);
2152 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002153 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002154 if (touch && touch->focus == view)
2155 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002156 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002157}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002158
Jason Ekstranda7af7042013-10-12 22:38:11 -05002159WL_EXPORT void
2160weston_surface_unmap(struct weston_surface *surface)
2161{
2162 struct weston_view *view;
2163
Armin Krezovićf8486c32016-06-30 06:04:28 +02002164 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002165 wl_list_for_each(view, &surface->views, surface_link)
2166 weston_view_unmap(view);
2167 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002168}
2169
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002170static void
2171weston_surface_reset_pending_buffer(struct weston_surface *surface)
2172{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002173 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002174 surface->pending.sx = 0;
2175 surface->pending.sy = 0;
2176 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002177 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002178}
2179
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180WL_EXPORT void
2181weston_view_destroy(struct weston_view *view)
2182{
2183 wl_signal_emit(&view->destroy_signal, view);
2184
2185 assert(wl_list_empty(&view->geometry.child_list));
2186
2187 if (weston_view_is_mapped(view)) {
2188 weston_view_unmap(view);
2189 weston_compositor_build_view_list(view->surface->compositor);
2190 }
2191
2192 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002193 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194
2195 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002196 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002197 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002198 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002199
2200 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002201 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002202
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203 wl_list_remove(&view->surface_link);
2204
2205 free(view);
2206}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002207
2208WL_EXPORT void
2209weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002210{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002211 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002213 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002214
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002215 if (--surface->ref_count > 0)
2216 return;
2217
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002218 assert(surface->resource == NULL);
2219
Pekka Paalanenca790762015-04-17 14:23:38 +03002220 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002221
Pekka Paalanene67858b2013-04-25 13:57:42 +03002222 assert(wl_list_empty(&surface->subsurface_list_pending));
2223 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002224
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2226 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002227
Jason Ekstrand7b982072014-05-20 14:33:03 -05002228 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002229
Pekka Paalanende685b82012-12-04 15:58:12 +02002230 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002231 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002232
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002233 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002234 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002235 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002236
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002237 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002238 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002239
Pekka Paalanen133e4392014-09-23 22:08:46 -04002240 weston_presentation_feedback_discard_list(&surface->feedback_list);
2241
Jonas Ådahld3414f22016-07-22 17:56:31 +08002242 wl_list_for_each_safe(constraint, next_constraint,
2243 &surface->pointer_constraints,
2244 link)
2245 weston_pointer_constraint_destroy(constraint);
2246
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002247 fd_clear(&surface->acquire_fence_fd);
2248
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002249 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002250}
2251
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002252static void
2253destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002254{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002255 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002256
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002257 assert(surface);
2258
Giulio Camuffo0d379742013-11-15 22:06:15 +01002259 /* Set the resource to NULL, since we don't want to leave a
2260 * dangling pointer if the surface was refcounted and survives
2261 * the weston_surface_destroy() call. */
2262 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002263
2264 if (surface->viewport_resource)
2265 wl_resource_set_user_data(surface->viewport_resource, NULL);
2266
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002267 if (surface->synchronization_resource) {
2268 wl_resource_set_user_data(surface->synchronization_resource,
2269 NULL);
2270 }
2271
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002272 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002273}
2274
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002275static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002276weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2277{
2278 struct weston_buffer *buffer =
2279 container_of(listener, struct weston_buffer, destroy_listener);
2280
2281 wl_signal_emit(&buffer->destroy_signal, buffer);
2282 free(buffer);
2283}
2284
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002285WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002286weston_buffer_from_resource(struct wl_resource *resource)
2287{
2288 struct weston_buffer *buffer;
2289 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002290
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002291 listener = wl_resource_get_destroy_listener(resource,
2292 weston_buffer_destroy_handler);
2293
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002294 if (listener)
2295 return container_of(listener, struct weston_buffer,
2296 destroy_listener);
2297
2298 buffer = zalloc(sizeof *buffer);
2299 if (buffer == NULL)
2300 return NULL;
2301
2302 buffer->resource = resource;
2303 wl_signal_init(&buffer->destroy_signal);
2304 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002305 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002306 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002307
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002308 return buffer;
2309}
2310
2311static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002312weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2313 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002314{
Pekka Paalanende685b82012-12-04 15:58:12 +02002315 struct weston_buffer_reference *ref =
2316 container_of(listener, struct weston_buffer_reference,
2317 destroy_listener);
2318
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002319 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002320 ref->buffer = NULL;
2321}
2322
2323WL_EXPORT void
2324weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002325 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002326{
2327 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002328 ref->buffer->busy_count--;
2329 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002330 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002331 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002332 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002333 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002334 }
2335
Pekka Paalanende685b82012-12-04 15:58:12 +02002336 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002337 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002338 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002339 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002340 }
2341
Pekka Paalanende685b82012-12-04 15:58:12 +02002342 ref->buffer = buffer;
2343 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2344}
2345
2346static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002347weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2348 void *data)
2349{
2350 struct weston_buffer_release_reference *ref =
2351 container_of(listener, struct weston_buffer_release_reference,
2352 destroy_listener);
2353
2354 assert((struct wl_resource *)data == ref->buffer_release->resource);
2355 ref->buffer_release = NULL;
2356}
2357
2358static void
2359weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2360{
2361 struct wl_resource *resource = buffer_release->resource;
2362 int release_fence_fd = buffer_release->fence_fd;
2363
2364 if (release_fence_fd >= 0) {
2365 zwp_linux_buffer_release_v1_send_fenced_release(
2366 resource, release_fence_fd);
2367 } else {
2368 zwp_linux_buffer_release_v1_send_immediate_release(
2369 resource);
2370 }
2371
2372 wl_resource_destroy(resource);
2373}
2374
2375WL_EXPORT void
2376weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2377 struct weston_buffer_release *buffer_release)
2378{
2379 if (buffer_release == ref->buffer_release)
2380 return;
2381
2382 if (ref->buffer_release) {
2383 ref->buffer_release->ref_count--;
2384 wl_list_remove(&ref->destroy_listener.link);
2385 if (ref->buffer_release->ref_count == 0)
2386 weston_buffer_release_destroy(ref->buffer_release);
2387 }
2388
2389 if (buffer_release) {
2390 buffer_release->ref_count++;
2391 wl_resource_add_destroy_listener(buffer_release->resource,
2392 &ref->destroy_listener);
2393 }
2394
2395 ref->buffer_release = buffer_release;
2396 ref->destroy_listener.notify =
2397 weston_buffer_release_reference_handle_destroy;
2398}
2399
2400WL_EXPORT void
2401weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2402 struct weston_buffer_release_reference *src)
2403{
2404 weston_buffer_release_reference(dest, src->buffer_release);
2405 weston_buffer_release_reference(src, NULL);
2406}
2407
2408static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002409weston_surface_attach(struct weston_surface *surface,
2410 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002411{
2412 weston_buffer_reference(&surface->buffer_ref, buffer);
2413
Pekka Paalanena6421c42012-12-04 15:58:10 +02002414 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002415 if (weston_surface_is_mapped(surface))
2416 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002417 }
2418
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002419 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002420
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002421 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002422 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002423}
2424
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002425/** weston_compositor_damage_all
2426 * \ingroup compositor
2427 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002428WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002429weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002430{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002431 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002432
2433 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002434 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002435}
2436
Marius Vlad55d87362019-06-11 01:15:35 +03002437/**
2438 * \ingroup output
2439 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002440WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002441weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002442{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002443 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002444
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002445 pixman_region32_union(&compositor->primary_plane.damage,
2446 &compositor->primary_plane.damage,
2447 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002448 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002449}
2450
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002451static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002452surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002453{
Pekka Paalanende685b82012-12-04 15:58:12 +02002454 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002455 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002456 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002457
Marius Vlad2a1b7862019-09-05 13:12:18 +03002458 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002459 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002460 TLP_OUTPUT(surface->output), TLP_END);
2461
Jason Ekstrandef540082014-06-26 10:37:36 -07002462 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002463}
2464
2465static void
2466view_accumulate_damage(struct weston_view *view,
2467 pixman_region32_t *opaque)
2468{
2469 pixman_region32_t damage;
2470
2471 pixman_region32_init(&damage);
2472 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002473 pixman_box32_t *extents;
2474
Jason Ekstranda7af7042013-10-12 22:38:11 -05002475 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002476 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002477 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002478 pixman_region32_copy(&damage, &view->surface->damage);
2479 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002480 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002481 }
2482
Pekka Paalanen380adf52015-02-16 14:39:11 +02002483 pixman_region32_intersect(&damage, &damage,
2484 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002485 pixman_region32_subtract(&damage, &damage, opaque);
2486 pixman_region32_union(&view->plane->damage,
2487 &view->plane->damage, &damage);
2488 pixman_region32_fini(&damage);
2489 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002490 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002491}
2492
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002493static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002494compositor_accumulate_damage(struct weston_compositor *ec)
2495{
2496 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002497 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002498 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002499
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002500 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002501
2502 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002503 pixman_region32_copy(&plane->clip, &clip);
2504
2505 pixman_region32_init(&opaque);
2506
Jason Ekstranda7af7042013-10-12 22:38:11 -05002507 wl_list_for_each(ev, &ec->view_list, link) {
2508 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002509 continue;
2510
Jason Ekstranda7af7042013-10-12 22:38:11 -05002511 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002512 }
2513
2514 pixman_region32_union(&clip, &clip, &opaque);
2515 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002516 }
2517
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002518 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002519
Jason Ekstranda7af7042013-10-12 22:38:11 -05002520 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002521 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002522
2523 wl_list_for_each(ev, &ec->view_list, link) {
2524 if (ev->surface->touched)
2525 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002526 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002527
2528 surface_flush_damage(ev->surface);
2529
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002530 /* Both the renderer and the backend have seen the buffer
2531 * by now. If renderer needs the buffer, it has its own
2532 * reference set. If the backend wants to keep the buffer
2533 * around for migrating the surface into a non-primary plane
2534 * later, keep_buffer is true. Otherwise, drop the core
2535 * reference now, and allow early buffer release. This enables
2536 * clients to use single-buffering.
2537 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002538 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002539 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002540 weston_buffer_release_reference(
2541 &ev->surface->buffer_release_ref, NULL);
2542 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002543 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002544}
2545
2546static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002547surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002548{
2549 struct weston_subsurface *sub;
2550
Pekka Paalanene67858b2013-04-25 13:57:42 +03002551 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002552 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002553 continue;
2554
Jason Ekstranda7af7042013-10-12 22:38:11 -05002555 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2556 wl_list_init(&sub->surface->views);
2557
2558 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002559 }
2560}
2561
2562static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002563surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002564{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002565 struct weston_subsurface *sub;
2566 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002567
Jason Ekstranda7af7042013-10-12 22:38:11 -05002568 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2569 if (sub->surface == surface)
2570 continue;
2571
George Kiagiadakised04d382014-06-13 18:10:26 +02002572 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2573 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002574 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002575 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002576
2577 surface_free_unused_subsurface_views(sub->surface);
2578 }
2579}
2580
2581static void
2582view_list_add_subsurface_view(struct weston_compositor *compositor,
2583 struct weston_subsurface *sub,
2584 struct weston_view *parent)
2585{
2586 struct weston_subsurface *child;
2587 struct weston_view *view = NULL, *iv;
2588
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002589 if (!weston_surface_is_mapped(sub->surface))
2590 return;
2591
Jason Ekstranda7af7042013-10-12 22:38:11 -05002592 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2593 if (iv->geometry.parent == parent) {
2594 view = iv;
2595 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002596 }
2597 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002598
2599 if (view) {
2600 /* Put it back in the surface's list of views */
2601 wl_list_remove(&view->surface_link);
2602 wl_list_insert(&sub->surface->views, &view->surface_link);
2603 } else {
2604 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002605 weston_view_set_position(view,
2606 sub->position.x,
2607 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002608 weston_view_set_transform_parent(view, parent);
2609 }
2610
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002611 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002612 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002613 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002614
Pekka Paalanenb188e912013-11-19 14:03:35 +02002615 if (wl_list_empty(&sub->surface->subsurface_list)) {
2616 wl_list_insert(compositor->view_list.prev, &view->link);
2617 return;
2618 }
2619
2620 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2621 if (child->surface == sub->surface)
2622 wl_list_insert(compositor->view_list.prev, &view->link);
2623 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002624 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002625 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002626}
2627
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002628/* This recursively adds the sub-surfaces for a view, relying on the
2629 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2630 * change first happens to the sub-surface list, and then automatically
2631 * propagates here. See weston_surface_damage_subsurfaces() for how the
2632 * sub-surfaces receive damage when the client changes the state.
2633 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002634static void
2635view_list_add(struct weston_compositor *compositor,
2636 struct weston_view *view)
2637{
2638 struct weston_subsurface *sub;
2639
2640 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002641
Pekka Paalanenb188e912013-11-19 14:03:35 +02002642 if (wl_list_empty(&view->surface->subsurface_list)) {
2643 wl_list_insert(compositor->view_list.prev, &view->link);
2644 return;
2645 }
2646
2647 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2648 if (sub->surface == view->surface)
2649 wl_list_insert(compositor->view_list.prev, &view->link);
2650 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002651 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002652 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002653}
2654
2655static void
2656weston_compositor_build_view_list(struct weston_compositor *compositor)
2657{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002658 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002659 struct weston_layer *layer;
2660
2661 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002662 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002663 surface_stash_subsurface_views(view->surface);
2664
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002665 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2666 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002668
Jason Ekstranda7af7042013-10-12 22:38:11 -05002669 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002670 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002671 view_list_add(compositor, view);
2672 }
2673 }
2674
2675 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002676 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002677 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002678}
2679
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002680static void
2681weston_output_take_feedback_list(struct weston_output *output,
2682 struct weston_surface *surface)
2683{
2684 struct weston_view *view;
2685 struct weston_presentation_feedback *feedback;
2686 uint32_t flags = 0xffffffff;
2687
2688 if (wl_list_empty(&surface->feedback_list))
2689 return;
2690
2691 /* All views must have the flag for the flag to survive. */
2692 wl_list_for_each(view, &surface->views, surface_link) {
2693 /* ignore views that are not on this output at all */
2694 if (view->output_mask & (1u << output->id))
2695 flags &= view->psf_flags;
2696 }
2697
2698 wl_list_for_each(feedback, &surface->feedback_list, link)
2699 feedback->psf_flags = flags;
2700
2701 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2702 wl_list_init(&surface->feedback_list);
2703}
2704
David Herrmann1edf44c2013-10-22 17:11:26 +02002705static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002706weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002707{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002708 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002709 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002710 struct weston_animation *animation, *next;
2711 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002712 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002713 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002714 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002715 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302716 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002717
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002718 if (output->destroying)
2719 return 0;
2720
Marius Vlad3203ff62019-09-05 14:56:12 +03002721 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002722
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002723 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002724 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002725
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302726 /* Find the highest protection desired for an output */
2727 wl_list_for_each(ev, &ec->view_list, link) {
2728 if (ev->surface->output_mask & (1u << output->id)) {
2729 /*
2730 * The desired_protection of the output should be the
2731 * maximum of the desired_protection of the surfaces,
2732 * that are displayed on that output, to avoid
2733 * reducing the protection for existing surfaces.
2734 */
2735 if (ev->surface->desired_protection > highest_requested)
2736 highest_requested =
2737 ev->surface->desired_protection;
2738 }
2739 }
2740
2741 output->desired_protection = highest_requested;
2742
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002743 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002744 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002745 } else {
2746 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002747 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002748 ev->psf_flags = 0;
2749 }
2750 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002751
Pekka Paalanene67858b2013-04-25 13:57:42 +03002752 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002753 wl_list_for_each(ev, &ec->view_list, link) {
2754 /* Note: This operation is safe to do multiple times on the
2755 * same surface.
2756 */
2757 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002758 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002759 &ev->surface->frame_callback_list);
2760 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002761
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002762 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002763 }
2764 }
2765
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002766 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002767
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002768 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002769 pixman_region32_intersect(&output_damage,
2770 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002771 pixman_region32_subtract(&output_damage,
2772 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002773
Scott Moreauccbf29d2012-02-22 14:21:41 -07002774 if (output->dirty)
2775 weston_output_update_matrix(output);
2776
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002777 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002778
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002779 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002780
Daniel Stone09a97e22017-03-01 11:34:06 +00002781 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002782 if (r == 0)
2783 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002784
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002785 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002786
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002787 frame_time_msec = timespec_to_msec(&output->frame_time);
2788
Jonas Ådahldb773762012-06-13 00:01:21 +02002789 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002790 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002791 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002792 }
2793
Scott Moreaud64cf212012-06-08 19:40:54 -06002794 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002795 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002796 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002797 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002798
Marius Vlad3203ff62019-09-05 14:56:12 +03002799 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002800
David Herrmann1edf44c2013-10-22 17:11:26 +02002801 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002802}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002803
Pekka Paalanen82919792014-05-21 13:51:49 +03002804static void
2805weston_output_schedule_repaint_reset(struct weston_output *output)
2806{
Daniel Stone05df8c12017-03-03 16:59:42 +00002807 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002808 TL_POINT(output->compositor, "core_repaint_exit_loop",
2809 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002810}
2811
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002812static int
2813weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2814 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002815{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002816 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002817 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002818 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002819
Daniel Stone6847b852017-03-01 11:34:08 +00002820 /* We're not ready yet; come back to make a decision later. */
2821 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002822 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002823
2824 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2825 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002826 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002827
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002828 /* If we're sleeping, drop the repaint machinery entirely; we will
2829 * explicitly repaint all outputs when we come back. */
2830 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2831 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2832 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002833
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002834 /* We don't actually need to repaint this output; drop it from
2835 * repaint until something causes damage. */
2836 if (!output->repaint_needed)
2837 goto err;
2838
2839 /* If repaint fails, we aren't going to get weston_output_finish_frame
2840 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002841 * something schedules a successful repaint later. As repainting may
2842 * take some time, re-read our clock as a courtesy to the next
2843 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002844 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002845 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002846 if (ret != 0)
2847 goto err;
2848
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002849 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002850 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002851
2852err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002853 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002854 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002855}
2856
2857static void
2858output_repaint_timer_arm(struct weston_compositor *compositor)
2859{
2860 struct weston_output *output;
2861 bool any_should_repaint = false;
2862 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002863 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002864
2865 weston_compositor_read_presentation_clock(compositor, &now);
2866
2867 wl_list_for_each(output, &compositor->output_list, link) {
2868 int64_t msec_to_this;
2869
2870 if (output->repaint_status != REPAINT_SCHEDULED)
2871 continue;
2872
2873 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2874 &now);
2875 if (!any_should_repaint || msec_to_this < msec_to_next)
2876 msec_to_next = msec_to_this;
2877
2878 any_should_repaint = true;
2879 }
2880
2881 if (!any_should_repaint)
2882 return;
2883
2884 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2885 * This is a workaround to allow coalescing multiple output repaints
2886 * particularly from weston_output_finish_frame()
2887 * into the same call, which would not happen if we called
2888 * output_repaint_timer_handler() directly.
2889 */
2890 if (msec_to_next < 1)
2891 msec_to_next = 1;
2892
2893 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2894}
2895
2896static int
2897output_repaint_timer_handler(void *data)
2898{
2899 struct weston_compositor *compositor = data;
2900 struct weston_output *output;
2901 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002902 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002903 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002904
2905 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002906
2907 if (compositor->backend->repaint_begin)
2908 repaint_data = compositor->backend->repaint_begin(compositor);
2909
2910 wl_list_for_each(output, &compositor->output_list, link) {
2911 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2912 if (ret)
2913 break;
2914 }
2915
2916 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002917 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002918 ret = compositor->backend->repaint_flush(compositor,
2919 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002920 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002921 if (compositor->backend->repaint_cancel)
2922 compositor->backend->repaint_cancel(compositor,
2923 repaint_data);
2924 }
2925
2926 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002927 wl_list_for_each(output, &compositor->output_list, link) {
2928 if (output->repainted)
2929 weston_output_schedule_repaint_reset(output);
2930 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002931 }
Daniel Stone6847b852017-03-01 11:34:08 +00002932
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002933 wl_list_for_each(output, &compositor->output_list, link)
2934 output->repainted = false;
2935
Daniel Stone6847b852017-03-01 11:34:08 +00002936 output_repaint_timer_arm(compositor);
2937
Pekka Paalanen0513a952014-05-21 16:17:27 +03002938 return 0;
2939}
2940
Marius Vlad55d87362019-06-11 01:15:35 +03002941/**
2942 * \ingroup output
2943 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002944WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002945weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002946 const struct timespec *stamp,
2947 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002948{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002949 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002950 int32_t refresh_nsec;
2951 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002952 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002953
Pekka Paalanenb5026542014-11-12 15:09:24 +02002954
Daniel Stone05df8c12017-03-03 16:59:42 +00002955 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002956 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2957
Daniel Stone6847b852017-03-01 11:34:08 +00002958 weston_compositor_read_presentation_clock(compositor, &now);
2959
Daniel Stone3615ce12017-03-01 11:34:05 +00002960 /* If we haven't been supplied any timestamp at all, we don't have a
2961 * timebase to work against, so any delay just wastes time. Push a
2962 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002963 if (!stamp) {
2964 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002965 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002966 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002967
Marius Vlad3203ff62019-09-05 14:56:12 +03002968 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Marius Vladdf9278a2018-03-06 18:56:23 +02002969 TLP_VBLANK(stamp), TLP_END);
2970
Pekka Paalanend7894d02015-07-03 15:08:53 +03002971 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002972 weston_presentation_feedback_present_list(&output->feedback_list,
2973 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002974 output->msc,
2975 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002976
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002977 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002978
Daniel Stone6847b852017-03-01 11:34:08 +00002979 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2980 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2981 -compositor->repaint_msec);
2982 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002983
2984 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002985 static bool warned;
2986
2987 if (!warned)
2988 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002989 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002990 warned = true;
2991
Daniel Stone6847b852017-03-01 11:34:08 +00002992 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002993 }
2994
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002995 /* Called from restart_repaint_loop and restart happens already after
2996 * the deadline given by repaint_msec? In that case we delay until
2997 * the deadline of the next frame, to give clients a more predictable
2998 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002999 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3000 msec_rel < 0) {
3001 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3002 timespec_add_nsec(&output->next_repaint,
3003 &output->next_repaint,
3004 refresh_nsec);
3005 }
3006 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003007
Daniel Stone3615ce12017-03-01 11:34:05 +00003008out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003009 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003010 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003011}
3012
3013static void
3014idle_repaint(void *data)
3015{
3016 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003017 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003018
Daniel Stone05df8c12017-03-03 16:59:42 +00003019 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3020 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003021 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003022 ret = output->start_repaint_loop(output);
3023 if (ret != 0)
3024 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003025}
3026
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003027WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003028weston_layer_entry_insert(struct weston_layer_entry *list,
3029 struct weston_layer_entry *entry)
3030{
3031 wl_list_insert(&list->link, &entry->link);
3032 entry->layer = list->layer;
3033}
3034
3035WL_EXPORT void
3036weston_layer_entry_remove(struct weston_layer_entry *entry)
3037{
3038 wl_list_remove(&entry->link);
3039 wl_list_init(&entry->link);
3040 entry->layer = NULL;
3041}
3042
Quentin Glidic82681572016-12-17 13:40:51 +01003043
3044/** Initialize the weston_layer struct.
3045 *
3046 * \param compositor The compositor instance
3047 * \param layer The layer to initialize
3048 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003049WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003050weston_layer_init(struct weston_layer *layer,
3051 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003052{
Quentin Glidic82681572016-12-17 13:40:51 +01003053 layer->compositor = compositor;
3054 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003055 wl_list_init(&layer->view_list.link);
3056 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003057 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003058}
3059
3060/** Sets the position of the layer in the layer list. The layer will be placed
3061 * below any layer with the same position value, if any.
3062 * This function is safe to call if the layer is already on the list, but the
3063 * layer may be moved below other layers at the same position, if any.
3064 *
3065 * \param layer The layer to modify
3066 * \param position The position the layer will be placed at
3067 */
3068WL_EXPORT void
3069weston_layer_set_position(struct weston_layer *layer,
3070 enum weston_layer_position position)
3071{
3072 struct weston_layer *below;
3073
3074 wl_list_remove(&layer->link);
3075
3076 /* layer_list is ordered from top to bottom, the last layer being the
3077 * background with the smallest position value */
3078
3079 layer->position = position;
3080 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3081 if (below->position >= layer->position) {
3082 wl_list_insert(&below->link, &layer->link);
3083 return;
3084 }
3085 }
3086 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3087}
3088
3089/** Hide a layer by taking it off the layer list.
3090 * This function is safe to call if the layer is not on the list.
3091 *
3092 * \param layer The layer to hide
3093 */
3094WL_EXPORT void
3095weston_layer_unset_position(struct weston_layer *layer)
3096{
3097 wl_list_remove(&layer->link);
3098 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003099}
3100
3101WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003102weston_layer_set_mask(struct weston_layer *layer,
3103 int x, int y, int width, int height)
3104{
3105 struct weston_view *view;
3106
3107 layer->mask.x1 = x;
3108 layer->mask.x2 = x + width;
3109 layer->mask.y1 = y;
3110 layer->mask.y2 = y + height;
3111
3112 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3113 weston_view_geometry_dirty(view);
3114 }
3115}
3116
3117WL_EXPORT void
3118weston_layer_set_mask_infinite(struct weston_layer *layer)
3119{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003120 struct weston_view *view;
3121
3122 layer->mask.x1 = INT32_MIN;
3123 layer->mask.x2 = INT32_MAX;
3124 layer->mask.y1 = INT32_MIN;
3125 layer->mask.y2 = INT32_MAX;
3126
3127 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3128 weston_view_geometry_dirty(view);
3129 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003130}
3131
Daniel Stone3b775632018-07-20 08:38:25 +01003132WL_EXPORT bool
3133weston_layer_mask_is_infinite(struct weston_layer *layer)
3134{
3135 return layer->mask.x1 == INT32_MIN &&
3136 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003137 layer->mask.x2 == INT32_MAX &&
3138 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003139}
3140
Marius Vlad55d87362019-06-11 01:15:35 +03003141/**
3142 * \ingroup output
3143 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003144WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003145weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003146{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003147 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003148 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003149
Bryce Harrington08976ac2016-08-30 12:05:16 -07003150 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3151 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003152 return;
3153
Pekka Paalanenb5026542014-11-12 15:09:24 +02003154 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003155 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003156
Kristian Høgsbergef044142011-06-21 15:02:12 -04003157 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003158 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003159
3160 /* If we already have a repaint scheduled for our idle handler,
3161 * no need to set it again. If the repaint has been called but
3162 * not finished, then weston_output_finish_frame() will notice
3163 * that a repaint is needed and schedule one. */
3164 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003165 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003166
Daniel Stone05df8c12017-03-03 16:59:42 +00003167 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003168 assert(!output->idle_repaint_source);
3169 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3170 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003171 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003172}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003173
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003174/** weston_compositor_schedule_repaint
3175 * \ingroup compositor
3176 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003177WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003178weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3179{
3180 struct weston_output *output;
3181
3182 wl_list_for_each(output, &compositor->output_list, link)
3183 weston_output_schedule_repaint(output);
3184}
3185
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003186static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003187surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003188{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003189 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003190}
3191
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003192static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003193surface_attach(struct wl_client *client,
3194 struct wl_resource *resource,
3195 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3196{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003197 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003198 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003199
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003200 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003201 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003202 if (buffer == NULL) {
3203 wl_client_post_no_memory(client);
3204 return;
3205 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003206 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003207
Pekka Paalanende685b82012-12-04 15:58:12 +02003208 /* Attach, attach, without commit in between does not send
3209 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003210 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003211
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003212 surface->pending.sx = sx;
3213 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003214 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003215}
3216
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003217static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003218surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003219 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003220 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003221{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003222 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003223
Derek Foreman57e92ed2015-11-17 14:11:35 -06003224 if (width <= 0 || height <= 0)
3225 return;
3226
Derek Foreman152254b2015-11-26 14:17:48 -06003227 pixman_region32_union_rect(&surface->pending.damage_surface,
3228 &surface->pending.damage_surface,
3229 x, y, width, height);
3230}
3231
3232static void
3233surface_damage_buffer(struct wl_client *client,
3234 struct wl_resource *resource,
3235 int32_t x, int32_t y, int32_t width, int32_t height)
3236{
3237 struct weston_surface *surface = wl_resource_get_user_data(resource);
3238
3239 if (width <= 0 || height <= 0)
3240 return;
3241
3242 pixman_region32_union_rect(&surface->pending.damage_buffer,
3243 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003244 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003245}
3246
Kristian Høgsberg33418202011-08-16 23:01:28 -04003247static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003248destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003249{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003250 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003251
3252 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003253 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003254}
3255
3256static void
3257surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003258 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003259{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003260 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003261 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003262
3263 cb = malloc(sizeof *cb);
3264 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003265 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003266 return;
3267 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003268
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003269 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3270 callback);
3271 if (cb->resource == NULL) {
3272 free(cb);
3273 wl_resource_post_no_memory(resource);
3274 return;
3275 }
3276
Jason Ekstranda85118c2013-06-27 20:17:02 -05003277 wl_resource_set_implementation(cb->resource, NULL, cb,
3278 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003279
Pekka Paalanenbc106382012-10-10 12:49:31 +03003280 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003281}
3282
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003283static void
3284surface_set_opaque_region(struct wl_client *client,
3285 struct wl_resource *resource,
3286 struct wl_resource *region_resource)
3287{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003288 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003289 struct weston_region *region;
3290
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003291 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003292 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003293 pixman_region32_copy(&surface->pending.opaque,
3294 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003295 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003296 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003297 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003298}
3299
3300static void
3301surface_set_input_region(struct wl_client *client,
3302 struct wl_resource *resource,
3303 struct wl_resource *region_resource)
3304{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003305 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003306 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003307
3308 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003309 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003310 pixman_region32_copy(&surface->pending.input,
3311 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003312 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003313 pixman_region32_fini(&surface->pending.input);
3314 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003315 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003316}
3317
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003318/* Cause damage to this sub-surface and all its children.
3319 *
3320 * This is useful when there are state changes that need an implicit
3321 * damage, e.g. a z-order change.
3322 */
3323static void
3324weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3325{
3326 struct weston_subsurface *child;
3327
3328 weston_surface_damage(sub->surface);
3329 sub->reordered = false;
3330
3331 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3332 if (child != sub)
3333 weston_surface_damage_subsurfaces(child);
3334}
3335
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003336static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003337weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003338{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003339 struct weston_subsurface *sub;
3340
3341 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3342 parent_link_pending) {
3343 wl_list_remove(&sub->parent_link);
3344 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003345
3346 if (sub->reordered)
3347 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003348 }
3349}
3350
3351static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003352weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003353 struct weston_matrix *matrix)
3354{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003355 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003356 double src_width, src_height, dest_width, dest_height;
3357
3358 weston_matrix_init(matrix);
3359
3360 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3361 src_width = surface->width_from_buffer;
3362 src_height = surface->height_from_buffer;
3363 } else {
3364 src_width = wl_fixed_to_double(vp->buffer.src_width);
3365 src_height = wl_fixed_to_double(vp->buffer.src_height);
3366 }
3367
3368 if (vp->surface.width == -1) {
3369 dest_width = src_width;
3370 dest_height = src_height;
3371 } else {
3372 dest_width = vp->surface.width;
3373 dest_height = vp->surface.height;
3374 }
3375
3376 if (src_width != dest_width || src_height != dest_height)
3377 weston_matrix_scale(matrix,
3378 src_width / dest_width,
3379 src_height / dest_height, 1);
3380
3381 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3382 weston_matrix_translate(matrix,
3383 wl_fixed_to_double(vp->buffer.src_x),
3384 wl_fixed_to_double(vp->buffer.src_y),
3385 0);
3386
3387 switch (vp->buffer.transform) {
3388 case WL_OUTPUT_TRANSFORM_FLIPPED:
3389 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3390 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3391 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3392 weston_matrix_scale(matrix, -1, 1, 1);
3393 weston_matrix_translate(matrix,
3394 surface->width_from_buffer, 0, 0);
3395 break;
3396 }
3397
3398 switch (vp->buffer.transform) {
3399 default:
3400 case WL_OUTPUT_TRANSFORM_NORMAL:
3401 case WL_OUTPUT_TRANSFORM_FLIPPED:
3402 break;
3403 case WL_OUTPUT_TRANSFORM_90:
3404 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003405 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003406 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003407 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003408 break;
3409 case WL_OUTPUT_TRANSFORM_180:
3410 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3411 weston_matrix_rotate_xy(matrix, -1, 0);
3412 weston_matrix_translate(matrix,
3413 surface->width_from_buffer,
3414 surface->height_from_buffer, 0);
3415 break;
3416 case WL_OUTPUT_TRANSFORM_270:
3417 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003418 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003419 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003420 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003421 break;
3422 }
3423
3424 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3425}
3426
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003427/**
3428 * Compute a + b > c while being safe to overflows.
3429 */
3430static bool
3431fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3432{
3433 return (int64_t)a + (int64_t)b > (int64_t)c;
3434}
3435
3436static bool
3437weston_surface_is_pending_viewport_source_valid(
3438 const struct weston_surface *surface)
3439{
3440 const struct weston_surface_state *pend = &surface->pending;
3441 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3442 int width_from_buffer = 0;
3443 int height_from_buffer = 0;
3444 wl_fixed_t w;
3445 wl_fixed_t h;
3446
3447 /* If viewport source rect is not set, it is always ok. */
3448 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3449 return true;
3450
3451 if (pend->newly_attached) {
3452 if (pend->buffer) {
3453 convert_size_by_transform_scale(&width_from_buffer,
3454 &height_from_buffer,
3455 pend->buffer->width,
3456 pend->buffer->height,
3457 vp->buffer.transform,
3458 vp->buffer.scale);
3459 } else {
3460 /* No buffer: viewport is irrelevant. */
3461 return true;
3462 }
3463 } else {
3464 width_from_buffer = surface->width_from_buffer;
3465 height_from_buffer = surface->height_from_buffer;
3466 }
3467
3468 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3469 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3470
3471 /* No buffer: viewport is irrelevant. */
3472 if (width_from_buffer == 0 || height_from_buffer == 0)
3473 return true;
3474
3475 /* overflow checks for wl_fixed_from_int() */
3476 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3477 return false;
3478 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3479 return false;
3480
3481 w = wl_fixed_from_int(width_from_buffer);
3482 h = wl_fixed_from_int(height_from_buffer);
3483
3484 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3485 return false;
3486 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3487 return false;
3488
3489 return true;
3490}
3491
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003492static bool
3493fixed_is_integer(wl_fixed_t v)
3494{
3495 return (v & 0xff) == 0;
3496}
3497
3498static bool
3499weston_surface_is_pending_viewport_dst_size_int(
3500 const struct weston_surface *surface)
3501{
3502 const struct weston_buffer_viewport *vp =
3503 &surface->pending.buffer_viewport;
3504
3505 if (vp->surface.width != -1) {
3506 assert(vp->surface.width > 0 && vp->surface.height > 0);
3507 return true;
3508 }
3509
3510 return fixed_is_integer(vp->buffer.src_width) &&
3511 fixed_is_integer(vp->buffer.src_height);
3512}
3513
Derek Foreman152254b2015-11-26 14:17:48 -06003514/* Translate pending damage in buffer co-ordinates to surface
3515 * co-ordinates and union it with a pixman_region32_t.
3516 * This should only be called after the buffer is attached.
3517 */
3518static void
3519apply_damage_buffer(pixman_region32_t *dest,
3520 struct weston_surface *surface,
3521 struct weston_surface_state *state)
3522{
3523 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3524
3525 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3526 * translated into surface co-ordinates and unioned with
3527 * any other surface damage.
3528 * None of this makes sense if there is no buffer though.
3529 */
3530 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3531 pixman_region32_t buffer_damage;
3532
3533 pixman_region32_intersect_rect(&state->damage_buffer,
3534 &state->damage_buffer,
3535 0, 0, buffer->width,
3536 buffer->height);
3537 pixman_region32_init(&buffer_damage);
3538 weston_matrix_transform_region(&buffer_damage,
3539 &surface->buffer_to_surface_matrix,
3540 &state->damage_buffer);
3541 pixman_region32_union(dest, dest, &buffer_damage);
3542 pixman_region32_fini(&buffer_damage);
3543 }
3544 /* We should clear this on commit even if there was no buffer */
3545 pixman_region32_clear(&state->damage_buffer);
3546}
3547
Jason Ekstrand1e059042014-10-16 10:55:19 -05003548static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303549weston_surface_set_desired_protection(struct weston_surface *surface,
3550 enum weston_hdcp_protection protection)
3551{
3552 if (surface->desired_protection == protection)
3553 return;
3554 surface->desired_protection = protection;
3555 weston_surface_damage(surface);
3556}
3557
3558static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303559weston_surface_set_protection_mode(struct weston_surface *surface,
3560 enum weston_surface_protection_mode p_mode)
3561{
3562 struct content_protection *cp = surface->compositor->content_protection;
3563 struct protected_surface *psurface;
3564
3565 surface->protection_mode = p_mode;
3566 wl_list_for_each(psurface, &cp->protected_list, link) {
3567 if (!psurface || psurface->surface != surface)
3568 continue;
3569 weston_protected_surface_send_event(psurface,
3570 surface->current_protection);
3571 }
3572}
3573
3574static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003575weston_surface_commit_state(struct weston_surface *surface,
3576 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003577{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003578 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003579 pixman_region32_t opaque;
3580
Alexander Larsson4ea95522013-05-22 14:41:37 +02003581 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003582 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003583 /* wp_viewport.set_source */
3584 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003585 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003586
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003587 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003588 if (state->newly_attached) {
3589 /* zwp_surface_synchronization_v1.set_acquire_fence */
3590 fd_move(&surface->acquire_fence_fd,
3591 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003592 /* zwp_surface_synchronization_v1.get_release */
3593 weston_buffer_release_move(&surface->buffer_release_ref,
3594 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003595 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003596 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003597 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003598 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003599 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003600
Jason Ekstrand1e059042014-10-16 10:55:19 -05003601 weston_surface_build_buffer_matrix(surface,
3602 &surface->surface_to_buffer_matrix);
3603 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3604 &surface->surface_to_buffer_matrix);
3605
Jason Ekstrand7b982072014-05-20 14:33:03 -05003606 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003607 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003608 if (surface->committed)
3609 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003610 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003611
Jason Ekstrand7b982072014-05-20 14:33:03 -05003612 state->sx = 0;
3613 state->sy = 0;
3614 state->newly_attached = 0;
3615 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003616
Derek Foreman152254b2015-11-26 14:17:48 -06003617 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003618 if (pixman_region32_not_empty(&state->damage_surface) ||
3619 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003620 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003621
Pekka Paalanen8e159182012-10-10 12:49:25 +03003622 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003623 &state->damage_surface);
3624
3625 apply_damage_buffer(&surface->damage, surface, state);
3626
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003627 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003628 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003629 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003630
3631 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003632 pixman_region32_init(&opaque);
3633 pixman_region32_intersect_rect(&opaque, &state->opaque,
3634 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003635
3636 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3637 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003638 wl_list_for_each(view, &surface->views, surface_link)
3639 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003640 }
3641
3642 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003643
3644 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003645 pixman_region32_intersect_rect(&surface->input, &state->input,
3646 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003647
Pekka Paalanenbc106382012-10-10 12:49:31 +03003648 /* wl_surface.frame */
3649 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003650 &state->frame_callback_list);
3651 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003652
3653 /* XXX:
3654 * What should happen with a feedback request, if there
3655 * is no wl_buffer attached for this commit?
3656 */
3657
3658 /* presentation.feedback */
3659 wl_list_insert_list(&surface->feedback_list,
3660 &state->feedback_list);
3661 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003662
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303663 /* weston_protected_surface.enforced/relaxed */
3664 if (surface->protection_mode != state->protection_mode)
3665 weston_surface_set_protection_mode(surface,
3666 state->protection_mode);
3667
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303668 /* weston_protected_surface.set_type */
3669 weston_surface_set_desired_protection(surface, state->desired_protection);
3670
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003671 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003672}
3673
3674static void
3675weston_surface_commit(struct weston_surface *surface)
3676{
3677 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003678
Pekka Paalanene67858b2013-04-25 13:57:42 +03003679 weston_surface_commit_subsurface_order(surface);
3680
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003681 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003682}
3683
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003684static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003685weston_subsurface_commit(struct weston_subsurface *sub);
3686
3687static void
3688weston_subsurface_parent_commit(struct weston_subsurface *sub,
3689 int parent_is_synchronized);
3690
3691static void
3692surface_commit(struct wl_client *client, struct wl_resource *resource)
3693{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003694 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003695 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3696
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003697 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3698 assert(surface->viewport_resource);
3699
3700 wl_resource_post_error(surface->viewport_resource,
3701 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3702 "wl_surface@%d has viewport source outside buffer",
3703 wl_resource_get_id(resource));
3704 return;
3705 }
3706
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003707 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3708 assert(surface->viewport_resource);
3709
3710 wl_resource_post_error(surface->viewport_resource,
3711 WP_VIEWPORT_ERROR_BAD_SIZE,
3712 "wl_surface@%d viewport dst size not integer",
3713 wl_resource_get_id(resource));
3714 return;
3715 }
3716
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003717 if (surface->pending.acquire_fence_fd >= 0) {
3718 assert(surface->synchronization_resource);
3719
3720 if (!surface->pending.buffer) {
3721 fd_clear(&surface->pending.acquire_fence_fd);
3722 wl_resource_post_error(surface->synchronization_resource,
3723 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3724 "wl_surface@%"PRIu32" no buffer for synchronization",
3725 wl_resource_get_id(resource));
3726 return;
3727 }
3728
3729 /* We support fences for both wp_linux_dmabuf and opaque EGL
3730 * buffers, as mandated by minor version 2 of the
3731 * zwp_linux_explicit_synchronization_v1 protocol. Since
3732 * renderers that support fences currently only support these
3733 * two buffer types plus SHM buffers, we can just check for the
3734 * SHM buffer case here.
3735 */
3736 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3737 fd_clear(&surface->pending.acquire_fence_fd);
3738 wl_resource_post_error(surface->synchronization_resource,
3739 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3740 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3741 wl_resource_get_id(resource));
3742 return;
3743 }
3744 }
3745
Alexandros Frantzis67629672018-10-19 12:14:11 +03003746 if (surface->pending.buffer_release_ref.buffer_release &&
3747 !surface->pending.buffer) {
3748 assert(surface->synchronization_resource);
3749
3750 wl_resource_post_error(surface->synchronization_resource,
3751 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3752 "wl_surface@%"PRIu32" no buffer for synchronization",
3753 wl_resource_get_id(resource));
3754 return;
3755 }
3756
Pekka Paalanene67858b2013-04-25 13:57:42 +03003757 if (sub) {
3758 weston_subsurface_commit(sub);
3759 return;
3760 }
3761
3762 weston_surface_commit(surface);
3763
3764 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3765 if (sub->surface != surface)
3766 weston_subsurface_parent_commit(sub, 0);
3767 }
3768}
3769
3770static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003771surface_set_buffer_transform(struct wl_client *client,
3772 struct wl_resource *resource, int transform)
3773{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003774 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003775
Jonny Lamba55f1392014-05-30 12:07:15 +02003776 /* if wl_output.transform grows more members this will need to be updated. */
3777 if (transform < 0 ||
3778 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3779 wl_resource_post_error(resource,
3780 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3781 "buffer transform must be a valid transform "
3782 "('%d' specified)", transform);
3783 return;
3784 }
3785
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003786 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003787 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003788}
3789
Alexander Larsson4ea95522013-05-22 14:41:37 +02003790static void
3791surface_set_buffer_scale(struct wl_client *client,
3792 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003793 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003794{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003795 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003796
Jonny Lamba55f1392014-05-30 12:07:15 +02003797 if (scale < 1) {
3798 wl_resource_post_error(resource,
3799 WL_SURFACE_ERROR_INVALID_SCALE,
3800 "buffer scale must be at least one "
3801 "('%d' specified)", scale);
3802 return;
3803 }
3804
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003805 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003806 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003807}
3808
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003809static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003810 surface_destroy,
3811 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003812 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003813 surface_frame,
3814 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003815 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003816 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003817 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003818 surface_set_buffer_scale,
3819 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003820};
3821
3822static void
3823compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003824 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003825{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003826 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003827 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003828
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003829 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003830 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003831 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003832 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003833 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003834
Jason Ekstranda85118c2013-06-27 20:17:02 -05003835 surface->resource =
3836 wl_resource_create(client, &wl_surface_interface,
3837 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003838 if (surface->resource == NULL) {
3839 weston_surface_destroy(surface);
3840 wl_resource_post_no_memory(resource);
3841 return;
3842 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003843 wl_resource_set_implementation(surface->resource, &surface_interface,
3844 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003845
3846 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003847}
3848
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003849static void
3850destroy_region(struct wl_resource *resource)
3851{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003852 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003853
3854 pixman_region32_fini(&region->region);
3855 free(region);
3856}
3857
3858static void
3859region_destroy(struct wl_client *client, struct wl_resource *resource)
3860{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003861 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003862}
3863
3864static void
3865region_add(struct wl_client *client, struct wl_resource *resource,
3866 int32_t x, int32_t y, int32_t width, int32_t height)
3867{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003868 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003869
3870 pixman_region32_union_rect(&region->region, &region->region,
3871 x, y, width, height);
3872}
3873
3874static void
3875region_subtract(struct wl_client *client, struct wl_resource *resource,
3876 int32_t x, int32_t y, int32_t width, int32_t height)
3877{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003878 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003879 pixman_region32_t rect;
3880
3881 pixman_region32_init_rect(&rect, x, y, width, height);
3882 pixman_region32_subtract(&region->region, &region->region, &rect);
3883 pixman_region32_fini(&rect);
3884}
3885
3886static const struct wl_region_interface region_interface = {
3887 region_destroy,
3888 region_add,
3889 region_subtract
3890};
3891
3892static void
3893compositor_create_region(struct wl_client *client,
3894 struct wl_resource *resource, uint32_t id)
3895{
3896 struct weston_region *region;
3897
3898 region = malloc(sizeof *region);
3899 if (region == NULL) {
3900 wl_resource_post_no_memory(resource);
3901 return;
3902 }
3903
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003904 pixman_region32_init(&region->region);
3905
Jason Ekstranda85118c2013-06-27 20:17:02 -05003906 region->resource =
3907 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003908 if (region->resource == NULL) {
3909 free(region);
3910 wl_resource_post_no_memory(resource);
3911 return;
3912 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003913 wl_resource_set_implementation(region->resource, &region_interface,
3914 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003915}
3916
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003917static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003918 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003919 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003920};
3921
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003922static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003923weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3924{
3925 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003926
Jason Ekstrand7b982072014-05-20 14:33:03 -05003927 weston_surface_commit_state(surface, &sub->cached);
3928 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003929
3930 weston_surface_commit_subsurface_order(surface);
3931
3932 weston_surface_schedule_repaint(surface);
3933
Jason Ekstrand7b982072014-05-20 14:33:03 -05003934 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003935}
3936
3937static void
3938weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3939{
3940 struct weston_surface *surface = sub->surface;
3941
3942 /*
3943 * If this commit would cause the surface to move by the
3944 * attach(dx, dy) parameters, the old damage region must be
3945 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003946 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003947 */
Derek Foreman152254b2015-11-26 14:17:48 -06003948 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003949 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003950 pixman_region32_union(&sub->cached.damage_surface,
3951 &sub->cached.damage_surface,
3952 &surface->pending.damage_surface);
3953 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003954
3955 if (surface->pending.newly_attached) {
3956 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003957 weston_surface_state_set_buffer(&sub->cached,
3958 surface->pending.buffer);
3959 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003960 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003961 weston_presentation_feedback_discard_list(
3962 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003963 /* zwp_surface_synchronization_v1.set_acquire_fence */
3964 fd_move(&sub->cached.acquire_fence_fd,
3965 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003966 /* zwp_surface_synchronization_v1.get_release */
3967 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3968 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003969 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303970 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303971 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003972 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003973 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003974 sub->cached.sx += surface->pending.sx;
3975 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003976
Derek Foreman152254b2015-11-26 14:17:48 -06003977 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3978
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003979 sub->cached.buffer_viewport.changed |=
3980 surface->pending.buffer_viewport.changed;
3981 sub->cached.buffer_viewport.buffer =
3982 surface->pending.buffer_viewport.buffer;
3983 sub->cached.buffer_viewport.surface =
3984 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003985
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003986 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003987
3988 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3989
3990 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3991
3992 wl_list_insert_list(&sub->cached.frame_callback_list,
3993 &surface->pending.frame_callback_list);
3994 wl_list_init(&surface->pending.frame_callback_list);
3995
Pekka Paalanen133e4392014-09-23 22:08:46 -04003996 wl_list_insert_list(&sub->cached.feedback_list,
3997 &surface->pending.feedback_list);
3998 wl_list_init(&surface->pending.feedback_list);
3999
Jason Ekstrand7b982072014-05-20 14:33:03 -05004000 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004001}
4002
Derek Foreman280e7dd2014-10-03 13:13:42 -05004003static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004004weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4005{
4006 while (sub) {
4007 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004008 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004009
4010 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004011 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004012
4013 sub = weston_surface_to_subsurface(sub->parent);
4014 }
4015
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004016 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004017}
4018
4019static void
4020weston_subsurface_commit(struct weston_subsurface *sub)
4021{
4022 struct weston_surface *surface = sub->surface;
4023 struct weston_subsurface *tmp;
4024
4025 /* Recursive check for effectively synchronized. */
4026 if (weston_subsurface_is_synchronized(sub)) {
4027 weston_subsurface_commit_to_cache(sub);
4028 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004029 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004030 /* flush accumulated state from cache */
4031 weston_subsurface_commit_to_cache(sub);
4032 weston_subsurface_commit_from_cache(sub);
4033 } else {
4034 weston_surface_commit(surface);
4035 }
4036
4037 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4038 if (tmp->surface != surface)
4039 weston_subsurface_parent_commit(tmp, 0);
4040 }
4041 }
4042}
4043
4044static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004045weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004046{
4047 struct weston_surface *surface = sub->surface;
4048 struct weston_subsurface *tmp;
4049
Pekka Paalanene67858b2013-04-25 13:57:42 +03004050 /* From now on, commit_from_cache the whole sub-tree, regardless of
4051 * the synchronized mode of each child. This sub-surface or some
4052 * of its ancestors were synchronized, so we are synchronized
4053 * all the way down.
4054 */
4055
Jason Ekstrand7b982072014-05-20 14:33:03 -05004056 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004057 weston_subsurface_commit_from_cache(sub);
4058
4059 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4060 if (tmp->surface != surface)
4061 weston_subsurface_parent_commit(tmp, 1);
4062 }
4063}
4064
4065static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004066weston_subsurface_parent_commit(struct weston_subsurface *sub,
4067 int parent_is_synchronized)
4068{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004069 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004070 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004071 wl_list_for_each(view, &sub->surface->views, surface_link)
4072 weston_view_set_position(view,
4073 sub->position.x,
4074 sub->position.y);
4075
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004076 sub->position.set = 0;
4077 }
4078
4079 if (parent_is_synchronized || sub->synchronized)
4080 weston_subsurface_synchronized_commit(sub);
4081}
4082
Pekka Paalanen8274d902014-08-06 19:36:51 +03004083static int
4084subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4085{
4086 return snprintf(buf, len, "sub-surface");
4087}
4088
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004089static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004090subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004091{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004092 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004093
Jason Ekstranda7af7042013-10-12 22:38:11 -05004094 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004095 weston_view_set_position(view,
4096 view->geometry.x + dx,
4097 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004098
4099 /* No need to check parent mappedness, because if parent is not
4100 * mapped, parent is not in a visible layer, so this sub-surface
4101 * will not be drawn either.
4102 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004103
Pekka Paalanene67858b2013-04-25 13:57:42 +03004104 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004105 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004106
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004107 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004108 * because that would call it also for the parent surface,
4109 * which might not be mapped yet. That would lead to
4110 * inconsistent state, where the window could never be
4111 * mapped.
4112 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004113 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004114 * weston_surface_is_mapped() return true, so that when the
4115 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004116 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004117 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004118 }
4119}
4120
4121static struct weston_subsurface *
4122weston_surface_to_subsurface(struct weston_surface *surface)
4123{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004124 if (surface->committed == subsurface_committed)
4125 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004126
4127 return NULL;
4128}
4129
Pekka Paalanen01388e22013-04-25 13:57:44 +03004130WL_EXPORT struct weston_surface *
4131weston_surface_get_main_surface(struct weston_surface *surface)
4132{
4133 struct weston_subsurface *sub;
4134
4135 while (surface && (sub = weston_surface_to_subsurface(surface)))
4136 surface = sub->parent;
4137
4138 return surface;
4139}
4140
Pekka Paalanen50b67472014-10-01 15:02:41 +03004141WL_EXPORT int
4142weston_surface_set_role(struct weston_surface *surface,
4143 const char *role_name,
4144 struct wl_resource *error_resource,
4145 uint32_t error_code)
4146{
4147 assert(role_name);
4148
4149 if (surface->role_name == NULL ||
4150 surface->role_name == role_name ||
4151 strcmp(surface->role_name, role_name) == 0) {
4152 surface->role_name = role_name;
4153
4154 return 0;
4155 }
4156
4157 wl_resource_post_error(error_resource, error_code,
4158 "Cannot assign role %s to wl_surface@%d,"
4159 " already has role %s\n",
4160 role_name,
4161 wl_resource_get_id(surface->resource),
4162 surface->role_name);
4163 return -1;
4164}
4165
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004166WL_EXPORT const char *
4167weston_surface_get_role(struct weston_surface *surface)
4168{
4169 return surface->role_name;
4170}
4171
Pekka Paalanen8274d902014-08-06 19:36:51 +03004172WL_EXPORT void
4173weston_surface_set_label_func(struct weston_surface *surface,
4174 int (*desc)(struct weston_surface *,
4175 char *, size_t))
4176{
4177 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004178 weston_timeline_refresh_subscription_objects(surface->compositor,
4179 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004180}
4181
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004182/** Get the size of surface contents
4183 *
4184 * \param surface The surface to query.
4185 * \param width Returns the width of raw contents.
4186 * \param height Returns the height of raw contents.
4187 *
4188 * Retrieves the raw surface content size in pixels for the given surface.
4189 * This is the whole content size in buffer pixels. If the surface
4190 * has no content or the renderer does not implement this feature,
4191 * zeroes are returned.
4192 *
4193 * This function is used to determine the buffer size needed for
4194 * a weston_surface_copy_content() call.
4195 */
4196WL_EXPORT void
4197weston_surface_get_content_size(struct weston_surface *surface,
4198 int *width, int *height)
4199{
4200 struct weston_renderer *rer = surface->compositor->renderer;
4201
4202 if (!rer->surface_get_content_size) {
4203 *width = 0;
4204 *height = 0;
4205 return;
4206 }
4207
4208 rer->surface_get_content_size(surface, width, height);
4209}
4210
Quentin Glidic248dd102016-08-12 10:41:34 +02004211/** Get the bounding box of a surface and its subsurfaces
4212 *
4213 * \param surface The surface to query.
4214 * \return The bounding box relative to the surface origin.
4215 *
4216 */
4217WL_EXPORT struct weston_geometry
4218weston_surface_get_bounding_box(struct weston_surface *surface)
4219{
4220 pixman_region32_t region;
4221 pixman_box32_t *box;
4222 struct weston_subsurface *subsurface;
4223
4224 pixman_region32_init_rect(&region,
4225 0, 0,
4226 surface->width, surface->height);
4227
4228 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4229 pixman_region32_union_rect(&region, &region,
4230 subsurface->position.x,
4231 subsurface->position.y,
4232 subsurface->surface->width,
4233 subsurface->surface->height);
4234
4235 box = pixman_region32_extents(&region);
4236 struct weston_geometry geometry = {
4237 .x = box->x1,
4238 .y = box->y1,
4239 .width = box->x2 - box->x1,
4240 .height = box->y2 - box->y1,
4241 };
4242
4243 pixman_region32_fini(&region);
4244
4245 return geometry;
4246}
4247
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004248/** Copy surface contents to system memory.
4249 *
4250 * \param surface The surface to copy from.
4251 * \param target Pointer to the target memory buffer.
4252 * \param size Size of the target buffer in bytes.
4253 * \param src_x X location on contents to copy from.
4254 * \param src_y Y location on contents to copy from.
4255 * \param width Width in pixels of the area to copy.
4256 * \param height Height in pixels of the area to copy.
4257 * \return 0 for success, -1 for failure.
4258 *
4259 * Surface contents are maintained by the renderer. They can be in a
4260 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4261 * else.
4262 *
4263 * Surface contents are copied into memory pointed to by target,
4264 * which has size bytes of space available. The target memory
4265 * may be larger than needed, but being smaller returns an error.
4266 * The extra bytes in target may or may not be written; their content is
4267 * unspecified. Size must be large enough to hold the image.
4268 *
4269 * The image in the target memory will be arranged in rows from
4270 * top to bottom, and pixels on a row from left to right. The pixel
4271 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4272 * width * 4.
4273 *
4274 * Parameters src_x and src_y define the upper-left corner in buffer
4275 * coordinates (pixels) to copy from. Parameters width and height
4276 * define the size of the area to copy in pixels.
4277 *
4278 * The rectangle defined by src_x, src_y, width, height must fit in
4279 * the surface contents. Otherwise an error is returned.
4280 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004281 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004282 * needed target buffer size and rectangle limits.
4283 *
4284 * CURRENT IMPLEMENTATION RESTRICTIONS:
4285 * - the machine must be little-endian due to Pixman formats.
4286 *
4287 * NOTE: Pixman formats are premultiplied.
4288 */
4289WL_EXPORT int
4290weston_surface_copy_content(struct weston_surface *surface,
4291 void *target, size_t size,
4292 int src_x, int src_y,
4293 int width, int height)
4294{
4295 struct weston_renderer *rer = surface->compositor->renderer;
4296 int cw, ch;
4297 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4298
4299 if (!rer->surface_copy_content)
4300 return -1;
4301
4302 weston_surface_get_content_size(surface, &cw, &ch);
4303
4304 if (src_x < 0 || src_y < 0)
4305 return -1;
4306
4307 if (width <= 0 || height <= 0)
4308 return -1;
4309
4310 if (src_x + width > cw || src_y + height > ch)
4311 return -1;
4312
4313 if (width * bytespp * height > size)
4314 return -1;
4315
4316 return rer->surface_copy_content(surface, target, size,
4317 src_x, src_y, width, height);
4318}
4319
Pekka Paalanene67858b2013-04-25 13:57:42 +03004320static void
4321subsurface_set_position(struct wl_client *client,
4322 struct wl_resource *resource, int32_t x, int32_t y)
4323{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004324 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004325
4326 if (!sub)
4327 return;
4328
4329 sub->position.x = x;
4330 sub->position.y = y;
4331 sub->position.set = 1;
4332}
4333
4334static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004335subsurface_find_sibling(struct weston_subsurface *sub,
4336 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004337{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004338 struct weston_surface *parent = sub->parent;
4339 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004340
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004341 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4342 if (sibling->surface == surface && sibling != sub)
4343 return sibling;
4344 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004345
4346 return NULL;
4347}
4348
4349static struct weston_subsurface *
4350subsurface_sibling_check(struct weston_subsurface *sub,
4351 struct weston_surface *surface,
4352 const char *request)
4353{
4354 struct weston_subsurface *sibling;
4355
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004356 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004357 if (!sibling) {
4358 wl_resource_post_error(sub->resource,
4359 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4360 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004361 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004362 return NULL;
4363 }
4364
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004365 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004366
4367 return sibling;
4368}
4369
4370static void
4371subsurface_place_above(struct wl_client *client,
4372 struct wl_resource *resource,
4373 struct wl_resource *sibling_resource)
4374{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004375 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004376 struct weston_surface *surface =
4377 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004378 struct weston_subsurface *sibling;
4379
4380 if (!sub)
4381 return;
4382
4383 sibling = subsurface_sibling_check(sub, surface, "place_above");
4384 if (!sibling)
4385 return;
4386
4387 wl_list_remove(&sub->parent_link_pending);
4388 wl_list_insert(sibling->parent_link_pending.prev,
4389 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004390
4391 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004392}
4393
4394static void
4395subsurface_place_below(struct wl_client *client,
4396 struct wl_resource *resource,
4397 struct wl_resource *sibling_resource)
4398{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004399 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004400 struct weston_surface *surface =
4401 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004402 struct weston_subsurface *sibling;
4403
4404 if (!sub)
4405 return;
4406
4407 sibling = subsurface_sibling_check(sub, surface, "place_below");
4408 if (!sibling)
4409 return;
4410
4411 wl_list_remove(&sub->parent_link_pending);
4412 wl_list_insert(&sibling->parent_link_pending,
4413 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004414
4415 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004416}
4417
4418static void
4419subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4420{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004421 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004422
4423 if (sub)
4424 sub->synchronized = 1;
4425}
4426
4427static void
4428subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4429{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004430 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004431
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004432 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004433 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004434
4435 /* If sub became effectively desynchronized, flush. */
4436 if (!weston_subsurface_is_synchronized(sub))
4437 weston_subsurface_synchronized_commit(sub);
4438 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004439}
4440
4441static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004442weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4443{
4444 wl_list_remove(&sub->parent_link);
4445 wl_list_remove(&sub->parent_link_pending);
4446 wl_list_remove(&sub->parent_destroy_listener.link);
4447 sub->parent = NULL;
4448}
4449
4450static void
4451weston_subsurface_destroy(struct weston_subsurface *sub);
4452
4453static void
4454subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4455{
4456 struct weston_subsurface *sub =
4457 container_of(listener, struct weston_subsurface,
4458 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004459 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004460
4461 /* The protocol object (wl_resource) is left inert. */
4462 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004463 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004464
4465 weston_subsurface_destroy(sub);
4466}
4467
4468static void
4469subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4470{
4471 struct weston_subsurface *sub =
4472 container_of(listener, struct weston_subsurface,
4473 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004474 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004475 assert(sub->surface != sub->parent);
4476
4477 if (weston_surface_is_mapped(sub->surface))
4478 weston_surface_unmap(sub->surface);
4479
4480 weston_subsurface_unlink_parent(sub);
4481}
4482
4483static void
4484subsurface_resource_destroy(struct wl_resource *resource)
4485{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004486 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004487
4488 if (sub)
4489 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004490}
4491
4492static void
4493subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4494{
4495 wl_resource_destroy(resource);
4496}
4497
4498static void
4499weston_subsurface_link_parent(struct weston_subsurface *sub,
4500 struct weston_surface *parent)
4501{
4502 sub->parent = parent;
4503 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004504 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004505 &sub->parent_destroy_listener);
4506
4507 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4508 wl_list_insert(&parent->subsurface_list_pending,
4509 &sub->parent_link_pending);
4510}
4511
4512static void
4513weston_subsurface_link_surface(struct weston_subsurface *sub,
4514 struct weston_surface *surface)
4515{
4516 sub->surface = surface;
4517 sub->surface_destroy_listener.notify =
4518 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004519 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004520 &sub->surface_destroy_listener);
4521}
4522
4523static void
4524weston_subsurface_destroy(struct weston_subsurface *sub)
4525{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004526 struct weston_view *view, *next;
4527
Pekka Paalanene67858b2013-04-25 13:57:42 +03004528 assert(sub->surface);
4529
4530 if (sub->resource) {
4531 assert(weston_surface_to_subsurface(sub->surface) == sub);
4532 assert(sub->parent_destroy_listener.notify ==
4533 subsurface_handle_parent_destroy);
4534
George Kiagiadakised04d382014-06-13 18:10:26 +02004535 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4536 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004537 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004538 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004539
Pekka Paalanene67858b2013-04-25 13:57:42 +03004540 if (sub->parent)
4541 weston_subsurface_unlink_parent(sub);
4542
Jason Ekstrand7b982072014-05-20 14:33:03 -05004543 weston_surface_state_fini(&sub->cached);
4544 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004545
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004546 sub->surface->committed = NULL;
4547 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004548 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004549 } else {
4550 /* the dummy weston_subsurface for the parent itself */
4551 assert(sub->parent_destroy_listener.notify == NULL);
4552 wl_list_remove(&sub->parent_link);
4553 wl_list_remove(&sub->parent_link_pending);
4554 }
4555
4556 wl_list_remove(&sub->surface_destroy_listener.link);
4557 free(sub);
4558}
4559
4560static const struct wl_subsurface_interface subsurface_implementation = {
4561 subsurface_destroy,
4562 subsurface_set_position,
4563 subsurface_place_above,
4564 subsurface_place_below,
4565 subsurface_set_sync,
4566 subsurface_set_desync
4567};
4568
4569static struct weston_subsurface *
4570weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4571 struct weston_surface *parent)
4572{
4573 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004574 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004575
Bryce Harringtonde16d892014-11-20 22:21:57 -08004576 sub = zalloc(sizeof *sub);
4577 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004578 return NULL;
4579
Jason Ekstranda7af7042013-10-12 22:38:11 -05004580 wl_list_init(&sub->unused_views);
4581
Jason Ekstranda85118c2013-06-27 20:17:02 -05004582 sub->resource =
4583 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004584 if (!sub->resource) {
4585 free(sub);
4586 return NULL;
4587 }
4588
Jason Ekstranda85118c2013-06-27 20:17:02 -05004589 wl_resource_set_implementation(sub->resource,
4590 &subsurface_implementation,
4591 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004592 weston_subsurface_link_surface(sub, surface);
4593 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004594 weston_surface_state_init(&sub->cached);
4595 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004596 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004597
4598 return sub;
4599}
4600
4601/* Create a dummy subsurface for having the parent itself in its
4602 * sub-surface lists. Makes stacking order manipulation easy.
4603 */
4604static struct weston_subsurface *
4605weston_subsurface_create_for_parent(struct weston_surface *parent)
4606{
4607 struct weston_subsurface *sub;
4608
Bryce Harringtonde16d892014-11-20 22:21:57 -08004609 sub = zalloc(sizeof *sub);
4610 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004611 return NULL;
4612
4613 weston_subsurface_link_surface(sub, parent);
4614 sub->parent = parent;
4615 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4616 wl_list_insert(&parent->subsurface_list_pending,
4617 &sub->parent_link_pending);
4618
4619 return sub;
4620}
4621
4622static void
4623subcompositor_get_subsurface(struct wl_client *client,
4624 struct wl_resource *resource,
4625 uint32_t id,
4626 struct wl_resource *surface_resource,
4627 struct wl_resource *parent_resource)
4628{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004629 struct weston_surface *surface =
4630 wl_resource_get_user_data(surface_resource);
4631 struct weston_surface *parent =
4632 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004633 struct weston_subsurface *sub;
4634 static const char where[] = "get_subsurface: wl_subsurface@";
4635
4636 if (surface == parent) {
4637 wl_resource_post_error(resource,
4638 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4639 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004640 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004641 return;
4642 }
4643
4644 if (weston_surface_to_subsurface(surface)) {
4645 wl_resource_post_error(resource,
4646 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4647 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004648 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004649 return;
4650 }
4651
Pekka Paalanen50b67472014-10-01 15:02:41 +03004652 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4653 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004654 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004655
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004656 if (weston_surface_get_main_surface(parent) == surface) {
4657 wl_resource_post_error(resource,
4658 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4659 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004660 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004661 return;
4662 }
4663
Pekka Paalanene67858b2013-04-25 13:57:42 +03004664 /* make sure the parent is in its own list */
4665 if (wl_list_empty(&parent->subsurface_list)) {
4666 if (!weston_subsurface_create_for_parent(parent)) {
4667 wl_resource_post_no_memory(resource);
4668 return;
4669 }
4670 }
4671
4672 sub = weston_subsurface_create(id, surface, parent);
4673 if (!sub) {
4674 wl_resource_post_no_memory(resource);
4675 return;
4676 }
4677
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004678 surface->committed = subsurface_committed;
4679 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004680 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004681}
4682
4683static void
4684subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4685{
4686 wl_resource_destroy(resource);
4687}
4688
4689static const struct wl_subcompositor_interface subcompositor_interface = {
4690 subcompositor_destroy,
4691 subcompositor_get_subsurface
4692};
4693
4694static void
4695bind_subcompositor(struct wl_client *client,
4696 void *data, uint32_t version, uint32_t id)
4697{
4698 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004699 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004700
Jason Ekstranda85118c2013-06-27 20:17:02 -05004701 resource =
4702 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004703 if (resource == NULL) {
4704 wl_client_post_no_memory(client);
4705 return;
4706 }
4707 wl_resource_set_implementation(resource, &subcompositor_interface,
4708 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004709}
4710
Bryce Harrington0795ece2016-08-30 12:04:26 -07004711/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004712 *
4713 * \param compositor The compositor instance
4714 * \param state The DPMS state the outputs will be set to
4715 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004716static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004717weston_compositor_dpms(struct weston_compositor *compositor,
4718 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004719{
4720 struct weston_output *output;
4721
Bryce Harrington08976ac2016-08-30 12:05:16 -07004722 wl_list_for_each(output, &compositor->output_list, link)
4723 if (output->set_dpms)
4724 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004725}
4726
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004727/** Restores the compositor to active status
4728 *
4729 * \param compositor The compositor instance
4730 *
4731 * If the compositor was in a sleeping mode, all outputs are powered
4732 * back on via DPMS. Otherwise if the compositor was inactive
4733 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4734 * signal will fire.
4735 *
4736 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004737 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004738 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004739WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004740weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004741{
Neil Roberts8b62e202013-09-30 13:14:47 +01004742 uint32_t old_state = compositor->state;
4743
4744 /* The state needs to be changed before emitting the wake
4745 * signal because that may try to schedule a repaint which
4746 * will not work if the compositor is still sleeping */
4747 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4748
4749 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004750 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004751 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004752 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004753 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004754 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004755 /* fall through */
4756 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004757 wl_event_source_timer_update(compositor->idle_source,
4758 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004759 }
4760}
4761
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004762/** Turns off rendering and frame events for the compositor.
4763 *
4764 * \param compositor The compositor instance
4765 *
4766 * This is used for example to prevent further rendering while the
4767 * compositor is shutting down.
4768 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004769 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004770 *
4771 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004772 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004773WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004774weston_compositor_offscreen(struct weston_compositor *compositor)
4775{
4776 switch (compositor->state) {
4777 case WESTON_COMPOSITOR_OFFSCREEN:
4778 return;
4779 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004780 default:
4781 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4782 wl_event_source_timer_update(compositor->idle_source, 0);
4783 }
4784}
4785
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004786/** Powers down all attached output devices
4787 *
4788 * \param compositor The compositor instance
4789 *
4790 * Causes rendering to the outputs to cease, and no frame events to be
4791 * sent. Only powers down the outputs if the compositor is not already
4792 * in sleep mode.
4793 *
4794 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004795 *
4796 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004797 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004798WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004799weston_compositor_sleep(struct weston_compositor *compositor)
4800{
4801 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4802 return;
4803
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004804 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004805 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4806 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4807}
4808
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004809/** Sets compositor to idle mode
4810 *
4811 * \param data The compositor instance
4812 *
4813 * This is called when the idle timer fires. Once the compositor is in
4814 * idle mode it requires a wake action (e.g. via
4815 * weston_compositor_wake()) to restore it. The compositor's
4816 * idle_signal will be triggered when the idle event occurs.
4817 *
4818 * Idleness can be inhibited by setting the compositor's idle_inhibit
4819 * property.
4820 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004821static int
4822idle_handler(void *data)
4823{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004824 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004825
4826 if (compositor->idle_inhibit)
4827 return 1;
4828
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004829 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004830 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004831
4832 return 1;
4833}
4834
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004835WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004836weston_plane_init(struct weston_plane *plane,
4837 struct weston_compositor *ec,
4838 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004839{
4840 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004841 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004842 plane->x = x;
4843 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004844 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004845
4846 /* Init the link so that the call to wl_list_remove() when releasing
4847 * the plane without ever stacking doesn't lead to a crash */
4848 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004849}
4850
4851WL_EXPORT void
4852weston_plane_release(struct weston_plane *plane)
4853{
Xiong Zhang97116532013-10-23 13:58:31 +08004854 struct weston_view *view;
4855
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004856 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004857 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004858
Xiong Zhang97116532013-10-23 13:58:31 +08004859 wl_list_for_each(view, &plane->compositor->view_list, link) {
4860 if (view->plane == plane)
4861 view->plane = NULL;
4862 }
4863
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004864 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004865}
4866
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004867/** weston_compositor_stack_plane
4868 * \ingroup compositor
4869 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004870WL_EXPORT void
4871weston_compositor_stack_plane(struct weston_compositor *ec,
4872 struct weston_plane *plane,
4873 struct weston_plane *above)
4874{
4875 if (above)
4876 wl_list_insert(above->link.prev, &plane->link);
4877 else
4878 wl_list_insert(&ec->plane_list, &plane->link);
4879}
4880
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004881static void
4882output_release(struct wl_client *client, struct wl_resource *resource)
4883{
4884 wl_resource_destroy(resource);
4885}
4886
4887static const struct wl_output_interface output_interface = {
4888 output_release,
4889};
4890
4891
Casey Dahlin9074db52012-04-19 22:50:09 -04004892static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004893{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004894 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004895}
4896
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004897static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004898bind_output(struct wl_client *client,
4899 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004900{
Pekka Paalanen05347622017-03-27 12:24:34 +03004901 struct weston_head *head = data;
4902 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004903 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004904 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004905
Jason Ekstranda85118c2013-06-27 20:17:02 -05004906 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004907 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004908 if (resource == NULL) {
4909 wl_client_post_no_memory(client);
4910 return;
4911 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004912
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004913 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004914 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004915 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004916
Pekka Paalanen05347622017-03-27 12:24:34 +03004917 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004918 wl_output_send_geometry(resource,
4919 output->x,
4920 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004921 head->mm_width,
4922 head->mm_height,
4923 head->subpixel,
4924 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004925 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004926 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004927 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004928 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004929
4930 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004931 wl_output_send_mode(resource,
4932 mode->flags,
4933 mode->width,
4934 mode->height,
4935 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004936 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004937
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004938 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004939 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004940}
4941
Pekka Paalanendcac3512017-12-08 14:13:34 +02004942static void
4943weston_head_add_global(struct weston_head *head)
4944{
4945 head->global = wl_global_create(head->compositor->wl_display,
4946 &wl_output_interface, 3,
4947 head, bind_output);
4948}
4949
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004950/** Remove the global wl_output protocol object
4951 *
4952 * \param head The head whose global to remove.
4953 *
4954 * Also orphans the wl_resources for this head (wl_output).
4955 */
4956static void
4957weston_head_remove_global(struct weston_head *head)
4958{
4959 struct wl_resource *resource, *tmp;
4960
4961 if (head->global)
4962 wl_global_destroy(head->global);
4963 head->global = NULL;
4964
4965 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4966 unbind_resource(resource);
4967 wl_resource_set_destructor(resource, NULL);
4968 wl_resource_set_user_data(resource, NULL);
4969 }
Roman Gilge97391c2019-03-29 13:01:06 +01004970
4971 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
4972 /* It's sufficient to unset the destructor, then the list elements
4973 * won't be accessed.
4974 */
4975 wl_resource_set_destructor(resource, NULL);
4976 }
4977 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004978}
4979
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004980/** Get the backing object of wl_output
4981 *
4982 * \param resource A wl_output protocol object.
4983 * \return The backing object (user data) of a wl_resource representing a
4984 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004985 *
4986 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004987 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004988WL_EXPORT struct weston_head *
4989weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004990{
4991 assert(wl_resource_instance_of(resource, &wl_output_interface,
4992 &output_interface));
4993
4994 return wl_resource_get_user_data(resource);
4995}
4996
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004997/** Initialize a pre-allocated weston_head
4998 *
4999 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005000 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005001 *
5002 * The head will be safe to attach, detach and release.
5003 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005004 * The name is used in logs, and can be used by compositors as a configuration
5005 * identifier.
5006 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005007 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005008 * \internal
5009 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005010WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005011weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005012{
5013 /* Add some (in)sane defaults which can be used
5014 * for checking if an output was properly configured
5015 */
5016 memset(head, 0, sizeof *head);
5017
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005018 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005019 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005020 wl_list_init(&head->output_link);
5021 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005022 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005023 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305024 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005025}
5026
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005027/** Send output heads changed signal
5028 *
5029 * \param output The output that changed.
5030 *
5031 * Notify that the enabled output gained and/or lost heads, or that the
5032 * associated heads may have changed their connection status. This does not
5033 * include cases where the output becomes enabled or disabled. The registered
5034 * callbacks are called after the change has successfully happened.
5035 *
5036 * If connection status change causes the compositor to attach or detach a head
5037 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005038 *
5039 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005040 */
5041static void
5042weston_output_emit_heads_changed(struct weston_output *output)
5043{
5044 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5045 output);
5046}
5047
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005048/** Idle task for emitting heads_changed_signal */
5049static void
5050weston_compositor_call_heads_changed(void *data)
5051{
5052 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005053 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005054
5055 compositor->heads_changed_source = NULL;
5056
5057 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005058
5059 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5060 if (head->output && head->output->enabled)
5061 weston_output_emit_heads_changed(head->output);
5062 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005063}
5064
5065/** Schedule a call on idle to heads_changed callback
5066 *
5067 * \param compositor The Compositor.
5068 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005069 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005070 * \internal
5071 */
5072static void
5073weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5074{
5075 struct wl_event_loop *loop;
5076
5077 if (compositor->heads_changed_source)
5078 return;
5079
5080 loop = wl_display_get_event_loop(compositor->wl_display);
5081 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5082 weston_compositor_call_heads_changed, compositor);
5083}
5084
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005085/** Register a new head
5086 *
5087 * \param compositor The compositor.
5088 * \param head The head to register, must not be already registered.
5089 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005090 * This signals the core that a new head has become available, leading to
5091 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005092 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005093 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005094 * \internal
5095 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005096WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005097weston_compositor_add_head(struct weston_compositor *compositor,
5098 struct weston_head *head)
5099{
5100 assert(wl_list_empty(&head->compositor_link));
5101 assert(head->name);
5102
5103 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5104 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005105 weston_compositor_schedule_heads_changed(compositor);
5106}
5107
5108/** Adds a listener to be called when heads change
5109 *
5110 * \param compositor The compositor.
5111 * \param listener The listener to add.
5112 *
Marius Vlada2dace22019-06-12 16:05:44 +03005113 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005114 *
5115 * The listener function will be called after heads are added or their
5116 * connection status has changed. Several changes may be accumulated into a
5117 * single call. The user is expected to iterate over the existing heads and
5118 * check their statuses to find out what changed.
5119 *
5120 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5121 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005122 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005123 */
5124WL_EXPORT void
5125weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5126 struct wl_listener *listener)
5127{
5128 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005129}
5130
5131/** Iterate over available heads
5132 *
5133 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005134 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005135 * \return The next available head in the list.
5136 *
5137 * Returns all available heads, regardless of being connected or enabled.
5138 *
5139 * You can iterate over all heads as follows:
5140 * \code
5141 * struct weston_head *head = NULL;
5142 *
5143 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5144 * ...
5145 * }
5146 * \endcode
5147 *
5148 * If you cause \c iter to be removed from the list, you cannot use it to
5149 * continue iterating. Removing any other item is safe.
5150 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005151 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005152 */
5153WL_EXPORT struct weston_head *
5154weston_compositor_iterate_heads(struct weston_compositor *compositor,
5155 struct weston_head *iter)
5156{
5157 struct wl_list *list = &compositor->head_list;
5158 struct wl_list *node;
5159
5160 assert(compositor);
5161 assert(!iter || iter->compositor == compositor);
5162
5163 if (iter)
5164 node = iter->compositor_link.next;
5165 else
5166 node = list->next;
5167
5168 assert(node);
5169 assert(!iter || node != &iter->compositor_link);
5170
5171 if (node == list)
5172 return NULL;
5173
5174 return container_of(node, struct weston_head, compositor_link);
5175}
5176
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005177/** Iterate over attached heads
5178 *
5179 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005180 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005181 * \return The next attached head in the list.
5182 *
5183 * Returns all heads currently attached to the output.
5184 *
5185 * You can iterate over all heads as follows:
5186 * \code
5187 * struct weston_head *head = NULL;
5188 *
5189 * while ((head = weston_output_iterate_heads(output, head))) {
5190 * ...
5191 * }
5192 * \endcode
5193 *
5194 * If you cause \c iter to be removed from the list, you cannot use it to
5195 * continue iterating. Removing any other item is safe.
5196 *
Marius Vlad55d87362019-06-11 01:15:35 +03005197 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005198 */
5199WL_EXPORT struct weston_head *
5200weston_output_iterate_heads(struct weston_output *output,
5201 struct weston_head *iter)
5202{
5203 struct wl_list *list = &output->head_list;
5204 struct wl_list *node;
5205
5206 assert(output);
5207 assert(!iter || iter->output == output);
5208
5209 if (iter)
5210 node = iter->output_link.next;
5211 else
5212 node = list->next;
5213
5214 assert(node);
5215 assert(!iter || node != &iter->output_link);
5216
5217 if (node == list)
5218 return NULL;
5219
5220 return container_of(node, struct weston_head, output_link);
5221}
5222
Pekka Paalanendcac3512017-12-08 14:13:34 +02005223/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005224 *
5225 * \param output The output to attach to.
5226 * \param head The head that is not yet attached.
5227 * \return 0 on success, -1 on failure.
5228 *
5229 * Attaches the given head to the output. All heads of an output are clones
5230 * and share the resolution and timings.
5231 *
5232 * Cloning heads this way uses less resources than creating an output for
5233 * each head, but is not always possible due to environment, driver and hardware
5234 * limitations.
5235 *
5236 * On failure, the head remains unattached. Success of this function does not
5237 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005238 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005239 *
Marius Vlad55d87362019-06-11 01:15:35 +03005240 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005241 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005242WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005243weston_output_attach_head(struct weston_output *output,
5244 struct weston_head *head)
5245{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005246 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005247
5248 if (!wl_list_empty(&head->output_link))
5249 return -1;
5250
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005251 if (output->attach_head) {
5252 if (output->attach_head(output, head) < 0)
5253 return -1;
5254 } else if (!wl_list_empty(&output->head_list)) {
5255 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005256 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005257 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005258
5259 head->output = output;
5260 wl_list_insert(output->head_list.prev, &head->output_link);
5261
Pekka Paalanendcac3512017-12-08 14:13:34 +02005262 if (output->enabled) {
5263 weston_head_add_global(head);
5264
5265 head_names = weston_output_create_heads_string(output);
5266 weston_log("Output '%s' updated to have head(s) %s\n",
5267 output->name, head_names);
5268 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005269
5270 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005271 }
5272
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005273 return 0;
5274}
5275
5276/** Detach a head from its output
5277 *
5278 * \param head The head to detach.
5279 *
5280 * It is safe to detach a non-attached head.
5281 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005282 * If the head is attached to an enabled output and the output will be left
5283 * with no heads, the output will be disabled.
5284 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005285 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005286 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005287 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005288WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005289weston_head_detach(struct weston_head *head)
5290{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005291 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005292 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005293
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005294 wl_list_remove(&head->output_link);
5295 wl_list_init(&head->output_link);
5296 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005297
5298 if (!output)
5299 return;
5300
5301 if (output->detach_head)
5302 output->detach_head(output, head);
5303
5304 if (output->enabled) {
5305 weston_head_remove_global(head);
5306
Pekka Paalanena0106992017-12-08 16:11:17 +02005307 if (wl_list_empty(&output->head_list)) {
5308 weston_log("Output '%s' no heads left, disabling.\n",
5309 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005310 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005311 } else {
5312 head_names = weston_output_create_heads_string(output);
5313 weston_log("Output '%s' updated to have head(s) %s\n",
5314 output->name, head_names);
5315 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005316
5317 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005318 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005319 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005320}
5321
5322/** Destroy a head
5323 *
5324 * \param head The head to be released.
5325 *
5326 * Destroys the head. The caller is responsible for freeing the memory pointed
5327 * to by \c head.
5328 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005329 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005330 * \internal
5331 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005332WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005333weston_head_release(struct weston_head *head)
5334{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005335 wl_signal_emit(&head->destroy_signal, head);
5336
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005337 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005338
5339 free(head->make);
5340 free(head->model);
5341 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005342 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005343
5344 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005345}
5346
Pekka Paalanene19970f2017-08-28 14:11:02 +03005347static void
5348weston_head_set_device_changed(struct weston_head *head)
5349{
5350 head->device_changed = true;
5351
5352 if (head->compositor)
5353 weston_compositor_schedule_heads_changed(head->compositor);
5354}
5355
5356/** String equal comparison with NULLs being equal */
5357static bool
5358str_null_eq(const char *a, const char *b)
5359{
5360 if (!a && !b)
5361 return true;
5362
5363 if (!!a != !!b)
5364 return false;
5365
5366 return strcmp(a, b) == 0;
5367}
5368
Pekka Paalanen01f60212017-03-24 15:39:24 +02005369/** Store monitor make, model and serial number
5370 *
5371 * \param head The head to modify.
5372 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5373 * any string, or NULL for none.
5374 * \param model The monitor model or name, or a made-up string, or NULL for
5375 * none.
5376 * \param serialno The monitor serial number, a made-up string, or NULL for
5377 * none.
5378 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005379 * This may set the device_changed flag.
5380 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005381 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005382 * \internal
5383 */
5384WL_EXPORT void
5385weston_head_set_monitor_strings(struct weston_head *head,
5386 const char *make,
5387 const char *model,
5388 const char *serialno)
5389{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005390 if (str_null_eq(head->make, make) &&
5391 str_null_eq(head->model, model) &&
5392 str_null_eq(head->serial_number, serialno))
5393 return;
5394
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005395 free(head->make);
5396 free(head->model);
5397 free(head->serial_number);
5398
5399 head->make = make ? strdup(make) : NULL;
5400 head->model = model ? strdup(model) : NULL;
5401 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005402
5403 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005404}
5405
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005406/** Store display non-desktop status
5407 *
5408 * \param head The head to modify.
5409 * \param non_desktop Whether the head connects to a non-desktop display.
5410 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005411 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005412 * \internal
5413 */
5414WL_EXPORT void
5415weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5416{
5417 if (head->non_desktop == non_desktop)
5418 return;
5419
5420 head->non_desktop = non_desktop;
5421
5422 weston_head_set_device_changed(head);
5423}
5424
Lucas Stacha69cb712019-11-25 23:29:31 +00005425/** Store display transformation
5426 *
5427 * \param head The head to modify.
5428 * \param transform The transformation to apply for this head
5429 *
5430 * This may set the device_changed flag.
5431 *
5432 * \ingroup head
5433 * \internal
5434 */
5435WL_EXPORT void
5436weston_head_set_transform(struct weston_head *head, uint32_t transform)
5437{
5438 if (head->transform == transform)
5439 return;
5440
5441 head->transform = transform;
5442
5443 weston_head_set_device_changed(head);
5444}
5445
5446
Pekka Paalanen01f60212017-03-24 15:39:24 +02005447/** Store physical image size
5448 *
5449 * \param head The head to modify.
5450 * \param mm_width Image area width in millimeters.
5451 * \param mm_height Image area height in millimeters.
5452 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005453 * This may set the device_changed flag.
5454 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005455 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005456 * \internal
5457 */
5458WL_EXPORT void
5459weston_head_set_physical_size(struct weston_head *head,
5460 int32_t mm_width, int32_t mm_height)
5461{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005462 if (head->mm_width == mm_width &&
5463 head->mm_height == mm_height)
5464 return;
5465
Pekka Paalanen01f60212017-03-24 15:39:24 +02005466 head->mm_width = mm_width;
5467 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005468
5469 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005470}
5471
5472/** Store monitor sub-pixel layout
5473 *
5474 * \param head The head to modify.
5475 * \param sp Sub-pixel layout. The possible values are:
5476 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5477 * - WL_OUTPUT_SUBPIXEL_NONE,
5478 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5479 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5480 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5481 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5482 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005483 * This may set the device_changed flag.
5484 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005485 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005486 * \internal
5487 */
5488WL_EXPORT void
5489weston_head_set_subpixel(struct weston_head *head,
5490 enum wl_output_subpixel sp)
5491{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005492 if (head->subpixel == sp)
5493 return;
5494
Pekka Paalanen01f60212017-03-24 15:39:24 +02005495 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005496
5497 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005498}
5499
5500/** Mark the monitor as internal
5501 *
5502 * This is used for embedded screens, like laptop panels.
5503 *
5504 * \param head The head to mark as internal.
5505 *
5506 * By default a head is external. The type is often inferred from the physical
5507 * connector type.
5508 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005509 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005510 * \internal
5511 */
5512WL_EXPORT void
5513weston_head_set_internal(struct weston_head *head)
5514{
5515 head->connection_internal = true;
5516}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005517
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005518/** Store connector status
5519 *
5520 * \param head The head to modify.
5521 * \param connected Whether the head is connected.
5522 *
5523 * Connectors are created as disconnected. This function can be used to
5524 * set the connector status.
5525 *
5526 * The status should be set to true when a physical connector is connected to
5527 * a video sink device like a monitor and to false when the connector is
5528 * disconnected. For nested backends, the connection status should reflect the
5529 * connection to the parent display server.
5530 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005531 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005532 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005533 *
5534 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005535 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005536 * \internal
5537 */
5538WL_EXPORT void
5539weston_head_set_connection_status(struct weston_head *head, bool connected)
5540{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005541 if (head->connected == connected)
5542 return;
5543
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005544 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005545
Pekka Paalanene19970f2017-08-28 14:11:02 +03005546 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005547}
5548
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305549static void
5550weston_output_compute_protection(struct weston_output *output)
5551{
5552 struct weston_head *head;
5553 enum weston_hdcp_protection op_protection;
5554 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305555 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305556
5557 wl_list_for_each(head, &output->head_list, output_link) {
5558 if (!op_protection_valid) {
5559 op_protection = head->current_protection;
5560 op_protection_valid = true;
5561 }
5562 if (head->current_protection < op_protection)
5563 op_protection = head->current_protection;
5564 }
5565
5566 if (!op_protection_valid)
5567 op_protection = WESTON_HDCP_DISABLE;
5568
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305569 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305570 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305571 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305572 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305573 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305574}
5575
5576WL_EXPORT void
5577weston_head_set_content_protection_status(struct weston_head *head,
5578 enum weston_hdcp_protection status)
5579{
5580 head->current_protection = status;
5581 if (head->output)
5582 weston_output_compute_protection(head->output);
5583}
5584
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005585/** Is the head currently connected?
5586 *
5587 * \param head The head to query.
5588 * \return Connection status.
5589 *
5590 * Returns true if the head is physically connected to a monitor, or in
5591 * case of a nested backend returns true when there is a connection to the
5592 * parent display server.
5593 *
5594 * This is independent from the head being enabled.
5595 *
5596 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005597 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005598 */
5599WL_EXPORT bool
5600weston_head_is_connected(struct weston_head *head)
5601{
5602 return head->connected;
5603}
5604
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005605/** Is the head currently enabled?
5606 *
5607 * \param head The head to query.
5608 * \return Video status.
5609 *
5610 * Returns true if the head is currently transmitting a video stream.
5611 *
5612 * This is independent of the head being connected.
5613 *
5614 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005615 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005616 */
5617WL_EXPORT bool
5618weston_head_is_enabled(struct weston_head *head)
5619{
5620 if (!head->output)
5621 return false;
5622
5623 return head->output->enabled;
5624}
5625
Pekka Paalanene19970f2017-08-28 14:11:02 +03005626/** Has the device information changed?
5627 *
5628 * \param head The head to query.
5629 * \return True if the device information has changed since last reset.
5630 *
5631 * The information about the connected display device, e.g. a monitor, may
5632 * change without being disconnected in between. Changing information
5633 * causes a call to the heads_changed hook.
5634 *
5635 * The information includes make, model, serial number, physical size,
5636 * and sub-pixel type. The connection status is also included.
5637 *
5638 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005639 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005640 */
5641WL_EXPORT bool
5642weston_head_is_device_changed(struct weston_head *head)
5643{
5644 return head->device_changed;
5645}
5646
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005647/** Does the head represent a non-desktop display?
5648 *
5649 * \param head The head to query.
5650 * \return True if the device is a non-desktop display.
5651 *
5652 * Non-desktop heads are not attached to outputs by default.
5653 * This stops weston from extending the desktop onto head mounted displays.
5654 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005655 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005656 */
5657WL_EXPORT bool
5658weston_head_is_non_desktop(struct weston_head *head)
5659{
5660 return head->non_desktop;
5661}
5662
Pekka Paalanene19970f2017-08-28 14:11:02 +03005663/** Acknowledge device information change
5664 *
5665 * \param head The head to acknowledge.
5666 *
5667 * Clears the device changed flag on this head. When a compositor has processed
5668 * device information, it should call this to be able to notice further
5669 * changes.
5670 *
5671 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005672 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005673 */
5674WL_EXPORT void
5675weston_head_reset_device_changed(struct weston_head *head)
5676{
5677 head->device_changed = false;
5678}
5679
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005680/** Get the name of a head
5681 *
5682 * \param head The head to query.
5683 * \return The head's name, not NULL.
5684 *
5685 * The name depends on the backend. The DRM backend uses connector names,
5686 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005687 *
5688 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005689 */
5690WL_EXPORT const char *
5691weston_head_get_name(struct weston_head *head)
5692{
5693 return head->name;
5694}
5695
5696/** Get the output the head is attached to
5697 *
5698 * \param head The head to query.
5699 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005700 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005701 */
5702WL_EXPORT struct weston_output *
5703weston_head_get_output(struct weston_head *head)
5704{
5705 return head->output;
5706}
5707
Lucas Stacha69cb712019-11-25 23:29:31 +00005708/** Get the head's native transformation
5709 *
5710 * \param head The head to query.
5711 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5712 *
5713 * A weston_head may have a 'native' transform provided by the backend.
5714 * Examples include panels which are physically rotated, where the rotation
5715 * is recorded and described as part of the system configuration. This call
5716 * will return any known native transform for the head.
5717 *
5718 * \ingroup head
5719 */
5720WL_EXPORT uint32_t
5721weston_head_get_transform(struct weston_head *head)
5722{
5723 return head->transform;
5724}
5725
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005726/** Add destroy callback for a head
5727 *
5728 * \param head The head to watch for.
5729 * \param listener The listener to add. The \c notify member must be set.
5730 *
5731 * Heads may get destroyed for various reasons by the backends. If a head is
5732 * attached to an output, the compositor should listen for head destruction
5733 * and reconfigure or destroy the output if necessary.
5734 *
5735 * The destroy callbacks will be called on weston_head destruction before any
5736 * automatic detaching from an associated weston_output and before any
5737 * weston_head information is lost.
5738 *
5739 * The \c data argument to the notify callback is the weston_head being
5740 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005741 *
5742 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005743 */
5744WL_EXPORT void
5745weston_head_add_destroy_listener(struct weston_head *head,
5746 struct wl_listener *listener)
5747{
5748 wl_signal_add(&head->destroy_signal, listener);
5749}
5750
5751/** Look up destroy listener for a head
5752 *
5753 * \param head The head to query.
5754 * \param notify The notify function used used for the added destroy listener.
5755 * \return The listener, or NULL if not found.
5756 *
5757 * This looks up the previously added destroy listener struct based on the
5758 * notify function it has. The listener can be used to access user data
5759 * through \c container_of().
5760 *
5761 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005762 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005763 */
5764WL_EXPORT struct wl_listener *
5765weston_head_get_destroy_listener(struct weston_head *head,
5766 wl_notify_func_t notify)
5767{
5768 return wl_signal_get(&head->destroy_signal, notify);
5769}
5770
David Fort0de859e2016-05-27 23:22:57 +02005771/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005772static void
David Fort0de859e2016-05-27 23:22:57 +02005773weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5774 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005775{
5776 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005777 bool start_resizing = false;
5778
5779 if (!delta_width)
5780 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005781
5782 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005783 if (output == resized_output) {
5784 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005785 continue;
5786 }
5787
David Fort0de859e2016-05-27 23:22:57 +02005788 if (start_resizing) {
5789 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005790 output->dirty = 1;
5791 }
5792 }
5793}
5794
Pekka Paalanend72bad22017-03-29 17:01:41 +03005795static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005796weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005797{
Scott Moreau850ca422012-05-21 15:21:25 -06005798 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005799
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005800 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005801 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005802
Scott Moreauccbf29d2012-02-22 14:21:41 -07005803 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005804 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005805 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005806 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005807 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005808 weston_matrix_scale(&output->matrix, magnification,
5809 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005810 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005811
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005812 switch (output->transform) {
5813 case WL_OUTPUT_TRANSFORM_FLIPPED:
5814 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5815 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5816 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5817 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5818 weston_matrix_scale(&output->matrix, -1, 1, 1);
5819 break;
5820 }
5821
5822 switch (output->transform) {
5823 default:
5824 case WL_OUTPUT_TRANSFORM_NORMAL:
5825 case WL_OUTPUT_TRANSFORM_FLIPPED:
5826 break;
5827 case WL_OUTPUT_TRANSFORM_90:
5828 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02005829 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5830 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005831 break;
5832 case WL_OUTPUT_TRANSFORM_180:
5833 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5834 weston_matrix_translate(&output->matrix,
5835 -output->width, -output->height, 0);
5836 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5837 break;
5838 case WL_OUTPUT_TRANSFORM_270:
5839 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02005840 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5841 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005842 break;
5843 }
5844
5845 if (output->current_scale != 1)
5846 weston_matrix_scale(&output->matrix,
5847 output->current_scale,
5848 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005849
Scott Moreauccbf29d2012-02-22 14:21:41 -07005850 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005851
5852 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005853}
5854
Scott Moreau1bad5db2012-08-18 01:04:05 -06005855static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005856weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005857{
5858 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005859 output->native_scale = scale;
5860 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005861
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005862 convert_size_by_transform_scale(&output->width, &output->height,
5863 output->current_mode->width,
5864 output->current_mode->height,
5865 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005866}
5867
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005868static void
5869weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005870{
5871 output->x = x;
5872 output->y = y;
5873
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005874 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005875 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005876 output->width,
5877 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005878}
5879
Marius Vlad55d87362019-06-11 01:15:35 +03005880/**
5881 * \ingroup output
5882 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005883WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005884weston_output_move(struct weston_output *output, int x, int y)
5885{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005886 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005887 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005888 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005889
5890 output->move_x = x - output->x;
5891 output->move_y = y - output->y;
5892
5893 if (output->move_x == 0 && output->move_y == 0)
5894 return;
5895
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005896 weston_output_init_geometry(output, x, y);
5897
5898 output->dirty = 1;
5899
5900 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005901 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005902
5903 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005904 wl_list_for_each(head, &output->head_list, output_link) {
5905 wl_resource_for_each(resource, &head->resource_list) {
5906 wl_output_send_geometry(resource,
5907 output->x,
5908 output->y,
5909 head->mm_width,
5910 head->mm_height,
5911 head->subpixel,
5912 head->make,
5913 head->model,
5914 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005915
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005916 ver = wl_resource_get_version(resource);
5917 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5918 wl_output_send_done(resource);
5919 }
Roman Gilge97391c2019-03-29 13:01:06 +01005920
5921 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5922 zxdg_output_v1_send_logical_position(resource,
5923 output->x,
5924 output->y);
5925 zxdg_output_v1_send_done(resource);
5926 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005927 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005928}
5929
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005930/** Signal that a pending output is taken into use.
5931 *
5932 * Removes the output from the pending list and adds it to the compositor's
5933 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005934 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005935 * The output gets an internal ID assigned, and the wl_output global is
5936 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005937 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005938 * \param compositor The compositor instance.
5939 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005940 *
5941 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005942 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005943 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005944static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005945weston_compositor_add_output(struct weston_compositor *compositor,
5946 struct weston_output *output)
5947{
Armin Krezoviće5403842016-08-05 15:28:29 +02005948 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005949 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005950
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005951 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005952
5953 /* Verify we haven't reached the limit of 32 available output IDs */
5954 assert(ffs(~compositor->output_id_pool) > 0);
5955
5956 /* Invert the output id pool and look for the lowest numbered
5957 * switch (the least significant bit). Take that bit's position
5958 * as our ID, and mark it used in the compositor's output_id_pool.
5959 */
5960 output->id = ffs(~compositor->output_id_pool) - 1;
5961 compositor->output_id_pool |= 1u << output->id;
5962
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005963 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005964 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005965 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005966
Pekka Paalanendcac3512017-12-08 14:13:34 +02005967 wl_list_for_each(head, &output->head_list, output_link)
5968 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005969
Giulio Camuffob1147152015-05-06 21:41:57 +03005970 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005971
5972 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5973 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005974}
5975
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005976/** Transform device coordinates into global coordinates
5977 *
Marius Vlada2dace22019-06-12 16:05:44 +03005978 * \param output the weston_output object
5979 * \param[in] device_x X coordinate in device units.
5980 * \param[in] device_y Y coordinate in device units.
5981 * \param[out] x X coordinate in the global space.
5982 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005983 *
Marius Vlada2dace22019-06-12 16:05:44 +03005984 * Transforms coordinates from the device coordinate space (physical pixel
5985 * units) to the global coordinate space (logical pixel units). This takes
5986 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005987 *
Marius Vlad55d87362019-06-11 01:15:35 +03005988 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005989 * \internal
5990 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005991WL_EXPORT void
5992weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005993 double device_x, double device_y,
5994 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005995{
Derek Foreman0f679412014-10-02 13:41:17 -05005996 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005997 device_x,
5998 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005999 0.0,
6000 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006001
Derek Foreman67a18b92015-03-24 11:36:14 -05006002 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006003
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006004 *x = p.f[0] / p.f[3];
6005 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006006}
6007
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006008/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006009 *
6010 * \param output The weston_output object that is being removed.
6011 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006012 * The following happens:
6013 *
6014 * - The output assignments of all views in the current scenegraph are
6015 * recomputed.
6016 *
6017 * - Presentation feedback is discarded.
6018 *
6019 * - Compositor is notified that outputs were changed and
6020 * applies the necessary changes to re-layout outputs.
6021 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006022 * - The output is put back in the pending outputs list.
6023 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006024 * - Signal is emitted to notify all users of the weston_output
6025 * object that the output is being destroyed.
6026 *
6027 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006028 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006029 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006030 * - The output's internal ID is released.
6031 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006032 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006033 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006034 */
6035static void
6036weston_compositor_remove_output(struct weston_output *output)
6037{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006038 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006039 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006040 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006041
6042 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006043 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006044
Pekka Paalanenbccda712017-03-29 16:16:04 +03006045 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006046 if (view->output_mask & (1u << output->id))
6047 weston_view_assign_output(view);
6048 }
6049
6050 weston_presentation_feedback_discard_list(&output->feedback_list);
6051
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006052 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006053
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006054 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006055 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006056 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006057
Pekka Paalanenbccda712017-03-29 16:16:04 +03006058 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006059 wl_signal_emit(&output->destroy_signal, output);
6060
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006061 wl_list_for_each(head, &output->head_list, output_link)
6062 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006063
6064 compositor->output_id_pool &= ~(1u << output->id);
6065 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006066}
6067
6068/** Sets the output scale for a given output.
6069 *
6070 * \param output The weston_output object that the scale is set for.
6071 * \param scale Scale factor for the given output.
6072 *
6073 * It only supports setting scale for an output that
6074 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006075 *
Marius Vlad55d87362019-06-11 01:15:35 +03006076 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006077 */
6078WL_EXPORT void
6079weston_output_set_scale(struct weston_output *output,
6080 int32_t scale)
6081{
6082 /* We can only set scale on a disabled output */
6083 assert(!output->enabled);
6084
6085 /* We only want to set scale once */
6086 assert(!output->scale);
6087
6088 output->scale = scale;
6089}
6090
6091/** Sets the output transform for a given output.
6092 *
6093 * \param output The weston_output object that the transform is set for.
6094 * \param transform Transform value for the given output.
6095 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006096 * Refer to wl_output::transform section located at
6097 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6098 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006099 *
Marius Vlad55d87362019-06-11 01:15:35 +03006100 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006101 */
6102WL_EXPORT void
6103weston_output_set_transform(struct weston_output *output,
6104 uint32_t transform)
6105{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006106 struct weston_pointer_motion_event ev;
6107 struct wl_resource *resource;
6108 struct weston_seat *seat;
6109 pixman_region32_t old_region;
6110 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006111 struct weston_head *head;
6112 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006113
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006114 if (!output->enabled && output->transform == UINT32_MAX) {
6115 output->transform = transform;
6116 return;
6117 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006118
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006119 weston_output_transform_scale_init(output, transform, output->scale);
6120
6121 pixman_region32_init(&old_region);
6122 pixman_region32_copy(&old_region, &output->region);
6123
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006124 weston_output_init_geometry(output, output->x, output->y);
6125
6126 output->dirty = 1;
6127
6128 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006129 wl_list_for_each(head, &output->head_list, output_link) {
6130 wl_resource_for_each(resource, &head->resource_list) {
6131 wl_output_send_geometry(resource,
6132 output->x,
6133 output->y,
6134 head->mm_width,
6135 head->mm_height,
6136 head->subpixel,
6137 head->make,
6138 head->model,
6139 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006140
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006141 ver = wl_resource_get_version(resource);
6142 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6143 wl_output_send_done(resource);
6144 }
Roman Gilge97391c2019-03-29 13:01:06 +01006145 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6146 zxdg_output_v1_send_logical_position(resource,
6147 output->x,
6148 output->y);
6149 zxdg_output_v1_send_logical_size(resource,
6150 output->width,
6151 output->height);
6152 zxdg_output_v1_send_done(resource);
6153 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006154 }
6155
6156 /* we must ensure that pointers are inside output, otherwise they disappear */
6157 mid_x = output->x + output->width / 2;
6158 mid_y = output->y + output->height / 2;
6159
6160 ev.mask = WESTON_POINTER_MOTION_ABS;
6161 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6162 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6163
6164 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6165 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6166
6167 if (pointer && pixman_region32_contains_point(&old_region,
6168 wl_fixed_to_int(pointer->x),
6169 wl_fixed_to_int(pointer->y),
6170 NULL))
6171 weston_pointer_move(pointer, &ev);
6172 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006173}
6174
6175/** Initializes a weston_output object with enough data so
6176 ** an output can be configured.
6177 *
6178 * \param output The weston_output object to initialize
6179 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006180 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006181 *
6182 * Sets initial values for fields that are expected to be
6183 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006184 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006185 * The name is used in logs, and can be used by compositors as a configuration
6186 * identifier.
6187 *
Marius Vlad55d87362019-06-11 01:15:35 +03006188 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006189 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006190 */
6191WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006192weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006193 struct weston_compositor *compositor,
6194 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006195{
6196 output->compositor = compositor;
6197 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006198 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006199 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006200 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006201 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306202 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306203 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006204
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006205 wl_list_init(&output->head_list);
6206
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006207 /* Add some (in)sane defaults which can be used
6208 * for checking if an output was properly configured
6209 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006210 output->scale = 0;
6211 /* Can't use -1 on uint32_t and 0 is valid enum value */
6212 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006213
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006214 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006215 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006216}
6217
6218/** Adds weston_output object to pending output list.
6219 *
6220 * \param output The weston_output object to add
6221 * \param compositor The compositor instance.
6222 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006223 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006224 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006225 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006226 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006227 */
6228WL_EXPORT void
6229weston_compositor_add_pending_output(struct weston_output *output,
6230 struct weston_compositor *compositor)
6231{
Pekka Paalanene952a012017-03-29 17:14:00 +03006232 assert(output->disable);
6233 assert(output->enable);
6234
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006235 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006236 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006237}
6238
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006239/** Create a string with the attached heads' names.
6240 *
6241 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006242 *
6243 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006244 */
6245static char *
6246weston_output_create_heads_string(struct weston_output *output)
6247{
6248 FILE *fp;
6249 char *str = NULL;
6250 size_t size = 0;
6251 struct weston_head *head;
6252 const char *sep = "";
6253
6254 fp = open_memstream(&str, &size);
6255 if (!fp)
6256 return NULL;
6257
6258 wl_list_for_each(head, &output->head_list, output_link) {
6259 fprintf(fp, "%s%s", sep, head->name);
6260 sep = ", ";
6261 }
6262 fclose(fp);
6263
6264 return str;
6265}
6266
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006267/** Constructs a weston_output object that can be used by the compositor.
6268 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006269 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006270 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006271 *
6272 * Output coordinates are calculated and each new output is by default
6273 * assigned to the right of previous one.
6274 *
6275 * Sets up the transformation, zoom, and geometry of the output using
6276 * the properties that need to be configured by the compositor.
6277 *
6278 * Establishes a repaint timer for the output with the relevant display
6279 * object's event loop. See output_repaint_timer_handler().
6280 *
6281 * The output is assigned an ID. Weston can support up to 32 distinct
6282 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6283 * is referred to and used to find the first available ID number, and
6284 * then this ID is marked as used in output_id_pool.
6285 *
6286 * The output is also assigned a Wayland global with the wl_output
6287 * external interface.
6288 *
6289 * Backend specific function is called to set up the output output.
6290 *
6291 * Output is added to the compositor's output list
6292 *
6293 * If the backend specific function fails, the weston_output object
6294 * is returned to a state it was before calling this function and
6295 * is added to the compositor's pending_output_list in case it needs
6296 * to be reconfigured or just so it can be destroyed at shutdown.
6297 *
6298 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006299 *
6300 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006301 */
6302WL_EXPORT int
6303weston_output_enable(struct weston_output *output)
6304{
Armin Krezović782f5df2016-09-30 14:11:11 +02006305 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006306 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006307 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006308 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006309 int x = 0, y = 0;
6310
Pekka Paalanencc201e42017-03-30 15:11:25 +03006311 if (output->enabled) {
6312 weston_log("Error: attempt to enable an enabled output '%s'\n",
6313 output->name);
6314 return -1;
6315 }
6316
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006317 if (wl_list_empty(&output->head_list)) {
6318 weston_log("Error: cannot enable output '%s' without heads.\n",
6319 output->name);
6320 return -1;
6321 }
6322
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006323 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6324 weston_log("Error: no video mode for output '%s'.\n",
6325 output->name);
6326 return -1;
6327 }
6328
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006329 wl_list_for_each(head, &output->head_list, output_link) {
6330 assert(head->make);
6331 assert(head->model);
6332 }
6333
Armin Krezović782f5df2016-09-30 14:11:11 +02006334 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006335 struct weston_output, link);
6336
Armin Krezović782f5df2016-09-30 14:11:11 +02006337 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006338 x = iterator->x + iterator->width;
6339
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006340 /* Make sure the scale is set up */
6341 assert(output->scale);
6342
6343 /* Make sure we have a transform set */
6344 assert(output->transform != UINT32_MAX);
6345
Armin Krezović782f5df2016-09-30 14:11:11 +02006346 output->x = x;
6347 output->y = y;
6348 output->dirty = 1;
6349 output->original_scale = output->scale;
6350
Marius Vlad3a2f8292019-11-04 17:53:46 +02006351 wl_signal_init(&output->frame_signal);
6352 wl_signal_init(&output->destroy_signal);
6353
Armin Krezović782f5df2016-09-30 14:11:11 +02006354 weston_output_transform_scale_init(output, output->transform, output->scale);
6355 weston_output_init_zoom(output);
6356
6357 weston_output_init_geometry(output, x, y);
6358 weston_output_damage(output);
6359
Armin Krezović782f5df2016-09-30 14:11:11 +02006360 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006361 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006362
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006363 /* Enable the output (set up the crtc or create a
6364 * window representing the output, set up the
6365 * renderer, etc)
6366 */
6367 if (output->enable(output) < 0) {
6368 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006369 return -1;
6370 }
6371
6372 weston_compositor_add_output(output->compositor, output);
6373
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006374 head_names = weston_output_create_heads_string(output);
6375 weston_log("Output '%s' enabled with head(s) %s\n",
6376 output->name, head_names);
6377 free(head_names);
6378
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006379 return 0;
6380}
6381
6382/** Converts a weston_output object to a pending output state, so it
6383 ** can be configured again or destroyed.
6384 *
6385 * \param output The weston_output object that needs to be disabled.
6386 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006387 * Calls a backend specific function to disable an output, in case
6388 * such function exists.
6389 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006390 * The backend specific disable function may choose to postpone the disabling
6391 * by returning a negative value, in which case this function returns early.
6392 * In that case the backend will guarantee the output will be disabled soon
6393 * by the backend calling this function again. One must not attempt to re-enable
6394 * the output until that happens.
6395 *
6396 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006397 * from weston's output_list (see weston_compositor_remove_output())
6398 * and is returned to a state it was before weston_output_enable()
6399 * was ran (see weston_output_enable_undo()).
6400 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006401 * See weston_output_init() for more information on the
6402 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006403 *
6404 * If the output has never been enabled yet, this function can still be
6405 * called to ensure that the output is actually turned off rather than left
6406 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006407 *
6408 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006409 */
6410WL_EXPORT void
6411weston_output_disable(struct weston_output *output)
6412{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006413 /* Should we rename this? */
6414 output->destroying = 1;
6415
Pekka Paalanenc65df642017-03-29 15:45:46 +03006416 /* Disable is called unconditionally also for not-enabled outputs,
6417 * because at compositor start-up, if there is an output that is
6418 * already on but the compositor wants to turn it off, we have to
6419 * forward the turn-off to the backend so it knows to do it.
6420 * The backend cannot initially turn off everything, because it
6421 * would cause unnecessary mode-sets for all outputs the compositor
6422 * wants to be on.
6423 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006424 if (output->disable(output) < 0)
6425 return;
6426
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006427 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006428 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006429
6430 output->destroying = 0;
6431}
6432
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006433/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006434 *
6435 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006436 *
6437 * If there are new or changed heads, calls the heads_changed hook and
6438 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006439 *
6440 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006441 */
6442WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006443weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006444{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006445 if (compositor->heads_changed_source) {
6446 wl_event_source_remove(compositor->heads_changed_source);
6447 weston_compositor_call_heads_changed(compositor);
6448 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006449}
6450
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006451/** Add destroy callback for an output
6452 *
6453 * \param output The output to watch.
6454 * \param listener The listener to add. The \c notify member must be set.
6455 *
6456 * The listener callback will be called when user destroys an output. This
6457 * may be delayed by a backend in some cases. The main purpose of the
6458 * listener is to allow hooking up custom data to the output. The custom data
6459 * can be fetched via weston_output_get_destroy_listener() followed by
6460 * container_of().
6461 *
6462 * The \c data argument to the notify callback is the weston_output being
6463 * destroyed.
6464 *
6465 * @note This is for the final destruction of an output, not when it gets
6466 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006467 *
6468 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006469 */
6470WL_EXPORT void
6471weston_output_add_destroy_listener(struct weston_output *output,
6472 struct wl_listener *listener)
6473{
6474 wl_signal_add(&output->user_destroy_signal, listener);
6475}
6476
6477/** Look up destroy listener for an output
6478 *
6479 * \param output The output to query.
6480 * \param notify The notify function used used for the added destroy listener.
6481 * \return The listener, or NULL if not found.
6482 *
6483 * This looks up the previously added destroy listener struct based on the
6484 * notify function it has. The listener can be used to access user data
6485 * through \c container_of().
6486 *
6487 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006488 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006489 */
6490WL_EXPORT struct wl_listener *
6491weston_output_get_destroy_listener(struct weston_output *output,
6492 wl_notify_func_t notify)
6493{
6494 return wl_signal_get(&output->user_destroy_signal, notify);
6495}
6496
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006497/** Uninitialize an output
6498 *
6499 * Removes the output from the list of enabled outputs if necessary, but
6500 * does not call the backend's output disable function. The output will no
6501 * longer be in the list of pending outputs either.
6502 *
6503 * All fields of weston_output become uninitialized, i.e. should not be used
6504 * anymore. The caller can free the memory after this.
6505 *
Marius Vlad55d87362019-06-11 01:15:35 +03006506 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006507 * \internal
6508 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006509WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006510weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006511{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006512 struct weston_head *head, *tmp;
6513
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006514 output->destroying = 1;
6515
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006516 wl_signal_emit(&output->user_destroy_signal, output);
6517
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006518 if (output->idle_repaint_source)
6519 wl_event_source_remove(output->idle_repaint_source);
6520
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006521 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006522 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006523
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006524 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006525 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006526
6527 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6528 weston_head_detach(head);
6529
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006530 free(output->name);
6531}
6532
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006533/** Find an output by its given name
6534 *
6535 * \param compositor The compositor to search in.
6536 * \param name The output name to search for.
6537 * \return An existing output with the given name, or NULL if not found.
6538 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006539 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006540 */
6541WL_EXPORT struct weston_output *
6542weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6543 const char *name)
6544{
6545 struct weston_output *output;
6546
6547 wl_list_for_each(output, &compositor->output_list, link)
6548 if (strcmp(output->name, name) == 0)
6549 return output;
6550
6551 wl_list_for_each(output, &compositor->pending_output_list, link)
6552 if (strcmp(output->name, name) == 0)
6553 return output;
6554
6555 return NULL;
6556}
6557
6558/** Create a named output
6559 *
6560 * \param compositor The compositor.
6561 * \param name The name for the output.
6562 * \return A new \c weston_output, or NULL on failure.
6563 *
6564 * This creates a new weston_output that starts with no heads attached.
6565 *
6566 * An output must be configured and it must have at least one head before
6567 * it can be enabled.
6568 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006569 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006570 */
6571WL_EXPORT struct weston_output *
6572weston_compositor_create_output(struct weston_compositor *compositor,
6573 const char *name)
6574{
6575 assert(compositor->backend->create_output);
6576
6577 if (weston_compositor_find_output_by_name(compositor, name)) {
6578 weston_log("Warning: attempted to create an output with a "
6579 "duplicate name '%s'.\n", name);
6580 return NULL;
6581 }
6582
6583 return compositor->backend->create_output(compositor, name);
6584}
6585
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006586/** Create an output for an unused head
6587 *
6588 * \param compositor The compositor.
6589 * \param head The head to attach to the output.
6590 * \return A new \c weston_output, or NULL on failure.
6591 *
6592 * This creates a new weston_output that starts with the given head attached.
6593 * The output inherits the name of the head. The head must not be already
6594 * attached to another output.
6595 *
6596 * An output must be configured before it can be enabled.
6597 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006598 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006599 */
6600WL_EXPORT struct weston_output *
6601weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6602 struct weston_head *head)
6603{
6604 struct weston_output *output;
6605
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006606 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006607 if (!output)
6608 return NULL;
6609
6610 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006611 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006612 return NULL;
6613 }
6614
6615 return output;
6616}
6617
6618/** Destroy an output
6619 *
6620 * \param output The output to destroy.
6621 *
6622 * The heads attached to the given output are detached and become unused again.
6623 *
6624 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03006625 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006626 * outputs.
6627 *
Marius Vlad55d87362019-06-11 01:15:35 +03006628 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006629 */
6630WL_EXPORT void
6631weston_output_destroy(struct weston_output *output)
6632{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006633 output->destroy(output);
6634}
6635
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006636/** When you need a head...
6637 *
6638 * This function is a hack, used until all code has been converted to become
6639 * multi-head aware.
6640 *
6641 * \param output The weston_output whose head to get.
6642 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006643 *
6644 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006645 */
6646WL_EXPORT struct weston_head *
6647weston_output_get_first_head(struct weston_output *output)
6648{
6649 if (wl_list_empty(&output->head_list))
6650 return NULL;
6651
6652 return container_of(output->head_list.next,
6653 struct weston_head, output_link);
6654}
6655
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306656/** Allow/Disallow content-protection support for an output
6657 *
6658 * This function sets the allow_protection member for an output. Setting of
6659 * this field will allow the compositor to attempt content-protection for this
6660 * output, for a backend that supports the content-protection protocol.
6661 *
6662 * \param output The weston_output for whom the content-protection is to be
6663 * allowed.
6664 * \param allow_protection The bool value which is to be set.
6665 */
6666WL_EXPORT void
6667weston_output_allow_protection(struct weston_output *output,
6668 bool allow_protection)
6669{
6670 output->allow_protection = allow_protection;
6671}
6672
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006673static void
Roman Gilge97391c2019-03-29 13:01:06 +01006674xdg_output_unlist(struct wl_resource *resource)
6675{
6676 wl_list_remove(wl_resource_get_link(resource));
6677}
6678
6679static void
6680xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6681{
6682 wl_resource_destroy(resource);
6683}
6684
6685static const struct zxdg_output_v1_interface xdg_output_interface = {
6686 xdg_output_destroy
6687};
6688
6689static void
6690xdg_output_manager_destroy(struct wl_client *client,
6691 struct wl_resource *resource)
6692{
6693 wl_resource_destroy(resource);
6694}
6695
6696static void
6697xdg_output_manager_get_xdg_output(struct wl_client *client,
6698 struct wl_resource *manager,
6699 uint32_t id,
6700 struct wl_resource *output_resource)
6701{
6702 int version = wl_resource_get_version(manager);
6703 struct weston_head *head = wl_resource_get_user_data(output_resource);
6704 struct weston_output *output = head->output;
6705 struct wl_resource *resource;
6706
6707 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6708 version, id);
6709 if (resource == NULL) {
6710 wl_client_post_no_memory(client);
6711 return;
6712 }
6713
6714 wl_list_insert(&head->xdg_output_resource_list,
6715 wl_resource_get_link(resource));
6716
6717 wl_resource_set_implementation(resource, &xdg_output_interface,
6718 NULL, xdg_output_unlist);
6719
6720 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6721 zxdg_output_v1_send_logical_size(resource,
6722 output->width,
6723 output->height);
6724 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6725 zxdg_output_v1_send_name(resource, head->name);
6726
6727 zxdg_output_v1_send_done(resource);
6728}
6729
6730static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6731 xdg_output_manager_destroy,
6732 xdg_output_manager_get_xdg_output
6733};
6734
6735static void
6736bind_xdg_output_manager(struct wl_client *client,
6737 void *data, uint32_t version, uint32_t id)
6738{
6739 struct wl_resource *resource;
6740
6741 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6742 version, id);
6743 if (resource == NULL) {
6744 wl_client_post_no_memory(client);
6745 return;
6746 }
6747
6748 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6749 NULL, NULL);
6750}
6751
6752static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006753destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006754{
Jonny Lamb74130762013-11-26 18:19:46 +01006755 struct weston_surface *surface =
6756 wl_resource_get_user_data(resource);
6757
Pekka Paalanen4826f872016-04-22 14:14:38 +03006758 if (!surface)
6759 return;
6760
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006761 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006762 surface->pending.buffer_viewport.buffer.src_width =
6763 wl_fixed_from_int(-1);
6764 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006765 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006766}
6767
6768static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006769viewport_destroy(struct wl_client *client,
6770 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006771{
6772 wl_resource_destroy(resource);
6773}
6774
6775static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006776viewport_set_source(struct wl_client *client,
6777 struct wl_resource *resource,
6778 wl_fixed_t src_x,
6779 wl_fixed_t src_y,
6780 wl_fixed_t src_width,
6781 wl_fixed_t src_height)
6782{
6783 struct weston_surface *surface =
6784 wl_resource_get_user_data(resource);
6785
Pekka Paalanen4826f872016-04-22 14:14:38 +03006786 if (!surface) {
6787 wl_resource_post_error(resource,
6788 WP_VIEWPORT_ERROR_NO_SURFACE,
6789 "wl_surface for this viewport is no longer exists");
6790 return;
6791 }
6792
6793 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006794 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006795
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006796 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006797 src_height == wl_fixed_from_int(-1) &&
6798 src_x == wl_fixed_from_int(-1) &&
6799 src_y == wl_fixed_from_int(-1)) {
6800 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006801 surface->pending.buffer_viewport.buffer.src_width =
6802 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006803 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006804 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006805 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006806
Pekka Paalanen201769a2016-04-26 14:42:11 +03006807 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006808 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006809 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006810 "wl_surface@%d viewport source "
6811 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6812 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006813 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006814 wl_fixed_to_double(src_height),
6815 wl_fixed_to_double(src_x),
6816 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006817 return;
6818 }
6819
6820 surface->pending.buffer_viewport.buffer.src_x = src_x;
6821 surface->pending.buffer_viewport.buffer.src_y = src_y;
6822 surface->pending.buffer_viewport.buffer.src_width = src_width;
6823 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006824 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006825}
6826
6827static void
6828viewport_set_destination(struct wl_client *client,
6829 struct wl_resource *resource,
6830 int32_t dst_width,
6831 int32_t dst_height)
6832{
6833 struct weston_surface *surface =
6834 wl_resource_get_user_data(resource);
6835
Pekka Paalanen4826f872016-04-22 14:14:38 +03006836 if (!surface) {
6837 wl_resource_post_error(resource,
6838 WP_VIEWPORT_ERROR_NO_SURFACE,
6839 "wl_surface for this viewport no longer exists");
6840 return;
6841 }
6842
6843 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006844
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006845 if (dst_width == -1 && dst_height == -1) {
6846 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006847 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006848 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006849 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006850 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006851
6852 if (dst_width <= 0 || dst_height <= 0) {
6853 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006854 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006855 "destination size must be positive (%dx%d)",
6856 dst_width, dst_height);
6857 return;
6858 }
6859
6860 surface->pending.buffer_viewport.surface.width = dst_width;
6861 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006862 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006863}
6864
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006865static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006866 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006867 viewport_set_source,
6868 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006869};
6870
6871static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006872viewporter_destroy(struct wl_client *client,
6873 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006874{
6875 wl_resource_destroy(resource);
6876}
6877
6878static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006879viewporter_get_viewport(struct wl_client *client,
6880 struct wl_resource *viewporter,
6881 uint32_t id,
6882 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006883{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006884 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006885 struct weston_surface *surface =
6886 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006887 struct wl_resource *resource;
6888
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006889 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006890 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006891 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006892 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006893 return;
6894 }
6895
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006896 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006897 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006898 if (resource == NULL) {
6899 wl_client_post_no_memory(client);
6900 return;
6901 }
6902
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006903 wl_resource_set_implementation(resource, &viewport_interface,
6904 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006905
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006906 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006907}
6908
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006909static const struct wp_viewporter_interface viewporter_interface = {
6910 viewporter_destroy,
6911 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006912};
6913
6914static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006915bind_viewporter(struct wl_client *client,
6916 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006917{
6918 struct wl_resource *resource;
6919
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006920 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006921 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006922 if (resource == NULL) {
6923 wl_client_post_no_memory(client);
6924 return;
6925 }
6926
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006927 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006928 NULL, NULL);
6929}
6930
6931static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006932destroy_presentation_feedback(struct wl_resource *feedback_resource)
6933{
6934 struct weston_presentation_feedback *feedback;
6935
6936 feedback = wl_resource_get_user_data(feedback_resource);
6937
6938 wl_list_remove(&feedback->link);
6939 free(feedback);
6940}
6941
6942static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006943presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6944{
6945 wl_resource_destroy(resource);
6946}
6947
6948static void
6949presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006950 struct wl_resource *presentation_resource,
6951 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006952 uint32_t callback)
6953{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006954 struct weston_surface *surface;
6955 struct weston_presentation_feedback *feedback;
6956
6957 surface = wl_resource_get_user_data(surface_resource);
6958
Bryce Harringtonde16d892014-11-20 22:21:57 -08006959 feedback = zalloc(sizeof *feedback);
6960 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006961 goto err_calloc;
6962
6963 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006964 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006965 1, callback);
6966 if (!feedback->resource)
6967 goto err_create;
6968
6969 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6970 destroy_presentation_feedback);
6971
6972 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6973
6974 return;
6975
6976err_create:
6977 free(feedback);
6978
6979err_calloc:
6980 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006981}
6982
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006983static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006984 presentation_destroy,
6985 presentation_feedback
6986};
6987
6988static void
6989bind_presentation(struct wl_client *client,
6990 void *data, uint32_t version, uint32_t id)
6991{
6992 struct weston_compositor *compositor = data;
6993 struct wl_resource *resource;
6994
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006995 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006996 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006997 if (resource == NULL) {
6998 wl_client_post_no_memory(client);
6999 return;
7000 }
7001
7002 wl_resource_set_implementation(resource, &presentation_implementation,
7003 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007004 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007005}
7006
7007static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007008compositor_bind(struct wl_client *client,
7009 void *data, uint32_t version, uint32_t id)
7010{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007011 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007012 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007013
Jason Ekstranda85118c2013-06-27 20:17:02 -05007014 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007015 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007016 if (resource == NULL) {
7017 wl_client_post_no_memory(client);
7018 return;
7019 }
7020
7021 wl_resource_set_implementation(resource, &compositor_interface,
7022 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007023}
7024
Daniel Stonece62cb32018-07-20 09:46:24 +01007025static const char *
7026output_repaint_status_text(struct weston_output *output)
7027{
7028 switch (output->repaint_status) {
7029 case REPAINT_NOT_SCHEDULED:
7030 return "no repaint";
7031 case REPAINT_BEGIN_FROM_IDLE:
7032 return "start_repaint_loop scheduled";
7033 case REPAINT_SCHEDULED:
7034 return "repaint scheduled";
7035 case REPAINT_AWAITING_COMPLETION:
7036 return "awaiting completion";
7037 }
7038
7039 assert(!"output_repaint_status_text missing enum");
7040 return NULL;
7041}
7042
7043static void
7044debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7045{
7046 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7047 struct wl_shm_buffer *shm;
7048 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007049 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007050
7051 if (!buffer) {
7052 fprintf(fp, "\t\t[buffer not available]\n");
7053 return;
7054 }
7055
7056 shm = wl_shm_buffer_get(buffer->resource);
7057 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007058 uint32_t _format = wl_shm_buffer_get_format(shm);
7059 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007060 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007061 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7062 (unsigned long) _format,
7063 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007064 return;
7065 }
7066
7067 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7068 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007069 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007070 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007071 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7072 (unsigned long) dmabuf->attributes.format,
7073 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007074 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
7075 (unsigned long long) dmabuf->attributes.modifier[0]);
7076 return;
7077 }
7078
Marius Vlad253ba9a2019-03-17 18:22:21 +02007079 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007080}
7081
7082static void
7083debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7084{
7085 struct weston_compositor *ec = view->surface->compositor;
7086 struct weston_output *output;
7087 char desc[512];
7088 pixman_box32_t *box;
7089 uint32_t surface_id = 0;
7090 pid_t pid = 0;
7091
7092 if (view->surface->resource) {
7093 struct wl_resource *resource = view->surface->resource;
7094 wl_client_get_credentials(wl_resource_get_client(resource),
7095 &pid, NULL, NULL);
7096 surface_id = wl_resource_get_id(view->surface->resource);
7097 }
7098
7099 if (!view->surface->get_label ||
7100 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7101 strcpy(desc, "[no description available]");
7102 }
7103 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7104 view_idx, view->surface->role_name, pid, surface_id,
7105 desc, view);
7106
7107 box = pixman_region32_extents(&view->transform.boundingbox);
7108 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7109 box->x1, box->y1, box->x2, box->y2);
7110 box = pixman_region32_extents(&view->transform.opaque);
7111
7112 if (pixman_region32_equal(&view->transform.opaque,
7113 &view->transform.boundingbox)) {
7114 fprintf(fp, "\t\t[fully opaque]\n");
7115 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7116 fprintf(fp, "\t\t[not opaque]\n");
7117 } else {
7118 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7119 box->x1, box->y1, box->x2, box->y2);
7120 }
7121
7122 if (view->alpha < 1.0)
7123 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7124
7125 if (view->output_mask != 0) {
7126 bool first_output = true;
7127 fprintf(fp, "\t\toutputs: ");
7128 wl_list_for_each(output, &ec->output_list, link) {
7129 if (!(view->output_mask & (1 << output->id)))
7130 continue;
7131 fprintf(fp, "%s%d (%s)%s",
7132 (first_output) ? "" : ", ",
7133 output->id, output->name,
7134 (view->output == output) ? " (primary)" : "");
7135 first_output = false;
7136 }
7137 } else {
7138 fprintf(fp, "\t\t[no outputs]");
7139 }
7140
7141 fprintf(fp, "\n");
7142
7143 debug_scene_view_print_buffer(fp, view);
7144}
7145
Marius Vlad433f4e72019-02-17 22:14:23 +02007146static void
7147debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007148 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007149{
7150 struct weston_subsurface *sub;
7151 struct weston_view *ev;
7152
7153 /*
7154 * print the view first, then we recursively go on printing
7155 * sub-surfaces. We bail out once no more sub-surfaces are available.
7156 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007157 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007158
7159 /* no more sub-surfaces */
7160 if (wl_list_empty(&view->surface->subsurface_list))
7161 return;
7162
7163 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7164 wl_list_for_each(ev, &sub->surface->views, surface_link) {
7165 /* do not print again the parent view */
7166 if (view == ev)
7167 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007168
7169 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007170 debug_scene_view_print_tree(ev, fp, view_idx);
7171 }
7172 }
7173}
7174
Daniel Stonece62cb32018-07-20 09:46:24 +01007175/**
7176 * Output information on how libweston is currently composing the scene
7177 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007178 *
7179 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007180 */
7181WL_EXPORT char *
7182weston_compositor_print_scene_graph(struct weston_compositor *ec)
7183{
7184 struct weston_output *output;
7185 struct weston_layer *layer;
7186 struct timespec now;
7187 int layer_idx = 0;
7188 FILE *fp;
7189 char *ret;
7190 size_t len;
7191 int err;
7192
7193 fp = open_memstream(&ret, &len);
7194 assert(fp);
7195
7196 weston_compositor_read_presentation_clock(ec, &now);
7197 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7198 now.tv_sec, now.tv_nsec);
7199
7200 wl_list_for_each(output, &ec->output_list, link) {
7201 struct weston_head *head;
7202 int head_idx = 0;
7203
7204 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7205 assert(output->enabled);
7206
7207 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7208 output->x, output->y,
7209 output->x + output->width,
7210 output->y + output->height);
7211 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7212 output->current_mode->width,
7213 output->current_mode->height,
7214 output->current_mode->refresh / 1000.0);
7215 fprintf(fp, "\tscale: %d\n", output->scale);
7216
7217 fprintf(fp, "\trepaint status: %s\n",
7218 output_repaint_status_text(output));
7219 if (output->repaint_status == REPAINT_SCHEDULED)
7220 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7221 output->next_repaint.tv_sec,
7222 output->next_repaint.tv_nsec);
7223
7224 wl_list_for_each(head, &output->head_list, output_link) {
7225 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7226 head_idx++, head->name,
7227 (head->connected) ? "" : "not ");
7228 }
7229 }
7230
7231 fprintf(fp, "\n");
7232
7233 wl_list_for_each(layer, &ec->layer_list, link) {
7234 struct weston_view *view;
7235 int view_idx = 0;
7236
7237 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7238 (unsigned long) layer->position);
7239
7240 if (!weston_layer_mask_is_infinite(layer)) {
7241 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7242 layer->mask.x1, layer->mask.y1,
7243 layer->mask.x2, layer->mask.y2);
7244 }
7245
Marius Vlada6acfa82019-03-17 18:10:09 +02007246 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7247 debug_scene_view_print_tree(view, fp, &view_idx);
7248 view_idx++;
7249 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007250
7251 if (wl_list_empty(&layer->view_list.link))
7252 fprintf(fp, "\t[no views]\n");
7253
7254 fprintf(fp, "\n");
7255 }
7256
7257 err = fclose(fp);
7258 assert(err == 0);
7259
7260 return ret;
7261}
7262
7263/**
7264 * Called when the 'scene-graph' debug scope is bound by a client. This
7265 * one-shot weston-debug scope prints the current scene graph when bound,
7266 * and then terminates the stream.
7267 */
7268static void
Marius Vladdad882a2019-07-17 15:43:53 +03007269debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007270{
7271 struct weston_compositor *ec = data;
7272 char *str = weston_compositor_print_scene_graph(ec);
7273
Marius Vladdad882a2019-07-17 15:43:53 +03007274 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007275 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007276 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007277}
7278
Giulio Camuffo459137b2014-10-11 23:56:24 +03007279/** Create the compositor.
7280 *
7281 * This functions creates and initializes a compositor instance.
7282 *
7283 * \param display The Wayland display to be used.
7284 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007285 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007286 * using the \ref weston_compositor_get_user_data function.
7287 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007288 *
7289 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007290 */
7291WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007292weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007293 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007294 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007295{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007296 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007297 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007298
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007299 if (!log_ctx)
7300 return NULL;
7301
Giulio Camuffo459137b2014-10-11 23:56:24 +03007302 ec = zalloc(sizeof *ec);
7303 if (!ec)
7304 return NULL;
7305
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007306 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007307 ec->wl_display = display;
7308 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007309 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007310 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007311 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007312 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007313 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007314 wl_signal_init(&ec->idle_signal);
7315 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007316 wl_signal_init(&ec->show_input_panel_signal);
7317 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007318 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007319 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007320 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007321 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007322 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007323 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007324 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007325 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007326 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007327 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007328
Casey Dahlin58ba1372012-04-19 22:50:08 -04007329 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007330 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007331
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007332 ec->activate_serial = 1;
7333
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007334 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7335
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307336 ec->content_protection = NULL;
7337
Derek Foreman152254b2015-11-26 14:17:48 -06007338 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007339 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007340 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007341
Giulio Camuffo954f1832014-10-11 18:27:30 +03007342 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007343 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007344 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007345
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007346 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007347 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007348 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007349
Roman Gilge97391c2019-03-29 13:01:06 +01007350 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7351 ec, bind_xdg_output_manager))
7352 goto fail;
7353
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007354 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007355 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007356 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007357
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007358 if (weston_input_init(ec) != 0)
7359 goto fail;
7360
Jason Ekstranda7af7042013-10-12 22:38:11 -05007361 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007362 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007363 wl_list_init(&ec->layer_list);
7364 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007365 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007366 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007367 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007368 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007369 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007370 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007371 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007372 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007373 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007374
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007375 wl_list_init(&ec->plugin_api_list);
7376
Xiong Zhang97116532013-10-23 13:58:31 +08007377 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007378 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007379
Giulio Camuffo459137b2014-10-11 23:56:24 +03007380 wl_data_device_manager_init(ec->wl_display);
7381
7382 wl_display_init_shm(ec->wl_display);
7383
7384 loop = wl_display_get_event_loop(ec->wl_display);
7385 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007386 ec->repaint_timer =
7387 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7388 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007389
Quentin Glidic82681572016-12-17 13:40:51 +01007390 weston_layer_init(&ec->fade_layer, ec);
7391 weston_layer_init(&ec->cursor_layer, ec);
7392
7393 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7394 weston_layer_set_position(&ec->cursor_layer,
7395 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007396
Daniel Stonece62cb32018-07-20 09:46:24 +01007397 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007398 weston_compositor_add_log_scope(ec, "scene-graph",
7399 "Scene graph details\n",
7400 debug_scene_graph_cb, NULL,
7401 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007402
Marius Vladda104eb2019-09-05 14:31:01 +03007403 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007404 weston_compositor_add_log_scope(ec, "timeline",
7405 "Timeline event points\n",
7406 weston_timeline_create_subscription,
7407 weston_timeline_destroy_subscription,
7408 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007409 return ec;
7410
7411fail:
7412 free(ec);
7413 return NULL;
7414}
7415
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007416/** weston_compositor_shutdown
7417 * \ingroup compositor
7418 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007419WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007420weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007421{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007422 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007423
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007424 wl_event_source_remove(ec->idle_source);
7425
Matt Roper361d2ad2011-08-29 13:52:23 -07007426 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007427 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007428 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007429
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007430 /* Destroy all pending outputs associated with this compositor */
7431 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7432 output->destroy(output);
7433
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007434 if (ec->renderer)
7435 ec->renderer->destroy(ec);
7436
Daniel Stone325fc2d2012-05-30 16:31:58 +01007437 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007438 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007439 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007440 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007441 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007442 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007443
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007444 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007445}
7446
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007447/** weston_compositor_exit_with_code
7448 * \ingroup compositor
7449 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007450WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007451weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007452 int exit_code)
7453{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007454 if (compositor->exit_code == EXIT_SUCCESS)
7455 compositor->exit_code = exit_code;
7456
Giulio Camuffo459137b2014-10-11 23:56:24 +03007457 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007458}
7459
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007460/** weston_compositor_set_default_pointer_grab
7461 * \ingroup compositor
7462 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007463WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007464weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7465 const struct weston_pointer_grab_interface *interface)
7466{
7467 struct weston_seat *seat;
7468
7469 ec->default_pointer_grab = interface;
7470 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007471 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7472
7473 if (pointer)
7474 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007475 }
7476}
7477
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007478/** weston_compositor_set_presentation_clock
7479 * \ingroup compositor
7480 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007481WL_EXPORT int
7482weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7483 clockid_t clk_id)
7484{
7485 struct timespec ts;
7486
7487 if (clock_gettime(clk_id, &ts) < 0)
7488 return -1;
7489
7490 compositor->presentation_clock = clk_id;
7491
7492 return 0;
7493}
7494
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007495/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007496 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007497 *
7498 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007499 */
7500WL_EXPORT int
7501weston_compositor_set_presentation_clock_software(
7502 struct weston_compositor *compositor)
7503{
7504 /* In order of preference */
7505 static const clockid_t clocks[] = {
7506 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7507 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7508 CLOCK_MONOTONIC, /* no jumps, may crawl */
7509 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7510 CLOCK_REALTIME /* may jump and crawl */
7511 };
7512 unsigned i;
7513
7514 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7515 if (weston_compositor_set_presentation_clock(compositor,
7516 clocks[i]) == 0)
7517 return 0;
7518
7519 weston_log("Error: no suitable presentation clock available.\n");
7520
7521 return -1;
7522}
7523
Pekka Paalanen662f3842015-03-18 12:17:26 +02007524/** Read the current time from the Presentation clock
7525 *
7526 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007527 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007528 *
7529 * \note Reading the current time in user space is always imprecise to some
7530 * degree.
7531 *
7532 * This function is never meant to fail. If reading the clock does fail,
7533 * an error message is logged and a zero time is returned. Callers are not
7534 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007535 *
7536 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007537 */
7538WL_EXPORT void
7539weston_compositor_read_presentation_clock(
7540 const struct weston_compositor *compositor,
7541 struct timespec *ts)
7542{
7543 static bool warned;
7544 int ret;
7545
7546 ret = clock_gettime(compositor->presentation_clock, ts);
7547 if (ret < 0) {
7548 ts->tv_sec = 0;
7549 ts->tv_nsec = 0;
7550
7551 if (!warned)
7552 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007553 "the presentation clock %#x: '%s' (%d)\n",
7554 compositor->presentation_clock,
7555 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007556 warned = true;
7557 }
7558}
7559
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007560/** Import dmabuf buffer into current renderer
7561 *
7562 * \param compositor
7563 * \param buffer the dmabuf buffer to import
7564 * \return true on usable buffers, false otherwise
7565 *
7566 * This function tests that the linux_dmabuf_buffer is usable
7567 * for the current renderer. Returns false on unusable buffers. Usually
7568 * usability is tested by importing the dmabufs for composition.
7569 *
7570 * This hook is also used for detecting if the renderer supports
7571 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7572 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007573 *
7574 * \ingroup compositor
7575 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007576WL_EXPORT bool
7577weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7578 struct linux_dmabuf_buffer *buffer)
7579{
7580 struct weston_renderer *renderer;
7581
7582 renderer = compositor->renderer;
7583
7584 if (renderer->import_dmabuf == NULL)
7585 return false;
7586
7587 return renderer->import_dmabuf(compositor, buffer);
7588}
7589
Marius Vlad5a701542019-11-16 20:26:52 +02007590WL_EXPORT bool
7591weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
7592 struct linux_dmabuf_buffer *buffer)
7593{
7594 struct weston_backend *backend = compositor->backend;
7595
7596 if (backend->can_scanout_dmabuf == NULL)
7597 return false;
7598
7599 return backend->can_scanout_dmabuf(compositor, buffer);
7600}
7601
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007602WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007603weston_version(int *major, int *minor, int *micro)
7604{
7605 *major = WESTON_VERSION_MAJOR;
7606 *minor = WESTON_VERSION_MINOR;
7607 *micro = WESTON_VERSION_MICRO;
7608}
7609
Daniel Stonee03c1112016-11-24 20:45:45 +00007610/**
7611 * Attempts to find a module path from the module map specified in the
7612 * environment. If found, writes the full path into the path variable.
7613 *
7614 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7615 * each entry is of the form "name=path" and entries are separated by
7616 * semicolons. Whitespace is significant.
7617 *
7618 * \param name The name to search for.
7619 * \param path Where the path is written to if found.
7620 * \param path_len Allocated bytes at \c path .
7621 * \returns The length of the string written to path on success, or 0 if the
7622 * module was not specified in the environment map or path_len was too small.
7623 */
7624WL_EXPORT size_t
7625weston_module_path_from_env(const char *name, char *path, size_t path_len)
7626{
7627 const char *mapping = getenv("WESTON_MODULE_MAP");
7628 const char *end;
7629 const int name_len = strlen(name);
7630
7631 if (!mapping)
7632 return 0;
7633
7634 end = mapping + strlen(mapping);
7635 while (mapping < end && *mapping) {
7636 const char *filename, *next;
7637
7638 /* early out: impossibly short string */
7639 if (end - mapping < name_len + 1)
7640 return 0;
7641
7642 filename = &mapping[name_len + 1];
7643 next = strchrnul(mapping, ';');
7644
7645 if (strncmp(mapping, name, name_len) == 0 &&
7646 mapping[name_len] == '=') {
7647 size_t file_len = next - filename; /* no trailing NUL */
7648 if (file_len >= path_len)
7649 return 0;
7650 strncpy(path, filename, file_len);
7651 path[file_len] = '\0';
7652 return file_len;
7653 }
7654
7655 mapping = next + 1;
7656 }
7657
7658 return 0;
7659}
7660
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007661WL_EXPORT void *
7662weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007663{
7664 char path[PATH_MAX];
7665 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007666 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007667
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007668 if (name == NULL)
7669 return NULL;
7670
Derek Foreman3f86e502015-06-08 11:46:54 -05007671 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007672 len = weston_module_path_from_env(name, path, sizeof path);
7673 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007674 len = snprintf(path, sizeof path, "%s/%s",
7675 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007676 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007677 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007678 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007679
Daniel Stonebeb97e52016-11-28 12:13:54 +00007680 /* snprintf returns the length of the string it would've written,
7681 * _excluding_ the NUL byte. So even being equal to the size of
7682 * our buffer is an error here. */
7683 if (len >= sizeof path)
7684 return NULL;
7685
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007686 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7687 if (module) {
7688 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02007689 } else {
7690 weston_log("Loading module '%s'\n", path);
7691 module = dlopen(path, RTLD_NOW);
7692 if (!module) {
7693 weston_log("Failed to load module: %s\n", dlerror());
7694 return NULL;
7695 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007696 }
7697
7698 init = dlsym(module, entrypoint);
7699 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007700 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007701 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007702 return NULL;
7703 }
7704
7705 return init;
7706}
7707
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007708/** Add a compositor destroy listener only once
7709 *
7710 * \param compositor The compositor whose destroy to watch for.
7711 * \param listener The listener struct to initialize.
7712 * \param destroy_handler The callback when compositor is destroyed.
7713 * \return True if listener is added, or false if there already is a listener
7714 * with the given \c destroy_handler.
7715 *
7716 * This function does nothing and returns false if the given callback function
7717 * is already present in the weston_compositor destroy callbacks list.
7718 * Otherwise, this function initializes the given listener with the given
7719 * callback pointer and adds it to the compositor's destroy callbacks list.
7720 *
7721 * This can be used to ensure that plugin initialization is done only once
7722 * in case the same plugin is loaded multiple times. If this function returns
7723 * false, the plugin should be already initialized successfully.
7724 *
7725 * All plugins should register a destroy listener for cleaning up. Note, that
7726 * the plugin destruction order is not guaranteed: plugins that depend on other
7727 * plugins must be able to be torn down in arbitrary order.
7728 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007729 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007730 */
7731WL_EXPORT bool
7732weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
7733 struct wl_listener *listener,
7734 wl_notify_func_t destroy_handler)
7735{
7736 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
7737 return false;
7738
7739 listener->notify = destroy_handler;
7740 wl_signal_add(&compositor->destroy_signal, listener);
7741 return true;
7742}
Giulio Camuffo459137b2014-10-11 23:56:24 +03007743
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007744/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007745 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007746 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007747 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007748 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007749 *
Marius Vlad284d5342019-06-24 12:00:47 +03007750 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007751 */
7752WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007753weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03007754{
7755 /* prevent further rendering while shutting down */
7756 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7757
7758 wl_signal_emit(&compositor->destroy_signal, compositor);
7759
7760 weston_compositor_xkb_destroy(compositor);
7761
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007762 if (compositor->backend)
7763 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007764
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007765 /* The backend is responsible for destroying the heads. */
7766 assert(wl_list_empty(&compositor->head_list));
7767
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007768 weston_plugin_api_destroy_list(compositor);
7769
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007770 if (compositor->heads_changed_source)
7771 wl_event_source_remove(compositor->heads_changed_source);
7772
Leandro Ribeirof0149642019-12-18 15:52:18 -03007773 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007774 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03007775
Leandro Ribeirof0149642019-12-18 15:52:18 -03007776 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03007777 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007778
Giulio Camuffo459137b2014-10-11 23:56:24 +03007779 free(compositor);
7780}
7781
7782/** Instruct the compositor to exit.
7783 *
7784 * This functions does not directly destroy the compositor object, it merely
7785 * command it to start the tear down process. It is not guaranteed that the
7786 * tear down will happen immediately.
7787 *
7788 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007789 *
7790 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007791 */
7792WL_EXPORT void
7793weston_compositor_exit(struct weston_compositor *compositor)
7794{
7795 compositor->exit(compositor);
7796}
7797
7798/** Return the user data stored in the compositor.
7799 *
7800 * This function returns the user data pointer set with user_data parameter
7801 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007802 *
7803 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007804 */
7805WL_EXPORT void *
7806weston_compositor_get_user_data(struct weston_compositor *compositor)
7807{
7808 return compositor->user_data;
7809}
Pekka Paalanendd186732016-06-03 14:49:54 +03007810
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007811static const char * const backend_map[] = {
7812 [WESTON_BACKEND_DRM] = "drm-backend.so",
7813 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7814 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7815 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7816 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7817 [WESTON_BACKEND_X11] = "x11-backend.so",
7818};
7819
Pekka Paalanendd186732016-06-03 14:49:54 +03007820/** Load a backend into a weston_compositor
7821 *
7822 * A backend must be loaded to make a weston_compositor work. A backend
7823 * provides input and output capabilities, and determines the renderer to use.
7824 *
7825 * \param compositor A compositor that has not had a backend loaded yet.
7826 * \param backend Name of the backend file.
7827 * \param config_base A pointer to a backend-specific configuration
7828 * structure's 'base' member.
7829 *
7830 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007831 *
7832 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007833 */
7834WL_EXPORT int
7835weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007836 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007837 struct weston_backend_config *config_base)
7838{
7839 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007840 struct weston_backend_config *config_base);
7841
Pekka Paalanend7e35112017-08-29 17:04:12 +03007842 if (compositor->backend) {
7843 weston_log("Error: attempt to load a backend when one is already loaded\n");
7844 return -1;
7845 }
7846
Quentin Glidic887c0182016-07-10 11:00:53 +02007847 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007848 return -1;
7849
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007850 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007851 if (!backend_init)
7852 return -1;
7853
Pekka Paalanend7e35112017-08-29 17:04:12 +03007854 if (backend_init(compositor, config_base) < 0) {
7855 compositor->backend = NULL;
7856 return -1;
7857 }
7858
7859 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007860}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007861
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007862/** weston_compositor_load_xwayland
7863 * \ingroup compositor
7864 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007865WL_EXPORT int
7866weston_compositor_load_xwayland(struct weston_compositor *compositor)
7867{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007868 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007869
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007870 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007871 if (!module_init)
7872 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007873 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007874 return -1;
7875 return 0;
7876}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007877
7878/** Resolve an internal compositor error by disconnecting the client.
7879 *
7880 * This function is used in cases when the wl_buffer turns out
7881 * unusable and there is no fallback path.
7882 *
7883 * It is possible the fault is caused by a compositor bug, the underlying
7884 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7885 * In any case, the options are to either composite garbage or nothing,
7886 * or disconnect the client. This is a helper function for the latter.
7887 *
7888 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7889 *
7890 * \param buffer The weston buffer that is unusable.
7891 * \param msg A custom error message attached to the protocol error.
7892 */
7893WL_EXPORT void
7894weston_buffer_send_server_error(struct weston_buffer *buffer,
7895 const char *msg)
7896{
7897 struct wl_client *client;
7898 struct wl_resource *display_resource;
7899 uint32_t id;
7900
7901 assert(buffer->resource);
7902 id = wl_resource_get_id(buffer->resource);
7903 client = wl_resource_get_client(buffer->resource);
7904 display_resource = wl_client_get_object(client, 1);
7905
7906 assert(display_resource);
7907 wl_resource_post_error(display_resource,
7908 WL_DISPLAY_ERROR_INVALID_OBJECT,
7909 "server error with "
7910 "wl_buffer@%u: %s", id, msg);
7911}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307912
7913WL_EXPORT void
7914weston_output_disable_planes_incr(struct weston_output *output)
7915{
7916 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05307917 /*
7918 * If disable_planes changes from 0 to non-zero, it means some type of
7919 * recording of content has started, and therefore protection level of
7920 * the protected surfaces must be updated to avoid the recording of
7921 * the protected content.
7922 */
7923 if (output->disable_planes == 1)
7924 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307925}
7926
7927WL_EXPORT void
7928weston_output_disable_planes_decr(struct weston_output *output)
7929{
7930 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05307931 /*
7932 * If disable_planes changes from non-zero to 0, it means no content
7933 * recording is going on any more, and the protected and surfaces can be
7934 * shown without any apprehensions about content being recorded.
7935 */
7936 if (output->disable_planes == 0)
7937 weston_schedule_surface_protection_update(output->compositor);
7938
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307939}