blob: a99deedaffa1e6565a947ffd0b82fae5c5319654 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanen925788f2018-04-19 14:20:01 +03004 * Copyright © 2012-2018 Collabora, Ltd.
5 * Copyright © 2017, 2018 General Electric Company
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05006 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07007 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050014 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070015 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050027 */
28
Kristian Høgsberga9410222011-01-14 17:22:35 -050029#include "config.h"
30
Daniel Stoneb7452fe2012-06-01 12:14:06 +010031#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040032#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010036#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050037#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020038#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040039#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010040#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040041#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020042#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020043#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020044#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040045#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050046#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040047#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040048#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040049#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050050#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040051#include <sys/time.h>
52#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030053#include <errno.h>
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030054#include <inttypes.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050055
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020058#include <libweston/libweston.h>
Marius Vladf4f4c2b2019-04-29 13:27:47 +030059#include <libweston/weston-debug.h>
Daniel Stonece62cb32018-07-20 09:46:24 +010060#include "linux-dmabuf.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030061#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020062#include "presentation-time-server-protocol.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030063#include "linux-explicit-synchronization-unstable-v1-server-protocol.h"
Alexandros Frantzis67629672018-10-19 12:14:11 +030064#include "linux-explicit-synchronization.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030065#include "shared/fd-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070066#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070067#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070068#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030069#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040070#include "git-version.h"
Pekka Paalanencda14882019-04-04 15:41:02 +030071#include <libweston/version.h>
Pekka Paalanen27b377f2019-03-29 17:07:34 +020072#include <libweston/plugin-registry.h>
Marius Vlad00a6e012018-11-20 17:52:31 +020073#include "pixel-formats.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050074
Marius Vlad78984ee2019-06-11 00:05:08 +030075/**
76 * \defgroup head Head
77 * \defgroup output Output
78 * \defgroup compositor Compositor
79 */
80
Pekka Paalanen0513a952014-05-21 16:17:27 +030081#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
82
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020083static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030084weston_output_update_matrix(struct weston_output *output);
85
86static void
Alexander Larsson0b135062013-05-28 16:23:36 +020087weston_output_transform_scale_init(struct weston_output *output,
88 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020089
Rob Bradford27b17932013-06-26 18:08:46 +010090static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050091weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010092
Pekka Paalanendcac3512017-12-08 14:13:34 +020093static char *
94weston_output_create_heads_string(struct weston_output *output);
95
Pekka Paalanen6528c032017-03-28 15:27:10 +030096/** Send wl_output events for mode and scale changes
97 *
98 * \param head Send on all resources bound to this head.
99 * \param mode_changed If true, send the current mode.
100 * \param scale_changed If true, send the current scale.
101 */
102static void
103weston_mode_switch_send_events(struct weston_head *head,
104 bool mode_changed, bool scale_changed)
105{
106 struct weston_output *output = head->output;
107 struct wl_resource *resource;
108 int version;
109
110 wl_resource_for_each(resource, &head->resource_list) {
111 if (mode_changed) {
112 wl_output_send_mode(resource,
113 output->current_mode->flags,
114 output->current_mode->width,
115 output->current_mode->height,
116 output->current_mode->refresh);
117 }
118
119 version = wl_resource_get_version(resource);
120 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
121 wl_output_send_scale(resource, output->current_scale);
122
123 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
124 wl_output_send_done(resource);
125 }
126}
127
128static void
129weston_mode_switch_finish(struct weston_output *output,
130 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800131{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200132 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300133 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200134 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200135
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200136 pixman_region32_init(&old_output_region);
137 pixman_region32_copy(&old_output_region, &output->region);
138
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200139 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200140 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200141
142 pixman_region32_init(&output->previous_damage);
143 pixman_region32_init_rect(&output->region, output->x, output->y,
144 output->width, output->height);
145
146 weston_output_update_matrix(output);
147
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200148 /* If a pointer falls outside the outputs new geometry, move it to its
149 * lower-right corner */
150 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500151 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200152 int32_t x, y;
153
154 if (!pointer)
155 continue;
156
157 x = wl_fixed_to_int(pointer->x);
158 y = wl_fixed_to_int(pointer->y);
159
160 if (!pixman_region32_contains_point(&old_output_region,
161 x, y, NULL) ||
162 pixman_region32_contains_point(&output->region,
163 x, y, NULL))
164 continue;
165
166 if (x >= output->x + output->width)
167 x = output->x + output->width - 1;
168 if (y >= output->y + output->height)
169 y = output->y + output->height - 1;
170
171 pointer->x = wl_fixed_from_int(x);
172 pointer->y = wl_fixed_from_int(y);
173 }
174
175 pixman_region32_fini(&old_output_region);
176
Derek Foremandd4cd332014-11-10 10:29:59 -0600177 if (!mode_changed && !scale_changed)
178 return;
179
Hardening57388e42013-09-18 23:56:36 +0200180 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300181 wl_list_for_each(head, &output->head_list, output_link)
182 weston_mode_switch_send_events(head,
183 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600184}
185
David Fort0de859e2016-05-27 23:22:57 +0200186static void
187weston_compositor_reflow_outputs(struct weston_compositor *compositor,
188 struct weston_output *resized_output, int delta_width);
189
Marius Vlad55d87362019-06-11 01:15:35 +0300190/**
191 * \ingroup output
192 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600193WL_EXPORT int
194weston_output_mode_set_native(struct weston_output *output,
195 struct weston_mode *mode,
196 int32_t scale)
197{
198 int ret;
199 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200200 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600201
202 if (!output->switch_mode)
203 return -1;
204
205 if (!output->original_mode) {
206 mode_changed = 1;
207 ret = output->switch_mode(output, mode);
208 if (ret < 0)
209 return ret;
210 if (output->current_scale != scale) {
211 scale_changed = 1;
212 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200213 }
214 }
215
David Fort0de859e2016-05-27 23:22:57 +0200216 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600217 output->native_mode = mode;
218 output->native_scale = scale;
219
220 weston_mode_switch_finish(output, mode_changed, scale_changed);
221
David Fort0de859e2016-05-27 23:22:57 +0200222 if (mode_changed || scale_changed) {
223 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
224
225 wl_signal_emit(&output->compositor->output_resized_signal, output);
226 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600227 return 0;
228}
229
Marius Vlad55d87362019-06-11 01:15:35 +0300230/**
231 * \ingroup output
232 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600233WL_EXPORT int
234weston_output_mode_switch_to_native(struct weston_output *output)
235{
236 int ret;
237 int mode_changed = 0, scale_changed = 0;
238
239 if (!output->switch_mode)
240 return -1;
241
242 if (!output->original_mode) {
243 weston_log("already in the native mode\n");
244 return -1;
245 }
246 /* the non fullscreen clients haven't seen a mode set since we
247 * switched into a temporary, so we need to notify them if the
248 * mode at that time is different from the native mode now.
249 */
250 mode_changed = (output->original_mode != output->native_mode);
251 scale_changed = (output->original_scale != output->native_scale);
252
253 ret = output->switch_mode(output, output->native_mode);
254 if (ret < 0)
255 return ret;
256
257 output->current_scale = output->native_scale;
258
259 output->original_mode = NULL;
260 output->original_scale = 0;
261
262 weston_mode_switch_finish(output, mode_changed, scale_changed);
263
264 return 0;
265}
266
Marius Vlad55d87362019-06-11 01:15:35 +0300267/**
268 * \ingroup output
269 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600270WL_EXPORT int
271weston_output_mode_switch_to_temporary(struct weston_output *output,
272 struct weston_mode *mode,
273 int32_t scale)
274{
275 int ret;
276
277 if (!output->switch_mode)
278 return -1;
279
280 /* original_mode is the last mode non full screen clients have seen,
281 * so we shouldn't change it if we already have one set.
282 */
283 if (!output->original_mode) {
284 output->original_mode = output->native_mode;
285 output->original_scale = output->native_scale;
286 }
287 ret = output->switch_mode(output, mode);
288 if (ret < 0)
289 return ret;
290
291 output->current_scale = scale;
292
293 weston_mode_switch_finish(output, 0, 0);
294
295 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800296}
297
Benjamin Franzke06286262011-05-06 19:12:33 +0200298static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300299region_init_infinite(pixman_region32_t *region)
300{
301 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
302 UINT32_MAX, UINT32_MAX);
303}
304
Pekka Paalanene67858b2013-04-25 13:57:42 +0300305static struct weston_subsurface *
306weston_surface_to_subsurface(struct weston_surface *surface);
307
Jason Ekstranda7af7042013-10-12 22:38:11 -0500308WL_EXPORT struct weston_view *
309weston_view_create(struct weston_surface *surface)
310{
311 struct weston_view *view;
312
Bryce Harringtonde16d892014-11-20 22:21:57 -0800313 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500314 if (view == NULL)
315 return NULL;
316
317 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000318 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500319
Jason Ekstranda7af7042013-10-12 22:38:11 -0500320 /* Assign to surface */
321 wl_list_insert(&surface->views, &view->surface_link);
322
323 wl_signal_init(&view->destroy_signal);
324 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300325 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500326
Jason Ekstranda7af7042013-10-12 22:38:11 -0500327 pixman_region32_init(&view->clip);
328
329 view->alpha = 1.0;
330 pixman_region32_init(&view->transform.opaque);
331
332 wl_list_init(&view->geometry.transformation_list);
333 wl_list_insert(&view->geometry.transformation_list,
334 &view->transform.position.link);
335 weston_matrix_init(&view->transform.position.matrix);
336 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200337 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500338 pixman_region32_init(&view->transform.boundingbox);
339 view->transform.dirty = 1;
340
Jason Ekstranda7af7042013-10-12 22:38:11 -0500341 return view;
342}
343
Jason Ekstrand108865d2014-06-26 10:04:49 -0700344struct weston_frame_callback {
345 struct wl_resource *resource;
346 struct wl_list link;
347};
348
Pekka Paalanen133e4392014-09-23 22:08:46 -0400349struct weston_presentation_feedback {
350 struct wl_resource *resource;
351
352 /* XXX: could use just wl_resource_get_link() instead */
353 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200354
355 /* The per-surface feedback flags */
356 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400357};
358
359static void
360weston_presentation_feedback_discard(
361 struct weston_presentation_feedback *feedback)
362{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200363 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400364 wl_resource_destroy(feedback->resource);
365}
366
367static void
368weston_presentation_feedback_discard_list(struct wl_list *list)
369{
370 struct weston_presentation_feedback *feedback, *tmp;
371
372 wl_list_for_each_safe(feedback, tmp, list, link)
373 weston_presentation_feedback_discard(feedback);
374}
375
376static void
377weston_presentation_feedback_present(
378 struct weston_presentation_feedback *feedback,
379 struct weston_output *output,
380 uint32_t refresh_nsec,
381 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200382 uint64_t seq,
383 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400384{
385 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300386 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400387 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200388 uint32_t tv_sec_hi;
389 uint32_t tv_sec_lo;
390 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300391 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400392
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300393 wl_list_for_each(head, &output->head_list, output_link) {
394 wl_resource_for_each(o, &head->resource_list) {
395 if (wl_resource_get_client(o) != client)
396 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400397
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300398 wp_presentation_feedback_send_sync_output(feedback->resource, o);
399 done = true;
400 }
401
402 /* For clone mode, send it for just one wl_output global,
403 * they are all equivalent anyway.
404 */
405 if (done)
406 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400407 }
408
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200409 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200410 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200411 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200412 refresh_nsec,
413 seq >> 32, seq & 0xffffffff,
414 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400415 wl_resource_destroy(feedback->resource);
416}
417
418static void
419weston_presentation_feedback_present_list(struct wl_list *list,
420 struct weston_output *output,
421 uint32_t refresh_nsec,
422 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200423 uint64_t seq,
424 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400425{
426 struct weston_presentation_feedback *feedback, *tmp;
427
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200428 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200429 wl_list_empty(list));
430
Pekka Paalanen133e4392014-09-23 22:08:46 -0400431 wl_list_for_each_safe(feedback, tmp, list, link)
432 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200433 refresh_nsec, ts, seq,
434 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400435}
436
Jason Ekstrand7b982072014-05-20 14:33:03 -0500437static void
438surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
439{
440 struct weston_surface_state *state =
441 container_of(listener, struct weston_surface_state,
442 buffer_destroy_listener);
443
444 state->buffer = NULL;
445}
446
447static void
448weston_surface_state_init(struct weston_surface_state *state)
449{
450 state->newly_attached = 0;
451 state->buffer = NULL;
452 state->buffer_destroy_listener.notify =
453 surface_state_handle_buffer_destroy;
454 state->sx = 0;
455 state->sy = 0;
456
Derek Foreman152254b2015-11-26 14:17:48 -0600457 pixman_region32_init(&state->damage_surface);
458 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500459 pixman_region32_init(&state->opaque);
460 region_init_infinite(&state->input);
461
462 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400463 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500464
465 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
466 state->buffer_viewport.buffer.scale = 1;
467 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
468 state->buffer_viewport.surface.width = -1;
469 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300470
471 state->acquire_fence_fd = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500472}
473
474static void
475weston_surface_state_fini(struct weston_surface_state *state)
476{
477 struct weston_frame_callback *cb, *next;
478
479 wl_list_for_each_safe(cb, next,
480 &state->frame_callback_list, link)
481 wl_resource_destroy(cb->resource);
482
Pekka Paalanen133e4392014-09-23 22:08:46 -0400483 weston_presentation_feedback_discard_list(&state->feedback_list);
484
Jason Ekstrand7b982072014-05-20 14:33:03 -0500485 pixman_region32_fini(&state->input);
486 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600487 pixman_region32_fini(&state->damage_surface);
488 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500489
490 if (state->buffer)
491 wl_list_remove(&state->buffer_destroy_listener.link);
492 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300493
494 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300495 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500496}
497
498static void
499weston_surface_state_set_buffer(struct weston_surface_state *state,
500 struct weston_buffer *buffer)
501{
502 if (state->buffer == buffer)
503 return;
504
505 if (state->buffer)
506 wl_list_remove(&state->buffer_destroy_listener.link);
507 state->buffer = buffer;
508 if (state->buffer)
509 wl_signal_add(&state->buffer->destroy_signal,
510 &state->buffer_destroy_listener);
511}
512
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500513WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500514weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500515{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500516 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400517
Bryce Harringtonde16d892014-11-20 22:21:57 -0800518 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400519 if (surface == NULL)
520 return NULL;
521
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500522 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800523 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500524
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500525 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200526 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400527
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200528 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
529 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200530 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
531 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500532
533 weston_surface_state_init(&surface->pending);
534
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400535 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500536 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300537 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400538
Jason Ekstranda7af7042013-10-12 22:38:11 -0500539 wl_list_init(&surface->views);
540
541 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400542 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500543
Pekka Paalanene67858b2013-04-25 13:57:42 +0300544 wl_list_init(&surface->subsurface_list);
545 wl_list_init(&surface->subsurface_list_pending);
546
Jason Ekstrand1e059042014-10-16 10:55:19 -0500547 weston_matrix_init(&surface->buffer_to_surface_matrix);
548 weston_matrix_init(&surface->surface_to_buffer_matrix);
549
Jonas Ådahld3414f22016-07-22 17:56:31 +0800550 wl_list_init(&surface->pointer_constraints);
551
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300552 surface->acquire_fence_fd = -1;
553
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400554 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500555}
556
Alex Wu8811bf92012-02-28 18:07:54 +0800557WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500558weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200559 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500560{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100561 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200562 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500563}
564
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400565WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500566weston_view_to_global_float(struct weston_view *view,
567 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200568{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500569 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200570 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
571
Jason Ekstranda7af7042013-10-12 22:38:11 -0500572 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200573
574 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200575 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700576 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200577 v.f[3]);
578 *x = 0;
579 *y = 0;
580 return;
581 }
582
583 *x = v.f[0] / v.f[3];
584 *y = v.f[1] / v.f[3];
585 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586 *x = sx + view->geometry.x;
587 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200588 }
589}
590
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500591WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200592weston_transformed_coord(int width, int height,
593 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200594 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200595 float sx, float sy, float *bx, float *by)
596{
597 switch (transform) {
598 case WL_OUTPUT_TRANSFORM_NORMAL:
599 default:
600 *bx = sx;
601 *by = sy;
602 break;
603 case WL_OUTPUT_TRANSFORM_FLIPPED:
604 *bx = width - sx;
605 *by = sy;
606 break;
607 case WL_OUTPUT_TRANSFORM_90:
608 *bx = height - sy;
609 *by = sx;
610 break;
611 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
612 *bx = height - sy;
613 *by = width - sx;
614 break;
615 case WL_OUTPUT_TRANSFORM_180:
616 *bx = width - sx;
617 *by = height - sy;
618 break;
619 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
620 *bx = sx;
621 *by = height - sy;
622 break;
623 case WL_OUTPUT_TRANSFORM_270:
624 *bx = sy;
625 *by = width - sx;
626 break;
627 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
628 *bx = sy;
629 *by = sx;
630 break;
631 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200632
633 *bx *= scale;
634 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200635}
636
637WL_EXPORT pixman_box32_t
638weston_transformed_rect(int width, int height,
639 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200640 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200641 pixman_box32_t rect)
642{
643 float x1, x2, y1, y2;
644
645 pixman_box32_t ret;
646
Alexander Larsson4ea95522013-05-22 14:41:37 +0200647 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200648 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200649 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200650 rect.x2, rect.y2, &x2, &y2);
651
652 if (x1 <= x2) {
653 ret.x1 = x1;
654 ret.x2 = x2;
655 } else {
656 ret.x1 = x2;
657 ret.x2 = x1;
658 }
659
660 if (y1 <= y2) {
661 ret.y1 = y1;
662 ret.y2 = y2;
663 } else {
664 ret.y1 = y2;
665 ret.y2 = y1;
666 }
667
668 return ret;
669}
670
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600671/** Transform a region by a matrix, restricted to axis-aligned transformations
672 *
673 * Warning: This function does not work for projective, affine, or matrices
674 * that encode arbitrary rotations. Only 90-degree step rotations are
675 * supported.
676 */
677WL_EXPORT void
678weston_matrix_transform_region(pixman_region32_t *dest,
679 struct weston_matrix *matrix,
680 pixman_region32_t *src)
681{
682 pixman_box32_t *src_rects, *dest_rects;
683 int nrects, i;
684
685 src_rects = pixman_region32_rectangles(src, &nrects);
686 dest_rects = malloc(nrects * sizeof(*dest_rects));
687 if (!dest_rects)
688 return;
689
690 for (i = 0; i < nrects; i++) {
691 struct weston_vector vec1 = {{
692 src_rects[i].x1, src_rects[i].y1, 0, 1
693 }};
694 weston_matrix_transform(matrix, &vec1);
695 vec1.f[0] /= vec1.f[3];
696 vec1.f[1] /= vec1.f[3];
697
698 struct weston_vector vec2 = {{
699 src_rects[i].x2, src_rects[i].y2, 0, 1
700 }};
701 weston_matrix_transform(matrix, &vec2);
702 vec2.f[0] /= vec2.f[3];
703 vec2.f[1] /= vec2.f[3];
704
705 if (vec1.f[0] < vec2.f[0]) {
706 dest_rects[i].x1 = floor(vec1.f[0]);
707 dest_rects[i].x2 = ceil(vec2.f[0]);
708 } else {
709 dest_rects[i].x1 = floor(vec2.f[0]);
710 dest_rects[i].x2 = ceil(vec1.f[0]);
711 }
712
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600713 if (vec1.f[1] < vec2.f[1]) {
714 dest_rects[i].y1 = floor(vec1.f[1]);
715 dest_rects[i].y2 = ceil(vec2.f[1]);
716 } else {
717 dest_rects[i].y1 = floor(vec2.f[1]);
718 dest_rects[i].y2 = ceil(vec1.f[1]);
719 }
720 }
721
722 pixman_region32_clear(dest);
723 pixman_region32_init_rects(dest, dest_rects, nrects);
724 free(dest_rects);
725}
726
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200727WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500728weston_transformed_region(int width, int height,
729 enum wl_output_transform transform,
730 int32_t scale,
731 pixman_region32_t *src, pixman_region32_t *dest)
732{
733 pixman_box32_t *src_rects, *dest_rects;
734 int nrects, i;
735
736 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
737 if (src != dest)
738 pixman_region32_copy(dest, src);
739 return;
740 }
741
742 src_rects = pixman_region32_rectangles(src, &nrects);
743 dest_rects = malloc(nrects * sizeof(*dest_rects));
744 if (!dest_rects)
745 return;
746
747 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
748 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
749 } else {
750 for (i = 0; i < nrects; i++) {
751 switch (transform) {
752 default:
753 case WL_OUTPUT_TRANSFORM_NORMAL:
754 dest_rects[i].x1 = src_rects[i].x1;
755 dest_rects[i].y1 = src_rects[i].y1;
756 dest_rects[i].x2 = src_rects[i].x2;
757 dest_rects[i].y2 = src_rects[i].y2;
758 break;
759 case WL_OUTPUT_TRANSFORM_90:
760 dest_rects[i].x1 = height - src_rects[i].y2;
761 dest_rects[i].y1 = src_rects[i].x1;
762 dest_rects[i].x2 = height - src_rects[i].y1;
763 dest_rects[i].y2 = src_rects[i].x2;
764 break;
765 case WL_OUTPUT_TRANSFORM_180:
766 dest_rects[i].x1 = width - src_rects[i].x2;
767 dest_rects[i].y1 = height - src_rects[i].y2;
768 dest_rects[i].x2 = width - src_rects[i].x1;
769 dest_rects[i].y2 = height - src_rects[i].y1;
770 break;
771 case WL_OUTPUT_TRANSFORM_270:
772 dest_rects[i].x1 = src_rects[i].y1;
773 dest_rects[i].y1 = width - src_rects[i].x2;
774 dest_rects[i].x2 = src_rects[i].y2;
775 dest_rects[i].y2 = width - src_rects[i].x1;
776 break;
777 case WL_OUTPUT_TRANSFORM_FLIPPED:
778 dest_rects[i].x1 = width - src_rects[i].x2;
779 dest_rects[i].y1 = src_rects[i].y1;
780 dest_rects[i].x2 = width - src_rects[i].x1;
781 dest_rects[i].y2 = src_rects[i].y2;
782 break;
783 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
784 dest_rects[i].x1 = height - src_rects[i].y2;
785 dest_rects[i].y1 = width - src_rects[i].x2;
786 dest_rects[i].x2 = height - src_rects[i].y1;
787 dest_rects[i].y2 = width - src_rects[i].x1;
788 break;
789 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
790 dest_rects[i].x1 = src_rects[i].x1;
791 dest_rects[i].y1 = height - src_rects[i].y2;
792 dest_rects[i].x2 = src_rects[i].x2;
793 dest_rects[i].y2 = height - src_rects[i].y1;
794 break;
795 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
796 dest_rects[i].x1 = src_rects[i].y1;
797 dest_rects[i].y1 = src_rects[i].x1;
798 dest_rects[i].x2 = src_rects[i].y2;
799 dest_rects[i].y2 = src_rects[i].x2;
800 break;
801 }
802 }
803 }
804
805 if (scale != 1) {
806 for (i = 0; i < nrects; i++) {
807 dest_rects[i].x1 *= scale;
808 dest_rects[i].x2 *= scale;
809 dest_rects[i].y1 *= scale;
810 dest_rects[i].y2 *= scale;
811 }
812 }
813
814 pixman_region32_clear(dest);
815 pixman_region32_init_rects(dest, dest_rects, nrects);
816 free(dest_rects);
817}
818
Jonny Lamb74130762013-11-26 18:19:46 +0100819static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300820viewport_surface_to_buffer(struct weston_surface *surface,
821 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100822{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200823 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200824 double src_width, src_height;
825 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200826
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200827 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
828 if (vp->surface.width == -1) {
829 *bx = sx;
830 *by = sy;
831 return;
832 }
Jonny Lamb74130762013-11-26 18:19:46 +0100833
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200834 src_x = 0.0;
835 src_y = 0.0;
836 src_width = surface->width_from_buffer;
837 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100838 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200839 src_x = wl_fixed_to_double(vp->buffer.src_x);
840 src_y = wl_fixed_to_double(vp->buffer.src_y);
841 src_width = wl_fixed_to_double(vp->buffer.src_width);
842 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100843 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200844
845 *bx = sx * src_width / surface->width + src_x;
846 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100847}
848
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500849WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200850weston_surface_to_buffer_float(struct weston_surface *surface,
851 float sx, float sy, float *bx, float *by)
852{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200853 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
854
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300855 /* first transform coordinates if the viewport is set */
856 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100857
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500858 weston_transformed_coord(surface->width_from_buffer,
859 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200860 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100861 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200862}
863
Derek Foreman99739672015-12-03 16:38:11 -0600864/** Transform a rectangle from surface coordinates to buffer coordinates
865 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300866 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600867 * from.
868 * \param rect The rectangle to transform.
869 * \return The transformed rectangle.
870 *
871 * Viewport and buffer transformations can only do translation, scaling,
872 * and rotations in 90-degree steps. Therefore the only loss in the
873 * conversion is coordinate rounding.
874 *
875 * However, some coordinate rounding takes place as an intermediate
876 * step before the buffer scale factor is applied, so the rectangle
877 * boundary may not be exactly as expected.
878 *
879 * This is OK for damage tracking since a little extra coverage is
880 * not a problem.
881 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200882WL_EXPORT pixman_box32_t
883weston_surface_to_buffer_rect(struct weston_surface *surface,
884 pixman_box32_t rect)
885{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200886 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100887 float xf, yf;
888
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300889 /* first transform box coordinates if the viewport is set */
890 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100891 rect.x1 = floorf(xf);
892 rect.y1 = floorf(yf);
893
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300894 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600895 rect.x2 = ceilf(xf);
896 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100897
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500898 return weston_transformed_rect(surface->width_from_buffer,
899 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200900 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200901 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200902}
903
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200904/** Transform a region from surface coordinates to buffer coordinates
905 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300906 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200907 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300908 * \param[in] surface_region The region in surface coordinates.
909 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200910 *
911 * Buffer_region must be init'd, but will be completely overwritten.
912 *
913 * Viewport and buffer transformations can only do translation, scaling,
914 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600915 * conversion is from the coordinate rounding that takes place in
916 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300917 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200918 */
919WL_EXPORT void
920weston_surface_to_buffer_region(struct weston_surface *surface,
921 pixman_region32_t *surface_region,
922 pixman_region32_t *buffer_region)
923{
924 pixman_box32_t *src_rects, *dest_rects;
925 int nrects, i;
926
927 src_rects = pixman_region32_rectangles(surface_region, &nrects);
928 dest_rects = malloc(nrects * sizeof(*dest_rects));
929 if (!dest_rects)
930 return;
931
932 for (i = 0; i < nrects; i++) {
933 dest_rects[i] = weston_surface_to_buffer_rect(surface,
934 src_rects[i]);
935 }
936
937 pixman_region32_fini(buffer_region);
938 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
939 free(dest_rects);
940}
941
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200942WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500943weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400944 struct weston_plane *plane)
945{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500946 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400947 return;
948
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 weston_view_damage_below(view);
950 view->plane = plane;
951 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400952}
953
Pekka Paalanen51723d52015-02-17 13:10:01 +0200954/** Inflict damage on the plane where the view is visible.
955 *
956 * \param view The view that causes the damage.
957 *
958 * If the view is currently on a plane (including the primary plane),
959 * take the view's boundingbox, subtract all the opaque views that cover it,
960 * and add the remaining region as damage to the plane. This corresponds
961 * to the damage inflicted to the plane if this view disappeared.
962 *
963 * A repaint is scheduled for this view.
964 *
965 * The region of all opaque views covering this view is stored in
966 * weston_view::clip and updated by view_accumulate_damage() during
967 * weston_output_repaint(). Specifically, that region matches the
968 * scenegraph as it was last painted.
969 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400970WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200972{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500973 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200974
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500975 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200976 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800978 if (view->plane)
979 pixman_region32_union(&view->plane->damage,
980 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500981 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800982 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200983}
984
Pekka Paalanen01e00682017-03-24 16:21:06 +0200985/** Send wl_surface.enter/leave events
986 *
987 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300988 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200989 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +0300990 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200991 *
992 * Send the enter/leave events for all protocol objects bound to the given
993 * output by the client owning the surface.
994 */
995static void
996weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300997 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +0200998 bool enter,
999 bool leave)
1000{
1001 struct wl_resource *wloutput;
1002 struct wl_client *client;
1003
1004 assert(enter != leave);
1005
1006 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001007 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001008 if (wl_resource_get_client(wloutput) != client)
1009 continue;
1010
1011 if (enter)
1012 wl_surface_send_enter(surface->resource, wloutput);
1013 if (leave)
1014 wl_surface_send_leave(surface->resource, wloutput);
1015 }
1016}
1017
Bryce Harrington3f650b82015-12-23 11:01:58 -08001018/**
1019 * \param es The surface
1020 * \param mask The new set of outputs for the surface
1021 *
1022 * Sets the surface's set of outputs to the ones specified by
1023 * the new output mask provided. Identifies the outputs that
1024 * have changed, the posts enter and leave events for these
1025 * outputs as appropriate.
1026 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001027static void
1028weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1029{
1030 uint32_t different = es->output_mask ^ mask;
1031 uint32_t entered = mask & different;
1032 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001033 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001034 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001035 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001036
1037 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001038 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001039 return;
1040 if (different == 0)
1041 return;
1042
1043 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001044 output_bit = 1u << output->id;
1045 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001046 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001047
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001048 wl_list_for_each(head, &output->head_list, output_link) {
1049 weston_surface_send_enter_leave(es, head,
1050 output_bit & entered,
1051 output_bit & left);
1052 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001053 }
1054}
1055
Semi Malinene7a52fb2018-04-26 11:08:10 +02001056static void
1057notify_view_output_destroy(struct wl_listener *listener, void *data)
1058{
1059 struct weston_view *view =
1060 container_of(listener,
1061 struct weston_view, output_destroy_listener);
1062
1063 view->output = NULL;
1064 view->output_destroy_listener.notify = NULL;
1065}
1066
1067/** Set the primary output of the view
1068 *
1069 * \param view The view whose primary output to set
1070 * \param output The new primary output for the view
1071 *
1072 * Set \a output to be the primary output of the \a view.
1073 *
1074 * Notice that the assignment may be temporary; the primary output could be
1075 * automatically changed. Hence, one cannot rely on the value persisting.
1076 *
1077 * Passing NULL as /a output will set the primary output to NULL.
1078 */
1079WL_EXPORT void
1080weston_view_set_output(struct weston_view *view, struct weston_output *output)
1081{
1082 if (view->output_destroy_listener.notify) {
1083 wl_list_remove(&view->output_destroy_listener.link);
1084 view->output_destroy_listener.notify = NULL;
1085 }
1086 view->output = output;
1087 if (output) {
1088 view->output_destroy_listener.notify =
1089 notify_view_output_destroy;
1090 wl_signal_add(&output->destroy_signal,
1091 &view->output_destroy_listener);
1092 }
1093}
1094
Bryce Harrington3f650b82015-12-23 11:01:58 -08001095/** Recalculate which output(s) the surface has views displayed on
1096 *
1097 * \param es The surface to remap to outputs
1098 *
1099 * Finds the output that is showing the largest amount of one
1100 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001101 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001102 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001103 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001104 * in the output_mask for the surface.
1105 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001106static void
1107weston_surface_assign_output(struct weston_surface *es)
1108{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001109 struct weston_output *new_output;
1110 struct weston_view *view;
1111 pixman_region32_t region;
1112 uint32_t max, area, mask;
1113 pixman_box32_t *e;
1114
1115 new_output = NULL;
1116 max = 0;
1117 mask = 0;
1118 pixman_region32_init(&region);
1119 wl_list_for_each(view, &es->views, surface_link) {
1120 if (!view->output)
1121 continue;
1122
1123 pixman_region32_intersect(&region, &view->transform.boundingbox,
1124 &view->output->region);
1125
1126 e = pixman_region32_extents(&region);
1127 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1128
1129 mask |= view->output_mask;
1130
1131 if (area >= max) {
1132 new_output = view->output;
1133 max = area;
1134 }
1135 }
1136 pixman_region32_fini(&region);
1137
1138 es->output = new_output;
1139 weston_surface_update_output_mask(es, mask);
1140}
1141
Bryce Harrington3f650b82015-12-23 11:01:58 -08001142/** Recalculate which output(s) the view is displayed on
1143 *
1144 * \param ev The view to remap to outputs
1145 *
1146 * Identifies the set of outputs that the view is visible on,
1147 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001148 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001149 *
1150 * Also does the same for the view's surface. See
1151 * weston_surface_assign_output().
1152 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001153static void
1154weston_view_assign_output(struct weston_view *ev)
1155{
1156 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001157 struct weston_output *output, *new_output;
1158 pixman_region32_t region;
1159 uint32_t max, area, mask;
1160 pixman_box32_t *e;
1161
1162 new_output = NULL;
1163 max = 0;
1164 mask = 0;
1165 pixman_region32_init(&region);
1166 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001167 if (output->destroying)
1168 continue;
1169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001171 &output->region);
1172
1173 e = pixman_region32_extents(&region);
1174 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1175
1176 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001177 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001178
1179 if (area >= max) {
1180 new_output = output;
1181 max = area;
1182 }
1183 }
1184 pixman_region32_fini(&region);
1185
Semi Malinene7a52fb2018-04-26 11:08:10 +02001186 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 ev->output_mask = mask;
1188
1189 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001190}
1191
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001192static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001193weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1194 int from_x, int from_y, int *to_x, int *to_y)
1195{
1196 float x, y;
1197
1198 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1199 weston_view_from_global_float(to, x, y, &x, &y);
1200
1201 *to_x = round(x);
1202 *to_y = round(y);
1203}
1204
1205static void
1206weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1207{
1208 pixman_box32_t *a;
1209 pixman_box32_t b;
1210
1211 a = pixman_region32_extents(&from->geometry.scissor);
1212
1213 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1214 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1215
1216 pixman_region32_fini(&to->geometry.scissor);
1217 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1218}
1219
1220static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001221view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001223{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001224 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1225 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001226 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001227 { inbox->x1, inbox->y1 },
1228 { inbox->x1, inbox->y2 },
1229 { inbox->x2, inbox->y1 },
1230 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001231 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001232 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001233 int i;
1234
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001235 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001236 /* avoid rounding empty bbox to 1x1 */
1237 pixman_region32_init(bbox);
1238 return;
1239 }
1240
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001241 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001242 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001244 if (x < min_x)
1245 min_x = x;
1246 if (x > max_x)
1247 max_x = x;
1248 if (y < min_y)
1249 min_y = y;
1250 if (y > max_y)
1251 max_y = y;
1252 }
1253
Pekka Paalanen219b9822012-02-08 15:38:37 +02001254 int_x = floorf(min_x);
1255 int_y = floorf(min_y);
1256 pixman_region32_init_rect(bbox, int_x, int_y,
1257 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001258}
1259
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001260static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001262{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001264
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001265 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 view->geometry.x = roundf(view->geometry.x);
1267 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001268
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001269 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1271 view->transform.position.matrix.d[12] = view->geometry.x;
1272 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001273
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001275
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 view->transform.inverse = view->transform.position.matrix;
1277 view->transform.inverse.d[12] = -view->geometry.x;
1278 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001279
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001281 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001282 view->surface->width,
1283 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001284 if (view->geometry.scissor_enabled)
1285 pixman_region32_intersect(&view->transform.boundingbox,
1286 &view->transform.boundingbox,
1287 &view->geometry.scissor);
1288
1289 pixman_region32_translate(&view->transform.boundingbox,
1290 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001291
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 if (view->alpha == 1.0) {
1293 pixman_region32_copy(&view->transform.opaque,
1294 &view->surface->opaque);
1295 pixman_region32_translate(&view->transform.opaque,
1296 view->geometry.x,
1297 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001298 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001299}
1300
1301static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001303{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 struct weston_view *parent = view->geometry.parent;
1305 struct weston_matrix *matrix = &view->transform.matrix;
1306 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001307 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001308 pixman_region32_t surfregion;
1309 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001310
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001312
1313 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1315 view->transform.position.matrix.d[12] = view->geometry.x;
1316 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001317
1318 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001320 weston_matrix_multiply(matrix, &tform->matrix);
1321
Pekka Paalanen483243f2013-03-08 14:56:50 +02001322 if (parent)
1323 weston_matrix_multiply(matrix, &parent->transform.matrix);
1324
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001325 if (weston_matrix_invert(inverse, matrix) < 0) {
1326 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327 weston_log("error: weston_view %p"
1328 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001329 return -1;
1330 }
1331
Giulio Camuffo148c1992016-09-04 18:50:46 +03001332 if (view->alpha == 1.0 &&
1333 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1334 pixman_region32_copy(&view->transform.opaque,
1335 &view->surface->opaque);
1336 pixman_region32_translate(&view->transform.opaque,
1337 matrix->d[12],
1338 matrix->d[13]);
1339 }
1340
Pekka Paalanen380adf52015-02-16 14:39:11 +02001341 pixman_region32_init_rect(&surfregion, 0, 0,
1342 view->surface->width, view->surface->height);
1343 if (view->geometry.scissor_enabled)
1344 pixman_region32_intersect(&surfregion, &surfregion,
1345 &view->geometry.scissor);
1346 surfbox = pixman_region32_extents(&surfregion);
1347
1348 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1349 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001350
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001351 return 0;
1352}
1353
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001354static struct weston_layer *
1355get_view_layer(struct weston_view *view)
1356{
1357 if (view->parent_view)
1358 return get_view_layer(view->parent_view);
1359 return view->layer_link.layer;
1360}
1361
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001362WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001364{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001366 struct weston_layer *layer;
1367 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001368
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001370 return;
1371
Pekka Paalanen483243f2013-03-08 14:56:50 +02001372 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001374
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001376
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001378
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 pixman_region32_fini(&view->transform.boundingbox);
1380 pixman_region32_fini(&view->transform.opaque);
1381 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001382
Pekka Paalanencd403622012-01-25 13:37:39 +02001383 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384 if (view->geometry.transformation_list.next ==
1385 &view->transform.position.link &&
1386 view->geometry.transformation_list.prev ==
1387 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001388 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001390 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 if (weston_view_update_transform_enable(view) < 0)
1392 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001393 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001394
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001395 layer = get_view_layer(view);
1396 if (layer) {
1397 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001398 pixman_region32_intersect(&view->transform.boundingbox,
1399 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001400 pixman_region32_intersect(&view->transform.opaque,
1401 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001402 pixman_region32_fini(&mask);
1403 }
1404
Pekka Paalanen380adf52015-02-16 14:39:11 +02001405 if (parent) {
1406 if (parent->geometry.scissor_enabled) {
1407 view->geometry.scissor_enabled = true;
1408 weston_view_transfer_scissor(parent, view);
1409 } else {
1410 view->geometry.scissor_enabled = false;
1411 }
1412 }
1413
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001415
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001417
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 wl_signal_emit(&view->surface->compositor->transform_signal,
1419 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001420}
1421
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001422WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001424{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001425 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001426
1427 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 * The invariant: if view->geometry.dirty, then all views
1429 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001430 * Corollary: if not parent->geometry.dirty, then all ancestors
1431 * are not dirty.
1432 */
1433
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001435 return;
1436
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001438
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001440 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001442}
1443
1444WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445weston_view_to_global_fixed(struct weston_view *view,
1446 wl_fixed_t vx, wl_fixed_t vy,
1447 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001448{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001449 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001450
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 weston_view_to_global_float(view,
1452 wl_fixed_to_double(vx),
1453 wl_fixed_to_double(vy),
1454 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001455 *x = wl_fixed_from_double(xf);
1456 *y = wl_fixed_from_double(yf);
1457}
1458
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001459WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460weston_view_from_global_float(struct weston_view *view,
1461 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001462{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001464 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1465
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001467
1468 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001469 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001471 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 *vx = 0;
1473 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001474 return;
1475 }
1476
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 *vx = v.f[0] / v.f[3];
1478 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001479 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 *vx = x - view->geometry.x;
1481 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001482 }
1483}
1484
1485WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486weston_view_from_global_fixed(struct weston_view *view,
1487 wl_fixed_t x, wl_fixed_t y,
1488 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001489{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001491
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492 weston_view_from_global_float(view,
1493 wl_fixed_to_double(x),
1494 wl_fixed_to_double(y),
1495 &vxf, &vyf);
1496 *vx = wl_fixed_from_double(vxf);
1497 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001498}
1499
1500WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501weston_view_from_global(struct weston_view *view,
1502 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001503{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001504 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001505
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1507 *vx = floorf(vxf);
1508 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001509}
1510
Bryce Harrington3f650b82015-12-23 11:01:58 -08001511/**
1512 * \param surface The surface to be repainted
1513 *
1514 * Marks the output(s) that the surface is shown on as needing to be
1515 * repainted. See weston_output_schedule_repaint().
1516 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001517WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001518weston_surface_schedule_repaint(struct weston_surface *surface)
1519{
1520 struct weston_output *output;
1521
1522 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001523 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001524 weston_output_schedule_repaint(output);
1525}
1526
Bryce Harrington3f650b82015-12-23 11:01:58 -08001527/**
1528 * \param view The view to be repainted
1529 *
1530 * Marks the output(s) that the view is shown on as needing to be
1531 * repainted. See weston_output_schedule_repaint().
1532 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001533WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534weston_view_schedule_repaint(struct weston_view *view)
1535{
1536 struct weston_output *output;
1537
1538 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001539 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001540 weston_output_schedule_repaint(output);
1541}
1542
Pekka Paalanene508ce62015-02-19 13:59:55 +02001543/**
1544 * XXX: This function does it the wrong way.
1545 * surface->damage is the damage from the client, and causes
1546 * surface_flush_damage() to copy pixels. No window management action can
1547 * cause damage to the client-provided content, warranting re-upload!
1548 *
1549 * Instead of surface->damage, this function should record the damage
1550 * with all the views for this surface to avoid extraneous texture
1551 * uploads.
1552 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001554weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001555{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001556 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 0, 0, surface->width,
1558 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001559
Kristian Høgsberg98238702012-08-03 16:29:12 -04001560 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001561}
1562
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001563WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001565{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001566 if (view->geometry.x == x && view->geometry.y == y)
1567 return;
1568
Jason Ekstranda7af7042013-10-12 22:38:11 -05001569 view->geometry.x = x;
1570 view->geometry.y = y;
1571 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001572}
1573
Pekka Paalanen483243f2013-03-08 14:56:50 +02001574static void
1575transform_parent_handle_parent_destroy(struct wl_listener *listener,
1576 void *data)
1577{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 struct weston_view *view =
1579 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001580 geometry.parent_destroy_listener);
1581
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001583}
1584
1585WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001587 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001588{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589 if (view->geometry.parent) {
1590 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1591 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001592
1593 if (!parent)
1594 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001595 }
1596
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001598
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001600 transform_parent_handle_parent_destroy;
1601 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001602 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001604 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001605 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001606 }
1607
Jason Ekstranda7af7042013-10-12 22:38:11 -05001608 weston_view_geometry_dirty(view);
1609}
1610
Pekka Paalanen380adf52015-02-16 14:39:11 +02001611/** Set a clip mask rectangle on a view
1612 *
1613 * \param view The view to set the clip mask on.
1614 * \param x Top-left corner X coordinate of the clip rectangle.
1615 * \param y Top-left corner Y coordinate of the clip rectangle.
1616 * \param width Width of the clip rectangle, non-negative.
1617 * \param height Height of the clip rectangle, non-negative.
1618 *
1619 * A shell may set a clip mask rectangle on a view. Everything outside
1620 * the rectangle is cut away for input and output purposes: it is
1621 * not drawn and cannot be hit by hit-test based input like pointer
1622 * motion or touch-downs. Everything inside the rectangle will behave
1623 * normally. Clients are unaware of clipping.
1624 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001625 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001626 * mask rectangle does not affect the view position, the view is positioned
1627 * as it would be without a clip. The clip also does not change
1628 * weston_surface::width,height.
1629 *
1630 * The clip mask rectangle is part of transformation inheritance
1631 * (weston_view_set_transform_parent()). A clip set in the root of the
1632 * transformation inheritance tree will affect all views in the tree.
1633 * A clip can be set only on the root view. Attempting to set a clip
1634 * on view that has a transformation parent will fail. Assigning a parent
1635 * to a view that has a clip set will cause the clip to be forgotten.
1636 *
1637 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1638 * on the additional transformations in the child views. These transformations
1639 * may not rotate the coordinate axes, i.e., only translation and scaling
1640 * are allowed. Violating this restriction causes the clipping to malfunction.
1641 * Furthermore, using scaling may cause rounding errors in child clipping.
1642 *
1643 * The clip mask rectangle is not automatically adjusted based on
1644 * wl_surface.attach dx and dy arguments.
1645 *
1646 * A clip mask rectangle can be set only if the compositor capability
1647 * WESTON_CAP_VIEW_CLIP_MASK is present.
1648 *
1649 * This function sets the clip mask rectangle and schedules a repaint for
1650 * the view.
1651 */
1652WL_EXPORT void
1653weston_view_set_mask(struct weston_view *view,
1654 int x, int y, int width, int height)
1655{
1656 struct weston_compositor *compositor = view->surface->compositor;
1657
1658 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1659 weston_log("%s not allowed without capability!\n", __func__);
1660 return;
1661 }
1662
1663 if (view->geometry.parent) {
1664 weston_log("view %p has a parent, clip forbidden!\n", view);
1665 return;
1666 }
1667
1668 if (width < 0 || height < 0) {
1669 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1670 x, y, width, height);
1671 return;
1672 }
1673
1674 pixman_region32_fini(&view->geometry.scissor);
1675 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1676 view->geometry.scissor_enabled = true;
1677 weston_view_geometry_dirty(view);
1678 weston_view_schedule_repaint(view);
1679}
1680
1681/** Remove the clip mask from a view
1682 *
1683 * \param view The view to remove the clip mask from.
1684 *
1685 * Removed the clip mask rectangle and schedules a repaint.
1686 *
1687 * \sa weston_view_set_mask
1688 */
1689WL_EXPORT void
1690weston_view_set_mask_infinite(struct weston_view *view)
1691{
1692 view->geometry.scissor_enabled = false;
1693 weston_view_geometry_dirty(view);
1694 weston_view_schedule_repaint(view);
1695}
1696
Armin Krezović0da12b82016-06-30 06:04:33 +02001697/* Check if view should be displayed
1698 *
1699 * The indicator is set manually when assigning
1700 * a view to a surface.
1701 *
1702 * This needs reworking. See the thread starting at:
1703 *
1704 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1705 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001706WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001707weston_view_is_mapped(struct weston_view *view)
1708{
Armin Krezović0da12b82016-06-30 06:04:33 +02001709 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001710}
1711
Philipp Zabel70decd52018-09-03 20:11:15 +02001712/* Check if view is opaque in specified region
1713 *
1714 * \param view The view to check for opacity.
1715 * \param region The region to check for opacity, in view coordinates.
1716 *
1717 * Returns true if the view is opaque in the specified region, because view
1718 * alpha is 1.0 and either the opaque region set by the client contains the
1719 * specified region, or the buffer pixel format or solid color is opaque.
1720 */
1721WL_EXPORT bool
1722weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1723{
1724 pixman_region32_t r;
1725 bool ret = false;
1726
1727 if (ev->alpha < 1.0)
1728 return false;
1729
1730 if (ev->surface->is_opaque)
1731 return true;
1732
1733 if (ev->transform.dirty) {
1734 weston_log("%s: transform dirty", __func__);
1735 return false;
1736 }
1737
1738 pixman_region32_init(&r);
1739 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1740
1741 if (!pixman_region32_not_empty(&r))
1742 ret = true;
1743
1744 pixman_region32_fini(&r);
1745
1746 return ret;
1747}
1748
Armin Krezović0da12b82016-06-30 06:04:33 +02001749/* Check if a surface has a view assigned to it
1750 *
1751 * The indicator is set manually when mapping
1752 * a surface and creating a view for it.
1753 *
1754 * This needs to go. See the thread starting at:
1755 *
1756 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1757 *
1758 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001759WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001760weston_surface_is_mapped(struct weston_surface *surface)
1761{
Armin Krezović0da12b82016-06-30 06:04:33 +02001762 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001763}
1764
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001765static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001766surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001767{
1768 struct weston_view *view;
1769
1770 if (surface->width == width && surface->height == height)
1771 return;
1772
1773 surface->width = width;
1774 surface->height = height;
1775
1776 wl_list_for_each(view, &surface->views, surface_link)
1777 weston_view_geometry_dirty(view);
1778}
1779
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001780WL_EXPORT void
1781weston_surface_set_size(struct weston_surface *surface,
1782 int32_t width, int32_t height)
1783{
1784 assert(!surface->resource);
1785 surface_set_size(surface, width, height);
1786}
1787
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001788static int
1789fixed_round_up_to_int(wl_fixed_t f)
1790{
1791 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1792}
1793
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001794static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001795convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1796 int32_t width, int32_t height,
1797 uint32_t transform,
1798 int32_t scale)
1799{
1800 assert(scale > 0);
1801
1802 switch (transform) {
1803 case WL_OUTPUT_TRANSFORM_NORMAL:
1804 case WL_OUTPUT_TRANSFORM_180:
1805 case WL_OUTPUT_TRANSFORM_FLIPPED:
1806 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1807 *width_out = width / scale;
1808 *height_out = height / scale;
1809 break;
1810 case WL_OUTPUT_TRANSFORM_90:
1811 case WL_OUTPUT_TRANSFORM_270:
1812 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1813 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1814 *width_out = height / scale;
1815 *height_out = width / scale;
1816 break;
1817 default:
1818 assert(0 && "invalid transform");
1819 }
1820}
1821
1822static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001823weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001824{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001825 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001826
1827 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001828 surface->width_from_buffer = 0;
1829 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001830 return;
1831 }
1832
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001833 convert_size_by_transform_scale(&surface->width_from_buffer,
1834 &surface->height_from_buffer,
1835 surface->buffer_ref.buffer->width,
1836 surface->buffer_ref.buffer->height,
1837 vp->buffer.transform,
1838 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001839}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001840
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001841static void
1842weston_surface_update_size(struct weston_surface *surface)
1843{
1844 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1845 int32_t width, height;
1846
1847 width = surface->width_from_buffer;
1848 height = surface->height_from_buffer;
1849
1850 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001851 surface_set_size(surface,
1852 vp->surface.width, vp->surface.height);
1853 return;
1854 }
1855
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001856 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001857 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1858 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1859
1860 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001861 return;
1862 }
1863
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001864 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001865}
1866
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001867/** weston_compositor_get_time
1868 * \ingroup compositor
1869 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001870WL_EXPORT void
1871weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001872{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001873 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001874}
1875
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001876/** weston_compositor_pick_view
1877 * \ingroup compositor
1878 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001879WL_EXPORT struct weston_view *
1880weston_compositor_pick_view(struct weston_compositor *compositor,
1881 wl_fixed_t x, wl_fixed_t y,
1882 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001883{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001884 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001885 wl_fixed_t view_x, view_y;
1886 int view_ix, view_iy;
1887 int ix = wl_fixed_to_int(x);
1888 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001889
Jason Ekstranda7af7042013-10-12 22:38:11 -05001890 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001891 if (!pixman_region32_contains_point(
1892 &view->transform.boundingbox, ix, iy, NULL))
1893 continue;
1894
1895 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1896 view_ix = wl_fixed_to_int(view_x);
1897 view_iy = wl_fixed_to_int(view_y);
1898
1899 if (!pixman_region32_contains_point(&view->surface->input,
1900 view_ix, view_iy, NULL))
1901 continue;
1902
Pekka Paalanen380adf52015-02-16 14:39:11 +02001903 if (view->geometry.scissor_enabled &&
1904 !pixman_region32_contains_point(&view->geometry.scissor,
1905 view_ix, view_iy, NULL))
1906 continue;
1907
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001908 *vx = view_x;
1909 *vy = view_y;
1910 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001911 }
1912
Derek Foremanf9318d12015-05-11 15:40:11 -05001913 *vx = wl_fixed_from_int(-1000000);
1914 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001915 return NULL;
1916}
1917
1918static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001919weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001920{
Daniel Stone37816df2012-05-16 18:45:18 +01001921 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001922
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001923 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001924 return;
1925
Daniel Stone37816df2012-05-16 18:45:18 +01001926 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001927 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001928}
1929
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001930WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001931weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001932{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001933 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001934
Jason Ekstranda7af7042013-10-12 22:38:11 -05001935 if (!weston_view_is_mapped(view))
1936 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001937
Jason Ekstranda7af7042013-10-12 22:38:11 -05001938 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02001939 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08001940 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001941 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001942 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001943 wl_list_remove(&view->link);
1944 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001945 view->output_mask = 0;
1946 weston_surface_assign_output(view->surface);
1947
1948 if (weston_surface_is_mapped(view->surface))
1949 return;
1950
1951 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001952 struct weston_touch *touch = weston_seat_get_touch(seat);
1953 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1954 struct weston_keyboard *keyboard =
1955 weston_seat_get_keyboard(seat);
1956
1957 if (keyboard && keyboard->focus == view->surface)
1958 weston_keyboard_set_focus(keyboard, NULL);
1959 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001960 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001961 if (touch && touch->focus == view)
1962 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001963 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001964}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001965
Jason Ekstranda7af7042013-10-12 22:38:11 -05001966WL_EXPORT void
1967weston_surface_unmap(struct weston_surface *surface)
1968{
1969 struct weston_view *view;
1970
Armin Krezovićf8486c32016-06-30 06:04:28 +02001971 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001972 wl_list_for_each(view, &surface->views, surface_link)
1973 weston_view_unmap(view);
1974 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001975}
1976
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001977static void
1978weston_surface_reset_pending_buffer(struct weston_surface *surface)
1979{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001980 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001981 surface->pending.sx = 0;
1982 surface->pending.sy = 0;
1983 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001984 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001985}
1986
Jason Ekstranda7af7042013-10-12 22:38:11 -05001987WL_EXPORT void
1988weston_view_destroy(struct weston_view *view)
1989{
1990 wl_signal_emit(&view->destroy_signal, view);
1991
1992 assert(wl_list_empty(&view->geometry.child_list));
1993
1994 if (weston_view_is_mapped(view)) {
1995 weston_view_unmap(view);
1996 weston_compositor_build_view_list(view->surface->compositor);
1997 }
1998
1999 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002000 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002001
2002 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002003 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002004 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002005 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002006
2007 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002008 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002009
Jason Ekstranda7af7042013-10-12 22:38:11 -05002010 wl_list_remove(&view->surface_link);
2011
2012 free(view);
2013}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002014
2015WL_EXPORT void
2016weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002017{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002018 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002019 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002020 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002021
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002022 if (--surface->ref_count > 0)
2023 return;
2024
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002025 assert(surface->resource == NULL);
2026
Pekka Paalanenca790762015-04-17 14:23:38 +03002027 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002028
Pekka Paalanene67858b2013-04-25 13:57:42 +03002029 assert(wl_list_empty(&surface->subsurface_list_pending));
2030 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002031
Jason Ekstranda7af7042013-10-12 22:38:11 -05002032 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2033 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002034
Jason Ekstrand7b982072014-05-20 14:33:03 -05002035 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002036
Pekka Paalanende685b82012-12-04 15:58:12 +02002037 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002038 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002039
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002040 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002041 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002042 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002043
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002044 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002045 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002046
Pekka Paalanen133e4392014-09-23 22:08:46 -04002047 weston_presentation_feedback_discard_list(&surface->feedback_list);
2048
Jonas Ådahld3414f22016-07-22 17:56:31 +08002049 wl_list_for_each_safe(constraint, next_constraint,
2050 &surface->pointer_constraints,
2051 link)
2052 weston_pointer_constraint_destroy(constraint);
2053
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002054 fd_clear(&surface->acquire_fence_fd);
2055
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002056 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002057}
2058
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002059static void
2060destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002061{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002062 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002063
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002064 assert(surface);
2065
Giulio Camuffo0d379742013-11-15 22:06:15 +01002066 /* Set the resource to NULL, since we don't want to leave a
2067 * dangling pointer if the surface was refcounted and survives
2068 * the weston_surface_destroy() call. */
2069 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002070
2071 if (surface->viewport_resource)
2072 wl_resource_set_user_data(surface->viewport_resource, NULL);
2073
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002074 if (surface->synchronization_resource) {
2075 wl_resource_set_user_data(surface->synchronization_resource,
2076 NULL);
2077 }
2078
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002079 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002080}
2081
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002082static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002083weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2084{
2085 struct weston_buffer *buffer =
2086 container_of(listener, struct weston_buffer, destroy_listener);
2087
2088 wl_signal_emit(&buffer->destroy_signal, buffer);
2089 free(buffer);
2090}
2091
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002092WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002093weston_buffer_from_resource(struct wl_resource *resource)
2094{
2095 struct weston_buffer *buffer;
2096 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002097
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002098 listener = wl_resource_get_destroy_listener(resource,
2099 weston_buffer_destroy_handler);
2100
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002101 if (listener)
2102 return container_of(listener, struct weston_buffer,
2103 destroy_listener);
2104
2105 buffer = zalloc(sizeof *buffer);
2106 if (buffer == NULL)
2107 return NULL;
2108
2109 buffer->resource = resource;
2110 wl_signal_init(&buffer->destroy_signal);
2111 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002112 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002113 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002114
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002115 return buffer;
2116}
2117
2118static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002119weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2120 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002121{
Pekka Paalanende685b82012-12-04 15:58:12 +02002122 struct weston_buffer_reference *ref =
2123 container_of(listener, struct weston_buffer_reference,
2124 destroy_listener);
2125
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002126 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002127 ref->buffer = NULL;
2128}
2129
2130WL_EXPORT void
2131weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002132 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002133{
2134 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002135 ref->buffer->busy_count--;
2136 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002137 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002138 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002139 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002140 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002141 }
2142
Pekka Paalanende685b82012-12-04 15:58:12 +02002143 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002144 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002145 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002146 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002147 }
2148
Pekka Paalanende685b82012-12-04 15:58:12 +02002149 ref->buffer = buffer;
2150 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2151}
2152
2153static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002154weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2155 void *data)
2156{
2157 struct weston_buffer_release_reference *ref =
2158 container_of(listener, struct weston_buffer_release_reference,
2159 destroy_listener);
2160
2161 assert((struct wl_resource *)data == ref->buffer_release->resource);
2162 ref->buffer_release = NULL;
2163}
2164
2165static void
2166weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2167{
2168 struct wl_resource *resource = buffer_release->resource;
2169 int release_fence_fd = buffer_release->fence_fd;
2170
2171 if (release_fence_fd >= 0) {
2172 zwp_linux_buffer_release_v1_send_fenced_release(
2173 resource, release_fence_fd);
2174 } else {
2175 zwp_linux_buffer_release_v1_send_immediate_release(
2176 resource);
2177 }
2178
2179 wl_resource_destroy(resource);
2180}
2181
2182WL_EXPORT void
2183weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2184 struct weston_buffer_release *buffer_release)
2185{
2186 if (buffer_release == ref->buffer_release)
2187 return;
2188
2189 if (ref->buffer_release) {
2190 ref->buffer_release->ref_count--;
2191 wl_list_remove(&ref->destroy_listener.link);
2192 if (ref->buffer_release->ref_count == 0)
2193 weston_buffer_release_destroy(ref->buffer_release);
2194 }
2195
2196 if (buffer_release) {
2197 buffer_release->ref_count++;
2198 wl_resource_add_destroy_listener(buffer_release->resource,
2199 &ref->destroy_listener);
2200 }
2201
2202 ref->buffer_release = buffer_release;
2203 ref->destroy_listener.notify =
2204 weston_buffer_release_reference_handle_destroy;
2205}
2206
2207WL_EXPORT void
2208weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2209 struct weston_buffer_release_reference *src)
2210{
2211 weston_buffer_release_reference(dest, src->buffer_release);
2212 weston_buffer_release_reference(src, NULL);
2213}
2214
2215static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002216weston_surface_attach(struct weston_surface *surface,
2217 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002218{
2219 weston_buffer_reference(&surface->buffer_ref, buffer);
2220
Pekka Paalanena6421c42012-12-04 15:58:10 +02002221 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002222 if (weston_surface_is_mapped(surface))
2223 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002224 }
2225
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002226 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002227
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002228 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002229 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002230}
2231
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002232/** weston_compositor_damage_all
2233 * \ingroup compositor
2234 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002235WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002236weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002237{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002238 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002239
2240 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002241 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002242}
2243
Marius Vlad55d87362019-06-11 01:15:35 +03002244/**
2245 * \ingroup output
2246 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002247WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002248weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002249{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002250 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002251
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002252 pixman_region32_union(&compositor->primary_plane.damage,
2253 &compositor->primary_plane.damage,
2254 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002255 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002256}
2257
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002258static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002259surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002260{
Pekka Paalanende685b82012-12-04 15:58:12 +02002261 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002262 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002263 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002264
Pekka Paalanenb5026542014-11-12 15:09:24 +02002265 if (weston_timeline_enabled_ &&
2266 pixman_region32_not_empty(&surface->damage))
2267 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2268 TLP_OUTPUT(surface->output), TLP_END);
2269
Jason Ekstrandef540082014-06-26 10:37:36 -07002270 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002271}
2272
2273static void
2274view_accumulate_damage(struct weston_view *view,
2275 pixman_region32_t *opaque)
2276{
2277 pixman_region32_t damage;
2278
2279 pixman_region32_init(&damage);
2280 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002281 pixman_box32_t *extents;
2282
Jason Ekstranda7af7042013-10-12 22:38:11 -05002283 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002284 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002285 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002286 pixman_region32_copy(&damage, &view->surface->damage);
2287 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002288 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002289 }
2290
Pekka Paalanen380adf52015-02-16 14:39:11 +02002291 pixman_region32_intersect(&damage, &damage,
2292 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293 pixman_region32_subtract(&damage, &damage, opaque);
2294 pixman_region32_union(&view->plane->damage,
2295 &view->plane->damage, &damage);
2296 pixman_region32_fini(&damage);
2297 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002298 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002299}
2300
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002301static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002302compositor_accumulate_damage(struct weston_compositor *ec)
2303{
2304 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002306 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002307
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002308 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002309
2310 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002311 pixman_region32_copy(&plane->clip, &clip);
2312
2313 pixman_region32_init(&opaque);
2314
Jason Ekstranda7af7042013-10-12 22:38:11 -05002315 wl_list_for_each(ev, &ec->view_list, link) {
2316 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002317 continue;
2318
Jason Ekstranda7af7042013-10-12 22:38:11 -05002319 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002320 }
2321
2322 pixman_region32_union(&clip, &clip, &opaque);
2323 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002324 }
2325
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002326 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002327
Jason Ekstranda7af7042013-10-12 22:38:11 -05002328 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002329 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002330
2331 wl_list_for_each(ev, &ec->view_list, link) {
2332 if (ev->surface->touched)
2333 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002334 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002335
2336 surface_flush_damage(ev->surface);
2337
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002338 /* Both the renderer and the backend have seen the buffer
2339 * by now. If renderer needs the buffer, it has its own
2340 * reference set. If the backend wants to keep the buffer
2341 * around for migrating the surface into a non-primary plane
2342 * later, keep_buffer is true. Otherwise, drop the core
2343 * reference now, and allow early buffer release. This enables
2344 * clients to use single-buffering.
2345 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002346 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002347 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002348 weston_buffer_release_reference(
2349 &ev->surface->buffer_release_ref, NULL);
2350 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002351 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002352}
2353
2354static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002355surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002356{
2357 struct weston_subsurface *sub;
2358
Pekka Paalanene67858b2013-04-25 13:57:42 +03002359 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002360 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002361 continue;
2362
Jason Ekstranda7af7042013-10-12 22:38:11 -05002363 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2364 wl_list_init(&sub->surface->views);
2365
2366 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002367 }
2368}
2369
2370static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002371surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002372{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002373 struct weston_subsurface *sub;
2374 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002375
Jason Ekstranda7af7042013-10-12 22:38:11 -05002376 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2377 if (sub->surface == surface)
2378 continue;
2379
George Kiagiadakised04d382014-06-13 18:10:26 +02002380 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2381 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002382 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002383 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002384
2385 surface_free_unused_subsurface_views(sub->surface);
2386 }
2387}
2388
2389static void
2390view_list_add_subsurface_view(struct weston_compositor *compositor,
2391 struct weston_subsurface *sub,
2392 struct weston_view *parent)
2393{
2394 struct weston_subsurface *child;
2395 struct weston_view *view = NULL, *iv;
2396
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002397 if (!weston_surface_is_mapped(sub->surface))
2398 return;
2399
Jason Ekstranda7af7042013-10-12 22:38:11 -05002400 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2401 if (iv->geometry.parent == parent) {
2402 view = iv;
2403 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002404 }
2405 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002406
2407 if (view) {
2408 /* Put it back in the surface's list of views */
2409 wl_list_remove(&view->surface_link);
2410 wl_list_insert(&sub->surface->views, &view->surface_link);
2411 } else {
2412 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002413 weston_view_set_position(view,
2414 sub->position.x,
2415 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002416 weston_view_set_transform_parent(view, parent);
2417 }
2418
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002419 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002420 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002421 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002422
Pekka Paalanenb188e912013-11-19 14:03:35 +02002423 if (wl_list_empty(&sub->surface->subsurface_list)) {
2424 wl_list_insert(compositor->view_list.prev, &view->link);
2425 return;
2426 }
2427
2428 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2429 if (child->surface == sub->surface)
2430 wl_list_insert(compositor->view_list.prev, &view->link);
2431 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002432 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002433 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002434}
2435
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002436/* This recursively adds the sub-surfaces for a view, relying on the
2437 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2438 * change first happens to the sub-surface list, and then automatically
2439 * propagates here. See weston_surface_damage_subsurfaces() for how the
2440 * sub-surfaces receive damage when the client changes the state.
2441 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002442static void
2443view_list_add(struct weston_compositor *compositor,
2444 struct weston_view *view)
2445{
2446 struct weston_subsurface *sub;
2447
2448 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002449
Pekka Paalanenb188e912013-11-19 14:03:35 +02002450 if (wl_list_empty(&view->surface->subsurface_list)) {
2451 wl_list_insert(compositor->view_list.prev, &view->link);
2452 return;
2453 }
2454
2455 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2456 if (sub->surface == view->surface)
2457 wl_list_insert(compositor->view_list.prev, &view->link);
2458 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002459 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002460 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002461}
2462
2463static void
2464weston_compositor_build_view_list(struct weston_compositor *compositor)
2465{
2466 struct weston_view *view;
2467 struct weston_layer *layer;
2468
2469 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002470 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002471 surface_stash_subsurface_views(view->surface);
2472
2473 wl_list_init(&compositor->view_list);
2474 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002475 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002476 view_list_add(compositor, view);
2477 }
2478 }
2479
2480 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002481 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002482 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002483}
2484
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002485static void
2486weston_output_take_feedback_list(struct weston_output *output,
2487 struct weston_surface *surface)
2488{
2489 struct weston_view *view;
2490 struct weston_presentation_feedback *feedback;
2491 uint32_t flags = 0xffffffff;
2492
2493 if (wl_list_empty(&surface->feedback_list))
2494 return;
2495
2496 /* All views must have the flag for the flag to survive. */
2497 wl_list_for_each(view, &surface->views, surface_link) {
2498 /* ignore views that are not on this output at all */
2499 if (view->output_mask & (1u << output->id))
2500 flags &= view->psf_flags;
2501 }
2502
2503 wl_list_for_each(feedback, &surface->feedback_list, link)
2504 feedback->psf_flags = flags;
2505
2506 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2507 wl_list_init(&surface->feedback_list);
2508}
2509
David Herrmann1edf44c2013-10-22 17:11:26 +02002510static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002511weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002512{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002513 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002514 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002515 struct weston_animation *animation, *next;
2516 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002517 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002518 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002519 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002520 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002521
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002522 if (output->destroying)
2523 return 0;
2524
Pekka Paalanenb5026542014-11-12 15:09:24 +02002525 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2526
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002527 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002528 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002529
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002530 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002531 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002532 } else {
2533 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002534 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002535 ev->psf_flags = 0;
2536 }
2537 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002538
Pekka Paalanene67858b2013-04-25 13:57:42 +03002539 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002540 wl_list_for_each(ev, &ec->view_list, link) {
2541 /* Note: This operation is safe to do multiple times on the
2542 * same surface.
2543 */
2544 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002545 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002546 &ev->surface->frame_callback_list);
2547 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002548
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002549 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002550 }
2551 }
2552
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002553 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002554
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002555 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002556 pixman_region32_intersect(&output_damage,
2557 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002558 pixman_region32_subtract(&output_damage,
2559 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002560
Scott Moreauccbf29d2012-02-22 14:21:41 -07002561 if (output->dirty)
2562 weston_output_update_matrix(output);
2563
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002564 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002565
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002566 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002567
Daniel Stone09a97e22017-03-01 11:34:06 +00002568 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002569 if (r == 0)
2570 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002571
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002572 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002573
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002574 frame_time_msec = timespec_to_msec(&output->frame_time);
2575
Jonas Ådahldb773762012-06-13 00:01:21 +02002576 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002577 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002578 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002579 }
2580
Scott Moreaud64cf212012-06-08 19:40:54 -06002581 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002582 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002583 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002584 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002585
Pekka Paalanenb5026542014-11-12 15:09:24 +02002586 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2587
David Herrmann1edf44c2013-10-22 17:11:26 +02002588 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002589}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002590
Pekka Paalanen82919792014-05-21 13:51:49 +03002591static void
2592weston_output_schedule_repaint_reset(struct weston_output *output)
2593{
Daniel Stone05df8c12017-03-03 16:59:42 +00002594 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002595 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002596}
2597
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002598static int
2599weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2600 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002601{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002602 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002603 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002604 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002605
Daniel Stone6847b852017-03-01 11:34:08 +00002606 /* We're not ready yet; come back to make a decision later. */
2607 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002608 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002609
2610 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2611 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002612 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002613
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002614 /* If we're sleeping, drop the repaint machinery entirely; we will
2615 * explicitly repaint all outputs when we come back. */
2616 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2617 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2618 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002619
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002620 /* We don't actually need to repaint this output; drop it from
2621 * repaint until something causes damage. */
2622 if (!output->repaint_needed)
2623 goto err;
2624
2625 /* If repaint fails, we aren't going to get weston_output_finish_frame
2626 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002627 * something schedules a successful repaint later. As repainting may
2628 * take some time, re-read our clock as a courtesy to the next
2629 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002630 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002631 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002632 if (ret != 0)
2633 goto err;
2634
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002635 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002636 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002637
2638err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002639 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002640 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002641}
2642
2643static void
2644output_repaint_timer_arm(struct weston_compositor *compositor)
2645{
2646 struct weston_output *output;
2647 bool any_should_repaint = false;
2648 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002649 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002650
2651 weston_compositor_read_presentation_clock(compositor, &now);
2652
2653 wl_list_for_each(output, &compositor->output_list, link) {
2654 int64_t msec_to_this;
2655
2656 if (output->repaint_status != REPAINT_SCHEDULED)
2657 continue;
2658
2659 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2660 &now);
2661 if (!any_should_repaint || msec_to_this < msec_to_next)
2662 msec_to_next = msec_to_this;
2663
2664 any_should_repaint = true;
2665 }
2666
2667 if (!any_should_repaint)
2668 return;
2669
2670 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2671 * This is a workaround to allow coalescing multiple output repaints
2672 * particularly from weston_output_finish_frame()
2673 * into the same call, which would not happen if we called
2674 * output_repaint_timer_handler() directly.
2675 */
2676 if (msec_to_next < 1)
2677 msec_to_next = 1;
2678
2679 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2680}
2681
2682static int
2683output_repaint_timer_handler(void *data)
2684{
2685 struct weston_compositor *compositor = data;
2686 struct weston_output *output;
2687 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002688 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002689 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002690
2691 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002692
2693 if (compositor->backend->repaint_begin)
2694 repaint_data = compositor->backend->repaint_begin(compositor);
2695
2696 wl_list_for_each(output, &compositor->output_list, link) {
2697 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2698 if (ret)
2699 break;
2700 }
2701
2702 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002703 if (compositor->backend->repaint_flush)
2704 compositor->backend->repaint_flush(compositor,
2705 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002706 } else {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002707 wl_list_for_each(output, &compositor->output_list, link) {
2708 if (output->repainted)
2709 weston_output_schedule_repaint_reset(output);
2710 }
2711
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002712 if (compositor->backend->repaint_cancel)
2713 compositor->backend->repaint_cancel(compositor,
2714 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002715 }
Daniel Stone6847b852017-03-01 11:34:08 +00002716
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002717 wl_list_for_each(output, &compositor->output_list, link)
2718 output->repainted = false;
2719
Daniel Stone6847b852017-03-01 11:34:08 +00002720 output_repaint_timer_arm(compositor);
2721
Pekka Paalanen0513a952014-05-21 16:17:27 +03002722 return 0;
2723}
2724
Marius Vlad55d87362019-06-11 01:15:35 +03002725/**
2726 * \ingroup output
2727 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002728WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002729weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002730 const struct timespec *stamp,
2731 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002732{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002733 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002734 int32_t refresh_nsec;
2735 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002736 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002737
Pekka Paalanenb5026542014-11-12 15:09:24 +02002738
Daniel Stone05df8c12017-03-03 16:59:42 +00002739 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002740 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2741
Daniel Stone6847b852017-03-01 11:34:08 +00002742 weston_compositor_read_presentation_clock(compositor, &now);
2743
Daniel Stone3615ce12017-03-01 11:34:05 +00002744 /* If we haven't been supplied any timestamp at all, we don't have a
2745 * timebase to work against, so any delay just wastes time. Push a
2746 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002747 if (!stamp) {
2748 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002749 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002750 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002751
Marius Vladdf9278a2018-03-06 18:56:23 +02002752 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2753 TLP_VBLANK(stamp), TLP_END);
2754
Pekka Paalanend7894d02015-07-03 15:08:53 +03002755 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002756 weston_presentation_feedback_present_list(&output->feedback_list,
2757 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002758 output->msc,
2759 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002760
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002761 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002762
Daniel Stone6847b852017-03-01 11:34:08 +00002763 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2764 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2765 -compositor->repaint_msec);
2766 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002767
2768 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002769 static bool warned;
2770
2771 if (!warned)
2772 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002773 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002774 warned = true;
2775
Daniel Stone6847b852017-03-01 11:34:08 +00002776 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002777 }
2778
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002779 /* Called from restart_repaint_loop and restart happens already after
2780 * the deadline given by repaint_msec? In that case we delay until
2781 * the deadline of the next frame, to give clients a more predictable
2782 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002783 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2784 msec_rel < 0) {
2785 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2786 timespec_add_nsec(&output->next_repaint,
2787 &output->next_repaint,
2788 refresh_nsec);
2789 }
2790 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002791
Daniel Stone3615ce12017-03-01 11:34:05 +00002792out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002793 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002794 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002795}
2796
2797static void
2798idle_repaint(void *data)
2799{
2800 struct weston_output *output = data;
2801
Daniel Stone05df8c12017-03-03 16:59:42 +00002802 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2803 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002804 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002805 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002806}
2807
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002808WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002809weston_layer_entry_insert(struct weston_layer_entry *list,
2810 struct weston_layer_entry *entry)
2811{
2812 wl_list_insert(&list->link, &entry->link);
2813 entry->layer = list->layer;
2814}
2815
2816WL_EXPORT void
2817weston_layer_entry_remove(struct weston_layer_entry *entry)
2818{
2819 wl_list_remove(&entry->link);
2820 wl_list_init(&entry->link);
2821 entry->layer = NULL;
2822}
2823
Quentin Glidic82681572016-12-17 13:40:51 +01002824
2825/** Initialize the weston_layer struct.
2826 *
2827 * \param compositor The compositor instance
2828 * \param layer The layer to initialize
2829 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002830WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002831weston_layer_init(struct weston_layer *layer,
2832 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002833{
Quentin Glidic82681572016-12-17 13:40:51 +01002834 layer->compositor = compositor;
2835 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002836 wl_list_init(&layer->view_list.link);
2837 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002838 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002839}
2840
2841/** Sets the position of the layer in the layer list. The layer will be placed
2842 * below any layer with the same position value, if any.
2843 * This function is safe to call if the layer is already on the list, but the
2844 * layer may be moved below other layers at the same position, if any.
2845 *
2846 * \param layer The layer to modify
2847 * \param position The position the layer will be placed at
2848 */
2849WL_EXPORT void
2850weston_layer_set_position(struct weston_layer *layer,
2851 enum weston_layer_position position)
2852{
2853 struct weston_layer *below;
2854
2855 wl_list_remove(&layer->link);
2856
2857 /* layer_list is ordered from top to bottom, the last layer being the
2858 * background with the smallest position value */
2859
2860 layer->position = position;
2861 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2862 if (below->position >= layer->position) {
2863 wl_list_insert(&below->link, &layer->link);
2864 return;
2865 }
2866 }
2867 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2868}
2869
2870/** Hide a layer by taking it off the layer list.
2871 * This function is safe to call if the layer is not on the list.
2872 *
2873 * \param layer The layer to hide
2874 */
2875WL_EXPORT void
2876weston_layer_unset_position(struct weston_layer *layer)
2877{
2878 wl_list_remove(&layer->link);
2879 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002880}
2881
2882WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002883weston_layer_set_mask(struct weston_layer *layer,
2884 int x, int y, int width, int height)
2885{
2886 struct weston_view *view;
2887
2888 layer->mask.x1 = x;
2889 layer->mask.x2 = x + width;
2890 layer->mask.y1 = y;
2891 layer->mask.y2 = y + height;
2892
2893 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2894 weston_view_geometry_dirty(view);
2895 }
2896}
2897
2898WL_EXPORT void
2899weston_layer_set_mask_infinite(struct weston_layer *layer)
2900{
2901 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2902 UINT32_MAX, UINT32_MAX);
2903}
2904
Daniel Stone3b775632018-07-20 08:38:25 +01002905WL_EXPORT bool
2906weston_layer_mask_is_infinite(struct weston_layer *layer)
2907{
2908 return layer->mask.x1 == INT32_MIN &&
2909 layer->mask.y1 == INT32_MIN &&
2910 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
2911 layer->mask.y2 == INT32_MIN + UINT32_MAX;
2912}
2913
Marius Vlad55d87362019-06-11 01:15:35 +03002914/**
2915 * \ingroup output
2916 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002917WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002918weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002919{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002920 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002921 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002922
Bryce Harrington08976ac2016-08-30 12:05:16 -07002923 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2924 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002925 return;
2926
Pekka Paalanenb5026542014-11-12 15:09:24 +02002927 if (!output->repaint_needed)
2928 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2929
Kristian Høgsbergef044142011-06-21 15:02:12 -04002930 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002931 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002932
2933 /* If we already have a repaint scheduled for our idle handler,
2934 * no need to set it again. If the repaint has been called but
2935 * not finished, then weston_output_finish_frame() will notice
2936 * that a repaint is needed and schedule one. */
2937 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002938 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002939
Daniel Stone05df8c12017-03-03 16:59:42 +00002940 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002941 assert(!output->idle_repaint_source);
2942 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2943 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002944 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002945}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002946
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002947/** weston_compositor_schedule_repaint
2948 * \ingroup compositor
2949 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002950WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002951weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2952{
2953 struct weston_output *output;
2954
2955 wl_list_for_each(output, &compositor->output_list, link)
2956 weston_output_schedule_repaint(output);
2957}
2958
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002959static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002960surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002961{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002962 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002963}
2964
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002965static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002966surface_attach(struct wl_client *client,
2967 struct wl_resource *resource,
2968 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2969{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002970 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002971 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002972
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002973 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002974 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002975 if (buffer == NULL) {
2976 wl_client_post_no_memory(client);
2977 return;
2978 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002979 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002980
Pekka Paalanende685b82012-12-04 15:58:12 +02002981 /* Attach, attach, without commit in between does not send
2982 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002983 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002984
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002985 surface->pending.sx = sx;
2986 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002987 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002988}
2989
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002990static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002991surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002992 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002993 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002994{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002995 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002996
Derek Foreman57e92ed2015-11-17 14:11:35 -06002997 if (width <= 0 || height <= 0)
2998 return;
2999
Derek Foreman152254b2015-11-26 14:17:48 -06003000 pixman_region32_union_rect(&surface->pending.damage_surface,
3001 &surface->pending.damage_surface,
3002 x, y, width, height);
3003}
3004
3005static void
3006surface_damage_buffer(struct wl_client *client,
3007 struct wl_resource *resource,
3008 int32_t x, int32_t y, int32_t width, int32_t height)
3009{
3010 struct weston_surface *surface = wl_resource_get_user_data(resource);
3011
3012 if (width <= 0 || height <= 0)
3013 return;
3014
3015 pixman_region32_union_rect(&surface->pending.damage_buffer,
3016 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003017 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003018}
3019
Kristian Høgsberg33418202011-08-16 23:01:28 -04003020static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003021destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003022{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003023 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003024
3025 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003026 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003027}
3028
3029static void
3030surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003031 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003032{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003033 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003034 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003035
3036 cb = malloc(sizeof *cb);
3037 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003038 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003039 return;
3040 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003041
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003042 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3043 callback);
3044 if (cb->resource == NULL) {
3045 free(cb);
3046 wl_resource_post_no_memory(resource);
3047 return;
3048 }
3049
Jason Ekstranda85118c2013-06-27 20:17:02 -05003050 wl_resource_set_implementation(cb->resource, NULL, cb,
3051 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003052
Pekka Paalanenbc106382012-10-10 12:49:31 +03003053 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003054}
3055
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003056static void
3057surface_set_opaque_region(struct wl_client *client,
3058 struct wl_resource *resource,
3059 struct wl_resource *region_resource)
3060{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003061 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003062 struct weston_region *region;
3063
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003064 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003065 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003066 pixman_region32_copy(&surface->pending.opaque,
3067 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003068 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003069 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003070 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003071}
3072
3073static void
3074surface_set_input_region(struct wl_client *client,
3075 struct wl_resource *resource,
3076 struct wl_resource *region_resource)
3077{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003078 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003079 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003080
3081 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003082 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003083 pixman_region32_copy(&surface->pending.input,
3084 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003085 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003086 pixman_region32_fini(&surface->pending.input);
3087 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003088 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003089}
3090
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003091/* Cause damage to this sub-surface and all its children.
3092 *
3093 * This is useful when there are state changes that need an implicit
3094 * damage, e.g. a z-order change.
3095 */
3096static void
3097weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3098{
3099 struct weston_subsurface *child;
3100
3101 weston_surface_damage(sub->surface);
3102 sub->reordered = false;
3103
3104 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3105 if (child != sub)
3106 weston_surface_damage_subsurfaces(child);
3107}
3108
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003109static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003110weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003111{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003112 struct weston_subsurface *sub;
3113
3114 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3115 parent_link_pending) {
3116 wl_list_remove(&sub->parent_link);
3117 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003118
3119 if (sub->reordered)
3120 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003121 }
3122}
3123
3124static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003125weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003126 struct weston_matrix *matrix)
3127{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003128 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003129 double src_width, src_height, dest_width, dest_height;
3130
3131 weston_matrix_init(matrix);
3132
3133 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3134 src_width = surface->width_from_buffer;
3135 src_height = surface->height_from_buffer;
3136 } else {
3137 src_width = wl_fixed_to_double(vp->buffer.src_width);
3138 src_height = wl_fixed_to_double(vp->buffer.src_height);
3139 }
3140
3141 if (vp->surface.width == -1) {
3142 dest_width = src_width;
3143 dest_height = src_height;
3144 } else {
3145 dest_width = vp->surface.width;
3146 dest_height = vp->surface.height;
3147 }
3148
3149 if (src_width != dest_width || src_height != dest_height)
3150 weston_matrix_scale(matrix,
3151 src_width / dest_width,
3152 src_height / dest_height, 1);
3153
3154 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3155 weston_matrix_translate(matrix,
3156 wl_fixed_to_double(vp->buffer.src_x),
3157 wl_fixed_to_double(vp->buffer.src_y),
3158 0);
3159
3160 switch (vp->buffer.transform) {
3161 case WL_OUTPUT_TRANSFORM_FLIPPED:
3162 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3163 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3164 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3165 weston_matrix_scale(matrix, -1, 1, 1);
3166 weston_matrix_translate(matrix,
3167 surface->width_from_buffer, 0, 0);
3168 break;
3169 }
3170
3171 switch (vp->buffer.transform) {
3172 default:
3173 case WL_OUTPUT_TRANSFORM_NORMAL:
3174 case WL_OUTPUT_TRANSFORM_FLIPPED:
3175 break;
3176 case WL_OUTPUT_TRANSFORM_90:
3177 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3178 weston_matrix_rotate_xy(matrix, 0, 1);
3179 weston_matrix_translate(matrix,
3180 surface->height_from_buffer, 0, 0);
3181 break;
3182 case WL_OUTPUT_TRANSFORM_180:
3183 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3184 weston_matrix_rotate_xy(matrix, -1, 0);
3185 weston_matrix_translate(matrix,
3186 surface->width_from_buffer,
3187 surface->height_from_buffer, 0);
3188 break;
3189 case WL_OUTPUT_TRANSFORM_270:
3190 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3191 weston_matrix_rotate_xy(matrix, 0, -1);
3192 weston_matrix_translate(matrix,
3193 0, surface->width_from_buffer, 0);
3194 break;
3195 }
3196
3197 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3198}
3199
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003200/**
3201 * Compute a + b > c while being safe to overflows.
3202 */
3203static bool
3204fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3205{
3206 return (int64_t)a + (int64_t)b > (int64_t)c;
3207}
3208
3209static bool
3210weston_surface_is_pending_viewport_source_valid(
3211 const struct weston_surface *surface)
3212{
3213 const struct weston_surface_state *pend = &surface->pending;
3214 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3215 int width_from_buffer = 0;
3216 int height_from_buffer = 0;
3217 wl_fixed_t w;
3218 wl_fixed_t h;
3219
3220 /* If viewport source rect is not set, it is always ok. */
3221 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3222 return true;
3223
3224 if (pend->newly_attached) {
3225 if (pend->buffer) {
3226 convert_size_by_transform_scale(&width_from_buffer,
3227 &height_from_buffer,
3228 pend->buffer->width,
3229 pend->buffer->height,
3230 vp->buffer.transform,
3231 vp->buffer.scale);
3232 } else {
3233 /* No buffer: viewport is irrelevant. */
3234 return true;
3235 }
3236 } else {
3237 width_from_buffer = surface->width_from_buffer;
3238 height_from_buffer = surface->height_from_buffer;
3239 }
3240
3241 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3242 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3243
3244 /* No buffer: viewport is irrelevant. */
3245 if (width_from_buffer == 0 || height_from_buffer == 0)
3246 return true;
3247
3248 /* overflow checks for wl_fixed_from_int() */
3249 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3250 return false;
3251 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3252 return false;
3253
3254 w = wl_fixed_from_int(width_from_buffer);
3255 h = wl_fixed_from_int(height_from_buffer);
3256
3257 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3258 return false;
3259 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3260 return false;
3261
3262 return true;
3263}
3264
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003265static bool
3266fixed_is_integer(wl_fixed_t v)
3267{
3268 return (v & 0xff) == 0;
3269}
3270
3271static bool
3272weston_surface_is_pending_viewport_dst_size_int(
3273 const struct weston_surface *surface)
3274{
3275 const struct weston_buffer_viewport *vp =
3276 &surface->pending.buffer_viewport;
3277
3278 if (vp->surface.width != -1) {
3279 assert(vp->surface.width > 0 && vp->surface.height > 0);
3280 return true;
3281 }
3282
3283 return fixed_is_integer(vp->buffer.src_width) &&
3284 fixed_is_integer(vp->buffer.src_height);
3285}
3286
Derek Foreman152254b2015-11-26 14:17:48 -06003287/* Translate pending damage in buffer co-ordinates to surface
3288 * co-ordinates and union it with a pixman_region32_t.
3289 * This should only be called after the buffer is attached.
3290 */
3291static void
3292apply_damage_buffer(pixman_region32_t *dest,
3293 struct weston_surface *surface,
3294 struct weston_surface_state *state)
3295{
3296 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3297
3298 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3299 * translated into surface co-ordinates and unioned with
3300 * any other surface damage.
3301 * None of this makes sense if there is no buffer though.
3302 */
3303 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3304 pixman_region32_t buffer_damage;
3305
3306 pixman_region32_intersect_rect(&state->damage_buffer,
3307 &state->damage_buffer,
3308 0, 0, buffer->width,
3309 buffer->height);
3310 pixman_region32_init(&buffer_damage);
3311 weston_matrix_transform_region(&buffer_damage,
3312 &surface->buffer_to_surface_matrix,
3313 &state->damage_buffer);
3314 pixman_region32_union(dest, dest, &buffer_damage);
3315 pixman_region32_fini(&buffer_damage);
3316 }
3317 /* We should clear this on commit even if there was no buffer */
3318 pixman_region32_clear(&state->damage_buffer);
3319}
3320
Jason Ekstrand1e059042014-10-16 10:55:19 -05003321static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003322weston_surface_commit_state(struct weston_surface *surface,
3323 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003324{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003325 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003326 pixman_region32_t opaque;
3327
Alexander Larsson4ea95522013-05-22 14:41:37 +02003328 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003329 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003330 /* wp_viewport.set_source */
3331 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003332 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003333
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003334 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003335 if (state->newly_attached) {
3336 /* zwp_surface_synchronization_v1.set_acquire_fence */
3337 fd_move(&surface->acquire_fence_fd,
3338 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003339 /* zwp_surface_synchronization_v1.get_release */
3340 weston_buffer_release_move(&surface->buffer_release_ref,
3341 &state->buffer_release_ref);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003342
Jason Ekstrand7b982072014-05-20 14:33:03 -05003343 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003344 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003345 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003346 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003347 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003348
Jason Ekstrand1e059042014-10-16 10:55:19 -05003349 weston_surface_build_buffer_matrix(surface,
3350 &surface->surface_to_buffer_matrix);
3351 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3352 &surface->surface_to_buffer_matrix);
3353
Jason Ekstrand7b982072014-05-20 14:33:03 -05003354 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003355 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003356 if (surface->committed)
3357 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003358 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003359
Jason Ekstrand7b982072014-05-20 14:33:03 -05003360 state->sx = 0;
3361 state->sy = 0;
3362 state->newly_attached = 0;
3363 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003364
Derek Foreman152254b2015-11-26 14:17:48 -06003365 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003366 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003367 (pixman_region32_not_empty(&state->damage_surface) ||
3368 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003369 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003370
Pekka Paalanen8e159182012-10-10 12:49:25 +03003371 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003372 &state->damage_surface);
3373
3374 apply_damage_buffer(&surface->damage, surface, state);
3375
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003376 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003377 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003378 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003379
3380 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003381 pixman_region32_init(&opaque);
3382 pixman_region32_intersect_rect(&opaque, &state->opaque,
3383 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003384
3385 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3386 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003387 wl_list_for_each(view, &surface->views, surface_link)
3388 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003389 }
3390
3391 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003392
3393 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003394 pixman_region32_intersect_rect(&surface->input, &state->input,
3395 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003396
Pekka Paalanenbc106382012-10-10 12:49:31 +03003397 /* wl_surface.frame */
3398 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003399 &state->frame_callback_list);
3400 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003401
3402 /* XXX:
3403 * What should happen with a feedback request, if there
3404 * is no wl_buffer attached for this commit?
3405 */
3406
3407 /* presentation.feedback */
3408 wl_list_insert_list(&surface->feedback_list,
3409 &state->feedback_list);
3410 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003411
3412 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003413}
3414
3415static void
3416weston_surface_commit(struct weston_surface *surface)
3417{
3418 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003419
Pekka Paalanene67858b2013-04-25 13:57:42 +03003420 weston_surface_commit_subsurface_order(surface);
3421
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003422 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003423}
3424
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003425static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003426weston_subsurface_commit(struct weston_subsurface *sub);
3427
3428static void
3429weston_subsurface_parent_commit(struct weston_subsurface *sub,
3430 int parent_is_synchronized);
3431
3432static void
3433surface_commit(struct wl_client *client, struct wl_resource *resource)
3434{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003435 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003436 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3437
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003438 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3439 assert(surface->viewport_resource);
3440
3441 wl_resource_post_error(surface->viewport_resource,
3442 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3443 "wl_surface@%d has viewport source outside buffer",
3444 wl_resource_get_id(resource));
3445 return;
3446 }
3447
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003448 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3449 assert(surface->viewport_resource);
3450
3451 wl_resource_post_error(surface->viewport_resource,
3452 WP_VIEWPORT_ERROR_BAD_SIZE,
3453 "wl_surface@%d viewport dst size not integer",
3454 wl_resource_get_id(resource));
3455 return;
3456 }
3457
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003458 if (surface->pending.acquire_fence_fd >= 0) {
3459 assert(surface->synchronization_resource);
3460
3461 if (!surface->pending.buffer) {
3462 fd_clear(&surface->pending.acquire_fence_fd);
3463 wl_resource_post_error(surface->synchronization_resource,
3464 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3465 "wl_surface@%"PRIu32" no buffer for synchronization",
3466 wl_resource_get_id(resource));
3467 return;
3468 }
3469
3470 /* We support fences for both wp_linux_dmabuf and opaque EGL
3471 * buffers, as mandated by minor version 2 of the
3472 * zwp_linux_explicit_synchronization_v1 protocol. Since
3473 * renderers that support fences currently only support these
3474 * two buffer types plus SHM buffers, we can just check for the
3475 * SHM buffer case here.
3476 */
3477 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3478 fd_clear(&surface->pending.acquire_fence_fd);
3479 wl_resource_post_error(surface->synchronization_resource,
3480 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3481 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3482 wl_resource_get_id(resource));
3483 return;
3484 }
3485 }
3486
Alexandros Frantzis67629672018-10-19 12:14:11 +03003487 if (surface->pending.buffer_release_ref.buffer_release &&
3488 !surface->pending.buffer) {
3489 assert(surface->synchronization_resource);
3490
3491 wl_resource_post_error(surface->synchronization_resource,
3492 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3493 "wl_surface@%"PRIu32" no buffer for synchronization",
3494 wl_resource_get_id(resource));
3495 return;
3496 }
3497
Pekka Paalanene67858b2013-04-25 13:57:42 +03003498 if (sub) {
3499 weston_subsurface_commit(sub);
3500 return;
3501 }
3502
3503 weston_surface_commit(surface);
3504
3505 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3506 if (sub->surface != surface)
3507 weston_subsurface_parent_commit(sub, 0);
3508 }
3509}
3510
3511static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003512surface_set_buffer_transform(struct wl_client *client,
3513 struct wl_resource *resource, int transform)
3514{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003515 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003516
Jonny Lamba55f1392014-05-30 12:07:15 +02003517 /* if wl_output.transform grows more members this will need to be updated. */
3518 if (transform < 0 ||
3519 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3520 wl_resource_post_error(resource,
3521 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3522 "buffer transform must be a valid transform "
3523 "('%d' specified)", transform);
3524 return;
3525 }
3526
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003527 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003528 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003529}
3530
Alexander Larsson4ea95522013-05-22 14:41:37 +02003531static void
3532surface_set_buffer_scale(struct wl_client *client,
3533 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003534 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003535{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003536 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003537
Jonny Lamba55f1392014-05-30 12:07:15 +02003538 if (scale < 1) {
3539 wl_resource_post_error(resource,
3540 WL_SURFACE_ERROR_INVALID_SCALE,
3541 "buffer scale must be at least one "
3542 "('%d' specified)", scale);
3543 return;
3544 }
3545
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003546 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003547 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003548}
3549
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003550static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003551 surface_destroy,
3552 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003553 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003554 surface_frame,
3555 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003556 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003557 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003558 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003559 surface_set_buffer_scale,
3560 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003561};
3562
3563static void
3564compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003565 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003566{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003567 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003568 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003569
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003570 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003571 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003572 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003573 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003574 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003575
Jason Ekstranda85118c2013-06-27 20:17:02 -05003576 surface->resource =
3577 wl_resource_create(client, &wl_surface_interface,
3578 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003579 if (surface->resource == NULL) {
3580 weston_surface_destroy(surface);
3581 wl_resource_post_no_memory(resource);
3582 return;
3583 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003584 wl_resource_set_implementation(surface->resource, &surface_interface,
3585 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003586
3587 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003588}
3589
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003590static void
3591destroy_region(struct wl_resource *resource)
3592{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003593 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003594
3595 pixman_region32_fini(&region->region);
3596 free(region);
3597}
3598
3599static void
3600region_destroy(struct wl_client *client, struct wl_resource *resource)
3601{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003602 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003603}
3604
3605static void
3606region_add(struct wl_client *client, struct wl_resource *resource,
3607 int32_t x, int32_t y, int32_t width, int32_t height)
3608{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003609 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003610
3611 pixman_region32_union_rect(&region->region, &region->region,
3612 x, y, width, height);
3613}
3614
3615static void
3616region_subtract(struct wl_client *client, struct wl_resource *resource,
3617 int32_t x, int32_t y, int32_t width, int32_t height)
3618{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003619 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003620 pixman_region32_t rect;
3621
3622 pixman_region32_init_rect(&rect, x, y, width, height);
3623 pixman_region32_subtract(&region->region, &region->region, &rect);
3624 pixman_region32_fini(&rect);
3625}
3626
3627static const struct wl_region_interface region_interface = {
3628 region_destroy,
3629 region_add,
3630 region_subtract
3631};
3632
3633static void
3634compositor_create_region(struct wl_client *client,
3635 struct wl_resource *resource, uint32_t id)
3636{
3637 struct weston_region *region;
3638
3639 region = malloc(sizeof *region);
3640 if (region == NULL) {
3641 wl_resource_post_no_memory(resource);
3642 return;
3643 }
3644
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003645 pixman_region32_init(&region->region);
3646
Jason Ekstranda85118c2013-06-27 20:17:02 -05003647 region->resource =
3648 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003649 if (region->resource == NULL) {
3650 free(region);
3651 wl_resource_post_no_memory(resource);
3652 return;
3653 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003654 wl_resource_set_implementation(region->resource, &region_interface,
3655 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003656}
3657
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003658static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003659 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003660 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003661};
3662
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003663static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003664weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3665{
3666 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003667
Jason Ekstrand7b982072014-05-20 14:33:03 -05003668 weston_surface_commit_state(surface, &sub->cached);
3669 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003670
3671 weston_surface_commit_subsurface_order(surface);
3672
3673 weston_surface_schedule_repaint(surface);
3674
Jason Ekstrand7b982072014-05-20 14:33:03 -05003675 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003676}
3677
3678static void
3679weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3680{
3681 struct weston_surface *surface = sub->surface;
3682
3683 /*
3684 * If this commit would cause the surface to move by the
3685 * attach(dx, dy) parameters, the old damage region must be
3686 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003687 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003688 */
Derek Foreman152254b2015-11-26 14:17:48 -06003689 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003690 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003691 pixman_region32_union(&sub->cached.damage_surface,
3692 &sub->cached.damage_surface,
3693 &surface->pending.damage_surface);
3694 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003695
3696 if (surface->pending.newly_attached) {
3697 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003698 weston_surface_state_set_buffer(&sub->cached,
3699 surface->pending.buffer);
3700 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003701 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003702 weston_presentation_feedback_discard_list(
3703 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003704 /* zwp_surface_synchronization_v1.set_acquire_fence */
3705 fd_move(&sub->cached.acquire_fence_fd,
3706 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003707 /* zwp_surface_synchronization_v1.get_release */
3708 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3709 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003710 }
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003711 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003712 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003713 sub->cached.sx += surface->pending.sx;
3714 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003715
Derek Foreman152254b2015-11-26 14:17:48 -06003716 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3717
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003718 sub->cached.buffer_viewport.changed |=
3719 surface->pending.buffer_viewport.changed;
3720 sub->cached.buffer_viewport.buffer =
3721 surface->pending.buffer_viewport.buffer;
3722 sub->cached.buffer_viewport.surface =
3723 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003724
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003725 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003726
3727 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3728
3729 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3730
3731 wl_list_insert_list(&sub->cached.frame_callback_list,
3732 &surface->pending.frame_callback_list);
3733 wl_list_init(&surface->pending.frame_callback_list);
3734
Pekka Paalanen133e4392014-09-23 22:08:46 -04003735 wl_list_insert_list(&sub->cached.feedback_list,
3736 &surface->pending.feedback_list);
3737 wl_list_init(&surface->pending.feedback_list);
3738
Jason Ekstrand7b982072014-05-20 14:33:03 -05003739 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003740}
3741
Derek Foreman280e7dd2014-10-03 13:13:42 -05003742static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003743weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3744{
3745 while (sub) {
3746 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003747 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003748
3749 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003750 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003751
3752 sub = weston_surface_to_subsurface(sub->parent);
3753 }
3754
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003755 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003756}
3757
3758static void
3759weston_subsurface_commit(struct weston_subsurface *sub)
3760{
3761 struct weston_surface *surface = sub->surface;
3762 struct weston_subsurface *tmp;
3763
3764 /* Recursive check for effectively synchronized. */
3765 if (weston_subsurface_is_synchronized(sub)) {
3766 weston_subsurface_commit_to_cache(sub);
3767 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003768 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003769 /* flush accumulated state from cache */
3770 weston_subsurface_commit_to_cache(sub);
3771 weston_subsurface_commit_from_cache(sub);
3772 } else {
3773 weston_surface_commit(surface);
3774 }
3775
3776 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3777 if (tmp->surface != surface)
3778 weston_subsurface_parent_commit(tmp, 0);
3779 }
3780 }
3781}
3782
3783static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003784weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003785{
3786 struct weston_surface *surface = sub->surface;
3787 struct weston_subsurface *tmp;
3788
Pekka Paalanene67858b2013-04-25 13:57:42 +03003789 /* From now on, commit_from_cache the whole sub-tree, regardless of
3790 * the synchronized mode of each child. This sub-surface or some
3791 * of its ancestors were synchronized, so we are synchronized
3792 * all the way down.
3793 */
3794
Jason Ekstrand7b982072014-05-20 14:33:03 -05003795 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003796 weston_subsurface_commit_from_cache(sub);
3797
3798 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3799 if (tmp->surface != surface)
3800 weston_subsurface_parent_commit(tmp, 1);
3801 }
3802}
3803
3804static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003805weston_subsurface_parent_commit(struct weston_subsurface *sub,
3806 int parent_is_synchronized)
3807{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003808 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003809 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003810 wl_list_for_each(view, &sub->surface->views, surface_link)
3811 weston_view_set_position(view,
3812 sub->position.x,
3813 sub->position.y);
3814
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003815 sub->position.set = 0;
3816 }
3817
3818 if (parent_is_synchronized || sub->synchronized)
3819 weston_subsurface_synchronized_commit(sub);
3820}
3821
Pekka Paalanen8274d902014-08-06 19:36:51 +03003822static int
3823subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3824{
3825 return snprintf(buf, len, "sub-surface");
3826}
3827
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003828static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003829subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003830{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003831 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003832
Jason Ekstranda7af7042013-10-12 22:38:11 -05003833 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003834 weston_view_set_position(view,
3835 view->geometry.x + dx,
3836 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003837
3838 /* No need to check parent mappedness, because if parent is not
3839 * mapped, parent is not in a visible layer, so this sub-surface
3840 * will not be drawn either.
3841 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003842
Pekka Paalanene67858b2013-04-25 13:57:42 +03003843 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003844 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003845
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003846 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003847 * because that would call it also for the parent surface,
3848 * which might not be mapped yet. That would lead to
3849 * inconsistent state, where the window could never be
3850 * mapped.
3851 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003852 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003853 * weston_surface_is_mapped() return true, so that when the
3854 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003855 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003856 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003857 }
3858}
3859
3860static struct weston_subsurface *
3861weston_surface_to_subsurface(struct weston_surface *surface)
3862{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003863 if (surface->committed == subsurface_committed)
3864 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003865
3866 return NULL;
3867}
3868
Pekka Paalanen01388e22013-04-25 13:57:44 +03003869WL_EXPORT struct weston_surface *
3870weston_surface_get_main_surface(struct weston_surface *surface)
3871{
3872 struct weston_subsurface *sub;
3873
3874 while (surface && (sub = weston_surface_to_subsurface(surface)))
3875 surface = sub->parent;
3876
3877 return surface;
3878}
3879
Pekka Paalanen50b67472014-10-01 15:02:41 +03003880WL_EXPORT int
3881weston_surface_set_role(struct weston_surface *surface,
3882 const char *role_name,
3883 struct wl_resource *error_resource,
3884 uint32_t error_code)
3885{
3886 assert(role_name);
3887
3888 if (surface->role_name == NULL ||
3889 surface->role_name == role_name ||
3890 strcmp(surface->role_name, role_name) == 0) {
3891 surface->role_name = role_name;
3892
3893 return 0;
3894 }
3895
3896 wl_resource_post_error(error_resource, error_code,
3897 "Cannot assign role %s to wl_surface@%d,"
3898 " already has role %s\n",
3899 role_name,
3900 wl_resource_get_id(surface->resource),
3901 surface->role_name);
3902 return -1;
3903}
3904
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003905WL_EXPORT const char *
3906weston_surface_get_role(struct weston_surface *surface)
3907{
3908 return surface->role_name;
3909}
3910
Pekka Paalanen8274d902014-08-06 19:36:51 +03003911WL_EXPORT void
3912weston_surface_set_label_func(struct weston_surface *surface,
3913 int (*desc)(struct weston_surface *,
3914 char *, size_t))
3915{
3916 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003917 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003918}
3919
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003920/** Get the size of surface contents
3921 *
3922 * \param surface The surface to query.
3923 * \param width Returns the width of raw contents.
3924 * \param height Returns the height of raw contents.
3925 *
3926 * Retrieves the raw surface content size in pixels for the given surface.
3927 * This is the whole content size in buffer pixels. If the surface
3928 * has no content or the renderer does not implement this feature,
3929 * zeroes are returned.
3930 *
3931 * This function is used to determine the buffer size needed for
3932 * a weston_surface_copy_content() call.
3933 */
3934WL_EXPORT void
3935weston_surface_get_content_size(struct weston_surface *surface,
3936 int *width, int *height)
3937{
3938 struct weston_renderer *rer = surface->compositor->renderer;
3939
3940 if (!rer->surface_get_content_size) {
3941 *width = 0;
3942 *height = 0;
3943 return;
3944 }
3945
3946 rer->surface_get_content_size(surface, width, height);
3947}
3948
Quentin Glidic248dd102016-08-12 10:41:34 +02003949/** Get the bounding box of a surface and its subsurfaces
3950 *
3951 * \param surface The surface to query.
3952 * \return The bounding box relative to the surface origin.
3953 *
3954 */
3955WL_EXPORT struct weston_geometry
3956weston_surface_get_bounding_box(struct weston_surface *surface)
3957{
3958 pixman_region32_t region;
3959 pixman_box32_t *box;
3960 struct weston_subsurface *subsurface;
3961
3962 pixman_region32_init_rect(&region,
3963 0, 0,
3964 surface->width, surface->height);
3965
3966 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3967 pixman_region32_union_rect(&region, &region,
3968 subsurface->position.x,
3969 subsurface->position.y,
3970 subsurface->surface->width,
3971 subsurface->surface->height);
3972
3973 box = pixman_region32_extents(&region);
3974 struct weston_geometry geometry = {
3975 .x = box->x1,
3976 .y = box->y1,
3977 .width = box->x2 - box->x1,
3978 .height = box->y2 - box->y1,
3979 };
3980
3981 pixman_region32_fini(&region);
3982
3983 return geometry;
3984}
3985
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003986/** Copy surface contents to system memory.
3987 *
3988 * \param surface The surface to copy from.
3989 * \param target Pointer to the target memory buffer.
3990 * \param size Size of the target buffer in bytes.
3991 * \param src_x X location on contents to copy from.
3992 * \param src_y Y location on contents to copy from.
3993 * \param width Width in pixels of the area to copy.
3994 * \param height Height in pixels of the area to copy.
3995 * \return 0 for success, -1 for failure.
3996 *
3997 * Surface contents are maintained by the renderer. They can be in a
3998 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3999 * else.
4000 *
4001 * Surface contents are copied into memory pointed to by target,
4002 * which has size bytes of space available. The target memory
4003 * may be larger than needed, but being smaller returns an error.
4004 * The extra bytes in target may or may not be written; their content is
4005 * unspecified. Size must be large enough to hold the image.
4006 *
4007 * The image in the target memory will be arranged in rows from
4008 * top to bottom, and pixels on a row from left to right. The pixel
4009 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4010 * width * 4.
4011 *
4012 * Parameters src_x and src_y define the upper-left corner in buffer
4013 * coordinates (pixels) to copy from. Parameters width and height
4014 * define the size of the area to copy in pixels.
4015 *
4016 * The rectangle defined by src_x, src_y, width, height must fit in
4017 * the surface contents. Otherwise an error is returned.
4018 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004019 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004020 * needed target buffer size and rectangle limits.
4021 *
4022 * CURRENT IMPLEMENTATION RESTRICTIONS:
4023 * - the machine must be little-endian due to Pixman formats.
4024 *
4025 * NOTE: Pixman formats are premultiplied.
4026 */
4027WL_EXPORT int
4028weston_surface_copy_content(struct weston_surface *surface,
4029 void *target, size_t size,
4030 int src_x, int src_y,
4031 int width, int height)
4032{
4033 struct weston_renderer *rer = surface->compositor->renderer;
4034 int cw, ch;
4035 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4036
4037 if (!rer->surface_copy_content)
4038 return -1;
4039
4040 weston_surface_get_content_size(surface, &cw, &ch);
4041
4042 if (src_x < 0 || src_y < 0)
4043 return -1;
4044
4045 if (width <= 0 || height <= 0)
4046 return -1;
4047
4048 if (src_x + width > cw || src_y + height > ch)
4049 return -1;
4050
4051 if (width * bytespp * height > size)
4052 return -1;
4053
4054 return rer->surface_copy_content(surface, target, size,
4055 src_x, src_y, width, height);
4056}
4057
Pekka Paalanene67858b2013-04-25 13:57:42 +03004058static void
4059subsurface_set_position(struct wl_client *client,
4060 struct wl_resource *resource, int32_t x, int32_t y)
4061{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004062 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004063
4064 if (!sub)
4065 return;
4066
4067 sub->position.x = x;
4068 sub->position.y = y;
4069 sub->position.set = 1;
4070}
4071
4072static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004073subsurface_find_sibling(struct weston_subsurface *sub,
4074 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004075{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004076 struct weston_surface *parent = sub->parent;
4077 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004078
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004079 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4080 if (sibling->surface == surface && sibling != sub)
4081 return sibling;
4082 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004083
4084 return NULL;
4085}
4086
4087static struct weston_subsurface *
4088subsurface_sibling_check(struct weston_subsurface *sub,
4089 struct weston_surface *surface,
4090 const char *request)
4091{
4092 struct weston_subsurface *sibling;
4093
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004094 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004095 if (!sibling) {
4096 wl_resource_post_error(sub->resource,
4097 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4098 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004099 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004100 return NULL;
4101 }
4102
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004103 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004104
4105 return sibling;
4106}
4107
4108static void
4109subsurface_place_above(struct wl_client *client,
4110 struct wl_resource *resource,
4111 struct wl_resource *sibling_resource)
4112{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004113 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004114 struct weston_surface *surface =
4115 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004116 struct weston_subsurface *sibling;
4117
4118 if (!sub)
4119 return;
4120
4121 sibling = subsurface_sibling_check(sub, surface, "place_above");
4122 if (!sibling)
4123 return;
4124
4125 wl_list_remove(&sub->parent_link_pending);
4126 wl_list_insert(sibling->parent_link_pending.prev,
4127 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004128
4129 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004130}
4131
4132static void
4133subsurface_place_below(struct wl_client *client,
4134 struct wl_resource *resource,
4135 struct wl_resource *sibling_resource)
4136{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004137 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004138 struct weston_surface *surface =
4139 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004140 struct weston_subsurface *sibling;
4141
4142 if (!sub)
4143 return;
4144
4145 sibling = subsurface_sibling_check(sub, surface, "place_below");
4146 if (!sibling)
4147 return;
4148
4149 wl_list_remove(&sub->parent_link_pending);
4150 wl_list_insert(&sibling->parent_link_pending,
4151 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004152
4153 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004154}
4155
4156static void
4157subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4158{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004159 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004160
4161 if (sub)
4162 sub->synchronized = 1;
4163}
4164
4165static void
4166subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4167{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004168 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004169
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004170 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004171 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004172
4173 /* If sub became effectively desynchronized, flush. */
4174 if (!weston_subsurface_is_synchronized(sub))
4175 weston_subsurface_synchronized_commit(sub);
4176 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004177}
4178
4179static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004180weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4181{
4182 wl_list_remove(&sub->parent_link);
4183 wl_list_remove(&sub->parent_link_pending);
4184 wl_list_remove(&sub->parent_destroy_listener.link);
4185 sub->parent = NULL;
4186}
4187
4188static void
4189weston_subsurface_destroy(struct weston_subsurface *sub);
4190
4191static void
4192subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4193{
4194 struct weston_subsurface *sub =
4195 container_of(listener, struct weston_subsurface,
4196 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004197 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004198
4199 /* The protocol object (wl_resource) is left inert. */
4200 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004201 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004202
4203 weston_subsurface_destroy(sub);
4204}
4205
4206static void
4207subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4208{
4209 struct weston_subsurface *sub =
4210 container_of(listener, struct weston_subsurface,
4211 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004212 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004213 assert(sub->surface != sub->parent);
4214
4215 if (weston_surface_is_mapped(sub->surface))
4216 weston_surface_unmap(sub->surface);
4217
4218 weston_subsurface_unlink_parent(sub);
4219}
4220
4221static void
4222subsurface_resource_destroy(struct wl_resource *resource)
4223{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004224 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004225
4226 if (sub)
4227 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004228}
4229
4230static void
4231subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4232{
4233 wl_resource_destroy(resource);
4234}
4235
4236static void
4237weston_subsurface_link_parent(struct weston_subsurface *sub,
4238 struct weston_surface *parent)
4239{
4240 sub->parent = parent;
4241 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004242 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004243 &sub->parent_destroy_listener);
4244
4245 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4246 wl_list_insert(&parent->subsurface_list_pending,
4247 &sub->parent_link_pending);
4248}
4249
4250static void
4251weston_subsurface_link_surface(struct weston_subsurface *sub,
4252 struct weston_surface *surface)
4253{
4254 sub->surface = surface;
4255 sub->surface_destroy_listener.notify =
4256 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004257 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004258 &sub->surface_destroy_listener);
4259}
4260
4261static void
4262weston_subsurface_destroy(struct weston_subsurface *sub)
4263{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004264 struct weston_view *view, *next;
4265
Pekka Paalanene67858b2013-04-25 13:57:42 +03004266 assert(sub->surface);
4267
4268 if (sub->resource) {
4269 assert(weston_surface_to_subsurface(sub->surface) == sub);
4270 assert(sub->parent_destroy_listener.notify ==
4271 subsurface_handle_parent_destroy);
4272
George Kiagiadakised04d382014-06-13 18:10:26 +02004273 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4274 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004275 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004276 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004277
Pekka Paalanene67858b2013-04-25 13:57:42 +03004278 if (sub->parent)
4279 weston_subsurface_unlink_parent(sub);
4280
Jason Ekstrand7b982072014-05-20 14:33:03 -05004281 weston_surface_state_fini(&sub->cached);
4282 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004283
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004284 sub->surface->committed = NULL;
4285 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004286 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004287 } else {
4288 /* the dummy weston_subsurface for the parent itself */
4289 assert(sub->parent_destroy_listener.notify == NULL);
4290 wl_list_remove(&sub->parent_link);
4291 wl_list_remove(&sub->parent_link_pending);
4292 }
4293
4294 wl_list_remove(&sub->surface_destroy_listener.link);
4295 free(sub);
4296}
4297
4298static const struct wl_subsurface_interface subsurface_implementation = {
4299 subsurface_destroy,
4300 subsurface_set_position,
4301 subsurface_place_above,
4302 subsurface_place_below,
4303 subsurface_set_sync,
4304 subsurface_set_desync
4305};
4306
4307static struct weston_subsurface *
4308weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4309 struct weston_surface *parent)
4310{
4311 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004312 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004313
Bryce Harringtonde16d892014-11-20 22:21:57 -08004314 sub = zalloc(sizeof *sub);
4315 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004316 return NULL;
4317
Jason Ekstranda7af7042013-10-12 22:38:11 -05004318 wl_list_init(&sub->unused_views);
4319
Jason Ekstranda85118c2013-06-27 20:17:02 -05004320 sub->resource =
4321 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004322 if (!sub->resource) {
4323 free(sub);
4324 return NULL;
4325 }
4326
Jason Ekstranda85118c2013-06-27 20:17:02 -05004327 wl_resource_set_implementation(sub->resource,
4328 &subsurface_implementation,
4329 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004330 weston_subsurface_link_surface(sub, surface);
4331 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004332 weston_surface_state_init(&sub->cached);
4333 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004334 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004335
4336 return sub;
4337}
4338
4339/* Create a dummy subsurface for having the parent itself in its
4340 * sub-surface lists. Makes stacking order manipulation easy.
4341 */
4342static struct weston_subsurface *
4343weston_subsurface_create_for_parent(struct weston_surface *parent)
4344{
4345 struct weston_subsurface *sub;
4346
Bryce Harringtonde16d892014-11-20 22:21:57 -08004347 sub = zalloc(sizeof *sub);
4348 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004349 return NULL;
4350
4351 weston_subsurface_link_surface(sub, parent);
4352 sub->parent = parent;
4353 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4354 wl_list_insert(&parent->subsurface_list_pending,
4355 &sub->parent_link_pending);
4356
4357 return sub;
4358}
4359
4360static void
4361subcompositor_get_subsurface(struct wl_client *client,
4362 struct wl_resource *resource,
4363 uint32_t id,
4364 struct wl_resource *surface_resource,
4365 struct wl_resource *parent_resource)
4366{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004367 struct weston_surface *surface =
4368 wl_resource_get_user_data(surface_resource);
4369 struct weston_surface *parent =
4370 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004371 struct weston_subsurface *sub;
4372 static const char where[] = "get_subsurface: wl_subsurface@";
4373
4374 if (surface == parent) {
4375 wl_resource_post_error(resource,
4376 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4377 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004378 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004379 return;
4380 }
4381
4382 if (weston_surface_to_subsurface(surface)) {
4383 wl_resource_post_error(resource,
4384 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4385 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004386 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004387 return;
4388 }
4389
Pekka Paalanen50b67472014-10-01 15:02:41 +03004390 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4391 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004392 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004393
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004394 if (weston_surface_get_main_surface(parent) == surface) {
4395 wl_resource_post_error(resource,
4396 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4397 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004398 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004399 return;
4400 }
4401
Pekka Paalanene67858b2013-04-25 13:57:42 +03004402 /* make sure the parent is in its own list */
4403 if (wl_list_empty(&parent->subsurface_list)) {
4404 if (!weston_subsurface_create_for_parent(parent)) {
4405 wl_resource_post_no_memory(resource);
4406 return;
4407 }
4408 }
4409
4410 sub = weston_subsurface_create(id, surface, parent);
4411 if (!sub) {
4412 wl_resource_post_no_memory(resource);
4413 return;
4414 }
4415
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004416 surface->committed = subsurface_committed;
4417 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004418 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004419}
4420
4421static void
4422subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4423{
4424 wl_resource_destroy(resource);
4425}
4426
4427static const struct wl_subcompositor_interface subcompositor_interface = {
4428 subcompositor_destroy,
4429 subcompositor_get_subsurface
4430};
4431
4432static void
4433bind_subcompositor(struct wl_client *client,
4434 void *data, uint32_t version, uint32_t id)
4435{
4436 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004437 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004438
Jason Ekstranda85118c2013-06-27 20:17:02 -05004439 resource =
4440 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004441 if (resource == NULL) {
4442 wl_client_post_no_memory(client);
4443 return;
4444 }
4445 wl_resource_set_implementation(resource, &subcompositor_interface,
4446 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004447}
4448
Bryce Harrington0795ece2016-08-30 12:04:26 -07004449/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004450 *
4451 * \param compositor The compositor instance
4452 * \param state The DPMS state the outputs will be set to
4453 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004454static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004455weston_compositor_dpms(struct weston_compositor *compositor,
4456 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004457{
4458 struct weston_output *output;
4459
Bryce Harrington08976ac2016-08-30 12:05:16 -07004460 wl_list_for_each(output, &compositor->output_list, link)
4461 if (output->set_dpms)
4462 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004463}
4464
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004465/** Restores the compositor to active status
4466 *
4467 * \param compositor The compositor instance
4468 *
4469 * If the compositor was in a sleeping mode, all outputs are powered
4470 * back on via DPMS. Otherwise if the compositor was inactive
4471 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4472 * signal will fire.
4473 *
4474 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004475 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004476 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004477WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004478weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004479{
Neil Roberts8b62e202013-09-30 13:14:47 +01004480 uint32_t old_state = compositor->state;
4481
4482 /* The state needs to be changed before emitting the wake
4483 * signal because that may try to schedule a repaint which
4484 * will not work if the compositor is still sleeping */
4485 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4486
4487 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004488 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004489 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004490 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004491 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004492 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004493 /* fall through */
4494 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004495 wl_event_source_timer_update(compositor->idle_source,
4496 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004497 }
4498}
4499
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004500/** Turns off rendering and frame events for the compositor.
4501 *
4502 * \param compositor The compositor instance
4503 *
4504 * This is used for example to prevent further rendering while the
4505 * compositor is shutting down.
4506 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004507 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004508 *
4509 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004510 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004511WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004512weston_compositor_offscreen(struct weston_compositor *compositor)
4513{
4514 switch (compositor->state) {
4515 case WESTON_COMPOSITOR_OFFSCREEN:
4516 return;
4517 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004518 default:
4519 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4520 wl_event_source_timer_update(compositor->idle_source, 0);
4521 }
4522}
4523
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004524/** Powers down all attached output devices
4525 *
4526 * \param compositor The compositor instance
4527 *
4528 * Causes rendering to the outputs to cease, and no frame events to be
4529 * sent. Only powers down the outputs if the compositor is not already
4530 * in sleep mode.
4531 *
4532 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004533 *
4534 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004535 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004536WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004537weston_compositor_sleep(struct weston_compositor *compositor)
4538{
4539 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4540 return;
4541
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004542 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004543 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4544 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4545}
4546
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004547/** Sets compositor to idle mode
4548 *
4549 * \param data The compositor instance
4550 *
4551 * This is called when the idle timer fires. Once the compositor is in
4552 * idle mode it requires a wake action (e.g. via
4553 * weston_compositor_wake()) to restore it. The compositor's
4554 * idle_signal will be triggered when the idle event occurs.
4555 *
4556 * Idleness can be inhibited by setting the compositor's idle_inhibit
4557 * property.
4558 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004559static int
4560idle_handler(void *data)
4561{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004562 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004563
4564 if (compositor->idle_inhibit)
4565 return 1;
4566
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004567 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004568 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004569
4570 return 1;
4571}
4572
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004573WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004574weston_plane_init(struct weston_plane *plane,
4575 struct weston_compositor *ec,
4576 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004577{
4578 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004579 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004580 plane->x = x;
4581 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004582 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004583
4584 /* Init the link so that the call to wl_list_remove() when releasing
4585 * the plane without ever stacking doesn't lead to a crash */
4586 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004587}
4588
4589WL_EXPORT void
4590weston_plane_release(struct weston_plane *plane)
4591{
Xiong Zhang97116532013-10-23 13:58:31 +08004592 struct weston_view *view;
4593
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004594 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004595 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004596
Xiong Zhang97116532013-10-23 13:58:31 +08004597 wl_list_for_each(view, &plane->compositor->view_list, link) {
4598 if (view->plane == plane)
4599 view->plane = NULL;
4600 }
4601
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004602 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004603}
4604
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004605/** weston_compositor_stack_plane
4606 * \ingroup compositor
4607 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004608WL_EXPORT void
4609weston_compositor_stack_plane(struct weston_compositor *ec,
4610 struct weston_plane *plane,
4611 struct weston_plane *above)
4612{
4613 if (above)
4614 wl_list_insert(above->link.prev, &plane->link);
4615 else
4616 wl_list_insert(&ec->plane_list, &plane->link);
4617}
4618
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004619static void
4620output_release(struct wl_client *client, struct wl_resource *resource)
4621{
4622 wl_resource_destroy(resource);
4623}
4624
4625static const struct wl_output_interface output_interface = {
4626 output_release,
4627};
4628
4629
Casey Dahlin9074db52012-04-19 22:50:09 -04004630static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004631{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004632 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004633}
4634
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004635static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004636bind_output(struct wl_client *client,
4637 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004638{
Pekka Paalanen05347622017-03-27 12:24:34 +03004639 struct weston_head *head = data;
4640 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004641 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004642 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004643
Jason Ekstranda85118c2013-06-27 20:17:02 -05004644 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004645 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004646 if (resource == NULL) {
4647 wl_client_post_no_memory(client);
4648 return;
4649 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004650
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004651 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004652 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004653 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004654
Pekka Paalanen05347622017-03-27 12:24:34 +03004655 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004656 wl_output_send_geometry(resource,
4657 output->x,
4658 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004659 head->mm_width,
4660 head->mm_height,
4661 head->subpixel,
4662 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004663 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004664 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004665 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004666 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004667
4668 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004669 wl_output_send_mode(resource,
4670 mode->flags,
4671 mode->width,
4672 mode->height,
4673 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004674 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004675
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004676 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004677 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004678}
4679
Pekka Paalanendcac3512017-12-08 14:13:34 +02004680static void
4681weston_head_add_global(struct weston_head *head)
4682{
4683 head->global = wl_global_create(head->compositor->wl_display,
4684 &wl_output_interface, 3,
4685 head, bind_output);
4686}
4687
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004688/** Remove the global wl_output protocol object
4689 *
4690 * \param head The head whose global to remove.
4691 *
4692 * Also orphans the wl_resources for this head (wl_output).
4693 */
4694static void
4695weston_head_remove_global(struct weston_head *head)
4696{
4697 struct wl_resource *resource, *tmp;
4698
4699 if (head->global)
4700 wl_global_destroy(head->global);
4701 head->global = NULL;
4702
4703 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4704 unbind_resource(resource);
4705 wl_resource_set_destructor(resource, NULL);
4706 wl_resource_set_user_data(resource, NULL);
4707 }
4708}
4709
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004710/** Get the backing object of wl_output
4711 *
4712 * \param resource A wl_output protocol object.
4713 * \return The backing object (user data) of a wl_resource representing a
4714 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004715 *
4716 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004717 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004718WL_EXPORT struct weston_head *
4719weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004720{
4721 assert(wl_resource_instance_of(resource, &wl_output_interface,
4722 &output_interface));
4723
4724 return wl_resource_get_user_data(resource);
4725}
4726
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004727/** Initialize a pre-allocated weston_head
4728 *
4729 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004730 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004731 *
4732 * The head will be safe to attach, detach and release.
4733 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004734 * The name is used in logs, and can be used by compositors as a configuration
4735 * identifier.
4736 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004737 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004738 * \internal
4739 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004740WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004741weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004742{
4743 /* Add some (in)sane defaults which can be used
4744 * for checking if an output was properly configured
4745 */
4746 memset(head, 0, sizeof *head);
4747
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004748 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004749 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004750 wl_list_init(&head->output_link);
4751 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004752 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004753}
4754
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004755/** Send output heads changed signal
4756 *
4757 * \param output The output that changed.
4758 *
4759 * Notify that the enabled output gained and/or lost heads, or that the
4760 * associated heads may have changed their connection status. This does not
4761 * include cases where the output becomes enabled or disabled. The registered
4762 * callbacks are called after the change has successfully happened.
4763 *
4764 * If connection status change causes the compositor to attach or detach a head
4765 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03004766 *
4767 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004768 */
4769static void
4770weston_output_emit_heads_changed(struct weston_output *output)
4771{
4772 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4773 output);
4774}
4775
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004776/** Idle task for emitting heads_changed_signal */
4777static void
4778weston_compositor_call_heads_changed(void *data)
4779{
4780 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004781 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004782
4783 compositor->heads_changed_source = NULL;
4784
4785 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004786
4787 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4788 if (head->output && head->output->enabled)
4789 weston_output_emit_heads_changed(head->output);
4790 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004791}
4792
4793/** Schedule a call on idle to heads_changed callback
4794 *
4795 * \param compositor The Compositor.
4796 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004797 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004798 * \internal
4799 */
4800static void
4801weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4802{
4803 struct wl_event_loop *loop;
4804
4805 if (compositor->heads_changed_source)
4806 return;
4807
4808 loop = wl_display_get_event_loop(compositor->wl_display);
4809 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4810 weston_compositor_call_heads_changed, compositor);
4811}
4812
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004813/** Register a new head
4814 *
4815 * \param compositor The compositor.
4816 * \param head The head to register, must not be already registered.
4817 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004818 * This signals the core that a new head has become available, leading to
4819 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004820 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004821 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004822 * \internal
4823 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004824WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004825weston_compositor_add_head(struct weston_compositor *compositor,
4826 struct weston_head *head)
4827{
4828 assert(wl_list_empty(&head->compositor_link));
4829 assert(head->name);
4830
4831 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4832 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004833 weston_compositor_schedule_heads_changed(compositor);
4834}
4835
4836/** Adds a listener to be called when heads change
4837 *
4838 * \param compositor The compositor.
4839 * \param listener The listener to add.
4840 *
Marius Vlada2dace22019-06-12 16:05:44 +03004841 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004842 *
4843 * The listener function will be called after heads are added or their
4844 * connection status has changed. Several changes may be accumulated into a
4845 * single call. The user is expected to iterate over the existing heads and
4846 * check their statuses to find out what changed.
4847 *
4848 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4849 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004850 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004851 */
4852WL_EXPORT void
4853weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4854 struct wl_listener *listener)
4855{
4856 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004857}
4858
4859/** Iterate over available heads
4860 *
4861 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03004862 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004863 * \return The next available head in the list.
4864 *
4865 * Returns all available heads, regardless of being connected or enabled.
4866 *
4867 * You can iterate over all heads as follows:
4868 * \code
4869 * struct weston_head *head = NULL;
4870 *
4871 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4872 * ...
4873 * }
4874 * \endcode
4875 *
4876 * If you cause \c iter to be removed from the list, you cannot use it to
4877 * continue iterating. Removing any other item is safe.
4878 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004879 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004880 */
4881WL_EXPORT struct weston_head *
4882weston_compositor_iterate_heads(struct weston_compositor *compositor,
4883 struct weston_head *iter)
4884{
4885 struct wl_list *list = &compositor->head_list;
4886 struct wl_list *node;
4887
4888 assert(compositor);
4889 assert(!iter || iter->compositor == compositor);
4890
4891 if (iter)
4892 node = iter->compositor_link.next;
4893 else
4894 node = list->next;
4895
4896 assert(node);
4897 assert(!iter || node != &iter->compositor_link);
4898
4899 if (node == list)
4900 return NULL;
4901
4902 return container_of(node, struct weston_head, compositor_link);
4903}
4904
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004905/** Iterate over attached heads
4906 *
4907 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03004908 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004909 * \return The next attached head in the list.
4910 *
4911 * Returns all heads currently attached to the output.
4912 *
4913 * You can iterate over all heads as follows:
4914 * \code
4915 * struct weston_head *head = NULL;
4916 *
4917 * while ((head = weston_output_iterate_heads(output, head))) {
4918 * ...
4919 * }
4920 * \endcode
4921 *
4922 * If you cause \c iter to be removed from the list, you cannot use it to
4923 * continue iterating. Removing any other item is safe.
4924 *
Marius Vlad55d87362019-06-11 01:15:35 +03004925 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004926 */
4927WL_EXPORT struct weston_head *
4928weston_output_iterate_heads(struct weston_output *output,
4929 struct weston_head *iter)
4930{
4931 struct wl_list *list = &output->head_list;
4932 struct wl_list *node;
4933
4934 assert(output);
4935 assert(!iter || iter->output == output);
4936
4937 if (iter)
4938 node = iter->output_link.next;
4939 else
4940 node = list->next;
4941
4942 assert(node);
4943 assert(!iter || node != &iter->output_link);
4944
4945 if (node == list)
4946 return NULL;
4947
4948 return container_of(node, struct weston_head, output_link);
4949}
4950
Pekka Paalanendcac3512017-12-08 14:13:34 +02004951/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004952 *
4953 * \param output The output to attach to.
4954 * \param head The head that is not yet attached.
4955 * \return 0 on success, -1 on failure.
4956 *
4957 * Attaches the given head to the output. All heads of an output are clones
4958 * and share the resolution and timings.
4959 *
4960 * Cloning heads this way uses less resources than creating an output for
4961 * each head, but is not always possible due to environment, driver and hardware
4962 * limitations.
4963 *
4964 * On failure, the head remains unattached. Success of this function does not
4965 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004966 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004967 *
Marius Vlad55d87362019-06-11 01:15:35 +03004968 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004969 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004970WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004971weston_output_attach_head(struct weston_output *output,
4972 struct weston_head *head)
4973{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004974 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004975
4976 if (!wl_list_empty(&head->output_link))
4977 return -1;
4978
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004979 if (output->attach_head) {
4980 if (output->attach_head(output, head) < 0)
4981 return -1;
4982 } else if (!wl_list_empty(&output->head_list)) {
4983 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004984 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004985 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004986
4987 head->output = output;
4988 wl_list_insert(output->head_list.prev, &head->output_link);
4989
Pekka Paalanendcac3512017-12-08 14:13:34 +02004990 if (output->enabled) {
4991 weston_head_add_global(head);
4992
4993 head_names = weston_output_create_heads_string(output);
4994 weston_log("Output '%s' updated to have head(s) %s\n",
4995 output->name, head_names);
4996 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004997
4998 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02004999 }
5000
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005001 return 0;
5002}
5003
5004/** Detach a head from its output
5005 *
5006 * \param head The head to detach.
5007 *
5008 * It is safe to detach a non-attached head.
5009 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005010 * If the head is attached to an enabled output and the output will be left
5011 * with no heads, the output will be disabled.
5012 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005013 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005014 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005015 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005016WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005017weston_head_detach(struct weston_head *head)
5018{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005019 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005020 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005021
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005022 wl_list_remove(&head->output_link);
5023 wl_list_init(&head->output_link);
5024 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005025
5026 if (!output)
5027 return;
5028
5029 if (output->detach_head)
5030 output->detach_head(output, head);
5031
5032 if (output->enabled) {
5033 weston_head_remove_global(head);
5034
Pekka Paalanena0106992017-12-08 16:11:17 +02005035 if (wl_list_empty(&output->head_list)) {
5036 weston_log("Output '%s' no heads left, disabling.\n",
5037 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005038 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005039 } else {
5040 head_names = weston_output_create_heads_string(output);
5041 weston_log("Output '%s' updated to have head(s) %s\n",
5042 output->name, head_names);
5043 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005044
5045 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005046 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005047 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005048}
5049
5050/** Destroy a head
5051 *
5052 * \param head The head to be released.
5053 *
5054 * Destroys the head. The caller is responsible for freeing the memory pointed
5055 * to by \c head.
5056 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005057 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005058 * \internal
5059 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005060WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005061weston_head_release(struct weston_head *head)
5062{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005063 wl_signal_emit(&head->destroy_signal, head);
5064
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005065 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005066
5067 free(head->make);
5068 free(head->model);
5069 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005070 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005071
5072 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005073}
5074
Pekka Paalanene19970f2017-08-28 14:11:02 +03005075static void
5076weston_head_set_device_changed(struct weston_head *head)
5077{
5078 head->device_changed = true;
5079
5080 if (head->compositor)
5081 weston_compositor_schedule_heads_changed(head->compositor);
5082}
5083
5084/** String equal comparison with NULLs being equal */
5085static bool
5086str_null_eq(const char *a, const char *b)
5087{
5088 if (!a && !b)
5089 return true;
5090
5091 if (!!a != !!b)
5092 return false;
5093
5094 return strcmp(a, b) == 0;
5095}
5096
Pekka Paalanen01f60212017-03-24 15:39:24 +02005097/** Store monitor make, model and serial number
5098 *
5099 * \param head The head to modify.
5100 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5101 * any string, or NULL for none.
5102 * \param model The monitor model or name, or a made-up string, or NULL for
5103 * none.
5104 * \param serialno The monitor serial number, a made-up string, or NULL for
5105 * none.
5106 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005107 * This may set the device_changed flag.
5108 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005109 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005110 * \internal
5111 */
5112WL_EXPORT void
5113weston_head_set_monitor_strings(struct weston_head *head,
5114 const char *make,
5115 const char *model,
5116 const char *serialno)
5117{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005118 if (str_null_eq(head->make, make) &&
5119 str_null_eq(head->model, model) &&
5120 str_null_eq(head->serial_number, serialno))
5121 return;
5122
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005123 free(head->make);
5124 free(head->model);
5125 free(head->serial_number);
5126
5127 head->make = make ? strdup(make) : NULL;
5128 head->model = model ? strdup(model) : NULL;
5129 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005130
5131 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005132}
5133
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005134/** Store display non-desktop status
5135 *
5136 * \param head The head to modify.
5137 * \param non_desktop Whether the head connects to a non-desktop display.
5138 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005139 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005140 * \internal
5141 */
5142WL_EXPORT void
5143weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5144{
5145 if (head->non_desktop == non_desktop)
5146 return;
5147
5148 head->non_desktop = non_desktop;
5149
5150 weston_head_set_device_changed(head);
5151}
5152
Pekka Paalanen01f60212017-03-24 15:39:24 +02005153/** Store physical image size
5154 *
5155 * \param head The head to modify.
5156 * \param mm_width Image area width in millimeters.
5157 * \param mm_height Image area height in millimeters.
5158 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005159 * This may set the device_changed flag.
5160 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005161 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005162 * \internal
5163 */
5164WL_EXPORT void
5165weston_head_set_physical_size(struct weston_head *head,
5166 int32_t mm_width, int32_t mm_height)
5167{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005168 if (head->mm_width == mm_width &&
5169 head->mm_height == mm_height)
5170 return;
5171
Pekka Paalanen01f60212017-03-24 15:39:24 +02005172 head->mm_width = mm_width;
5173 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005174
5175 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005176}
5177
5178/** Store monitor sub-pixel layout
5179 *
5180 * \param head The head to modify.
5181 * \param sp Sub-pixel layout. The possible values are:
5182 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5183 * - WL_OUTPUT_SUBPIXEL_NONE,
5184 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5185 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5186 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5187 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5188 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005189 * This may set the device_changed flag.
5190 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005191 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005192 * \internal
5193 */
5194WL_EXPORT void
5195weston_head_set_subpixel(struct weston_head *head,
5196 enum wl_output_subpixel sp)
5197{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005198 if (head->subpixel == sp)
5199 return;
5200
Pekka Paalanen01f60212017-03-24 15:39:24 +02005201 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005202
5203 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005204}
5205
5206/** Mark the monitor as internal
5207 *
5208 * This is used for embedded screens, like laptop panels.
5209 *
5210 * \param head The head to mark as internal.
5211 *
5212 * By default a head is external. The type is often inferred from the physical
5213 * connector type.
5214 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005215 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005216 * \internal
5217 */
5218WL_EXPORT void
5219weston_head_set_internal(struct weston_head *head)
5220{
5221 head->connection_internal = true;
5222}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005223
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005224/** Store connector status
5225 *
5226 * \param head The head to modify.
5227 * \param connected Whether the head is connected.
5228 *
5229 * Connectors are created as disconnected. This function can be used to
5230 * set the connector status.
5231 *
5232 * The status should be set to true when a physical connector is connected to
5233 * a video sink device like a monitor and to false when the connector is
5234 * disconnected. For nested backends, the connection status should reflect the
5235 * connection to the parent display server.
5236 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005237 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005238 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005239 *
5240 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005241 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005242 * \internal
5243 */
5244WL_EXPORT void
5245weston_head_set_connection_status(struct weston_head *head, bool connected)
5246{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005247 if (head->connected == connected)
5248 return;
5249
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005250 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005251
Pekka Paalanene19970f2017-08-28 14:11:02 +03005252 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005253}
5254
5255/** Is the head currently connected?
5256 *
5257 * \param head The head to query.
5258 * \return Connection status.
5259 *
5260 * Returns true if the head is physically connected to a monitor, or in
5261 * case of a nested backend returns true when there is a connection to the
5262 * parent display server.
5263 *
5264 * This is independent from the head being enabled.
5265 *
5266 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005267 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005268 */
5269WL_EXPORT bool
5270weston_head_is_connected(struct weston_head *head)
5271{
5272 return head->connected;
5273}
5274
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005275/** Is the head currently enabled?
5276 *
5277 * \param head The head to query.
5278 * \return Video status.
5279 *
5280 * Returns true if the head is currently transmitting a video stream.
5281 *
5282 * This is independent of the head being connected.
5283 *
5284 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005285 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005286 */
5287WL_EXPORT bool
5288weston_head_is_enabled(struct weston_head *head)
5289{
5290 if (!head->output)
5291 return false;
5292
5293 return head->output->enabled;
5294}
5295
Pekka Paalanene19970f2017-08-28 14:11:02 +03005296/** Has the device information changed?
5297 *
5298 * \param head The head to query.
5299 * \return True if the device information has changed since last reset.
5300 *
5301 * The information about the connected display device, e.g. a monitor, may
5302 * change without being disconnected in between. Changing information
5303 * causes a call to the heads_changed hook.
5304 *
5305 * The information includes make, model, serial number, physical size,
5306 * and sub-pixel type. The connection status is also included.
5307 *
5308 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005309 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005310 */
5311WL_EXPORT bool
5312weston_head_is_device_changed(struct weston_head *head)
5313{
5314 return head->device_changed;
5315}
5316
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005317/** Does the head represent a non-desktop display?
5318 *
5319 * \param head The head to query.
5320 * \return True if the device is a non-desktop display.
5321 *
5322 * Non-desktop heads are not attached to outputs by default.
5323 * This stops weston from extending the desktop onto head mounted displays.
5324 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005325 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005326 */
5327WL_EXPORT bool
5328weston_head_is_non_desktop(struct weston_head *head)
5329{
5330 return head->non_desktop;
5331}
5332
Pekka Paalanene19970f2017-08-28 14:11:02 +03005333/** Acknowledge device information change
5334 *
5335 * \param head The head to acknowledge.
5336 *
5337 * Clears the device changed flag on this head. When a compositor has processed
5338 * device information, it should call this to be able to notice further
5339 * changes.
5340 *
5341 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005342 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005343 */
5344WL_EXPORT void
5345weston_head_reset_device_changed(struct weston_head *head)
5346{
5347 head->device_changed = false;
5348}
5349
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005350/** Get the name of a head
5351 *
5352 * \param head The head to query.
5353 * \return The head's name, not NULL.
5354 *
5355 * The name depends on the backend. The DRM backend uses connector names,
5356 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005357 *
5358 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005359 */
5360WL_EXPORT const char *
5361weston_head_get_name(struct weston_head *head)
5362{
5363 return head->name;
5364}
5365
5366/** Get the output the head is attached to
5367 *
5368 * \param head The head to query.
5369 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005370 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005371 */
5372WL_EXPORT struct weston_output *
5373weston_head_get_output(struct weston_head *head)
5374{
5375 return head->output;
5376}
5377
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005378/** Add destroy callback for a head
5379 *
5380 * \param head The head to watch for.
5381 * \param listener The listener to add. The \c notify member must be set.
5382 *
5383 * Heads may get destroyed for various reasons by the backends. If a head is
5384 * attached to an output, the compositor should listen for head destruction
5385 * and reconfigure or destroy the output if necessary.
5386 *
5387 * The destroy callbacks will be called on weston_head destruction before any
5388 * automatic detaching from an associated weston_output and before any
5389 * weston_head information is lost.
5390 *
5391 * The \c data argument to the notify callback is the weston_head being
5392 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005393 *
5394 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005395 */
5396WL_EXPORT void
5397weston_head_add_destroy_listener(struct weston_head *head,
5398 struct wl_listener *listener)
5399{
5400 wl_signal_add(&head->destroy_signal, listener);
5401}
5402
5403/** Look up destroy listener for a head
5404 *
5405 * \param head The head to query.
5406 * \param notify The notify function used used for the added destroy listener.
5407 * \return The listener, or NULL if not found.
5408 *
5409 * This looks up the previously added destroy listener struct based on the
5410 * notify function it has. The listener can be used to access user data
5411 * through \c container_of().
5412 *
5413 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005414 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005415 */
5416WL_EXPORT struct wl_listener *
5417weston_head_get_destroy_listener(struct weston_head *head,
5418 wl_notify_func_t notify)
5419{
5420 return wl_signal_get(&head->destroy_signal, notify);
5421}
5422
David Fort0de859e2016-05-27 23:22:57 +02005423/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005424static void
David Fort0de859e2016-05-27 23:22:57 +02005425weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5426 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005427{
5428 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005429 bool start_resizing = false;
5430
5431 if (!delta_width)
5432 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005433
5434 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005435 if (output == resized_output) {
5436 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005437 continue;
5438 }
5439
David Fort0de859e2016-05-27 23:22:57 +02005440 if (start_resizing) {
5441 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005442 output->dirty = 1;
5443 }
5444 }
5445}
5446
Pekka Paalanend72bad22017-03-29 17:01:41 +03005447static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005448weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005449{
Scott Moreau850ca422012-05-21 15:21:25 -06005450 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005451
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005452 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005453 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005454
Scott Moreauccbf29d2012-02-22 14:21:41 -07005455 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005456 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005457 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005458 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005459 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005460 weston_matrix_scale(&output->matrix, magnification,
5461 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005462 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005463
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005464 switch (output->transform) {
5465 case WL_OUTPUT_TRANSFORM_FLIPPED:
5466 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5467 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5468 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5469 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5470 weston_matrix_scale(&output->matrix, -1, 1, 1);
5471 break;
5472 }
5473
5474 switch (output->transform) {
5475 default:
5476 case WL_OUTPUT_TRANSFORM_NORMAL:
5477 case WL_OUTPUT_TRANSFORM_FLIPPED:
5478 break;
5479 case WL_OUTPUT_TRANSFORM_90:
5480 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5481 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5482 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5483 break;
5484 case WL_OUTPUT_TRANSFORM_180:
5485 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5486 weston_matrix_translate(&output->matrix,
5487 -output->width, -output->height, 0);
5488 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5489 break;
5490 case WL_OUTPUT_TRANSFORM_270:
5491 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5492 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5493 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5494 break;
5495 }
5496
5497 if (output->current_scale != 1)
5498 weston_matrix_scale(&output->matrix,
5499 output->current_scale,
5500 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005501
Scott Moreauccbf29d2012-02-22 14:21:41 -07005502 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005503
5504 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005505}
5506
Scott Moreau1bad5db2012-08-18 01:04:05 -06005507static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005508weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005509{
5510 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005511 output->native_scale = scale;
5512 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005513
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005514 convert_size_by_transform_scale(&output->width, &output->height,
5515 output->current_mode->width,
5516 output->current_mode->height,
5517 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005518}
5519
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005520static void
5521weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005522{
5523 output->x = x;
5524 output->y = y;
5525
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005526 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005527 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005528
5529 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005530 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005531 output->width,
5532 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005533}
5534
Marius Vlad55d87362019-06-11 01:15:35 +03005535/**
5536 * \ingroup output
5537 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005538WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005539weston_output_move(struct weston_output *output, int x, int y)
5540{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005541 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005542 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005543 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005544
5545 output->move_x = x - output->x;
5546 output->move_y = y - output->y;
5547
5548 if (output->move_x == 0 && output->move_y == 0)
5549 return;
5550
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005551 weston_output_init_geometry(output, x, y);
5552
5553 output->dirty = 1;
5554
5555 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005556 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005557
5558 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005559 wl_list_for_each(head, &output->head_list, output_link) {
5560 wl_resource_for_each(resource, &head->resource_list) {
5561 wl_output_send_geometry(resource,
5562 output->x,
5563 output->y,
5564 head->mm_width,
5565 head->mm_height,
5566 head->subpixel,
5567 head->make,
5568 head->model,
5569 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005570
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005571 ver = wl_resource_get_version(resource);
5572 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5573 wl_output_send_done(resource);
5574 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005575 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005576}
5577
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005578/** Signal that a pending output is taken into use.
5579 *
5580 * Removes the output from the pending list and adds it to the compositor's
5581 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005582 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005583 * The output gets an internal ID assigned, and the wl_output global is
5584 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005585 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005586 * \param compositor The compositor instance.
5587 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005588 *
5589 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005590 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005591 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005592static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005593weston_compositor_add_output(struct weston_compositor *compositor,
5594 struct weston_output *output)
5595{
Armin Krezoviće5403842016-08-05 15:28:29 +02005596 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005597 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005598
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005599 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005600
5601 /* Verify we haven't reached the limit of 32 available output IDs */
5602 assert(ffs(~compositor->output_id_pool) > 0);
5603
5604 /* Invert the output id pool and look for the lowest numbered
5605 * switch (the least significant bit). Take that bit's position
5606 * as our ID, and mark it used in the compositor's output_id_pool.
5607 */
5608 output->id = ffs(~compositor->output_id_pool) - 1;
5609 compositor->output_id_pool |= 1u << output->id;
5610
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005611 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005612 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005613 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005614
Pekka Paalanendcac3512017-12-08 14:13:34 +02005615 wl_list_for_each(head, &output->head_list, output_link)
5616 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005617
Giulio Camuffob1147152015-05-06 21:41:57 +03005618 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005619
5620 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5621 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005622}
5623
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005624/** Transform device coordinates into global coordinates
5625 *
Marius Vlada2dace22019-06-12 16:05:44 +03005626 * \param output the weston_output object
5627 * \param[in] device_x X coordinate in device units.
5628 * \param[in] device_y Y coordinate in device units.
5629 * \param[out] x X coordinate in the global space.
5630 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005631 *
Marius Vlada2dace22019-06-12 16:05:44 +03005632 * Transforms coordinates from the device coordinate space (physical pixel
5633 * units) to the global coordinate space (logical pixel units). This takes
5634 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005635 *
Marius Vlad55d87362019-06-11 01:15:35 +03005636 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005637 * \internal
5638 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005639WL_EXPORT void
5640weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005641 double device_x, double device_y,
5642 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005643{
Derek Foreman0f679412014-10-02 13:41:17 -05005644 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005645 device_x,
5646 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005647 0.0,
5648 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005649
Derek Foreman67a18b92015-03-24 11:36:14 -05005650 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005651
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005652 *x = p.f[0] / p.f[3];
5653 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005654}
5655
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005656/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005657 *
5658 * \param output The weston_output object that is being removed.
5659 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005660 * The following happens:
5661 *
5662 * - The output assignments of all views in the current scenegraph are
5663 * recomputed.
5664 *
5665 * - Presentation feedback is discarded.
5666 *
5667 * - Compositor is notified that outputs were changed and
5668 * applies the necessary changes to re-layout outputs.
5669 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005670 * - The output is put back in the pending outputs list.
5671 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005672 * - Signal is emitted to notify all users of the weston_output
5673 * object that the output is being destroyed.
5674 *
5675 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005676 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005677 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005678 * - The output's internal ID is released.
5679 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005680 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005681 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005682 */
5683static void
5684weston_compositor_remove_output(struct weston_output *output)
5685{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005686 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005687 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005688 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005689
5690 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005691 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005692
Pekka Paalanenbccda712017-03-29 16:16:04 +03005693 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005694 if (view->output_mask & (1u << output->id))
5695 weston_view_assign_output(view);
5696 }
5697
5698 weston_presentation_feedback_discard_list(&output->feedback_list);
5699
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005700 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005701
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005702 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005703 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005704 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005705
Pekka Paalanenbccda712017-03-29 16:16:04 +03005706 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005707 wl_signal_emit(&output->destroy_signal, output);
5708
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005709 wl_list_for_each(head, &output->head_list, output_link)
5710 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005711
5712 compositor->output_id_pool &= ~(1u << output->id);
5713 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005714}
5715
5716/** Sets the output scale for a given output.
5717 *
5718 * \param output The weston_output object that the scale is set for.
5719 * \param scale Scale factor for the given output.
5720 *
5721 * It only supports setting scale for an output that
5722 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005723 *
Marius Vlad55d87362019-06-11 01:15:35 +03005724 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005725 */
5726WL_EXPORT void
5727weston_output_set_scale(struct weston_output *output,
5728 int32_t scale)
5729{
5730 /* We can only set scale on a disabled output */
5731 assert(!output->enabled);
5732
5733 /* We only want to set scale once */
5734 assert(!output->scale);
5735
5736 output->scale = scale;
5737}
5738
5739/** Sets the output transform for a given output.
5740 *
5741 * \param output The weston_output object that the transform is set for.
5742 * \param transform Transform value for the given output.
5743 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005744 * Refer to wl_output::transform section located at
5745 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5746 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005747 *
Marius Vlad55d87362019-06-11 01:15:35 +03005748 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005749 */
5750WL_EXPORT void
5751weston_output_set_transform(struct weston_output *output,
5752 uint32_t transform)
5753{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005754 struct weston_pointer_motion_event ev;
5755 struct wl_resource *resource;
5756 struct weston_seat *seat;
5757 pixman_region32_t old_region;
5758 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005759 struct weston_head *head;
5760 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005761
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005762 if (!output->enabled && output->transform == UINT32_MAX) {
5763 output->transform = transform;
5764 return;
5765 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005766
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005767 weston_output_transform_scale_init(output, transform, output->scale);
5768
5769 pixman_region32_init(&old_region);
5770 pixman_region32_copy(&old_region, &output->region);
5771
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005772 weston_output_init_geometry(output, output->x, output->y);
5773
5774 output->dirty = 1;
5775
5776 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005777 wl_list_for_each(head, &output->head_list, output_link) {
5778 wl_resource_for_each(resource, &head->resource_list) {
5779 wl_output_send_geometry(resource,
5780 output->x,
5781 output->y,
5782 head->mm_width,
5783 head->mm_height,
5784 head->subpixel,
5785 head->make,
5786 head->model,
5787 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005788
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005789 ver = wl_resource_get_version(resource);
5790 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5791 wl_output_send_done(resource);
5792 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005793 }
5794
5795 /* we must ensure that pointers are inside output, otherwise they disappear */
5796 mid_x = output->x + output->width / 2;
5797 mid_y = output->y + output->height / 2;
5798
5799 ev.mask = WESTON_POINTER_MOTION_ABS;
5800 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5801 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5802
5803 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5804 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5805
5806 if (pointer && pixman_region32_contains_point(&old_region,
5807 wl_fixed_to_int(pointer->x),
5808 wl_fixed_to_int(pointer->y),
5809 NULL))
5810 weston_pointer_move(pointer, &ev);
5811 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005812}
5813
5814/** Initializes a weston_output object with enough data so
5815 ** an output can be configured.
5816 *
5817 * \param output The weston_output object to initialize
5818 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005819 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005820 *
5821 * Sets initial values for fields that are expected to be
5822 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005823 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005824 * The name is used in logs, and can be used by compositors as a configuration
5825 * identifier.
5826 *
Marius Vlad55d87362019-06-11 01:15:35 +03005827 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005828 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005829 */
5830WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005831weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005832 struct weston_compositor *compositor,
5833 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005834{
5835 output->compositor = compositor;
5836 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005837 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005838 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005839 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005840 output->enabled = false;
5841
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005842 wl_list_init(&output->head_list);
5843
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005844 /* Add some (in)sane defaults which can be used
5845 * for checking if an output was properly configured
5846 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005847 output->scale = 0;
5848 /* Can't use -1 on uint32_t and 0 is valid enum value */
5849 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005850
5851 pixman_region32_init(&output->previous_damage);
5852 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005853 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005854}
5855
5856/** Adds weston_output object to pending output list.
5857 *
5858 * \param output The weston_output object to add
5859 * \param compositor The compositor instance.
5860 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005861 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005862 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005863 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005864 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005865 */
5866WL_EXPORT void
5867weston_compositor_add_pending_output(struct weston_output *output,
5868 struct weston_compositor *compositor)
5869{
Pekka Paalanene952a012017-03-29 17:14:00 +03005870 assert(output->disable);
5871 assert(output->enable);
5872
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005873 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005874 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005875}
5876
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005877/** Create a string with the attached heads' names.
5878 *
5879 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03005880 *
5881 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005882 */
5883static char *
5884weston_output_create_heads_string(struct weston_output *output)
5885{
5886 FILE *fp;
5887 char *str = NULL;
5888 size_t size = 0;
5889 struct weston_head *head;
5890 const char *sep = "";
5891
5892 fp = open_memstream(&str, &size);
5893 if (!fp)
5894 return NULL;
5895
5896 wl_list_for_each(head, &output->head_list, output_link) {
5897 fprintf(fp, "%s%s", sep, head->name);
5898 sep = ", ";
5899 }
5900 fclose(fp);
5901
5902 return str;
5903}
5904
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005905/** Constructs a weston_output object that can be used by the compositor.
5906 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005907 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005908 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005909 *
5910 * Output coordinates are calculated and each new output is by default
5911 * assigned to the right of previous one.
5912 *
5913 * Sets up the transformation, zoom, and geometry of the output using
5914 * the properties that need to be configured by the compositor.
5915 *
5916 * Establishes a repaint timer for the output with the relevant display
5917 * object's event loop. See output_repaint_timer_handler().
5918 *
5919 * The output is assigned an ID. Weston can support up to 32 distinct
5920 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5921 * is referred to and used to find the first available ID number, and
5922 * then this ID is marked as used in output_id_pool.
5923 *
5924 * The output is also assigned a Wayland global with the wl_output
5925 * external interface.
5926 *
5927 * Backend specific function is called to set up the output output.
5928 *
5929 * Output is added to the compositor's output list
5930 *
5931 * If the backend specific function fails, the weston_output object
5932 * is returned to a state it was before calling this function and
5933 * is added to the compositor's pending_output_list in case it needs
5934 * to be reconfigured or just so it can be destroyed at shutdown.
5935 *
5936 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03005937 *
5938 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005939 */
5940WL_EXPORT int
5941weston_output_enable(struct weston_output *output)
5942{
Armin Krezović782f5df2016-09-30 14:11:11 +02005943 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005944 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005945 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005946 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005947 int x = 0, y = 0;
5948
Pekka Paalanencc201e42017-03-30 15:11:25 +03005949 if (output->enabled) {
5950 weston_log("Error: attempt to enable an enabled output '%s'\n",
5951 output->name);
5952 return -1;
5953 }
5954
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005955 if (wl_list_empty(&output->head_list)) {
5956 weston_log("Error: cannot enable output '%s' without heads.\n",
5957 output->name);
5958 return -1;
5959 }
5960
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005961 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5962 weston_log("Error: no video mode for output '%s'.\n",
5963 output->name);
5964 return -1;
5965 }
5966
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005967 wl_list_for_each(head, &output->head_list, output_link) {
5968 assert(head->make);
5969 assert(head->model);
5970 }
5971
Armin Krezović782f5df2016-09-30 14:11:11 +02005972 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005973 struct weston_output, link);
5974
Armin Krezović782f5df2016-09-30 14:11:11 +02005975 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005976 x = iterator->x + iterator->width;
5977
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005978 /* Make sure the scale is set up */
5979 assert(output->scale);
5980
5981 /* Make sure we have a transform set */
5982 assert(output->transform != UINT32_MAX);
5983
Armin Krezović782f5df2016-09-30 14:11:11 +02005984 output->x = x;
5985 output->y = y;
5986 output->dirty = 1;
5987 output->original_scale = output->scale;
5988
5989 weston_output_transform_scale_init(output, output->transform, output->scale);
5990 weston_output_init_zoom(output);
5991
5992 weston_output_init_geometry(output, x, y);
5993 weston_output_damage(output);
5994
5995 wl_signal_init(&output->frame_signal);
5996 wl_signal_init(&output->destroy_signal);
5997 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005998 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005999
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006000 /* Enable the output (set up the crtc or create a
6001 * window representing the output, set up the
6002 * renderer, etc)
6003 */
6004 if (output->enable(output) < 0) {
6005 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006006 return -1;
6007 }
6008
6009 weston_compositor_add_output(output->compositor, output);
6010
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006011 head_names = weston_output_create_heads_string(output);
6012 weston_log("Output '%s' enabled with head(s) %s\n",
6013 output->name, head_names);
6014 free(head_names);
6015
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006016 return 0;
6017}
6018
6019/** Converts a weston_output object to a pending output state, so it
6020 ** can be configured again or destroyed.
6021 *
6022 * \param output The weston_output object that needs to be disabled.
6023 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006024 * Calls a backend specific function to disable an output, in case
6025 * such function exists.
6026 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006027 * The backend specific disable function may choose to postpone the disabling
6028 * by returning a negative value, in which case this function returns early.
6029 * In that case the backend will guarantee the output will be disabled soon
6030 * by the backend calling this function again. One must not attempt to re-enable
6031 * the output until that happens.
6032 *
6033 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006034 * from weston's output_list (see weston_compositor_remove_output())
6035 * and is returned to a state it was before weston_output_enable()
6036 * was ran (see weston_output_enable_undo()).
6037 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006038 * See weston_output_init() for more information on the
6039 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006040 *
6041 * If the output has never been enabled yet, this function can still be
6042 * called to ensure that the output is actually turned off rather than left
6043 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006044 *
6045 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006046 */
6047WL_EXPORT void
6048weston_output_disable(struct weston_output *output)
6049{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006050 /* Should we rename this? */
6051 output->destroying = 1;
6052
Pekka Paalanenc65df642017-03-29 15:45:46 +03006053 /* Disable is called unconditionally also for not-enabled outputs,
6054 * because at compositor start-up, if there is an output that is
6055 * already on but the compositor wants to turn it off, we have to
6056 * forward the turn-off to the backend so it knows to do it.
6057 * The backend cannot initially turn off everything, because it
6058 * would cause unnecessary mode-sets for all outputs the compositor
6059 * wants to be on.
6060 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006061 if (output->disable(output) < 0)
6062 return;
6063
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006064 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006065 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006066
6067 output->destroying = 0;
6068}
6069
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006070/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006071 *
6072 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006073 *
6074 * If there are new or changed heads, calls the heads_changed hook and
6075 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006076 *
6077 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006078 */
6079WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006080weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006081{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006082 if (compositor->heads_changed_source) {
6083 wl_event_source_remove(compositor->heads_changed_source);
6084 weston_compositor_call_heads_changed(compositor);
6085 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006086}
6087
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006088/** Add destroy callback for an output
6089 *
6090 * \param output The output to watch.
6091 * \param listener The listener to add. The \c notify member must be set.
6092 *
6093 * The listener callback will be called when user destroys an output. This
6094 * may be delayed by a backend in some cases. The main purpose of the
6095 * listener is to allow hooking up custom data to the output. The custom data
6096 * can be fetched via weston_output_get_destroy_listener() followed by
6097 * container_of().
6098 *
6099 * The \c data argument to the notify callback is the weston_output being
6100 * destroyed.
6101 *
6102 * @note This is for the final destruction of an output, not when it gets
6103 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006104 *
6105 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006106 */
6107WL_EXPORT void
6108weston_output_add_destroy_listener(struct weston_output *output,
6109 struct wl_listener *listener)
6110{
6111 wl_signal_add(&output->user_destroy_signal, listener);
6112}
6113
6114/** Look up destroy listener for an output
6115 *
6116 * \param output The output to query.
6117 * \param notify The notify function used used for the added destroy listener.
6118 * \return The listener, or NULL if not found.
6119 *
6120 * This looks up the previously added destroy listener struct based on the
6121 * notify function it has. The listener can be used to access user data
6122 * through \c container_of().
6123 *
6124 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006125 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006126 */
6127WL_EXPORT struct wl_listener *
6128weston_output_get_destroy_listener(struct weston_output *output,
6129 wl_notify_func_t notify)
6130{
6131 return wl_signal_get(&output->user_destroy_signal, notify);
6132}
6133
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006134/** Uninitialize an output
6135 *
6136 * Removes the output from the list of enabled outputs if necessary, but
6137 * does not call the backend's output disable function. The output will no
6138 * longer be in the list of pending outputs either.
6139 *
6140 * All fields of weston_output become uninitialized, i.e. should not be used
6141 * anymore. The caller can free the memory after this.
6142 *
Marius Vlad55d87362019-06-11 01:15:35 +03006143 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006144 * \internal
6145 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006146WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006147weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006148{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006149 struct weston_head *head, *tmp;
6150
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006151 output->destroying = 1;
6152
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006153 wl_signal_emit(&output->user_destroy_signal, output);
6154
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006155 if (output->idle_repaint_source)
6156 wl_event_source_remove(output->idle_repaint_source);
6157
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006158 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006159 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006160
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006161 pixman_region32_fini(&output->region);
6162 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006163 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006164
6165 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6166 weston_head_detach(head);
6167
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006168 free(output->name);
6169}
6170
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006171/** Find an output by its given name
6172 *
6173 * \param compositor The compositor to search in.
6174 * \param name The output name to search for.
6175 * \return An existing output with the given name, or NULL if not found.
6176 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006177 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006178 */
6179WL_EXPORT struct weston_output *
6180weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6181 const char *name)
6182{
6183 struct weston_output *output;
6184
6185 wl_list_for_each(output, &compositor->output_list, link)
6186 if (strcmp(output->name, name) == 0)
6187 return output;
6188
6189 wl_list_for_each(output, &compositor->pending_output_list, link)
6190 if (strcmp(output->name, name) == 0)
6191 return output;
6192
6193 return NULL;
6194}
6195
6196/** Create a named output
6197 *
6198 * \param compositor The compositor.
6199 * \param name The name for the output.
6200 * \return A new \c weston_output, or NULL on failure.
6201 *
6202 * This creates a new weston_output that starts with no heads attached.
6203 *
6204 * An output must be configured and it must have at least one head before
6205 * it can be enabled.
6206 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006207 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006208 */
6209WL_EXPORT struct weston_output *
6210weston_compositor_create_output(struct weston_compositor *compositor,
6211 const char *name)
6212{
6213 assert(compositor->backend->create_output);
6214
6215 if (weston_compositor_find_output_by_name(compositor, name)) {
6216 weston_log("Warning: attempted to create an output with a "
6217 "duplicate name '%s'.\n", name);
6218 return NULL;
6219 }
6220
6221 return compositor->backend->create_output(compositor, name);
6222}
6223
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006224/** Create an output for an unused head
6225 *
6226 * \param compositor The compositor.
6227 * \param head The head to attach to the output.
6228 * \return A new \c weston_output, or NULL on failure.
6229 *
6230 * This creates a new weston_output that starts with the given head attached.
6231 * The output inherits the name of the head. The head must not be already
6232 * attached to another output.
6233 *
6234 * An output must be configured before it can be enabled.
6235 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006236 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006237 */
6238WL_EXPORT struct weston_output *
6239weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6240 struct weston_head *head)
6241{
6242 struct weston_output *output;
6243
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006244 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006245 if (!output)
6246 return NULL;
6247
6248 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006249 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006250 return NULL;
6251 }
6252
6253 return output;
6254}
6255
6256/** Destroy an output
6257 *
6258 * \param output The output to destroy.
6259 *
6260 * The heads attached to the given output are detached and become unused again.
6261 *
6262 * It is not necessary to explicitly destroy all outputs at compositor exit.
6263 * weston_compositor_destroy() will automatically destroy any remaining
6264 * outputs.
6265 *
Marius Vlad55d87362019-06-11 01:15:35 +03006266 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006267 */
6268WL_EXPORT void
6269weston_output_destroy(struct weston_output *output)
6270{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006271 output->destroy(output);
6272}
6273
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006274/** When you need a head...
6275 *
6276 * This function is a hack, used until all code has been converted to become
6277 * multi-head aware.
6278 *
6279 * \param output The weston_output whose head to get.
6280 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006281 *
6282 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006283 */
6284WL_EXPORT struct weston_head *
6285weston_output_get_first_head(struct weston_output *output)
6286{
6287 if (wl_list_empty(&output->head_list))
6288 return NULL;
6289
6290 return container_of(output->head_list.next,
6291 struct weston_head, output_link);
6292}
6293
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006294static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006295destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006296{
Jonny Lamb74130762013-11-26 18:19:46 +01006297 struct weston_surface *surface =
6298 wl_resource_get_user_data(resource);
6299
Pekka Paalanen4826f872016-04-22 14:14:38 +03006300 if (!surface)
6301 return;
6302
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006303 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006304 surface->pending.buffer_viewport.buffer.src_width =
6305 wl_fixed_from_int(-1);
6306 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006307 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006308}
6309
6310static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006311viewport_destroy(struct wl_client *client,
6312 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006313{
6314 wl_resource_destroy(resource);
6315}
6316
6317static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006318viewport_set_source(struct wl_client *client,
6319 struct wl_resource *resource,
6320 wl_fixed_t src_x,
6321 wl_fixed_t src_y,
6322 wl_fixed_t src_width,
6323 wl_fixed_t src_height)
6324{
6325 struct weston_surface *surface =
6326 wl_resource_get_user_data(resource);
6327
Pekka Paalanen4826f872016-04-22 14:14:38 +03006328 if (!surface) {
6329 wl_resource_post_error(resource,
6330 WP_VIEWPORT_ERROR_NO_SURFACE,
6331 "wl_surface for this viewport is no longer exists");
6332 return;
6333 }
6334
6335 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006336 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006337
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006338 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006339 src_height == wl_fixed_from_int(-1) &&
6340 src_x == wl_fixed_from_int(-1) &&
6341 src_y == wl_fixed_from_int(-1)) {
6342 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006343 surface->pending.buffer_viewport.buffer.src_width =
6344 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006345 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006346 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006347 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006348
Pekka Paalanen201769a2016-04-26 14:42:11 +03006349 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006350 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006351 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006352 "wl_surface@%d viewport source "
6353 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6354 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006355 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006356 wl_fixed_to_double(src_height),
6357 wl_fixed_to_double(src_x),
6358 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006359 return;
6360 }
6361
6362 surface->pending.buffer_viewport.buffer.src_x = src_x;
6363 surface->pending.buffer_viewport.buffer.src_y = src_y;
6364 surface->pending.buffer_viewport.buffer.src_width = src_width;
6365 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006366 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006367}
6368
6369static void
6370viewport_set_destination(struct wl_client *client,
6371 struct wl_resource *resource,
6372 int32_t dst_width,
6373 int32_t dst_height)
6374{
6375 struct weston_surface *surface =
6376 wl_resource_get_user_data(resource);
6377
Pekka Paalanen4826f872016-04-22 14:14:38 +03006378 if (!surface) {
6379 wl_resource_post_error(resource,
6380 WP_VIEWPORT_ERROR_NO_SURFACE,
6381 "wl_surface for this viewport no longer exists");
6382 return;
6383 }
6384
6385 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006386
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006387 if (dst_width == -1 && dst_height == -1) {
6388 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006389 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006390 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006391 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006392 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006393
6394 if (dst_width <= 0 || dst_height <= 0) {
6395 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006396 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006397 "destination size must be positive (%dx%d)",
6398 dst_width, dst_height);
6399 return;
6400 }
6401
6402 surface->pending.buffer_viewport.surface.width = dst_width;
6403 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006404 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006405}
6406
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006407static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006408 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006409 viewport_set_source,
6410 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006411};
6412
6413static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006414viewporter_destroy(struct wl_client *client,
6415 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006416{
6417 wl_resource_destroy(resource);
6418}
6419
6420static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006421viewporter_get_viewport(struct wl_client *client,
6422 struct wl_resource *viewporter,
6423 uint32_t id,
6424 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006425{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006426 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006427 struct weston_surface *surface =
6428 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006429 struct wl_resource *resource;
6430
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006431 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006432 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006433 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006434 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006435 return;
6436 }
6437
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006438 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006439 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006440 if (resource == NULL) {
6441 wl_client_post_no_memory(client);
6442 return;
6443 }
6444
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006445 wl_resource_set_implementation(resource, &viewport_interface,
6446 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006447
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006448 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006449}
6450
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006451static const struct wp_viewporter_interface viewporter_interface = {
6452 viewporter_destroy,
6453 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006454};
6455
6456static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006457bind_viewporter(struct wl_client *client,
6458 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006459{
6460 struct wl_resource *resource;
6461
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006462 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006463 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006464 if (resource == NULL) {
6465 wl_client_post_no_memory(client);
6466 return;
6467 }
6468
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006469 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006470 NULL, NULL);
6471}
6472
6473static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006474destroy_presentation_feedback(struct wl_resource *feedback_resource)
6475{
6476 struct weston_presentation_feedback *feedback;
6477
6478 feedback = wl_resource_get_user_data(feedback_resource);
6479
6480 wl_list_remove(&feedback->link);
6481 free(feedback);
6482}
6483
6484static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006485presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6486{
6487 wl_resource_destroy(resource);
6488}
6489
6490static void
6491presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006492 struct wl_resource *presentation_resource,
6493 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006494 uint32_t callback)
6495{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006496 struct weston_surface *surface;
6497 struct weston_presentation_feedback *feedback;
6498
6499 surface = wl_resource_get_user_data(surface_resource);
6500
Bryce Harringtonde16d892014-11-20 22:21:57 -08006501 feedback = zalloc(sizeof *feedback);
6502 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006503 goto err_calloc;
6504
6505 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006506 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006507 1, callback);
6508 if (!feedback->resource)
6509 goto err_create;
6510
6511 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6512 destroy_presentation_feedback);
6513
6514 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6515
6516 return;
6517
6518err_create:
6519 free(feedback);
6520
6521err_calloc:
6522 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006523}
6524
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006525static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006526 presentation_destroy,
6527 presentation_feedback
6528};
6529
6530static void
6531bind_presentation(struct wl_client *client,
6532 void *data, uint32_t version, uint32_t id)
6533{
6534 struct weston_compositor *compositor = data;
6535 struct wl_resource *resource;
6536
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006537 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006538 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006539 if (resource == NULL) {
6540 wl_client_post_no_memory(client);
6541 return;
6542 }
6543
6544 wl_resource_set_implementation(resource, &presentation_implementation,
6545 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006546 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006547}
6548
6549static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006550compositor_bind(struct wl_client *client,
6551 void *data, uint32_t version, uint32_t id)
6552{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006553 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006554 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006555
Jason Ekstranda85118c2013-06-27 20:17:02 -05006556 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006557 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006558 if (resource == NULL) {
6559 wl_client_post_no_memory(client);
6560 return;
6561 }
6562
6563 wl_resource_set_implementation(resource, &compositor_interface,
6564 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006565}
6566
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006567WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006568weston_environment_get_fd(const char *env)
6569{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006570 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006571 int fd, flags;
6572
6573 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006574 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006575 return -1;
6576
6577 flags = fcntl(fd, F_GETFD);
6578 if (flags == -1)
6579 return -1;
6580
6581 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6582 unsetenv(env);
6583
6584 return fd;
6585}
6586
Pekka Paalanenb5026542014-11-12 15:09:24 +02006587static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006588timeline_key_binding_handler(struct weston_keyboard *keyboard,
6589 const struct timespec *time, uint32_t key,
6590 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006591{
6592 struct weston_compositor *compositor = data;
6593
6594 if (weston_timeline_enabled_)
6595 weston_timeline_close();
6596 else
6597 weston_timeline_open(compositor);
6598}
6599
Daniel Stonece62cb32018-07-20 09:46:24 +01006600static const char *
6601output_repaint_status_text(struct weston_output *output)
6602{
6603 switch (output->repaint_status) {
6604 case REPAINT_NOT_SCHEDULED:
6605 return "no repaint";
6606 case REPAINT_BEGIN_FROM_IDLE:
6607 return "start_repaint_loop scheduled";
6608 case REPAINT_SCHEDULED:
6609 return "repaint scheduled";
6610 case REPAINT_AWAITING_COMPLETION:
6611 return "awaiting completion";
6612 }
6613
6614 assert(!"output_repaint_status_text missing enum");
6615 return NULL;
6616}
6617
6618static void
6619debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6620{
6621 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6622 struct wl_shm_buffer *shm;
6623 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006624 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006625
6626 if (!buffer) {
6627 fprintf(fp, "\t\t[buffer not available]\n");
6628 return;
6629 }
6630
6631 shm = wl_shm_buffer_get(buffer->resource);
6632 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006633 uint32_t _format = wl_shm_buffer_get_format(shm);
6634 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006635 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006636 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6637 (unsigned long) _format,
6638 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006639 return;
6640 }
6641
6642 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6643 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006644 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006645 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006646 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6647 (unsigned long) dmabuf->attributes.format,
6648 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006649 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6650 (unsigned long long) dmabuf->attributes.modifier[0]);
6651 return;
6652 }
6653
Marius Vlad253ba9a2019-03-17 18:22:21 +02006654 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006655}
6656
6657static void
6658debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6659{
6660 struct weston_compositor *ec = view->surface->compositor;
6661 struct weston_output *output;
6662 char desc[512];
6663 pixman_box32_t *box;
6664 uint32_t surface_id = 0;
6665 pid_t pid = 0;
6666
6667 if (view->surface->resource) {
6668 struct wl_resource *resource = view->surface->resource;
6669 wl_client_get_credentials(wl_resource_get_client(resource),
6670 &pid, NULL, NULL);
6671 surface_id = wl_resource_get_id(view->surface->resource);
6672 }
6673
6674 if (!view->surface->get_label ||
6675 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6676 strcpy(desc, "[no description available]");
6677 }
6678 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6679 view_idx, view->surface->role_name, pid, surface_id,
6680 desc, view);
6681
6682 box = pixman_region32_extents(&view->transform.boundingbox);
6683 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6684 box->x1, box->y1, box->x2, box->y2);
6685 box = pixman_region32_extents(&view->transform.opaque);
6686
6687 if (pixman_region32_equal(&view->transform.opaque,
6688 &view->transform.boundingbox)) {
6689 fprintf(fp, "\t\t[fully opaque]\n");
6690 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6691 fprintf(fp, "\t\t[not opaque]\n");
6692 } else {
6693 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6694 box->x1, box->y1, box->x2, box->y2);
6695 }
6696
6697 if (view->alpha < 1.0)
6698 fprintf(fp, "\t\talpha: %f\n", view->alpha);
6699
6700 if (view->output_mask != 0) {
6701 bool first_output = true;
6702 fprintf(fp, "\t\toutputs: ");
6703 wl_list_for_each(output, &ec->output_list, link) {
6704 if (!(view->output_mask & (1 << output->id)))
6705 continue;
6706 fprintf(fp, "%s%d (%s)%s",
6707 (first_output) ? "" : ", ",
6708 output->id, output->name,
6709 (view->output == output) ? " (primary)" : "");
6710 first_output = false;
6711 }
6712 } else {
6713 fprintf(fp, "\t\t[no outputs]");
6714 }
6715
6716 fprintf(fp, "\n");
6717
6718 debug_scene_view_print_buffer(fp, view);
6719}
6720
Marius Vlad433f4e72019-02-17 22:14:23 +02006721static void
6722debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02006723 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02006724{
6725 struct weston_subsurface *sub;
6726 struct weston_view *ev;
6727
6728 /*
6729 * print the view first, then we recursively go on printing
6730 * sub-surfaces. We bail out once no more sub-surfaces are available.
6731 */
Marius Vlada6acfa82019-03-17 18:10:09 +02006732 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02006733
6734 /* no more sub-surfaces */
6735 if (wl_list_empty(&view->surface->subsurface_list))
6736 return;
6737
6738 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
6739 wl_list_for_each(ev, &sub->surface->views, surface_link) {
6740 /* do not print again the parent view */
6741 if (view == ev)
6742 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02006743
6744 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02006745 debug_scene_view_print_tree(ev, fp, view_idx);
6746 }
6747 }
6748}
6749
Daniel Stonece62cb32018-07-20 09:46:24 +01006750/**
6751 * Output information on how libweston is currently composing the scene
6752 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006753 *
6754 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01006755 */
6756WL_EXPORT char *
6757weston_compositor_print_scene_graph(struct weston_compositor *ec)
6758{
6759 struct weston_output *output;
6760 struct weston_layer *layer;
6761 struct timespec now;
6762 int layer_idx = 0;
6763 FILE *fp;
6764 char *ret;
6765 size_t len;
6766 int err;
6767
6768 fp = open_memstream(&ret, &len);
6769 assert(fp);
6770
6771 weston_compositor_read_presentation_clock(ec, &now);
6772 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
6773 now.tv_sec, now.tv_nsec);
6774
6775 wl_list_for_each(output, &ec->output_list, link) {
6776 struct weston_head *head;
6777 int head_idx = 0;
6778
6779 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
6780 assert(output->enabled);
6781
6782 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
6783 output->x, output->y,
6784 output->x + output->width,
6785 output->y + output->height);
6786 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
6787 output->current_mode->width,
6788 output->current_mode->height,
6789 output->current_mode->refresh / 1000.0);
6790 fprintf(fp, "\tscale: %d\n", output->scale);
6791
6792 fprintf(fp, "\trepaint status: %s\n",
6793 output_repaint_status_text(output));
6794 if (output->repaint_status == REPAINT_SCHEDULED)
6795 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
6796 output->next_repaint.tv_sec,
6797 output->next_repaint.tv_nsec);
6798
6799 wl_list_for_each(head, &output->head_list, output_link) {
6800 fprintf(fp, "\tHead %d (%s): %sconnected\n",
6801 head_idx++, head->name,
6802 (head->connected) ? "" : "not ");
6803 }
6804 }
6805
6806 fprintf(fp, "\n");
6807
6808 wl_list_for_each(layer, &ec->layer_list, link) {
6809 struct weston_view *view;
6810 int view_idx = 0;
6811
6812 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
6813 (unsigned long) layer->position);
6814
6815 if (!weston_layer_mask_is_infinite(layer)) {
6816 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
6817 layer->mask.x1, layer->mask.y1,
6818 layer->mask.x2, layer->mask.y2);
6819 }
6820
Marius Vlada6acfa82019-03-17 18:10:09 +02006821 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
6822 debug_scene_view_print_tree(view, fp, &view_idx);
6823 view_idx++;
6824 }
Daniel Stonece62cb32018-07-20 09:46:24 +01006825
6826 if (wl_list_empty(&layer->view_list.link))
6827 fprintf(fp, "\t[no views]\n");
6828
6829 fprintf(fp, "\n");
6830 }
6831
6832 err = fclose(fp);
6833 assert(err == 0);
6834
6835 return ret;
6836}
6837
6838/**
6839 * Called when the 'scene-graph' debug scope is bound by a client. This
6840 * one-shot weston-debug scope prints the current scene graph when bound,
6841 * and then terminates the stream.
6842 */
6843static void
6844debug_scene_graph_cb(struct weston_debug_stream *stream, void *data)
6845{
6846 struct weston_compositor *ec = data;
6847 char *str = weston_compositor_print_scene_graph(ec);
6848
6849 weston_debug_stream_printf(stream, "%s", str);
6850 free(str);
6851 weston_debug_stream_complete(stream);
6852}
6853
Giulio Camuffo459137b2014-10-11 23:56:24 +03006854/** Create the compositor.
6855 *
6856 * This functions creates and initializes a compositor instance.
6857 *
6858 * \param display The Wayland display to be used.
6859 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03006860 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03006861 * using the \ref weston_compositor_get_user_data function.
6862 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006863 *
6864 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03006865 */
6866WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03006867weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03006868 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03006869 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006870{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006871 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006872 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006873
Giulio Camuffo459137b2014-10-11 23:56:24 +03006874 ec = zalloc(sizeof *ec);
6875 if (!ec)
6876 return NULL;
6877
6878 ec->wl_display = display;
6879 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006880 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006881 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006882 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006883 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006884 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006885 wl_signal_init(&ec->idle_signal);
6886 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006887 wl_signal_init(&ec->show_input_panel_signal);
6888 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006889 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006890 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006891 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006892 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006893 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006894 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006895 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02006896 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006897 wl_signal_init(&ec->session_signal);
6898 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006899
Casey Dahlin58ba1372012-04-19 22:50:08 -04006900 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006901 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006902
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006903 ec->activate_serial = 1;
6904
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05006905 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
6906
Derek Foreman152254b2015-11-26 14:17:48 -06006907 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006908 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006909 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006910
Giulio Camuffo954f1832014-10-11 18:27:30 +03006911 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006912 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006913 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006914
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006915 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006916 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006917 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006918
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006919 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006920 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006921 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006922
Marius Vlad3d7d9782019-04-17 12:35:38 +03006923 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02006924 goto fail;
6925
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006926 if (weston_input_init(ec) != 0)
6927 goto fail;
6928
Jason Ekstranda7af7042013-10-12 22:38:11 -05006929 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006930 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006931 wl_list_init(&ec->layer_list);
6932 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006933 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006934 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006935 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006936 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006937 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006938 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006939 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006940 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006941 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006942
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006943 wl_list_init(&ec->plugin_api_list);
6944
Xiong Zhang97116532013-10-23 13:58:31 +08006945 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006946 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006947
Giulio Camuffo459137b2014-10-11 23:56:24 +03006948 wl_data_device_manager_init(ec->wl_display);
6949
6950 wl_display_init_shm(ec->wl_display);
6951
6952 loop = wl_display_get_event_loop(ec->wl_display);
6953 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006954 ec->repaint_timer =
6955 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6956 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006957
Quentin Glidic82681572016-12-17 13:40:51 +01006958 weston_layer_init(&ec->fade_layer, ec);
6959 weston_layer_init(&ec->cursor_layer, ec);
6960
6961 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6962 weston_layer_set_position(&ec->cursor_layer,
6963 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006964
6965 weston_compositor_add_debug_binding(ec, KEY_T,
6966 timeline_key_binding_handler, ec);
6967
Daniel Stonece62cb32018-07-20 09:46:24 +01006968 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03006969 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
6970 "Scene graph details\n",
6971 debug_scene_graph_cb,
6972 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01006973
Giulio Camuffo459137b2014-10-11 23:56:24 +03006974 return ec;
6975
6976fail:
6977 free(ec);
6978 return NULL;
6979}
6980
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006981/** weston_compositor_shutdown
6982 * \ingroup compositor
6983 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006984WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006985weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006986{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006987 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006988
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006989 wl_event_source_remove(ec->idle_source);
6990
Matt Roper361d2ad2011-08-29 13:52:23 -07006991 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006992 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006993 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006994
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006995 /* Destroy all pending outputs associated with this compositor */
6996 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6997 output->destroy(output);
6998
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006999 if (ec->renderer)
7000 ec->renderer->destroy(ec);
7001
Daniel Stone325fc2d2012-05-30 16:31:58 +01007002 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007003 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007004 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007005 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007006 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007007 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007008
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007009 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007010}
7011
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007012/** weston_compositor_exit_with_code
7013 * \ingroup compositor
7014 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007015WL_EXPORT void
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007016weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007017 int exit_code)
7018{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007019 if (compositor->exit_code == EXIT_SUCCESS)
7020 compositor->exit_code = exit_code;
7021
Giulio Camuffo459137b2014-10-11 23:56:24 +03007022 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007023}
7024
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007025/** weston_compositor_set_default_pointer_grab
7026 * \ingroup compositor
7027 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007028WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007029weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7030 const struct weston_pointer_grab_interface *interface)
7031{
7032 struct weston_seat *seat;
7033
7034 ec->default_pointer_grab = interface;
7035 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007036 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7037
7038 if (pointer)
7039 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007040 }
7041}
7042
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007043/** weston_compositor_set_presentation_clock
7044 * \ingroup compositor
7045 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007046WL_EXPORT int
7047weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7048 clockid_t clk_id)
7049{
7050 struct timespec ts;
7051
7052 if (clock_gettime(clk_id, &ts) < 0)
7053 return -1;
7054
7055 compositor->presentation_clock = clk_id;
7056
7057 return 0;
7058}
7059
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007060/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007061 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007062 *
7063 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007064 */
7065WL_EXPORT int
7066weston_compositor_set_presentation_clock_software(
7067 struct weston_compositor *compositor)
7068{
7069 /* In order of preference */
7070 static const clockid_t clocks[] = {
7071 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7072 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7073 CLOCK_MONOTONIC, /* no jumps, may crawl */
7074 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7075 CLOCK_REALTIME /* may jump and crawl */
7076 };
7077 unsigned i;
7078
7079 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7080 if (weston_compositor_set_presentation_clock(compositor,
7081 clocks[i]) == 0)
7082 return 0;
7083
7084 weston_log("Error: no suitable presentation clock available.\n");
7085
7086 return -1;
7087}
7088
Pekka Paalanen662f3842015-03-18 12:17:26 +02007089/** Read the current time from the Presentation clock
7090 *
7091 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007092 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007093 *
7094 * \note Reading the current time in user space is always imprecise to some
7095 * degree.
7096 *
7097 * This function is never meant to fail. If reading the clock does fail,
7098 * an error message is logged and a zero time is returned. Callers are not
7099 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007100 *
7101 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007102 */
7103WL_EXPORT void
7104weston_compositor_read_presentation_clock(
7105 const struct weston_compositor *compositor,
7106 struct timespec *ts)
7107{
7108 static bool warned;
7109 int ret;
7110
7111 ret = clock_gettime(compositor->presentation_clock, ts);
7112 if (ret < 0) {
7113 ts->tv_sec = 0;
7114 ts->tv_nsec = 0;
7115
7116 if (!warned)
7117 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007118 "the presentation clock %#x: '%s' (%d)\n",
7119 compositor->presentation_clock,
7120 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007121 warned = true;
7122 }
7123}
7124
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007125/** Import dmabuf buffer into current renderer
7126 *
7127 * \param compositor
7128 * \param buffer the dmabuf buffer to import
7129 * \return true on usable buffers, false otherwise
7130 *
7131 * This function tests that the linux_dmabuf_buffer is usable
7132 * for the current renderer. Returns false on unusable buffers. Usually
7133 * usability is tested by importing the dmabufs for composition.
7134 *
7135 * This hook is also used for detecting if the renderer supports
7136 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7137 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007138 *
7139 * \ingroup compositor
7140 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007141WL_EXPORT bool
7142weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7143 struct linux_dmabuf_buffer *buffer)
7144{
7145 struct weston_renderer *renderer;
7146
7147 renderer = compositor->renderer;
7148
7149 if (renderer->import_dmabuf == NULL)
7150 return false;
7151
7152 return renderer->import_dmabuf(compositor, buffer);
7153}
7154
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007155WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007156weston_version(int *major, int *minor, int *micro)
7157{
7158 *major = WESTON_VERSION_MAJOR;
7159 *minor = WESTON_VERSION_MINOR;
7160 *micro = WESTON_VERSION_MICRO;
7161}
7162
Daniel Stonee03c1112016-11-24 20:45:45 +00007163/**
7164 * Attempts to find a module path from the module map specified in the
7165 * environment. If found, writes the full path into the path variable.
7166 *
7167 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7168 * each entry is of the form "name=path" and entries are separated by
7169 * semicolons. Whitespace is significant.
7170 *
7171 * \param name The name to search for.
7172 * \param path Where the path is written to if found.
7173 * \param path_len Allocated bytes at \c path .
7174 * \returns The length of the string written to path on success, or 0 if the
7175 * module was not specified in the environment map or path_len was too small.
7176 */
7177WL_EXPORT size_t
7178weston_module_path_from_env(const char *name, char *path, size_t path_len)
7179{
7180 const char *mapping = getenv("WESTON_MODULE_MAP");
7181 const char *end;
7182 const int name_len = strlen(name);
7183
7184 if (!mapping)
7185 return 0;
7186
7187 end = mapping + strlen(mapping);
7188 while (mapping < end && *mapping) {
7189 const char *filename, *next;
7190
7191 /* early out: impossibly short string */
7192 if (end - mapping < name_len + 1)
7193 return 0;
7194
7195 filename = &mapping[name_len + 1];
7196 next = strchrnul(mapping, ';');
7197
7198 if (strncmp(mapping, name, name_len) == 0 &&
7199 mapping[name_len] == '=') {
7200 size_t file_len = next - filename; /* no trailing NUL */
7201 if (file_len >= path_len)
7202 return 0;
7203 strncpy(path, filename, file_len);
7204 path[file_len] = '\0';
7205 return file_len;
7206 }
7207
7208 mapping = next + 1;
7209 }
7210
7211 return 0;
7212}
7213
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007214WL_EXPORT void *
7215weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007216{
7217 char path[PATH_MAX];
7218 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007219 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007220
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007221 if (name == NULL)
7222 return NULL;
7223
Derek Foreman3f86e502015-06-08 11:46:54 -05007224 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007225 len = weston_module_path_from_env(name, path, sizeof path);
7226 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007227 len = snprintf(path, sizeof path, "%s/%s",
7228 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007229 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007230 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007231 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007232
Daniel Stonebeb97e52016-11-28 12:13:54 +00007233 /* snprintf returns the length of the string it would've written,
7234 * _excluding_ the NUL byte. So even being equal to the size of
7235 * our buffer is an error here. */
7236 if (len >= sizeof path)
7237 return NULL;
7238
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007239 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7240 if (module) {
7241 weston_log("Module '%s' already loaded\n", path);
7242 dlclose(module);
7243 return NULL;
7244 }
7245
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007246 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007247 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007248 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007249 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007250 return NULL;
7251 }
7252
7253 init = dlsym(module, entrypoint);
7254 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007255 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007256 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007257 return NULL;
7258 }
7259
7260 return init;
7261}
7262
Giulio Camuffo459137b2014-10-11 23:56:24 +03007263
7264/** Destroys the compositor.
7265 *
7266 * This function cleans up the compositor state and destroys it.
7267 *
7268 * \param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007269 *
7270 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007271 */
7272WL_EXPORT void
7273weston_compositor_destroy(struct weston_compositor *compositor)
7274{
7275 /* prevent further rendering while shutting down */
7276 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7277
7278 wl_signal_emit(&compositor->destroy_signal, compositor);
7279
7280 weston_compositor_xkb_destroy(compositor);
7281
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007282 if (compositor->backend)
7283 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007284
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007285 /* The backend is responsible for destroying the heads. */
7286 assert(wl_list_empty(&compositor->head_list));
7287
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007288 weston_plugin_api_destroy_list(compositor);
7289
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007290 if (compositor->heads_changed_source)
7291 wl_event_source_remove(compositor->heads_changed_source);
7292
Marius Vlad7e4db952019-04-17 13:47:06 +03007293 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007294 compositor->debug_scene = NULL;
Marius Vlad3d7d9782019-04-17 12:35:38 +03007295 weston_log_ctx_compositor_destroy(compositor);
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007296
Giulio Camuffo459137b2014-10-11 23:56:24 +03007297 free(compositor);
7298}
7299
7300/** Instruct the compositor to exit.
7301 *
7302 * This functions does not directly destroy the compositor object, it merely
7303 * command it to start the tear down process. It is not guaranteed that the
7304 * tear down will happen immediately.
7305 *
7306 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007307 *
7308 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007309 */
7310WL_EXPORT void
7311weston_compositor_exit(struct weston_compositor *compositor)
7312{
7313 compositor->exit(compositor);
7314}
7315
7316/** Return the user data stored in the compositor.
7317 *
7318 * This function returns the user data pointer set with user_data parameter
7319 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007320 *
7321 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007322 */
7323WL_EXPORT void *
7324weston_compositor_get_user_data(struct weston_compositor *compositor)
7325{
7326 return compositor->user_data;
7327}
Pekka Paalanendd186732016-06-03 14:49:54 +03007328
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007329static const char * const backend_map[] = {
7330 [WESTON_BACKEND_DRM] = "drm-backend.so",
7331 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7332 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7333 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7334 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7335 [WESTON_BACKEND_X11] = "x11-backend.so",
7336};
7337
Pekka Paalanendd186732016-06-03 14:49:54 +03007338/** Load a backend into a weston_compositor
7339 *
7340 * A backend must be loaded to make a weston_compositor work. A backend
7341 * provides input and output capabilities, and determines the renderer to use.
7342 *
7343 * \param compositor A compositor that has not had a backend loaded yet.
7344 * \param backend Name of the backend file.
7345 * \param config_base A pointer to a backend-specific configuration
7346 * structure's 'base' member.
7347 *
7348 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007349 *
7350 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007351 */
7352WL_EXPORT int
7353weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007354 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007355 struct weston_backend_config *config_base)
7356{
7357 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007358 struct weston_backend_config *config_base);
7359
Pekka Paalanend7e35112017-08-29 17:04:12 +03007360 if (compositor->backend) {
7361 weston_log("Error: attempt to load a backend when one is already loaded\n");
7362 return -1;
7363 }
7364
Quentin Glidic887c0182016-07-10 11:00:53 +02007365 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007366 return -1;
7367
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007368 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007369 if (!backend_init)
7370 return -1;
7371
Pekka Paalanend7e35112017-08-29 17:04:12 +03007372 if (backend_init(compositor, config_base) < 0) {
7373 compositor->backend = NULL;
7374 return -1;
7375 }
7376
7377 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007378}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007379
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007380/** weston_compositor_load_xwayland
7381 * \ingroup compositor
7382 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007383WL_EXPORT int
7384weston_compositor_load_xwayland(struct weston_compositor *compositor)
7385{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007386 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007387
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007388 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007389 if (!module_init)
7390 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007391 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007392 return -1;
7393 return 0;
7394}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007395
7396/** Resolve an internal compositor error by disconnecting the client.
7397 *
7398 * This function is used in cases when the wl_buffer turns out
7399 * unusable and there is no fallback path.
7400 *
7401 * It is possible the fault is caused by a compositor bug, the underlying
7402 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7403 * In any case, the options are to either composite garbage or nothing,
7404 * or disconnect the client. This is a helper function for the latter.
7405 *
7406 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7407 *
7408 * \param buffer The weston buffer that is unusable.
7409 * \param msg A custom error message attached to the protocol error.
7410 */
7411WL_EXPORT void
7412weston_buffer_send_server_error(struct weston_buffer *buffer,
7413 const char *msg)
7414{
7415 struct wl_client *client;
7416 struct wl_resource *display_resource;
7417 uint32_t id;
7418
7419 assert(buffer->resource);
7420 id = wl_resource_get_id(buffer->resource);
7421 client = wl_resource_get_client(buffer->resource);
7422 display_resource = wl_client_get_object(client, 1);
7423
7424 assert(display_resource);
7425 wl_resource_post_error(display_resource,
7426 WL_DISPLAY_ERROR_INVALID_OBJECT,
7427 "server error with "
7428 "wl_buffer@%u: %s", id, msg);
7429}