blob: ce4f51ea868f197344f7c49dd043e5157ea1fe05 [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
Kristian Høgsberg82863022010-06-04 21:52:02 -040058#include "compositor.h"
Daniel Stonece62cb32018-07-20 09:46:24 +010059#include "weston-debug.h"
60#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"
64#include "shared/fd-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070065#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070066#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070067#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030068#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040069#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050070#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020071#include "plugin-registry.h"
Marius Vlad00a6e012018-11-20 17:52:31 +020072#include "pixel-formats.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050073
Pekka Paalanen0513a952014-05-21 16:17:27 +030074#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
75
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020076static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030077weston_output_update_matrix(struct weston_output *output);
78
79static void
Alexander Larsson0b135062013-05-28 16:23:36 +020080weston_output_transform_scale_init(struct weston_output *output,
81 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020082
Rob Bradford27b17932013-06-26 18:08:46 +010083static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050084weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010085
Pekka Paalanendcac3512017-12-08 14:13:34 +020086static char *
87weston_output_create_heads_string(struct weston_output *output);
88
Pekka Paalanen6528c032017-03-28 15:27:10 +030089/** Send wl_output events for mode and scale changes
90 *
91 * \param head Send on all resources bound to this head.
92 * \param mode_changed If true, send the current mode.
93 * \param scale_changed If true, send the current scale.
94 */
95static void
96weston_mode_switch_send_events(struct weston_head *head,
97 bool mode_changed, bool scale_changed)
98{
99 struct weston_output *output = head->output;
100 struct wl_resource *resource;
101 int version;
102
103 wl_resource_for_each(resource, &head->resource_list) {
104 if (mode_changed) {
105 wl_output_send_mode(resource,
106 output->current_mode->flags,
107 output->current_mode->width,
108 output->current_mode->height,
109 output->current_mode->refresh);
110 }
111
112 version = wl_resource_get_version(resource);
113 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
114 wl_output_send_scale(resource, output->current_scale);
115
116 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
117 wl_output_send_done(resource);
118 }
119}
120
121static void
122weston_mode_switch_finish(struct weston_output *output,
123 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800124{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200125 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300126 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200127 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200128
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200129 pixman_region32_init(&old_output_region);
130 pixman_region32_copy(&old_output_region, &output->region);
131
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200132 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200133 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200134
135 pixman_region32_init(&output->previous_damage);
136 pixman_region32_init_rect(&output->region, output->x, output->y,
137 output->width, output->height);
138
139 weston_output_update_matrix(output);
140
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200141 /* If a pointer falls outside the outputs new geometry, move it to its
142 * lower-right corner */
143 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500144 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200145 int32_t x, y;
146
147 if (!pointer)
148 continue;
149
150 x = wl_fixed_to_int(pointer->x);
151 y = wl_fixed_to_int(pointer->y);
152
153 if (!pixman_region32_contains_point(&old_output_region,
154 x, y, NULL) ||
155 pixman_region32_contains_point(&output->region,
156 x, y, NULL))
157 continue;
158
159 if (x >= output->x + output->width)
160 x = output->x + output->width - 1;
161 if (y >= output->y + output->height)
162 y = output->y + output->height - 1;
163
164 pointer->x = wl_fixed_from_int(x);
165 pointer->y = wl_fixed_from_int(y);
166 }
167
168 pixman_region32_fini(&old_output_region);
169
Derek Foremandd4cd332014-11-10 10:29:59 -0600170 if (!mode_changed && !scale_changed)
171 return;
172
Hardening57388e42013-09-18 23:56:36 +0200173 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300174 wl_list_for_each(head, &output->head_list, output_link)
175 weston_mode_switch_send_events(head,
176 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600177}
178
David Fort0de859e2016-05-27 23:22:57 +0200179static void
180weston_compositor_reflow_outputs(struct weston_compositor *compositor,
181 struct weston_output *resized_output, int delta_width);
182
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600183WL_EXPORT int
184weston_output_mode_set_native(struct weston_output *output,
185 struct weston_mode *mode,
186 int32_t scale)
187{
188 int ret;
189 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200190 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600191
192 if (!output->switch_mode)
193 return -1;
194
195 if (!output->original_mode) {
196 mode_changed = 1;
197 ret = output->switch_mode(output, mode);
198 if (ret < 0)
199 return ret;
200 if (output->current_scale != scale) {
201 scale_changed = 1;
202 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200203 }
204 }
205
David Fort0de859e2016-05-27 23:22:57 +0200206 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600207 output->native_mode = mode;
208 output->native_scale = scale;
209
210 weston_mode_switch_finish(output, mode_changed, scale_changed);
211
David Fort0de859e2016-05-27 23:22:57 +0200212 if (mode_changed || scale_changed) {
213 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
214
215 wl_signal_emit(&output->compositor->output_resized_signal, output);
216 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600217 return 0;
218}
219
220WL_EXPORT int
221weston_output_mode_switch_to_native(struct weston_output *output)
222{
223 int ret;
224 int mode_changed = 0, scale_changed = 0;
225
226 if (!output->switch_mode)
227 return -1;
228
229 if (!output->original_mode) {
230 weston_log("already in the native mode\n");
231 return -1;
232 }
233 /* the non fullscreen clients haven't seen a mode set since we
234 * switched into a temporary, so we need to notify them if the
235 * mode at that time is different from the native mode now.
236 */
237 mode_changed = (output->original_mode != output->native_mode);
238 scale_changed = (output->original_scale != output->native_scale);
239
240 ret = output->switch_mode(output, output->native_mode);
241 if (ret < 0)
242 return ret;
243
244 output->current_scale = output->native_scale;
245
246 output->original_mode = NULL;
247 output->original_scale = 0;
248
249 weston_mode_switch_finish(output, mode_changed, scale_changed);
250
251 return 0;
252}
253
254WL_EXPORT int
255weston_output_mode_switch_to_temporary(struct weston_output *output,
256 struct weston_mode *mode,
257 int32_t scale)
258{
259 int ret;
260
261 if (!output->switch_mode)
262 return -1;
263
264 /* original_mode is the last mode non full screen clients have seen,
265 * so we shouldn't change it if we already have one set.
266 */
267 if (!output->original_mode) {
268 output->original_mode = output->native_mode;
269 output->original_scale = output->native_scale;
270 }
271 ret = output->switch_mode(output, mode);
272 if (ret < 0)
273 return ret;
274
275 output->current_scale = scale;
276
277 weston_mode_switch_finish(output, 0, 0);
278
279 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800280}
281
Benjamin Franzke06286262011-05-06 19:12:33 +0200282static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300283region_init_infinite(pixman_region32_t *region)
284{
285 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
286 UINT32_MAX, UINT32_MAX);
287}
288
Pekka Paalanene67858b2013-04-25 13:57:42 +0300289static struct weston_subsurface *
290weston_surface_to_subsurface(struct weston_surface *surface);
291
Jason Ekstranda7af7042013-10-12 22:38:11 -0500292WL_EXPORT struct weston_view *
293weston_view_create(struct weston_surface *surface)
294{
295 struct weston_view *view;
296
Bryce Harringtonde16d892014-11-20 22:21:57 -0800297 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500298 if (view == NULL)
299 return NULL;
300
301 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000302 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500303
Jason Ekstranda7af7042013-10-12 22:38:11 -0500304 /* Assign to surface */
305 wl_list_insert(&surface->views, &view->surface_link);
306
307 wl_signal_init(&view->destroy_signal);
308 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300309 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500310
Jason Ekstranda7af7042013-10-12 22:38:11 -0500311 pixman_region32_init(&view->clip);
312
313 view->alpha = 1.0;
314 pixman_region32_init(&view->transform.opaque);
315
316 wl_list_init(&view->geometry.transformation_list);
317 wl_list_insert(&view->geometry.transformation_list,
318 &view->transform.position.link);
319 weston_matrix_init(&view->transform.position.matrix);
320 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200321 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500322 pixman_region32_init(&view->transform.boundingbox);
323 view->transform.dirty = 1;
324
Jason Ekstranda7af7042013-10-12 22:38:11 -0500325 return view;
326}
327
Jason Ekstrand108865d2014-06-26 10:04:49 -0700328struct weston_frame_callback {
329 struct wl_resource *resource;
330 struct wl_list link;
331};
332
Pekka Paalanen133e4392014-09-23 22:08:46 -0400333struct weston_presentation_feedback {
334 struct wl_resource *resource;
335
336 /* XXX: could use just wl_resource_get_link() instead */
337 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200338
339 /* The per-surface feedback flags */
340 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400341};
342
343static void
344weston_presentation_feedback_discard(
345 struct weston_presentation_feedback *feedback)
346{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200347 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400348 wl_resource_destroy(feedback->resource);
349}
350
351static void
352weston_presentation_feedback_discard_list(struct wl_list *list)
353{
354 struct weston_presentation_feedback *feedback, *tmp;
355
356 wl_list_for_each_safe(feedback, tmp, list, link)
357 weston_presentation_feedback_discard(feedback);
358}
359
360static void
361weston_presentation_feedback_present(
362 struct weston_presentation_feedback *feedback,
363 struct weston_output *output,
364 uint32_t refresh_nsec,
365 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200366 uint64_t seq,
367 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400368{
369 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300370 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400371 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200372 uint32_t tv_sec_hi;
373 uint32_t tv_sec_lo;
374 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300375 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400376
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300377 wl_list_for_each(head, &output->head_list, output_link) {
378 wl_resource_for_each(o, &head->resource_list) {
379 if (wl_resource_get_client(o) != client)
380 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400381
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300382 wp_presentation_feedback_send_sync_output(feedback->resource, o);
383 done = true;
384 }
385
386 /* For clone mode, send it for just one wl_output global,
387 * they are all equivalent anyway.
388 */
389 if (done)
390 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400391 }
392
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200393 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200394 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200395 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200396 refresh_nsec,
397 seq >> 32, seq & 0xffffffff,
398 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400399 wl_resource_destroy(feedback->resource);
400}
401
402static void
403weston_presentation_feedback_present_list(struct wl_list *list,
404 struct weston_output *output,
405 uint32_t refresh_nsec,
406 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200407 uint64_t seq,
408 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400409{
410 struct weston_presentation_feedback *feedback, *tmp;
411
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200412 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200413 wl_list_empty(list));
414
Pekka Paalanen133e4392014-09-23 22:08:46 -0400415 wl_list_for_each_safe(feedback, tmp, list, link)
416 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200417 refresh_nsec, ts, seq,
418 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400419}
420
Jason Ekstrand7b982072014-05-20 14:33:03 -0500421static void
422surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
423{
424 struct weston_surface_state *state =
425 container_of(listener, struct weston_surface_state,
426 buffer_destroy_listener);
427
428 state->buffer = NULL;
429}
430
431static void
432weston_surface_state_init(struct weston_surface_state *state)
433{
434 state->newly_attached = 0;
435 state->buffer = NULL;
436 state->buffer_destroy_listener.notify =
437 surface_state_handle_buffer_destroy;
438 state->sx = 0;
439 state->sy = 0;
440
Derek Foreman152254b2015-11-26 14:17:48 -0600441 pixman_region32_init(&state->damage_surface);
442 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500443 pixman_region32_init(&state->opaque);
444 region_init_infinite(&state->input);
445
446 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400447 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500448
449 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
450 state->buffer_viewport.buffer.scale = 1;
451 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
452 state->buffer_viewport.surface.width = -1;
453 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300454
455 state->acquire_fence_fd = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500456}
457
458static void
459weston_surface_state_fini(struct weston_surface_state *state)
460{
461 struct weston_frame_callback *cb, *next;
462
463 wl_list_for_each_safe(cb, next,
464 &state->frame_callback_list, link)
465 wl_resource_destroy(cb->resource);
466
Pekka Paalanen133e4392014-09-23 22:08:46 -0400467 weston_presentation_feedback_discard_list(&state->feedback_list);
468
Jason Ekstrand7b982072014-05-20 14:33:03 -0500469 pixman_region32_fini(&state->input);
470 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600471 pixman_region32_fini(&state->damage_surface);
472 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500473
474 if (state->buffer)
475 wl_list_remove(&state->buffer_destroy_listener.link);
476 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300477
478 fd_clear(&state->acquire_fence_fd);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500479}
480
481static void
482weston_surface_state_set_buffer(struct weston_surface_state *state,
483 struct weston_buffer *buffer)
484{
485 if (state->buffer == buffer)
486 return;
487
488 if (state->buffer)
489 wl_list_remove(&state->buffer_destroy_listener.link);
490 state->buffer = buffer;
491 if (state->buffer)
492 wl_signal_add(&state->buffer->destroy_signal,
493 &state->buffer_destroy_listener);
494}
495
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500496WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500497weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500498{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500499 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400500
Bryce Harringtonde16d892014-11-20 22:21:57 -0800501 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400502 if (surface == NULL)
503 return NULL;
504
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500505 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800506 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500507
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500508 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200509 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400510
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200511 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
512 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200513 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
514 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500515
516 weston_surface_state_init(&surface->pending);
517
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400518 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500519 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300520 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400521
Jason Ekstranda7af7042013-10-12 22:38:11 -0500522 wl_list_init(&surface->views);
523
524 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400525 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500526
Pekka Paalanene67858b2013-04-25 13:57:42 +0300527 wl_list_init(&surface->subsurface_list);
528 wl_list_init(&surface->subsurface_list_pending);
529
Jason Ekstrand1e059042014-10-16 10:55:19 -0500530 weston_matrix_init(&surface->buffer_to_surface_matrix);
531 weston_matrix_init(&surface->surface_to_buffer_matrix);
532
Jonas Ådahld3414f22016-07-22 17:56:31 +0800533 wl_list_init(&surface->pointer_constraints);
534
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300535 surface->acquire_fence_fd = -1;
536
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400537 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500538}
539
Alex Wu8811bf92012-02-28 18:07:54 +0800540WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500541weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200542 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500543{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100544 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200545 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500546}
547
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400548WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500549weston_view_to_global_float(struct weston_view *view,
550 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200551{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500552 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200553 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
554
Jason Ekstranda7af7042013-10-12 22:38:11 -0500555 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200556
557 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200558 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700559 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200560 v.f[3]);
561 *x = 0;
562 *y = 0;
563 return;
564 }
565
566 *x = v.f[0] / v.f[3];
567 *y = v.f[1] / v.f[3];
568 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500569 *x = sx + view->geometry.x;
570 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200571 }
572}
573
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500574WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200575weston_transformed_coord(int width, int height,
576 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200577 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200578 float sx, float sy, float *bx, float *by)
579{
580 switch (transform) {
581 case WL_OUTPUT_TRANSFORM_NORMAL:
582 default:
583 *bx = sx;
584 *by = sy;
585 break;
586 case WL_OUTPUT_TRANSFORM_FLIPPED:
587 *bx = width - sx;
588 *by = sy;
589 break;
590 case WL_OUTPUT_TRANSFORM_90:
591 *bx = height - sy;
592 *by = sx;
593 break;
594 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
595 *bx = height - sy;
596 *by = width - sx;
597 break;
598 case WL_OUTPUT_TRANSFORM_180:
599 *bx = width - sx;
600 *by = height - sy;
601 break;
602 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
603 *bx = sx;
604 *by = height - sy;
605 break;
606 case WL_OUTPUT_TRANSFORM_270:
607 *bx = sy;
608 *by = width - sx;
609 break;
610 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
611 *bx = sy;
612 *by = sx;
613 break;
614 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200615
616 *bx *= scale;
617 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200618}
619
620WL_EXPORT pixman_box32_t
621weston_transformed_rect(int width, int height,
622 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200623 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200624 pixman_box32_t rect)
625{
626 float x1, x2, y1, y2;
627
628 pixman_box32_t ret;
629
Alexander Larsson4ea95522013-05-22 14:41:37 +0200630 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200631 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200632 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200633 rect.x2, rect.y2, &x2, &y2);
634
635 if (x1 <= x2) {
636 ret.x1 = x1;
637 ret.x2 = x2;
638 } else {
639 ret.x1 = x2;
640 ret.x2 = x1;
641 }
642
643 if (y1 <= y2) {
644 ret.y1 = y1;
645 ret.y2 = y2;
646 } else {
647 ret.y1 = y2;
648 ret.y2 = y1;
649 }
650
651 return ret;
652}
653
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600654/** Transform a region by a matrix, restricted to axis-aligned transformations
655 *
656 * Warning: This function does not work for projective, affine, or matrices
657 * that encode arbitrary rotations. Only 90-degree step rotations are
658 * supported.
659 */
660WL_EXPORT void
661weston_matrix_transform_region(pixman_region32_t *dest,
662 struct weston_matrix *matrix,
663 pixman_region32_t *src)
664{
665 pixman_box32_t *src_rects, *dest_rects;
666 int nrects, i;
667
668 src_rects = pixman_region32_rectangles(src, &nrects);
669 dest_rects = malloc(nrects * sizeof(*dest_rects));
670 if (!dest_rects)
671 return;
672
673 for (i = 0; i < nrects; i++) {
674 struct weston_vector vec1 = {{
675 src_rects[i].x1, src_rects[i].y1, 0, 1
676 }};
677 weston_matrix_transform(matrix, &vec1);
678 vec1.f[0] /= vec1.f[3];
679 vec1.f[1] /= vec1.f[3];
680
681 struct weston_vector vec2 = {{
682 src_rects[i].x2, src_rects[i].y2, 0, 1
683 }};
684 weston_matrix_transform(matrix, &vec2);
685 vec2.f[0] /= vec2.f[3];
686 vec2.f[1] /= vec2.f[3];
687
688 if (vec1.f[0] < vec2.f[0]) {
689 dest_rects[i].x1 = floor(vec1.f[0]);
690 dest_rects[i].x2 = ceil(vec2.f[0]);
691 } else {
692 dest_rects[i].x1 = floor(vec2.f[0]);
693 dest_rects[i].x2 = ceil(vec1.f[0]);
694 }
695
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600696 if (vec1.f[1] < vec2.f[1]) {
697 dest_rects[i].y1 = floor(vec1.f[1]);
698 dest_rects[i].y2 = ceil(vec2.f[1]);
699 } else {
700 dest_rects[i].y1 = floor(vec2.f[1]);
701 dest_rects[i].y2 = ceil(vec1.f[1]);
702 }
703 }
704
705 pixman_region32_clear(dest);
706 pixman_region32_init_rects(dest, dest_rects, nrects);
707 free(dest_rects);
708}
709
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200710WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500711weston_transformed_region(int width, int height,
712 enum wl_output_transform transform,
713 int32_t scale,
714 pixman_region32_t *src, pixman_region32_t *dest)
715{
716 pixman_box32_t *src_rects, *dest_rects;
717 int nrects, i;
718
719 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
720 if (src != dest)
721 pixman_region32_copy(dest, src);
722 return;
723 }
724
725 src_rects = pixman_region32_rectangles(src, &nrects);
726 dest_rects = malloc(nrects * sizeof(*dest_rects));
727 if (!dest_rects)
728 return;
729
730 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
731 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
732 } else {
733 for (i = 0; i < nrects; i++) {
734 switch (transform) {
735 default:
736 case WL_OUTPUT_TRANSFORM_NORMAL:
737 dest_rects[i].x1 = src_rects[i].x1;
738 dest_rects[i].y1 = src_rects[i].y1;
739 dest_rects[i].x2 = src_rects[i].x2;
740 dest_rects[i].y2 = src_rects[i].y2;
741 break;
742 case WL_OUTPUT_TRANSFORM_90:
743 dest_rects[i].x1 = height - src_rects[i].y2;
744 dest_rects[i].y1 = src_rects[i].x1;
745 dest_rects[i].x2 = height - src_rects[i].y1;
746 dest_rects[i].y2 = src_rects[i].x2;
747 break;
748 case WL_OUTPUT_TRANSFORM_180:
749 dest_rects[i].x1 = width - src_rects[i].x2;
750 dest_rects[i].y1 = height - src_rects[i].y2;
751 dest_rects[i].x2 = width - src_rects[i].x1;
752 dest_rects[i].y2 = height - src_rects[i].y1;
753 break;
754 case WL_OUTPUT_TRANSFORM_270:
755 dest_rects[i].x1 = src_rects[i].y1;
756 dest_rects[i].y1 = width - src_rects[i].x2;
757 dest_rects[i].x2 = src_rects[i].y2;
758 dest_rects[i].y2 = width - src_rects[i].x1;
759 break;
760 case WL_OUTPUT_TRANSFORM_FLIPPED:
761 dest_rects[i].x1 = width - src_rects[i].x2;
762 dest_rects[i].y1 = src_rects[i].y1;
763 dest_rects[i].x2 = width - src_rects[i].x1;
764 dest_rects[i].y2 = src_rects[i].y2;
765 break;
766 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
767 dest_rects[i].x1 = height - src_rects[i].y2;
768 dest_rects[i].y1 = width - src_rects[i].x2;
769 dest_rects[i].x2 = height - src_rects[i].y1;
770 dest_rects[i].y2 = width - src_rects[i].x1;
771 break;
772 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
773 dest_rects[i].x1 = src_rects[i].x1;
774 dest_rects[i].y1 = height - src_rects[i].y2;
775 dest_rects[i].x2 = src_rects[i].x2;
776 dest_rects[i].y2 = height - src_rects[i].y1;
777 break;
778 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
779 dest_rects[i].x1 = src_rects[i].y1;
780 dest_rects[i].y1 = src_rects[i].x1;
781 dest_rects[i].x2 = src_rects[i].y2;
782 dest_rects[i].y2 = src_rects[i].x2;
783 break;
784 }
785 }
786 }
787
788 if (scale != 1) {
789 for (i = 0; i < nrects; i++) {
790 dest_rects[i].x1 *= scale;
791 dest_rects[i].x2 *= scale;
792 dest_rects[i].y1 *= scale;
793 dest_rects[i].y2 *= scale;
794 }
795 }
796
797 pixman_region32_clear(dest);
798 pixman_region32_init_rects(dest, dest_rects, nrects);
799 free(dest_rects);
800}
801
Jonny Lamb74130762013-11-26 18:19:46 +0100802static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300803viewport_surface_to_buffer(struct weston_surface *surface,
804 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100805{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200806 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200807 double src_width, src_height;
808 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200809
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200810 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
811 if (vp->surface.width == -1) {
812 *bx = sx;
813 *by = sy;
814 return;
815 }
Jonny Lamb74130762013-11-26 18:19:46 +0100816
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200817 src_x = 0.0;
818 src_y = 0.0;
819 src_width = surface->width_from_buffer;
820 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100821 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200822 src_x = wl_fixed_to_double(vp->buffer.src_x);
823 src_y = wl_fixed_to_double(vp->buffer.src_y);
824 src_width = wl_fixed_to_double(vp->buffer.src_width);
825 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100826 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200827
828 *bx = sx * src_width / surface->width + src_x;
829 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100830}
831
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500832WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200833weston_surface_to_buffer_float(struct weston_surface *surface,
834 float sx, float sy, float *bx, float *by)
835{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200836 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
837
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300838 /* first transform coordinates if the viewport is set */
839 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100840
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500841 weston_transformed_coord(surface->width_from_buffer,
842 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200843 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100844 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200845}
846
Derek Foreman99739672015-12-03 16:38:11 -0600847/** Transform a rectangle from surface coordinates to buffer coordinates
848 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300849 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600850 * from.
851 * \param rect The rectangle to transform.
852 * \return The transformed rectangle.
853 *
854 * Viewport and buffer transformations can only do translation, scaling,
855 * and rotations in 90-degree steps. Therefore the only loss in the
856 * conversion is coordinate rounding.
857 *
858 * However, some coordinate rounding takes place as an intermediate
859 * step before the buffer scale factor is applied, so the rectangle
860 * boundary may not be exactly as expected.
861 *
862 * This is OK for damage tracking since a little extra coverage is
863 * not a problem.
864 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200865WL_EXPORT pixman_box32_t
866weston_surface_to_buffer_rect(struct weston_surface *surface,
867 pixman_box32_t rect)
868{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200869 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100870 float xf, yf;
871
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300872 /* first transform box coordinates if the viewport is set */
873 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100874 rect.x1 = floorf(xf);
875 rect.y1 = floorf(yf);
876
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300877 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600878 rect.x2 = ceilf(xf);
879 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100880
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500881 return weston_transformed_rect(surface->width_from_buffer,
882 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200883 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200884 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200885}
886
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200887/** Transform a region from surface coordinates to buffer coordinates
888 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300889 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200890 * from.
891 * \param surface_region[in] The region in surface coordinates.
892 * \param buffer_region[out] The region converted to buffer coordinates.
893 *
894 * Buffer_region must be init'd, but will be completely overwritten.
895 *
896 * Viewport and buffer transformations can only do translation, scaling,
897 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600898 * conversion is from the coordinate rounding that takes place in
899 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200900 */
901WL_EXPORT void
902weston_surface_to_buffer_region(struct weston_surface *surface,
903 pixman_region32_t *surface_region,
904 pixman_region32_t *buffer_region)
905{
906 pixman_box32_t *src_rects, *dest_rects;
907 int nrects, i;
908
909 src_rects = pixman_region32_rectangles(surface_region, &nrects);
910 dest_rects = malloc(nrects * sizeof(*dest_rects));
911 if (!dest_rects)
912 return;
913
914 for (i = 0; i < nrects; i++) {
915 dest_rects[i] = weston_surface_to_buffer_rect(surface,
916 src_rects[i]);
917 }
918
919 pixman_region32_fini(buffer_region);
920 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
921 free(dest_rects);
922}
923
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200924WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500925weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400926 struct weston_plane *plane)
927{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500928 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400929 return;
930
Jason Ekstranda7af7042013-10-12 22:38:11 -0500931 weston_view_damage_below(view);
932 view->plane = plane;
933 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400934}
935
Pekka Paalanen51723d52015-02-17 13:10:01 +0200936/** Inflict damage on the plane where the view is visible.
937 *
938 * \param view The view that causes the damage.
939 *
940 * If the view is currently on a plane (including the primary plane),
941 * take the view's boundingbox, subtract all the opaque views that cover it,
942 * and add the remaining region as damage to the plane. This corresponds
943 * to the damage inflicted to the plane if this view disappeared.
944 *
945 * A repaint is scheduled for this view.
946 *
947 * The region of all opaque views covering this view is stored in
948 * weston_view::clip and updated by view_accumulate_damage() during
949 * weston_output_repaint(). Specifically, that region matches the
950 * scenegraph as it was last painted.
951 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400952WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500953weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200954{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500955 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200956
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500957 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200958 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800960 if (view->plane)
961 pixman_region32_union(&view->plane->damage,
962 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500963 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800964 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200965}
966
Pekka Paalanen01e00682017-03-24 16:21:06 +0200967/** Send wl_surface.enter/leave events
968 *
969 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300970 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200971 * \param enter True if entered.
972 * \param left True if left.
973 *
974 * Send the enter/leave events for all protocol objects bound to the given
975 * output by the client owning the surface.
976 */
977static void
978weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300979 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +0200980 bool enter,
981 bool leave)
982{
983 struct wl_resource *wloutput;
984 struct wl_client *client;
985
986 assert(enter != leave);
987
988 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300989 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +0200990 if (wl_resource_get_client(wloutput) != client)
991 continue;
992
993 if (enter)
994 wl_surface_send_enter(surface->resource, wloutput);
995 if (leave)
996 wl_surface_send_leave(surface->resource, wloutput);
997 }
998}
999
Bryce Harrington3f650b82015-12-23 11:01:58 -08001000/**
1001 * \param es The surface
1002 * \param mask The new set of outputs for the surface
1003 *
1004 * Sets the surface's set of outputs to the ones specified by
1005 * the new output mask provided. Identifies the outputs that
1006 * have changed, the posts enter and leave events for these
1007 * outputs as appropriate.
1008 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001009static void
1010weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1011{
1012 uint32_t different = es->output_mask ^ mask;
1013 uint32_t entered = mask & different;
1014 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001015 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001016 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001017 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001018
1019 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001020 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001021 return;
1022 if (different == 0)
1023 return;
1024
1025 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001026 output_bit = 1u << output->id;
1027 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001028 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001029
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001030 wl_list_for_each(head, &output->head_list, output_link) {
1031 weston_surface_send_enter_leave(es, head,
1032 output_bit & entered,
1033 output_bit & left);
1034 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001035 }
1036}
1037
Semi Malinene7a52fb2018-04-26 11:08:10 +02001038static void
1039notify_view_output_destroy(struct wl_listener *listener, void *data)
1040{
1041 struct weston_view *view =
1042 container_of(listener,
1043 struct weston_view, output_destroy_listener);
1044
1045 view->output = NULL;
1046 view->output_destroy_listener.notify = NULL;
1047}
1048
1049/** Set the primary output of the view
1050 *
1051 * \param view The view whose primary output to set
1052 * \param output The new primary output for the view
1053 *
1054 * Set \a output to be the primary output of the \a view.
1055 *
1056 * Notice that the assignment may be temporary; the primary output could be
1057 * automatically changed. Hence, one cannot rely on the value persisting.
1058 *
1059 * Passing NULL as /a output will set the primary output to NULL.
1060 */
1061WL_EXPORT void
1062weston_view_set_output(struct weston_view *view, struct weston_output *output)
1063{
1064 if (view->output_destroy_listener.notify) {
1065 wl_list_remove(&view->output_destroy_listener.link);
1066 view->output_destroy_listener.notify = NULL;
1067 }
1068 view->output = output;
1069 if (output) {
1070 view->output_destroy_listener.notify =
1071 notify_view_output_destroy;
1072 wl_signal_add(&output->destroy_signal,
1073 &view->output_destroy_listener);
1074 }
1075}
1076
Bryce Harrington3f650b82015-12-23 11:01:58 -08001077/** Recalculate which output(s) the surface has views displayed on
1078 *
1079 * \param es The surface to remap to outputs
1080 *
1081 * Finds the output that is showing the largest amount of one
1082 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001083 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001084 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001085 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001086 * in the output_mask for the surface.
1087 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001088static void
1089weston_surface_assign_output(struct weston_surface *es)
1090{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001091 struct weston_output *new_output;
1092 struct weston_view *view;
1093 pixman_region32_t region;
1094 uint32_t max, area, mask;
1095 pixman_box32_t *e;
1096
1097 new_output = NULL;
1098 max = 0;
1099 mask = 0;
1100 pixman_region32_init(&region);
1101 wl_list_for_each(view, &es->views, surface_link) {
1102 if (!view->output)
1103 continue;
1104
1105 pixman_region32_intersect(&region, &view->transform.boundingbox,
1106 &view->output->region);
1107
1108 e = pixman_region32_extents(&region);
1109 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1110
1111 mask |= view->output_mask;
1112
1113 if (area >= max) {
1114 new_output = view->output;
1115 max = area;
1116 }
1117 }
1118 pixman_region32_fini(&region);
1119
1120 es->output = new_output;
1121 weston_surface_update_output_mask(es, mask);
1122}
1123
Bryce Harrington3f650b82015-12-23 11:01:58 -08001124/** Recalculate which output(s) the view is displayed on
1125 *
1126 * \param ev The view to remap to outputs
1127 *
1128 * Identifies the set of outputs that the view is visible on,
1129 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001130 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001131 *
1132 * Also does the same for the view's surface. See
1133 * weston_surface_assign_output().
1134 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135static void
1136weston_view_assign_output(struct weston_view *ev)
1137{
1138 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001139 struct weston_output *output, *new_output;
1140 pixman_region32_t region;
1141 uint32_t max, area, mask;
1142 pixman_box32_t *e;
1143
1144 new_output = NULL;
1145 max = 0;
1146 mask = 0;
1147 pixman_region32_init(&region);
1148 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001149 if (output->destroying)
1150 continue;
1151
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001153 &output->region);
1154
1155 e = pixman_region32_extents(&region);
1156 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1157
1158 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001159 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001160
1161 if (area >= max) {
1162 new_output = output;
1163 max = area;
1164 }
1165 }
1166 pixman_region32_fini(&region);
1167
Semi Malinene7a52fb2018-04-26 11:08:10 +02001168 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 ev->output_mask = mask;
1170
1171 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001172}
1173
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001174static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001175weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1176 int from_x, int from_y, int *to_x, int *to_y)
1177{
1178 float x, y;
1179
1180 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1181 weston_view_from_global_float(to, x, y, &x, &y);
1182
1183 *to_x = round(x);
1184 *to_y = round(y);
1185}
1186
1187static void
1188weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1189{
1190 pixman_box32_t *a;
1191 pixman_box32_t b;
1192
1193 a = pixman_region32_extents(&from->geometry.scissor);
1194
1195 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1196 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1197
1198 pixman_region32_fini(&to->geometry.scissor);
1199 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1200}
1201
1202static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001203view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001205{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001206 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1207 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001208 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001209 { inbox->x1, inbox->y1 },
1210 { inbox->x1, inbox->y2 },
1211 { inbox->x2, inbox->y1 },
1212 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001213 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001214 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001215 int i;
1216
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001217 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001218 /* avoid rounding empty bbox to 1x1 */
1219 pixman_region32_init(bbox);
1220 return;
1221 }
1222
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001223 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001224 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001226 if (x < min_x)
1227 min_x = x;
1228 if (x > max_x)
1229 max_x = x;
1230 if (y < min_y)
1231 min_y = y;
1232 if (y > max_y)
1233 max_y = y;
1234 }
1235
Pekka Paalanen219b9822012-02-08 15:38:37 +02001236 int_x = floorf(min_x);
1237 int_y = floorf(min_y);
1238 pixman_region32_init_rect(bbox, int_x, int_y,
1239 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001240}
1241
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001242static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001244{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001246
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001247 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248 view->geometry.x = roundf(view->geometry.x);
1249 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001250
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001251 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1253 view->transform.position.matrix.d[12] = view->geometry.x;
1254 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001255
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001257
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 view->transform.inverse = view->transform.position.matrix;
1259 view->transform.inverse.d[12] = -view->geometry.x;
1260 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001261
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001263 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001264 view->surface->width,
1265 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001266 if (view->geometry.scissor_enabled)
1267 pixman_region32_intersect(&view->transform.boundingbox,
1268 &view->transform.boundingbox,
1269 &view->geometry.scissor);
1270
1271 pixman_region32_translate(&view->transform.boundingbox,
1272 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001273
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 if (view->alpha == 1.0) {
1275 pixman_region32_copy(&view->transform.opaque,
1276 &view->surface->opaque);
1277 pixman_region32_translate(&view->transform.opaque,
1278 view->geometry.x,
1279 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001280 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001281}
1282
1283static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001285{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 struct weston_view *parent = view->geometry.parent;
1287 struct weston_matrix *matrix = &view->transform.matrix;
1288 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001289 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001290 pixman_region32_t surfregion;
1291 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001292
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001294
1295 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1297 view->transform.position.matrix.d[12] = view->geometry.x;
1298 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001299
1300 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001302 weston_matrix_multiply(matrix, &tform->matrix);
1303
Pekka Paalanen483243f2013-03-08 14:56:50 +02001304 if (parent)
1305 weston_matrix_multiply(matrix, &parent->transform.matrix);
1306
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001307 if (weston_matrix_invert(inverse, matrix) < 0) {
1308 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 weston_log("error: weston_view %p"
1310 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001311 return -1;
1312 }
1313
Giulio Camuffo148c1992016-09-04 18:50:46 +03001314 if (view->alpha == 1.0 &&
1315 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1316 pixman_region32_copy(&view->transform.opaque,
1317 &view->surface->opaque);
1318 pixman_region32_translate(&view->transform.opaque,
1319 matrix->d[12],
1320 matrix->d[13]);
1321 }
1322
Pekka Paalanen380adf52015-02-16 14:39:11 +02001323 pixman_region32_init_rect(&surfregion, 0, 0,
1324 view->surface->width, view->surface->height);
1325 if (view->geometry.scissor_enabled)
1326 pixman_region32_intersect(&surfregion, &surfregion,
1327 &view->geometry.scissor);
1328 surfbox = pixman_region32_extents(&surfregion);
1329
1330 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1331 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001332
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001333 return 0;
1334}
1335
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001336static struct weston_layer *
1337get_view_layer(struct weston_view *view)
1338{
1339 if (view->parent_view)
1340 return get_view_layer(view->parent_view);
1341 return view->layer_link.layer;
1342}
1343
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001344WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001346{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001348 struct weston_layer *layer;
1349 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001350
Jason Ekstranda7af7042013-10-12 22:38:11 -05001351 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001352 return;
1353
Pekka Paalanen483243f2013-03-08 14:56:50 +02001354 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001356
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001360
Jason Ekstranda7af7042013-10-12 22:38:11 -05001361 pixman_region32_fini(&view->transform.boundingbox);
1362 pixman_region32_fini(&view->transform.opaque);
1363 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001364
Pekka Paalanencd403622012-01-25 13:37:39 +02001365 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 if (view->geometry.transformation_list.next ==
1367 &view->transform.position.link &&
1368 view->geometry.transformation_list.prev ==
1369 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001370 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001372 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 if (weston_view_update_transform_enable(view) < 0)
1374 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001375 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001376
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001377 layer = get_view_layer(view);
1378 if (layer) {
1379 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001380 pixman_region32_intersect(&view->transform.boundingbox,
1381 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001382 pixman_region32_intersect(&view->transform.opaque,
1383 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001384 pixman_region32_fini(&mask);
1385 }
1386
Pekka Paalanen380adf52015-02-16 14:39:11 +02001387 if (parent) {
1388 if (parent->geometry.scissor_enabled) {
1389 view->geometry.scissor_enabled = true;
1390 weston_view_transfer_scissor(parent, view);
1391 } else {
1392 view->geometry.scissor_enabled = false;
1393 }
1394 }
1395
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001397
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001399
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400 wl_signal_emit(&view->surface->compositor->transform_signal,
1401 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001402}
1403
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001404WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001406{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001408
1409 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 * The invariant: if view->geometry.dirty, then all views
1411 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001412 * Corollary: if not parent->geometry.dirty, then all ancestors
1413 * are not dirty.
1414 */
1415
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001417 return;
1418
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001420
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001422 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001424}
1425
1426WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001427weston_view_to_global_fixed(struct weston_view *view,
1428 wl_fixed_t vx, wl_fixed_t vy,
1429 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001430{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001431 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001432
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 weston_view_to_global_float(view,
1434 wl_fixed_to_double(vx),
1435 wl_fixed_to_double(vy),
1436 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001437 *x = wl_fixed_from_double(xf);
1438 *y = wl_fixed_from_double(yf);
1439}
1440
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001441WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442weston_view_from_global_float(struct weston_view *view,
1443 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001444{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001446 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1447
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001449
1450 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001451 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001453 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454 *vx = 0;
1455 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001456 return;
1457 }
1458
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459 *vx = v.f[0] / v.f[3];
1460 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001461 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 *vx = x - view->geometry.x;
1463 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001464 }
1465}
1466
1467WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001468weston_view_from_global_fixed(struct weston_view *view,
1469 wl_fixed_t x, wl_fixed_t y,
1470 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001471{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001473
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 weston_view_from_global_float(view,
1475 wl_fixed_to_double(x),
1476 wl_fixed_to_double(y),
1477 &vxf, &vyf);
1478 *vx = wl_fixed_from_double(vxf);
1479 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001480}
1481
1482WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483weston_view_from_global(struct weston_view *view,
1484 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001485{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001487
Jason Ekstranda7af7042013-10-12 22:38:11 -05001488 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1489 *vx = floorf(vxf);
1490 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001491}
1492
Bryce Harrington3f650b82015-12-23 11:01:58 -08001493/**
1494 * \param surface The surface to be repainted
1495 *
1496 * Marks the output(s) that the surface is shown on as needing to be
1497 * repainted. See weston_output_schedule_repaint().
1498 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001499WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001500weston_surface_schedule_repaint(struct weston_surface *surface)
1501{
1502 struct weston_output *output;
1503
1504 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001505 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001506 weston_output_schedule_repaint(output);
1507}
1508
Bryce Harrington3f650b82015-12-23 11:01:58 -08001509/**
1510 * \param view The view to be repainted
1511 *
1512 * Marks the output(s) that the view is shown on as needing to be
1513 * repainted. See weston_output_schedule_repaint().
1514 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001515WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001516weston_view_schedule_repaint(struct weston_view *view)
1517{
1518 struct weston_output *output;
1519
1520 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001521 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522 weston_output_schedule_repaint(output);
1523}
1524
Pekka Paalanene508ce62015-02-19 13:59:55 +02001525/**
1526 * XXX: This function does it the wrong way.
1527 * surface->damage is the damage from the client, and causes
1528 * surface_flush_damage() to copy pixels. No window management action can
1529 * cause damage to the client-provided content, warranting re-upload!
1530 *
1531 * Instead of surface->damage, this function should record the damage
1532 * with all the views for this surface to avoid extraneous texture
1533 * uploads.
1534 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001536weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001537{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001538 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001539 0, 0, surface->width,
1540 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001541
Kristian Høgsberg98238702012-08-03 16:29:12 -04001542 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001543}
1544
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001545WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001546weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001547{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001548 if (view->geometry.x == x && view->geometry.y == y)
1549 return;
1550
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 view->geometry.x = x;
1552 view->geometry.y = y;
1553 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001554}
1555
Pekka Paalanen483243f2013-03-08 14:56:50 +02001556static void
1557transform_parent_handle_parent_destroy(struct wl_listener *listener,
1558 void *data)
1559{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 struct weston_view *view =
1561 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001562 geometry.parent_destroy_listener);
1563
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001565}
1566
1567WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001568weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001569 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001570{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001571 if (view->geometry.parent) {
1572 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1573 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001574
1575 if (!parent)
1576 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001577 }
1578
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001580
Jason Ekstranda7af7042013-10-12 22:38:11 -05001581 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001582 transform_parent_handle_parent_destroy;
1583 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001584 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001585 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001586 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001587 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001588 }
1589
Jason Ekstranda7af7042013-10-12 22:38:11 -05001590 weston_view_geometry_dirty(view);
1591}
1592
Pekka Paalanen380adf52015-02-16 14:39:11 +02001593/** Set a clip mask rectangle on a view
1594 *
1595 * \param view The view to set the clip mask on.
1596 * \param x Top-left corner X coordinate of the clip rectangle.
1597 * \param y Top-left corner Y coordinate of the clip rectangle.
1598 * \param width Width of the clip rectangle, non-negative.
1599 * \param height Height of the clip rectangle, non-negative.
1600 *
1601 * A shell may set a clip mask rectangle on a view. Everything outside
1602 * the rectangle is cut away for input and output purposes: it is
1603 * not drawn and cannot be hit by hit-test based input like pointer
1604 * motion or touch-downs. Everything inside the rectangle will behave
1605 * normally. Clients are unaware of clipping.
1606 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001607 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001608 * mask rectangle does not affect the view position, the view is positioned
1609 * as it would be without a clip. The clip also does not change
1610 * weston_surface::width,height.
1611 *
1612 * The clip mask rectangle is part of transformation inheritance
1613 * (weston_view_set_transform_parent()). A clip set in the root of the
1614 * transformation inheritance tree will affect all views in the tree.
1615 * A clip can be set only on the root view. Attempting to set a clip
1616 * on view that has a transformation parent will fail. Assigning a parent
1617 * to a view that has a clip set will cause the clip to be forgotten.
1618 *
1619 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1620 * on the additional transformations in the child views. These transformations
1621 * may not rotate the coordinate axes, i.e., only translation and scaling
1622 * are allowed. Violating this restriction causes the clipping to malfunction.
1623 * Furthermore, using scaling may cause rounding errors in child clipping.
1624 *
1625 * The clip mask rectangle is not automatically adjusted based on
1626 * wl_surface.attach dx and dy arguments.
1627 *
1628 * A clip mask rectangle can be set only if the compositor capability
1629 * WESTON_CAP_VIEW_CLIP_MASK is present.
1630 *
1631 * This function sets the clip mask rectangle and schedules a repaint for
1632 * the view.
1633 */
1634WL_EXPORT void
1635weston_view_set_mask(struct weston_view *view,
1636 int x, int y, int width, int height)
1637{
1638 struct weston_compositor *compositor = view->surface->compositor;
1639
1640 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1641 weston_log("%s not allowed without capability!\n", __func__);
1642 return;
1643 }
1644
1645 if (view->geometry.parent) {
1646 weston_log("view %p has a parent, clip forbidden!\n", view);
1647 return;
1648 }
1649
1650 if (width < 0 || height < 0) {
1651 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1652 x, y, width, height);
1653 return;
1654 }
1655
1656 pixman_region32_fini(&view->geometry.scissor);
1657 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1658 view->geometry.scissor_enabled = true;
1659 weston_view_geometry_dirty(view);
1660 weston_view_schedule_repaint(view);
1661}
1662
1663/** Remove the clip mask from a view
1664 *
1665 * \param view The view to remove the clip mask from.
1666 *
1667 * Removed the clip mask rectangle and schedules a repaint.
1668 *
1669 * \sa weston_view_set_mask
1670 */
1671WL_EXPORT void
1672weston_view_set_mask_infinite(struct weston_view *view)
1673{
1674 view->geometry.scissor_enabled = false;
1675 weston_view_geometry_dirty(view);
1676 weston_view_schedule_repaint(view);
1677}
1678
Armin Krezović0da12b82016-06-30 06:04:33 +02001679/* Check if view should be displayed
1680 *
1681 * The indicator is set manually when assigning
1682 * a view to a surface.
1683 *
1684 * This needs reworking. See the thread starting at:
1685 *
1686 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1687 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001688WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001689weston_view_is_mapped(struct weston_view *view)
1690{
Armin Krezović0da12b82016-06-30 06:04:33 +02001691 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001692}
1693
Philipp Zabel70decd52018-09-03 20:11:15 +02001694/* Check if view is opaque in specified region
1695 *
1696 * \param view The view to check for opacity.
1697 * \param region The region to check for opacity, in view coordinates.
1698 *
1699 * Returns true if the view is opaque in the specified region, because view
1700 * alpha is 1.0 and either the opaque region set by the client contains the
1701 * specified region, or the buffer pixel format or solid color is opaque.
1702 */
1703WL_EXPORT bool
1704weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1705{
1706 pixman_region32_t r;
1707 bool ret = false;
1708
1709 if (ev->alpha < 1.0)
1710 return false;
1711
1712 if (ev->surface->is_opaque)
1713 return true;
1714
1715 if (ev->transform.dirty) {
1716 weston_log("%s: transform dirty", __func__);
1717 return false;
1718 }
1719
1720 pixman_region32_init(&r);
1721 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1722
1723 if (!pixman_region32_not_empty(&r))
1724 ret = true;
1725
1726 pixman_region32_fini(&r);
1727
1728 return ret;
1729}
1730
Armin Krezović0da12b82016-06-30 06:04:33 +02001731/* Check if a surface has a view assigned to it
1732 *
1733 * The indicator is set manually when mapping
1734 * a surface and creating a view for it.
1735 *
1736 * This needs to go. See the thread starting at:
1737 *
1738 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1739 *
1740 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001741WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001742weston_surface_is_mapped(struct weston_surface *surface)
1743{
Armin Krezović0da12b82016-06-30 06:04:33 +02001744 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001745}
1746
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001747static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001748surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001749{
1750 struct weston_view *view;
1751
1752 if (surface->width == width && surface->height == height)
1753 return;
1754
1755 surface->width = width;
1756 surface->height = height;
1757
1758 wl_list_for_each(view, &surface->views, surface_link)
1759 weston_view_geometry_dirty(view);
1760}
1761
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001762WL_EXPORT void
1763weston_surface_set_size(struct weston_surface *surface,
1764 int32_t width, int32_t height)
1765{
1766 assert(!surface->resource);
1767 surface_set_size(surface, width, height);
1768}
1769
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001770static int
1771fixed_round_up_to_int(wl_fixed_t f)
1772{
1773 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1774}
1775
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001776static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001777convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1778 int32_t width, int32_t height,
1779 uint32_t transform,
1780 int32_t scale)
1781{
1782 assert(scale > 0);
1783
1784 switch (transform) {
1785 case WL_OUTPUT_TRANSFORM_NORMAL:
1786 case WL_OUTPUT_TRANSFORM_180:
1787 case WL_OUTPUT_TRANSFORM_FLIPPED:
1788 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1789 *width_out = width / scale;
1790 *height_out = height / scale;
1791 break;
1792 case WL_OUTPUT_TRANSFORM_90:
1793 case WL_OUTPUT_TRANSFORM_270:
1794 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1795 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1796 *width_out = height / scale;
1797 *height_out = width / scale;
1798 break;
1799 default:
1800 assert(0 && "invalid transform");
1801 }
1802}
1803
1804static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001805weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001806{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001807 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001808
1809 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001810 surface->width_from_buffer = 0;
1811 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001812 return;
1813 }
1814
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001815 convert_size_by_transform_scale(&surface->width_from_buffer,
1816 &surface->height_from_buffer,
1817 surface->buffer_ref.buffer->width,
1818 surface->buffer_ref.buffer->height,
1819 vp->buffer.transform,
1820 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001821}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001822
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001823static void
1824weston_surface_update_size(struct weston_surface *surface)
1825{
1826 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1827 int32_t width, height;
1828
1829 width = surface->width_from_buffer;
1830 height = surface->height_from_buffer;
1831
1832 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001833 surface_set_size(surface,
1834 vp->surface.width, vp->surface.height);
1835 return;
1836 }
1837
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001838 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001839 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1840 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1841
1842 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001843 return;
1844 }
1845
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001846 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001847}
1848
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001849WL_EXPORT void
1850weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001851{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001852 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001853}
1854
Jason Ekstranda7af7042013-10-12 22:38:11 -05001855WL_EXPORT struct weston_view *
1856weston_compositor_pick_view(struct weston_compositor *compositor,
1857 wl_fixed_t x, wl_fixed_t y,
1858 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001859{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001860 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001861 wl_fixed_t view_x, view_y;
1862 int view_ix, view_iy;
1863 int ix = wl_fixed_to_int(x);
1864 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001865
Jason Ekstranda7af7042013-10-12 22:38:11 -05001866 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001867 if (!pixman_region32_contains_point(
1868 &view->transform.boundingbox, ix, iy, NULL))
1869 continue;
1870
1871 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1872 view_ix = wl_fixed_to_int(view_x);
1873 view_iy = wl_fixed_to_int(view_y);
1874
1875 if (!pixman_region32_contains_point(&view->surface->input,
1876 view_ix, view_iy, NULL))
1877 continue;
1878
Pekka Paalanen380adf52015-02-16 14:39:11 +02001879 if (view->geometry.scissor_enabled &&
1880 !pixman_region32_contains_point(&view->geometry.scissor,
1881 view_ix, view_iy, NULL))
1882 continue;
1883
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001884 *vx = view_x;
1885 *vy = view_y;
1886 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001887 }
1888
Derek Foremanf9318d12015-05-11 15:40:11 -05001889 *vx = wl_fixed_from_int(-1000000);
1890 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001891 return NULL;
1892}
1893
1894static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001895weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001896{
Daniel Stone37816df2012-05-16 18:45:18 +01001897 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001898
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001899 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001900 return;
1901
Daniel Stone37816df2012-05-16 18:45:18 +01001902 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001903 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001904}
1905
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001906WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001907weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001908{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001909 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001910
Jason Ekstranda7af7042013-10-12 22:38:11 -05001911 if (!weston_view_is_mapped(view))
1912 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001913
Jason Ekstranda7af7042013-10-12 22:38:11 -05001914 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02001915 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08001916 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001917 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001918 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001919 wl_list_remove(&view->link);
1920 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001921 view->output_mask = 0;
1922 weston_surface_assign_output(view->surface);
1923
1924 if (weston_surface_is_mapped(view->surface))
1925 return;
1926
1927 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001928 struct weston_touch *touch = weston_seat_get_touch(seat);
1929 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1930 struct weston_keyboard *keyboard =
1931 weston_seat_get_keyboard(seat);
1932
1933 if (keyboard && keyboard->focus == view->surface)
1934 weston_keyboard_set_focus(keyboard, NULL);
1935 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001936 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001937 if (touch && touch->focus == view)
1938 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001939 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001940}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001941
Jason Ekstranda7af7042013-10-12 22:38:11 -05001942WL_EXPORT void
1943weston_surface_unmap(struct weston_surface *surface)
1944{
1945 struct weston_view *view;
1946
Armin Krezovićf8486c32016-06-30 06:04:28 +02001947 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001948 wl_list_for_each(view, &surface->views, surface_link)
1949 weston_view_unmap(view);
1950 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001951}
1952
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001953static void
1954weston_surface_reset_pending_buffer(struct weston_surface *surface)
1955{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001956 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001957 surface->pending.sx = 0;
1958 surface->pending.sy = 0;
1959 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001960 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001961}
1962
Jason Ekstranda7af7042013-10-12 22:38:11 -05001963WL_EXPORT void
1964weston_view_destroy(struct weston_view *view)
1965{
1966 wl_signal_emit(&view->destroy_signal, view);
1967
1968 assert(wl_list_empty(&view->geometry.child_list));
1969
1970 if (weston_view_is_mapped(view)) {
1971 weston_view_unmap(view);
1972 weston_compositor_build_view_list(view->surface->compositor);
1973 }
1974
1975 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001976 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001977
1978 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001979 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001980 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001981 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001982
1983 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03001984 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001985
Jason Ekstranda7af7042013-10-12 22:38:11 -05001986 wl_list_remove(&view->surface_link);
1987
1988 free(view);
1989}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001990
1991WL_EXPORT void
1992weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001993{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001994 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001995 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001996 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001997
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001998 if (--surface->ref_count > 0)
1999 return;
2000
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002001 assert(surface->resource == NULL);
2002
Pekka Paalanenca790762015-04-17 14:23:38 +03002003 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002004
Pekka Paalanene67858b2013-04-25 13:57:42 +03002005 assert(wl_list_empty(&surface->subsurface_list_pending));
2006 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002007
Jason Ekstranda7af7042013-10-12 22:38:11 -05002008 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2009 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002010
Jason Ekstrand7b982072014-05-20 14:33:03 -05002011 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002012
Pekka Paalanende685b82012-12-04 15:58:12 +02002013 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002014
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002015 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002016 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002017 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002018
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002019 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002020 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002021
Pekka Paalanen133e4392014-09-23 22:08:46 -04002022 weston_presentation_feedback_discard_list(&surface->feedback_list);
2023
Jonas Ådahld3414f22016-07-22 17:56:31 +08002024 wl_list_for_each_safe(constraint, next_constraint,
2025 &surface->pointer_constraints,
2026 link)
2027 weston_pointer_constraint_destroy(constraint);
2028
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002029 fd_clear(&surface->acquire_fence_fd);
2030
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002031 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002032}
2033
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002034static void
2035destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002036{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002037 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002038
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002039 assert(surface);
2040
Giulio Camuffo0d379742013-11-15 22:06:15 +01002041 /* Set the resource to NULL, since we don't want to leave a
2042 * dangling pointer if the surface was refcounted and survives
2043 * the weston_surface_destroy() call. */
2044 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002045
2046 if (surface->viewport_resource)
2047 wl_resource_set_user_data(surface->viewport_resource, NULL);
2048
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002049 if (surface->synchronization_resource) {
2050 wl_resource_set_user_data(surface->synchronization_resource,
2051 NULL);
2052 }
2053
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002054 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002055}
2056
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002057static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002058weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2059{
2060 struct weston_buffer *buffer =
2061 container_of(listener, struct weston_buffer, destroy_listener);
2062
2063 wl_signal_emit(&buffer->destroy_signal, buffer);
2064 free(buffer);
2065}
2066
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002067WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002068weston_buffer_from_resource(struct wl_resource *resource)
2069{
2070 struct weston_buffer *buffer;
2071 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002072
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002073 listener = wl_resource_get_destroy_listener(resource,
2074 weston_buffer_destroy_handler);
2075
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002076 if (listener)
2077 return container_of(listener, struct weston_buffer,
2078 destroy_listener);
2079
2080 buffer = zalloc(sizeof *buffer);
2081 if (buffer == NULL)
2082 return NULL;
2083
2084 buffer->resource = resource;
2085 wl_signal_init(&buffer->destroy_signal);
2086 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002087 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002088 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002089
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002090 return buffer;
2091}
2092
2093static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002094weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2095 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002096{
Pekka Paalanende685b82012-12-04 15:58:12 +02002097 struct weston_buffer_reference *ref =
2098 container_of(listener, struct weston_buffer_reference,
2099 destroy_listener);
2100
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002101 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002102 ref->buffer = NULL;
2103}
2104
2105WL_EXPORT void
2106weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002107 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002108{
2109 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002110 ref->buffer->busy_count--;
2111 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002112 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002113 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002114 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002115 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002116 }
2117
Pekka Paalanende685b82012-12-04 15:58:12 +02002118 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002119 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002120 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002121 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002122 }
2123
Pekka Paalanende685b82012-12-04 15:58:12 +02002124 ref->buffer = buffer;
2125 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2126}
2127
2128static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002129weston_surface_attach(struct weston_surface *surface,
2130 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002131{
2132 weston_buffer_reference(&surface->buffer_ref, buffer);
2133
Pekka Paalanena6421c42012-12-04 15:58:10 +02002134 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002135 if (weston_surface_is_mapped(surface))
2136 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002137 }
2138
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002139 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002140
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002141 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002142 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002143}
2144
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002145WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002146weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002147{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002148 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002149
2150 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002151 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002152}
2153
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002154WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002155weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002156{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002157 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002158
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002159 pixman_region32_union(&compositor->primary_plane.damage,
2160 &compositor->primary_plane.damage,
2161 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002162 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002163}
2164
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002165static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002166surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002167{
Pekka Paalanende685b82012-12-04 15:58:12 +02002168 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002169 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002170 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002171
Pekka Paalanenb5026542014-11-12 15:09:24 +02002172 if (weston_timeline_enabled_ &&
2173 pixman_region32_not_empty(&surface->damage))
2174 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2175 TLP_OUTPUT(surface->output), TLP_END);
2176
Jason Ekstrandef540082014-06-26 10:37:36 -07002177 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178}
2179
2180static void
2181view_accumulate_damage(struct weston_view *view,
2182 pixman_region32_t *opaque)
2183{
2184 pixman_region32_t damage;
2185
2186 pixman_region32_init(&damage);
2187 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002188 pixman_box32_t *extents;
2189
Jason Ekstranda7af7042013-10-12 22:38:11 -05002190 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002191 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002192 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002193 pixman_region32_copy(&damage, &view->surface->damage);
2194 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002195 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002196 }
2197
Pekka Paalanen380adf52015-02-16 14:39:11 +02002198 pixman_region32_intersect(&damage, &damage,
2199 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002200 pixman_region32_subtract(&damage, &damage, opaque);
2201 pixman_region32_union(&view->plane->damage,
2202 &view->plane->damage, &damage);
2203 pixman_region32_fini(&damage);
2204 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002205 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002206}
2207
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002208static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002209compositor_accumulate_damage(struct weston_compositor *ec)
2210{
2211 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002213 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002214
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002215 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002216
2217 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002218 pixman_region32_copy(&plane->clip, &clip);
2219
2220 pixman_region32_init(&opaque);
2221
Jason Ekstranda7af7042013-10-12 22:38:11 -05002222 wl_list_for_each(ev, &ec->view_list, link) {
2223 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002224 continue;
2225
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002227 }
2228
2229 pixman_region32_union(&clip, &clip, &opaque);
2230 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002231 }
2232
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002233 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002234
Jason Ekstranda7af7042013-10-12 22:38:11 -05002235 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002236 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002237
2238 wl_list_for_each(ev, &ec->view_list, link) {
2239 if (ev->surface->touched)
2240 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002241 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002242
2243 surface_flush_damage(ev->surface);
2244
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002245 /* Both the renderer and the backend have seen the buffer
2246 * by now. If renderer needs the buffer, it has its own
2247 * reference set. If the backend wants to keep the buffer
2248 * around for migrating the surface into a non-primary plane
2249 * later, keep_buffer is true. Otherwise, drop the core
2250 * reference now, and allow early buffer release. This enables
2251 * clients to use single-buffering.
2252 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002253 if (!ev->surface->keep_buffer)
2254 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002255 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002256}
2257
2258static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002259surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002260{
2261 struct weston_subsurface *sub;
2262
Pekka Paalanene67858b2013-04-25 13:57:42 +03002263 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002264 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002265 continue;
2266
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2268 wl_list_init(&sub->surface->views);
2269
2270 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002271 }
2272}
2273
2274static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002275surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002276{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002277 struct weston_subsurface *sub;
2278 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002279
Jason Ekstranda7af7042013-10-12 22:38:11 -05002280 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2281 if (sub->surface == surface)
2282 continue;
2283
George Kiagiadakised04d382014-06-13 18:10:26 +02002284 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2285 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002286 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002287 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288
2289 surface_free_unused_subsurface_views(sub->surface);
2290 }
2291}
2292
2293static void
2294view_list_add_subsurface_view(struct weston_compositor *compositor,
2295 struct weston_subsurface *sub,
2296 struct weston_view *parent)
2297{
2298 struct weston_subsurface *child;
2299 struct weston_view *view = NULL, *iv;
2300
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002301 if (!weston_surface_is_mapped(sub->surface))
2302 return;
2303
Jason Ekstranda7af7042013-10-12 22:38:11 -05002304 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2305 if (iv->geometry.parent == parent) {
2306 view = iv;
2307 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002308 }
2309 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002310
2311 if (view) {
2312 /* Put it back in the surface's list of views */
2313 wl_list_remove(&view->surface_link);
2314 wl_list_insert(&sub->surface->views, &view->surface_link);
2315 } else {
2316 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002317 weston_view_set_position(view,
2318 sub->position.x,
2319 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002320 weston_view_set_transform_parent(view, parent);
2321 }
2322
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002323 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002324 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002325 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002326
Pekka Paalanenb188e912013-11-19 14:03:35 +02002327 if (wl_list_empty(&sub->surface->subsurface_list)) {
2328 wl_list_insert(compositor->view_list.prev, &view->link);
2329 return;
2330 }
2331
2332 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2333 if (child->surface == sub->surface)
2334 wl_list_insert(compositor->view_list.prev, &view->link);
2335 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002336 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002337 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002338}
2339
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002340/* This recursively adds the sub-surfaces for a view, relying on the
2341 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2342 * change first happens to the sub-surface list, and then automatically
2343 * propagates here. See weston_surface_damage_subsurfaces() for how the
2344 * sub-surfaces receive damage when the client changes the state.
2345 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002346static void
2347view_list_add(struct weston_compositor *compositor,
2348 struct weston_view *view)
2349{
2350 struct weston_subsurface *sub;
2351
2352 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002353
Pekka Paalanenb188e912013-11-19 14:03:35 +02002354 if (wl_list_empty(&view->surface->subsurface_list)) {
2355 wl_list_insert(compositor->view_list.prev, &view->link);
2356 return;
2357 }
2358
2359 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2360 if (sub->surface == view->surface)
2361 wl_list_insert(compositor->view_list.prev, &view->link);
2362 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002363 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002364 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002365}
2366
2367static void
2368weston_compositor_build_view_list(struct weston_compositor *compositor)
2369{
2370 struct weston_view *view;
2371 struct weston_layer *layer;
2372
2373 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002374 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002375 surface_stash_subsurface_views(view->surface);
2376
2377 wl_list_init(&compositor->view_list);
2378 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002379 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002380 view_list_add(compositor, view);
2381 }
2382 }
2383
2384 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002385 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002386 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002387}
2388
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002389static void
2390weston_output_take_feedback_list(struct weston_output *output,
2391 struct weston_surface *surface)
2392{
2393 struct weston_view *view;
2394 struct weston_presentation_feedback *feedback;
2395 uint32_t flags = 0xffffffff;
2396
2397 if (wl_list_empty(&surface->feedback_list))
2398 return;
2399
2400 /* All views must have the flag for the flag to survive. */
2401 wl_list_for_each(view, &surface->views, surface_link) {
2402 /* ignore views that are not on this output at all */
2403 if (view->output_mask & (1u << output->id))
2404 flags &= view->psf_flags;
2405 }
2406
2407 wl_list_for_each(feedback, &surface->feedback_list, link)
2408 feedback->psf_flags = flags;
2409
2410 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2411 wl_list_init(&surface->feedback_list);
2412}
2413
David Herrmann1edf44c2013-10-22 17:11:26 +02002414static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002415weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002416{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002417 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002418 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002419 struct weston_animation *animation, *next;
2420 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002421 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002422 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002423 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002424 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002425
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002426 if (output->destroying)
2427 return 0;
2428
Pekka Paalanenb5026542014-11-12 15:09:24 +02002429 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2430
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002431 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002432 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002433
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002434 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002435 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002436 } else {
2437 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002438 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002439 ev->psf_flags = 0;
2440 }
2441 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002442
Pekka Paalanene67858b2013-04-25 13:57:42 +03002443 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002444 wl_list_for_each(ev, &ec->view_list, link) {
2445 /* Note: This operation is safe to do multiple times on the
2446 * same surface.
2447 */
2448 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002449 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002450 &ev->surface->frame_callback_list);
2451 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002452
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002453 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002454 }
2455 }
2456
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002457 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002458
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002459 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002460 pixman_region32_intersect(&output_damage,
2461 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002462 pixman_region32_subtract(&output_damage,
2463 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002464
Scott Moreauccbf29d2012-02-22 14:21:41 -07002465 if (output->dirty)
2466 weston_output_update_matrix(output);
2467
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002468 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002469
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002470 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002471
Daniel Stone09a97e22017-03-01 11:34:06 +00002472 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002473 if (r == 0)
2474 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002475
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002476 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002477
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002478 frame_time_msec = timespec_to_msec(&output->frame_time);
2479
Jonas Ådahldb773762012-06-13 00:01:21 +02002480 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002481 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002482 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002483 }
2484
Scott Moreaud64cf212012-06-08 19:40:54 -06002485 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002486 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002487 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002488 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002489
Pekka Paalanenb5026542014-11-12 15:09:24 +02002490 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2491
David Herrmann1edf44c2013-10-22 17:11:26 +02002492 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002493}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002494
Pekka Paalanen82919792014-05-21 13:51:49 +03002495static void
2496weston_output_schedule_repaint_reset(struct weston_output *output)
2497{
Daniel Stone05df8c12017-03-03 16:59:42 +00002498 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002499 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002500}
2501
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002502static int
2503weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2504 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002505{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002506 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002507 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002508 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002509
Daniel Stone6847b852017-03-01 11:34:08 +00002510 /* We're not ready yet; come back to make a decision later. */
2511 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002512 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002513
2514 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2515 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002516 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002517
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002518 /* If we're sleeping, drop the repaint machinery entirely; we will
2519 * explicitly repaint all outputs when we come back. */
2520 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2521 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2522 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002523
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002524 /* We don't actually need to repaint this output; drop it from
2525 * repaint until something causes damage. */
2526 if (!output->repaint_needed)
2527 goto err;
2528
2529 /* If repaint fails, we aren't going to get weston_output_finish_frame
2530 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002531 * something schedules a successful repaint later. As repainting may
2532 * take some time, re-read our clock as a courtesy to the next
2533 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002534 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002535 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002536 if (ret != 0)
2537 goto err;
2538
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002539 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002540 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002541
2542err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002543 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002544 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002545}
2546
2547static void
2548output_repaint_timer_arm(struct weston_compositor *compositor)
2549{
2550 struct weston_output *output;
2551 bool any_should_repaint = false;
2552 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002553 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002554
2555 weston_compositor_read_presentation_clock(compositor, &now);
2556
2557 wl_list_for_each(output, &compositor->output_list, link) {
2558 int64_t msec_to_this;
2559
2560 if (output->repaint_status != REPAINT_SCHEDULED)
2561 continue;
2562
2563 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2564 &now);
2565 if (!any_should_repaint || msec_to_this < msec_to_next)
2566 msec_to_next = msec_to_this;
2567
2568 any_should_repaint = true;
2569 }
2570
2571 if (!any_should_repaint)
2572 return;
2573
2574 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2575 * This is a workaround to allow coalescing multiple output repaints
2576 * particularly from weston_output_finish_frame()
2577 * into the same call, which would not happen if we called
2578 * output_repaint_timer_handler() directly.
2579 */
2580 if (msec_to_next < 1)
2581 msec_to_next = 1;
2582
2583 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2584}
2585
2586static int
2587output_repaint_timer_handler(void *data)
2588{
2589 struct weston_compositor *compositor = data;
2590 struct weston_output *output;
2591 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002592 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002593 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002594
2595 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002596
2597 if (compositor->backend->repaint_begin)
2598 repaint_data = compositor->backend->repaint_begin(compositor);
2599
2600 wl_list_for_each(output, &compositor->output_list, link) {
2601 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2602 if (ret)
2603 break;
2604 }
2605
2606 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002607 if (compositor->backend->repaint_flush)
2608 compositor->backend->repaint_flush(compositor,
2609 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002610 } else {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002611 wl_list_for_each(output, &compositor->output_list, link) {
2612 if (output->repainted)
2613 weston_output_schedule_repaint_reset(output);
2614 }
2615
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002616 if (compositor->backend->repaint_cancel)
2617 compositor->backend->repaint_cancel(compositor,
2618 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002619 }
Daniel Stone6847b852017-03-01 11:34:08 +00002620
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002621 wl_list_for_each(output, &compositor->output_list, link)
2622 output->repainted = false;
2623
Daniel Stone6847b852017-03-01 11:34:08 +00002624 output_repaint_timer_arm(compositor);
2625
Pekka Paalanen0513a952014-05-21 16:17:27 +03002626 return 0;
2627}
2628
Kristian Høgsbergef044142011-06-21 15:02:12 -04002629WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002630weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002631 const struct timespec *stamp,
2632 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002633{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002634 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002635 int32_t refresh_nsec;
2636 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002637 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002638
Pekka Paalanenb5026542014-11-12 15:09:24 +02002639
Daniel Stone05df8c12017-03-03 16:59:42 +00002640 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002641 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2642
Daniel Stone6847b852017-03-01 11:34:08 +00002643 weston_compositor_read_presentation_clock(compositor, &now);
2644
Daniel Stone3615ce12017-03-01 11:34:05 +00002645 /* If we haven't been supplied any timestamp at all, we don't have a
2646 * timebase to work against, so any delay just wastes time. Push a
2647 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002648 if (!stamp) {
2649 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002650 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002651 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002652
Marius Vladdf9278a2018-03-06 18:56:23 +02002653 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2654 TLP_VBLANK(stamp), TLP_END);
2655
Pekka Paalanend7894d02015-07-03 15:08:53 +03002656 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002657 weston_presentation_feedback_present_list(&output->feedback_list,
2658 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002659 output->msc,
2660 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002661
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002662 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002663
Daniel Stone6847b852017-03-01 11:34:08 +00002664 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2665 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2666 -compositor->repaint_msec);
2667 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002668
2669 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002670 static bool warned;
2671
2672 if (!warned)
2673 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002674 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002675 warned = true;
2676
Daniel Stone6847b852017-03-01 11:34:08 +00002677 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002678 }
2679
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002680 /* Called from restart_repaint_loop and restart happens already after
2681 * the deadline given by repaint_msec? In that case we delay until
2682 * the deadline of the next frame, to give clients a more predictable
2683 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002684 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2685 msec_rel < 0) {
2686 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2687 timespec_add_nsec(&output->next_repaint,
2688 &output->next_repaint,
2689 refresh_nsec);
2690 }
2691 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002692
Daniel Stone3615ce12017-03-01 11:34:05 +00002693out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002694 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002695 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002696}
2697
2698static void
2699idle_repaint(void *data)
2700{
2701 struct weston_output *output = data;
2702
Daniel Stone05df8c12017-03-03 16:59:42 +00002703 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2704 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002705 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002706 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002707}
2708
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002709WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002710weston_layer_entry_insert(struct weston_layer_entry *list,
2711 struct weston_layer_entry *entry)
2712{
2713 wl_list_insert(&list->link, &entry->link);
2714 entry->layer = list->layer;
2715}
2716
2717WL_EXPORT void
2718weston_layer_entry_remove(struct weston_layer_entry *entry)
2719{
2720 wl_list_remove(&entry->link);
2721 wl_list_init(&entry->link);
2722 entry->layer = NULL;
2723}
2724
Quentin Glidic82681572016-12-17 13:40:51 +01002725
2726/** Initialize the weston_layer struct.
2727 *
2728 * \param compositor The compositor instance
2729 * \param layer The layer to initialize
2730 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002731WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002732weston_layer_init(struct weston_layer *layer,
2733 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002734{
Quentin Glidic82681572016-12-17 13:40:51 +01002735 layer->compositor = compositor;
2736 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002737 wl_list_init(&layer->view_list.link);
2738 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002739 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002740}
2741
2742/** Sets the position of the layer in the layer list. The layer will be placed
2743 * below any layer with the same position value, if any.
2744 * This function is safe to call if the layer is already on the list, but the
2745 * layer may be moved below other layers at the same position, if any.
2746 *
2747 * \param layer The layer to modify
2748 * \param position The position the layer will be placed at
2749 */
2750WL_EXPORT void
2751weston_layer_set_position(struct weston_layer *layer,
2752 enum weston_layer_position position)
2753{
2754 struct weston_layer *below;
2755
2756 wl_list_remove(&layer->link);
2757
2758 /* layer_list is ordered from top to bottom, the last layer being the
2759 * background with the smallest position value */
2760
2761 layer->position = position;
2762 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2763 if (below->position >= layer->position) {
2764 wl_list_insert(&below->link, &layer->link);
2765 return;
2766 }
2767 }
2768 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2769}
2770
2771/** Hide a layer by taking it off the layer list.
2772 * This function is safe to call if the layer is not on the list.
2773 *
2774 * \param layer The layer to hide
2775 */
2776WL_EXPORT void
2777weston_layer_unset_position(struct weston_layer *layer)
2778{
2779 wl_list_remove(&layer->link);
2780 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002781}
2782
2783WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002784weston_layer_set_mask(struct weston_layer *layer,
2785 int x, int y, int width, int height)
2786{
2787 struct weston_view *view;
2788
2789 layer->mask.x1 = x;
2790 layer->mask.x2 = x + width;
2791 layer->mask.y1 = y;
2792 layer->mask.y2 = y + height;
2793
2794 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2795 weston_view_geometry_dirty(view);
2796 }
2797}
2798
2799WL_EXPORT void
2800weston_layer_set_mask_infinite(struct weston_layer *layer)
2801{
2802 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2803 UINT32_MAX, UINT32_MAX);
2804}
2805
Daniel Stone3b775632018-07-20 08:38:25 +01002806WL_EXPORT bool
2807weston_layer_mask_is_infinite(struct weston_layer *layer)
2808{
2809 return layer->mask.x1 == INT32_MIN &&
2810 layer->mask.y1 == INT32_MIN &&
2811 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
2812 layer->mask.y2 == INT32_MIN + UINT32_MAX;
2813}
2814
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002815WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002816weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002817{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002818 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002819 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002820
Bryce Harrington08976ac2016-08-30 12:05:16 -07002821 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2822 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002823 return;
2824
Pekka Paalanenb5026542014-11-12 15:09:24 +02002825 if (!output->repaint_needed)
2826 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2827
Kristian Høgsbergef044142011-06-21 15:02:12 -04002828 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002829 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002830
2831 /* If we already have a repaint scheduled for our idle handler,
2832 * no need to set it again. If the repaint has been called but
2833 * not finished, then weston_output_finish_frame() will notice
2834 * that a repaint is needed and schedule one. */
2835 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002836 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002837
Daniel Stone05df8c12017-03-03 16:59:42 +00002838 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002839 assert(!output->idle_repaint_source);
2840 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2841 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002842 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002843}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002844
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002845WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002846weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2847{
2848 struct weston_output *output;
2849
2850 wl_list_for_each(output, &compositor->output_list, link)
2851 weston_output_schedule_repaint(output);
2852}
2853
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002854static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002855surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002856{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002857 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002858}
2859
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002860static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002861surface_attach(struct wl_client *client,
2862 struct wl_resource *resource,
2863 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2864{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002865 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002866 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002867
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002868 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002869 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002870 if (buffer == NULL) {
2871 wl_client_post_no_memory(client);
2872 return;
2873 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002874 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002875
Pekka Paalanende685b82012-12-04 15:58:12 +02002876 /* Attach, attach, without commit in between does not send
2877 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002878 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002879
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002880 surface->pending.sx = sx;
2881 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002882 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002883}
2884
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002885static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002886surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002887 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002888 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002889{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002890 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002891
Derek Foreman57e92ed2015-11-17 14:11:35 -06002892 if (width <= 0 || height <= 0)
2893 return;
2894
Derek Foreman152254b2015-11-26 14:17:48 -06002895 pixman_region32_union_rect(&surface->pending.damage_surface,
2896 &surface->pending.damage_surface,
2897 x, y, width, height);
2898}
2899
2900static void
2901surface_damage_buffer(struct wl_client *client,
2902 struct wl_resource *resource,
2903 int32_t x, int32_t y, int32_t width, int32_t height)
2904{
2905 struct weston_surface *surface = wl_resource_get_user_data(resource);
2906
2907 if (width <= 0 || height <= 0)
2908 return;
2909
2910 pixman_region32_union_rect(&surface->pending.damage_buffer,
2911 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002912 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002913}
2914
Kristian Høgsberg33418202011-08-16 23:01:28 -04002915static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002916destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002917{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002918 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002919
2920 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002921 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002922}
2923
2924static void
2925surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002926 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002927{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002928 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002929 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002930
2931 cb = malloc(sizeof *cb);
2932 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002933 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002934 return;
2935 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002936
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002937 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2938 callback);
2939 if (cb->resource == NULL) {
2940 free(cb);
2941 wl_resource_post_no_memory(resource);
2942 return;
2943 }
2944
Jason Ekstranda85118c2013-06-27 20:17:02 -05002945 wl_resource_set_implementation(cb->resource, NULL, cb,
2946 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002947
Pekka Paalanenbc106382012-10-10 12:49:31 +03002948 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002949}
2950
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002951static void
2952surface_set_opaque_region(struct wl_client *client,
2953 struct wl_resource *resource,
2954 struct wl_resource *region_resource)
2955{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002956 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002957 struct weston_region *region;
2958
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002959 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002960 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002961 pixman_region32_copy(&surface->pending.opaque,
2962 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002963 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002964 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002965 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002966}
2967
2968static void
2969surface_set_input_region(struct wl_client *client,
2970 struct wl_resource *resource,
2971 struct wl_resource *region_resource)
2972{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002973 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002974 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002975
2976 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002977 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002978 pixman_region32_copy(&surface->pending.input,
2979 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002980 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002981 pixman_region32_fini(&surface->pending.input);
2982 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002983 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002984}
2985
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002986/* Cause damage to this sub-surface and all its children.
2987 *
2988 * This is useful when there are state changes that need an implicit
2989 * damage, e.g. a z-order change.
2990 */
2991static void
2992weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2993{
2994 struct weston_subsurface *child;
2995
2996 weston_surface_damage(sub->surface);
2997 sub->reordered = false;
2998
2999 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3000 if (child != sub)
3001 weston_surface_damage_subsurfaces(child);
3002}
3003
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003004static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003005weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003006{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003007 struct weston_subsurface *sub;
3008
3009 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3010 parent_link_pending) {
3011 wl_list_remove(&sub->parent_link);
3012 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003013
3014 if (sub->reordered)
3015 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003016 }
3017}
3018
3019static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003020weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003021 struct weston_matrix *matrix)
3022{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003023 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003024 double src_width, src_height, dest_width, dest_height;
3025
3026 weston_matrix_init(matrix);
3027
3028 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3029 src_width = surface->width_from_buffer;
3030 src_height = surface->height_from_buffer;
3031 } else {
3032 src_width = wl_fixed_to_double(vp->buffer.src_width);
3033 src_height = wl_fixed_to_double(vp->buffer.src_height);
3034 }
3035
3036 if (vp->surface.width == -1) {
3037 dest_width = src_width;
3038 dest_height = src_height;
3039 } else {
3040 dest_width = vp->surface.width;
3041 dest_height = vp->surface.height;
3042 }
3043
3044 if (src_width != dest_width || src_height != dest_height)
3045 weston_matrix_scale(matrix,
3046 src_width / dest_width,
3047 src_height / dest_height, 1);
3048
3049 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3050 weston_matrix_translate(matrix,
3051 wl_fixed_to_double(vp->buffer.src_x),
3052 wl_fixed_to_double(vp->buffer.src_y),
3053 0);
3054
3055 switch (vp->buffer.transform) {
3056 case WL_OUTPUT_TRANSFORM_FLIPPED:
3057 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3058 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3059 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3060 weston_matrix_scale(matrix, -1, 1, 1);
3061 weston_matrix_translate(matrix,
3062 surface->width_from_buffer, 0, 0);
3063 break;
3064 }
3065
3066 switch (vp->buffer.transform) {
3067 default:
3068 case WL_OUTPUT_TRANSFORM_NORMAL:
3069 case WL_OUTPUT_TRANSFORM_FLIPPED:
3070 break;
3071 case WL_OUTPUT_TRANSFORM_90:
3072 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3073 weston_matrix_rotate_xy(matrix, 0, 1);
3074 weston_matrix_translate(matrix,
3075 surface->height_from_buffer, 0, 0);
3076 break;
3077 case WL_OUTPUT_TRANSFORM_180:
3078 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3079 weston_matrix_rotate_xy(matrix, -1, 0);
3080 weston_matrix_translate(matrix,
3081 surface->width_from_buffer,
3082 surface->height_from_buffer, 0);
3083 break;
3084 case WL_OUTPUT_TRANSFORM_270:
3085 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3086 weston_matrix_rotate_xy(matrix, 0, -1);
3087 weston_matrix_translate(matrix,
3088 0, surface->width_from_buffer, 0);
3089 break;
3090 }
3091
3092 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3093}
3094
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003095/**
3096 * Compute a + b > c while being safe to overflows.
3097 */
3098static bool
3099fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3100{
3101 return (int64_t)a + (int64_t)b > (int64_t)c;
3102}
3103
3104static bool
3105weston_surface_is_pending_viewport_source_valid(
3106 const struct weston_surface *surface)
3107{
3108 const struct weston_surface_state *pend = &surface->pending;
3109 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3110 int width_from_buffer = 0;
3111 int height_from_buffer = 0;
3112 wl_fixed_t w;
3113 wl_fixed_t h;
3114
3115 /* If viewport source rect is not set, it is always ok. */
3116 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3117 return true;
3118
3119 if (pend->newly_attached) {
3120 if (pend->buffer) {
3121 convert_size_by_transform_scale(&width_from_buffer,
3122 &height_from_buffer,
3123 pend->buffer->width,
3124 pend->buffer->height,
3125 vp->buffer.transform,
3126 vp->buffer.scale);
3127 } else {
3128 /* No buffer: viewport is irrelevant. */
3129 return true;
3130 }
3131 } else {
3132 width_from_buffer = surface->width_from_buffer;
3133 height_from_buffer = surface->height_from_buffer;
3134 }
3135
3136 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3137 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3138
3139 /* No buffer: viewport is irrelevant. */
3140 if (width_from_buffer == 0 || height_from_buffer == 0)
3141 return true;
3142
3143 /* overflow checks for wl_fixed_from_int() */
3144 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3145 return false;
3146 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3147 return false;
3148
3149 w = wl_fixed_from_int(width_from_buffer);
3150 h = wl_fixed_from_int(height_from_buffer);
3151
3152 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3153 return false;
3154 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3155 return false;
3156
3157 return true;
3158}
3159
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003160static bool
3161fixed_is_integer(wl_fixed_t v)
3162{
3163 return (v & 0xff) == 0;
3164}
3165
3166static bool
3167weston_surface_is_pending_viewport_dst_size_int(
3168 const struct weston_surface *surface)
3169{
3170 const struct weston_buffer_viewport *vp =
3171 &surface->pending.buffer_viewport;
3172
3173 if (vp->surface.width != -1) {
3174 assert(vp->surface.width > 0 && vp->surface.height > 0);
3175 return true;
3176 }
3177
3178 return fixed_is_integer(vp->buffer.src_width) &&
3179 fixed_is_integer(vp->buffer.src_height);
3180}
3181
Derek Foreman152254b2015-11-26 14:17:48 -06003182/* Translate pending damage in buffer co-ordinates to surface
3183 * co-ordinates and union it with a pixman_region32_t.
3184 * This should only be called after the buffer is attached.
3185 */
3186static void
3187apply_damage_buffer(pixman_region32_t *dest,
3188 struct weston_surface *surface,
3189 struct weston_surface_state *state)
3190{
3191 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3192
3193 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3194 * translated into surface co-ordinates and unioned with
3195 * any other surface damage.
3196 * None of this makes sense if there is no buffer though.
3197 */
3198 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3199 pixman_region32_t buffer_damage;
3200
3201 pixman_region32_intersect_rect(&state->damage_buffer,
3202 &state->damage_buffer,
3203 0, 0, buffer->width,
3204 buffer->height);
3205 pixman_region32_init(&buffer_damage);
3206 weston_matrix_transform_region(&buffer_damage,
3207 &surface->buffer_to_surface_matrix,
3208 &state->damage_buffer);
3209 pixman_region32_union(dest, dest, &buffer_damage);
3210 pixman_region32_fini(&buffer_damage);
3211 }
3212 /* We should clear this on commit even if there was no buffer */
3213 pixman_region32_clear(&state->damage_buffer);
3214}
3215
Jason Ekstrand1e059042014-10-16 10:55:19 -05003216static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003217weston_surface_commit_state(struct weston_surface *surface,
3218 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003219{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003220 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003221 pixman_region32_t opaque;
3222
Alexander Larsson4ea95522013-05-22 14:41:37 +02003223 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003224 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003225 /* wp_viewport.set_source */
3226 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003227 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003228
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003229 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003230 if (state->newly_attached) {
3231 /* zwp_surface_synchronization_v1.set_acquire_fence */
3232 fd_move(&surface->acquire_fence_fd,
3233 &state->acquire_fence_fd);
3234
Jason Ekstrand7b982072014-05-20 14:33:03 -05003235 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003236 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003237 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003238 assert(state->acquire_fence_fd == -1);
Giulio Camuffo184df502013-02-21 11:29:21 +01003239
Jason Ekstrand1e059042014-10-16 10:55:19 -05003240 weston_surface_build_buffer_matrix(surface,
3241 &surface->surface_to_buffer_matrix);
3242 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3243 &surface->surface_to_buffer_matrix);
3244
Jason Ekstrand7b982072014-05-20 14:33:03 -05003245 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003246 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003247 if (surface->committed)
3248 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003249 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003250
Jason Ekstrand7b982072014-05-20 14:33:03 -05003251 state->sx = 0;
3252 state->sy = 0;
3253 state->newly_attached = 0;
3254 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003255
Derek Foreman152254b2015-11-26 14:17:48 -06003256 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003257 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003258 (pixman_region32_not_empty(&state->damage_surface) ||
3259 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003260 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003261
Pekka Paalanen8e159182012-10-10 12:49:25 +03003262 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003263 &state->damage_surface);
3264
3265 apply_damage_buffer(&surface->damage, surface, state);
3266
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003267 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003268 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003269 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003270
3271 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003272 pixman_region32_init(&opaque);
3273 pixman_region32_intersect_rect(&opaque, &state->opaque,
3274 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003275
3276 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3277 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003278 wl_list_for_each(view, &surface->views, surface_link)
3279 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003280 }
3281
3282 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003283
3284 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003285 pixman_region32_intersect_rect(&surface->input, &state->input,
3286 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003287
Pekka Paalanenbc106382012-10-10 12:49:31 +03003288 /* wl_surface.frame */
3289 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003290 &state->frame_callback_list);
3291 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003292
3293 /* XXX:
3294 * What should happen with a feedback request, if there
3295 * is no wl_buffer attached for this commit?
3296 */
3297
3298 /* presentation.feedback */
3299 wl_list_insert_list(&surface->feedback_list,
3300 &state->feedback_list);
3301 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003302
3303 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003304}
3305
3306static void
3307weston_surface_commit(struct weston_surface *surface)
3308{
3309 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003310
Pekka Paalanene67858b2013-04-25 13:57:42 +03003311 weston_surface_commit_subsurface_order(surface);
3312
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003313 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003314}
3315
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003316static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003317weston_subsurface_commit(struct weston_subsurface *sub);
3318
3319static void
3320weston_subsurface_parent_commit(struct weston_subsurface *sub,
3321 int parent_is_synchronized);
3322
3323static void
3324surface_commit(struct wl_client *client, struct wl_resource *resource)
3325{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003326 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003327 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3328
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003329 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3330 assert(surface->viewport_resource);
3331
3332 wl_resource_post_error(surface->viewport_resource,
3333 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3334 "wl_surface@%d has viewport source outside buffer",
3335 wl_resource_get_id(resource));
3336 return;
3337 }
3338
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003339 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3340 assert(surface->viewport_resource);
3341
3342 wl_resource_post_error(surface->viewport_resource,
3343 WP_VIEWPORT_ERROR_BAD_SIZE,
3344 "wl_surface@%d viewport dst size not integer",
3345 wl_resource_get_id(resource));
3346 return;
3347 }
3348
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003349 if (surface->pending.acquire_fence_fd >= 0) {
3350 assert(surface->synchronization_resource);
3351
3352 if (!surface->pending.buffer) {
3353 fd_clear(&surface->pending.acquire_fence_fd);
3354 wl_resource_post_error(surface->synchronization_resource,
3355 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3356 "wl_surface@%"PRIu32" no buffer for synchronization",
3357 wl_resource_get_id(resource));
3358 return;
3359 }
3360
3361 /* We support fences for both wp_linux_dmabuf and opaque EGL
3362 * buffers, as mandated by minor version 2 of the
3363 * zwp_linux_explicit_synchronization_v1 protocol. Since
3364 * renderers that support fences currently only support these
3365 * two buffer types plus SHM buffers, we can just check for the
3366 * SHM buffer case here.
3367 */
3368 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3369 fd_clear(&surface->pending.acquire_fence_fd);
3370 wl_resource_post_error(surface->synchronization_resource,
3371 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3372 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3373 wl_resource_get_id(resource));
3374 return;
3375 }
3376 }
3377
Pekka Paalanene67858b2013-04-25 13:57:42 +03003378 if (sub) {
3379 weston_subsurface_commit(sub);
3380 return;
3381 }
3382
3383 weston_surface_commit(surface);
3384
3385 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3386 if (sub->surface != surface)
3387 weston_subsurface_parent_commit(sub, 0);
3388 }
3389}
3390
3391static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003392surface_set_buffer_transform(struct wl_client *client,
3393 struct wl_resource *resource, int transform)
3394{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003395 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003396
Jonny Lamba55f1392014-05-30 12:07:15 +02003397 /* if wl_output.transform grows more members this will need to be updated. */
3398 if (transform < 0 ||
3399 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3400 wl_resource_post_error(resource,
3401 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3402 "buffer transform must be a valid transform "
3403 "('%d' specified)", transform);
3404 return;
3405 }
3406
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003407 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003408 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003409}
3410
Alexander Larsson4ea95522013-05-22 14:41:37 +02003411static void
3412surface_set_buffer_scale(struct wl_client *client,
3413 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003414 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003415{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003416 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003417
Jonny Lamba55f1392014-05-30 12:07:15 +02003418 if (scale < 1) {
3419 wl_resource_post_error(resource,
3420 WL_SURFACE_ERROR_INVALID_SCALE,
3421 "buffer scale must be at least one "
3422 "('%d' specified)", scale);
3423 return;
3424 }
3425
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003426 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003427 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003428}
3429
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003430static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003431 surface_destroy,
3432 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003433 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003434 surface_frame,
3435 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003436 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003437 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003438 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003439 surface_set_buffer_scale,
3440 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003441};
3442
3443static void
3444compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003445 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003446{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003447 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003448 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003449
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003450 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003451 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003452 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003453 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003454 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003455
Jason Ekstranda85118c2013-06-27 20:17:02 -05003456 surface->resource =
3457 wl_resource_create(client, &wl_surface_interface,
3458 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003459 if (surface->resource == NULL) {
3460 weston_surface_destroy(surface);
3461 wl_resource_post_no_memory(resource);
3462 return;
3463 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003464 wl_resource_set_implementation(surface->resource, &surface_interface,
3465 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003466
3467 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003468}
3469
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003470static void
3471destroy_region(struct wl_resource *resource)
3472{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003473 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003474
3475 pixman_region32_fini(&region->region);
3476 free(region);
3477}
3478
3479static void
3480region_destroy(struct wl_client *client, struct wl_resource *resource)
3481{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003482 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003483}
3484
3485static void
3486region_add(struct wl_client *client, struct wl_resource *resource,
3487 int32_t x, int32_t y, int32_t width, int32_t height)
3488{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003489 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003490
3491 pixman_region32_union_rect(&region->region, &region->region,
3492 x, y, width, height);
3493}
3494
3495static void
3496region_subtract(struct wl_client *client, struct wl_resource *resource,
3497 int32_t x, int32_t y, int32_t width, int32_t height)
3498{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003499 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003500 pixman_region32_t rect;
3501
3502 pixman_region32_init_rect(&rect, x, y, width, height);
3503 pixman_region32_subtract(&region->region, &region->region, &rect);
3504 pixman_region32_fini(&rect);
3505}
3506
3507static const struct wl_region_interface region_interface = {
3508 region_destroy,
3509 region_add,
3510 region_subtract
3511};
3512
3513static void
3514compositor_create_region(struct wl_client *client,
3515 struct wl_resource *resource, uint32_t id)
3516{
3517 struct weston_region *region;
3518
3519 region = malloc(sizeof *region);
3520 if (region == NULL) {
3521 wl_resource_post_no_memory(resource);
3522 return;
3523 }
3524
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003525 pixman_region32_init(&region->region);
3526
Jason Ekstranda85118c2013-06-27 20:17:02 -05003527 region->resource =
3528 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003529 if (region->resource == NULL) {
3530 free(region);
3531 wl_resource_post_no_memory(resource);
3532 return;
3533 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003534 wl_resource_set_implementation(region->resource, &region_interface,
3535 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003536}
3537
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003538static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003539 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003540 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003541};
3542
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003543static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003544weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3545{
3546 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003547
Jason Ekstrand7b982072014-05-20 14:33:03 -05003548 weston_surface_commit_state(surface, &sub->cached);
3549 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003550
3551 weston_surface_commit_subsurface_order(surface);
3552
3553 weston_surface_schedule_repaint(surface);
3554
Jason Ekstrand7b982072014-05-20 14:33:03 -05003555 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003556}
3557
3558static void
3559weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3560{
3561 struct weston_surface *surface = sub->surface;
3562
3563 /*
3564 * If this commit would cause the surface to move by the
3565 * attach(dx, dy) parameters, the old damage region must be
3566 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003567 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003568 */
Derek Foreman152254b2015-11-26 14:17:48 -06003569 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003570 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003571 pixman_region32_union(&sub->cached.damage_surface,
3572 &sub->cached.damage_surface,
3573 &surface->pending.damage_surface);
3574 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003575
3576 if (surface->pending.newly_attached) {
3577 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003578 weston_surface_state_set_buffer(&sub->cached,
3579 surface->pending.buffer);
3580 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003581 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003582 weston_presentation_feedback_discard_list(
3583 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003584 /* zwp_surface_synchronization_v1.set_acquire_fence */
3585 fd_move(&sub->cached.acquire_fence_fd,
3586 &surface->pending.acquire_fence_fd);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003587 }
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003588 assert(surface->pending.acquire_fence_fd == -1);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003589 sub->cached.sx += surface->pending.sx;
3590 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003591
Derek Foreman152254b2015-11-26 14:17:48 -06003592 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3593
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003594 sub->cached.buffer_viewport.changed |=
3595 surface->pending.buffer_viewport.changed;
3596 sub->cached.buffer_viewport.buffer =
3597 surface->pending.buffer_viewport.buffer;
3598 sub->cached.buffer_viewport.surface =
3599 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003600
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003601 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003602
3603 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3604
3605 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3606
3607 wl_list_insert_list(&sub->cached.frame_callback_list,
3608 &surface->pending.frame_callback_list);
3609 wl_list_init(&surface->pending.frame_callback_list);
3610
Pekka Paalanen133e4392014-09-23 22:08:46 -04003611 wl_list_insert_list(&sub->cached.feedback_list,
3612 &surface->pending.feedback_list);
3613 wl_list_init(&surface->pending.feedback_list);
3614
Jason Ekstrand7b982072014-05-20 14:33:03 -05003615 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003616}
3617
Derek Foreman280e7dd2014-10-03 13:13:42 -05003618static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003619weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3620{
3621 while (sub) {
3622 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003623 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003624
3625 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003626 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627
3628 sub = weston_surface_to_subsurface(sub->parent);
3629 }
3630
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003631 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003632}
3633
3634static void
3635weston_subsurface_commit(struct weston_subsurface *sub)
3636{
3637 struct weston_surface *surface = sub->surface;
3638 struct weston_subsurface *tmp;
3639
3640 /* Recursive check for effectively synchronized. */
3641 if (weston_subsurface_is_synchronized(sub)) {
3642 weston_subsurface_commit_to_cache(sub);
3643 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003644 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003645 /* flush accumulated state from cache */
3646 weston_subsurface_commit_to_cache(sub);
3647 weston_subsurface_commit_from_cache(sub);
3648 } else {
3649 weston_surface_commit(surface);
3650 }
3651
3652 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3653 if (tmp->surface != surface)
3654 weston_subsurface_parent_commit(tmp, 0);
3655 }
3656 }
3657}
3658
3659static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003660weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003661{
3662 struct weston_surface *surface = sub->surface;
3663 struct weston_subsurface *tmp;
3664
Pekka Paalanene67858b2013-04-25 13:57:42 +03003665 /* From now on, commit_from_cache the whole sub-tree, regardless of
3666 * the synchronized mode of each child. This sub-surface or some
3667 * of its ancestors were synchronized, so we are synchronized
3668 * all the way down.
3669 */
3670
Jason Ekstrand7b982072014-05-20 14:33:03 -05003671 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003672 weston_subsurface_commit_from_cache(sub);
3673
3674 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3675 if (tmp->surface != surface)
3676 weston_subsurface_parent_commit(tmp, 1);
3677 }
3678}
3679
3680static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003681weston_subsurface_parent_commit(struct weston_subsurface *sub,
3682 int parent_is_synchronized)
3683{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003684 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003685 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003686 wl_list_for_each(view, &sub->surface->views, surface_link)
3687 weston_view_set_position(view,
3688 sub->position.x,
3689 sub->position.y);
3690
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003691 sub->position.set = 0;
3692 }
3693
3694 if (parent_is_synchronized || sub->synchronized)
3695 weston_subsurface_synchronized_commit(sub);
3696}
3697
Pekka Paalanen8274d902014-08-06 19:36:51 +03003698static int
3699subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3700{
3701 return snprintf(buf, len, "sub-surface");
3702}
3703
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003704static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003705subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003706{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003707 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003708
Jason Ekstranda7af7042013-10-12 22:38:11 -05003709 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003710 weston_view_set_position(view,
3711 view->geometry.x + dx,
3712 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003713
3714 /* No need to check parent mappedness, because if parent is not
3715 * mapped, parent is not in a visible layer, so this sub-surface
3716 * will not be drawn either.
3717 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003718
Pekka Paalanene67858b2013-04-25 13:57:42 +03003719 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003720 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003721
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003722 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003723 * because that would call it also for the parent surface,
3724 * which might not be mapped yet. That would lead to
3725 * inconsistent state, where the window could never be
3726 * mapped.
3727 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003728 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003729 * weston_surface_is_mapped() return true, so that when the
3730 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003731 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003732 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003733 }
3734}
3735
3736static struct weston_subsurface *
3737weston_surface_to_subsurface(struct weston_surface *surface)
3738{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003739 if (surface->committed == subsurface_committed)
3740 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003741
3742 return NULL;
3743}
3744
Pekka Paalanen01388e22013-04-25 13:57:44 +03003745WL_EXPORT struct weston_surface *
3746weston_surface_get_main_surface(struct weston_surface *surface)
3747{
3748 struct weston_subsurface *sub;
3749
3750 while (surface && (sub = weston_surface_to_subsurface(surface)))
3751 surface = sub->parent;
3752
3753 return surface;
3754}
3755
Pekka Paalanen50b67472014-10-01 15:02:41 +03003756WL_EXPORT int
3757weston_surface_set_role(struct weston_surface *surface,
3758 const char *role_name,
3759 struct wl_resource *error_resource,
3760 uint32_t error_code)
3761{
3762 assert(role_name);
3763
3764 if (surface->role_name == NULL ||
3765 surface->role_name == role_name ||
3766 strcmp(surface->role_name, role_name) == 0) {
3767 surface->role_name = role_name;
3768
3769 return 0;
3770 }
3771
3772 wl_resource_post_error(error_resource, error_code,
3773 "Cannot assign role %s to wl_surface@%d,"
3774 " already has role %s\n",
3775 role_name,
3776 wl_resource_get_id(surface->resource),
3777 surface->role_name);
3778 return -1;
3779}
3780
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003781WL_EXPORT const char *
3782weston_surface_get_role(struct weston_surface *surface)
3783{
3784 return surface->role_name;
3785}
3786
Pekka Paalanen8274d902014-08-06 19:36:51 +03003787WL_EXPORT void
3788weston_surface_set_label_func(struct weston_surface *surface,
3789 int (*desc)(struct weston_surface *,
3790 char *, size_t))
3791{
3792 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003793 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003794}
3795
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003796/** Get the size of surface contents
3797 *
3798 * \param surface The surface to query.
3799 * \param width Returns the width of raw contents.
3800 * \param height Returns the height of raw contents.
3801 *
3802 * Retrieves the raw surface content size in pixels for the given surface.
3803 * This is the whole content size in buffer pixels. If the surface
3804 * has no content or the renderer does not implement this feature,
3805 * zeroes are returned.
3806 *
3807 * This function is used to determine the buffer size needed for
3808 * a weston_surface_copy_content() call.
3809 */
3810WL_EXPORT void
3811weston_surface_get_content_size(struct weston_surface *surface,
3812 int *width, int *height)
3813{
3814 struct weston_renderer *rer = surface->compositor->renderer;
3815
3816 if (!rer->surface_get_content_size) {
3817 *width = 0;
3818 *height = 0;
3819 return;
3820 }
3821
3822 rer->surface_get_content_size(surface, width, height);
3823}
3824
Quentin Glidic248dd102016-08-12 10:41:34 +02003825/** Get the bounding box of a surface and its subsurfaces
3826 *
3827 * \param surface The surface to query.
3828 * \return The bounding box relative to the surface origin.
3829 *
3830 */
3831WL_EXPORT struct weston_geometry
3832weston_surface_get_bounding_box(struct weston_surface *surface)
3833{
3834 pixman_region32_t region;
3835 pixman_box32_t *box;
3836 struct weston_subsurface *subsurface;
3837
3838 pixman_region32_init_rect(&region,
3839 0, 0,
3840 surface->width, surface->height);
3841
3842 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3843 pixman_region32_union_rect(&region, &region,
3844 subsurface->position.x,
3845 subsurface->position.y,
3846 subsurface->surface->width,
3847 subsurface->surface->height);
3848
3849 box = pixman_region32_extents(&region);
3850 struct weston_geometry geometry = {
3851 .x = box->x1,
3852 .y = box->y1,
3853 .width = box->x2 - box->x1,
3854 .height = box->y2 - box->y1,
3855 };
3856
3857 pixman_region32_fini(&region);
3858
3859 return geometry;
3860}
3861
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003862/** Copy surface contents to system memory.
3863 *
3864 * \param surface The surface to copy from.
3865 * \param target Pointer to the target memory buffer.
3866 * \param size Size of the target buffer in bytes.
3867 * \param src_x X location on contents to copy from.
3868 * \param src_y Y location on contents to copy from.
3869 * \param width Width in pixels of the area to copy.
3870 * \param height Height in pixels of the area to copy.
3871 * \return 0 for success, -1 for failure.
3872 *
3873 * Surface contents are maintained by the renderer. They can be in a
3874 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3875 * else.
3876 *
3877 * Surface contents are copied into memory pointed to by target,
3878 * which has size bytes of space available. The target memory
3879 * may be larger than needed, but being smaller returns an error.
3880 * The extra bytes in target may or may not be written; their content is
3881 * unspecified. Size must be large enough to hold the image.
3882 *
3883 * The image in the target memory will be arranged in rows from
3884 * top to bottom, and pixels on a row from left to right. The pixel
3885 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3886 * width * 4.
3887 *
3888 * Parameters src_x and src_y define the upper-left corner in buffer
3889 * coordinates (pixels) to copy from. Parameters width and height
3890 * define the size of the area to copy in pixels.
3891 *
3892 * The rectangle defined by src_x, src_y, width, height must fit in
3893 * the surface contents. Otherwise an error is returned.
3894 *
Changwoo Chof97d2502017-08-05 00:30:47 +09003895 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003896 * needed target buffer size and rectangle limits.
3897 *
3898 * CURRENT IMPLEMENTATION RESTRICTIONS:
3899 * - the machine must be little-endian due to Pixman formats.
3900 *
3901 * NOTE: Pixman formats are premultiplied.
3902 */
3903WL_EXPORT int
3904weston_surface_copy_content(struct weston_surface *surface,
3905 void *target, size_t size,
3906 int src_x, int src_y,
3907 int width, int height)
3908{
3909 struct weston_renderer *rer = surface->compositor->renderer;
3910 int cw, ch;
3911 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3912
3913 if (!rer->surface_copy_content)
3914 return -1;
3915
3916 weston_surface_get_content_size(surface, &cw, &ch);
3917
3918 if (src_x < 0 || src_y < 0)
3919 return -1;
3920
3921 if (width <= 0 || height <= 0)
3922 return -1;
3923
3924 if (src_x + width > cw || src_y + height > ch)
3925 return -1;
3926
3927 if (width * bytespp * height > size)
3928 return -1;
3929
3930 return rer->surface_copy_content(surface, target, size,
3931 src_x, src_y, width, height);
3932}
3933
Pekka Paalanene67858b2013-04-25 13:57:42 +03003934static void
3935subsurface_set_position(struct wl_client *client,
3936 struct wl_resource *resource, int32_t x, int32_t y)
3937{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003938 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003939
3940 if (!sub)
3941 return;
3942
3943 sub->position.x = x;
3944 sub->position.y = y;
3945 sub->position.set = 1;
3946}
3947
3948static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003949subsurface_find_sibling(struct weston_subsurface *sub,
3950 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003951{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003952 struct weston_surface *parent = sub->parent;
3953 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003954
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003955 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3956 if (sibling->surface == surface && sibling != sub)
3957 return sibling;
3958 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003959
3960 return NULL;
3961}
3962
3963static struct weston_subsurface *
3964subsurface_sibling_check(struct weston_subsurface *sub,
3965 struct weston_surface *surface,
3966 const char *request)
3967{
3968 struct weston_subsurface *sibling;
3969
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003970 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003971 if (!sibling) {
3972 wl_resource_post_error(sub->resource,
3973 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3974 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003975 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003976 return NULL;
3977 }
3978
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003979 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003980
3981 return sibling;
3982}
3983
3984static void
3985subsurface_place_above(struct wl_client *client,
3986 struct wl_resource *resource,
3987 struct wl_resource *sibling_resource)
3988{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003989 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003990 struct weston_surface *surface =
3991 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003992 struct weston_subsurface *sibling;
3993
3994 if (!sub)
3995 return;
3996
3997 sibling = subsurface_sibling_check(sub, surface, "place_above");
3998 if (!sibling)
3999 return;
4000
4001 wl_list_remove(&sub->parent_link_pending);
4002 wl_list_insert(sibling->parent_link_pending.prev,
4003 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004004
4005 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004006}
4007
4008static void
4009subsurface_place_below(struct wl_client *client,
4010 struct wl_resource *resource,
4011 struct wl_resource *sibling_resource)
4012{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004013 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004014 struct weston_surface *surface =
4015 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004016 struct weston_subsurface *sibling;
4017
4018 if (!sub)
4019 return;
4020
4021 sibling = subsurface_sibling_check(sub, surface, "place_below");
4022 if (!sibling)
4023 return;
4024
4025 wl_list_remove(&sub->parent_link_pending);
4026 wl_list_insert(&sibling->parent_link_pending,
4027 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004028
4029 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004030}
4031
4032static void
4033subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4034{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004035 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004036
4037 if (sub)
4038 sub->synchronized = 1;
4039}
4040
4041static void
4042subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4043{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004044 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004045
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004046 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004047 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004048
4049 /* If sub became effectively desynchronized, flush. */
4050 if (!weston_subsurface_is_synchronized(sub))
4051 weston_subsurface_synchronized_commit(sub);
4052 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004053}
4054
4055static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004056weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4057{
4058 wl_list_remove(&sub->parent_link);
4059 wl_list_remove(&sub->parent_link_pending);
4060 wl_list_remove(&sub->parent_destroy_listener.link);
4061 sub->parent = NULL;
4062}
4063
4064static void
4065weston_subsurface_destroy(struct weston_subsurface *sub);
4066
4067static void
4068subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4069{
4070 struct weston_subsurface *sub =
4071 container_of(listener, struct weston_subsurface,
4072 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004073 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004074
4075 /* The protocol object (wl_resource) is left inert. */
4076 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004077 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004078
4079 weston_subsurface_destroy(sub);
4080}
4081
4082static void
4083subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4084{
4085 struct weston_subsurface *sub =
4086 container_of(listener, struct weston_subsurface,
4087 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004088 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004089 assert(sub->surface != sub->parent);
4090
4091 if (weston_surface_is_mapped(sub->surface))
4092 weston_surface_unmap(sub->surface);
4093
4094 weston_subsurface_unlink_parent(sub);
4095}
4096
4097static void
4098subsurface_resource_destroy(struct wl_resource *resource)
4099{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004100 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004101
4102 if (sub)
4103 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004104}
4105
4106static void
4107subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4108{
4109 wl_resource_destroy(resource);
4110}
4111
4112static void
4113weston_subsurface_link_parent(struct weston_subsurface *sub,
4114 struct weston_surface *parent)
4115{
4116 sub->parent = parent;
4117 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004118 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004119 &sub->parent_destroy_listener);
4120
4121 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4122 wl_list_insert(&parent->subsurface_list_pending,
4123 &sub->parent_link_pending);
4124}
4125
4126static void
4127weston_subsurface_link_surface(struct weston_subsurface *sub,
4128 struct weston_surface *surface)
4129{
4130 sub->surface = surface;
4131 sub->surface_destroy_listener.notify =
4132 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004133 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004134 &sub->surface_destroy_listener);
4135}
4136
4137static void
4138weston_subsurface_destroy(struct weston_subsurface *sub)
4139{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004140 struct weston_view *view, *next;
4141
Pekka Paalanene67858b2013-04-25 13:57:42 +03004142 assert(sub->surface);
4143
4144 if (sub->resource) {
4145 assert(weston_surface_to_subsurface(sub->surface) == sub);
4146 assert(sub->parent_destroy_listener.notify ==
4147 subsurface_handle_parent_destroy);
4148
George Kiagiadakised04d382014-06-13 18:10:26 +02004149 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4150 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004151 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004152 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004153
Pekka Paalanene67858b2013-04-25 13:57:42 +03004154 if (sub->parent)
4155 weston_subsurface_unlink_parent(sub);
4156
Jason Ekstrand7b982072014-05-20 14:33:03 -05004157 weston_surface_state_fini(&sub->cached);
4158 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004159
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004160 sub->surface->committed = NULL;
4161 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004162 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004163 } else {
4164 /* the dummy weston_subsurface for the parent itself */
4165 assert(sub->parent_destroy_listener.notify == NULL);
4166 wl_list_remove(&sub->parent_link);
4167 wl_list_remove(&sub->parent_link_pending);
4168 }
4169
4170 wl_list_remove(&sub->surface_destroy_listener.link);
4171 free(sub);
4172}
4173
4174static const struct wl_subsurface_interface subsurface_implementation = {
4175 subsurface_destroy,
4176 subsurface_set_position,
4177 subsurface_place_above,
4178 subsurface_place_below,
4179 subsurface_set_sync,
4180 subsurface_set_desync
4181};
4182
4183static struct weston_subsurface *
4184weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4185 struct weston_surface *parent)
4186{
4187 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004188 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004189
Bryce Harringtonde16d892014-11-20 22:21:57 -08004190 sub = zalloc(sizeof *sub);
4191 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004192 return NULL;
4193
Jason Ekstranda7af7042013-10-12 22:38:11 -05004194 wl_list_init(&sub->unused_views);
4195
Jason Ekstranda85118c2013-06-27 20:17:02 -05004196 sub->resource =
4197 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004198 if (!sub->resource) {
4199 free(sub);
4200 return NULL;
4201 }
4202
Jason Ekstranda85118c2013-06-27 20:17:02 -05004203 wl_resource_set_implementation(sub->resource,
4204 &subsurface_implementation,
4205 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004206 weston_subsurface_link_surface(sub, surface);
4207 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004208 weston_surface_state_init(&sub->cached);
4209 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004210 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004211
4212 return sub;
4213}
4214
4215/* Create a dummy subsurface for having the parent itself in its
4216 * sub-surface lists. Makes stacking order manipulation easy.
4217 */
4218static struct weston_subsurface *
4219weston_subsurface_create_for_parent(struct weston_surface *parent)
4220{
4221 struct weston_subsurface *sub;
4222
Bryce Harringtonde16d892014-11-20 22:21:57 -08004223 sub = zalloc(sizeof *sub);
4224 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004225 return NULL;
4226
4227 weston_subsurface_link_surface(sub, parent);
4228 sub->parent = parent;
4229 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4230 wl_list_insert(&parent->subsurface_list_pending,
4231 &sub->parent_link_pending);
4232
4233 return sub;
4234}
4235
4236static void
4237subcompositor_get_subsurface(struct wl_client *client,
4238 struct wl_resource *resource,
4239 uint32_t id,
4240 struct wl_resource *surface_resource,
4241 struct wl_resource *parent_resource)
4242{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004243 struct weston_surface *surface =
4244 wl_resource_get_user_data(surface_resource);
4245 struct weston_surface *parent =
4246 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004247 struct weston_subsurface *sub;
4248 static const char where[] = "get_subsurface: wl_subsurface@";
4249
4250 if (surface == parent) {
4251 wl_resource_post_error(resource,
4252 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4253 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004254 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004255 return;
4256 }
4257
4258 if (weston_surface_to_subsurface(surface)) {
4259 wl_resource_post_error(resource,
4260 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4261 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004262 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004263 return;
4264 }
4265
Pekka Paalanen50b67472014-10-01 15:02:41 +03004266 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4267 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004268 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004269
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004270 if (weston_surface_get_main_surface(parent) == surface) {
4271 wl_resource_post_error(resource,
4272 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4273 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004274 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004275 return;
4276 }
4277
Pekka Paalanene67858b2013-04-25 13:57:42 +03004278 /* make sure the parent is in its own list */
4279 if (wl_list_empty(&parent->subsurface_list)) {
4280 if (!weston_subsurface_create_for_parent(parent)) {
4281 wl_resource_post_no_memory(resource);
4282 return;
4283 }
4284 }
4285
4286 sub = weston_subsurface_create(id, surface, parent);
4287 if (!sub) {
4288 wl_resource_post_no_memory(resource);
4289 return;
4290 }
4291
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004292 surface->committed = subsurface_committed;
4293 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004294 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004295}
4296
4297static void
4298subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4299{
4300 wl_resource_destroy(resource);
4301}
4302
4303static const struct wl_subcompositor_interface subcompositor_interface = {
4304 subcompositor_destroy,
4305 subcompositor_get_subsurface
4306};
4307
4308static void
4309bind_subcompositor(struct wl_client *client,
4310 void *data, uint32_t version, uint32_t id)
4311{
4312 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004313 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004314
Jason Ekstranda85118c2013-06-27 20:17:02 -05004315 resource =
4316 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004317 if (resource == NULL) {
4318 wl_client_post_no_memory(client);
4319 return;
4320 }
4321 wl_resource_set_implementation(resource, &subcompositor_interface,
4322 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004323}
4324
Bryce Harrington0795ece2016-08-30 12:04:26 -07004325/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004326 *
4327 * \param compositor The compositor instance
4328 * \param state The DPMS state the outputs will be set to
4329 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004330static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004331weston_compositor_dpms(struct weston_compositor *compositor,
4332 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004333{
4334 struct weston_output *output;
4335
Bryce Harrington08976ac2016-08-30 12:05:16 -07004336 wl_list_for_each(output, &compositor->output_list, link)
4337 if (output->set_dpms)
4338 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004339}
4340
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004341/** Restores the compositor to active status
4342 *
4343 * \param compositor The compositor instance
4344 *
4345 * If the compositor was in a sleeping mode, all outputs are powered
4346 * back on via DPMS. Otherwise if the compositor was inactive
4347 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4348 * signal will fire.
4349 *
4350 * Restarts the idle timer.
4351 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004352WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004353weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004354{
Neil Roberts8b62e202013-09-30 13:14:47 +01004355 uint32_t old_state = compositor->state;
4356
4357 /* The state needs to be changed before emitting the wake
4358 * signal because that may try to schedule a repaint which
4359 * will not work if the compositor is still sleeping */
4360 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4361
4362 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004363 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004364 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004365 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004366 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004367 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004368 /* fall through */
4369 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004370 wl_event_source_timer_update(compositor->idle_source,
4371 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004372 }
4373}
4374
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004375/** Turns off rendering and frame events for the compositor.
4376 *
4377 * \param compositor The compositor instance
4378 *
4379 * This is used for example to prevent further rendering while the
4380 * compositor is shutting down.
4381 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004382 * Stops the idle timer.
4383 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004384WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004385weston_compositor_offscreen(struct weston_compositor *compositor)
4386{
4387 switch (compositor->state) {
4388 case WESTON_COMPOSITOR_OFFSCREEN:
4389 return;
4390 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004391 default:
4392 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4393 wl_event_source_timer_update(compositor->idle_source, 0);
4394 }
4395}
4396
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004397/** Powers down all attached output devices
4398 *
4399 * \param compositor The compositor instance
4400 *
4401 * Causes rendering to the outputs to cease, and no frame events to be
4402 * sent. Only powers down the outputs if the compositor is not already
4403 * in sleep mode.
4404 *
4405 * Stops the idle timer.
4406 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004407WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004408weston_compositor_sleep(struct weston_compositor *compositor)
4409{
4410 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4411 return;
4412
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004413 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004414 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4415 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4416}
4417
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004418/** Sets compositor to idle mode
4419 *
4420 * \param data The compositor instance
4421 *
4422 * This is called when the idle timer fires. Once the compositor is in
4423 * idle mode it requires a wake action (e.g. via
4424 * weston_compositor_wake()) to restore it. The compositor's
4425 * idle_signal will be triggered when the idle event occurs.
4426 *
4427 * Idleness can be inhibited by setting the compositor's idle_inhibit
4428 * property.
4429 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004430static int
4431idle_handler(void *data)
4432{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004433 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004434
4435 if (compositor->idle_inhibit)
4436 return 1;
4437
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004438 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004439 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004440
4441 return 1;
4442}
4443
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004444WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004445weston_plane_init(struct weston_plane *plane,
4446 struct weston_compositor *ec,
4447 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004448{
4449 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004450 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004451 plane->x = x;
4452 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004453 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004454
4455 /* Init the link so that the call to wl_list_remove() when releasing
4456 * the plane without ever stacking doesn't lead to a crash */
4457 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004458}
4459
4460WL_EXPORT void
4461weston_plane_release(struct weston_plane *plane)
4462{
Xiong Zhang97116532013-10-23 13:58:31 +08004463 struct weston_view *view;
4464
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004465 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004466 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004467
Xiong Zhang97116532013-10-23 13:58:31 +08004468 wl_list_for_each(view, &plane->compositor->view_list, link) {
4469 if (view->plane == plane)
4470 view->plane = NULL;
4471 }
4472
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004473 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004474}
4475
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004476WL_EXPORT void
4477weston_compositor_stack_plane(struct weston_compositor *ec,
4478 struct weston_plane *plane,
4479 struct weston_plane *above)
4480{
4481 if (above)
4482 wl_list_insert(above->link.prev, &plane->link);
4483 else
4484 wl_list_insert(&ec->plane_list, &plane->link);
4485}
4486
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004487static void
4488output_release(struct wl_client *client, struct wl_resource *resource)
4489{
4490 wl_resource_destroy(resource);
4491}
4492
4493static const struct wl_output_interface output_interface = {
4494 output_release,
4495};
4496
4497
Casey Dahlin9074db52012-04-19 22:50:09 -04004498static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004499{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004500 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004501}
4502
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004503static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004504bind_output(struct wl_client *client,
4505 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004506{
Pekka Paalanen05347622017-03-27 12:24:34 +03004507 struct weston_head *head = data;
4508 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004509 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004510 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004511
Jason Ekstranda85118c2013-06-27 20:17:02 -05004512 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004513 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004514 if (resource == NULL) {
4515 wl_client_post_no_memory(client);
4516 return;
4517 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004518
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004519 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004520 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004521 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004522
Pekka Paalanen05347622017-03-27 12:24:34 +03004523 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004524 wl_output_send_geometry(resource,
4525 output->x,
4526 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004527 head->mm_width,
4528 head->mm_height,
4529 head->subpixel,
4530 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004531 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004532 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004533 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004534 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004535
4536 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004537 wl_output_send_mode(resource,
4538 mode->flags,
4539 mode->width,
4540 mode->height,
4541 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004542 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004543
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004544 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004545 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004546}
4547
Pekka Paalanendcac3512017-12-08 14:13:34 +02004548static void
4549weston_head_add_global(struct weston_head *head)
4550{
4551 head->global = wl_global_create(head->compositor->wl_display,
4552 &wl_output_interface, 3,
4553 head, bind_output);
4554}
4555
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004556/** Remove the global wl_output protocol object
4557 *
4558 * \param head The head whose global to remove.
4559 *
4560 * Also orphans the wl_resources for this head (wl_output).
4561 */
4562static void
4563weston_head_remove_global(struct weston_head *head)
4564{
4565 struct wl_resource *resource, *tmp;
4566
4567 if (head->global)
4568 wl_global_destroy(head->global);
4569 head->global = NULL;
4570
4571 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4572 unbind_resource(resource);
4573 wl_resource_set_destructor(resource, NULL);
4574 wl_resource_set_user_data(resource, NULL);
4575 }
4576}
4577
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004578/** Get the backing object of wl_output
4579 *
4580 * \param resource A wl_output protocol object.
4581 * \return The backing object (user data) of a wl_resource representing a
4582 * wl_output protocol object.
4583 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004584WL_EXPORT struct weston_head *
4585weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004586{
4587 assert(wl_resource_instance_of(resource, &wl_output_interface,
4588 &output_interface));
4589
4590 return wl_resource_get_user_data(resource);
4591}
4592
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004593/** Initialize a pre-allocated weston_head
4594 *
4595 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004596 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004597 *
4598 * The head will be safe to attach, detach and release.
4599 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004600 * The name is used in logs, and can be used by compositors as a configuration
4601 * identifier.
4602 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004603 * \memberof weston_head
4604 * \internal
4605 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004606WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004607weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004608{
4609 /* Add some (in)sane defaults which can be used
4610 * for checking if an output was properly configured
4611 */
4612 memset(head, 0, sizeof *head);
4613
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004614 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004615 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004616 wl_list_init(&head->output_link);
4617 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004618 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004619}
4620
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004621/** Send output heads changed signal
4622 *
4623 * \param output The output that changed.
4624 *
4625 * Notify that the enabled output gained and/or lost heads, or that the
4626 * associated heads may have changed their connection status. This does not
4627 * include cases where the output becomes enabled or disabled. The registered
4628 * callbacks are called after the change has successfully happened.
4629 *
4630 * If connection status change causes the compositor to attach or detach a head
4631 * to an enabled output, the registered callbacks may be called multiple times.
4632 */
4633static void
4634weston_output_emit_heads_changed(struct weston_output *output)
4635{
4636 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4637 output);
4638}
4639
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004640/** Idle task for emitting heads_changed_signal */
4641static void
4642weston_compositor_call_heads_changed(void *data)
4643{
4644 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004645 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004646
4647 compositor->heads_changed_source = NULL;
4648
4649 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004650
4651 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4652 if (head->output && head->output->enabled)
4653 weston_output_emit_heads_changed(head->output);
4654 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004655}
4656
4657/** Schedule a call on idle to heads_changed callback
4658 *
4659 * \param compositor The Compositor.
4660 *
4661 * \memberof weston_compositor
4662 * \internal
4663 */
4664static void
4665weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4666{
4667 struct wl_event_loop *loop;
4668
4669 if (compositor->heads_changed_source)
4670 return;
4671
4672 loop = wl_display_get_event_loop(compositor->wl_display);
4673 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4674 weston_compositor_call_heads_changed, compositor);
4675}
4676
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004677/** Register a new head
4678 *
4679 * \param compositor The compositor.
4680 * \param head The head to register, must not be already registered.
4681 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004682 * This signals the core that a new head has become available, leading to
4683 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004684 *
4685 * \memberof weston_compositor
4686 * \internal
4687 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004688WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004689weston_compositor_add_head(struct weston_compositor *compositor,
4690 struct weston_head *head)
4691{
4692 assert(wl_list_empty(&head->compositor_link));
4693 assert(head->name);
4694
4695 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4696 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004697 weston_compositor_schedule_heads_changed(compositor);
4698}
4699
4700/** Adds a listener to be called when heads change
4701 *
4702 * \param compositor The compositor.
4703 * \param listener The listener to add.
4704 *
4705 * The listener notify function argument is the \var compositor.
4706 *
4707 * The listener function will be called after heads are added or their
4708 * connection status has changed. Several changes may be accumulated into a
4709 * single call. The user is expected to iterate over the existing heads and
4710 * check their statuses to find out what changed.
4711 *
4712 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4713 * weston_head_is_enabled
4714 * \memberof weston_compositor
4715 */
4716WL_EXPORT void
4717weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4718 struct wl_listener *listener)
4719{
4720 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004721}
4722
4723/** Iterate over available heads
4724 *
4725 * \param compositor The compositor.
4726 * \param item The iterator, or NULL for start.
4727 * \return The next available head in the list.
4728 *
4729 * Returns all available heads, regardless of being connected or enabled.
4730 *
4731 * You can iterate over all heads as follows:
4732 * \code
4733 * struct weston_head *head = NULL;
4734 *
4735 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4736 * ...
4737 * }
4738 * \endcode
4739 *
4740 * If you cause \c iter to be removed from the list, you cannot use it to
4741 * continue iterating. Removing any other item is safe.
4742 *
4743 * \memberof weston_compositor
4744 */
4745WL_EXPORT struct weston_head *
4746weston_compositor_iterate_heads(struct weston_compositor *compositor,
4747 struct weston_head *iter)
4748{
4749 struct wl_list *list = &compositor->head_list;
4750 struct wl_list *node;
4751
4752 assert(compositor);
4753 assert(!iter || iter->compositor == compositor);
4754
4755 if (iter)
4756 node = iter->compositor_link.next;
4757 else
4758 node = list->next;
4759
4760 assert(node);
4761 assert(!iter || node != &iter->compositor_link);
4762
4763 if (node == list)
4764 return NULL;
4765
4766 return container_of(node, struct weston_head, compositor_link);
4767}
4768
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004769/** Iterate over attached heads
4770 *
4771 * \param output The output whose heads to iterate.
4772 * \param item The iterator, or NULL for start.
4773 * \return The next attached head in the list.
4774 *
4775 * Returns all heads currently attached to the output.
4776 *
4777 * You can iterate over all heads as follows:
4778 * \code
4779 * struct weston_head *head = NULL;
4780 *
4781 * while ((head = weston_output_iterate_heads(output, head))) {
4782 * ...
4783 * }
4784 * \endcode
4785 *
4786 * If you cause \c iter to be removed from the list, you cannot use it to
4787 * continue iterating. Removing any other item is safe.
4788 *
4789 * \memberof weston_compositor
4790 */
4791WL_EXPORT struct weston_head *
4792weston_output_iterate_heads(struct weston_output *output,
4793 struct weston_head *iter)
4794{
4795 struct wl_list *list = &output->head_list;
4796 struct wl_list *node;
4797
4798 assert(output);
4799 assert(!iter || iter->output == output);
4800
4801 if (iter)
4802 node = iter->output_link.next;
4803 else
4804 node = list->next;
4805
4806 assert(node);
4807 assert(!iter || node != &iter->output_link);
4808
4809 if (node == list)
4810 return NULL;
4811
4812 return container_of(node, struct weston_head, output_link);
4813}
4814
Pekka Paalanendcac3512017-12-08 14:13:34 +02004815/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004816 *
4817 * \param output The output to attach to.
4818 * \param head The head that is not yet attached.
4819 * \return 0 on success, -1 on failure.
4820 *
4821 * Attaches the given head to the output. All heads of an output are clones
4822 * and share the resolution and timings.
4823 *
4824 * Cloning heads this way uses less resources than creating an output for
4825 * each head, but is not always possible due to environment, driver and hardware
4826 * limitations.
4827 *
4828 * On failure, the head remains unattached. Success of this function does not
4829 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004830 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004831 *
4832 * \memberof weston_output
4833 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004834WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004835weston_output_attach_head(struct weston_output *output,
4836 struct weston_head *head)
4837{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004838 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004839
4840 if (!wl_list_empty(&head->output_link))
4841 return -1;
4842
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004843 if (output->attach_head) {
4844 if (output->attach_head(output, head) < 0)
4845 return -1;
4846 } else if (!wl_list_empty(&output->head_list)) {
4847 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004848 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004849 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004850
4851 head->output = output;
4852 wl_list_insert(output->head_list.prev, &head->output_link);
4853
Pekka Paalanendcac3512017-12-08 14:13:34 +02004854 if (output->enabled) {
4855 weston_head_add_global(head);
4856
4857 head_names = weston_output_create_heads_string(output);
4858 weston_log("Output '%s' updated to have head(s) %s\n",
4859 output->name, head_names);
4860 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004861
4862 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02004863 }
4864
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004865 return 0;
4866}
4867
4868/** Detach a head from its output
4869 *
4870 * \param head The head to detach.
4871 *
4872 * It is safe to detach a non-attached head.
4873 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004874 * If the head is attached to an enabled output and the output will be left
4875 * with no heads, the output will be disabled.
4876 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004877 * \memberof weston_head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004878 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004879 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004880WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004881weston_head_detach(struct weston_head *head)
4882{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004883 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02004884 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004885
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004886 wl_list_remove(&head->output_link);
4887 wl_list_init(&head->output_link);
4888 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004889
4890 if (!output)
4891 return;
4892
4893 if (output->detach_head)
4894 output->detach_head(output, head);
4895
4896 if (output->enabled) {
4897 weston_head_remove_global(head);
4898
Pekka Paalanena0106992017-12-08 16:11:17 +02004899 if (wl_list_empty(&output->head_list)) {
4900 weston_log("Output '%s' no heads left, disabling.\n",
4901 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004902 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004903 } else {
4904 head_names = weston_output_create_heads_string(output);
4905 weston_log("Output '%s' updated to have head(s) %s\n",
4906 output->name, head_names);
4907 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004908
4909 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004910 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004911 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004912}
4913
4914/** Destroy a head
4915 *
4916 * \param head The head to be released.
4917 *
4918 * Destroys the head. The caller is responsible for freeing the memory pointed
4919 * to by \c head.
4920 *
4921 * \memberof weston_head
4922 * \internal
4923 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004924WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004925weston_head_release(struct weston_head *head)
4926{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004927 wl_signal_emit(&head->destroy_signal, head);
4928
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004929 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004930
4931 free(head->make);
4932 free(head->model);
4933 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004934 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004935
4936 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004937}
4938
Pekka Paalanene19970f2017-08-28 14:11:02 +03004939static void
4940weston_head_set_device_changed(struct weston_head *head)
4941{
4942 head->device_changed = true;
4943
4944 if (head->compositor)
4945 weston_compositor_schedule_heads_changed(head->compositor);
4946}
4947
4948/** String equal comparison with NULLs being equal */
4949static bool
4950str_null_eq(const char *a, const char *b)
4951{
4952 if (!a && !b)
4953 return true;
4954
4955 if (!!a != !!b)
4956 return false;
4957
4958 return strcmp(a, b) == 0;
4959}
4960
Pekka Paalanen01f60212017-03-24 15:39:24 +02004961/** Store monitor make, model and serial number
4962 *
4963 * \param head The head to modify.
4964 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
4965 * any string, or NULL for none.
4966 * \param model The monitor model or name, or a made-up string, or NULL for
4967 * none.
4968 * \param serialno The monitor serial number, a made-up string, or NULL for
4969 * none.
4970 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004971 * This may set the device_changed flag.
4972 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004973 * \memberof weston_head
4974 * \internal
4975 */
4976WL_EXPORT void
4977weston_head_set_monitor_strings(struct weston_head *head,
4978 const char *make,
4979 const char *model,
4980 const char *serialno)
4981{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004982 if (str_null_eq(head->make, make) &&
4983 str_null_eq(head->model, model) &&
4984 str_null_eq(head->serial_number, serialno))
4985 return;
4986
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004987 free(head->make);
4988 free(head->model);
4989 free(head->serial_number);
4990
4991 head->make = make ? strdup(make) : NULL;
4992 head->model = model ? strdup(model) : NULL;
4993 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004994
4995 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004996}
4997
Philipp Zabelc18ffd32018-08-30 17:38:03 +02004998/** Store display non-desktop status
4999 *
5000 * \param head The head to modify.
5001 * \param non_desktop Whether the head connects to a non-desktop display.
5002 *
5003 * \memberof weston_head
5004 * \internal
5005 */
5006WL_EXPORT void
5007weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5008{
5009 if (head->non_desktop == non_desktop)
5010 return;
5011
5012 head->non_desktop = non_desktop;
5013
5014 weston_head_set_device_changed(head);
5015}
5016
Pekka Paalanen01f60212017-03-24 15:39:24 +02005017/** Store physical image size
5018 *
5019 * \param head The head to modify.
5020 * \param mm_width Image area width in millimeters.
5021 * \param mm_height Image area height in millimeters.
5022 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005023 * This may set the device_changed flag.
5024 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02005025 * \memberof weston_head
5026 * \internal
5027 */
5028WL_EXPORT void
5029weston_head_set_physical_size(struct weston_head *head,
5030 int32_t mm_width, int32_t mm_height)
5031{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005032 if (head->mm_width == mm_width &&
5033 head->mm_height == mm_height)
5034 return;
5035
Pekka Paalanen01f60212017-03-24 15:39:24 +02005036 head->mm_width = mm_width;
5037 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005038
5039 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005040}
5041
5042/** Store monitor sub-pixel layout
5043 *
5044 * \param head The head to modify.
5045 * \param sp Sub-pixel layout. The possible values are:
5046 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5047 * - WL_OUTPUT_SUBPIXEL_NONE,
5048 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5049 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5050 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5051 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5052 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005053 * This may set the device_changed flag.
5054 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02005055 * \memberof weston_head
5056 * \internal
5057 */
5058WL_EXPORT void
5059weston_head_set_subpixel(struct weston_head *head,
5060 enum wl_output_subpixel sp)
5061{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005062 if (head->subpixel == sp)
5063 return;
5064
Pekka Paalanen01f60212017-03-24 15:39:24 +02005065 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005066
5067 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005068}
5069
5070/** Mark the monitor as internal
5071 *
5072 * This is used for embedded screens, like laptop panels.
5073 *
5074 * \param head The head to mark as internal.
5075 *
5076 * By default a head is external. The type is often inferred from the physical
5077 * connector type.
5078 *
5079 * \memberof weston_head
5080 * \internal
5081 */
5082WL_EXPORT void
5083weston_head_set_internal(struct weston_head *head)
5084{
5085 head->connection_internal = true;
5086}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005087
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005088/** Store connector status
5089 *
5090 * \param head The head to modify.
5091 * \param connected Whether the head is connected.
5092 *
5093 * Connectors are created as disconnected. This function can be used to
5094 * set the connector status.
5095 *
5096 * The status should be set to true when a physical connector is connected to
5097 * a video sink device like a monitor and to false when the connector is
5098 * disconnected. For nested backends, the connection status should reflect the
5099 * connection to the parent display server.
5100 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005101 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005102 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005103 *
5104 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005105 * \memberof weston_head
5106 * \internal
5107 */
5108WL_EXPORT void
5109weston_head_set_connection_status(struct weston_head *head, bool connected)
5110{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005111 if (head->connected == connected)
5112 return;
5113
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005114 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005115
Pekka Paalanene19970f2017-08-28 14:11:02 +03005116 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005117}
5118
5119/** Is the head currently connected?
5120 *
5121 * \param head The head to query.
5122 * \return Connection status.
5123 *
5124 * Returns true if the head is physically connected to a monitor, or in
5125 * case of a nested backend returns true when there is a connection to the
5126 * parent display server.
5127 *
5128 * This is independent from the head being enabled.
5129 *
5130 * \sa weston_head_is_enabled
5131 * \memberof weston_head
5132 */
5133WL_EXPORT bool
5134weston_head_is_connected(struct weston_head *head)
5135{
5136 return head->connected;
5137}
5138
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005139/** Is the head currently enabled?
5140 *
5141 * \param head The head to query.
5142 * \return Video status.
5143 *
5144 * Returns true if the head is currently transmitting a video stream.
5145 *
5146 * This is independent of the head being connected.
5147 *
5148 * \sa weston_head_is_connected
5149 * \memberof weston_head
5150 */
5151WL_EXPORT bool
5152weston_head_is_enabled(struct weston_head *head)
5153{
5154 if (!head->output)
5155 return false;
5156
5157 return head->output->enabled;
5158}
5159
Pekka Paalanene19970f2017-08-28 14:11:02 +03005160/** Has the device information changed?
5161 *
5162 * \param head The head to query.
5163 * \return True if the device information has changed since last reset.
5164 *
5165 * The information about the connected display device, e.g. a monitor, may
5166 * change without being disconnected in between. Changing information
5167 * causes a call to the heads_changed hook.
5168 *
5169 * The information includes make, model, serial number, physical size,
5170 * and sub-pixel type. The connection status is also included.
5171 *
5172 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
5173 * \memberof weston_head
5174 */
5175WL_EXPORT bool
5176weston_head_is_device_changed(struct weston_head *head)
5177{
5178 return head->device_changed;
5179}
5180
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005181/** Does the head represent a non-desktop display?
5182 *
5183 * \param head The head to query.
5184 * \return True if the device is a non-desktop display.
5185 *
5186 * Non-desktop heads are not attached to outputs by default.
5187 * This stops weston from extending the desktop onto head mounted displays.
5188 *
5189 * \memberof weston_head
5190 */
5191WL_EXPORT bool
5192weston_head_is_non_desktop(struct weston_head *head)
5193{
5194 return head->non_desktop;
5195}
5196
Pekka Paalanene19970f2017-08-28 14:11:02 +03005197/** Acknowledge device information change
5198 *
5199 * \param head The head to acknowledge.
5200 *
5201 * Clears the device changed flag on this head. When a compositor has processed
5202 * device information, it should call this to be able to notice further
5203 * changes.
5204 *
5205 * \sa weston_head_is_device_changed
5206 * \memberof weston_head
5207 */
5208WL_EXPORT void
5209weston_head_reset_device_changed(struct weston_head *head)
5210{
5211 head->device_changed = false;
5212}
5213
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005214/** Get the name of a head
5215 *
5216 * \param head The head to query.
5217 * \return The head's name, not NULL.
5218 *
5219 * The name depends on the backend. The DRM backend uses connector names,
5220 * other backends may use hardcoded names or user-given names.
5221 */
5222WL_EXPORT const char *
5223weston_head_get_name(struct weston_head *head)
5224{
5225 return head->name;
5226}
5227
5228/** Get the output the head is attached to
5229 *
5230 * \param head The head to query.
5231 * \return The output the head is attached to, or NULL if detached.
5232 */
5233WL_EXPORT struct weston_output *
5234weston_head_get_output(struct weston_head *head)
5235{
5236 return head->output;
5237}
5238
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005239/** Add destroy callback for a head
5240 *
5241 * \param head The head to watch for.
5242 * \param listener The listener to add. The \c notify member must be set.
5243 *
5244 * Heads may get destroyed for various reasons by the backends. If a head is
5245 * attached to an output, the compositor should listen for head destruction
5246 * and reconfigure or destroy the output if necessary.
5247 *
5248 * The destroy callbacks will be called on weston_head destruction before any
5249 * automatic detaching from an associated weston_output and before any
5250 * weston_head information is lost.
5251 *
5252 * The \c data argument to the notify callback is the weston_head being
5253 * destroyed.
5254 */
5255WL_EXPORT void
5256weston_head_add_destroy_listener(struct weston_head *head,
5257 struct wl_listener *listener)
5258{
5259 wl_signal_add(&head->destroy_signal, listener);
5260}
5261
5262/** Look up destroy listener for a head
5263 *
5264 * \param head The head to query.
5265 * \param notify The notify function used used for the added destroy listener.
5266 * \return The listener, or NULL if not found.
5267 *
5268 * This looks up the previously added destroy listener struct based on the
5269 * notify function it has. The listener can be used to access user data
5270 * through \c container_of().
5271 *
5272 * \sa wl_signal_get()
5273 */
5274WL_EXPORT struct wl_listener *
5275weston_head_get_destroy_listener(struct weston_head *head,
5276 wl_notify_func_t notify)
5277{
5278 return wl_signal_get(&head->destroy_signal, notify);
5279}
5280
David Fort0de859e2016-05-27 23:22:57 +02005281/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005282static void
David Fort0de859e2016-05-27 23:22:57 +02005283weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5284 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005285{
5286 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005287 bool start_resizing = false;
5288
5289 if (!delta_width)
5290 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005291
5292 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005293 if (output == resized_output) {
5294 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005295 continue;
5296 }
5297
David Fort0de859e2016-05-27 23:22:57 +02005298 if (start_resizing) {
5299 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005300 output->dirty = 1;
5301 }
5302 }
5303}
5304
Pekka Paalanend72bad22017-03-29 17:01:41 +03005305static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005306weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005307{
Scott Moreau850ca422012-05-21 15:21:25 -06005308 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005309
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005310 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005311 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005312
Scott Moreauccbf29d2012-02-22 14:21:41 -07005313 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005314 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005315 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005316 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005317 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005318 weston_matrix_scale(&output->matrix, magnification,
5319 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005320 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005321
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005322 switch (output->transform) {
5323 case WL_OUTPUT_TRANSFORM_FLIPPED:
5324 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5325 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5326 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5327 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5328 weston_matrix_scale(&output->matrix, -1, 1, 1);
5329 break;
5330 }
5331
5332 switch (output->transform) {
5333 default:
5334 case WL_OUTPUT_TRANSFORM_NORMAL:
5335 case WL_OUTPUT_TRANSFORM_FLIPPED:
5336 break;
5337 case WL_OUTPUT_TRANSFORM_90:
5338 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5339 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5340 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5341 break;
5342 case WL_OUTPUT_TRANSFORM_180:
5343 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5344 weston_matrix_translate(&output->matrix,
5345 -output->width, -output->height, 0);
5346 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5347 break;
5348 case WL_OUTPUT_TRANSFORM_270:
5349 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5350 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5351 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5352 break;
5353 }
5354
5355 if (output->current_scale != 1)
5356 weston_matrix_scale(&output->matrix,
5357 output->current_scale,
5358 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005359
Scott Moreauccbf29d2012-02-22 14:21:41 -07005360 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005361
5362 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005363}
5364
Scott Moreau1bad5db2012-08-18 01:04:05 -06005365static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005366weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005367{
5368 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005369 output->native_scale = scale;
5370 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005371
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005372 convert_size_by_transform_scale(&output->width, &output->height,
5373 output->current_mode->width,
5374 output->current_mode->height,
5375 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005376}
5377
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005378static void
5379weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005380{
5381 output->x = x;
5382 output->y = y;
5383
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005384 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005385 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005386
5387 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005388 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005389 output->width,
5390 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005391}
5392
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005393WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005394weston_output_move(struct weston_output *output, int x, int y)
5395{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005396 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005397 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005398 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005399
5400 output->move_x = x - output->x;
5401 output->move_y = y - output->y;
5402
5403 if (output->move_x == 0 && output->move_y == 0)
5404 return;
5405
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005406 weston_output_init_geometry(output, x, y);
5407
5408 output->dirty = 1;
5409
5410 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005411 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005412
5413 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005414 wl_list_for_each(head, &output->head_list, output_link) {
5415 wl_resource_for_each(resource, &head->resource_list) {
5416 wl_output_send_geometry(resource,
5417 output->x,
5418 output->y,
5419 head->mm_width,
5420 head->mm_height,
5421 head->subpixel,
5422 head->make,
5423 head->model,
5424 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005425
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005426 ver = wl_resource_get_version(resource);
5427 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5428 wl_output_send_done(resource);
5429 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005430 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005431}
5432
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005433/** Signal that a pending output is taken into use.
5434 *
5435 * Removes the output from the pending list and adds it to the compositor's
5436 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005437 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005438 * The output gets an internal ID assigned, and the wl_output global is
5439 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005440 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005441 * \param compositor The compositor instance.
5442 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005443 *
5444 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005445 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005446static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005447weston_compositor_add_output(struct weston_compositor *compositor,
5448 struct weston_output *output)
5449{
Armin Krezoviće5403842016-08-05 15:28:29 +02005450 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005451 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005452
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005453 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005454
5455 /* Verify we haven't reached the limit of 32 available output IDs */
5456 assert(ffs(~compositor->output_id_pool) > 0);
5457
5458 /* Invert the output id pool and look for the lowest numbered
5459 * switch (the least significant bit). Take that bit's position
5460 * as our ID, and mark it used in the compositor's output_id_pool.
5461 */
5462 output->id = ffs(~compositor->output_id_pool) - 1;
5463 compositor->output_id_pool |= 1u << output->id;
5464
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005465 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005466 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005467 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005468
Pekka Paalanendcac3512017-12-08 14:13:34 +02005469 wl_list_for_each(head, &output->head_list, output_link)
5470 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005471
Giulio Camuffob1147152015-05-06 21:41:57 +03005472 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005473
5474 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5475 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005476}
5477
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005478/** Transform device coordinates into global coordinates
5479 *
5480 * \param device_x[in] X coordinate in device units.
5481 * \param device_y[in] Y coordinate in device units.
5482 * \param x[out] X coordinate in the global space.
5483 * \param y[out] Y coordinate in the global space.
5484 *
5485 * Transforms coordinates from the device coordinate space
5486 * (physical pixel units) to the global coordinate space (logical pixel units).
5487 * This takes into account output transform and scale.
5488 *
5489 * \memberof weston_output
5490 * \internal
5491 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005492WL_EXPORT void
5493weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005494 double device_x, double device_y,
5495 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005496{
Derek Foreman0f679412014-10-02 13:41:17 -05005497 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005498 device_x,
5499 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005500 0.0,
5501 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005502
Derek Foreman67a18b92015-03-24 11:36:14 -05005503 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005504
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005505 *x = p.f[0] / p.f[3];
5506 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005507}
5508
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005509/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005510 *
5511 * \param output The weston_output object that is being removed.
5512 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005513 * The following happens:
5514 *
5515 * - The output assignments of all views in the current scenegraph are
5516 * recomputed.
5517 *
5518 * - Presentation feedback is discarded.
5519 *
5520 * - Compositor is notified that outputs were changed and
5521 * applies the necessary changes to re-layout outputs.
5522 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005523 * - The output is put back in the pending outputs list.
5524 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005525 * - Signal is emitted to notify all users of the weston_output
5526 * object that the output is being destroyed.
5527 *
5528 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005529 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005530 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005531 * - The output's internal ID is released.
5532 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005533 * \memberof weston_output
5534 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005535 */
5536static void
5537weston_compositor_remove_output(struct weston_output *output)
5538{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005539 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005540 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005541 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005542
5543 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005544 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005545
Pekka Paalanenbccda712017-03-29 16:16:04 +03005546 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005547 if (view->output_mask & (1u << output->id))
5548 weston_view_assign_output(view);
5549 }
5550
5551 weston_presentation_feedback_discard_list(&output->feedback_list);
5552
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005553 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005554
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005555 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005556 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005557 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005558
Pekka Paalanenbccda712017-03-29 16:16:04 +03005559 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005560 wl_signal_emit(&output->destroy_signal, output);
5561
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005562 wl_list_for_each(head, &output->head_list, output_link)
5563 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005564
5565 compositor->output_id_pool &= ~(1u << output->id);
5566 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005567}
5568
5569/** Sets the output scale for a given output.
5570 *
5571 * \param output The weston_output object that the scale is set for.
5572 * \param scale Scale factor for the given output.
5573 *
5574 * It only supports setting scale for an output that
5575 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005576 *
5577 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005578 */
5579WL_EXPORT void
5580weston_output_set_scale(struct weston_output *output,
5581 int32_t scale)
5582{
5583 /* We can only set scale on a disabled output */
5584 assert(!output->enabled);
5585
5586 /* We only want to set scale once */
5587 assert(!output->scale);
5588
5589 output->scale = scale;
5590}
5591
5592/** Sets the output transform for a given output.
5593 *
5594 * \param output The weston_output object that the transform is set for.
5595 * \param transform Transform value for the given output.
5596 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005597 * Refer to wl_output::transform section located at
5598 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5599 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005600 *
5601 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005602 */
5603WL_EXPORT void
5604weston_output_set_transform(struct weston_output *output,
5605 uint32_t transform)
5606{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005607 struct weston_pointer_motion_event ev;
5608 struct wl_resource *resource;
5609 struct weston_seat *seat;
5610 pixman_region32_t old_region;
5611 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005612 struct weston_head *head;
5613 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005614
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005615 if (!output->enabled && output->transform == UINT32_MAX) {
5616 output->transform = transform;
5617 return;
5618 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005619
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005620 weston_output_transform_scale_init(output, transform, output->scale);
5621
5622 pixman_region32_init(&old_region);
5623 pixman_region32_copy(&old_region, &output->region);
5624
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005625 weston_output_init_geometry(output, output->x, output->y);
5626
5627 output->dirty = 1;
5628
5629 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005630 wl_list_for_each(head, &output->head_list, output_link) {
5631 wl_resource_for_each(resource, &head->resource_list) {
5632 wl_output_send_geometry(resource,
5633 output->x,
5634 output->y,
5635 head->mm_width,
5636 head->mm_height,
5637 head->subpixel,
5638 head->make,
5639 head->model,
5640 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005641
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005642 ver = wl_resource_get_version(resource);
5643 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5644 wl_output_send_done(resource);
5645 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005646 }
5647
5648 /* we must ensure that pointers are inside output, otherwise they disappear */
5649 mid_x = output->x + output->width / 2;
5650 mid_y = output->y + output->height / 2;
5651
5652 ev.mask = WESTON_POINTER_MOTION_ABS;
5653 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5654 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5655
5656 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5657 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5658
5659 if (pointer && pixman_region32_contains_point(&old_region,
5660 wl_fixed_to_int(pointer->x),
5661 wl_fixed_to_int(pointer->y),
5662 NULL))
5663 weston_pointer_move(pointer, &ev);
5664 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005665}
5666
5667/** Initializes a weston_output object with enough data so
5668 ** an output can be configured.
5669 *
5670 * \param output The weston_output object to initialize
5671 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005672 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005673 *
5674 * Sets initial values for fields that are expected to be
5675 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005676 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005677 * The name is used in logs, and can be used by compositors as a configuration
5678 * identifier.
5679 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005680 * \memberof weston_output
5681 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005682 */
5683WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005684weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005685 struct weston_compositor *compositor,
5686 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005687{
5688 output->compositor = compositor;
5689 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005690 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005691 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005692 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005693 output->enabled = false;
5694
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005695 wl_list_init(&output->head_list);
5696
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005697 /* Add some (in)sane defaults which can be used
5698 * for checking if an output was properly configured
5699 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005700 output->scale = 0;
5701 /* Can't use -1 on uint32_t and 0 is valid enum value */
5702 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005703
5704 pixman_region32_init(&output->previous_damage);
5705 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005706 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005707}
5708
5709/** Adds weston_output object to pending output list.
5710 *
5711 * \param output The weston_output object to add
5712 * \param compositor The compositor instance.
5713 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005714 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005715 *
5716 * \memberof weston_output
5717 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005718 */
5719WL_EXPORT void
5720weston_compositor_add_pending_output(struct weston_output *output,
5721 struct weston_compositor *compositor)
5722{
Pekka Paalanene952a012017-03-29 17:14:00 +03005723 assert(output->disable);
5724 assert(output->enable);
5725
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005726 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005727 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005728}
5729
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005730/** Create a string with the attached heads' names.
5731 *
5732 * The string must be free()'d.
5733 */
5734static char *
5735weston_output_create_heads_string(struct weston_output *output)
5736{
5737 FILE *fp;
5738 char *str = NULL;
5739 size_t size = 0;
5740 struct weston_head *head;
5741 const char *sep = "";
5742
5743 fp = open_memstream(&str, &size);
5744 if (!fp)
5745 return NULL;
5746
5747 wl_list_for_each(head, &output->head_list, output_link) {
5748 fprintf(fp, "%s%s", sep, head->name);
5749 sep = ", ";
5750 }
5751 fclose(fp);
5752
5753 return str;
5754}
5755
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005756/** Constructs a weston_output object that can be used by the compositor.
5757 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005758 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005759 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005760 *
5761 * Output coordinates are calculated and each new output is by default
5762 * assigned to the right of previous one.
5763 *
5764 * Sets up the transformation, zoom, and geometry of the output using
5765 * the properties that need to be configured by the compositor.
5766 *
5767 * Establishes a repaint timer for the output with the relevant display
5768 * object's event loop. See output_repaint_timer_handler().
5769 *
5770 * The output is assigned an ID. Weston can support up to 32 distinct
5771 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5772 * is referred to and used to find the first available ID number, and
5773 * then this ID is marked as used in output_id_pool.
5774 *
5775 * The output is also assigned a Wayland global with the wl_output
5776 * external interface.
5777 *
5778 * Backend specific function is called to set up the output output.
5779 *
5780 * Output is added to the compositor's output list
5781 *
5782 * If the backend specific function fails, the weston_output object
5783 * is returned to a state it was before calling this function and
5784 * is added to the compositor's pending_output_list in case it needs
5785 * to be reconfigured or just so it can be destroyed at shutdown.
5786 *
5787 * 0 is returned on success, -1 on failure.
5788 */
5789WL_EXPORT int
5790weston_output_enable(struct weston_output *output)
5791{
Armin Krezović782f5df2016-09-30 14:11:11 +02005792 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005793 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005794 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005795 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005796 int x = 0, y = 0;
5797
Pekka Paalanencc201e42017-03-30 15:11:25 +03005798 if (output->enabled) {
5799 weston_log("Error: attempt to enable an enabled output '%s'\n",
5800 output->name);
5801 return -1;
5802 }
5803
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005804 if (wl_list_empty(&output->head_list)) {
5805 weston_log("Error: cannot enable output '%s' without heads.\n",
5806 output->name);
5807 return -1;
5808 }
5809
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005810 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5811 weston_log("Error: no video mode for output '%s'.\n",
5812 output->name);
5813 return -1;
5814 }
5815
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005816 wl_list_for_each(head, &output->head_list, output_link) {
5817 assert(head->make);
5818 assert(head->model);
5819 }
5820
Armin Krezović782f5df2016-09-30 14:11:11 +02005821 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005822 struct weston_output, link);
5823
Armin Krezović782f5df2016-09-30 14:11:11 +02005824 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005825 x = iterator->x + iterator->width;
5826
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005827 /* Make sure the scale is set up */
5828 assert(output->scale);
5829
5830 /* Make sure we have a transform set */
5831 assert(output->transform != UINT32_MAX);
5832
Armin Krezović782f5df2016-09-30 14:11:11 +02005833 output->x = x;
5834 output->y = y;
5835 output->dirty = 1;
5836 output->original_scale = output->scale;
5837
5838 weston_output_transform_scale_init(output, output->transform, output->scale);
5839 weston_output_init_zoom(output);
5840
5841 weston_output_init_geometry(output, x, y);
5842 weston_output_damage(output);
5843
5844 wl_signal_init(&output->frame_signal);
5845 wl_signal_init(&output->destroy_signal);
5846 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005847 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005848
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005849 /* Enable the output (set up the crtc or create a
5850 * window representing the output, set up the
5851 * renderer, etc)
5852 */
5853 if (output->enable(output) < 0) {
5854 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005855 return -1;
5856 }
5857
5858 weston_compositor_add_output(output->compositor, output);
5859
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005860 head_names = weston_output_create_heads_string(output);
5861 weston_log("Output '%s' enabled with head(s) %s\n",
5862 output->name, head_names);
5863 free(head_names);
5864
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005865 return 0;
5866}
5867
5868/** Converts a weston_output object to a pending output state, so it
5869 ** can be configured again or destroyed.
5870 *
5871 * \param output The weston_output object that needs to be disabled.
5872 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005873 * Calls a backend specific function to disable an output, in case
5874 * such function exists.
5875 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005876 * The backend specific disable function may choose to postpone the disabling
5877 * by returning a negative value, in which case this function returns early.
5878 * In that case the backend will guarantee the output will be disabled soon
5879 * by the backend calling this function again. One must not attempt to re-enable
5880 * the output until that happens.
5881 *
5882 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005883 * from weston's output_list (see weston_compositor_remove_output())
5884 * and is returned to a state it was before weston_output_enable()
5885 * was ran (see weston_output_enable_undo()).
5886 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005887 * See weston_output_init() for more information on the
5888 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005889 *
5890 * If the output has never been enabled yet, this function can still be
5891 * called to ensure that the output is actually turned off rather than left
5892 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005893 */
5894WL_EXPORT void
5895weston_output_disable(struct weston_output *output)
5896{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005897 /* Should we rename this? */
5898 output->destroying = 1;
5899
Pekka Paalanenc65df642017-03-29 15:45:46 +03005900 /* Disable is called unconditionally also for not-enabled outputs,
5901 * because at compositor start-up, if there is an output that is
5902 * already on but the compositor wants to turn it off, we have to
5903 * forward the turn-off to the backend so it knows to do it.
5904 * The backend cannot initially turn off everything, because it
5905 * would cause unnecessary mode-sets for all outputs the compositor
5906 * wants to be on.
5907 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005908 if (output->disable(output) < 0)
5909 return;
5910
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005911 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005912 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005913
5914 output->destroying = 0;
5915}
5916
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005917/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005918 *
5919 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005920 *
5921 * If there are new or changed heads, calls the heads_changed hook and
5922 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005923 */
5924WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005925weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005926{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005927 if (compositor->heads_changed_source) {
5928 wl_event_source_remove(compositor->heads_changed_source);
5929 weston_compositor_call_heads_changed(compositor);
5930 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005931}
5932
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005933/** Add destroy callback for an output
5934 *
5935 * \param output The output to watch.
5936 * \param listener The listener to add. The \c notify member must be set.
5937 *
5938 * The listener callback will be called when user destroys an output. This
5939 * may be delayed by a backend in some cases. The main purpose of the
5940 * listener is to allow hooking up custom data to the output. The custom data
5941 * can be fetched via weston_output_get_destroy_listener() followed by
5942 * container_of().
5943 *
5944 * The \c data argument to the notify callback is the weston_output being
5945 * destroyed.
5946 *
5947 * @note This is for the final destruction of an output, not when it gets
5948 * disabled. If you want to keep track of enabled outputs, this is not it.
5949 */
5950WL_EXPORT void
5951weston_output_add_destroy_listener(struct weston_output *output,
5952 struct wl_listener *listener)
5953{
5954 wl_signal_add(&output->user_destroy_signal, listener);
5955}
5956
5957/** Look up destroy listener for an output
5958 *
5959 * \param output The output to query.
5960 * \param notify The notify function used used for the added destroy listener.
5961 * \return The listener, or NULL if not found.
5962 *
5963 * This looks up the previously added destroy listener struct based on the
5964 * notify function it has. The listener can be used to access user data
5965 * through \c container_of().
5966 *
5967 * \sa wl_signal_get() weston_output_add_destroy_listener()
5968 */
5969WL_EXPORT struct wl_listener *
5970weston_output_get_destroy_listener(struct weston_output *output,
5971 wl_notify_func_t notify)
5972{
5973 return wl_signal_get(&output->user_destroy_signal, notify);
5974}
5975
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005976/** Uninitialize an output
5977 *
5978 * Removes the output from the list of enabled outputs if necessary, but
5979 * does not call the backend's output disable function. The output will no
5980 * longer be in the list of pending outputs either.
5981 *
5982 * All fields of weston_output become uninitialized, i.e. should not be used
5983 * anymore. The caller can free the memory after this.
5984 *
5985 * \memberof weston_output
5986 * \internal
5987 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005988WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005989weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005990{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005991 struct weston_head *head, *tmp;
5992
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005993 output->destroying = 1;
5994
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005995 wl_signal_emit(&output->user_destroy_signal, output);
5996
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03005997 if (output->idle_repaint_source)
5998 wl_event_source_remove(output->idle_repaint_source);
5999
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006000 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006001 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006002
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006003 pixman_region32_fini(&output->region);
6004 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006005 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006006
6007 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6008 weston_head_detach(head);
6009
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006010 free(output->name);
6011}
6012
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006013/** Find an output by its given name
6014 *
6015 * \param compositor The compositor to search in.
6016 * \param name The output name to search for.
6017 * \return An existing output with the given name, or NULL if not found.
6018 *
6019 * \memberof weston_compositor
6020 */
6021WL_EXPORT struct weston_output *
6022weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6023 const char *name)
6024{
6025 struct weston_output *output;
6026
6027 wl_list_for_each(output, &compositor->output_list, link)
6028 if (strcmp(output->name, name) == 0)
6029 return output;
6030
6031 wl_list_for_each(output, &compositor->pending_output_list, link)
6032 if (strcmp(output->name, name) == 0)
6033 return output;
6034
6035 return NULL;
6036}
6037
6038/** Create a named output
6039 *
6040 * \param compositor The compositor.
6041 * \param name The name for the output.
6042 * \return A new \c weston_output, or NULL on failure.
6043 *
6044 * This creates a new weston_output that starts with no heads attached.
6045 *
6046 * An output must be configured and it must have at least one head before
6047 * it can be enabled.
6048 *
6049 * \memberof weston_compositor
6050 */
6051WL_EXPORT struct weston_output *
6052weston_compositor_create_output(struct weston_compositor *compositor,
6053 const char *name)
6054{
6055 assert(compositor->backend->create_output);
6056
6057 if (weston_compositor_find_output_by_name(compositor, name)) {
6058 weston_log("Warning: attempted to create an output with a "
6059 "duplicate name '%s'.\n", name);
6060 return NULL;
6061 }
6062
6063 return compositor->backend->create_output(compositor, name);
6064}
6065
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006066/** Create an output for an unused head
6067 *
6068 * \param compositor The compositor.
6069 * \param head The head to attach to the output.
6070 * \return A new \c weston_output, or NULL on failure.
6071 *
6072 * This creates a new weston_output that starts with the given head attached.
6073 * The output inherits the name of the head. The head must not be already
6074 * attached to another output.
6075 *
6076 * An output must be configured before it can be enabled.
6077 *
6078 * \memberof weston_compositor
6079 */
6080WL_EXPORT struct weston_output *
6081weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6082 struct weston_head *head)
6083{
6084 struct weston_output *output;
6085
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006086 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006087 if (!output)
6088 return NULL;
6089
6090 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006091 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006092 return NULL;
6093 }
6094
6095 return output;
6096}
6097
6098/** Destroy an output
6099 *
6100 * \param output The output to destroy.
6101 *
6102 * The heads attached to the given output are detached and become unused again.
6103 *
6104 * It is not necessary to explicitly destroy all outputs at compositor exit.
6105 * weston_compositor_destroy() will automatically destroy any remaining
6106 * outputs.
6107 *
6108 * \memberof weston_output
6109 */
6110WL_EXPORT void
6111weston_output_destroy(struct weston_output *output)
6112{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006113 output->destroy(output);
6114}
6115
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006116/** When you need a head...
6117 *
6118 * This function is a hack, used until all code has been converted to become
6119 * multi-head aware.
6120 *
6121 * \param output The weston_output whose head to get.
6122 * \return The first head in the output's list.
6123 */
6124WL_EXPORT struct weston_head *
6125weston_output_get_first_head(struct weston_output *output)
6126{
6127 if (wl_list_empty(&output->head_list))
6128 return NULL;
6129
6130 return container_of(output->head_list.next,
6131 struct weston_head, output_link);
6132}
6133
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006134static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006135destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006136{
Jonny Lamb74130762013-11-26 18:19:46 +01006137 struct weston_surface *surface =
6138 wl_resource_get_user_data(resource);
6139
Pekka Paalanen4826f872016-04-22 14:14:38 +03006140 if (!surface)
6141 return;
6142
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006143 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006144 surface->pending.buffer_viewport.buffer.src_width =
6145 wl_fixed_from_int(-1);
6146 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006147 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006148}
6149
6150static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006151viewport_destroy(struct wl_client *client,
6152 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006153{
6154 wl_resource_destroy(resource);
6155}
6156
6157static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006158viewport_set_source(struct wl_client *client,
6159 struct wl_resource *resource,
6160 wl_fixed_t src_x,
6161 wl_fixed_t src_y,
6162 wl_fixed_t src_width,
6163 wl_fixed_t src_height)
6164{
6165 struct weston_surface *surface =
6166 wl_resource_get_user_data(resource);
6167
Pekka Paalanen4826f872016-04-22 14:14:38 +03006168 if (!surface) {
6169 wl_resource_post_error(resource,
6170 WP_VIEWPORT_ERROR_NO_SURFACE,
6171 "wl_surface for this viewport is no longer exists");
6172 return;
6173 }
6174
6175 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006176 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006177
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006178 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006179 src_height == wl_fixed_from_int(-1) &&
6180 src_x == wl_fixed_from_int(-1) &&
6181 src_y == wl_fixed_from_int(-1)) {
6182 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006183 surface->pending.buffer_viewport.buffer.src_width =
6184 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006185 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006186 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006187 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006188
Pekka Paalanen201769a2016-04-26 14:42:11 +03006189 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006190 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006191 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006192 "wl_surface@%d viewport source "
6193 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6194 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006195 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006196 wl_fixed_to_double(src_height),
6197 wl_fixed_to_double(src_x),
6198 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006199 return;
6200 }
6201
6202 surface->pending.buffer_viewport.buffer.src_x = src_x;
6203 surface->pending.buffer_viewport.buffer.src_y = src_y;
6204 surface->pending.buffer_viewport.buffer.src_width = src_width;
6205 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006206 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006207}
6208
6209static void
6210viewport_set_destination(struct wl_client *client,
6211 struct wl_resource *resource,
6212 int32_t dst_width,
6213 int32_t dst_height)
6214{
6215 struct weston_surface *surface =
6216 wl_resource_get_user_data(resource);
6217
Pekka Paalanen4826f872016-04-22 14:14:38 +03006218 if (!surface) {
6219 wl_resource_post_error(resource,
6220 WP_VIEWPORT_ERROR_NO_SURFACE,
6221 "wl_surface for this viewport no longer exists");
6222 return;
6223 }
6224
6225 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006226
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006227 if (dst_width == -1 && dst_height == -1) {
6228 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006229 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006230 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006231 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006232 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006233
6234 if (dst_width <= 0 || dst_height <= 0) {
6235 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006236 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006237 "destination size must be positive (%dx%d)",
6238 dst_width, dst_height);
6239 return;
6240 }
6241
6242 surface->pending.buffer_viewport.surface.width = dst_width;
6243 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006244 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006245}
6246
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006247static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006248 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006249 viewport_set_source,
6250 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006251};
6252
6253static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006254viewporter_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 Paalanen9c5a0d92016-04-15 16:42:49 +03006261viewporter_get_viewport(struct wl_client *client,
6262 struct wl_resource *viewporter,
6263 uint32_t id,
6264 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006265{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006266 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006267 struct weston_surface *surface =
6268 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006269 struct wl_resource *resource;
6270
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006271 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006272 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006273 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006274 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006275 return;
6276 }
6277
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006278 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006279 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006280 if (resource == NULL) {
6281 wl_client_post_no_memory(client);
6282 return;
6283 }
6284
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006285 wl_resource_set_implementation(resource, &viewport_interface,
6286 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006287
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006288 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006289}
6290
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006291static const struct wp_viewporter_interface viewporter_interface = {
6292 viewporter_destroy,
6293 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006294};
6295
6296static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006297bind_viewporter(struct wl_client *client,
6298 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006299{
6300 struct wl_resource *resource;
6301
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006302 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006303 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006304 if (resource == NULL) {
6305 wl_client_post_no_memory(client);
6306 return;
6307 }
6308
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006309 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006310 NULL, NULL);
6311}
6312
6313static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006314destroy_presentation_feedback(struct wl_resource *feedback_resource)
6315{
6316 struct weston_presentation_feedback *feedback;
6317
6318 feedback = wl_resource_get_user_data(feedback_resource);
6319
6320 wl_list_remove(&feedback->link);
6321 free(feedback);
6322}
6323
6324static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006325presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6326{
6327 wl_resource_destroy(resource);
6328}
6329
6330static void
6331presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006332 struct wl_resource *presentation_resource,
6333 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006334 uint32_t callback)
6335{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006336 struct weston_surface *surface;
6337 struct weston_presentation_feedback *feedback;
6338
6339 surface = wl_resource_get_user_data(surface_resource);
6340
Bryce Harringtonde16d892014-11-20 22:21:57 -08006341 feedback = zalloc(sizeof *feedback);
6342 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006343 goto err_calloc;
6344
6345 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006346 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006347 1, callback);
6348 if (!feedback->resource)
6349 goto err_create;
6350
6351 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6352 destroy_presentation_feedback);
6353
6354 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6355
6356 return;
6357
6358err_create:
6359 free(feedback);
6360
6361err_calloc:
6362 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006363}
6364
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006365static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006366 presentation_destroy,
6367 presentation_feedback
6368};
6369
6370static void
6371bind_presentation(struct wl_client *client,
6372 void *data, uint32_t version, uint32_t id)
6373{
6374 struct weston_compositor *compositor = data;
6375 struct wl_resource *resource;
6376
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006377 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006378 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006379 if (resource == NULL) {
6380 wl_client_post_no_memory(client);
6381 return;
6382 }
6383
6384 wl_resource_set_implementation(resource, &presentation_implementation,
6385 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006386 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006387}
6388
6389static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006390compositor_bind(struct wl_client *client,
6391 void *data, uint32_t version, uint32_t id)
6392{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006393 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006394 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006395
Jason Ekstranda85118c2013-06-27 20:17:02 -05006396 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006397 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006398 if (resource == NULL) {
6399 wl_client_post_no_memory(client);
6400 return;
6401 }
6402
6403 wl_resource_set_implementation(resource, &compositor_interface,
6404 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006405}
6406
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006407WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006408weston_environment_get_fd(const char *env)
6409{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006410 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006411 int fd, flags;
6412
6413 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006414 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006415 return -1;
6416
6417 flags = fcntl(fd, F_GETFD);
6418 if (flags == -1)
6419 return -1;
6420
6421 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6422 unsetenv(env);
6423
6424 return fd;
6425}
6426
Pekka Paalanenb5026542014-11-12 15:09:24 +02006427static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006428timeline_key_binding_handler(struct weston_keyboard *keyboard,
6429 const struct timespec *time, uint32_t key,
6430 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006431{
6432 struct weston_compositor *compositor = data;
6433
6434 if (weston_timeline_enabled_)
6435 weston_timeline_close();
6436 else
6437 weston_timeline_open(compositor);
6438}
6439
Daniel Stonece62cb32018-07-20 09:46:24 +01006440static const char *
6441output_repaint_status_text(struct weston_output *output)
6442{
6443 switch (output->repaint_status) {
6444 case REPAINT_NOT_SCHEDULED:
6445 return "no repaint";
6446 case REPAINT_BEGIN_FROM_IDLE:
6447 return "start_repaint_loop scheduled";
6448 case REPAINT_SCHEDULED:
6449 return "repaint scheduled";
6450 case REPAINT_AWAITING_COMPLETION:
6451 return "awaiting completion";
6452 }
6453
6454 assert(!"output_repaint_status_text missing enum");
6455 return NULL;
6456}
6457
6458static void
6459debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6460{
6461 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6462 struct wl_shm_buffer *shm;
6463 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006464 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006465
6466 if (!buffer) {
6467 fprintf(fp, "\t\t[buffer not available]\n");
6468 return;
6469 }
6470
6471 shm = wl_shm_buffer_get(buffer->resource);
6472 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006473 uint32_t _format = wl_shm_buffer_get_format(shm);
6474 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006475 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006476 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6477 (unsigned long) _format,
6478 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006479 return;
6480 }
6481
6482 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6483 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006484 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006485 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006486 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6487 (unsigned long) dmabuf->attributes.format,
6488 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006489 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6490 (unsigned long long) dmabuf->attributes.modifier[0]);
6491 return;
6492 }
6493
6494 fprintf(fp, "\t\tEGL buffer");
6495}
6496
6497static void
6498debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6499{
6500 struct weston_compositor *ec = view->surface->compositor;
6501 struct weston_output *output;
6502 char desc[512];
6503 pixman_box32_t *box;
6504 uint32_t surface_id = 0;
6505 pid_t pid = 0;
6506
6507 if (view->surface->resource) {
6508 struct wl_resource *resource = view->surface->resource;
6509 wl_client_get_credentials(wl_resource_get_client(resource),
6510 &pid, NULL, NULL);
6511 surface_id = wl_resource_get_id(view->surface->resource);
6512 }
6513
6514 if (!view->surface->get_label ||
6515 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6516 strcpy(desc, "[no description available]");
6517 }
6518 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6519 view_idx, view->surface->role_name, pid, surface_id,
6520 desc, view);
6521
6522 box = pixman_region32_extents(&view->transform.boundingbox);
6523 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6524 box->x1, box->y1, box->x2, box->y2);
6525 box = pixman_region32_extents(&view->transform.opaque);
6526
6527 if (pixman_region32_equal(&view->transform.opaque,
6528 &view->transform.boundingbox)) {
6529 fprintf(fp, "\t\t[fully opaque]\n");
6530 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6531 fprintf(fp, "\t\t[not opaque]\n");
6532 } else {
6533 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6534 box->x1, box->y1, box->x2, box->y2);
6535 }
6536
6537 if (view->alpha < 1.0)
6538 fprintf(fp, "\t\talpha: %f\n", view->alpha);
6539
6540 if (view->output_mask != 0) {
6541 bool first_output = true;
6542 fprintf(fp, "\t\toutputs: ");
6543 wl_list_for_each(output, &ec->output_list, link) {
6544 if (!(view->output_mask & (1 << output->id)))
6545 continue;
6546 fprintf(fp, "%s%d (%s)%s",
6547 (first_output) ? "" : ", ",
6548 output->id, output->name,
6549 (view->output == output) ? " (primary)" : "");
6550 first_output = false;
6551 }
6552 } else {
6553 fprintf(fp, "\t\t[no outputs]");
6554 }
6555
6556 fprintf(fp, "\n");
6557
6558 debug_scene_view_print_buffer(fp, view);
6559}
6560
6561/**
6562 * Output information on how libweston is currently composing the scene
6563 * graph.
6564 */
6565WL_EXPORT char *
6566weston_compositor_print_scene_graph(struct weston_compositor *ec)
6567{
6568 struct weston_output *output;
6569 struct weston_layer *layer;
6570 struct timespec now;
6571 int layer_idx = 0;
6572 FILE *fp;
6573 char *ret;
6574 size_t len;
6575 int err;
6576
6577 fp = open_memstream(&ret, &len);
6578 assert(fp);
6579
6580 weston_compositor_read_presentation_clock(ec, &now);
6581 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
6582 now.tv_sec, now.tv_nsec);
6583
6584 wl_list_for_each(output, &ec->output_list, link) {
6585 struct weston_head *head;
6586 int head_idx = 0;
6587
6588 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
6589 assert(output->enabled);
6590
6591 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
6592 output->x, output->y,
6593 output->x + output->width,
6594 output->y + output->height);
6595 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
6596 output->current_mode->width,
6597 output->current_mode->height,
6598 output->current_mode->refresh / 1000.0);
6599 fprintf(fp, "\tscale: %d\n", output->scale);
6600
6601 fprintf(fp, "\trepaint status: %s\n",
6602 output_repaint_status_text(output));
6603 if (output->repaint_status == REPAINT_SCHEDULED)
6604 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
6605 output->next_repaint.tv_sec,
6606 output->next_repaint.tv_nsec);
6607
6608 wl_list_for_each(head, &output->head_list, output_link) {
6609 fprintf(fp, "\tHead %d (%s): %sconnected\n",
6610 head_idx++, head->name,
6611 (head->connected) ? "" : "not ");
6612 }
6613 }
6614
6615 fprintf(fp, "\n");
6616
6617 wl_list_for_each(layer, &ec->layer_list, link) {
6618 struct weston_view *view;
6619 int view_idx = 0;
6620
6621 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
6622 (unsigned long) layer->position);
6623
6624 if (!weston_layer_mask_is_infinite(layer)) {
6625 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
6626 layer->mask.x1, layer->mask.y1,
6627 layer->mask.x2, layer->mask.y2);
6628 }
6629
6630 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
6631 debug_scene_view_print(fp, view, view_idx++);
6632
6633 if (wl_list_empty(&layer->view_list.link))
6634 fprintf(fp, "\t[no views]\n");
6635
6636 fprintf(fp, "\n");
6637 }
6638
6639 err = fclose(fp);
6640 assert(err == 0);
6641
6642 return ret;
6643}
6644
6645/**
6646 * Called when the 'scene-graph' debug scope is bound by a client. This
6647 * one-shot weston-debug scope prints the current scene graph when bound,
6648 * and then terminates the stream.
6649 */
6650static void
6651debug_scene_graph_cb(struct weston_debug_stream *stream, void *data)
6652{
6653 struct weston_compositor *ec = data;
6654 char *str = weston_compositor_print_scene_graph(ec);
6655
6656 weston_debug_stream_printf(stream, "%s", str);
6657 free(str);
6658 weston_debug_stream_complete(stream);
6659}
6660
Giulio Camuffo459137b2014-10-11 23:56:24 +03006661/** Create the compositor.
6662 *
6663 * This functions creates and initializes a compositor instance.
6664 *
6665 * \param display The Wayland display to be used.
6666 * \param user_data A pointer to an object that can later be retrieved
6667 * using the \ref weston_compositor_get_user_data function.
6668 * \return The compositor instance on success or NULL on failure.
6669 */
6670WL_EXPORT struct weston_compositor *
6671weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006672{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006673 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006674 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006675
Giulio Camuffo459137b2014-10-11 23:56:24 +03006676 ec = zalloc(sizeof *ec);
6677 if (!ec)
6678 return NULL;
6679
6680 ec->wl_display = display;
6681 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006682 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006683 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006684 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006685 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006686 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006687 wl_signal_init(&ec->idle_signal);
6688 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006689 wl_signal_init(&ec->show_input_panel_signal);
6690 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006691 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006692 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006693 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006694 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006695 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006696 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006697 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02006698 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006699 wl_signal_init(&ec->session_signal);
6700 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006701
Casey Dahlin58ba1372012-04-19 22:50:08 -04006702 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006703 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006704
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006705 ec->activate_serial = 1;
6706
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05006707 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
6708
Derek Foreman152254b2015-11-26 14:17:48 -06006709 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006710 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006711 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006712
Giulio Camuffo954f1832014-10-11 18:27:30 +03006713 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006714 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006715 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006716
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006717 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006718 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006719 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006720
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006721 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006722 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006723 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006724
Pekka Paalanena5630ea2017-10-12 13:13:42 +02006725 if (weston_debug_compositor_create(ec) < 0)
6726 goto fail;
6727
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006728 if (weston_input_init(ec) != 0)
6729 goto fail;
6730
Jason Ekstranda7af7042013-10-12 22:38:11 -05006731 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006732 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006733 wl_list_init(&ec->layer_list);
6734 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006735 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006736 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006737 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006738 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006739 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006740 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006741 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006742 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006743 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006744
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006745 wl_list_init(&ec->plugin_api_list);
6746
Xiong Zhang97116532013-10-23 13:58:31 +08006747 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006748 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006749
Giulio Camuffo459137b2014-10-11 23:56:24 +03006750 wl_data_device_manager_init(ec->wl_display);
6751
6752 wl_display_init_shm(ec->wl_display);
6753
6754 loop = wl_display_get_event_loop(ec->wl_display);
6755 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006756 ec->repaint_timer =
6757 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6758 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006759
Quentin Glidic82681572016-12-17 13:40:51 +01006760 weston_layer_init(&ec->fade_layer, ec);
6761 weston_layer_init(&ec->cursor_layer, ec);
6762
6763 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6764 weston_layer_set_position(&ec->cursor_layer,
6765 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006766
6767 weston_compositor_add_debug_binding(ec, KEY_T,
6768 timeline_key_binding_handler, ec);
6769
Daniel Stonece62cb32018-07-20 09:46:24 +01006770 ec->debug_scene =
6771 weston_compositor_add_debug_scope(ec, "scene-graph",
6772 "Scene graph details\n",
6773 debug_scene_graph_cb,
6774 ec);
6775
Giulio Camuffo459137b2014-10-11 23:56:24 +03006776 return ec;
6777
6778fail:
6779 free(ec);
6780 return NULL;
6781}
6782
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006783WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006784weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006785{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006786 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006787
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006788 wl_event_source_remove(ec->idle_source);
6789
Matt Roper361d2ad2011-08-29 13:52:23 -07006790 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006791 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006792 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006793
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006794 /* Destroy all pending outputs associated with this compositor */
6795 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6796 output->destroy(output);
6797
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006798 if (ec->renderer)
6799 ec->renderer->destroy(ec);
6800
Daniel Stone325fc2d2012-05-30 16:31:58 +01006801 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006802 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006803 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006804 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006805 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006806 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006807
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006808 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006809}
6810
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006811WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006812weston_compositor_exit_with_code(struct weston_compositor *compositor,
6813 int exit_code)
6814{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006815 if (compositor->exit_code == EXIT_SUCCESS)
6816 compositor->exit_code = exit_code;
6817
Giulio Camuffo459137b2014-10-11 23:56:24 +03006818 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006819}
6820
6821WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006822weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6823 const struct weston_pointer_grab_interface *interface)
6824{
6825 struct weston_seat *seat;
6826
6827 ec->default_pointer_grab = interface;
6828 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006829 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6830
6831 if (pointer)
6832 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006833 }
6834}
6835
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006836WL_EXPORT int
6837weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6838 clockid_t clk_id)
6839{
6840 struct timespec ts;
6841
6842 if (clock_gettime(clk_id, &ts) < 0)
6843 return -1;
6844
6845 compositor->presentation_clock = clk_id;
6846
6847 return 0;
6848}
6849
6850/*
6851 * For choosing the software clock, when the display hardware or API
6852 * does not expose a compatible presentation timestamp.
6853 */
6854WL_EXPORT int
6855weston_compositor_set_presentation_clock_software(
6856 struct weston_compositor *compositor)
6857{
6858 /* In order of preference */
6859 static const clockid_t clocks[] = {
6860 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6861 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6862 CLOCK_MONOTONIC, /* no jumps, may crawl */
6863 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
6864 CLOCK_REALTIME /* may jump and crawl */
6865 };
6866 unsigned i;
6867
6868 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
6869 if (weston_compositor_set_presentation_clock(compositor,
6870 clocks[i]) == 0)
6871 return 0;
6872
6873 weston_log("Error: no suitable presentation clock available.\n");
6874
6875 return -1;
6876}
6877
Pekka Paalanen662f3842015-03-18 12:17:26 +02006878/** Read the current time from the Presentation clock
6879 *
6880 * \param compositor
6881 * \param ts[out] The current time.
6882 *
6883 * \note Reading the current time in user space is always imprecise to some
6884 * degree.
6885 *
6886 * This function is never meant to fail. If reading the clock does fail,
6887 * an error message is logged and a zero time is returned. Callers are not
6888 * supposed to detect or react to failures.
6889 */
6890WL_EXPORT void
6891weston_compositor_read_presentation_clock(
6892 const struct weston_compositor *compositor,
6893 struct timespec *ts)
6894{
6895 static bool warned;
6896 int ret;
6897
6898 ret = clock_gettime(compositor->presentation_clock, ts);
6899 if (ret < 0) {
6900 ts->tv_sec = 0;
6901 ts->tv_nsec = 0;
6902
6903 if (!warned)
6904 weston_log("Error: failure to read "
6905 "the presentation clock %#x: '%m' (%d)\n",
6906 compositor->presentation_clock, errno);
6907 warned = true;
6908 }
6909}
6910
Pekka Paalanen230f3b12014-09-29 14:18:40 -04006911/** Import dmabuf buffer into current renderer
6912 *
6913 * \param compositor
6914 * \param buffer the dmabuf buffer to import
6915 * \return true on usable buffers, false otherwise
6916 *
6917 * This function tests that the linux_dmabuf_buffer is usable
6918 * for the current renderer. Returns false on unusable buffers. Usually
6919 * usability is tested by importing the dmabufs for composition.
6920 *
6921 * This hook is also used for detecting if the renderer supports
6922 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
6923 * supported.
6924 * */
6925WL_EXPORT bool
6926weston_compositor_import_dmabuf(struct weston_compositor *compositor,
6927 struct linux_dmabuf_buffer *buffer)
6928{
6929 struct weston_renderer *renderer;
6930
6931 renderer = compositor->renderer;
6932
6933 if (renderer->import_dmabuf == NULL)
6934 return false;
6935
6936 return renderer->import_dmabuf(compositor, buffer);
6937}
6938
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006939WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006940weston_version(int *major, int *minor, int *micro)
6941{
6942 *major = WESTON_VERSION_MAJOR;
6943 *minor = WESTON_VERSION_MINOR;
6944 *micro = WESTON_VERSION_MICRO;
6945}
6946
Daniel Stonee03c1112016-11-24 20:45:45 +00006947/**
6948 * Attempts to find a module path from the module map specified in the
6949 * environment. If found, writes the full path into the path variable.
6950 *
6951 * The module map is a string in environment variable WESTON_MODULE_MAP, where
6952 * each entry is of the form "name=path" and entries are separated by
6953 * semicolons. Whitespace is significant.
6954 *
6955 * \param name The name to search for.
6956 * \param path Where the path is written to if found.
6957 * \param path_len Allocated bytes at \c path .
6958 * \returns The length of the string written to path on success, or 0 if the
6959 * module was not specified in the environment map or path_len was too small.
6960 */
6961WL_EXPORT size_t
6962weston_module_path_from_env(const char *name, char *path, size_t path_len)
6963{
6964 const char *mapping = getenv("WESTON_MODULE_MAP");
6965 const char *end;
6966 const int name_len = strlen(name);
6967
6968 if (!mapping)
6969 return 0;
6970
6971 end = mapping + strlen(mapping);
6972 while (mapping < end && *mapping) {
6973 const char *filename, *next;
6974
6975 /* early out: impossibly short string */
6976 if (end - mapping < name_len + 1)
6977 return 0;
6978
6979 filename = &mapping[name_len + 1];
6980 next = strchrnul(mapping, ';');
6981
6982 if (strncmp(mapping, name, name_len) == 0 &&
6983 mapping[name_len] == '=') {
6984 size_t file_len = next - filename; /* no trailing NUL */
6985 if (file_len >= path_len)
6986 return 0;
6987 strncpy(path, filename, file_len);
6988 path[file_len] = '\0';
6989 return file_len;
6990 }
6991
6992 mapping = next + 1;
6993 }
6994
6995 return 0;
6996}
6997
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03006998WL_EXPORT void *
6999weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007000{
7001 char path[PATH_MAX];
7002 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007003 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007004
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007005 if (name == NULL)
7006 return NULL;
7007
Derek Foreman3f86e502015-06-08 11:46:54 -05007008 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007009 len = weston_module_path_from_env(name, path, sizeof path);
7010 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007011 len = snprintf(path, sizeof path, "%s/%s",
7012 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007013 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007014 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007015 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007016
Daniel Stonebeb97e52016-11-28 12:13:54 +00007017 /* snprintf returns the length of the string it would've written,
7018 * _excluding_ the NUL byte. So even being equal to the size of
7019 * our buffer is an error here. */
7020 if (len >= sizeof path)
7021 return NULL;
7022
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007023 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7024 if (module) {
7025 weston_log("Module '%s' already loaded\n", path);
7026 dlclose(module);
7027 return NULL;
7028 }
7029
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007030 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007031 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007032 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007033 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007034 return NULL;
7035 }
7036
7037 init = dlsym(module, entrypoint);
7038 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007039 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007040 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007041 return NULL;
7042 }
7043
7044 return init;
7045}
7046
Giulio Camuffo459137b2014-10-11 23:56:24 +03007047
7048/** Destroys the compositor.
7049 *
7050 * This function cleans up the compositor state and destroys it.
7051 *
7052 * \param compositor The compositor to be destroyed.
7053 */
7054WL_EXPORT void
7055weston_compositor_destroy(struct weston_compositor *compositor)
7056{
7057 /* prevent further rendering while shutting down */
7058 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7059
7060 wl_signal_emit(&compositor->destroy_signal, compositor);
7061
7062 weston_compositor_xkb_destroy(compositor);
7063
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007064 if (compositor->backend)
7065 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007066
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007067 /* The backend is responsible for destroying the heads. */
7068 assert(wl_list_empty(&compositor->head_list));
7069
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007070 weston_plugin_api_destroy_list(compositor);
7071
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007072 if (compositor->heads_changed_source)
7073 wl_event_source_remove(compositor->heads_changed_source);
7074
Daniel Stonece62cb32018-07-20 09:46:24 +01007075 weston_debug_scope_destroy(compositor->debug_scene);
7076 compositor->debug_scene = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007077 weston_debug_compositor_destroy(compositor);
7078
Giulio Camuffo459137b2014-10-11 23:56:24 +03007079 free(compositor);
7080}
7081
7082/** Instruct the compositor to exit.
7083 *
7084 * This functions does not directly destroy the compositor object, it merely
7085 * command it to start the tear down process. It is not guaranteed that the
7086 * tear down will happen immediately.
7087 *
7088 * \param compositor The compositor to tear down.
7089 */
7090WL_EXPORT void
7091weston_compositor_exit(struct weston_compositor *compositor)
7092{
7093 compositor->exit(compositor);
7094}
7095
7096/** Return the user data stored in the compositor.
7097 *
7098 * This function returns the user data pointer set with user_data parameter
7099 * to the \ref weston_compositor_create function.
7100 */
7101WL_EXPORT void *
7102weston_compositor_get_user_data(struct weston_compositor *compositor)
7103{
7104 return compositor->user_data;
7105}
Pekka Paalanendd186732016-06-03 14:49:54 +03007106
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007107static const char * const backend_map[] = {
7108 [WESTON_BACKEND_DRM] = "drm-backend.so",
7109 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7110 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7111 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7112 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7113 [WESTON_BACKEND_X11] = "x11-backend.so",
7114};
7115
Pekka Paalanendd186732016-06-03 14:49:54 +03007116/** Load a backend into a weston_compositor
7117 *
7118 * A backend must be loaded to make a weston_compositor work. A backend
7119 * provides input and output capabilities, and determines the renderer to use.
7120 *
7121 * \param compositor A compositor that has not had a backend loaded yet.
7122 * \param backend Name of the backend file.
7123 * \param config_base A pointer to a backend-specific configuration
7124 * structure's 'base' member.
7125 *
7126 * \return 0 on success, or -1 on error.
7127 */
7128WL_EXPORT int
7129weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007130 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007131 struct weston_backend_config *config_base)
7132{
7133 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007134 struct weston_backend_config *config_base);
7135
Pekka Paalanend7e35112017-08-29 17:04:12 +03007136 if (compositor->backend) {
7137 weston_log("Error: attempt to load a backend when one is already loaded\n");
7138 return -1;
7139 }
7140
Quentin Glidic887c0182016-07-10 11:00:53 +02007141 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007142 return -1;
7143
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007144 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007145 if (!backend_init)
7146 return -1;
7147
Pekka Paalanend7e35112017-08-29 17:04:12 +03007148 if (backend_init(compositor, config_base) < 0) {
7149 compositor->backend = NULL;
7150 return -1;
7151 }
7152
7153 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007154}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007155
7156WL_EXPORT int
7157weston_compositor_load_xwayland(struct weston_compositor *compositor)
7158{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007159 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007160
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007161 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007162 if (!module_init)
7163 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007164 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007165 return -1;
7166 return 0;
7167}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007168
7169/** Resolve an internal compositor error by disconnecting the client.
7170 *
7171 * This function is used in cases when the wl_buffer turns out
7172 * unusable and there is no fallback path.
7173 *
7174 * It is possible the fault is caused by a compositor bug, the underlying
7175 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7176 * In any case, the options are to either composite garbage or nothing,
7177 * or disconnect the client. This is a helper function for the latter.
7178 *
7179 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7180 *
7181 * \param buffer The weston buffer that is unusable.
7182 * \param msg A custom error message attached to the protocol error.
7183 */
7184WL_EXPORT void
7185weston_buffer_send_server_error(struct weston_buffer *buffer,
7186 const char *msg)
7187{
7188 struct wl_client *client;
7189 struct wl_resource *display_resource;
7190 uint32_t id;
7191
7192 assert(buffer->resource);
7193 id = wl_resource_get_id(buffer->resource);
7194 client = wl_resource_get_client(buffer->resource);
7195 display_resource = wl_client_get_object(client, 1);
7196
7197 assert(display_resource);
7198 wl_resource_post_error(display_resource,
7199 WL_DISPLAY_ERROR_INVALID_OBJECT,
7200 "server error with "
7201 "wl_buffer@%u: %s", id, msg);
7202}