blob: 31d90c1cae31dc41c5b938343bf1bc4d6e706673 [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
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600190WL_EXPORT int
191weston_output_mode_set_native(struct weston_output *output,
192 struct weston_mode *mode,
193 int32_t scale)
194{
195 int ret;
196 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200197 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600198
199 if (!output->switch_mode)
200 return -1;
201
202 if (!output->original_mode) {
203 mode_changed = 1;
204 ret = output->switch_mode(output, mode);
205 if (ret < 0)
206 return ret;
207 if (output->current_scale != scale) {
208 scale_changed = 1;
209 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200210 }
211 }
212
David Fort0de859e2016-05-27 23:22:57 +0200213 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600214 output->native_mode = mode;
215 output->native_scale = scale;
216
217 weston_mode_switch_finish(output, mode_changed, scale_changed);
218
David Fort0de859e2016-05-27 23:22:57 +0200219 if (mode_changed || scale_changed) {
220 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
221
222 wl_signal_emit(&output->compositor->output_resized_signal, output);
223 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600224 return 0;
225}
226
227WL_EXPORT int
228weston_output_mode_switch_to_native(struct weston_output *output)
229{
230 int ret;
231 int mode_changed = 0, scale_changed = 0;
232
233 if (!output->switch_mode)
234 return -1;
235
236 if (!output->original_mode) {
237 weston_log("already in the native mode\n");
238 return -1;
239 }
240 /* the non fullscreen clients haven't seen a mode set since we
241 * switched into a temporary, so we need to notify them if the
242 * mode at that time is different from the native mode now.
243 */
244 mode_changed = (output->original_mode != output->native_mode);
245 scale_changed = (output->original_scale != output->native_scale);
246
247 ret = output->switch_mode(output, output->native_mode);
248 if (ret < 0)
249 return ret;
250
251 output->current_scale = output->native_scale;
252
253 output->original_mode = NULL;
254 output->original_scale = 0;
255
256 weston_mode_switch_finish(output, mode_changed, scale_changed);
257
258 return 0;
259}
260
261WL_EXPORT int
262weston_output_mode_switch_to_temporary(struct weston_output *output,
263 struct weston_mode *mode,
264 int32_t scale)
265{
266 int ret;
267
268 if (!output->switch_mode)
269 return -1;
270
271 /* original_mode is the last mode non full screen clients have seen,
272 * so we shouldn't change it if we already have one set.
273 */
274 if (!output->original_mode) {
275 output->original_mode = output->native_mode;
276 output->original_scale = output->native_scale;
277 }
278 ret = output->switch_mode(output, mode);
279 if (ret < 0)
280 return ret;
281
282 output->current_scale = scale;
283
284 weston_mode_switch_finish(output, 0, 0);
285
286 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800287}
288
Benjamin Franzke06286262011-05-06 19:12:33 +0200289static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300290region_init_infinite(pixman_region32_t *region)
291{
292 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
293 UINT32_MAX, UINT32_MAX);
294}
295
Pekka Paalanene67858b2013-04-25 13:57:42 +0300296static struct weston_subsurface *
297weston_surface_to_subsurface(struct weston_surface *surface);
298
Jason Ekstranda7af7042013-10-12 22:38:11 -0500299WL_EXPORT struct weston_view *
300weston_view_create(struct weston_surface *surface)
301{
302 struct weston_view *view;
303
Bryce Harringtonde16d892014-11-20 22:21:57 -0800304 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500305 if (view == NULL)
306 return NULL;
307
308 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000309 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500310
Jason Ekstranda7af7042013-10-12 22:38:11 -0500311 /* Assign to surface */
312 wl_list_insert(&surface->views, &view->surface_link);
313
314 wl_signal_init(&view->destroy_signal);
315 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300316 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500317
Jason Ekstranda7af7042013-10-12 22:38:11 -0500318 pixman_region32_init(&view->clip);
319
320 view->alpha = 1.0;
321 pixman_region32_init(&view->transform.opaque);
322
323 wl_list_init(&view->geometry.transformation_list);
324 wl_list_insert(&view->geometry.transformation_list,
325 &view->transform.position.link);
326 weston_matrix_init(&view->transform.position.matrix);
327 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200328 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500329 pixman_region32_init(&view->transform.boundingbox);
330 view->transform.dirty = 1;
331
Jason Ekstranda7af7042013-10-12 22:38:11 -0500332 return view;
333}
334
Jason Ekstrand108865d2014-06-26 10:04:49 -0700335struct weston_frame_callback {
336 struct wl_resource *resource;
337 struct wl_list link;
338};
339
Pekka Paalanen133e4392014-09-23 22:08:46 -0400340struct weston_presentation_feedback {
341 struct wl_resource *resource;
342
343 /* XXX: could use just wl_resource_get_link() instead */
344 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200345
346 /* The per-surface feedback flags */
347 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400348};
349
350static void
351weston_presentation_feedback_discard(
352 struct weston_presentation_feedback *feedback)
353{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200354 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400355 wl_resource_destroy(feedback->resource);
356}
357
358static void
359weston_presentation_feedback_discard_list(struct wl_list *list)
360{
361 struct weston_presentation_feedback *feedback, *tmp;
362
363 wl_list_for_each_safe(feedback, tmp, list, link)
364 weston_presentation_feedback_discard(feedback);
365}
366
367static void
368weston_presentation_feedback_present(
369 struct weston_presentation_feedback *feedback,
370 struct weston_output *output,
371 uint32_t refresh_nsec,
372 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200373 uint64_t seq,
374 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400375{
376 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300377 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400378 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200379 uint32_t tv_sec_hi;
380 uint32_t tv_sec_lo;
381 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300382 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400383
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300384 wl_list_for_each(head, &output->head_list, output_link) {
385 wl_resource_for_each(o, &head->resource_list) {
386 if (wl_resource_get_client(o) != client)
387 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400388
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300389 wp_presentation_feedback_send_sync_output(feedback->resource, o);
390 done = true;
391 }
392
393 /* For clone mode, send it for just one wl_output global,
394 * they are all equivalent anyway.
395 */
396 if (done)
397 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400398 }
399
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200400 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200401 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200402 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200403 refresh_nsec,
404 seq >> 32, seq & 0xffffffff,
405 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400406 wl_resource_destroy(feedback->resource);
407}
408
409static void
410weston_presentation_feedback_present_list(struct wl_list *list,
411 struct weston_output *output,
412 uint32_t refresh_nsec,
413 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200414 uint64_t seq,
415 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400416{
417 struct weston_presentation_feedback *feedback, *tmp;
418
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200419 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200420 wl_list_empty(list));
421
Pekka Paalanen133e4392014-09-23 22:08:46 -0400422 wl_list_for_each_safe(feedback, tmp, list, link)
423 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200424 refresh_nsec, ts, seq,
425 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400426}
427
Jason Ekstrand7b982072014-05-20 14:33:03 -0500428static void
429surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
430{
431 struct weston_surface_state *state =
432 container_of(listener, struct weston_surface_state,
433 buffer_destroy_listener);
434
435 state->buffer = NULL;
436}
437
438static void
439weston_surface_state_init(struct weston_surface_state *state)
440{
441 state->newly_attached = 0;
442 state->buffer = NULL;
443 state->buffer_destroy_listener.notify =
444 surface_state_handle_buffer_destroy;
445 state->sx = 0;
446 state->sy = 0;
447
Derek Foreman152254b2015-11-26 14:17:48 -0600448 pixman_region32_init(&state->damage_surface);
449 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500450 pixman_region32_init(&state->opaque);
451 region_init_infinite(&state->input);
452
453 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400454 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500455
456 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
457 state->buffer_viewport.buffer.scale = 1;
458 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
459 state->buffer_viewport.surface.width = -1;
460 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300461
462 state->acquire_fence_fd = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500463}
464
465static void
466weston_surface_state_fini(struct weston_surface_state *state)
467{
468 struct weston_frame_callback *cb, *next;
469
470 wl_list_for_each_safe(cb, next,
471 &state->frame_callback_list, link)
472 wl_resource_destroy(cb->resource);
473
Pekka Paalanen133e4392014-09-23 22:08:46 -0400474 weston_presentation_feedback_discard_list(&state->feedback_list);
475
Jason Ekstrand7b982072014-05-20 14:33:03 -0500476 pixman_region32_fini(&state->input);
477 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600478 pixman_region32_fini(&state->damage_surface);
479 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500480
481 if (state->buffer)
482 wl_list_remove(&state->buffer_destroy_listener.link);
483 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300484
485 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300486 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500487}
488
489static void
490weston_surface_state_set_buffer(struct weston_surface_state *state,
491 struct weston_buffer *buffer)
492{
493 if (state->buffer == buffer)
494 return;
495
496 if (state->buffer)
497 wl_list_remove(&state->buffer_destroy_listener.link);
498 state->buffer = buffer;
499 if (state->buffer)
500 wl_signal_add(&state->buffer->destroy_signal,
501 &state->buffer_destroy_listener);
502}
503
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500504WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500505weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500506{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500507 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400508
Bryce Harringtonde16d892014-11-20 22:21:57 -0800509 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400510 if (surface == NULL)
511 return NULL;
512
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500513 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800514 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500515
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500516 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200517 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400518
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200519 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
520 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200521 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
522 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500523
524 weston_surface_state_init(&surface->pending);
525
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400526 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500527 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300528 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400529
Jason Ekstranda7af7042013-10-12 22:38:11 -0500530 wl_list_init(&surface->views);
531
532 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400533 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500534
Pekka Paalanene67858b2013-04-25 13:57:42 +0300535 wl_list_init(&surface->subsurface_list);
536 wl_list_init(&surface->subsurface_list_pending);
537
Jason Ekstrand1e059042014-10-16 10:55:19 -0500538 weston_matrix_init(&surface->buffer_to_surface_matrix);
539 weston_matrix_init(&surface->surface_to_buffer_matrix);
540
Jonas Ådahld3414f22016-07-22 17:56:31 +0800541 wl_list_init(&surface->pointer_constraints);
542
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300543 surface->acquire_fence_fd = -1;
544
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400545 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500546}
547
Alex Wu8811bf92012-02-28 18:07:54 +0800548WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500549weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200550 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500551{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100552 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200553 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500554}
555
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400556WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500557weston_view_to_global_float(struct weston_view *view,
558 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200559{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500560 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200561 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
562
Jason Ekstranda7af7042013-10-12 22:38:11 -0500563 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200564
565 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200566 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700567 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200568 v.f[3]);
569 *x = 0;
570 *y = 0;
571 return;
572 }
573
574 *x = v.f[0] / v.f[3];
575 *y = v.f[1] / v.f[3];
576 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500577 *x = sx + view->geometry.x;
578 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200579 }
580}
581
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500582WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200583weston_transformed_coord(int width, int height,
584 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200585 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200586 float sx, float sy, float *bx, float *by)
587{
588 switch (transform) {
589 case WL_OUTPUT_TRANSFORM_NORMAL:
590 default:
591 *bx = sx;
592 *by = sy;
593 break;
594 case WL_OUTPUT_TRANSFORM_FLIPPED:
595 *bx = width - sx;
596 *by = sy;
597 break;
598 case WL_OUTPUT_TRANSFORM_90:
599 *bx = height - sy;
600 *by = sx;
601 break;
602 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
603 *bx = height - sy;
604 *by = width - sx;
605 break;
606 case WL_OUTPUT_TRANSFORM_180:
607 *bx = width - sx;
608 *by = height - sy;
609 break;
610 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
611 *bx = sx;
612 *by = height - sy;
613 break;
614 case WL_OUTPUT_TRANSFORM_270:
615 *bx = sy;
616 *by = width - sx;
617 break;
618 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
619 *bx = sy;
620 *by = sx;
621 break;
622 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200623
624 *bx *= scale;
625 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200626}
627
628WL_EXPORT pixman_box32_t
629weston_transformed_rect(int width, int height,
630 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200631 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200632 pixman_box32_t rect)
633{
634 float x1, x2, y1, y2;
635
636 pixman_box32_t ret;
637
Alexander Larsson4ea95522013-05-22 14:41:37 +0200638 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200639 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200640 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200641 rect.x2, rect.y2, &x2, &y2);
642
643 if (x1 <= x2) {
644 ret.x1 = x1;
645 ret.x2 = x2;
646 } else {
647 ret.x1 = x2;
648 ret.x2 = x1;
649 }
650
651 if (y1 <= y2) {
652 ret.y1 = y1;
653 ret.y2 = y2;
654 } else {
655 ret.y1 = y2;
656 ret.y2 = y1;
657 }
658
659 return ret;
660}
661
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600662/** Transform a region by a matrix, restricted to axis-aligned transformations
663 *
664 * Warning: This function does not work for projective, affine, or matrices
665 * that encode arbitrary rotations. Only 90-degree step rotations are
666 * supported.
667 */
668WL_EXPORT void
669weston_matrix_transform_region(pixman_region32_t *dest,
670 struct weston_matrix *matrix,
671 pixman_region32_t *src)
672{
673 pixman_box32_t *src_rects, *dest_rects;
674 int nrects, i;
675
676 src_rects = pixman_region32_rectangles(src, &nrects);
677 dest_rects = malloc(nrects * sizeof(*dest_rects));
678 if (!dest_rects)
679 return;
680
681 for (i = 0; i < nrects; i++) {
682 struct weston_vector vec1 = {{
683 src_rects[i].x1, src_rects[i].y1, 0, 1
684 }};
685 weston_matrix_transform(matrix, &vec1);
686 vec1.f[0] /= vec1.f[3];
687 vec1.f[1] /= vec1.f[3];
688
689 struct weston_vector vec2 = {{
690 src_rects[i].x2, src_rects[i].y2, 0, 1
691 }};
692 weston_matrix_transform(matrix, &vec2);
693 vec2.f[0] /= vec2.f[3];
694 vec2.f[1] /= vec2.f[3];
695
696 if (vec1.f[0] < vec2.f[0]) {
697 dest_rects[i].x1 = floor(vec1.f[0]);
698 dest_rects[i].x2 = ceil(vec2.f[0]);
699 } else {
700 dest_rects[i].x1 = floor(vec2.f[0]);
701 dest_rects[i].x2 = ceil(vec1.f[0]);
702 }
703
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600704 if (vec1.f[1] < vec2.f[1]) {
705 dest_rects[i].y1 = floor(vec1.f[1]);
706 dest_rects[i].y2 = ceil(vec2.f[1]);
707 } else {
708 dest_rects[i].y1 = floor(vec2.f[1]);
709 dest_rects[i].y2 = ceil(vec1.f[1]);
710 }
711 }
712
713 pixman_region32_clear(dest);
714 pixman_region32_init_rects(dest, dest_rects, nrects);
715 free(dest_rects);
716}
717
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200718WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500719weston_transformed_region(int width, int height,
720 enum wl_output_transform transform,
721 int32_t scale,
722 pixman_region32_t *src, pixman_region32_t *dest)
723{
724 pixman_box32_t *src_rects, *dest_rects;
725 int nrects, i;
726
727 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
728 if (src != dest)
729 pixman_region32_copy(dest, src);
730 return;
731 }
732
733 src_rects = pixman_region32_rectangles(src, &nrects);
734 dest_rects = malloc(nrects * sizeof(*dest_rects));
735 if (!dest_rects)
736 return;
737
738 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
739 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
740 } else {
741 for (i = 0; i < nrects; i++) {
742 switch (transform) {
743 default:
744 case WL_OUTPUT_TRANSFORM_NORMAL:
745 dest_rects[i].x1 = src_rects[i].x1;
746 dest_rects[i].y1 = src_rects[i].y1;
747 dest_rects[i].x2 = src_rects[i].x2;
748 dest_rects[i].y2 = src_rects[i].y2;
749 break;
750 case WL_OUTPUT_TRANSFORM_90:
751 dest_rects[i].x1 = height - src_rects[i].y2;
752 dest_rects[i].y1 = src_rects[i].x1;
753 dest_rects[i].x2 = height - src_rects[i].y1;
754 dest_rects[i].y2 = src_rects[i].x2;
755 break;
756 case WL_OUTPUT_TRANSFORM_180:
757 dest_rects[i].x1 = width - src_rects[i].x2;
758 dest_rects[i].y1 = height - src_rects[i].y2;
759 dest_rects[i].x2 = width - src_rects[i].x1;
760 dest_rects[i].y2 = height - src_rects[i].y1;
761 break;
762 case WL_OUTPUT_TRANSFORM_270:
763 dest_rects[i].x1 = src_rects[i].y1;
764 dest_rects[i].y1 = width - src_rects[i].x2;
765 dest_rects[i].x2 = src_rects[i].y2;
766 dest_rects[i].y2 = width - src_rects[i].x1;
767 break;
768 case WL_OUTPUT_TRANSFORM_FLIPPED:
769 dest_rects[i].x1 = width - src_rects[i].x2;
770 dest_rects[i].y1 = src_rects[i].y1;
771 dest_rects[i].x2 = width - src_rects[i].x1;
772 dest_rects[i].y2 = src_rects[i].y2;
773 break;
774 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
775 dest_rects[i].x1 = height - src_rects[i].y2;
776 dest_rects[i].y1 = width - src_rects[i].x2;
777 dest_rects[i].x2 = height - src_rects[i].y1;
778 dest_rects[i].y2 = width - src_rects[i].x1;
779 break;
780 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
781 dest_rects[i].x1 = src_rects[i].x1;
782 dest_rects[i].y1 = height - src_rects[i].y2;
783 dest_rects[i].x2 = src_rects[i].x2;
784 dest_rects[i].y2 = height - src_rects[i].y1;
785 break;
786 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
787 dest_rects[i].x1 = src_rects[i].y1;
788 dest_rects[i].y1 = src_rects[i].x1;
789 dest_rects[i].x2 = src_rects[i].y2;
790 dest_rects[i].y2 = src_rects[i].x2;
791 break;
792 }
793 }
794 }
795
796 if (scale != 1) {
797 for (i = 0; i < nrects; i++) {
798 dest_rects[i].x1 *= scale;
799 dest_rects[i].x2 *= scale;
800 dest_rects[i].y1 *= scale;
801 dest_rects[i].y2 *= scale;
802 }
803 }
804
805 pixman_region32_clear(dest);
806 pixman_region32_init_rects(dest, dest_rects, nrects);
807 free(dest_rects);
808}
809
Jonny Lamb74130762013-11-26 18:19:46 +0100810static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300811viewport_surface_to_buffer(struct weston_surface *surface,
812 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100813{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200814 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200815 double src_width, src_height;
816 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200817
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200818 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
819 if (vp->surface.width == -1) {
820 *bx = sx;
821 *by = sy;
822 return;
823 }
Jonny Lamb74130762013-11-26 18:19:46 +0100824
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200825 src_x = 0.0;
826 src_y = 0.0;
827 src_width = surface->width_from_buffer;
828 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100829 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200830 src_x = wl_fixed_to_double(vp->buffer.src_x);
831 src_y = wl_fixed_to_double(vp->buffer.src_y);
832 src_width = wl_fixed_to_double(vp->buffer.src_width);
833 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100834 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200835
836 *bx = sx * src_width / surface->width + src_x;
837 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100838}
839
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500840WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200841weston_surface_to_buffer_float(struct weston_surface *surface,
842 float sx, float sy, float *bx, float *by)
843{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200844 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
845
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300846 /* first transform coordinates if the viewport is set */
847 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100848
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500849 weston_transformed_coord(surface->width_from_buffer,
850 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200851 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100852 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200853}
854
Derek Foreman99739672015-12-03 16:38:11 -0600855/** Transform a rectangle from surface coordinates to buffer coordinates
856 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300857 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600858 * from.
859 * \param rect The rectangle to transform.
860 * \return The transformed rectangle.
861 *
862 * Viewport and buffer transformations can only do translation, scaling,
863 * and rotations in 90-degree steps. Therefore the only loss in the
864 * conversion is coordinate rounding.
865 *
866 * However, some coordinate rounding takes place as an intermediate
867 * step before the buffer scale factor is applied, so the rectangle
868 * boundary may not be exactly as expected.
869 *
870 * This is OK for damage tracking since a little extra coverage is
871 * not a problem.
872 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200873WL_EXPORT pixman_box32_t
874weston_surface_to_buffer_rect(struct weston_surface *surface,
875 pixman_box32_t rect)
876{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200877 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100878 float xf, yf;
879
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300880 /* first transform box coordinates if the viewport is set */
881 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100882 rect.x1 = floorf(xf);
883 rect.y1 = floorf(yf);
884
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300885 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600886 rect.x2 = ceilf(xf);
887 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100888
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500889 return weston_transformed_rect(surface->width_from_buffer,
890 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200891 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200892 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200893}
894
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200895/** Transform a region from surface coordinates to buffer coordinates
896 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300897 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200898 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300899 * \param[in] surface_region The region in surface coordinates.
900 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200901 *
902 * Buffer_region must be init'd, but will be completely overwritten.
903 *
904 * Viewport and buffer transformations can only do translation, scaling,
905 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600906 * conversion is from the coordinate rounding that takes place in
907 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300908 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200909 */
910WL_EXPORT void
911weston_surface_to_buffer_region(struct weston_surface *surface,
912 pixman_region32_t *surface_region,
913 pixman_region32_t *buffer_region)
914{
915 pixman_box32_t *src_rects, *dest_rects;
916 int nrects, i;
917
918 src_rects = pixman_region32_rectangles(surface_region, &nrects);
919 dest_rects = malloc(nrects * sizeof(*dest_rects));
920 if (!dest_rects)
921 return;
922
923 for (i = 0; i < nrects; i++) {
924 dest_rects[i] = weston_surface_to_buffer_rect(surface,
925 src_rects[i]);
926 }
927
928 pixman_region32_fini(buffer_region);
929 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
930 free(dest_rects);
931}
932
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200933WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400935 struct weston_plane *plane)
936{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500937 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400938 return;
939
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 weston_view_damage_below(view);
941 view->plane = plane;
942 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400943}
944
Pekka Paalanen51723d52015-02-17 13:10:01 +0200945/** Inflict damage on the plane where the view is visible.
946 *
947 * \param view The view that causes the damage.
948 *
949 * If the view is currently on a plane (including the primary plane),
950 * take the view's boundingbox, subtract all the opaque views that cover it,
951 * and add the remaining region as damage to the plane. This corresponds
952 * to the damage inflicted to the plane if this view disappeared.
953 *
954 * A repaint is scheduled for this view.
955 *
956 * The region of all opaque views covering this view is stored in
957 * weston_view::clip and updated by view_accumulate_damage() during
958 * weston_output_repaint(). Specifically, that region matches the
959 * scenegraph as it was last painted.
960 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400961WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200963{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500964 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200965
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500966 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200967 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800969 if (view->plane)
970 pixman_region32_union(&view->plane->damage,
971 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500972 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800973 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200974}
975
Pekka Paalanen01e00682017-03-24 16:21:06 +0200976/** Send wl_surface.enter/leave events
977 *
978 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300979 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200980 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +0300981 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200982 *
983 * Send the enter/leave events for all protocol objects bound to the given
984 * output by the client owning the surface.
985 */
986static void
987weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300988 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +0200989 bool enter,
990 bool leave)
991{
992 struct wl_resource *wloutput;
993 struct wl_client *client;
994
995 assert(enter != leave);
996
997 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300998 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +0200999 if (wl_resource_get_client(wloutput) != client)
1000 continue;
1001
1002 if (enter)
1003 wl_surface_send_enter(surface->resource, wloutput);
1004 if (leave)
1005 wl_surface_send_leave(surface->resource, wloutput);
1006 }
1007}
1008
Bryce Harrington3f650b82015-12-23 11:01:58 -08001009/**
1010 * \param es The surface
1011 * \param mask The new set of outputs for the surface
1012 *
1013 * Sets the surface's set of outputs to the ones specified by
1014 * the new output mask provided. Identifies the outputs that
1015 * have changed, the posts enter and leave events for these
1016 * outputs as appropriate.
1017 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001018static void
1019weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1020{
1021 uint32_t different = es->output_mask ^ mask;
1022 uint32_t entered = mask & different;
1023 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001024 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001025 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001026 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001027
1028 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001029 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001030 return;
1031 if (different == 0)
1032 return;
1033
1034 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001035 output_bit = 1u << output->id;
1036 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001037 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001038
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001039 wl_list_for_each(head, &output->head_list, output_link) {
1040 weston_surface_send_enter_leave(es, head,
1041 output_bit & entered,
1042 output_bit & left);
1043 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001044 }
1045}
1046
Semi Malinene7a52fb2018-04-26 11:08:10 +02001047static void
1048notify_view_output_destroy(struct wl_listener *listener, void *data)
1049{
1050 struct weston_view *view =
1051 container_of(listener,
1052 struct weston_view, output_destroy_listener);
1053
1054 view->output = NULL;
1055 view->output_destroy_listener.notify = NULL;
1056}
1057
1058/** Set the primary output of the view
1059 *
1060 * \param view The view whose primary output to set
1061 * \param output The new primary output for the view
1062 *
1063 * Set \a output to be the primary output of the \a view.
1064 *
1065 * Notice that the assignment may be temporary; the primary output could be
1066 * automatically changed. Hence, one cannot rely on the value persisting.
1067 *
1068 * Passing NULL as /a output will set the primary output to NULL.
1069 */
1070WL_EXPORT void
1071weston_view_set_output(struct weston_view *view, struct weston_output *output)
1072{
1073 if (view->output_destroy_listener.notify) {
1074 wl_list_remove(&view->output_destroy_listener.link);
1075 view->output_destroy_listener.notify = NULL;
1076 }
1077 view->output = output;
1078 if (output) {
1079 view->output_destroy_listener.notify =
1080 notify_view_output_destroy;
1081 wl_signal_add(&output->destroy_signal,
1082 &view->output_destroy_listener);
1083 }
1084}
1085
Bryce Harrington3f650b82015-12-23 11:01:58 -08001086/** Recalculate which output(s) the surface has views displayed on
1087 *
1088 * \param es The surface to remap to outputs
1089 *
1090 * Finds the output that is showing the largest amount of one
1091 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001092 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001093 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001094 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001095 * in the output_mask for the surface.
1096 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001097static void
1098weston_surface_assign_output(struct weston_surface *es)
1099{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100 struct weston_output *new_output;
1101 struct weston_view *view;
1102 pixman_region32_t region;
1103 uint32_t max, area, mask;
1104 pixman_box32_t *e;
1105
1106 new_output = NULL;
1107 max = 0;
1108 mask = 0;
1109 pixman_region32_init(&region);
1110 wl_list_for_each(view, &es->views, surface_link) {
1111 if (!view->output)
1112 continue;
1113
1114 pixman_region32_intersect(&region, &view->transform.boundingbox,
1115 &view->output->region);
1116
1117 e = pixman_region32_extents(&region);
1118 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1119
1120 mask |= view->output_mask;
1121
1122 if (area >= max) {
1123 new_output = view->output;
1124 max = area;
1125 }
1126 }
1127 pixman_region32_fini(&region);
1128
1129 es->output = new_output;
1130 weston_surface_update_output_mask(es, mask);
1131}
1132
Bryce Harrington3f650b82015-12-23 11:01:58 -08001133/** Recalculate which output(s) the view is displayed on
1134 *
1135 * \param ev The view to remap to outputs
1136 *
1137 * Identifies the set of outputs that the view is visible on,
1138 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001139 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001140 *
1141 * Also does the same for the view's surface. See
1142 * weston_surface_assign_output().
1143 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001144static void
1145weston_view_assign_output(struct weston_view *ev)
1146{
1147 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001148 struct weston_output *output, *new_output;
1149 pixman_region32_t region;
1150 uint32_t max, area, mask;
1151 pixman_box32_t *e;
1152
1153 new_output = NULL;
1154 max = 0;
1155 mask = 0;
1156 pixman_region32_init(&region);
1157 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001158 if (output->destroying)
1159 continue;
1160
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001162 &output->region);
1163
1164 e = pixman_region32_extents(&region);
1165 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1166
1167 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001168 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001169
1170 if (area >= max) {
1171 new_output = output;
1172 max = area;
1173 }
1174 }
1175 pixman_region32_fini(&region);
1176
Semi Malinene7a52fb2018-04-26 11:08:10 +02001177 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 ev->output_mask = mask;
1179
1180 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001181}
1182
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001183static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001184weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1185 int from_x, int from_y, int *to_x, int *to_y)
1186{
1187 float x, y;
1188
1189 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1190 weston_view_from_global_float(to, x, y, &x, &y);
1191
1192 *to_x = round(x);
1193 *to_y = round(y);
1194}
1195
1196static void
1197weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1198{
1199 pixman_box32_t *a;
1200 pixman_box32_t b;
1201
1202 a = pixman_region32_extents(&from->geometry.scissor);
1203
1204 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1205 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1206
1207 pixman_region32_fini(&to->geometry.scissor);
1208 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1209}
1210
1211static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001212view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001214{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001215 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1216 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001217 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001218 { inbox->x1, inbox->y1 },
1219 { inbox->x1, inbox->y2 },
1220 { inbox->x2, inbox->y1 },
1221 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001222 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001223 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001224 int i;
1225
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001226 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001227 /* avoid rounding empty bbox to 1x1 */
1228 pixman_region32_init(bbox);
1229 return;
1230 }
1231
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001232 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001233 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001235 if (x < min_x)
1236 min_x = x;
1237 if (x > max_x)
1238 max_x = x;
1239 if (y < min_y)
1240 min_y = y;
1241 if (y > max_y)
1242 max_y = y;
1243 }
1244
Pekka Paalanen219b9822012-02-08 15:38:37 +02001245 int_x = floorf(min_x);
1246 int_y = floorf(min_y);
1247 pixman_region32_init_rect(bbox, int_x, int_y,
1248 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001249}
1250
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001251static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001253{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001255
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001256 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257 view->geometry.x = roundf(view->geometry.x);
1258 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001259
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001260 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1262 view->transform.position.matrix.d[12] = view->geometry.x;
1263 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001264
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001266
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 view->transform.inverse = view->transform.position.matrix;
1268 view->transform.inverse.d[12] = -view->geometry.x;
1269 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001270
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001272 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001273 view->surface->width,
1274 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001275 if (view->geometry.scissor_enabled)
1276 pixman_region32_intersect(&view->transform.boundingbox,
1277 &view->transform.boundingbox,
1278 &view->geometry.scissor);
1279
1280 pixman_region32_translate(&view->transform.boundingbox,
1281 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001282
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 if (view->alpha == 1.0) {
1284 pixman_region32_copy(&view->transform.opaque,
1285 &view->surface->opaque);
1286 pixman_region32_translate(&view->transform.opaque,
1287 view->geometry.x,
1288 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001289 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001290}
1291
1292static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001294{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 struct weston_view *parent = view->geometry.parent;
1296 struct weston_matrix *matrix = &view->transform.matrix;
1297 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001298 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001299 pixman_region32_t surfregion;
1300 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001301
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001303
1304 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1306 view->transform.position.matrix.d[12] = view->geometry.x;
1307 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001308
1309 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001311 weston_matrix_multiply(matrix, &tform->matrix);
1312
Pekka Paalanen483243f2013-03-08 14:56:50 +02001313 if (parent)
1314 weston_matrix_multiply(matrix, &parent->transform.matrix);
1315
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001316 if (weston_matrix_invert(inverse, matrix) < 0) {
1317 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 weston_log("error: weston_view %p"
1319 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001320 return -1;
1321 }
1322
Giulio Camuffo148c1992016-09-04 18:50:46 +03001323 if (view->alpha == 1.0 &&
1324 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1325 pixman_region32_copy(&view->transform.opaque,
1326 &view->surface->opaque);
1327 pixman_region32_translate(&view->transform.opaque,
1328 matrix->d[12],
1329 matrix->d[13]);
1330 }
1331
Pekka Paalanen380adf52015-02-16 14:39:11 +02001332 pixman_region32_init_rect(&surfregion, 0, 0,
1333 view->surface->width, view->surface->height);
1334 if (view->geometry.scissor_enabled)
1335 pixman_region32_intersect(&surfregion, &surfregion,
1336 &view->geometry.scissor);
1337 surfbox = pixman_region32_extents(&surfregion);
1338
1339 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1340 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001341
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001342 return 0;
1343}
1344
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001345static struct weston_layer *
1346get_view_layer(struct weston_view *view)
1347{
1348 if (view->parent_view)
1349 return get_view_layer(view->parent_view);
1350 return view->layer_link.layer;
1351}
1352
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001353WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001355{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001356 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001357 struct weston_layer *layer;
1358 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001361 return;
1362
Pekka Paalanen483243f2013-03-08 14:56:50 +02001363 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001365
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001367
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001369
Jason Ekstranda7af7042013-10-12 22:38:11 -05001370 pixman_region32_fini(&view->transform.boundingbox);
1371 pixman_region32_fini(&view->transform.opaque);
1372 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001373
Pekka Paalanencd403622012-01-25 13:37:39 +02001374 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 if (view->geometry.transformation_list.next ==
1376 &view->transform.position.link &&
1377 view->geometry.transformation_list.prev ==
1378 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001379 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001381 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 if (weston_view_update_transform_enable(view) < 0)
1383 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001384 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001385
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001386 layer = get_view_layer(view);
1387 if (layer) {
1388 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001389 pixman_region32_intersect(&view->transform.boundingbox,
1390 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001391 pixman_region32_intersect(&view->transform.opaque,
1392 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001393 pixman_region32_fini(&mask);
1394 }
1395
Pekka Paalanen380adf52015-02-16 14:39:11 +02001396 if (parent) {
1397 if (parent->geometry.scissor_enabled) {
1398 view->geometry.scissor_enabled = true;
1399 weston_view_transfer_scissor(parent, view);
1400 } else {
1401 view->geometry.scissor_enabled = false;
1402 }
1403 }
1404
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001406
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001408
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 wl_signal_emit(&view->surface->compositor->transform_signal,
1410 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001411}
1412
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001413WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001415{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001417
1418 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 * The invariant: if view->geometry.dirty, then all views
1420 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001421 * Corollary: if not parent->geometry.dirty, then all ancestors
1422 * are not dirty.
1423 */
1424
Jason Ekstranda7af7042013-10-12 22:38:11 -05001425 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001426 return;
1427
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001429
Jason Ekstranda7af7042013-10-12 22:38:11 -05001430 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001431 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001433}
1434
1435WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436weston_view_to_global_fixed(struct weston_view *view,
1437 wl_fixed_t vx, wl_fixed_t vy,
1438 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001439{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001440 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001441
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 weston_view_to_global_float(view,
1443 wl_fixed_to_double(vx),
1444 wl_fixed_to_double(vy),
1445 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001446 *x = wl_fixed_from_double(xf);
1447 *y = wl_fixed_from_double(yf);
1448}
1449
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001450WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451weston_view_from_global_float(struct weston_view *view,
1452 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001453{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001455 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1456
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001458
1459 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001460 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001462 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 *vx = 0;
1464 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001465 return;
1466 }
1467
Jason Ekstranda7af7042013-10-12 22:38:11 -05001468 *vx = v.f[0] / v.f[3];
1469 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001470 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471 *vx = x - view->geometry.x;
1472 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001473 }
1474}
1475
1476WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477weston_view_from_global_fixed(struct weston_view *view,
1478 wl_fixed_t x, wl_fixed_t y,
1479 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001480{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001482
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 weston_view_from_global_float(view,
1484 wl_fixed_to_double(x),
1485 wl_fixed_to_double(y),
1486 &vxf, &vyf);
1487 *vx = wl_fixed_from_double(vxf);
1488 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001489}
1490
1491WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492weston_view_from_global(struct weston_view *view,
1493 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001494{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001495 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001496
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1498 *vx = floorf(vxf);
1499 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001500}
1501
Bryce Harrington3f650b82015-12-23 11:01:58 -08001502/**
1503 * \param surface The surface to be repainted
1504 *
1505 * Marks the output(s) that the surface is shown on as needing to be
1506 * repainted. See weston_output_schedule_repaint().
1507 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001508WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001509weston_surface_schedule_repaint(struct weston_surface *surface)
1510{
1511 struct weston_output *output;
1512
1513 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001514 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001515 weston_output_schedule_repaint(output);
1516}
1517
Bryce Harrington3f650b82015-12-23 11:01:58 -08001518/**
1519 * \param view The view to be repainted
1520 *
1521 * Marks the output(s) that the view is shown on as needing to be
1522 * repainted. See weston_output_schedule_repaint().
1523 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001524WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001525weston_view_schedule_repaint(struct weston_view *view)
1526{
1527 struct weston_output *output;
1528
1529 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001530 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 weston_output_schedule_repaint(output);
1532}
1533
Pekka Paalanene508ce62015-02-19 13:59:55 +02001534/**
1535 * XXX: This function does it the wrong way.
1536 * surface->damage is the damage from the client, and causes
1537 * surface_flush_damage() to copy pixels. No window management action can
1538 * cause damage to the client-provided content, warranting re-upload!
1539 *
1540 * Instead of surface->damage, this function should record the damage
1541 * with all the views for this surface to avoid extraneous texture
1542 * uploads.
1543 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001544WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001545weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001546{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001547 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548 0, 0, surface->width,
1549 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001550
Kristian Høgsberg98238702012-08-03 16:29:12 -04001551 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001552}
1553
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001554WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001555weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001556{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001557 if (view->geometry.x == x && view->geometry.y == y)
1558 return;
1559
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 view->geometry.x = x;
1561 view->geometry.y = y;
1562 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001563}
1564
Pekka Paalanen483243f2013-03-08 14:56:50 +02001565static void
1566transform_parent_handle_parent_destroy(struct wl_listener *listener,
1567 void *data)
1568{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001569 struct weston_view *view =
1570 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001571 geometry.parent_destroy_listener);
1572
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001574}
1575
1576WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001578 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001579{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001580 if (view->geometry.parent) {
1581 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1582 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001583
1584 if (!parent)
1585 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001586 }
1587
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001589
Jason Ekstranda7af7042013-10-12 22:38:11 -05001590 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001591 transform_parent_handle_parent_destroy;
1592 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001593 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001594 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001595 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001596 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001597 }
1598
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599 weston_view_geometry_dirty(view);
1600}
1601
Pekka Paalanen380adf52015-02-16 14:39:11 +02001602/** Set a clip mask rectangle on a view
1603 *
1604 * \param view The view to set the clip mask on.
1605 * \param x Top-left corner X coordinate of the clip rectangle.
1606 * \param y Top-left corner Y coordinate of the clip rectangle.
1607 * \param width Width of the clip rectangle, non-negative.
1608 * \param height Height of the clip rectangle, non-negative.
1609 *
1610 * A shell may set a clip mask rectangle on a view. Everything outside
1611 * the rectangle is cut away for input and output purposes: it is
1612 * not drawn and cannot be hit by hit-test based input like pointer
1613 * motion or touch-downs. Everything inside the rectangle will behave
1614 * normally. Clients are unaware of clipping.
1615 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001616 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001617 * mask rectangle does not affect the view position, the view is positioned
1618 * as it would be without a clip. The clip also does not change
1619 * weston_surface::width,height.
1620 *
1621 * The clip mask rectangle is part of transformation inheritance
1622 * (weston_view_set_transform_parent()). A clip set in the root of the
1623 * transformation inheritance tree will affect all views in the tree.
1624 * A clip can be set only on the root view. Attempting to set a clip
1625 * on view that has a transformation parent will fail. Assigning a parent
1626 * to a view that has a clip set will cause the clip to be forgotten.
1627 *
1628 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1629 * on the additional transformations in the child views. These transformations
1630 * may not rotate the coordinate axes, i.e., only translation and scaling
1631 * are allowed. Violating this restriction causes the clipping to malfunction.
1632 * Furthermore, using scaling may cause rounding errors in child clipping.
1633 *
1634 * The clip mask rectangle is not automatically adjusted based on
1635 * wl_surface.attach dx and dy arguments.
1636 *
1637 * A clip mask rectangle can be set only if the compositor capability
1638 * WESTON_CAP_VIEW_CLIP_MASK is present.
1639 *
1640 * This function sets the clip mask rectangle and schedules a repaint for
1641 * the view.
1642 */
1643WL_EXPORT void
1644weston_view_set_mask(struct weston_view *view,
1645 int x, int y, int width, int height)
1646{
1647 struct weston_compositor *compositor = view->surface->compositor;
1648
1649 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1650 weston_log("%s not allowed without capability!\n", __func__);
1651 return;
1652 }
1653
1654 if (view->geometry.parent) {
1655 weston_log("view %p has a parent, clip forbidden!\n", view);
1656 return;
1657 }
1658
1659 if (width < 0 || height < 0) {
1660 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1661 x, y, width, height);
1662 return;
1663 }
1664
1665 pixman_region32_fini(&view->geometry.scissor);
1666 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1667 view->geometry.scissor_enabled = true;
1668 weston_view_geometry_dirty(view);
1669 weston_view_schedule_repaint(view);
1670}
1671
1672/** Remove the clip mask from a view
1673 *
1674 * \param view The view to remove the clip mask from.
1675 *
1676 * Removed the clip mask rectangle and schedules a repaint.
1677 *
1678 * \sa weston_view_set_mask
1679 */
1680WL_EXPORT void
1681weston_view_set_mask_infinite(struct weston_view *view)
1682{
1683 view->geometry.scissor_enabled = false;
1684 weston_view_geometry_dirty(view);
1685 weston_view_schedule_repaint(view);
1686}
1687
Armin Krezović0da12b82016-06-30 06:04:33 +02001688/* Check if view should be displayed
1689 *
1690 * The indicator is set manually when assigning
1691 * a view to a surface.
1692 *
1693 * This needs reworking. See the thread starting at:
1694 *
1695 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1696 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001697WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698weston_view_is_mapped(struct weston_view *view)
1699{
Armin Krezović0da12b82016-06-30 06:04:33 +02001700 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001701}
1702
Philipp Zabel70decd52018-09-03 20:11:15 +02001703/* Check if view is opaque in specified region
1704 *
1705 * \param view The view to check for opacity.
1706 * \param region The region to check for opacity, in view coordinates.
1707 *
1708 * Returns true if the view is opaque in the specified region, because view
1709 * alpha is 1.0 and either the opaque region set by the client contains the
1710 * specified region, or the buffer pixel format or solid color is opaque.
1711 */
1712WL_EXPORT bool
1713weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1714{
1715 pixman_region32_t r;
1716 bool ret = false;
1717
1718 if (ev->alpha < 1.0)
1719 return false;
1720
1721 if (ev->surface->is_opaque)
1722 return true;
1723
1724 if (ev->transform.dirty) {
1725 weston_log("%s: transform dirty", __func__);
1726 return false;
1727 }
1728
1729 pixman_region32_init(&r);
1730 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1731
1732 if (!pixman_region32_not_empty(&r))
1733 ret = true;
1734
1735 pixman_region32_fini(&r);
1736
1737 return ret;
1738}
1739
Armin Krezović0da12b82016-06-30 06:04:33 +02001740/* Check if a surface has a view assigned to it
1741 *
1742 * The indicator is set manually when mapping
1743 * a surface and creating a view for it.
1744 *
1745 * This needs to go. See the thread starting at:
1746 *
1747 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1748 *
1749 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001750WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001751weston_surface_is_mapped(struct weston_surface *surface)
1752{
Armin Krezović0da12b82016-06-30 06:04:33 +02001753 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001754}
1755
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001756static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001757surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001758{
1759 struct weston_view *view;
1760
1761 if (surface->width == width && surface->height == height)
1762 return;
1763
1764 surface->width = width;
1765 surface->height = height;
1766
1767 wl_list_for_each(view, &surface->views, surface_link)
1768 weston_view_geometry_dirty(view);
1769}
1770
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001771WL_EXPORT void
1772weston_surface_set_size(struct weston_surface *surface,
1773 int32_t width, int32_t height)
1774{
1775 assert(!surface->resource);
1776 surface_set_size(surface, width, height);
1777}
1778
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001779static int
1780fixed_round_up_to_int(wl_fixed_t f)
1781{
1782 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1783}
1784
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001785static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001786convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1787 int32_t width, int32_t height,
1788 uint32_t transform,
1789 int32_t scale)
1790{
1791 assert(scale > 0);
1792
1793 switch (transform) {
1794 case WL_OUTPUT_TRANSFORM_NORMAL:
1795 case WL_OUTPUT_TRANSFORM_180:
1796 case WL_OUTPUT_TRANSFORM_FLIPPED:
1797 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1798 *width_out = width / scale;
1799 *height_out = height / scale;
1800 break;
1801 case WL_OUTPUT_TRANSFORM_90:
1802 case WL_OUTPUT_TRANSFORM_270:
1803 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1804 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1805 *width_out = height / scale;
1806 *height_out = width / scale;
1807 break;
1808 default:
1809 assert(0 && "invalid transform");
1810 }
1811}
1812
1813static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001814weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001815{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001816 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001817
1818 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001819 surface->width_from_buffer = 0;
1820 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001821 return;
1822 }
1823
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001824 convert_size_by_transform_scale(&surface->width_from_buffer,
1825 &surface->height_from_buffer,
1826 surface->buffer_ref.buffer->width,
1827 surface->buffer_ref.buffer->height,
1828 vp->buffer.transform,
1829 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001830}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001831
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001832static void
1833weston_surface_update_size(struct weston_surface *surface)
1834{
1835 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1836 int32_t width, height;
1837
1838 width = surface->width_from_buffer;
1839 height = surface->height_from_buffer;
1840
1841 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001842 surface_set_size(surface,
1843 vp->surface.width, vp->surface.height);
1844 return;
1845 }
1846
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001847 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001848 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1849 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1850
1851 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001852 return;
1853 }
1854
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001855 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001856}
1857
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001858WL_EXPORT void
1859weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001860{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001861 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001862}
1863
Jason Ekstranda7af7042013-10-12 22:38:11 -05001864WL_EXPORT struct weston_view *
1865weston_compositor_pick_view(struct weston_compositor *compositor,
1866 wl_fixed_t x, wl_fixed_t y,
1867 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001868{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001870 wl_fixed_t view_x, view_y;
1871 int view_ix, view_iy;
1872 int ix = wl_fixed_to_int(x);
1873 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001874
Jason Ekstranda7af7042013-10-12 22:38:11 -05001875 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001876 if (!pixman_region32_contains_point(
1877 &view->transform.boundingbox, ix, iy, NULL))
1878 continue;
1879
1880 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1881 view_ix = wl_fixed_to_int(view_x);
1882 view_iy = wl_fixed_to_int(view_y);
1883
1884 if (!pixman_region32_contains_point(&view->surface->input,
1885 view_ix, view_iy, NULL))
1886 continue;
1887
Pekka Paalanen380adf52015-02-16 14:39:11 +02001888 if (view->geometry.scissor_enabled &&
1889 !pixman_region32_contains_point(&view->geometry.scissor,
1890 view_ix, view_iy, NULL))
1891 continue;
1892
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001893 *vx = view_x;
1894 *vy = view_y;
1895 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001896 }
1897
Derek Foremanf9318d12015-05-11 15:40:11 -05001898 *vx = wl_fixed_from_int(-1000000);
1899 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001900 return NULL;
1901}
1902
1903static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001904weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001905{
Daniel Stone37816df2012-05-16 18:45:18 +01001906 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001907
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001908 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001909 return;
1910
Daniel Stone37816df2012-05-16 18:45:18 +01001911 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001912 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001913}
1914
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001915WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001916weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001917{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001918 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001919
Jason Ekstranda7af7042013-10-12 22:38:11 -05001920 if (!weston_view_is_mapped(view))
1921 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001922
Jason Ekstranda7af7042013-10-12 22:38:11 -05001923 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02001924 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08001925 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001926 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001927 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001928 wl_list_remove(&view->link);
1929 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001930 view->output_mask = 0;
1931 weston_surface_assign_output(view->surface);
1932
1933 if (weston_surface_is_mapped(view->surface))
1934 return;
1935
1936 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001937 struct weston_touch *touch = weston_seat_get_touch(seat);
1938 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1939 struct weston_keyboard *keyboard =
1940 weston_seat_get_keyboard(seat);
1941
1942 if (keyboard && keyboard->focus == view->surface)
1943 weston_keyboard_set_focus(keyboard, NULL);
1944 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001945 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001946 if (touch && touch->focus == view)
1947 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001948 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001949}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001950
Jason Ekstranda7af7042013-10-12 22:38:11 -05001951WL_EXPORT void
1952weston_surface_unmap(struct weston_surface *surface)
1953{
1954 struct weston_view *view;
1955
Armin Krezovićf8486c32016-06-30 06:04:28 +02001956 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001957 wl_list_for_each(view, &surface->views, surface_link)
1958 weston_view_unmap(view);
1959 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001960}
1961
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001962static void
1963weston_surface_reset_pending_buffer(struct weston_surface *surface)
1964{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001965 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001966 surface->pending.sx = 0;
1967 surface->pending.sy = 0;
1968 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001969 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001970}
1971
Jason Ekstranda7af7042013-10-12 22:38:11 -05001972WL_EXPORT void
1973weston_view_destroy(struct weston_view *view)
1974{
1975 wl_signal_emit(&view->destroy_signal, view);
1976
1977 assert(wl_list_empty(&view->geometry.child_list));
1978
1979 if (weston_view_is_mapped(view)) {
1980 weston_view_unmap(view);
1981 weston_compositor_build_view_list(view->surface->compositor);
1982 }
1983
1984 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001985 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001986
1987 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001988 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001989 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001990 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001991
1992 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03001993 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001994
Jason Ekstranda7af7042013-10-12 22:38:11 -05001995 wl_list_remove(&view->surface_link);
1996
1997 free(view);
1998}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001999
2000WL_EXPORT void
2001weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002002{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002003 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002004 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002005 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002006
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002007 if (--surface->ref_count > 0)
2008 return;
2009
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002010 assert(surface->resource == NULL);
2011
Pekka Paalanenca790762015-04-17 14:23:38 +03002012 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002013
Pekka Paalanene67858b2013-04-25 13:57:42 +03002014 assert(wl_list_empty(&surface->subsurface_list_pending));
2015 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002016
Jason Ekstranda7af7042013-10-12 22:38:11 -05002017 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2018 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002019
Jason Ekstrand7b982072014-05-20 14:33:03 -05002020 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002021
Pekka Paalanende685b82012-12-04 15:58:12 +02002022 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002023 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002024
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002025 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002026 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002027 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002028
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002029 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002030 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002031
Pekka Paalanen133e4392014-09-23 22:08:46 -04002032 weston_presentation_feedback_discard_list(&surface->feedback_list);
2033
Jonas Ådahld3414f22016-07-22 17:56:31 +08002034 wl_list_for_each_safe(constraint, next_constraint,
2035 &surface->pointer_constraints,
2036 link)
2037 weston_pointer_constraint_destroy(constraint);
2038
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002039 fd_clear(&surface->acquire_fence_fd);
2040
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002041 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002042}
2043
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002044static void
2045destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002046{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002047 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002048
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002049 assert(surface);
2050
Giulio Camuffo0d379742013-11-15 22:06:15 +01002051 /* Set the resource to NULL, since we don't want to leave a
2052 * dangling pointer if the surface was refcounted and survives
2053 * the weston_surface_destroy() call. */
2054 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002055
2056 if (surface->viewport_resource)
2057 wl_resource_set_user_data(surface->viewport_resource, NULL);
2058
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002059 if (surface->synchronization_resource) {
2060 wl_resource_set_user_data(surface->synchronization_resource,
2061 NULL);
2062 }
2063
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002064 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002065}
2066
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002067static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002068weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2069{
2070 struct weston_buffer *buffer =
2071 container_of(listener, struct weston_buffer, destroy_listener);
2072
2073 wl_signal_emit(&buffer->destroy_signal, buffer);
2074 free(buffer);
2075}
2076
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002077WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002078weston_buffer_from_resource(struct wl_resource *resource)
2079{
2080 struct weston_buffer *buffer;
2081 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002082
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002083 listener = wl_resource_get_destroy_listener(resource,
2084 weston_buffer_destroy_handler);
2085
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002086 if (listener)
2087 return container_of(listener, struct weston_buffer,
2088 destroy_listener);
2089
2090 buffer = zalloc(sizeof *buffer);
2091 if (buffer == NULL)
2092 return NULL;
2093
2094 buffer->resource = resource;
2095 wl_signal_init(&buffer->destroy_signal);
2096 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002097 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002098 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002099
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002100 return buffer;
2101}
2102
2103static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002104weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2105 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002106{
Pekka Paalanende685b82012-12-04 15:58:12 +02002107 struct weston_buffer_reference *ref =
2108 container_of(listener, struct weston_buffer_reference,
2109 destroy_listener);
2110
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002111 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002112 ref->buffer = NULL;
2113}
2114
2115WL_EXPORT void
2116weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002117 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002118{
2119 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002120 ref->buffer->busy_count--;
2121 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002122 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002123 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002124 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002125 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002126 }
2127
Pekka Paalanende685b82012-12-04 15:58:12 +02002128 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002129 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002130 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002131 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002132 }
2133
Pekka Paalanende685b82012-12-04 15:58:12 +02002134 ref->buffer = buffer;
2135 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2136}
2137
2138static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002139weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2140 void *data)
2141{
2142 struct weston_buffer_release_reference *ref =
2143 container_of(listener, struct weston_buffer_release_reference,
2144 destroy_listener);
2145
2146 assert((struct wl_resource *)data == ref->buffer_release->resource);
2147 ref->buffer_release = NULL;
2148}
2149
2150static void
2151weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2152{
2153 struct wl_resource *resource = buffer_release->resource;
2154 int release_fence_fd = buffer_release->fence_fd;
2155
2156 if (release_fence_fd >= 0) {
2157 zwp_linux_buffer_release_v1_send_fenced_release(
2158 resource, release_fence_fd);
2159 } else {
2160 zwp_linux_buffer_release_v1_send_immediate_release(
2161 resource);
2162 }
2163
2164 wl_resource_destroy(resource);
2165}
2166
2167WL_EXPORT void
2168weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2169 struct weston_buffer_release *buffer_release)
2170{
2171 if (buffer_release == ref->buffer_release)
2172 return;
2173
2174 if (ref->buffer_release) {
2175 ref->buffer_release->ref_count--;
2176 wl_list_remove(&ref->destroy_listener.link);
2177 if (ref->buffer_release->ref_count == 0)
2178 weston_buffer_release_destroy(ref->buffer_release);
2179 }
2180
2181 if (buffer_release) {
2182 buffer_release->ref_count++;
2183 wl_resource_add_destroy_listener(buffer_release->resource,
2184 &ref->destroy_listener);
2185 }
2186
2187 ref->buffer_release = buffer_release;
2188 ref->destroy_listener.notify =
2189 weston_buffer_release_reference_handle_destroy;
2190}
2191
2192WL_EXPORT void
2193weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2194 struct weston_buffer_release_reference *src)
2195{
2196 weston_buffer_release_reference(dest, src->buffer_release);
2197 weston_buffer_release_reference(src, NULL);
2198}
2199
2200static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002201weston_surface_attach(struct weston_surface *surface,
2202 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002203{
2204 weston_buffer_reference(&surface->buffer_ref, buffer);
2205
Pekka Paalanena6421c42012-12-04 15:58:10 +02002206 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002207 if (weston_surface_is_mapped(surface))
2208 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002209 }
2210
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002211 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002212
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002213 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002214 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002215}
2216
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002217WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002218weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002219{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002220 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002221
2222 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002223 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002224}
2225
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002226WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002227weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002228{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002229 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002230
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002231 pixman_region32_union(&compositor->primary_plane.damage,
2232 &compositor->primary_plane.damage,
2233 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002234 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002235}
2236
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002237static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002238surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002239{
Pekka Paalanende685b82012-12-04 15:58:12 +02002240 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002241 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002242 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002243
Pekka Paalanenb5026542014-11-12 15:09:24 +02002244 if (weston_timeline_enabled_ &&
2245 pixman_region32_not_empty(&surface->damage))
2246 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2247 TLP_OUTPUT(surface->output), TLP_END);
2248
Jason Ekstrandef540082014-06-26 10:37:36 -07002249 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002250}
2251
2252static void
2253view_accumulate_damage(struct weston_view *view,
2254 pixman_region32_t *opaque)
2255{
2256 pixman_region32_t damage;
2257
2258 pixman_region32_init(&damage);
2259 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002260 pixman_box32_t *extents;
2261
Jason Ekstranda7af7042013-10-12 22:38:11 -05002262 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002263 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002264 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002265 pixman_region32_copy(&damage, &view->surface->damage);
2266 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002267 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002268 }
2269
Pekka Paalanen380adf52015-02-16 14:39:11 +02002270 pixman_region32_intersect(&damage, &damage,
2271 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002272 pixman_region32_subtract(&damage, &damage, opaque);
2273 pixman_region32_union(&view->plane->damage,
2274 &view->plane->damage, &damage);
2275 pixman_region32_fini(&damage);
2276 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002277 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002278}
2279
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002280static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002281compositor_accumulate_damage(struct weston_compositor *ec)
2282{
2283 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002284 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002285 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002286
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002287 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002288
2289 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002290 pixman_region32_copy(&plane->clip, &clip);
2291
2292 pixman_region32_init(&opaque);
2293
Jason Ekstranda7af7042013-10-12 22:38:11 -05002294 wl_list_for_each(ev, &ec->view_list, link) {
2295 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002296 continue;
2297
Jason Ekstranda7af7042013-10-12 22:38:11 -05002298 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002299 }
2300
2301 pixman_region32_union(&clip, &clip, &opaque);
2302 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002303 }
2304
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002305 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002306
Jason Ekstranda7af7042013-10-12 22:38:11 -05002307 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002308 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002309
2310 wl_list_for_each(ev, &ec->view_list, link) {
2311 if (ev->surface->touched)
2312 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002313 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002314
2315 surface_flush_damage(ev->surface);
2316
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002317 /* Both the renderer and the backend have seen the buffer
2318 * by now. If renderer needs the buffer, it has its own
2319 * reference set. If the backend wants to keep the buffer
2320 * around for migrating the surface into a non-primary plane
2321 * later, keep_buffer is true. Otherwise, drop the core
2322 * reference now, and allow early buffer release. This enables
2323 * clients to use single-buffering.
2324 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002325 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002326 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002327 weston_buffer_release_reference(
2328 &ev->surface->buffer_release_ref, NULL);
2329 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002330 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002331}
2332
2333static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002334surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002335{
2336 struct weston_subsurface *sub;
2337
Pekka Paalanene67858b2013-04-25 13:57:42 +03002338 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002339 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002340 continue;
2341
Jason Ekstranda7af7042013-10-12 22:38:11 -05002342 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2343 wl_list_init(&sub->surface->views);
2344
2345 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002346 }
2347}
2348
2349static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002350surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002351{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002352 struct weston_subsurface *sub;
2353 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002354
Jason Ekstranda7af7042013-10-12 22:38:11 -05002355 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2356 if (sub->surface == surface)
2357 continue;
2358
George Kiagiadakised04d382014-06-13 18:10:26 +02002359 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2360 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002361 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002362 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002363
2364 surface_free_unused_subsurface_views(sub->surface);
2365 }
2366}
2367
2368static void
2369view_list_add_subsurface_view(struct weston_compositor *compositor,
2370 struct weston_subsurface *sub,
2371 struct weston_view *parent)
2372{
2373 struct weston_subsurface *child;
2374 struct weston_view *view = NULL, *iv;
2375
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002376 if (!weston_surface_is_mapped(sub->surface))
2377 return;
2378
Jason Ekstranda7af7042013-10-12 22:38:11 -05002379 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2380 if (iv->geometry.parent == parent) {
2381 view = iv;
2382 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002383 }
2384 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002385
2386 if (view) {
2387 /* Put it back in the surface's list of views */
2388 wl_list_remove(&view->surface_link);
2389 wl_list_insert(&sub->surface->views, &view->surface_link);
2390 } else {
2391 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002392 weston_view_set_position(view,
2393 sub->position.x,
2394 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002395 weston_view_set_transform_parent(view, parent);
2396 }
2397
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002398 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002399 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002400 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002401
Pekka Paalanenb188e912013-11-19 14:03:35 +02002402 if (wl_list_empty(&sub->surface->subsurface_list)) {
2403 wl_list_insert(compositor->view_list.prev, &view->link);
2404 return;
2405 }
2406
2407 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2408 if (child->surface == sub->surface)
2409 wl_list_insert(compositor->view_list.prev, &view->link);
2410 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002411 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002412 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002413}
2414
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002415/* This recursively adds the sub-surfaces for a view, relying on the
2416 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2417 * change first happens to the sub-surface list, and then automatically
2418 * propagates here. See weston_surface_damage_subsurfaces() for how the
2419 * sub-surfaces receive damage when the client changes the state.
2420 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002421static void
2422view_list_add(struct weston_compositor *compositor,
2423 struct weston_view *view)
2424{
2425 struct weston_subsurface *sub;
2426
2427 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002428
Pekka Paalanenb188e912013-11-19 14:03:35 +02002429 if (wl_list_empty(&view->surface->subsurface_list)) {
2430 wl_list_insert(compositor->view_list.prev, &view->link);
2431 return;
2432 }
2433
2434 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2435 if (sub->surface == view->surface)
2436 wl_list_insert(compositor->view_list.prev, &view->link);
2437 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002438 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002439 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002440}
2441
2442static void
2443weston_compositor_build_view_list(struct weston_compositor *compositor)
2444{
2445 struct weston_view *view;
2446 struct weston_layer *layer;
2447
2448 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002449 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002450 surface_stash_subsurface_views(view->surface);
2451
2452 wl_list_init(&compositor->view_list);
2453 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002454 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002455 view_list_add(compositor, view);
2456 }
2457 }
2458
2459 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002460 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002461 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002462}
2463
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002464static void
2465weston_output_take_feedback_list(struct weston_output *output,
2466 struct weston_surface *surface)
2467{
2468 struct weston_view *view;
2469 struct weston_presentation_feedback *feedback;
2470 uint32_t flags = 0xffffffff;
2471
2472 if (wl_list_empty(&surface->feedback_list))
2473 return;
2474
2475 /* All views must have the flag for the flag to survive. */
2476 wl_list_for_each(view, &surface->views, surface_link) {
2477 /* ignore views that are not on this output at all */
2478 if (view->output_mask & (1u << output->id))
2479 flags &= view->psf_flags;
2480 }
2481
2482 wl_list_for_each(feedback, &surface->feedback_list, link)
2483 feedback->psf_flags = flags;
2484
2485 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2486 wl_list_init(&surface->feedback_list);
2487}
2488
David Herrmann1edf44c2013-10-22 17:11:26 +02002489static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002490weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002491{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002492 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002493 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002494 struct weston_animation *animation, *next;
2495 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002496 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002497 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002498 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002499 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002500
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002501 if (output->destroying)
2502 return 0;
2503
Pekka Paalanenb5026542014-11-12 15:09:24 +02002504 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2505
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002506 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002507 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002508
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002509 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002510 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002511 } else {
2512 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002513 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002514 ev->psf_flags = 0;
2515 }
2516 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002517
Pekka Paalanene67858b2013-04-25 13:57:42 +03002518 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002519 wl_list_for_each(ev, &ec->view_list, link) {
2520 /* Note: This operation is safe to do multiple times on the
2521 * same surface.
2522 */
2523 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002524 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002525 &ev->surface->frame_callback_list);
2526 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002527
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002528 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002529 }
2530 }
2531
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002532 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002533
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002534 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002535 pixman_region32_intersect(&output_damage,
2536 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002537 pixman_region32_subtract(&output_damage,
2538 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002539
Scott Moreauccbf29d2012-02-22 14:21:41 -07002540 if (output->dirty)
2541 weston_output_update_matrix(output);
2542
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002543 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002544
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002545 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002546
Daniel Stone09a97e22017-03-01 11:34:06 +00002547 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002548 if (r == 0)
2549 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002550
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002551 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002552
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002553 frame_time_msec = timespec_to_msec(&output->frame_time);
2554
Jonas Ådahldb773762012-06-13 00:01:21 +02002555 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002556 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002557 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002558 }
2559
Scott Moreaud64cf212012-06-08 19:40:54 -06002560 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002561 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002562 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002563 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002564
Pekka Paalanenb5026542014-11-12 15:09:24 +02002565 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2566
David Herrmann1edf44c2013-10-22 17:11:26 +02002567 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002568}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002569
Pekka Paalanen82919792014-05-21 13:51:49 +03002570static void
2571weston_output_schedule_repaint_reset(struct weston_output *output)
2572{
Daniel Stone05df8c12017-03-03 16:59:42 +00002573 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002574 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002575}
2576
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002577static int
2578weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2579 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002580{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002581 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002582 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002583 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002584
Daniel Stone6847b852017-03-01 11:34:08 +00002585 /* We're not ready yet; come back to make a decision later. */
2586 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002587 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002588
2589 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2590 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002591 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002592
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002593 /* If we're sleeping, drop the repaint machinery entirely; we will
2594 * explicitly repaint all outputs when we come back. */
2595 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2596 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2597 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002598
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002599 /* We don't actually need to repaint this output; drop it from
2600 * repaint until something causes damage. */
2601 if (!output->repaint_needed)
2602 goto err;
2603
2604 /* If repaint fails, we aren't going to get weston_output_finish_frame
2605 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002606 * something schedules a successful repaint later. As repainting may
2607 * take some time, re-read our clock as a courtesy to the next
2608 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002609 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002610 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002611 if (ret != 0)
2612 goto err;
2613
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002614 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002615 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002616
2617err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002618 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002619 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002620}
2621
2622static void
2623output_repaint_timer_arm(struct weston_compositor *compositor)
2624{
2625 struct weston_output *output;
2626 bool any_should_repaint = false;
2627 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002628 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002629
2630 weston_compositor_read_presentation_clock(compositor, &now);
2631
2632 wl_list_for_each(output, &compositor->output_list, link) {
2633 int64_t msec_to_this;
2634
2635 if (output->repaint_status != REPAINT_SCHEDULED)
2636 continue;
2637
2638 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2639 &now);
2640 if (!any_should_repaint || msec_to_this < msec_to_next)
2641 msec_to_next = msec_to_this;
2642
2643 any_should_repaint = true;
2644 }
2645
2646 if (!any_should_repaint)
2647 return;
2648
2649 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2650 * This is a workaround to allow coalescing multiple output repaints
2651 * particularly from weston_output_finish_frame()
2652 * into the same call, which would not happen if we called
2653 * output_repaint_timer_handler() directly.
2654 */
2655 if (msec_to_next < 1)
2656 msec_to_next = 1;
2657
2658 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2659}
2660
2661static int
2662output_repaint_timer_handler(void *data)
2663{
2664 struct weston_compositor *compositor = data;
2665 struct weston_output *output;
2666 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002667 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002668 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002669
2670 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002671
2672 if (compositor->backend->repaint_begin)
2673 repaint_data = compositor->backend->repaint_begin(compositor);
2674
2675 wl_list_for_each(output, &compositor->output_list, link) {
2676 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2677 if (ret)
2678 break;
2679 }
2680
2681 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002682 if (compositor->backend->repaint_flush)
2683 compositor->backend->repaint_flush(compositor,
2684 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002685 } else {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002686 wl_list_for_each(output, &compositor->output_list, link) {
2687 if (output->repainted)
2688 weston_output_schedule_repaint_reset(output);
2689 }
2690
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002691 if (compositor->backend->repaint_cancel)
2692 compositor->backend->repaint_cancel(compositor,
2693 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002694 }
Daniel Stone6847b852017-03-01 11:34:08 +00002695
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002696 wl_list_for_each(output, &compositor->output_list, link)
2697 output->repainted = false;
2698
Daniel Stone6847b852017-03-01 11:34:08 +00002699 output_repaint_timer_arm(compositor);
2700
Pekka Paalanen0513a952014-05-21 16:17:27 +03002701 return 0;
2702}
2703
Kristian Høgsbergef044142011-06-21 15:02:12 -04002704WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002705weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002706 const struct timespec *stamp,
2707 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002708{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002709 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002710 int32_t refresh_nsec;
2711 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002712 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002713
Pekka Paalanenb5026542014-11-12 15:09:24 +02002714
Daniel Stone05df8c12017-03-03 16:59:42 +00002715 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002716 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2717
Daniel Stone6847b852017-03-01 11:34:08 +00002718 weston_compositor_read_presentation_clock(compositor, &now);
2719
Daniel Stone3615ce12017-03-01 11:34:05 +00002720 /* If we haven't been supplied any timestamp at all, we don't have a
2721 * timebase to work against, so any delay just wastes time. Push a
2722 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002723 if (!stamp) {
2724 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002725 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002726 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002727
Marius Vladdf9278a2018-03-06 18:56:23 +02002728 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2729 TLP_VBLANK(stamp), TLP_END);
2730
Pekka Paalanend7894d02015-07-03 15:08:53 +03002731 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002732 weston_presentation_feedback_present_list(&output->feedback_list,
2733 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002734 output->msc,
2735 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002736
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002737 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002738
Daniel Stone6847b852017-03-01 11:34:08 +00002739 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2740 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2741 -compositor->repaint_msec);
2742 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002743
2744 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002745 static bool warned;
2746
2747 if (!warned)
2748 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002749 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002750 warned = true;
2751
Daniel Stone6847b852017-03-01 11:34:08 +00002752 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002753 }
2754
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002755 /* Called from restart_repaint_loop and restart happens already after
2756 * the deadline given by repaint_msec? In that case we delay until
2757 * the deadline of the next frame, to give clients a more predictable
2758 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002759 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2760 msec_rel < 0) {
2761 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2762 timespec_add_nsec(&output->next_repaint,
2763 &output->next_repaint,
2764 refresh_nsec);
2765 }
2766 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002767
Daniel Stone3615ce12017-03-01 11:34:05 +00002768out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002769 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002770 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002771}
2772
2773static void
2774idle_repaint(void *data)
2775{
2776 struct weston_output *output = data;
2777
Daniel Stone05df8c12017-03-03 16:59:42 +00002778 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2779 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002780 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002781 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002782}
2783
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002784WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002785weston_layer_entry_insert(struct weston_layer_entry *list,
2786 struct weston_layer_entry *entry)
2787{
2788 wl_list_insert(&list->link, &entry->link);
2789 entry->layer = list->layer;
2790}
2791
2792WL_EXPORT void
2793weston_layer_entry_remove(struct weston_layer_entry *entry)
2794{
2795 wl_list_remove(&entry->link);
2796 wl_list_init(&entry->link);
2797 entry->layer = NULL;
2798}
2799
Quentin Glidic82681572016-12-17 13:40:51 +01002800
2801/** Initialize the weston_layer struct.
2802 *
2803 * \param compositor The compositor instance
2804 * \param layer The layer to initialize
2805 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002806WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002807weston_layer_init(struct weston_layer *layer,
2808 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002809{
Quentin Glidic82681572016-12-17 13:40:51 +01002810 layer->compositor = compositor;
2811 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002812 wl_list_init(&layer->view_list.link);
2813 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002814 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002815}
2816
2817/** Sets the position of the layer in the layer list. The layer will be placed
2818 * below any layer with the same position value, if any.
2819 * This function is safe to call if the layer is already on the list, but the
2820 * layer may be moved below other layers at the same position, if any.
2821 *
2822 * \param layer The layer to modify
2823 * \param position The position the layer will be placed at
2824 */
2825WL_EXPORT void
2826weston_layer_set_position(struct weston_layer *layer,
2827 enum weston_layer_position position)
2828{
2829 struct weston_layer *below;
2830
2831 wl_list_remove(&layer->link);
2832
2833 /* layer_list is ordered from top to bottom, the last layer being the
2834 * background with the smallest position value */
2835
2836 layer->position = position;
2837 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2838 if (below->position >= layer->position) {
2839 wl_list_insert(&below->link, &layer->link);
2840 return;
2841 }
2842 }
2843 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2844}
2845
2846/** Hide a layer by taking it off the layer list.
2847 * This function is safe to call if the layer is not on the list.
2848 *
2849 * \param layer The layer to hide
2850 */
2851WL_EXPORT void
2852weston_layer_unset_position(struct weston_layer *layer)
2853{
2854 wl_list_remove(&layer->link);
2855 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002856}
2857
2858WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002859weston_layer_set_mask(struct weston_layer *layer,
2860 int x, int y, int width, int height)
2861{
2862 struct weston_view *view;
2863
2864 layer->mask.x1 = x;
2865 layer->mask.x2 = x + width;
2866 layer->mask.y1 = y;
2867 layer->mask.y2 = y + height;
2868
2869 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2870 weston_view_geometry_dirty(view);
2871 }
2872}
2873
2874WL_EXPORT void
2875weston_layer_set_mask_infinite(struct weston_layer *layer)
2876{
2877 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2878 UINT32_MAX, UINT32_MAX);
2879}
2880
Daniel Stone3b775632018-07-20 08:38:25 +01002881WL_EXPORT bool
2882weston_layer_mask_is_infinite(struct weston_layer *layer)
2883{
2884 return layer->mask.x1 == INT32_MIN &&
2885 layer->mask.y1 == INT32_MIN &&
2886 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
2887 layer->mask.y2 == INT32_MIN + UINT32_MAX;
2888}
2889
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002890WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002891weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002892{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002893 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002894 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002895
Bryce Harrington08976ac2016-08-30 12:05:16 -07002896 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2897 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002898 return;
2899
Pekka Paalanenb5026542014-11-12 15:09:24 +02002900 if (!output->repaint_needed)
2901 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2902
Kristian Høgsbergef044142011-06-21 15:02:12 -04002903 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002904 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002905
2906 /* If we already have a repaint scheduled for our idle handler,
2907 * no need to set it again. If the repaint has been called but
2908 * not finished, then weston_output_finish_frame() will notice
2909 * that a repaint is needed and schedule one. */
2910 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002911 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002912
Daniel Stone05df8c12017-03-03 16:59:42 +00002913 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002914 assert(!output->idle_repaint_source);
2915 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2916 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002917 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002918}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002919
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002920WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002921weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2922{
2923 struct weston_output *output;
2924
2925 wl_list_for_each(output, &compositor->output_list, link)
2926 weston_output_schedule_repaint(output);
2927}
2928
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002929static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002930surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002931{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002932 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002933}
2934
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002935static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002936surface_attach(struct wl_client *client,
2937 struct wl_resource *resource,
2938 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2939{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002940 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002941 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002942
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002943 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002944 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002945 if (buffer == NULL) {
2946 wl_client_post_no_memory(client);
2947 return;
2948 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002949 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002950
Pekka Paalanende685b82012-12-04 15:58:12 +02002951 /* Attach, attach, without commit in between does not send
2952 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002953 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002954
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002955 surface->pending.sx = sx;
2956 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002957 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002958}
2959
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002960static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002961surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002962 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002963 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002964{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002965 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002966
Derek Foreman57e92ed2015-11-17 14:11:35 -06002967 if (width <= 0 || height <= 0)
2968 return;
2969
Derek Foreman152254b2015-11-26 14:17:48 -06002970 pixman_region32_union_rect(&surface->pending.damage_surface,
2971 &surface->pending.damage_surface,
2972 x, y, width, height);
2973}
2974
2975static void
2976surface_damage_buffer(struct wl_client *client,
2977 struct wl_resource *resource,
2978 int32_t x, int32_t y, int32_t width, int32_t height)
2979{
2980 struct weston_surface *surface = wl_resource_get_user_data(resource);
2981
2982 if (width <= 0 || height <= 0)
2983 return;
2984
2985 pixman_region32_union_rect(&surface->pending.damage_buffer,
2986 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002987 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002988}
2989
Kristian Høgsberg33418202011-08-16 23:01:28 -04002990static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002991destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002992{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002993 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002994
2995 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002996 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002997}
2998
2999static void
3000surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003001 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003002{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003003 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003004 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003005
3006 cb = malloc(sizeof *cb);
3007 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003008 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003009 return;
3010 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003011
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003012 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3013 callback);
3014 if (cb->resource == NULL) {
3015 free(cb);
3016 wl_resource_post_no_memory(resource);
3017 return;
3018 }
3019
Jason Ekstranda85118c2013-06-27 20:17:02 -05003020 wl_resource_set_implementation(cb->resource, NULL, cb,
3021 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003022
Pekka Paalanenbc106382012-10-10 12:49:31 +03003023 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003024}
3025
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003026static void
3027surface_set_opaque_region(struct wl_client *client,
3028 struct wl_resource *resource,
3029 struct wl_resource *region_resource)
3030{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003031 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003032 struct weston_region *region;
3033
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003034 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003035 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003036 pixman_region32_copy(&surface->pending.opaque,
3037 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003038 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003039 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003040 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003041}
3042
3043static void
3044surface_set_input_region(struct wl_client *client,
3045 struct wl_resource *resource,
3046 struct wl_resource *region_resource)
3047{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003048 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003049 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003050
3051 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003052 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003053 pixman_region32_copy(&surface->pending.input,
3054 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003055 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003056 pixman_region32_fini(&surface->pending.input);
3057 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003058 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003059}
3060
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003061/* Cause damage to this sub-surface and all its children.
3062 *
3063 * This is useful when there are state changes that need an implicit
3064 * damage, e.g. a z-order change.
3065 */
3066static void
3067weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3068{
3069 struct weston_subsurface *child;
3070
3071 weston_surface_damage(sub->surface);
3072 sub->reordered = false;
3073
3074 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3075 if (child != sub)
3076 weston_surface_damage_subsurfaces(child);
3077}
3078
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003079static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003080weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003081{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003082 struct weston_subsurface *sub;
3083
3084 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3085 parent_link_pending) {
3086 wl_list_remove(&sub->parent_link);
3087 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003088
3089 if (sub->reordered)
3090 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003091 }
3092}
3093
3094static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003095weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003096 struct weston_matrix *matrix)
3097{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003098 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003099 double src_width, src_height, dest_width, dest_height;
3100
3101 weston_matrix_init(matrix);
3102
3103 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3104 src_width = surface->width_from_buffer;
3105 src_height = surface->height_from_buffer;
3106 } else {
3107 src_width = wl_fixed_to_double(vp->buffer.src_width);
3108 src_height = wl_fixed_to_double(vp->buffer.src_height);
3109 }
3110
3111 if (vp->surface.width == -1) {
3112 dest_width = src_width;
3113 dest_height = src_height;
3114 } else {
3115 dest_width = vp->surface.width;
3116 dest_height = vp->surface.height;
3117 }
3118
3119 if (src_width != dest_width || src_height != dest_height)
3120 weston_matrix_scale(matrix,
3121 src_width / dest_width,
3122 src_height / dest_height, 1);
3123
3124 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3125 weston_matrix_translate(matrix,
3126 wl_fixed_to_double(vp->buffer.src_x),
3127 wl_fixed_to_double(vp->buffer.src_y),
3128 0);
3129
3130 switch (vp->buffer.transform) {
3131 case WL_OUTPUT_TRANSFORM_FLIPPED:
3132 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3133 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3134 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3135 weston_matrix_scale(matrix, -1, 1, 1);
3136 weston_matrix_translate(matrix,
3137 surface->width_from_buffer, 0, 0);
3138 break;
3139 }
3140
3141 switch (vp->buffer.transform) {
3142 default:
3143 case WL_OUTPUT_TRANSFORM_NORMAL:
3144 case WL_OUTPUT_TRANSFORM_FLIPPED:
3145 break;
3146 case WL_OUTPUT_TRANSFORM_90:
3147 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3148 weston_matrix_rotate_xy(matrix, 0, 1);
3149 weston_matrix_translate(matrix,
3150 surface->height_from_buffer, 0, 0);
3151 break;
3152 case WL_OUTPUT_TRANSFORM_180:
3153 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3154 weston_matrix_rotate_xy(matrix, -1, 0);
3155 weston_matrix_translate(matrix,
3156 surface->width_from_buffer,
3157 surface->height_from_buffer, 0);
3158 break;
3159 case WL_OUTPUT_TRANSFORM_270:
3160 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3161 weston_matrix_rotate_xy(matrix, 0, -1);
3162 weston_matrix_translate(matrix,
3163 0, surface->width_from_buffer, 0);
3164 break;
3165 }
3166
3167 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3168}
3169
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003170/**
3171 * Compute a + b > c while being safe to overflows.
3172 */
3173static bool
3174fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3175{
3176 return (int64_t)a + (int64_t)b > (int64_t)c;
3177}
3178
3179static bool
3180weston_surface_is_pending_viewport_source_valid(
3181 const struct weston_surface *surface)
3182{
3183 const struct weston_surface_state *pend = &surface->pending;
3184 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3185 int width_from_buffer = 0;
3186 int height_from_buffer = 0;
3187 wl_fixed_t w;
3188 wl_fixed_t h;
3189
3190 /* If viewport source rect is not set, it is always ok. */
3191 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3192 return true;
3193
3194 if (pend->newly_attached) {
3195 if (pend->buffer) {
3196 convert_size_by_transform_scale(&width_from_buffer,
3197 &height_from_buffer,
3198 pend->buffer->width,
3199 pend->buffer->height,
3200 vp->buffer.transform,
3201 vp->buffer.scale);
3202 } else {
3203 /* No buffer: viewport is irrelevant. */
3204 return true;
3205 }
3206 } else {
3207 width_from_buffer = surface->width_from_buffer;
3208 height_from_buffer = surface->height_from_buffer;
3209 }
3210
3211 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3212 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3213
3214 /* No buffer: viewport is irrelevant. */
3215 if (width_from_buffer == 0 || height_from_buffer == 0)
3216 return true;
3217
3218 /* overflow checks for wl_fixed_from_int() */
3219 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3220 return false;
3221 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3222 return false;
3223
3224 w = wl_fixed_from_int(width_from_buffer);
3225 h = wl_fixed_from_int(height_from_buffer);
3226
3227 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3228 return false;
3229 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3230 return false;
3231
3232 return true;
3233}
3234
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003235static bool
3236fixed_is_integer(wl_fixed_t v)
3237{
3238 return (v & 0xff) == 0;
3239}
3240
3241static bool
3242weston_surface_is_pending_viewport_dst_size_int(
3243 const struct weston_surface *surface)
3244{
3245 const struct weston_buffer_viewport *vp =
3246 &surface->pending.buffer_viewport;
3247
3248 if (vp->surface.width != -1) {
3249 assert(vp->surface.width > 0 && vp->surface.height > 0);
3250 return true;
3251 }
3252
3253 return fixed_is_integer(vp->buffer.src_width) &&
3254 fixed_is_integer(vp->buffer.src_height);
3255}
3256
Derek Foreman152254b2015-11-26 14:17:48 -06003257/* Translate pending damage in buffer co-ordinates to surface
3258 * co-ordinates and union it with a pixman_region32_t.
3259 * This should only be called after the buffer is attached.
3260 */
3261static void
3262apply_damage_buffer(pixman_region32_t *dest,
3263 struct weston_surface *surface,
3264 struct weston_surface_state *state)
3265{
3266 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3267
3268 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3269 * translated into surface co-ordinates and unioned with
3270 * any other surface damage.
3271 * None of this makes sense if there is no buffer though.
3272 */
3273 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3274 pixman_region32_t buffer_damage;
3275
3276 pixman_region32_intersect_rect(&state->damage_buffer,
3277 &state->damage_buffer,
3278 0, 0, buffer->width,
3279 buffer->height);
3280 pixman_region32_init(&buffer_damage);
3281 weston_matrix_transform_region(&buffer_damage,
3282 &surface->buffer_to_surface_matrix,
3283 &state->damage_buffer);
3284 pixman_region32_union(dest, dest, &buffer_damage);
3285 pixman_region32_fini(&buffer_damage);
3286 }
3287 /* We should clear this on commit even if there was no buffer */
3288 pixman_region32_clear(&state->damage_buffer);
3289}
3290
Jason Ekstrand1e059042014-10-16 10:55:19 -05003291static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003292weston_surface_commit_state(struct weston_surface *surface,
3293 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003294{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003295 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003296 pixman_region32_t opaque;
3297
Alexander Larsson4ea95522013-05-22 14:41:37 +02003298 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003299 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003300 /* wp_viewport.set_source */
3301 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003302 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003303
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003304 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003305 if (state->newly_attached) {
3306 /* zwp_surface_synchronization_v1.set_acquire_fence */
3307 fd_move(&surface->acquire_fence_fd,
3308 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003309 /* zwp_surface_synchronization_v1.get_release */
3310 weston_buffer_release_move(&surface->buffer_release_ref,
3311 &state->buffer_release_ref);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003312
Jason Ekstrand7b982072014-05-20 14:33:03 -05003313 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003314 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003315 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003316 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003317 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003318
Jason Ekstrand1e059042014-10-16 10:55:19 -05003319 weston_surface_build_buffer_matrix(surface,
3320 &surface->surface_to_buffer_matrix);
3321 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3322 &surface->surface_to_buffer_matrix);
3323
Jason Ekstrand7b982072014-05-20 14:33:03 -05003324 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003325 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003326 if (surface->committed)
3327 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003328 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003329
Jason Ekstrand7b982072014-05-20 14:33:03 -05003330 state->sx = 0;
3331 state->sy = 0;
3332 state->newly_attached = 0;
3333 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003334
Derek Foreman152254b2015-11-26 14:17:48 -06003335 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003336 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003337 (pixman_region32_not_empty(&state->damage_surface) ||
3338 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003339 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003340
Pekka Paalanen8e159182012-10-10 12:49:25 +03003341 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003342 &state->damage_surface);
3343
3344 apply_damage_buffer(&surface->damage, surface, state);
3345
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003346 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003347 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003348 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003349
3350 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003351 pixman_region32_init(&opaque);
3352 pixman_region32_intersect_rect(&opaque, &state->opaque,
3353 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003354
3355 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3356 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003357 wl_list_for_each(view, &surface->views, surface_link)
3358 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003359 }
3360
3361 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003362
3363 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003364 pixman_region32_intersect_rect(&surface->input, &state->input,
3365 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003366
Pekka Paalanenbc106382012-10-10 12:49:31 +03003367 /* wl_surface.frame */
3368 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003369 &state->frame_callback_list);
3370 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003371
3372 /* XXX:
3373 * What should happen with a feedback request, if there
3374 * is no wl_buffer attached for this commit?
3375 */
3376
3377 /* presentation.feedback */
3378 wl_list_insert_list(&surface->feedback_list,
3379 &state->feedback_list);
3380 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003381
3382 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003383}
3384
3385static void
3386weston_surface_commit(struct weston_surface *surface)
3387{
3388 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003389
Pekka Paalanene67858b2013-04-25 13:57:42 +03003390 weston_surface_commit_subsurface_order(surface);
3391
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003392 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003393}
3394
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003395static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003396weston_subsurface_commit(struct weston_subsurface *sub);
3397
3398static void
3399weston_subsurface_parent_commit(struct weston_subsurface *sub,
3400 int parent_is_synchronized);
3401
3402static void
3403surface_commit(struct wl_client *client, struct wl_resource *resource)
3404{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003405 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003406 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3407
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003408 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3409 assert(surface->viewport_resource);
3410
3411 wl_resource_post_error(surface->viewport_resource,
3412 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3413 "wl_surface@%d has viewport source outside buffer",
3414 wl_resource_get_id(resource));
3415 return;
3416 }
3417
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003418 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3419 assert(surface->viewport_resource);
3420
3421 wl_resource_post_error(surface->viewport_resource,
3422 WP_VIEWPORT_ERROR_BAD_SIZE,
3423 "wl_surface@%d viewport dst size not integer",
3424 wl_resource_get_id(resource));
3425 return;
3426 }
3427
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003428 if (surface->pending.acquire_fence_fd >= 0) {
3429 assert(surface->synchronization_resource);
3430
3431 if (!surface->pending.buffer) {
3432 fd_clear(&surface->pending.acquire_fence_fd);
3433 wl_resource_post_error(surface->synchronization_resource,
3434 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3435 "wl_surface@%"PRIu32" no buffer for synchronization",
3436 wl_resource_get_id(resource));
3437 return;
3438 }
3439
3440 /* We support fences for both wp_linux_dmabuf and opaque EGL
3441 * buffers, as mandated by minor version 2 of the
3442 * zwp_linux_explicit_synchronization_v1 protocol. Since
3443 * renderers that support fences currently only support these
3444 * two buffer types plus SHM buffers, we can just check for the
3445 * SHM buffer case here.
3446 */
3447 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3448 fd_clear(&surface->pending.acquire_fence_fd);
3449 wl_resource_post_error(surface->synchronization_resource,
3450 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3451 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3452 wl_resource_get_id(resource));
3453 return;
3454 }
3455 }
3456
Alexandros Frantzis67629672018-10-19 12:14:11 +03003457 if (surface->pending.buffer_release_ref.buffer_release &&
3458 !surface->pending.buffer) {
3459 assert(surface->synchronization_resource);
3460
3461 wl_resource_post_error(surface->synchronization_resource,
3462 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3463 "wl_surface@%"PRIu32" no buffer for synchronization",
3464 wl_resource_get_id(resource));
3465 return;
3466 }
3467
Pekka Paalanene67858b2013-04-25 13:57:42 +03003468 if (sub) {
3469 weston_subsurface_commit(sub);
3470 return;
3471 }
3472
3473 weston_surface_commit(surface);
3474
3475 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3476 if (sub->surface != surface)
3477 weston_subsurface_parent_commit(sub, 0);
3478 }
3479}
3480
3481static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003482surface_set_buffer_transform(struct wl_client *client,
3483 struct wl_resource *resource, int transform)
3484{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003485 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003486
Jonny Lamba55f1392014-05-30 12:07:15 +02003487 /* if wl_output.transform grows more members this will need to be updated. */
3488 if (transform < 0 ||
3489 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3490 wl_resource_post_error(resource,
3491 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3492 "buffer transform must be a valid transform "
3493 "('%d' specified)", transform);
3494 return;
3495 }
3496
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003497 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003498 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003499}
3500
Alexander Larsson4ea95522013-05-22 14:41:37 +02003501static void
3502surface_set_buffer_scale(struct wl_client *client,
3503 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003504 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003505{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003506 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003507
Jonny Lamba55f1392014-05-30 12:07:15 +02003508 if (scale < 1) {
3509 wl_resource_post_error(resource,
3510 WL_SURFACE_ERROR_INVALID_SCALE,
3511 "buffer scale must be at least one "
3512 "('%d' specified)", scale);
3513 return;
3514 }
3515
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003516 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003517 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003518}
3519
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003520static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003521 surface_destroy,
3522 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003523 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003524 surface_frame,
3525 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003526 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003527 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003528 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003529 surface_set_buffer_scale,
3530 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003531};
3532
3533static void
3534compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003535 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003536{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003537 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003538 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003539
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003540 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003541 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003542 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003543 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003544 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003545
Jason Ekstranda85118c2013-06-27 20:17:02 -05003546 surface->resource =
3547 wl_resource_create(client, &wl_surface_interface,
3548 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003549 if (surface->resource == NULL) {
3550 weston_surface_destroy(surface);
3551 wl_resource_post_no_memory(resource);
3552 return;
3553 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003554 wl_resource_set_implementation(surface->resource, &surface_interface,
3555 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003556
3557 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003558}
3559
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003560static void
3561destroy_region(struct wl_resource *resource)
3562{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003563 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003564
3565 pixman_region32_fini(&region->region);
3566 free(region);
3567}
3568
3569static void
3570region_destroy(struct wl_client *client, struct wl_resource *resource)
3571{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003572 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003573}
3574
3575static void
3576region_add(struct wl_client *client, struct wl_resource *resource,
3577 int32_t x, int32_t y, int32_t width, int32_t height)
3578{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003579 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003580
3581 pixman_region32_union_rect(&region->region, &region->region,
3582 x, y, width, height);
3583}
3584
3585static void
3586region_subtract(struct wl_client *client, struct wl_resource *resource,
3587 int32_t x, int32_t y, int32_t width, int32_t height)
3588{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003589 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003590 pixman_region32_t rect;
3591
3592 pixman_region32_init_rect(&rect, x, y, width, height);
3593 pixman_region32_subtract(&region->region, &region->region, &rect);
3594 pixman_region32_fini(&rect);
3595}
3596
3597static const struct wl_region_interface region_interface = {
3598 region_destroy,
3599 region_add,
3600 region_subtract
3601};
3602
3603static void
3604compositor_create_region(struct wl_client *client,
3605 struct wl_resource *resource, uint32_t id)
3606{
3607 struct weston_region *region;
3608
3609 region = malloc(sizeof *region);
3610 if (region == NULL) {
3611 wl_resource_post_no_memory(resource);
3612 return;
3613 }
3614
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003615 pixman_region32_init(&region->region);
3616
Jason Ekstranda85118c2013-06-27 20:17:02 -05003617 region->resource =
3618 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003619 if (region->resource == NULL) {
3620 free(region);
3621 wl_resource_post_no_memory(resource);
3622 return;
3623 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003624 wl_resource_set_implementation(region->resource, &region_interface,
3625 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003626}
3627
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003628static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003629 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003630 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003631};
3632
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003633static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003634weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3635{
3636 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003637
Jason Ekstrand7b982072014-05-20 14:33:03 -05003638 weston_surface_commit_state(surface, &sub->cached);
3639 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003640
3641 weston_surface_commit_subsurface_order(surface);
3642
3643 weston_surface_schedule_repaint(surface);
3644
Jason Ekstrand7b982072014-05-20 14:33:03 -05003645 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003646}
3647
3648static void
3649weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3650{
3651 struct weston_surface *surface = sub->surface;
3652
3653 /*
3654 * If this commit would cause the surface to move by the
3655 * attach(dx, dy) parameters, the old damage region must be
3656 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003657 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003658 */
Derek Foreman152254b2015-11-26 14:17:48 -06003659 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003660 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003661 pixman_region32_union(&sub->cached.damage_surface,
3662 &sub->cached.damage_surface,
3663 &surface->pending.damage_surface);
3664 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003665
3666 if (surface->pending.newly_attached) {
3667 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003668 weston_surface_state_set_buffer(&sub->cached,
3669 surface->pending.buffer);
3670 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003671 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003672 weston_presentation_feedback_discard_list(
3673 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003674 /* zwp_surface_synchronization_v1.set_acquire_fence */
3675 fd_move(&sub->cached.acquire_fence_fd,
3676 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003677 /* zwp_surface_synchronization_v1.get_release */
3678 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3679 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003680 }
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003681 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003682 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003683 sub->cached.sx += surface->pending.sx;
3684 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003685
Derek Foreman152254b2015-11-26 14:17:48 -06003686 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3687
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003688 sub->cached.buffer_viewport.changed |=
3689 surface->pending.buffer_viewport.changed;
3690 sub->cached.buffer_viewport.buffer =
3691 surface->pending.buffer_viewport.buffer;
3692 sub->cached.buffer_viewport.surface =
3693 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003694
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003695 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003696
3697 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3698
3699 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3700
3701 wl_list_insert_list(&sub->cached.frame_callback_list,
3702 &surface->pending.frame_callback_list);
3703 wl_list_init(&surface->pending.frame_callback_list);
3704
Pekka Paalanen133e4392014-09-23 22:08:46 -04003705 wl_list_insert_list(&sub->cached.feedback_list,
3706 &surface->pending.feedback_list);
3707 wl_list_init(&surface->pending.feedback_list);
3708
Jason Ekstrand7b982072014-05-20 14:33:03 -05003709 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003710}
3711
Derek Foreman280e7dd2014-10-03 13:13:42 -05003712static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003713weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3714{
3715 while (sub) {
3716 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003717 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003718
3719 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003720 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003721
3722 sub = weston_surface_to_subsurface(sub->parent);
3723 }
3724
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003725 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003726}
3727
3728static void
3729weston_subsurface_commit(struct weston_subsurface *sub)
3730{
3731 struct weston_surface *surface = sub->surface;
3732 struct weston_subsurface *tmp;
3733
3734 /* Recursive check for effectively synchronized. */
3735 if (weston_subsurface_is_synchronized(sub)) {
3736 weston_subsurface_commit_to_cache(sub);
3737 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003738 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003739 /* flush accumulated state from cache */
3740 weston_subsurface_commit_to_cache(sub);
3741 weston_subsurface_commit_from_cache(sub);
3742 } else {
3743 weston_surface_commit(surface);
3744 }
3745
3746 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3747 if (tmp->surface != surface)
3748 weston_subsurface_parent_commit(tmp, 0);
3749 }
3750 }
3751}
3752
3753static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003754weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003755{
3756 struct weston_surface *surface = sub->surface;
3757 struct weston_subsurface *tmp;
3758
Pekka Paalanene67858b2013-04-25 13:57:42 +03003759 /* From now on, commit_from_cache the whole sub-tree, regardless of
3760 * the synchronized mode of each child. This sub-surface or some
3761 * of its ancestors were synchronized, so we are synchronized
3762 * all the way down.
3763 */
3764
Jason Ekstrand7b982072014-05-20 14:33:03 -05003765 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003766 weston_subsurface_commit_from_cache(sub);
3767
3768 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3769 if (tmp->surface != surface)
3770 weston_subsurface_parent_commit(tmp, 1);
3771 }
3772}
3773
3774static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003775weston_subsurface_parent_commit(struct weston_subsurface *sub,
3776 int parent_is_synchronized)
3777{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003778 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003779 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003780 wl_list_for_each(view, &sub->surface->views, surface_link)
3781 weston_view_set_position(view,
3782 sub->position.x,
3783 sub->position.y);
3784
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003785 sub->position.set = 0;
3786 }
3787
3788 if (parent_is_synchronized || sub->synchronized)
3789 weston_subsurface_synchronized_commit(sub);
3790}
3791
Pekka Paalanen8274d902014-08-06 19:36:51 +03003792static int
3793subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3794{
3795 return snprintf(buf, len, "sub-surface");
3796}
3797
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003798static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003799subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003800{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003801 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003802
Jason Ekstranda7af7042013-10-12 22:38:11 -05003803 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003804 weston_view_set_position(view,
3805 view->geometry.x + dx,
3806 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003807
3808 /* No need to check parent mappedness, because if parent is not
3809 * mapped, parent is not in a visible layer, so this sub-surface
3810 * will not be drawn either.
3811 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003812
Pekka Paalanene67858b2013-04-25 13:57:42 +03003813 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003814 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003815
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003816 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003817 * because that would call it also for the parent surface,
3818 * which might not be mapped yet. That would lead to
3819 * inconsistent state, where the window could never be
3820 * mapped.
3821 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003822 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003823 * weston_surface_is_mapped() return true, so that when the
3824 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003825 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003826 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003827 }
3828}
3829
3830static struct weston_subsurface *
3831weston_surface_to_subsurface(struct weston_surface *surface)
3832{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003833 if (surface->committed == subsurface_committed)
3834 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003835
3836 return NULL;
3837}
3838
Pekka Paalanen01388e22013-04-25 13:57:44 +03003839WL_EXPORT struct weston_surface *
3840weston_surface_get_main_surface(struct weston_surface *surface)
3841{
3842 struct weston_subsurface *sub;
3843
3844 while (surface && (sub = weston_surface_to_subsurface(surface)))
3845 surface = sub->parent;
3846
3847 return surface;
3848}
3849
Pekka Paalanen50b67472014-10-01 15:02:41 +03003850WL_EXPORT int
3851weston_surface_set_role(struct weston_surface *surface,
3852 const char *role_name,
3853 struct wl_resource *error_resource,
3854 uint32_t error_code)
3855{
3856 assert(role_name);
3857
3858 if (surface->role_name == NULL ||
3859 surface->role_name == role_name ||
3860 strcmp(surface->role_name, role_name) == 0) {
3861 surface->role_name = role_name;
3862
3863 return 0;
3864 }
3865
3866 wl_resource_post_error(error_resource, error_code,
3867 "Cannot assign role %s to wl_surface@%d,"
3868 " already has role %s\n",
3869 role_name,
3870 wl_resource_get_id(surface->resource),
3871 surface->role_name);
3872 return -1;
3873}
3874
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003875WL_EXPORT const char *
3876weston_surface_get_role(struct weston_surface *surface)
3877{
3878 return surface->role_name;
3879}
3880
Pekka Paalanen8274d902014-08-06 19:36:51 +03003881WL_EXPORT void
3882weston_surface_set_label_func(struct weston_surface *surface,
3883 int (*desc)(struct weston_surface *,
3884 char *, size_t))
3885{
3886 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003887 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003888}
3889
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003890/** Get the size of surface contents
3891 *
3892 * \param surface The surface to query.
3893 * \param width Returns the width of raw contents.
3894 * \param height Returns the height of raw contents.
3895 *
3896 * Retrieves the raw surface content size in pixels for the given surface.
3897 * This is the whole content size in buffer pixels. If the surface
3898 * has no content or the renderer does not implement this feature,
3899 * zeroes are returned.
3900 *
3901 * This function is used to determine the buffer size needed for
3902 * a weston_surface_copy_content() call.
3903 */
3904WL_EXPORT void
3905weston_surface_get_content_size(struct weston_surface *surface,
3906 int *width, int *height)
3907{
3908 struct weston_renderer *rer = surface->compositor->renderer;
3909
3910 if (!rer->surface_get_content_size) {
3911 *width = 0;
3912 *height = 0;
3913 return;
3914 }
3915
3916 rer->surface_get_content_size(surface, width, height);
3917}
3918
Quentin Glidic248dd102016-08-12 10:41:34 +02003919/** Get the bounding box of a surface and its subsurfaces
3920 *
3921 * \param surface The surface to query.
3922 * \return The bounding box relative to the surface origin.
3923 *
3924 */
3925WL_EXPORT struct weston_geometry
3926weston_surface_get_bounding_box(struct weston_surface *surface)
3927{
3928 pixman_region32_t region;
3929 pixman_box32_t *box;
3930 struct weston_subsurface *subsurface;
3931
3932 pixman_region32_init_rect(&region,
3933 0, 0,
3934 surface->width, surface->height);
3935
3936 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3937 pixman_region32_union_rect(&region, &region,
3938 subsurface->position.x,
3939 subsurface->position.y,
3940 subsurface->surface->width,
3941 subsurface->surface->height);
3942
3943 box = pixman_region32_extents(&region);
3944 struct weston_geometry geometry = {
3945 .x = box->x1,
3946 .y = box->y1,
3947 .width = box->x2 - box->x1,
3948 .height = box->y2 - box->y1,
3949 };
3950
3951 pixman_region32_fini(&region);
3952
3953 return geometry;
3954}
3955
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003956/** Copy surface contents to system memory.
3957 *
3958 * \param surface The surface to copy from.
3959 * \param target Pointer to the target memory buffer.
3960 * \param size Size of the target buffer in bytes.
3961 * \param src_x X location on contents to copy from.
3962 * \param src_y Y location on contents to copy from.
3963 * \param width Width in pixels of the area to copy.
3964 * \param height Height in pixels of the area to copy.
3965 * \return 0 for success, -1 for failure.
3966 *
3967 * Surface contents are maintained by the renderer. They can be in a
3968 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3969 * else.
3970 *
3971 * Surface contents are copied into memory pointed to by target,
3972 * which has size bytes of space available. The target memory
3973 * may be larger than needed, but being smaller returns an error.
3974 * The extra bytes in target may or may not be written; their content is
3975 * unspecified. Size must be large enough to hold the image.
3976 *
3977 * The image in the target memory will be arranged in rows from
3978 * top to bottom, and pixels on a row from left to right. The pixel
3979 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3980 * width * 4.
3981 *
3982 * Parameters src_x and src_y define the upper-left corner in buffer
3983 * coordinates (pixels) to copy from. Parameters width and height
3984 * define the size of the area to copy in pixels.
3985 *
3986 * The rectangle defined by src_x, src_y, width, height must fit in
3987 * the surface contents. Otherwise an error is returned.
3988 *
Changwoo Chof97d2502017-08-05 00:30:47 +09003989 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003990 * needed target buffer size and rectangle limits.
3991 *
3992 * CURRENT IMPLEMENTATION RESTRICTIONS:
3993 * - the machine must be little-endian due to Pixman formats.
3994 *
3995 * NOTE: Pixman formats are premultiplied.
3996 */
3997WL_EXPORT int
3998weston_surface_copy_content(struct weston_surface *surface,
3999 void *target, size_t size,
4000 int src_x, int src_y,
4001 int width, int height)
4002{
4003 struct weston_renderer *rer = surface->compositor->renderer;
4004 int cw, ch;
4005 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4006
4007 if (!rer->surface_copy_content)
4008 return -1;
4009
4010 weston_surface_get_content_size(surface, &cw, &ch);
4011
4012 if (src_x < 0 || src_y < 0)
4013 return -1;
4014
4015 if (width <= 0 || height <= 0)
4016 return -1;
4017
4018 if (src_x + width > cw || src_y + height > ch)
4019 return -1;
4020
4021 if (width * bytespp * height > size)
4022 return -1;
4023
4024 return rer->surface_copy_content(surface, target, size,
4025 src_x, src_y, width, height);
4026}
4027
Pekka Paalanene67858b2013-04-25 13:57:42 +03004028static void
4029subsurface_set_position(struct wl_client *client,
4030 struct wl_resource *resource, int32_t x, int32_t y)
4031{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004032 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004033
4034 if (!sub)
4035 return;
4036
4037 sub->position.x = x;
4038 sub->position.y = y;
4039 sub->position.set = 1;
4040}
4041
4042static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004043subsurface_find_sibling(struct weston_subsurface *sub,
4044 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004045{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004046 struct weston_surface *parent = sub->parent;
4047 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004048
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004049 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4050 if (sibling->surface == surface && sibling != sub)
4051 return sibling;
4052 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004053
4054 return NULL;
4055}
4056
4057static struct weston_subsurface *
4058subsurface_sibling_check(struct weston_subsurface *sub,
4059 struct weston_surface *surface,
4060 const char *request)
4061{
4062 struct weston_subsurface *sibling;
4063
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004064 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004065 if (!sibling) {
4066 wl_resource_post_error(sub->resource,
4067 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4068 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004069 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004070 return NULL;
4071 }
4072
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004073 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004074
4075 return sibling;
4076}
4077
4078static void
4079subsurface_place_above(struct wl_client *client,
4080 struct wl_resource *resource,
4081 struct wl_resource *sibling_resource)
4082{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004083 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004084 struct weston_surface *surface =
4085 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004086 struct weston_subsurface *sibling;
4087
4088 if (!sub)
4089 return;
4090
4091 sibling = subsurface_sibling_check(sub, surface, "place_above");
4092 if (!sibling)
4093 return;
4094
4095 wl_list_remove(&sub->parent_link_pending);
4096 wl_list_insert(sibling->parent_link_pending.prev,
4097 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004098
4099 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004100}
4101
4102static void
4103subsurface_place_below(struct wl_client *client,
4104 struct wl_resource *resource,
4105 struct wl_resource *sibling_resource)
4106{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004107 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004108 struct weston_surface *surface =
4109 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004110 struct weston_subsurface *sibling;
4111
4112 if (!sub)
4113 return;
4114
4115 sibling = subsurface_sibling_check(sub, surface, "place_below");
4116 if (!sibling)
4117 return;
4118
4119 wl_list_remove(&sub->parent_link_pending);
4120 wl_list_insert(&sibling->parent_link_pending,
4121 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004122
4123 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004124}
4125
4126static void
4127subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4128{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004129 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004130
4131 if (sub)
4132 sub->synchronized = 1;
4133}
4134
4135static void
4136subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4137{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004138 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004139
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004140 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004141 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004142
4143 /* If sub became effectively desynchronized, flush. */
4144 if (!weston_subsurface_is_synchronized(sub))
4145 weston_subsurface_synchronized_commit(sub);
4146 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004147}
4148
4149static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004150weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4151{
4152 wl_list_remove(&sub->parent_link);
4153 wl_list_remove(&sub->parent_link_pending);
4154 wl_list_remove(&sub->parent_destroy_listener.link);
4155 sub->parent = NULL;
4156}
4157
4158static void
4159weston_subsurface_destroy(struct weston_subsurface *sub);
4160
4161static void
4162subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4163{
4164 struct weston_subsurface *sub =
4165 container_of(listener, struct weston_subsurface,
4166 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004167 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004168
4169 /* The protocol object (wl_resource) is left inert. */
4170 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004171 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004172
4173 weston_subsurface_destroy(sub);
4174}
4175
4176static void
4177subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4178{
4179 struct weston_subsurface *sub =
4180 container_of(listener, struct weston_subsurface,
4181 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004182 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004183 assert(sub->surface != sub->parent);
4184
4185 if (weston_surface_is_mapped(sub->surface))
4186 weston_surface_unmap(sub->surface);
4187
4188 weston_subsurface_unlink_parent(sub);
4189}
4190
4191static void
4192subsurface_resource_destroy(struct wl_resource *resource)
4193{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004194 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004195
4196 if (sub)
4197 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004198}
4199
4200static void
4201subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4202{
4203 wl_resource_destroy(resource);
4204}
4205
4206static void
4207weston_subsurface_link_parent(struct weston_subsurface *sub,
4208 struct weston_surface *parent)
4209{
4210 sub->parent = parent;
4211 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004212 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004213 &sub->parent_destroy_listener);
4214
4215 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4216 wl_list_insert(&parent->subsurface_list_pending,
4217 &sub->parent_link_pending);
4218}
4219
4220static void
4221weston_subsurface_link_surface(struct weston_subsurface *sub,
4222 struct weston_surface *surface)
4223{
4224 sub->surface = surface;
4225 sub->surface_destroy_listener.notify =
4226 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004227 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004228 &sub->surface_destroy_listener);
4229}
4230
4231static void
4232weston_subsurface_destroy(struct weston_subsurface *sub)
4233{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004234 struct weston_view *view, *next;
4235
Pekka Paalanene67858b2013-04-25 13:57:42 +03004236 assert(sub->surface);
4237
4238 if (sub->resource) {
4239 assert(weston_surface_to_subsurface(sub->surface) == sub);
4240 assert(sub->parent_destroy_listener.notify ==
4241 subsurface_handle_parent_destroy);
4242
George Kiagiadakised04d382014-06-13 18:10:26 +02004243 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4244 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004245 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004246 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004247
Pekka Paalanene67858b2013-04-25 13:57:42 +03004248 if (sub->parent)
4249 weston_subsurface_unlink_parent(sub);
4250
Jason Ekstrand7b982072014-05-20 14:33:03 -05004251 weston_surface_state_fini(&sub->cached);
4252 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004253
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004254 sub->surface->committed = NULL;
4255 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004256 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004257 } else {
4258 /* the dummy weston_subsurface for the parent itself */
4259 assert(sub->parent_destroy_listener.notify == NULL);
4260 wl_list_remove(&sub->parent_link);
4261 wl_list_remove(&sub->parent_link_pending);
4262 }
4263
4264 wl_list_remove(&sub->surface_destroy_listener.link);
4265 free(sub);
4266}
4267
4268static const struct wl_subsurface_interface subsurface_implementation = {
4269 subsurface_destroy,
4270 subsurface_set_position,
4271 subsurface_place_above,
4272 subsurface_place_below,
4273 subsurface_set_sync,
4274 subsurface_set_desync
4275};
4276
4277static struct weston_subsurface *
4278weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4279 struct weston_surface *parent)
4280{
4281 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004282 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004283
Bryce Harringtonde16d892014-11-20 22:21:57 -08004284 sub = zalloc(sizeof *sub);
4285 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004286 return NULL;
4287
Jason Ekstranda7af7042013-10-12 22:38:11 -05004288 wl_list_init(&sub->unused_views);
4289
Jason Ekstranda85118c2013-06-27 20:17:02 -05004290 sub->resource =
4291 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004292 if (!sub->resource) {
4293 free(sub);
4294 return NULL;
4295 }
4296
Jason Ekstranda85118c2013-06-27 20:17:02 -05004297 wl_resource_set_implementation(sub->resource,
4298 &subsurface_implementation,
4299 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004300 weston_subsurface_link_surface(sub, surface);
4301 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004302 weston_surface_state_init(&sub->cached);
4303 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004304 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004305
4306 return sub;
4307}
4308
4309/* Create a dummy subsurface for having the parent itself in its
4310 * sub-surface lists. Makes stacking order manipulation easy.
4311 */
4312static struct weston_subsurface *
4313weston_subsurface_create_for_parent(struct weston_surface *parent)
4314{
4315 struct weston_subsurface *sub;
4316
Bryce Harringtonde16d892014-11-20 22:21:57 -08004317 sub = zalloc(sizeof *sub);
4318 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004319 return NULL;
4320
4321 weston_subsurface_link_surface(sub, parent);
4322 sub->parent = parent;
4323 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4324 wl_list_insert(&parent->subsurface_list_pending,
4325 &sub->parent_link_pending);
4326
4327 return sub;
4328}
4329
4330static void
4331subcompositor_get_subsurface(struct wl_client *client,
4332 struct wl_resource *resource,
4333 uint32_t id,
4334 struct wl_resource *surface_resource,
4335 struct wl_resource *parent_resource)
4336{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004337 struct weston_surface *surface =
4338 wl_resource_get_user_data(surface_resource);
4339 struct weston_surface *parent =
4340 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004341 struct weston_subsurface *sub;
4342 static const char where[] = "get_subsurface: wl_subsurface@";
4343
4344 if (surface == parent) {
4345 wl_resource_post_error(resource,
4346 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4347 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004348 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004349 return;
4350 }
4351
4352 if (weston_surface_to_subsurface(surface)) {
4353 wl_resource_post_error(resource,
4354 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4355 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004356 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004357 return;
4358 }
4359
Pekka Paalanen50b67472014-10-01 15:02:41 +03004360 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4361 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004362 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004363
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004364 if (weston_surface_get_main_surface(parent) == surface) {
4365 wl_resource_post_error(resource,
4366 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4367 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004368 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004369 return;
4370 }
4371
Pekka Paalanene67858b2013-04-25 13:57:42 +03004372 /* make sure the parent is in its own list */
4373 if (wl_list_empty(&parent->subsurface_list)) {
4374 if (!weston_subsurface_create_for_parent(parent)) {
4375 wl_resource_post_no_memory(resource);
4376 return;
4377 }
4378 }
4379
4380 sub = weston_subsurface_create(id, surface, parent);
4381 if (!sub) {
4382 wl_resource_post_no_memory(resource);
4383 return;
4384 }
4385
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004386 surface->committed = subsurface_committed;
4387 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004388 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004389}
4390
4391static void
4392subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4393{
4394 wl_resource_destroy(resource);
4395}
4396
4397static const struct wl_subcompositor_interface subcompositor_interface = {
4398 subcompositor_destroy,
4399 subcompositor_get_subsurface
4400};
4401
4402static void
4403bind_subcompositor(struct wl_client *client,
4404 void *data, uint32_t version, uint32_t id)
4405{
4406 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004407 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004408
Jason Ekstranda85118c2013-06-27 20:17:02 -05004409 resource =
4410 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004411 if (resource == NULL) {
4412 wl_client_post_no_memory(client);
4413 return;
4414 }
4415 wl_resource_set_implementation(resource, &subcompositor_interface,
4416 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004417}
4418
Bryce Harrington0795ece2016-08-30 12:04:26 -07004419/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004420 *
4421 * \param compositor The compositor instance
4422 * \param state The DPMS state the outputs will be set to
4423 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004424static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004425weston_compositor_dpms(struct weston_compositor *compositor,
4426 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004427{
4428 struct weston_output *output;
4429
Bryce Harrington08976ac2016-08-30 12:05:16 -07004430 wl_list_for_each(output, &compositor->output_list, link)
4431 if (output->set_dpms)
4432 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004433}
4434
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004435/** Restores the compositor to active status
4436 *
4437 * \param compositor The compositor instance
4438 *
4439 * If the compositor was in a sleeping mode, all outputs are powered
4440 * back on via DPMS. Otherwise if the compositor was inactive
4441 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4442 * signal will fire.
4443 *
4444 * Restarts the idle timer.
4445 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004446WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004447weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004448{
Neil Roberts8b62e202013-09-30 13:14:47 +01004449 uint32_t old_state = compositor->state;
4450
4451 /* The state needs to be changed before emitting the wake
4452 * signal because that may try to schedule a repaint which
4453 * will not work if the compositor is still sleeping */
4454 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4455
4456 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004457 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004458 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004459 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004460 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004461 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004462 /* fall through */
4463 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004464 wl_event_source_timer_update(compositor->idle_source,
4465 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004466 }
4467}
4468
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004469/** Turns off rendering and frame events for the compositor.
4470 *
4471 * \param compositor The compositor instance
4472 *
4473 * This is used for example to prevent further rendering while the
4474 * compositor is shutting down.
4475 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004476 * Stops the idle timer.
4477 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004478WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004479weston_compositor_offscreen(struct weston_compositor *compositor)
4480{
4481 switch (compositor->state) {
4482 case WESTON_COMPOSITOR_OFFSCREEN:
4483 return;
4484 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004485 default:
4486 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4487 wl_event_source_timer_update(compositor->idle_source, 0);
4488 }
4489}
4490
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004491/** Powers down all attached output devices
4492 *
4493 * \param compositor The compositor instance
4494 *
4495 * Causes rendering to the outputs to cease, and no frame events to be
4496 * sent. Only powers down the outputs if the compositor is not already
4497 * in sleep mode.
4498 *
4499 * Stops the idle timer.
4500 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004501WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004502weston_compositor_sleep(struct weston_compositor *compositor)
4503{
4504 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4505 return;
4506
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004507 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004508 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4509 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4510}
4511
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004512/** Sets compositor to idle mode
4513 *
4514 * \param data The compositor instance
4515 *
4516 * This is called when the idle timer fires. Once the compositor is in
4517 * idle mode it requires a wake action (e.g. via
4518 * weston_compositor_wake()) to restore it. The compositor's
4519 * idle_signal will be triggered when the idle event occurs.
4520 *
4521 * Idleness can be inhibited by setting the compositor's idle_inhibit
4522 * property.
4523 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004524static int
4525idle_handler(void *data)
4526{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004527 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004528
4529 if (compositor->idle_inhibit)
4530 return 1;
4531
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004532 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004533 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004534
4535 return 1;
4536}
4537
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004538WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004539weston_plane_init(struct weston_plane *plane,
4540 struct weston_compositor *ec,
4541 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004542{
4543 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004544 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004545 plane->x = x;
4546 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004547 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004548
4549 /* Init the link so that the call to wl_list_remove() when releasing
4550 * the plane without ever stacking doesn't lead to a crash */
4551 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004552}
4553
4554WL_EXPORT void
4555weston_plane_release(struct weston_plane *plane)
4556{
Xiong Zhang97116532013-10-23 13:58:31 +08004557 struct weston_view *view;
4558
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004559 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004560 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004561
Xiong Zhang97116532013-10-23 13:58:31 +08004562 wl_list_for_each(view, &plane->compositor->view_list, link) {
4563 if (view->plane == plane)
4564 view->plane = NULL;
4565 }
4566
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004567 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004568}
4569
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004570WL_EXPORT void
4571weston_compositor_stack_plane(struct weston_compositor *ec,
4572 struct weston_plane *plane,
4573 struct weston_plane *above)
4574{
4575 if (above)
4576 wl_list_insert(above->link.prev, &plane->link);
4577 else
4578 wl_list_insert(&ec->plane_list, &plane->link);
4579}
4580
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004581static void
4582output_release(struct wl_client *client, struct wl_resource *resource)
4583{
4584 wl_resource_destroy(resource);
4585}
4586
4587static const struct wl_output_interface output_interface = {
4588 output_release,
4589};
4590
4591
Casey Dahlin9074db52012-04-19 22:50:09 -04004592static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004593{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004594 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004595}
4596
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004597static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004598bind_output(struct wl_client *client,
4599 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004600{
Pekka Paalanen05347622017-03-27 12:24:34 +03004601 struct weston_head *head = data;
4602 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004603 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004604 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004605
Jason Ekstranda85118c2013-06-27 20:17:02 -05004606 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004607 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004608 if (resource == NULL) {
4609 wl_client_post_no_memory(client);
4610 return;
4611 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004612
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004613 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004614 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004615 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004616
Pekka Paalanen05347622017-03-27 12:24:34 +03004617 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004618 wl_output_send_geometry(resource,
4619 output->x,
4620 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004621 head->mm_width,
4622 head->mm_height,
4623 head->subpixel,
4624 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004625 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004626 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004627 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004628 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004629
4630 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004631 wl_output_send_mode(resource,
4632 mode->flags,
4633 mode->width,
4634 mode->height,
4635 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004636 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004637
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004638 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004639 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004640}
4641
Pekka Paalanendcac3512017-12-08 14:13:34 +02004642static void
4643weston_head_add_global(struct weston_head *head)
4644{
4645 head->global = wl_global_create(head->compositor->wl_display,
4646 &wl_output_interface, 3,
4647 head, bind_output);
4648}
4649
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004650/** Remove the global wl_output protocol object
4651 *
4652 * \param head The head whose global to remove.
4653 *
4654 * Also orphans the wl_resources for this head (wl_output).
4655 */
4656static void
4657weston_head_remove_global(struct weston_head *head)
4658{
4659 struct wl_resource *resource, *tmp;
4660
4661 if (head->global)
4662 wl_global_destroy(head->global);
4663 head->global = NULL;
4664
4665 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4666 unbind_resource(resource);
4667 wl_resource_set_destructor(resource, NULL);
4668 wl_resource_set_user_data(resource, NULL);
4669 }
4670}
4671
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004672/** Get the backing object of wl_output
4673 *
4674 * \param resource A wl_output protocol object.
4675 * \return The backing object (user data) of a wl_resource representing a
4676 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004677 *
4678 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004679 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004680WL_EXPORT struct weston_head *
4681weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004682{
4683 assert(wl_resource_instance_of(resource, &wl_output_interface,
4684 &output_interface));
4685
4686 return wl_resource_get_user_data(resource);
4687}
4688
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004689/** Initialize a pre-allocated weston_head
4690 *
4691 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004692 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004693 *
4694 * The head will be safe to attach, detach and release.
4695 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004696 * The name is used in logs, and can be used by compositors as a configuration
4697 * identifier.
4698 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004699 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004700 * \internal
4701 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004702WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004703weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004704{
4705 /* Add some (in)sane defaults which can be used
4706 * for checking if an output was properly configured
4707 */
4708 memset(head, 0, sizeof *head);
4709
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004710 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004711 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004712 wl_list_init(&head->output_link);
4713 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004714 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004715}
4716
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004717/** Send output heads changed signal
4718 *
4719 * \param output The output that changed.
4720 *
4721 * Notify that the enabled output gained and/or lost heads, or that the
4722 * associated heads may have changed their connection status. This does not
4723 * include cases where the output becomes enabled or disabled. The registered
4724 * callbacks are called after the change has successfully happened.
4725 *
4726 * If connection status change causes the compositor to attach or detach a head
4727 * to an enabled output, the registered callbacks may be called multiple times.
4728 */
4729static void
4730weston_output_emit_heads_changed(struct weston_output *output)
4731{
4732 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4733 output);
4734}
4735
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004736/** Idle task for emitting heads_changed_signal */
4737static void
4738weston_compositor_call_heads_changed(void *data)
4739{
4740 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004741 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004742
4743 compositor->heads_changed_source = NULL;
4744
4745 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004746
4747 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4748 if (head->output && head->output->enabled)
4749 weston_output_emit_heads_changed(head->output);
4750 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004751}
4752
4753/** Schedule a call on idle to heads_changed callback
4754 *
4755 * \param compositor The Compositor.
4756 *
4757 * \memberof weston_compositor
4758 * \internal
4759 */
4760static void
4761weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4762{
4763 struct wl_event_loop *loop;
4764
4765 if (compositor->heads_changed_source)
4766 return;
4767
4768 loop = wl_display_get_event_loop(compositor->wl_display);
4769 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4770 weston_compositor_call_heads_changed, compositor);
4771}
4772
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004773/** Register a new head
4774 *
4775 * \param compositor The compositor.
4776 * \param head The head to register, must not be already registered.
4777 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004778 * This signals the core that a new head has become available, leading to
4779 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004780 *
4781 * \memberof weston_compositor
4782 * \internal
4783 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004784WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004785weston_compositor_add_head(struct weston_compositor *compositor,
4786 struct weston_head *head)
4787{
4788 assert(wl_list_empty(&head->compositor_link));
4789 assert(head->name);
4790
4791 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4792 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004793 weston_compositor_schedule_heads_changed(compositor);
4794}
4795
4796/** Adds a listener to be called when heads change
4797 *
4798 * \param compositor The compositor.
4799 * \param listener The listener to add.
4800 *
Marius Vlada2dace22019-06-12 16:05:44 +03004801 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004802 *
4803 * The listener function will be called after heads are added or their
4804 * connection status has changed. Several changes may be accumulated into a
4805 * single call. The user is expected to iterate over the existing heads and
4806 * check their statuses to find out what changed.
4807 *
4808 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4809 * weston_head_is_enabled
4810 * \memberof weston_compositor
4811 */
4812WL_EXPORT void
4813weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4814 struct wl_listener *listener)
4815{
4816 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004817}
4818
4819/** Iterate over available heads
4820 *
4821 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03004822 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004823 * \return The next available head in the list.
4824 *
4825 * Returns all available heads, regardless of being connected or enabled.
4826 *
4827 * You can iterate over all heads as follows:
4828 * \code
4829 * struct weston_head *head = NULL;
4830 *
4831 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4832 * ...
4833 * }
4834 * \endcode
4835 *
4836 * If you cause \c iter to be removed from the list, you cannot use it to
4837 * continue iterating. Removing any other item is safe.
4838 *
4839 * \memberof weston_compositor
4840 */
4841WL_EXPORT struct weston_head *
4842weston_compositor_iterate_heads(struct weston_compositor *compositor,
4843 struct weston_head *iter)
4844{
4845 struct wl_list *list = &compositor->head_list;
4846 struct wl_list *node;
4847
4848 assert(compositor);
4849 assert(!iter || iter->compositor == compositor);
4850
4851 if (iter)
4852 node = iter->compositor_link.next;
4853 else
4854 node = list->next;
4855
4856 assert(node);
4857 assert(!iter || node != &iter->compositor_link);
4858
4859 if (node == list)
4860 return NULL;
4861
4862 return container_of(node, struct weston_head, compositor_link);
4863}
4864
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004865/** Iterate over attached heads
4866 *
4867 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03004868 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004869 * \return The next attached head in the list.
4870 *
4871 * Returns all heads currently attached to the output.
4872 *
4873 * You can iterate over all heads as follows:
4874 * \code
4875 * struct weston_head *head = NULL;
4876 *
4877 * while ((head = weston_output_iterate_heads(output, head))) {
4878 * ...
4879 * }
4880 * \endcode
4881 *
4882 * If you cause \c iter to be removed from the list, you cannot use it to
4883 * continue iterating. Removing any other item is safe.
4884 *
4885 * \memberof weston_compositor
4886 */
4887WL_EXPORT struct weston_head *
4888weston_output_iterate_heads(struct weston_output *output,
4889 struct weston_head *iter)
4890{
4891 struct wl_list *list = &output->head_list;
4892 struct wl_list *node;
4893
4894 assert(output);
4895 assert(!iter || iter->output == output);
4896
4897 if (iter)
4898 node = iter->output_link.next;
4899 else
4900 node = list->next;
4901
4902 assert(node);
4903 assert(!iter || node != &iter->output_link);
4904
4905 if (node == list)
4906 return NULL;
4907
4908 return container_of(node, struct weston_head, output_link);
4909}
4910
Pekka Paalanendcac3512017-12-08 14:13:34 +02004911/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004912 *
4913 * \param output The output to attach to.
4914 * \param head The head that is not yet attached.
4915 * \return 0 on success, -1 on failure.
4916 *
4917 * Attaches the given head to the output. All heads of an output are clones
4918 * and share the resolution and timings.
4919 *
4920 * Cloning heads this way uses less resources than creating an output for
4921 * each head, but is not always possible due to environment, driver and hardware
4922 * limitations.
4923 *
4924 * On failure, the head remains unattached. Success of this function does not
4925 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004926 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004927 *
4928 * \memberof weston_output
4929 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004930WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004931weston_output_attach_head(struct weston_output *output,
4932 struct weston_head *head)
4933{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004934 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004935
4936 if (!wl_list_empty(&head->output_link))
4937 return -1;
4938
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004939 if (output->attach_head) {
4940 if (output->attach_head(output, head) < 0)
4941 return -1;
4942 } else if (!wl_list_empty(&output->head_list)) {
4943 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004944 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004945 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004946
4947 head->output = output;
4948 wl_list_insert(output->head_list.prev, &head->output_link);
4949
Pekka Paalanendcac3512017-12-08 14:13:34 +02004950 if (output->enabled) {
4951 weston_head_add_global(head);
4952
4953 head_names = weston_output_create_heads_string(output);
4954 weston_log("Output '%s' updated to have head(s) %s\n",
4955 output->name, head_names);
4956 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004957
4958 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02004959 }
4960
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004961 return 0;
4962}
4963
4964/** Detach a head from its output
4965 *
4966 * \param head The head to detach.
4967 *
4968 * It is safe to detach a non-attached head.
4969 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004970 * If the head is attached to an enabled output and the output will be left
4971 * with no heads, the output will be disabled.
4972 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004973 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004974 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004975 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004976WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004977weston_head_detach(struct weston_head *head)
4978{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004979 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02004980 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004981
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004982 wl_list_remove(&head->output_link);
4983 wl_list_init(&head->output_link);
4984 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004985
4986 if (!output)
4987 return;
4988
4989 if (output->detach_head)
4990 output->detach_head(output, head);
4991
4992 if (output->enabled) {
4993 weston_head_remove_global(head);
4994
Pekka Paalanena0106992017-12-08 16:11:17 +02004995 if (wl_list_empty(&output->head_list)) {
4996 weston_log("Output '%s' no heads left, disabling.\n",
4997 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004998 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004999 } else {
5000 head_names = weston_output_create_heads_string(output);
5001 weston_log("Output '%s' updated to have head(s) %s\n",
5002 output->name, head_names);
5003 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005004
5005 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005006 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005007 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005008}
5009
5010/** Destroy a head
5011 *
5012 * \param head The head to be released.
5013 *
5014 * Destroys the head. The caller is responsible for freeing the memory pointed
5015 * to by \c head.
5016 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005017 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005018 * \internal
5019 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005020WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005021weston_head_release(struct weston_head *head)
5022{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005023 wl_signal_emit(&head->destroy_signal, head);
5024
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005025 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005026
5027 free(head->make);
5028 free(head->model);
5029 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005030 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005031
5032 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005033}
5034
Pekka Paalanene19970f2017-08-28 14:11:02 +03005035static void
5036weston_head_set_device_changed(struct weston_head *head)
5037{
5038 head->device_changed = true;
5039
5040 if (head->compositor)
5041 weston_compositor_schedule_heads_changed(head->compositor);
5042}
5043
5044/** String equal comparison with NULLs being equal */
5045static bool
5046str_null_eq(const char *a, const char *b)
5047{
5048 if (!a && !b)
5049 return true;
5050
5051 if (!!a != !!b)
5052 return false;
5053
5054 return strcmp(a, b) == 0;
5055}
5056
Pekka Paalanen01f60212017-03-24 15:39:24 +02005057/** Store monitor make, model and serial number
5058 *
5059 * \param head The head to modify.
5060 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5061 * any string, or NULL for none.
5062 * \param model The monitor model or name, or a made-up string, or NULL for
5063 * none.
5064 * \param serialno The monitor serial number, a made-up string, or NULL for
5065 * none.
5066 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005067 * This may set the device_changed flag.
5068 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005069 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005070 * \internal
5071 */
5072WL_EXPORT void
5073weston_head_set_monitor_strings(struct weston_head *head,
5074 const char *make,
5075 const char *model,
5076 const char *serialno)
5077{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005078 if (str_null_eq(head->make, make) &&
5079 str_null_eq(head->model, model) &&
5080 str_null_eq(head->serial_number, serialno))
5081 return;
5082
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005083 free(head->make);
5084 free(head->model);
5085 free(head->serial_number);
5086
5087 head->make = make ? strdup(make) : NULL;
5088 head->model = model ? strdup(model) : NULL;
5089 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005090
5091 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005092}
5093
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005094/** Store display non-desktop status
5095 *
5096 * \param head The head to modify.
5097 * \param non_desktop Whether the head connects to a non-desktop display.
5098 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005099 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005100 * \internal
5101 */
5102WL_EXPORT void
5103weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5104{
5105 if (head->non_desktop == non_desktop)
5106 return;
5107
5108 head->non_desktop = non_desktop;
5109
5110 weston_head_set_device_changed(head);
5111}
5112
Pekka Paalanen01f60212017-03-24 15:39:24 +02005113/** Store physical image size
5114 *
5115 * \param head The head to modify.
5116 * \param mm_width Image area width in millimeters.
5117 * \param mm_height Image area height in millimeters.
5118 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005119 * This may set the device_changed flag.
5120 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005121 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005122 * \internal
5123 */
5124WL_EXPORT void
5125weston_head_set_physical_size(struct weston_head *head,
5126 int32_t mm_width, int32_t mm_height)
5127{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005128 if (head->mm_width == mm_width &&
5129 head->mm_height == mm_height)
5130 return;
5131
Pekka Paalanen01f60212017-03-24 15:39:24 +02005132 head->mm_width = mm_width;
5133 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005134
5135 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005136}
5137
5138/** Store monitor sub-pixel layout
5139 *
5140 * \param head The head to modify.
5141 * \param sp Sub-pixel layout. The possible values are:
5142 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5143 * - WL_OUTPUT_SUBPIXEL_NONE,
5144 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5145 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5146 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5147 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5148 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005149 * This may set the device_changed flag.
5150 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005151 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005152 * \internal
5153 */
5154WL_EXPORT void
5155weston_head_set_subpixel(struct weston_head *head,
5156 enum wl_output_subpixel sp)
5157{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005158 if (head->subpixel == sp)
5159 return;
5160
Pekka Paalanen01f60212017-03-24 15:39:24 +02005161 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005162
5163 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005164}
5165
5166/** Mark the monitor as internal
5167 *
5168 * This is used for embedded screens, like laptop panels.
5169 *
5170 * \param head The head to mark as internal.
5171 *
5172 * By default a head is external. The type is often inferred from the physical
5173 * connector type.
5174 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005175 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005176 * \internal
5177 */
5178WL_EXPORT void
5179weston_head_set_internal(struct weston_head *head)
5180{
5181 head->connection_internal = true;
5182}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005183
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005184/** Store connector status
5185 *
5186 * \param head The head to modify.
5187 * \param connected Whether the head is connected.
5188 *
5189 * Connectors are created as disconnected. This function can be used to
5190 * set the connector status.
5191 *
5192 * The status should be set to true when a physical connector is connected to
5193 * a video sink device like a monitor and to false when the connector is
5194 * disconnected. For nested backends, the connection status should reflect the
5195 * connection to the parent display server.
5196 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005197 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005198 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005199 *
5200 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005201 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005202 * \internal
5203 */
5204WL_EXPORT void
5205weston_head_set_connection_status(struct weston_head *head, bool connected)
5206{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005207 if (head->connected == connected)
5208 return;
5209
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005210 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005211
Pekka Paalanene19970f2017-08-28 14:11:02 +03005212 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005213}
5214
5215/** Is the head currently connected?
5216 *
5217 * \param head The head to query.
5218 * \return Connection status.
5219 *
5220 * Returns true if the head is physically connected to a monitor, or in
5221 * case of a nested backend returns true when there is a connection to the
5222 * parent display server.
5223 *
5224 * This is independent from the head being enabled.
5225 *
5226 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005227 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005228 */
5229WL_EXPORT bool
5230weston_head_is_connected(struct weston_head *head)
5231{
5232 return head->connected;
5233}
5234
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005235/** Is the head currently enabled?
5236 *
5237 * \param head The head to query.
5238 * \return Video status.
5239 *
5240 * Returns true if the head is currently transmitting a video stream.
5241 *
5242 * This is independent of the head being connected.
5243 *
5244 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005245 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005246 */
5247WL_EXPORT bool
5248weston_head_is_enabled(struct weston_head *head)
5249{
5250 if (!head->output)
5251 return false;
5252
5253 return head->output->enabled;
5254}
5255
Pekka Paalanene19970f2017-08-28 14:11:02 +03005256/** Has the device information changed?
5257 *
5258 * \param head The head to query.
5259 * \return True if the device information has changed since last reset.
5260 *
5261 * The information about the connected display device, e.g. a monitor, may
5262 * change without being disconnected in between. Changing information
5263 * causes a call to the heads_changed hook.
5264 *
5265 * The information includes make, model, serial number, physical size,
5266 * and sub-pixel type. The connection status is also included.
5267 *
5268 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005269 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005270 */
5271WL_EXPORT bool
5272weston_head_is_device_changed(struct weston_head *head)
5273{
5274 return head->device_changed;
5275}
5276
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005277/** Does the head represent a non-desktop display?
5278 *
5279 * \param head The head to query.
5280 * \return True if the device is a non-desktop display.
5281 *
5282 * Non-desktop heads are not attached to outputs by default.
5283 * This stops weston from extending the desktop onto head mounted displays.
5284 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005285 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005286 */
5287WL_EXPORT bool
5288weston_head_is_non_desktop(struct weston_head *head)
5289{
5290 return head->non_desktop;
5291}
5292
Pekka Paalanene19970f2017-08-28 14:11:02 +03005293/** Acknowledge device information change
5294 *
5295 * \param head The head to acknowledge.
5296 *
5297 * Clears the device changed flag on this head. When a compositor has processed
5298 * device information, it should call this to be able to notice further
5299 * changes.
5300 *
5301 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005302 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005303 */
5304WL_EXPORT void
5305weston_head_reset_device_changed(struct weston_head *head)
5306{
5307 head->device_changed = false;
5308}
5309
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005310/** Get the name of a head
5311 *
5312 * \param head The head to query.
5313 * \return The head's name, not NULL.
5314 *
5315 * The name depends on the backend. The DRM backend uses connector names,
5316 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005317 *
5318 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005319 */
5320WL_EXPORT const char *
5321weston_head_get_name(struct weston_head *head)
5322{
5323 return head->name;
5324}
5325
5326/** Get the output the head is attached to
5327 *
5328 * \param head The head to query.
5329 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005330 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005331 */
5332WL_EXPORT struct weston_output *
5333weston_head_get_output(struct weston_head *head)
5334{
5335 return head->output;
5336}
5337
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005338/** Add destroy callback for a head
5339 *
5340 * \param head The head to watch for.
5341 * \param listener The listener to add. The \c notify member must be set.
5342 *
5343 * Heads may get destroyed for various reasons by the backends. If a head is
5344 * attached to an output, the compositor should listen for head destruction
5345 * and reconfigure or destroy the output if necessary.
5346 *
5347 * The destroy callbacks will be called on weston_head destruction before any
5348 * automatic detaching from an associated weston_output and before any
5349 * weston_head information is lost.
5350 *
5351 * The \c data argument to the notify callback is the weston_head being
5352 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005353 *
5354 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005355 */
5356WL_EXPORT void
5357weston_head_add_destroy_listener(struct weston_head *head,
5358 struct wl_listener *listener)
5359{
5360 wl_signal_add(&head->destroy_signal, listener);
5361}
5362
5363/** Look up destroy listener for a head
5364 *
5365 * \param head The head to query.
5366 * \param notify The notify function used used for the added destroy listener.
5367 * \return The listener, or NULL if not found.
5368 *
5369 * This looks up the previously added destroy listener struct based on the
5370 * notify function it has. The listener can be used to access user data
5371 * through \c container_of().
5372 *
5373 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005374 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005375 */
5376WL_EXPORT struct wl_listener *
5377weston_head_get_destroy_listener(struct weston_head *head,
5378 wl_notify_func_t notify)
5379{
5380 return wl_signal_get(&head->destroy_signal, notify);
5381}
5382
David Fort0de859e2016-05-27 23:22:57 +02005383/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005384static void
David Fort0de859e2016-05-27 23:22:57 +02005385weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5386 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005387{
5388 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005389 bool start_resizing = false;
5390
5391 if (!delta_width)
5392 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005393
5394 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005395 if (output == resized_output) {
5396 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005397 continue;
5398 }
5399
David Fort0de859e2016-05-27 23:22:57 +02005400 if (start_resizing) {
5401 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005402 output->dirty = 1;
5403 }
5404 }
5405}
5406
Pekka Paalanend72bad22017-03-29 17:01:41 +03005407static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005408weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005409{
Scott Moreau850ca422012-05-21 15:21:25 -06005410 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005411
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005412 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005413 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005414
Scott Moreauccbf29d2012-02-22 14:21:41 -07005415 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005416 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005417 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005418 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005419 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005420 weston_matrix_scale(&output->matrix, magnification,
5421 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005422 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005423
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005424 switch (output->transform) {
5425 case WL_OUTPUT_TRANSFORM_FLIPPED:
5426 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5427 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5428 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5429 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5430 weston_matrix_scale(&output->matrix, -1, 1, 1);
5431 break;
5432 }
5433
5434 switch (output->transform) {
5435 default:
5436 case WL_OUTPUT_TRANSFORM_NORMAL:
5437 case WL_OUTPUT_TRANSFORM_FLIPPED:
5438 break;
5439 case WL_OUTPUT_TRANSFORM_90:
5440 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5441 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5442 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5443 break;
5444 case WL_OUTPUT_TRANSFORM_180:
5445 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5446 weston_matrix_translate(&output->matrix,
5447 -output->width, -output->height, 0);
5448 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5449 break;
5450 case WL_OUTPUT_TRANSFORM_270:
5451 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5452 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5453 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5454 break;
5455 }
5456
5457 if (output->current_scale != 1)
5458 weston_matrix_scale(&output->matrix,
5459 output->current_scale,
5460 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005461
Scott Moreauccbf29d2012-02-22 14:21:41 -07005462 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005463
5464 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005465}
5466
Scott Moreau1bad5db2012-08-18 01:04:05 -06005467static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005468weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005469{
5470 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005471 output->native_scale = scale;
5472 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005473
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005474 convert_size_by_transform_scale(&output->width, &output->height,
5475 output->current_mode->width,
5476 output->current_mode->height,
5477 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005478}
5479
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005480static void
5481weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005482{
5483 output->x = x;
5484 output->y = y;
5485
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005486 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005487 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005488
5489 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005490 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005491 output->width,
5492 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005493}
5494
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005495WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005496weston_output_move(struct weston_output *output, int x, int y)
5497{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005498 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005499 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005500 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005501
5502 output->move_x = x - output->x;
5503 output->move_y = y - output->y;
5504
5505 if (output->move_x == 0 && output->move_y == 0)
5506 return;
5507
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005508 weston_output_init_geometry(output, x, y);
5509
5510 output->dirty = 1;
5511
5512 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005513 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005514
5515 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005516 wl_list_for_each(head, &output->head_list, output_link) {
5517 wl_resource_for_each(resource, &head->resource_list) {
5518 wl_output_send_geometry(resource,
5519 output->x,
5520 output->y,
5521 head->mm_width,
5522 head->mm_height,
5523 head->subpixel,
5524 head->make,
5525 head->model,
5526 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005527
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005528 ver = wl_resource_get_version(resource);
5529 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5530 wl_output_send_done(resource);
5531 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005532 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005533}
5534
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005535/** Signal that a pending output is taken into use.
5536 *
5537 * Removes the output from the pending list and adds it to the compositor's
5538 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005539 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005540 * The output gets an internal ID assigned, and the wl_output global is
5541 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005542 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005543 * \param compositor The compositor instance.
5544 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005545 *
5546 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005547 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005548static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005549weston_compositor_add_output(struct weston_compositor *compositor,
5550 struct weston_output *output)
5551{
Armin Krezoviće5403842016-08-05 15:28:29 +02005552 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005553 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005554
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005555 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005556
5557 /* Verify we haven't reached the limit of 32 available output IDs */
5558 assert(ffs(~compositor->output_id_pool) > 0);
5559
5560 /* Invert the output id pool and look for the lowest numbered
5561 * switch (the least significant bit). Take that bit's position
5562 * as our ID, and mark it used in the compositor's output_id_pool.
5563 */
5564 output->id = ffs(~compositor->output_id_pool) - 1;
5565 compositor->output_id_pool |= 1u << output->id;
5566
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005567 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005568 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005569 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005570
Pekka Paalanendcac3512017-12-08 14:13:34 +02005571 wl_list_for_each(head, &output->head_list, output_link)
5572 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005573
Giulio Camuffob1147152015-05-06 21:41:57 +03005574 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005575
5576 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5577 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005578}
5579
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005580/** Transform device coordinates into global coordinates
5581 *
Marius Vlada2dace22019-06-12 16:05:44 +03005582 * \param output the weston_output object
5583 * \param[in] device_x X coordinate in device units.
5584 * \param[in] device_y Y coordinate in device units.
5585 * \param[out] x X coordinate in the global space.
5586 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005587 *
Marius Vlada2dace22019-06-12 16:05:44 +03005588 * Transforms coordinates from the device coordinate space (physical pixel
5589 * units) to the global coordinate space (logical pixel units). This takes
5590 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005591 *
5592 * \memberof weston_output
5593 * \internal
5594 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005595WL_EXPORT void
5596weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005597 double device_x, double device_y,
5598 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005599{
Derek Foreman0f679412014-10-02 13:41:17 -05005600 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005601 device_x,
5602 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005603 0.0,
5604 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005605
Derek Foreman67a18b92015-03-24 11:36:14 -05005606 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005607
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005608 *x = p.f[0] / p.f[3];
5609 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005610}
5611
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005612/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005613 *
5614 * \param output The weston_output object that is being removed.
5615 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005616 * The following happens:
5617 *
5618 * - The output assignments of all views in the current scenegraph are
5619 * recomputed.
5620 *
5621 * - Presentation feedback is discarded.
5622 *
5623 * - Compositor is notified that outputs were changed and
5624 * applies the necessary changes to re-layout outputs.
5625 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005626 * - The output is put back in the pending outputs list.
5627 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005628 * - Signal is emitted to notify all users of the weston_output
5629 * object that the output is being destroyed.
5630 *
5631 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005632 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005633 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005634 * - The output's internal ID is released.
5635 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005636 * \memberof weston_output
5637 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005638 */
5639static void
5640weston_compositor_remove_output(struct weston_output *output)
5641{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005642 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005643 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005644 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005645
5646 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005647 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005648
Pekka Paalanenbccda712017-03-29 16:16:04 +03005649 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005650 if (view->output_mask & (1u << output->id))
5651 weston_view_assign_output(view);
5652 }
5653
5654 weston_presentation_feedback_discard_list(&output->feedback_list);
5655
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005656 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005657
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005658 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005659 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005660 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005661
Pekka Paalanenbccda712017-03-29 16:16:04 +03005662 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005663 wl_signal_emit(&output->destroy_signal, output);
5664
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005665 wl_list_for_each(head, &output->head_list, output_link)
5666 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005667
5668 compositor->output_id_pool &= ~(1u << output->id);
5669 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005670}
5671
5672/** Sets the output scale for a given output.
5673 *
5674 * \param output The weston_output object that the scale is set for.
5675 * \param scale Scale factor for the given output.
5676 *
5677 * It only supports setting scale for an output that
5678 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005679 *
5680 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005681 */
5682WL_EXPORT void
5683weston_output_set_scale(struct weston_output *output,
5684 int32_t scale)
5685{
5686 /* We can only set scale on a disabled output */
5687 assert(!output->enabled);
5688
5689 /* We only want to set scale once */
5690 assert(!output->scale);
5691
5692 output->scale = scale;
5693}
5694
5695/** Sets the output transform for a given output.
5696 *
5697 * \param output The weston_output object that the transform is set for.
5698 * \param transform Transform value for the given output.
5699 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005700 * Refer to wl_output::transform section located at
5701 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5702 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005703 *
5704 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005705 */
5706WL_EXPORT void
5707weston_output_set_transform(struct weston_output *output,
5708 uint32_t transform)
5709{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005710 struct weston_pointer_motion_event ev;
5711 struct wl_resource *resource;
5712 struct weston_seat *seat;
5713 pixman_region32_t old_region;
5714 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005715 struct weston_head *head;
5716 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005717
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005718 if (!output->enabled && output->transform == UINT32_MAX) {
5719 output->transform = transform;
5720 return;
5721 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005722
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005723 weston_output_transform_scale_init(output, transform, output->scale);
5724
5725 pixman_region32_init(&old_region);
5726 pixman_region32_copy(&old_region, &output->region);
5727
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005728 weston_output_init_geometry(output, output->x, output->y);
5729
5730 output->dirty = 1;
5731
5732 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005733 wl_list_for_each(head, &output->head_list, output_link) {
5734 wl_resource_for_each(resource, &head->resource_list) {
5735 wl_output_send_geometry(resource,
5736 output->x,
5737 output->y,
5738 head->mm_width,
5739 head->mm_height,
5740 head->subpixel,
5741 head->make,
5742 head->model,
5743 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005744
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005745 ver = wl_resource_get_version(resource);
5746 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5747 wl_output_send_done(resource);
5748 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005749 }
5750
5751 /* we must ensure that pointers are inside output, otherwise they disappear */
5752 mid_x = output->x + output->width / 2;
5753 mid_y = output->y + output->height / 2;
5754
5755 ev.mask = WESTON_POINTER_MOTION_ABS;
5756 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5757 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5758
5759 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5760 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5761
5762 if (pointer && pixman_region32_contains_point(&old_region,
5763 wl_fixed_to_int(pointer->x),
5764 wl_fixed_to_int(pointer->y),
5765 NULL))
5766 weston_pointer_move(pointer, &ev);
5767 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005768}
5769
5770/** Initializes a weston_output object with enough data so
5771 ** an output can be configured.
5772 *
5773 * \param output The weston_output object to initialize
5774 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005775 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005776 *
5777 * Sets initial values for fields that are expected to be
5778 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005779 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005780 * The name is used in logs, and can be used by compositors as a configuration
5781 * identifier.
5782 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005783 * \memberof weston_output
5784 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005785 */
5786WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005787weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005788 struct weston_compositor *compositor,
5789 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005790{
5791 output->compositor = compositor;
5792 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005793 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005794 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005795 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005796 output->enabled = false;
5797
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005798 wl_list_init(&output->head_list);
5799
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005800 /* Add some (in)sane defaults which can be used
5801 * for checking if an output was properly configured
5802 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005803 output->scale = 0;
5804 /* Can't use -1 on uint32_t and 0 is valid enum value */
5805 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005806
5807 pixman_region32_init(&output->previous_damage);
5808 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005809 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005810}
5811
5812/** Adds weston_output object to pending output list.
5813 *
5814 * \param output The weston_output object to add
5815 * \param compositor The compositor instance.
5816 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005817 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005818 *
5819 * \memberof weston_output
5820 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005821 */
5822WL_EXPORT void
5823weston_compositor_add_pending_output(struct weston_output *output,
5824 struct weston_compositor *compositor)
5825{
Pekka Paalanene952a012017-03-29 17:14:00 +03005826 assert(output->disable);
5827 assert(output->enable);
5828
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005829 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005830 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005831}
5832
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005833/** Create a string with the attached heads' names.
5834 *
5835 * The string must be free()'d.
5836 */
5837static char *
5838weston_output_create_heads_string(struct weston_output *output)
5839{
5840 FILE *fp;
5841 char *str = NULL;
5842 size_t size = 0;
5843 struct weston_head *head;
5844 const char *sep = "";
5845
5846 fp = open_memstream(&str, &size);
5847 if (!fp)
5848 return NULL;
5849
5850 wl_list_for_each(head, &output->head_list, output_link) {
5851 fprintf(fp, "%s%s", sep, head->name);
5852 sep = ", ";
5853 }
5854 fclose(fp);
5855
5856 return str;
5857}
5858
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005859/** Constructs a weston_output object that can be used by the compositor.
5860 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005861 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005862 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005863 *
5864 * Output coordinates are calculated and each new output is by default
5865 * assigned to the right of previous one.
5866 *
5867 * Sets up the transformation, zoom, and geometry of the output using
5868 * the properties that need to be configured by the compositor.
5869 *
5870 * Establishes a repaint timer for the output with the relevant display
5871 * object's event loop. See output_repaint_timer_handler().
5872 *
5873 * The output is assigned an ID. Weston can support up to 32 distinct
5874 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5875 * is referred to and used to find the first available ID number, and
5876 * then this ID is marked as used in output_id_pool.
5877 *
5878 * The output is also assigned a Wayland global with the wl_output
5879 * external interface.
5880 *
5881 * Backend specific function is called to set up the output output.
5882 *
5883 * Output is added to the compositor's output list
5884 *
5885 * If the backend specific function fails, the weston_output object
5886 * is returned to a state it was before calling this function and
5887 * is added to the compositor's pending_output_list in case it needs
5888 * to be reconfigured or just so it can be destroyed at shutdown.
5889 *
5890 * 0 is returned on success, -1 on failure.
5891 */
5892WL_EXPORT int
5893weston_output_enable(struct weston_output *output)
5894{
Armin Krezović782f5df2016-09-30 14:11:11 +02005895 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005896 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005897 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005898 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005899 int x = 0, y = 0;
5900
Pekka Paalanencc201e42017-03-30 15:11:25 +03005901 if (output->enabled) {
5902 weston_log("Error: attempt to enable an enabled output '%s'\n",
5903 output->name);
5904 return -1;
5905 }
5906
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005907 if (wl_list_empty(&output->head_list)) {
5908 weston_log("Error: cannot enable output '%s' without heads.\n",
5909 output->name);
5910 return -1;
5911 }
5912
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005913 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5914 weston_log("Error: no video mode for output '%s'.\n",
5915 output->name);
5916 return -1;
5917 }
5918
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005919 wl_list_for_each(head, &output->head_list, output_link) {
5920 assert(head->make);
5921 assert(head->model);
5922 }
5923
Armin Krezović782f5df2016-09-30 14:11:11 +02005924 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005925 struct weston_output, link);
5926
Armin Krezović782f5df2016-09-30 14:11:11 +02005927 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005928 x = iterator->x + iterator->width;
5929
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005930 /* Make sure the scale is set up */
5931 assert(output->scale);
5932
5933 /* Make sure we have a transform set */
5934 assert(output->transform != UINT32_MAX);
5935
Armin Krezović782f5df2016-09-30 14:11:11 +02005936 output->x = x;
5937 output->y = y;
5938 output->dirty = 1;
5939 output->original_scale = output->scale;
5940
5941 weston_output_transform_scale_init(output, output->transform, output->scale);
5942 weston_output_init_zoom(output);
5943
5944 weston_output_init_geometry(output, x, y);
5945 weston_output_damage(output);
5946
5947 wl_signal_init(&output->frame_signal);
5948 wl_signal_init(&output->destroy_signal);
5949 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005950 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005951
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005952 /* Enable the output (set up the crtc or create a
5953 * window representing the output, set up the
5954 * renderer, etc)
5955 */
5956 if (output->enable(output) < 0) {
5957 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005958 return -1;
5959 }
5960
5961 weston_compositor_add_output(output->compositor, output);
5962
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005963 head_names = weston_output_create_heads_string(output);
5964 weston_log("Output '%s' enabled with head(s) %s\n",
5965 output->name, head_names);
5966 free(head_names);
5967
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005968 return 0;
5969}
5970
5971/** Converts a weston_output object to a pending output state, so it
5972 ** can be configured again or destroyed.
5973 *
5974 * \param output The weston_output object that needs to be disabled.
5975 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005976 * Calls a backend specific function to disable an output, in case
5977 * such function exists.
5978 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005979 * The backend specific disable function may choose to postpone the disabling
5980 * by returning a negative value, in which case this function returns early.
5981 * In that case the backend will guarantee the output will be disabled soon
5982 * by the backend calling this function again. One must not attempt to re-enable
5983 * the output until that happens.
5984 *
5985 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005986 * from weston's output_list (see weston_compositor_remove_output())
5987 * and is returned to a state it was before weston_output_enable()
5988 * was ran (see weston_output_enable_undo()).
5989 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005990 * See weston_output_init() for more information on the
5991 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005992 *
5993 * If the output has never been enabled yet, this function can still be
5994 * called to ensure that the output is actually turned off rather than left
5995 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005996 */
5997WL_EXPORT void
5998weston_output_disable(struct weston_output *output)
5999{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006000 /* Should we rename this? */
6001 output->destroying = 1;
6002
Pekka Paalanenc65df642017-03-29 15:45:46 +03006003 /* Disable is called unconditionally also for not-enabled outputs,
6004 * because at compositor start-up, if there is an output that is
6005 * already on but the compositor wants to turn it off, we have to
6006 * forward the turn-off to the backend so it knows to do it.
6007 * The backend cannot initially turn off everything, because it
6008 * would cause unnecessary mode-sets for all outputs the compositor
6009 * wants to be on.
6010 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006011 if (output->disable(output) < 0)
6012 return;
6013
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006014 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006015 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006016
6017 output->destroying = 0;
6018}
6019
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006020/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006021 *
6022 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006023 *
6024 * If there are new or changed heads, calls the heads_changed hook and
6025 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006026 */
6027WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006028weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006029{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006030 if (compositor->heads_changed_source) {
6031 wl_event_source_remove(compositor->heads_changed_source);
6032 weston_compositor_call_heads_changed(compositor);
6033 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006034}
6035
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006036/** Add destroy callback for an output
6037 *
6038 * \param output The output to watch.
6039 * \param listener The listener to add. The \c notify member must be set.
6040 *
6041 * The listener callback will be called when user destroys an output. This
6042 * may be delayed by a backend in some cases. The main purpose of the
6043 * listener is to allow hooking up custom data to the output. The custom data
6044 * can be fetched via weston_output_get_destroy_listener() followed by
6045 * container_of().
6046 *
6047 * The \c data argument to the notify callback is the weston_output being
6048 * destroyed.
6049 *
6050 * @note This is for the final destruction of an output, not when it gets
6051 * disabled. If you want to keep track of enabled outputs, this is not it.
6052 */
6053WL_EXPORT void
6054weston_output_add_destroy_listener(struct weston_output *output,
6055 struct wl_listener *listener)
6056{
6057 wl_signal_add(&output->user_destroy_signal, listener);
6058}
6059
6060/** Look up destroy listener for an output
6061 *
6062 * \param output The output to query.
6063 * \param notify The notify function used used for the added destroy listener.
6064 * \return The listener, or NULL if not found.
6065 *
6066 * This looks up the previously added destroy listener struct based on the
6067 * notify function it has. The listener can be used to access user data
6068 * through \c container_of().
6069 *
6070 * \sa wl_signal_get() weston_output_add_destroy_listener()
6071 */
6072WL_EXPORT struct wl_listener *
6073weston_output_get_destroy_listener(struct weston_output *output,
6074 wl_notify_func_t notify)
6075{
6076 return wl_signal_get(&output->user_destroy_signal, notify);
6077}
6078
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006079/** Uninitialize an output
6080 *
6081 * Removes the output from the list of enabled outputs if necessary, but
6082 * does not call the backend's output disable function. The output will no
6083 * longer be in the list of pending outputs either.
6084 *
6085 * All fields of weston_output become uninitialized, i.e. should not be used
6086 * anymore. The caller can free the memory after this.
6087 *
6088 * \memberof weston_output
6089 * \internal
6090 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006091WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006092weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006093{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006094 struct weston_head *head, *tmp;
6095
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006096 output->destroying = 1;
6097
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006098 wl_signal_emit(&output->user_destroy_signal, output);
6099
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006100 if (output->idle_repaint_source)
6101 wl_event_source_remove(output->idle_repaint_source);
6102
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006103 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006104 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006105
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006106 pixman_region32_fini(&output->region);
6107 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006108 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006109
6110 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6111 weston_head_detach(head);
6112
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006113 free(output->name);
6114}
6115
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006116/** Find an output by its given name
6117 *
6118 * \param compositor The compositor to search in.
6119 * \param name The output name to search for.
6120 * \return An existing output with the given name, or NULL if not found.
6121 *
6122 * \memberof weston_compositor
6123 */
6124WL_EXPORT struct weston_output *
6125weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6126 const char *name)
6127{
6128 struct weston_output *output;
6129
6130 wl_list_for_each(output, &compositor->output_list, link)
6131 if (strcmp(output->name, name) == 0)
6132 return output;
6133
6134 wl_list_for_each(output, &compositor->pending_output_list, link)
6135 if (strcmp(output->name, name) == 0)
6136 return output;
6137
6138 return NULL;
6139}
6140
6141/** Create a named output
6142 *
6143 * \param compositor The compositor.
6144 * \param name The name for the output.
6145 * \return A new \c weston_output, or NULL on failure.
6146 *
6147 * This creates a new weston_output that starts with no heads attached.
6148 *
6149 * An output must be configured and it must have at least one head before
6150 * it can be enabled.
6151 *
6152 * \memberof weston_compositor
6153 */
6154WL_EXPORT struct weston_output *
6155weston_compositor_create_output(struct weston_compositor *compositor,
6156 const char *name)
6157{
6158 assert(compositor->backend->create_output);
6159
6160 if (weston_compositor_find_output_by_name(compositor, name)) {
6161 weston_log("Warning: attempted to create an output with a "
6162 "duplicate name '%s'.\n", name);
6163 return NULL;
6164 }
6165
6166 return compositor->backend->create_output(compositor, name);
6167}
6168
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006169/** Create an output for an unused head
6170 *
6171 * \param compositor The compositor.
6172 * \param head The head to attach to the output.
6173 * \return A new \c weston_output, or NULL on failure.
6174 *
6175 * This creates a new weston_output that starts with the given head attached.
6176 * The output inherits the name of the head. The head must not be already
6177 * attached to another output.
6178 *
6179 * An output must be configured before it can be enabled.
6180 *
6181 * \memberof weston_compositor
6182 */
6183WL_EXPORT struct weston_output *
6184weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6185 struct weston_head *head)
6186{
6187 struct weston_output *output;
6188
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006189 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006190 if (!output)
6191 return NULL;
6192
6193 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006194 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006195 return NULL;
6196 }
6197
6198 return output;
6199}
6200
6201/** Destroy an output
6202 *
6203 * \param output The output to destroy.
6204 *
6205 * The heads attached to the given output are detached and become unused again.
6206 *
6207 * It is not necessary to explicitly destroy all outputs at compositor exit.
6208 * weston_compositor_destroy() will automatically destroy any remaining
6209 * outputs.
6210 *
6211 * \memberof weston_output
6212 */
6213WL_EXPORT void
6214weston_output_destroy(struct weston_output *output)
6215{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006216 output->destroy(output);
6217}
6218
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006219/** When you need a head...
6220 *
6221 * This function is a hack, used until all code has been converted to become
6222 * multi-head aware.
6223 *
6224 * \param output The weston_output whose head to get.
6225 * \return The first head in the output's list.
6226 */
6227WL_EXPORT struct weston_head *
6228weston_output_get_first_head(struct weston_output *output)
6229{
6230 if (wl_list_empty(&output->head_list))
6231 return NULL;
6232
6233 return container_of(output->head_list.next,
6234 struct weston_head, output_link);
6235}
6236
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006237static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006238destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006239{
Jonny Lamb74130762013-11-26 18:19:46 +01006240 struct weston_surface *surface =
6241 wl_resource_get_user_data(resource);
6242
Pekka Paalanen4826f872016-04-22 14:14:38 +03006243 if (!surface)
6244 return;
6245
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006246 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006247 surface->pending.buffer_viewport.buffer.src_width =
6248 wl_fixed_from_int(-1);
6249 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006250 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006251}
6252
6253static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006254viewport_destroy(struct wl_client *client,
6255 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006256{
6257 wl_resource_destroy(resource);
6258}
6259
6260static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006261viewport_set_source(struct wl_client *client,
6262 struct wl_resource *resource,
6263 wl_fixed_t src_x,
6264 wl_fixed_t src_y,
6265 wl_fixed_t src_width,
6266 wl_fixed_t src_height)
6267{
6268 struct weston_surface *surface =
6269 wl_resource_get_user_data(resource);
6270
Pekka Paalanen4826f872016-04-22 14:14:38 +03006271 if (!surface) {
6272 wl_resource_post_error(resource,
6273 WP_VIEWPORT_ERROR_NO_SURFACE,
6274 "wl_surface for this viewport is no longer exists");
6275 return;
6276 }
6277
6278 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006279 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006280
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006281 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006282 src_height == wl_fixed_from_int(-1) &&
6283 src_x == wl_fixed_from_int(-1) &&
6284 src_y == wl_fixed_from_int(-1)) {
6285 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006286 surface->pending.buffer_viewport.buffer.src_width =
6287 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006288 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006289 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006290 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006291
Pekka Paalanen201769a2016-04-26 14:42:11 +03006292 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006293 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006294 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006295 "wl_surface@%d viewport source "
6296 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6297 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006298 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006299 wl_fixed_to_double(src_height),
6300 wl_fixed_to_double(src_x),
6301 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006302 return;
6303 }
6304
6305 surface->pending.buffer_viewport.buffer.src_x = src_x;
6306 surface->pending.buffer_viewport.buffer.src_y = src_y;
6307 surface->pending.buffer_viewport.buffer.src_width = src_width;
6308 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006309 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006310}
6311
6312static void
6313viewport_set_destination(struct wl_client *client,
6314 struct wl_resource *resource,
6315 int32_t dst_width,
6316 int32_t dst_height)
6317{
6318 struct weston_surface *surface =
6319 wl_resource_get_user_data(resource);
6320
Pekka Paalanen4826f872016-04-22 14:14:38 +03006321 if (!surface) {
6322 wl_resource_post_error(resource,
6323 WP_VIEWPORT_ERROR_NO_SURFACE,
6324 "wl_surface for this viewport no longer exists");
6325 return;
6326 }
6327
6328 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006329
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006330 if (dst_width == -1 && dst_height == -1) {
6331 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006332 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006333 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006334 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006335 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006336
6337 if (dst_width <= 0 || dst_height <= 0) {
6338 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006339 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006340 "destination size must be positive (%dx%d)",
6341 dst_width, dst_height);
6342 return;
6343 }
6344
6345 surface->pending.buffer_viewport.surface.width = dst_width;
6346 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006347 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006348}
6349
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006350static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006351 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006352 viewport_set_source,
6353 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006354};
6355
6356static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006357viewporter_destroy(struct wl_client *client,
6358 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006359{
6360 wl_resource_destroy(resource);
6361}
6362
6363static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006364viewporter_get_viewport(struct wl_client *client,
6365 struct wl_resource *viewporter,
6366 uint32_t id,
6367 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006368{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006369 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006370 struct weston_surface *surface =
6371 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006372 struct wl_resource *resource;
6373
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006374 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006375 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006376 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006377 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006378 return;
6379 }
6380
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006381 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006382 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006383 if (resource == NULL) {
6384 wl_client_post_no_memory(client);
6385 return;
6386 }
6387
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006388 wl_resource_set_implementation(resource, &viewport_interface,
6389 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006390
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006391 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006392}
6393
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006394static const struct wp_viewporter_interface viewporter_interface = {
6395 viewporter_destroy,
6396 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006397};
6398
6399static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006400bind_viewporter(struct wl_client *client,
6401 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006402{
6403 struct wl_resource *resource;
6404
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006405 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006406 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006407 if (resource == NULL) {
6408 wl_client_post_no_memory(client);
6409 return;
6410 }
6411
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006412 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006413 NULL, NULL);
6414}
6415
6416static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006417destroy_presentation_feedback(struct wl_resource *feedback_resource)
6418{
6419 struct weston_presentation_feedback *feedback;
6420
6421 feedback = wl_resource_get_user_data(feedback_resource);
6422
6423 wl_list_remove(&feedback->link);
6424 free(feedback);
6425}
6426
6427static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006428presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6429{
6430 wl_resource_destroy(resource);
6431}
6432
6433static void
6434presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006435 struct wl_resource *presentation_resource,
6436 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006437 uint32_t callback)
6438{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006439 struct weston_surface *surface;
6440 struct weston_presentation_feedback *feedback;
6441
6442 surface = wl_resource_get_user_data(surface_resource);
6443
Bryce Harringtonde16d892014-11-20 22:21:57 -08006444 feedback = zalloc(sizeof *feedback);
6445 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006446 goto err_calloc;
6447
6448 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006449 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006450 1, callback);
6451 if (!feedback->resource)
6452 goto err_create;
6453
6454 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6455 destroy_presentation_feedback);
6456
6457 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6458
6459 return;
6460
6461err_create:
6462 free(feedback);
6463
6464err_calloc:
6465 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006466}
6467
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006468static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006469 presentation_destroy,
6470 presentation_feedback
6471};
6472
6473static void
6474bind_presentation(struct wl_client *client,
6475 void *data, uint32_t version, uint32_t id)
6476{
6477 struct weston_compositor *compositor = data;
6478 struct wl_resource *resource;
6479
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006480 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006481 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006482 if (resource == NULL) {
6483 wl_client_post_no_memory(client);
6484 return;
6485 }
6486
6487 wl_resource_set_implementation(resource, &presentation_implementation,
6488 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006489 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006490}
6491
6492static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006493compositor_bind(struct wl_client *client,
6494 void *data, uint32_t version, uint32_t id)
6495{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006496 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006497 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006498
Jason Ekstranda85118c2013-06-27 20:17:02 -05006499 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006500 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006501 if (resource == NULL) {
6502 wl_client_post_no_memory(client);
6503 return;
6504 }
6505
6506 wl_resource_set_implementation(resource, &compositor_interface,
6507 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006508}
6509
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006510WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006511weston_environment_get_fd(const char *env)
6512{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006513 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006514 int fd, flags;
6515
6516 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006517 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006518 return -1;
6519
6520 flags = fcntl(fd, F_GETFD);
6521 if (flags == -1)
6522 return -1;
6523
6524 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6525 unsetenv(env);
6526
6527 return fd;
6528}
6529
Pekka Paalanenb5026542014-11-12 15:09:24 +02006530static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006531timeline_key_binding_handler(struct weston_keyboard *keyboard,
6532 const struct timespec *time, uint32_t key,
6533 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006534{
6535 struct weston_compositor *compositor = data;
6536
6537 if (weston_timeline_enabled_)
6538 weston_timeline_close();
6539 else
6540 weston_timeline_open(compositor);
6541}
6542
Daniel Stonece62cb32018-07-20 09:46:24 +01006543static const char *
6544output_repaint_status_text(struct weston_output *output)
6545{
6546 switch (output->repaint_status) {
6547 case REPAINT_NOT_SCHEDULED:
6548 return "no repaint";
6549 case REPAINT_BEGIN_FROM_IDLE:
6550 return "start_repaint_loop scheduled";
6551 case REPAINT_SCHEDULED:
6552 return "repaint scheduled";
6553 case REPAINT_AWAITING_COMPLETION:
6554 return "awaiting completion";
6555 }
6556
6557 assert(!"output_repaint_status_text missing enum");
6558 return NULL;
6559}
6560
6561static void
6562debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6563{
6564 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6565 struct wl_shm_buffer *shm;
6566 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006567 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006568
6569 if (!buffer) {
6570 fprintf(fp, "\t\t[buffer not available]\n");
6571 return;
6572 }
6573
6574 shm = wl_shm_buffer_get(buffer->resource);
6575 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006576 uint32_t _format = wl_shm_buffer_get_format(shm);
6577 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006578 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006579 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6580 (unsigned long) _format,
6581 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006582 return;
6583 }
6584
6585 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6586 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006587 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006588 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006589 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6590 (unsigned long) dmabuf->attributes.format,
6591 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006592 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6593 (unsigned long long) dmabuf->attributes.modifier[0]);
6594 return;
6595 }
6596
Marius Vlad253ba9a2019-03-17 18:22:21 +02006597 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006598}
6599
6600static void
6601debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6602{
6603 struct weston_compositor *ec = view->surface->compositor;
6604 struct weston_output *output;
6605 char desc[512];
6606 pixman_box32_t *box;
6607 uint32_t surface_id = 0;
6608 pid_t pid = 0;
6609
6610 if (view->surface->resource) {
6611 struct wl_resource *resource = view->surface->resource;
6612 wl_client_get_credentials(wl_resource_get_client(resource),
6613 &pid, NULL, NULL);
6614 surface_id = wl_resource_get_id(view->surface->resource);
6615 }
6616
6617 if (!view->surface->get_label ||
6618 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6619 strcpy(desc, "[no description available]");
6620 }
6621 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6622 view_idx, view->surface->role_name, pid, surface_id,
6623 desc, view);
6624
6625 box = pixman_region32_extents(&view->transform.boundingbox);
6626 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6627 box->x1, box->y1, box->x2, box->y2);
6628 box = pixman_region32_extents(&view->transform.opaque);
6629
6630 if (pixman_region32_equal(&view->transform.opaque,
6631 &view->transform.boundingbox)) {
6632 fprintf(fp, "\t\t[fully opaque]\n");
6633 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6634 fprintf(fp, "\t\t[not opaque]\n");
6635 } else {
6636 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6637 box->x1, box->y1, box->x2, box->y2);
6638 }
6639
6640 if (view->alpha < 1.0)
6641 fprintf(fp, "\t\talpha: %f\n", view->alpha);
6642
6643 if (view->output_mask != 0) {
6644 bool first_output = true;
6645 fprintf(fp, "\t\toutputs: ");
6646 wl_list_for_each(output, &ec->output_list, link) {
6647 if (!(view->output_mask & (1 << output->id)))
6648 continue;
6649 fprintf(fp, "%s%d (%s)%s",
6650 (first_output) ? "" : ", ",
6651 output->id, output->name,
6652 (view->output == output) ? " (primary)" : "");
6653 first_output = false;
6654 }
6655 } else {
6656 fprintf(fp, "\t\t[no outputs]");
6657 }
6658
6659 fprintf(fp, "\n");
6660
6661 debug_scene_view_print_buffer(fp, view);
6662}
6663
Marius Vlad433f4e72019-02-17 22:14:23 +02006664static void
6665debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02006666 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02006667{
6668 struct weston_subsurface *sub;
6669 struct weston_view *ev;
6670
6671 /*
6672 * print the view first, then we recursively go on printing
6673 * sub-surfaces. We bail out once no more sub-surfaces are available.
6674 */
Marius Vlada6acfa82019-03-17 18:10:09 +02006675 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02006676
6677 /* no more sub-surfaces */
6678 if (wl_list_empty(&view->surface->subsurface_list))
6679 return;
6680
6681 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
6682 wl_list_for_each(ev, &sub->surface->views, surface_link) {
6683 /* do not print again the parent view */
6684 if (view == ev)
6685 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02006686
6687 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02006688 debug_scene_view_print_tree(ev, fp, view_idx);
6689 }
6690 }
6691}
6692
Daniel Stonece62cb32018-07-20 09:46:24 +01006693/**
6694 * Output information on how libweston is currently composing the scene
6695 * graph.
6696 */
6697WL_EXPORT char *
6698weston_compositor_print_scene_graph(struct weston_compositor *ec)
6699{
6700 struct weston_output *output;
6701 struct weston_layer *layer;
6702 struct timespec now;
6703 int layer_idx = 0;
6704 FILE *fp;
6705 char *ret;
6706 size_t len;
6707 int err;
6708
6709 fp = open_memstream(&ret, &len);
6710 assert(fp);
6711
6712 weston_compositor_read_presentation_clock(ec, &now);
6713 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
6714 now.tv_sec, now.tv_nsec);
6715
6716 wl_list_for_each(output, &ec->output_list, link) {
6717 struct weston_head *head;
6718 int head_idx = 0;
6719
6720 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
6721 assert(output->enabled);
6722
6723 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
6724 output->x, output->y,
6725 output->x + output->width,
6726 output->y + output->height);
6727 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
6728 output->current_mode->width,
6729 output->current_mode->height,
6730 output->current_mode->refresh / 1000.0);
6731 fprintf(fp, "\tscale: %d\n", output->scale);
6732
6733 fprintf(fp, "\trepaint status: %s\n",
6734 output_repaint_status_text(output));
6735 if (output->repaint_status == REPAINT_SCHEDULED)
6736 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
6737 output->next_repaint.tv_sec,
6738 output->next_repaint.tv_nsec);
6739
6740 wl_list_for_each(head, &output->head_list, output_link) {
6741 fprintf(fp, "\tHead %d (%s): %sconnected\n",
6742 head_idx++, head->name,
6743 (head->connected) ? "" : "not ");
6744 }
6745 }
6746
6747 fprintf(fp, "\n");
6748
6749 wl_list_for_each(layer, &ec->layer_list, link) {
6750 struct weston_view *view;
6751 int view_idx = 0;
6752
6753 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
6754 (unsigned long) layer->position);
6755
6756 if (!weston_layer_mask_is_infinite(layer)) {
6757 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
6758 layer->mask.x1, layer->mask.y1,
6759 layer->mask.x2, layer->mask.y2);
6760 }
6761
Marius Vlada6acfa82019-03-17 18:10:09 +02006762 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
6763 debug_scene_view_print_tree(view, fp, &view_idx);
6764 view_idx++;
6765 }
Daniel Stonece62cb32018-07-20 09:46:24 +01006766
6767 if (wl_list_empty(&layer->view_list.link))
6768 fprintf(fp, "\t[no views]\n");
6769
6770 fprintf(fp, "\n");
6771 }
6772
6773 err = fclose(fp);
6774 assert(err == 0);
6775
6776 return ret;
6777}
6778
6779/**
6780 * Called when the 'scene-graph' debug scope is bound by a client. This
6781 * one-shot weston-debug scope prints the current scene graph when bound,
6782 * and then terminates the stream.
6783 */
6784static void
6785debug_scene_graph_cb(struct weston_debug_stream *stream, void *data)
6786{
6787 struct weston_compositor *ec = data;
6788 char *str = weston_compositor_print_scene_graph(ec);
6789
6790 weston_debug_stream_printf(stream, "%s", str);
6791 free(str);
6792 weston_debug_stream_complete(stream);
6793}
6794
Giulio Camuffo459137b2014-10-11 23:56:24 +03006795/** Create the compositor.
6796 *
6797 * This functions creates and initializes a compositor instance.
6798 *
6799 * \param display The Wayland display to be used.
6800 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03006801 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03006802 * using the \ref weston_compositor_get_user_data function.
6803 * \return The compositor instance on success or NULL on failure.
6804 */
6805WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03006806weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03006807 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03006808 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006809{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006810 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006811 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006812
Giulio Camuffo459137b2014-10-11 23:56:24 +03006813 ec = zalloc(sizeof *ec);
6814 if (!ec)
6815 return NULL;
6816
6817 ec->wl_display = display;
6818 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006819 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006820 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006821 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006822 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006823 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006824 wl_signal_init(&ec->idle_signal);
6825 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006826 wl_signal_init(&ec->show_input_panel_signal);
6827 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006828 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006829 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006830 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006831 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006832 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006833 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006834 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02006835 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006836 wl_signal_init(&ec->session_signal);
6837 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006838
Casey Dahlin58ba1372012-04-19 22:50:08 -04006839 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006840 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006841
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006842 ec->activate_serial = 1;
6843
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05006844 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
6845
Derek Foreman152254b2015-11-26 14:17:48 -06006846 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006847 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006848 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006849
Giulio Camuffo954f1832014-10-11 18:27:30 +03006850 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006851 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006852 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006853
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006854 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006855 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006856 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006857
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006858 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006859 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006860 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006861
Marius Vlad3d7d9782019-04-17 12:35:38 +03006862 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02006863 goto fail;
6864
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006865 if (weston_input_init(ec) != 0)
6866 goto fail;
6867
Jason Ekstranda7af7042013-10-12 22:38:11 -05006868 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006869 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006870 wl_list_init(&ec->layer_list);
6871 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006872 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006873 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006874 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006875 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006876 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006877 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006878 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006879 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006880 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006881
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006882 wl_list_init(&ec->plugin_api_list);
6883
Xiong Zhang97116532013-10-23 13:58:31 +08006884 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006885 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006886
Giulio Camuffo459137b2014-10-11 23:56:24 +03006887 wl_data_device_manager_init(ec->wl_display);
6888
6889 wl_display_init_shm(ec->wl_display);
6890
6891 loop = wl_display_get_event_loop(ec->wl_display);
6892 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006893 ec->repaint_timer =
6894 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6895 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006896
Quentin Glidic82681572016-12-17 13:40:51 +01006897 weston_layer_init(&ec->fade_layer, ec);
6898 weston_layer_init(&ec->cursor_layer, ec);
6899
6900 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6901 weston_layer_set_position(&ec->cursor_layer,
6902 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006903
6904 weston_compositor_add_debug_binding(ec, KEY_T,
6905 timeline_key_binding_handler, ec);
6906
Daniel Stonece62cb32018-07-20 09:46:24 +01006907 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03006908 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
6909 "Scene graph details\n",
6910 debug_scene_graph_cb,
6911 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01006912
Giulio Camuffo459137b2014-10-11 23:56:24 +03006913 return ec;
6914
6915fail:
6916 free(ec);
6917 return NULL;
6918}
6919
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006920WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006921weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006922{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006923 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006924
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006925 wl_event_source_remove(ec->idle_source);
6926
Matt Roper361d2ad2011-08-29 13:52:23 -07006927 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006928 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006929 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006930
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006931 /* Destroy all pending outputs associated with this compositor */
6932 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6933 output->destroy(output);
6934
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006935 if (ec->renderer)
6936 ec->renderer->destroy(ec);
6937
Daniel Stone325fc2d2012-05-30 16:31:58 +01006938 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006939 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006940 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006941 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006942 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006943 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006944
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006945 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006946}
6947
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006948WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006949weston_compositor_exit_with_code(struct weston_compositor *compositor,
6950 int exit_code)
6951{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006952 if (compositor->exit_code == EXIT_SUCCESS)
6953 compositor->exit_code = exit_code;
6954
Giulio Camuffo459137b2014-10-11 23:56:24 +03006955 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006956}
6957
6958WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006959weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6960 const struct weston_pointer_grab_interface *interface)
6961{
6962 struct weston_seat *seat;
6963
6964 ec->default_pointer_grab = interface;
6965 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006966 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6967
6968 if (pointer)
6969 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006970 }
6971}
6972
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006973WL_EXPORT int
6974weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6975 clockid_t clk_id)
6976{
6977 struct timespec ts;
6978
6979 if (clock_gettime(clk_id, &ts) < 0)
6980 return -1;
6981
6982 compositor->presentation_clock = clk_id;
6983
6984 return 0;
6985}
6986
6987/*
6988 * For choosing the software clock, when the display hardware or API
6989 * does not expose a compatible presentation timestamp.
6990 */
6991WL_EXPORT int
6992weston_compositor_set_presentation_clock_software(
6993 struct weston_compositor *compositor)
6994{
6995 /* In order of preference */
6996 static const clockid_t clocks[] = {
6997 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6998 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6999 CLOCK_MONOTONIC, /* no jumps, may crawl */
7000 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7001 CLOCK_REALTIME /* may jump and crawl */
7002 };
7003 unsigned i;
7004
7005 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7006 if (weston_compositor_set_presentation_clock(compositor,
7007 clocks[i]) == 0)
7008 return 0;
7009
7010 weston_log("Error: no suitable presentation clock available.\n");
7011
7012 return -1;
7013}
7014
Pekka Paalanen662f3842015-03-18 12:17:26 +02007015/** Read the current time from the Presentation clock
7016 *
7017 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007018 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007019 *
7020 * \note Reading the current time in user space is always imprecise to some
7021 * degree.
7022 *
7023 * This function is never meant to fail. If reading the clock does fail,
7024 * an error message is logged and a zero time is returned. Callers are not
7025 * supposed to detect or react to failures.
7026 */
7027WL_EXPORT void
7028weston_compositor_read_presentation_clock(
7029 const struct weston_compositor *compositor,
7030 struct timespec *ts)
7031{
7032 static bool warned;
7033 int ret;
7034
7035 ret = clock_gettime(compositor->presentation_clock, ts);
7036 if (ret < 0) {
7037 ts->tv_sec = 0;
7038 ts->tv_nsec = 0;
7039
7040 if (!warned)
7041 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007042 "the presentation clock %#x: '%s' (%d)\n",
7043 compositor->presentation_clock,
7044 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007045 warned = true;
7046 }
7047}
7048
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007049/** Import dmabuf buffer into current renderer
7050 *
7051 * \param compositor
7052 * \param buffer the dmabuf buffer to import
7053 * \return true on usable buffers, false otherwise
7054 *
7055 * This function tests that the linux_dmabuf_buffer is usable
7056 * for the current renderer. Returns false on unusable buffers. Usually
7057 * usability is tested by importing the dmabufs for composition.
7058 *
7059 * This hook is also used for detecting if the renderer supports
7060 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7061 * supported.
7062 * */
7063WL_EXPORT bool
7064weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7065 struct linux_dmabuf_buffer *buffer)
7066{
7067 struct weston_renderer *renderer;
7068
7069 renderer = compositor->renderer;
7070
7071 if (renderer->import_dmabuf == NULL)
7072 return false;
7073
7074 return renderer->import_dmabuf(compositor, buffer);
7075}
7076
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007077WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007078weston_version(int *major, int *minor, int *micro)
7079{
7080 *major = WESTON_VERSION_MAJOR;
7081 *minor = WESTON_VERSION_MINOR;
7082 *micro = WESTON_VERSION_MICRO;
7083}
7084
Daniel Stonee03c1112016-11-24 20:45:45 +00007085/**
7086 * Attempts to find a module path from the module map specified in the
7087 * environment. If found, writes the full path into the path variable.
7088 *
7089 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7090 * each entry is of the form "name=path" and entries are separated by
7091 * semicolons. Whitespace is significant.
7092 *
7093 * \param name The name to search for.
7094 * \param path Where the path is written to if found.
7095 * \param path_len Allocated bytes at \c path .
7096 * \returns The length of the string written to path on success, or 0 if the
7097 * module was not specified in the environment map or path_len was too small.
7098 */
7099WL_EXPORT size_t
7100weston_module_path_from_env(const char *name, char *path, size_t path_len)
7101{
7102 const char *mapping = getenv("WESTON_MODULE_MAP");
7103 const char *end;
7104 const int name_len = strlen(name);
7105
7106 if (!mapping)
7107 return 0;
7108
7109 end = mapping + strlen(mapping);
7110 while (mapping < end && *mapping) {
7111 const char *filename, *next;
7112
7113 /* early out: impossibly short string */
7114 if (end - mapping < name_len + 1)
7115 return 0;
7116
7117 filename = &mapping[name_len + 1];
7118 next = strchrnul(mapping, ';');
7119
7120 if (strncmp(mapping, name, name_len) == 0 &&
7121 mapping[name_len] == '=') {
7122 size_t file_len = next - filename; /* no trailing NUL */
7123 if (file_len >= path_len)
7124 return 0;
7125 strncpy(path, filename, file_len);
7126 path[file_len] = '\0';
7127 return file_len;
7128 }
7129
7130 mapping = next + 1;
7131 }
7132
7133 return 0;
7134}
7135
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007136WL_EXPORT void *
7137weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007138{
7139 char path[PATH_MAX];
7140 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007141 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007142
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007143 if (name == NULL)
7144 return NULL;
7145
Derek Foreman3f86e502015-06-08 11:46:54 -05007146 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007147 len = weston_module_path_from_env(name, path, sizeof path);
7148 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007149 len = snprintf(path, sizeof path, "%s/%s",
7150 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007151 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007152 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007153 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007154
Daniel Stonebeb97e52016-11-28 12:13:54 +00007155 /* snprintf returns the length of the string it would've written,
7156 * _excluding_ the NUL byte. So even being equal to the size of
7157 * our buffer is an error here. */
7158 if (len >= sizeof path)
7159 return NULL;
7160
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007161 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7162 if (module) {
7163 weston_log("Module '%s' already loaded\n", path);
7164 dlclose(module);
7165 return NULL;
7166 }
7167
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007168 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007169 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007170 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007171 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007172 return NULL;
7173 }
7174
7175 init = dlsym(module, entrypoint);
7176 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007177 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007178 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007179 return NULL;
7180 }
7181
7182 return init;
7183}
7184
Giulio Camuffo459137b2014-10-11 23:56:24 +03007185
7186/** Destroys the compositor.
7187 *
7188 * This function cleans up the compositor state and destroys it.
7189 *
7190 * \param compositor The compositor to be destroyed.
7191 */
7192WL_EXPORT void
7193weston_compositor_destroy(struct weston_compositor *compositor)
7194{
7195 /* prevent further rendering while shutting down */
7196 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7197
7198 wl_signal_emit(&compositor->destroy_signal, compositor);
7199
7200 weston_compositor_xkb_destroy(compositor);
7201
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007202 if (compositor->backend)
7203 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007204
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007205 /* The backend is responsible for destroying the heads. */
7206 assert(wl_list_empty(&compositor->head_list));
7207
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007208 weston_plugin_api_destroy_list(compositor);
7209
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007210 if (compositor->heads_changed_source)
7211 wl_event_source_remove(compositor->heads_changed_source);
7212
Marius Vlad7e4db952019-04-17 13:47:06 +03007213 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007214 compositor->debug_scene = NULL;
Marius Vlad3d7d9782019-04-17 12:35:38 +03007215 weston_log_ctx_compositor_destroy(compositor);
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007216
Giulio Camuffo459137b2014-10-11 23:56:24 +03007217 free(compositor);
7218}
7219
7220/** Instruct the compositor to exit.
7221 *
7222 * This functions does not directly destroy the compositor object, it merely
7223 * command it to start the tear down process. It is not guaranteed that the
7224 * tear down will happen immediately.
7225 *
7226 * \param compositor The compositor to tear down.
7227 */
7228WL_EXPORT void
7229weston_compositor_exit(struct weston_compositor *compositor)
7230{
7231 compositor->exit(compositor);
7232}
7233
7234/** Return the user data stored in the compositor.
7235 *
7236 * This function returns the user data pointer set with user_data parameter
7237 * to the \ref weston_compositor_create function.
7238 */
7239WL_EXPORT void *
7240weston_compositor_get_user_data(struct weston_compositor *compositor)
7241{
7242 return compositor->user_data;
7243}
Pekka Paalanendd186732016-06-03 14:49:54 +03007244
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007245static const char * const backend_map[] = {
7246 [WESTON_BACKEND_DRM] = "drm-backend.so",
7247 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7248 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7249 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7250 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7251 [WESTON_BACKEND_X11] = "x11-backend.so",
7252};
7253
Pekka Paalanendd186732016-06-03 14:49:54 +03007254/** Load a backend into a weston_compositor
7255 *
7256 * A backend must be loaded to make a weston_compositor work. A backend
7257 * provides input and output capabilities, and determines the renderer to use.
7258 *
7259 * \param compositor A compositor that has not had a backend loaded yet.
7260 * \param backend Name of the backend file.
7261 * \param config_base A pointer to a backend-specific configuration
7262 * structure's 'base' member.
7263 *
7264 * \return 0 on success, or -1 on error.
7265 */
7266WL_EXPORT int
7267weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007268 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007269 struct weston_backend_config *config_base)
7270{
7271 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007272 struct weston_backend_config *config_base);
7273
Pekka Paalanend7e35112017-08-29 17:04:12 +03007274 if (compositor->backend) {
7275 weston_log("Error: attempt to load a backend when one is already loaded\n");
7276 return -1;
7277 }
7278
Quentin Glidic887c0182016-07-10 11:00:53 +02007279 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007280 return -1;
7281
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007282 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007283 if (!backend_init)
7284 return -1;
7285
Pekka Paalanend7e35112017-08-29 17:04:12 +03007286 if (backend_init(compositor, config_base) < 0) {
7287 compositor->backend = NULL;
7288 return -1;
7289 }
7290
7291 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007292}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007293
7294WL_EXPORT int
7295weston_compositor_load_xwayland(struct weston_compositor *compositor)
7296{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007297 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007298
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007299 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007300 if (!module_init)
7301 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007302 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007303 return -1;
7304 return 0;
7305}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007306
7307/** Resolve an internal compositor error by disconnecting the client.
7308 *
7309 * This function is used in cases when the wl_buffer turns out
7310 * unusable and there is no fallback path.
7311 *
7312 * It is possible the fault is caused by a compositor bug, the underlying
7313 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7314 * In any case, the options are to either composite garbage or nothing,
7315 * or disconnect the client. This is a helper function for the latter.
7316 *
7317 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7318 *
7319 * \param buffer The weston buffer that is unusable.
7320 * \param msg A custom error message attached to the protocol error.
7321 */
7322WL_EXPORT void
7323weston_buffer_send_server_error(struct weston_buffer *buffer,
7324 const char *msg)
7325{
7326 struct wl_client *client;
7327 struct wl_resource *display_resource;
7328 uint32_t id;
7329
7330 assert(buffer->resource);
7331 id = wl_resource_get_id(buffer->resource);
7332 client = wl_resource_get_client(buffer->resource);
7333 display_resource = wl_client_get_object(client, 1);
7334
7335 assert(display_resource);
7336 wl_resource_post_error(display_resource,
7337 WL_DISPLAY_ERROR_INVALID_OBJECT,
7338 "server error with "
7339 "wl_buffer@%u: %s", id, msg);
7340}