blob: 2ca3da3b461de17e89f28c31382e910960cc9b39 [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>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050054
Pekka Paalanenb5026542014-11-12 15:09:24 +020055#include "timeline.h"
56
Kristian Høgsberg82863022010-06-04 21:52:02 -040057#include "compositor.h"
Daniel Stonece62cb32018-07-20 09:46:24 +010058#include "weston-debug.h"
59#include "linux-dmabuf.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030060#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020061#include "presentation-time-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070062#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070063#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070064#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030065#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040066#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050067#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020068#include "plugin-registry.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050069
Pekka Paalanen0513a952014-05-21 16:17:27 +030070#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
71
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020072static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030073weston_output_update_matrix(struct weston_output *output);
74
75static void
Alexander Larsson0b135062013-05-28 16:23:36 +020076weston_output_transform_scale_init(struct weston_output *output,
77 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020078
Rob Bradford27b17932013-06-26 18:08:46 +010079static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050080weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010081
Pekka Paalanendcac3512017-12-08 14:13:34 +020082static char *
83weston_output_create_heads_string(struct weston_output *output);
84
Pekka Paalanen6528c032017-03-28 15:27:10 +030085/** Send wl_output events for mode and scale changes
86 *
87 * \param head Send on all resources bound to this head.
88 * \param mode_changed If true, send the current mode.
89 * \param scale_changed If true, send the current scale.
90 */
91static void
92weston_mode_switch_send_events(struct weston_head *head,
93 bool mode_changed, bool scale_changed)
94{
95 struct weston_output *output = head->output;
96 struct wl_resource *resource;
97 int version;
98
99 wl_resource_for_each(resource, &head->resource_list) {
100 if (mode_changed) {
101 wl_output_send_mode(resource,
102 output->current_mode->flags,
103 output->current_mode->width,
104 output->current_mode->height,
105 output->current_mode->refresh);
106 }
107
108 version = wl_resource_get_version(resource);
109 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
110 wl_output_send_scale(resource, output->current_scale);
111
112 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
113 wl_output_send_done(resource);
114 }
115}
116
117static void
118weston_mode_switch_finish(struct weston_output *output,
119 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800120{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200121 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300122 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200123 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200124
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200125 pixman_region32_init(&old_output_region);
126 pixman_region32_copy(&old_output_region, &output->region);
127
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200128 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200129 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200130
131 pixman_region32_init(&output->previous_damage);
132 pixman_region32_init_rect(&output->region, output->x, output->y,
133 output->width, output->height);
134
135 weston_output_update_matrix(output);
136
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200137 /* If a pointer falls outside the outputs new geometry, move it to its
138 * lower-right corner */
139 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500140 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200141 int32_t x, y;
142
143 if (!pointer)
144 continue;
145
146 x = wl_fixed_to_int(pointer->x);
147 y = wl_fixed_to_int(pointer->y);
148
149 if (!pixman_region32_contains_point(&old_output_region,
150 x, y, NULL) ||
151 pixman_region32_contains_point(&output->region,
152 x, y, NULL))
153 continue;
154
155 if (x >= output->x + output->width)
156 x = output->x + output->width - 1;
157 if (y >= output->y + output->height)
158 y = output->y + output->height - 1;
159
160 pointer->x = wl_fixed_from_int(x);
161 pointer->y = wl_fixed_from_int(y);
162 }
163
164 pixman_region32_fini(&old_output_region);
165
Derek Foremandd4cd332014-11-10 10:29:59 -0600166 if (!mode_changed && !scale_changed)
167 return;
168
Hardening57388e42013-09-18 23:56:36 +0200169 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300170 wl_list_for_each(head, &output->head_list, output_link)
171 weston_mode_switch_send_events(head,
172 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600173}
174
David Fort0de859e2016-05-27 23:22:57 +0200175static void
176weston_compositor_reflow_outputs(struct weston_compositor *compositor,
177 struct weston_output *resized_output, int delta_width);
178
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600179WL_EXPORT int
180weston_output_mode_set_native(struct weston_output *output,
181 struct weston_mode *mode,
182 int32_t scale)
183{
184 int ret;
185 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200186 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600187
188 if (!output->switch_mode)
189 return -1;
190
191 if (!output->original_mode) {
192 mode_changed = 1;
193 ret = output->switch_mode(output, mode);
194 if (ret < 0)
195 return ret;
196 if (output->current_scale != scale) {
197 scale_changed = 1;
198 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200199 }
200 }
201
David Fort0de859e2016-05-27 23:22:57 +0200202 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600203 output->native_mode = mode;
204 output->native_scale = scale;
205
206 weston_mode_switch_finish(output, mode_changed, scale_changed);
207
David Fort0de859e2016-05-27 23:22:57 +0200208 if (mode_changed || scale_changed) {
209 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
210
211 wl_signal_emit(&output->compositor->output_resized_signal, output);
212 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600213 return 0;
214}
215
216WL_EXPORT int
217weston_output_mode_switch_to_native(struct weston_output *output)
218{
219 int ret;
220 int mode_changed = 0, scale_changed = 0;
221
222 if (!output->switch_mode)
223 return -1;
224
225 if (!output->original_mode) {
226 weston_log("already in the native mode\n");
227 return -1;
228 }
229 /* the non fullscreen clients haven't seen a mode set since we
230 * switched into a temporary, so we need to notify them if the
231 * mode at that time is different from the native mode now.
232 */
233 mode_changed = (output->original_mode != output->native_mode);
234 scale_changed = (output->original_scale != output->native_scale);
235
236 ret = output->switch_mode(output, output->native_mode);
237 if (ret < 0)
238 return ret;
239
240 output->current_scale = output->native_scale;
241
242 output->original_mode = NULL;
243 output->original_scale = 0;
244
245 weston_mode_switch_finish(output, mode_changed, scale_changed);
246
247 return 0;
248}
249
250WL_EXPORT int
251weston_output_mode_switch_to_temporary(struct weston_output *output,
252 struct weston_mode *mode,
253 int32_t scale)
254{
255 int ret;
256
257 if (!output->switch_mode)
258 return -1;
259
260 /* original_mode is the last mode non full screen clients have seen,
261 * so we shouldn't change it if we already have one set.
262 */
263 if (!output->original_mode) {
264 output->original_mode = output->native_mode;
265 output->original_scale = output->native_scale;
266 }
267 ret = output->switch_mode(output, mode);
268 if (ret < 0)
269 return ret;
270
271 output->current_scale = scale;
272
273 weston_mode_switch_finish(output, 0, 0);
274
275 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800276}
277
Benjamin Franzke06286262011-05-06 19:12:33 +0200278static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300279region_init_infinite(pixman_region32_t *region)
280{
281 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
282 UINT32_MAX, UINT32_MAX);
283}
284
Pekka Paalanene67858b2013-04-25 13:57:42 +0300285static struct weston_subsurface *
286weston_surface_to_subsurface(struct weston_surface *surface);
287
Jason Ekstranda7af7042013-10-12 22:38:11 -0500288WL_EXPORT struct weston_view *
289weston_view_create(struct weston_surface *surface)
290{
291 struct weston_view *view;
292
Bryce Harringtonde16d892014-11-20 22:21:57 -0800293 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500294 if (view == NULL)
295 return NULL;
296
297 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000298 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500299
Jason Ekstranda7af7042013-10-12 22:38:11 -0500300 /* Assign to surface */
301 wl_list_insert(&surface->views, &view->surface_link);
302
303 wl_signal_init(&view->destroy_signal);
304 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300305 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500306
Jason Ekstranda7af7042013-10-12 22:38:11 -0500307 pixman_region32_init(&view->clip);
308
309 view->alpha = 1.0;
310 pixman_region32_init(&view->transform.opaque);
311
312 wl_list_init(&view->geometry.transformation_list);
313 wl_list_insert(&view->geometry.transformation_list,
314 &view->transform.position.link);
315 weston_matrix_init(&view->transform.position.matrix);
316 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200317 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500318 pixman_region32_init(&view->transform.boundingbox);
319 view->transform.dirty = 1;
320
Jason Ekstranda7af7042013-10-12 22:38:11 -0500321 return view;
322}
323
Jason Ekstrand108865d2014-06-26 10:04:49 -0700324struct weston_frame_callback {
325 struct wl_resource *resource;
326 struct wl_list link;
327};
328
Pekka Paalanen133e4392014-09-23 22:08:46 -0400329struct weston_presentation_feedback {
330 struct wl_resource *resource;
331
332 /* XXX: could use just wl_resource_get_link() instead */
333 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200334
335 /* The per-surface feedback flags */
336 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400337};
338
339static void
340weston_presentation_feedback_discard(
341 struct weston_presentation_feedback *feedback)
342{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200343 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400344 wl_resource_destroy(feedback->resource);
345}
346
347static void
348weston_presentation_feedback_discard_list(struct wl_list *list)
349{
350 struct weston_presentation_feedback *feedback, *tmp;
351
352 wl_list_for_each_safe(feedback, tmp, list, link)
353 weston_presentation_feedback_discard(feedback);
354}
355
356static void
357weston_presentation_feedback_present(
358 struct weston_presentation_feedback *feedback,
359 struct weston_output *output,
360 uint32_t refresh_nsec,
361 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200362 uint64_t seq,
363 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400364{
365 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300366 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400367 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200368 uint32_t tv_sec_hi;
369 uint32_t tv_sec_lo;
370 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300371 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400372
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300373 wl_list_for_each(head, &output->head_list, output_link) {
374 wl_resource_for_each(o, &head->resource_list) {
375 if (wl_resource_get_client(o) != client)
376 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400377
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300378 wp_presentation_feedback_send_sync_output(feedback->resource, o);
379 done = true;
380 }
381
382 /* For clone mode, send it for just one wl_output global,
383 * they are all equivalent anyway.
384 */
385 if (done)
386 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400387 }
388
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200389 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200390 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200391 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200392 refresh_nsec,
393 seq >> 32, seq & 0xffffffff,
394 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400395 wl_resource_destroy(feedback->resource);
396}
397
398static void
399weston_presentation_feedback_present_list(struct wl_list *list,
400 struct weston_output *output,
401 uint32_t refresh_nsec,
402 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200403 uint64_t seq,
404 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400405{
406 struct weston_presentation_feedback *feedback, *tmp;
407
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200408 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200409 wl_list_empty(list));
410
Pekka Paalanen133e4392014-09-23 22:08:46 -0400411 wl_list_for_each_safe(feedback, tmp, list, link)
412 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200413 refresh_nsec, ts, seq,
414 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400415}
416
Jason Ekstrand7b982072014-05-20 14:33:03 -0500417static void
418surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
419{
420 struct weston_surface_state *state =
421 container_of(listener, struct weston_surface_state,
422 buffer_destroy_listener);
423
424 state->buffer = NULL;
425}
426
427static void
428weston_surface_state_init(struct weston_surface_state *state)
429{
430 state->newly_attached = 0;
431 state->buffer = NULL;
432 state->buffer_destroy_listener.notify =
433 surface_state_handle_buffer_destroy;
434 state->sx = 0;
435 state->sy = 0;
436
Derek Foreman152254b2015-11-26 14:17:48 -0600437 pixman_region32_init(&state->damage_surface);
438 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500439 pixman_region32_init(&state->opaque);
440 region_init_infinite(&state->input);
441
442 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400443 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500444
445 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
446 state->buffer_viewport.buffer.scale = 1;
447 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
448 state->buffer_viewport.surface.width = -1;
449 state->buffer_viewport.changed = 0;
450}
451
452static void
453weston_surface_state_fini(struct weston_surface_state *state)
454{
455 struct weston_frame_callback *cb, *next;
456
457 wl_list_for_each_safe(cb, next,
458 &state->frame_callback_list, link)
459 wl_resource_destroy(cb->resource);
460
Pekka Paalanen133e4392014-09-23 22:08:46 -0400461 weston_presentation_feedback_discard_list(&state->feedback_list);
462
Jason Ekstrand7b982072014-05-20 14:33:03 -0500463 pixman_region32_fini(&state->input);
464 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600465 pixman_region32_fini(&state->damage_surface);
466 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500467
468 if (state->buffer)
469 wl_list_remove(&state->buffer_destroy_listener.link);
470 state->buffer = NULL;
471}
472
473static void
474weston_surface_state_set_buffer(struct weston_surface_state *state,
475 struct weston_buffer *buffer)
476{
477 if (state->buffer == buffer)
478 return;
479
480 if (state->buffer)
481 wl_list_remove(&state->buffer_destroy_listener.link);
482 state->buffer = buffer;
483 if (state->buffer)
484 wl_signal_add(&state->buffer->destroy_signal,
485 &state->buffer_destroy_listener);
486}
487
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500488WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500489weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500490{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500491 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400492
Bryce Harringtonde16d892014-11-20 22:21:57 -0800493 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400494 if (surface == NULL)
495 return NULL;
496
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500497 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800498 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500499
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500500 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200501 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400502
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200503 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
504 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200505 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
506 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500507
508 weston_surface_state_init(&surface->pending);
509
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400510 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500511 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300512 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400513
Jason Ekstranda7af7042013-10-12 22:38:11 -0500514 wl_list_init(&surface->views);
515
516 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400517 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500518
Pekka Paalanene67858b2013-04-25 13:57:42 +0300519 wl_list_init(&surface->subsurface_list);
520 wl_list_init(&surface->subsurface_list_pending);
521
Jason Ekstrand1e059042014-10-16 10:55:19 -0500522 weston_matrix_init(&surface->buffer_to_surface_matrix);
523 weston_matrix_init(&surface->surface_to_buffer_matrix);
524
Jonas Ådahld3414f22016-07-22 17:56:31 +0800525 wl_list_init(&surface->pointer_constraints);
526
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400527 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500528}
529
Alex Wu8811bf92012-02-28 18:07:54 +0800530WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500531weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200532 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500533{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100534 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500535}
536
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400537WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500538weston_view_to_global_float(struct weston_view *view,
539 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200540{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500541 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200542 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
543
Jason Ekstranda7af7042013-10-12 22:38:11 -0500544 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200545
546 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200547 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700548 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200549 v.f[3]);
550 *x = 0;
551 *y = 0;
552 return;
553 }
554
555 *x = v.f[0] / v.f[3];
556 *y = v.f[1] / v.f[3];
557 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500558 *x = sx + view->geometry.x;
559 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200560 }
561}
562
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500563WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200564weston_transformed_coord(int width, int height,
565 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200566 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200567 float sx, float sy, float *bx, float *by)
568{
569 switch (transform) {
570 case WL_OUTPUT_TRANSFORM_NORMAL:
571 default:
572 *bx = sx;
573 *by = sy;
574 break;
575 case WL_OUTPUT_TRANSFORM_FLIPPED:
576 *bx = width - sx;
577 *by = sy;
578 break;
579 case WL_OUTPUT_TRANSFORM_90:
580 *bx = height - sy;
581 *by = sx;
582 break;
583 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
584 *bx = height - sy;
585 *by = width - sx;
586 break;
587 case WL_OUTPUT_TRANSFORM_180:
588 *bx = width - sx;
589 *by = height - sy;
590 break;
591 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
592 *bx = sx;
593 *by = height - sy;
594 break;
595 case WL_OUTPUT_TRANSFORM_270:
596 *bx = sy;
597 *by = width - sx;
598 break;
599 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
600 *bx = sy;
601 *by = sx;
602 break;
603 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200604
605 *bx *= scale;
606 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200607}
608
609WL_EXPORT pixman_box32_t
610weston_transformed_rect(int width, int height,
611 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200612 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200613 pixman_box32_t rect)
614{
615 float x1, x2, y1, y2;
616
617 pixman_box32_t ret;
618
Alexander Larsson4ea95522013-05-22 14:41:37 +0200619 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200620 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200621 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200622 rect.x2, rect.y2, &x2, &y2);
623
624 if (x1 <= x2) {
625 ret.x1 = x1;
626 ret.x2 = x2;
627 } else {
628 ret.x1 = x2;
629 ret.x2 = x1;
630 }
631
632 if (y1 <= y2) {
633 ret.y1 = y1;
634 ret.y2 = y2;
635 } else {
636 ret.y1 = y2;
637 ret.y2 = y1;
638 }
639
640 return ret;
641}
642
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600643/** Transform a region by a matrix, restricted to axis-aligned transformations
644 *
645 * Warning: This function does not work for projective, affine, or matrices
646 * that encode arbitrary rotations. Only 90-degree step rotations are
647 * supported.
648 */
649WL_EXPORT void
650weston_matrix_transform_region(pixman_region32_t *dest,
651 struct weston_matrix *matrix,
652 pixman_region32_t *src)
653{
654 pixman_box32_t *src_rects, *dest_rects;
655 int nrects, i;
656
657 src_rects = pixman_region32_rectangles(src, &nrects);
658 dest_rects = malloc(nrects * sizeof(*dest_rects));
659 if (!dest_rects)
660 return;
661
662 for (i = 0; i < nrects; i++) {
663 struct weston_vector vec1 = {{
664 src_rects[i].x1, src_rects[i].y1, 0, 1
665 }};
666 weston_matrix_transform(matrix, &vec1);
667 vec1.f[0] /= vec1.f[3];
668 vec1.f[1] /= vec1.f[3];
669
670 struct weston_vector vec2 = {{
671 src_rects[i].x2, src_rects[i].y2, 0, 1
672 }};
673 weston_matrix_transform(matrix, &vec2);
674 vec2.f[0] /= vec2.f[3];
675 vec2.f[1] /= vec2.f[3];
676
677 if (vec1.f[0] < vec2.f[0]) {
678 dest_rects[i].x1 = floor(vec1.f[0]);
679 dest_rects[i].x2 = ceil(vec2.f[0]);
680 } else {
681 dest_rects[i].x1 = floor(vec2.f[0]);
682 dest_rects[i].x2 = ceil(vec1.f[0]);
683 }
684
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600685 if (vec1.f[1] < vec2.f[1]) {
686 dest_rects[i].y1 = floor(vec1.f[1]);
687 dest_rects[i].y2 = ceil(vec2.f[1]);
688 } else {
689 dest_rects[i].y1 = floor(vec2.f[1]);
690 dest_rects[i].y2 = ceil(vec1.f[1]);
691 }
692 }
693
694 pixman_region32_clear(dest);
695 pixman_region32_init_rects(dest, dest_rects, nrects);
696 free(dest_rects);
697}
698
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200699WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500700weston_transformed_region(int width, int height,
701 enum wl_output_transform transform,
702 int32_t scale,
703 pixman_region32_t *src, pixman_region32_t *dest)
704{
705 pixman_box32_t *src_rects, *dest_rects;
706 int nrects, i;
707
708 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
709 if (src != dest)
710 pixman_region32_copy(dest, src);
711 return;
712 }
713
714 src_rects = pixman_region32_rectangles(src, &nrects);
715 dest_rects = malloc(nrects * sizeof(*dest_rects));
716 if (!dest_rects)
717 return;
718
719 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
720 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
721 } else {
722 for (i = 0; i < nrects; i++) {
723 switch (transform) {
724 default:
725 case WL_OUTPUT_TRANSFORM_NORMAL:
726 dest_rects[i].x1 = src_rects[i].x1;
727 dest_rects[i].y1 = src_rects[i].y1;
728 dest_rects[i].x2 = src_rects[i].x2;
729 dest_rects[i].y2 = src_rects[i].y2;
730 break;
731 case WL_OUTPUT_TRANSFORM_90:
732 dest_rects[i].x1 = height - src_rects[i].y2;
733 dest_rects[i].y1 = src_rects[i].x1;
734 dest_rects[i].x2 = height - src_rects[i].y1;
735 dest_rects[i].y2 = src_rects[i].x2;
736 break;
737 case WL_OUTPUT_TRANSFORM_180:
738 dest_rects[i].x1 = width - src_rects[i].x2;
739 dest_rects[i].y1 = height - src_rects[i].y2;
740 dest_rects[i].x2 = width - src_rects[i].x1;
741 dest_rects[i].y2 = height - src_rects[i].y1;
742 break;
743 case WL_OUTPUT_TRANSFORM_270:
744 dest_rects[i].x1 = src_rects[i].y1;
745 dest_rects[i].y1 = width - src_rects[i].x2;
746 dest_rects[i].x2 = src_rects[i].y2;
747 dest_rects[i].y2 = width - src_rects[i].x1;
748 break;
749 case WL_OUTPUT_TRANSFORM_FLIPPED:
750 dest_rects[i].x1 = width - src_rects[i].x2;
751 dest_rects[i].y1 = src_rects[i].y1;
752 dest_rects[i].x2 = width - src_rects[i].x1;
753 dest_rects[i].y2 = src_rects[i].y2;
754 break;
755 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
756 dest_rects[i].x1 = height - src_rects[i].y2;
757 dest_rects[i].y1 = width - src_rects[i].x2;
758 dest_rects[i].x2 = height - src_rects[i].y1;
759 dest_rects[i].y2 = width - src_rects[i].x1;
760 break;
761 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
762 dest_rects[i].x1 = src_rects[i].x1;
763 dest_rects[i].y1 = height - src_rects[i].y2;
764 dest_rects[i].x2 = src_rects[i].x2;
765 dest_rects[i].y2 = height - src_rects[i].y1;
766 break;
767 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
768 dest_rects[i].x1 = src_rects[i].y1;
769 dest_rects[i].y1 = src_rects[i].x1;
770 dest_rects[i].x2 = src_rects[i].y2;
771 dest_rects[i].y2 = src_rects[i].x2;
772 break;
773 }
774 }
775 }
776
777 if (scale != 1) {
778 for (i = 0; i < nrects; i++) {
779 dest_rects[i].x1 *= scale;
780 dest_rects[i].x2 *= scale;
781 dest_rects[i].y1 *= scale;
782 dest_rects[i].y2 *= scale;
783 }
784 }
785
786 pixman_region32_clear(dest);
787 pixman_region32_init_rects(dest, dest_rects, nrects);
788 free(dest_rects);
789}
790
Jonny Lamb74130762013-11-26 18:19:46 +0100791static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300792viewport_surface_to_buffer(struct weston_surface *surface,
793 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100794{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200795 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200796 double src_width, src_height;
797 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200798
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200799 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
800 if (vp->surface.width == -1) {
801 *bx = sx;
802 *by = sy;
803 return;
804 }
Jonny Lamb74130762013-11-26 18:19:46 +0100805
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200806 src_x = 0.0;
807 src_y = 0.0;
808 src_width = surface->width_from_buffer;
809 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100810 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200811 src_x = wl_fixed_to_double(vp->buffer.src_x);
812 src_y = wl_fixed_to_double(vp->buffer.src_y);
813 src_width = wl_fixed_to_double(vp->buffer.src_width);
814 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100815 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200816
817 *bx = sx * src_width / surface->width + src_x;
818 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100819}
820
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500821WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200822weston_surface_to_buffer_float(struct weston_surface *surface,
823 float sx, float sy, float *bx, float *by)
824{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200825 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
826
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300827 /* first transform coordinates if the viewport is set */
828 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100829
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500830 weston_transformed_coord(surface->width_from_buffer,
831 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200832 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100833 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200834}
835
Derek Foreman99739672015-12-03 16:38:11 -0600836/** Transform a rectangle from surface coordinates to buffer coordinates
837 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300838 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600839 * from.
840 * \param rect The rectangle to transform.
841 * \return The transformed rectangle.
842 *
843 * Viewport and buffer transformations can only do translation, scaling,
844 * and rotations in 90-degree steps. Therefore the only loss in the
845 * conversion is coordinate rounding.
846 *
847 * However, some coordinate rounding takes place as an intermediate
848 * step before the buffer scale factor is applied, so the rectangle
849 * boundary may not be exactly as expected.
850 *
851 * This is OK for damage tracking since a little extra coverage is
852 * not a problem.
853 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200854WL_EXPORT pixman_box32_t
855weston_surface_to_buffer_rect(struct weston_surface *surface,
856 pixman_box32_t rect)
857{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200858 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100859 float xf, yf;
860
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300861 /* first transform box coordinates if the viewport is set */
862 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100863 rect.x1 = floorf(xf);
864 rect.y1 = floorf(yf);
865
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300866 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600867 rect.x2 = ceilf(xf);
868 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100869
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500870 return weston_transformed_rect(surface->width_from_buffer,
871 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200872 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200873 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200874}
875
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200876/** Transform a region from surface coordinates to buffer coordinates
877 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300878 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200879 * from.
880 * \param surface_region[in] The region in surface coordinates.
881 * \param buffer_region[out] The region converted to buffer coordinates.
882 *
883 * Buffer_region must be init'd, but will be completely overwritten.
884 *
885 * Viewport and buffer transformations can only do translation, scaling,
886 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600887 * conversion is from the coordinate rounding that takes place in
888 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200889 */
890WL_EXPORT void
891weston_surface_to_buffer_region(struct weston_surface *surface,
892 pixman_region32_t *surface_region,
893 pixman_region32_t *buffer_region)
894{
895 pixman_box32_t *src_rects, *dest_rects;
896 int nrects, i;
897
898 src_rects = pixman_region32_rectangles(surface_region, &nrects);
899 dest_rects = malloc(nrects * sizeof(*dest_rects));
900 if (!dest_rects)
901 return;
902
903 for (i = 0; i < nrects; i++) {
904 dest_rects[i] = weston_surface_to_buffer_rect(surface,
905 src_rects[i]);
906 }
907
908 pixman_region32_fini(buffer_region);
909 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
910 free(dest_rects);
911}
912
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200913WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500914weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400915 struct weston_plane *plane)
916{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500917 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400918 return;
919
Jason Ekstranda7af7042013-10-12 22:38:11 -0500920 weston_view_damage_below(view);
921 view->plane = plane;
922 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400923}
924
Pekka Paalanen51723d52015-02-17 13:10:01 +0200925/** Inflict damage on the plane where the view is visible.
926 *
927 * \param view The view that causes the damage.
928 *
929 * If the view is currently on a plane (including the primary plane),
930 * take the view's boundingbox, subtract all the opaque views that cover it,
931 * and add the remaining region as damage to the plane. This corresponds
932 * to the damage inflicted to the plane if this view disappeared.
933 *
934 * A repaint is scheduled for this view.
935 *
936 * The region of all opaque views covering this view is stored in
937 * weston_view::clip and updated by view_accumulate_damage() during
938 * weston_output_repaint(). Specifically, that region matches the
939 * scenegraph as it was last painted.
940 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400941WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500942weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200943{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500944 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200945
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500946 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200947 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500948 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800949 if (view->plane)
950 pixman_region32_union(&view->plane->damage,
951 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500952 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800953 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200954}
955
Pekka Paalanen01e00682017-03-24 16:21:06 +0200956/** Send wl_surface.enter/leave events
957 *
958 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300959 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200960 * \param enter True if entered.
961 * \param left True if left.
962 *
963 * Send the enter/leave events for all protocol objects bound to the given
964 * output by the client owning the surface.
965 */
966static void
967weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300968 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +0200969 bool enter,
970 bool leave)
971{
972 struct wl_resource *wloutput;
973 struct wl_client *client;
974
975 assert(enter != leave);
976
977 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300978 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +0200979 if (wl_resource_get_client(wloutput) != client)
980 continue;
981
982 if (enter)
983 wl_surface_send_enter(surface->resource, wloutput);
984 if (leave)
985 wl_surface_send_leave(surface->resource, wloutput);
986 }
987}
988
Bryce Harrington3f650b82015-12-23 11:01:58 -0800989/**
990 * \param es The surface
991 * \param mask The new set of outputs for the surface
992 *
993 * Sets the surface's set of outputs to the ones specified by
994 * the new output mask provided. Identifies the outputs that
995 * have changed, the posts enter and leave events for these
996 * outputs as appropriate.
997 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400998static void
999weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1000{
1001 uint32_t different = es->output_mask ^ mask;
1002 uint32_t entered = mask & different;
1003 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001004 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001005 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001006 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001007
1008 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001009 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001010 return;
1011 if (different == 0)
1012 return;
1013
1014 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001015 output_bit = 1u << output->id;
1016 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001017 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001018
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001019 wl_list_for_each(head, &output->head_list, output_link) {
1020 weston_surface_send_enter_leave(es, head,
1021 output_bit & entered,
1022 output_bit & left);
1023 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001024 }
1025}
1026
Semi Malinene7a52fb2018-04-26 11:08:10 +02001027static void
1028notify_view_output_destroy(struct wl_listener *listener, void *data)
1029{
1030 struct weston_view *view =
1031 container_of(listener,
1032 struct weston_view, output_destroy_listener);
1033
1034 view->output = NULL;
1035 view->output_destroy_listener.notify = NULL;
1036}
1037
1038/** Set the primary output of the view
1039 *
1040 * \param view The view whose primary output to set
1041 * \param output The new primary output for the view
1042 *
1043 * Set \a output to be the primary output of the \a view.
1044 *
1045 * Notice that the assignment may be temporary; the primary output could be
1046 * automatically changed. Hence, one cannot rely on the value persisting.
1047 *
1048 * Passing NULL as /a output will set the primary output to NULL.
1049 */
1050WL_EXPORT void
1051weston_view_set_output(struct weston_view *view, struct weston_output *output)
1052{
1053 if (view->output_destroy_listener.notify) {
1054 wl_list_remove(&view->output_destroy_listener.link);
1055 view->output_destroy_listener.notify = NULL;
1056 }
1057 view->output = output;
1058 if (output) {
1059 view->output_destroy_listener.notify =
1060 notify_view_output_destroy;
1061 wl_signal_add(&output->destroy_signal,
1062 &view->output_destroy_listener);
1063 }
1064}
1065
Bryce Harrington3f650b82015-12-23 11:01:58 -08001066/** Recalculate which output(s) the surface has views displayed on
1067 *
1068 * \param es The surface to remap to outputs
1069 *
1070 * Finds the output that is showing the largest amount of one
1071 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001072 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001073 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001074 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001075 * in the output_mask for the surface.
1076 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001077static void
1078weston_surface_assign_output(struct weston_surface *es)
1079{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 struct weston_output *new_output;
1081 struct weston_view *view;
1082 pixman_region32_t region;
1083 uint32_t max, area, mask;
1084 pixman_box32_t *e;
1085
1086 new_output = NULL;
1087 max = 0;
1088 mask = 0;
1089 pixman_region32_init(&region);
1090 wl_list_for_each(view, &es->views, surface_link) {
1091 if (!view->output)
1092 continue;
1093
1094 pixman_region32_intersect(&region, &view->transform.boundingbox,
1095 &view->output->region);
1096
1097 e = pixman_region32_extents(&region);
1098 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1099
1100 mask |= view->output_mask;
1101
1102 if (area >= max) {
1103 new_output = view->output;
1104 max = area;
1105 }
1106 }
1107 pixman_region32_fini(&region);
1108
1109 es->output = new_output;
1110 weston_surface_update_output_mask(es, mask);
1111}
1112
Bryce Harrington3f650b82015-12-23 11:01:58 -08001113/** Recalculate which output(s) the view is displayed on
1114 *
1115 * \param ev The view to remap to outputs
1116 *
1117 * Identifies the set of outputs that the view is visible on,
1118 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001119 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001120 *
1121 * Also does the same for the view's surface. See
1122 * weston_surface_assign_output().
1123 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124static void
1125weston_view_assign_output(struct weston_view *ev)
1126{
1127 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001128 struct weston_output *output, *new_output;
1129 pixman_region32_t region;
1130 uint32_t max, area, mask;
1131 pixman_box32_t *e;
1132
1133 new_output = NULL;
1134 max = 0;
1135 mask = 0;
1136 pixman_region32_init(&region);
1137 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001138 if (output->destroying)
1139 continue;
1140
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001142 &output->region);
1143
1144 e = pixman_region32_extents(&region);
1145 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1146
1147 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001148 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001149
1150 if (area >= max) {
1151 new_output = output;
1152 max = area;
1153 }
1154 }
1155 pixman_region32_fini(&region);
1156
Semi Malinene7a52fb2018-04-26 11:08:10 +02001157 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 ev->output_mask = mask;
1159
1160 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001161}
1162
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001163static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001164weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1165 int from_x, int from_y, int *to_x, int *to_y)
1166{
1167 float x, y;
1168
1169 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1170 weston_view_from_global_float(to, x, y, &x, &y);
1171
1172 *to_x = round(x);
1173 *to_y = round(y);
1174}
1175
1176static void
1177weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1178{
1179 pixman_box32_t *a;
1180 pixman_box32_t b;
1181
1182 a = pixman_region32_extents(&from->geometry.scissor);
1183
1184 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1185 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1186
1187 pixman_region32_fini(&to->geometry.scissor);
1188 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1189}
1190
1191static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001192view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001194{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001195 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1196 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001197 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001198 { inbox->x1, inbox->y1 },
1199 { inbox->x1, inbox->y2 },
1200 { inbox->x2, inbox->y1 },
1201 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001202 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001203 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001204 int i;
1205
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001206 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001207 /* avoid rounding empty bbox to 1x1 */
1208 pixman_region32_init(bbox);
1209 return;
1210 }
1211
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001212 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001213 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001215 if (x < min_x)
1216 min_x = x;
1217 if (x > max_x)
1218 max_x = x;
1219 if (y < min_y)
1220 min_y = y;
1221 if (y > max_y)
1222 max_y = y;
1223 }
1224
Pekka Paalanen219b9822012-02-08 15:38:37 +02001225 int_x = floorf(min_x);
1226 int_y = floorf(min_y);
1227 pixman_region32_init_rect(bbox, int_x, int_y,
1228 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001229}
1230
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001231static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001233{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001235
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001236 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 view->geometry.x = roundf(view->geometry.x);
1238 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001239
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001240 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1242 view->transform.position.matrix.d[12] = view->geometry.x;
1243 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001244
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 view->transform.inverse = view->transform.position.matrix;
1248 view->transform.inverse.d[12] = -view->geometry.x;
1249 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001250
Jason Ekstranda7af7042013-10-12 22:38:11 -05001251 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001252 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001253 view->surface->width,
1254 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001255 if (view->geometry.scissor_enabled)
1256 pixman_region32_intersect(&view->transform.boundingbox,
1257 &view->transform.boundingbox,
1258 &view->geometry.scissor);
1259
1260 pixman_region32_translate(&view->transform.boundingbox,
1261 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001262
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 if (view->alpha == 1.0) {
1264 pixman_region32_copy(&view->transform.opaque,
1265 &view->surface->opaque);
1266 pixman_region32_translate(&view->transform.opaque,
1267 view->geometry.x,
1268 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001269 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001270}
1271
1272static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001274{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 struct weston_view *parent = view->geometry.parent;
1276 struct weston_matrix *matrix = &view->transform.matrix;
1277 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001278 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001279 pixman_region32_t surfregion;
1280 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001281
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001283
1284 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1286 view->transform.position.matrix.d[12] = view->geometry.x;
1287 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001288
1289 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001291 weston_matrix_multiply(matrix, &tform->matrix);
1292
Pekka Paalanen483243f2013-03-08 14:56:50 +02001293 if (parent)
1294 weston_matrix_multiply(matrix, &parent->transform.matrix);
1295
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001296 if (weston_matrix_invert(inverse, matrix) < 0) {
1297 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298 weston_log("error: weston_view %p"
1299 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001300 return -1;
1301 }
1302
Giulio Camuffo148c1992016-09-04 18:50:46 +03001303 if (view->alpha == 1.0 &&
1304 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1305 pixman_region32_copy(&view->transform.opaque,
1306 &view->surface->opaque);
1307 pixman_region32_translate(&view->transform.opaque,
1308 matrix->d[12],
1309 matrix->d[13]);
1310 }
1311
Pekka Paalanen380adf52015-02-16 14:39:11 +02001312 pixman_region32_init_rect(&surfregion, 0, 0,
1313 view->surface->width, view->surface->height);
1314 if (view->geometry.scissor_enabled)
1315 pixman_region32_intersect(&surfregion, &surfregion,
1316 &view->geometry.scissor);
1317 surfbox = pixman_region32_extents(&surfregion);
1318
1319 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1320 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001321
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001322 return 0;
1323}
1324
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001325static struct weston_layer *
1326get_view_layer(struct weston_view *view)
1327{
1328 if (view->parent_view)
1329 return get_view_layer(view->parent_view);
1330 return view->layer_link.layer;
1331}
1332
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001333WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001335{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001337 struct weston_layer *layer;
1338 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001339
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001341 return;
1342
Pekka Paalanen483243f2013-03-08 14:56:50 +02001343 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001345
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001347
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001349
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350 pixman_region32_fini(&view->transform.boundingbox);
1351 pixman_region32_fini(&view->transform.opaque);
1352 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001353
Pekka Paalanencd403622012-01-25 13:37:39 +02001354 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355 if (view->geometry.transformation_list.next ==
1356 &view->transform.position.link &&
1357 view->geometry.transformation_list.prev ==
1358 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001359 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001361 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001362 if (weston_view_update_transform_enable(view) < 0)
1363 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001364 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001365
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001366 layer = get_view_layer(view);
1367 if (layer) {
1368 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001369 pixman_region32_intersect(&view->transform.boundingbox,
1370 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001371 pixman_region32_intersect(&view->transform.opaque,
1372 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001373 pixman_region32_fini(&mask);
1374 }
1375
Pekka Paalanen380adf52015-02-16 14:39:11 +02001376 if (parent) {
1377 if (parent->geometry.scissor_enabled) {
1378 view->geometry.scissor_enabled = true;
1379 weston_view_transfer_scissor(parent, view);
1380 } else {
1381 view->geometry.scissor_enabled = false;
1382 }
1383 }
1384
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001386
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001388
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 wl_signal_emit(&view->surface->compositor->transform_signal,
1390 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001391}
1392
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001393WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001394weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001395{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001397
1398 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 * The invariant: if view->geometry.dirty, then all views
1400 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001401 * Corollary: if not parent->geometry.dirty, then all ancestors
1402 * are not dirty.
1403 */
1404
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001406 return;
1407
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001409
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001411 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001412 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001413}
1414
1415WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416weston_view_to_global_fixed(struct weston_view *view,
1417 wl_fixed_t vx, wl_fixed_t vy,
1418 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001419{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001420 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001421
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 weston_view_to_global_float(view,
1423 wl_fixed_to_double(vx),
1424 wl_fixed_to_double(vy),
1425 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001426 *x = wl_fixed_from_double(xf);
1427 *y = wl_fixed_from_double(yf);
1428}
1429
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001430WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431weston_view_from_global_float(struct weston_view *view,
1432 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001433{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001435 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1436
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001438
1439 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001440 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001442 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443 *vx = 0;
1444 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001445 return;
1446 }
1447
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 *vx = v.f[0] / v.f[3];
1449 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001450 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 *vx = x - view->geometry.x;
1452 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001453 }
1454}
1455
1456WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457weston_view_from_global_fixed(struct weston_view *view,
1458 wl_fixed_t x, wl_fixed_t y,
1459 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001460{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001462
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 weston_view_from_global_float(view,
1464 wl_fixed_to_double(x),
1465 wl_fixed_to_double(y),
1466 &vxf, &vyf);
1467 *vx = wl_fixed_from_double(vxf);
1468 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001469}
1470
1471WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472weston_view_from_global(struct weston_view *view,
1473 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001474{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001476
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1478 *vx = floorf(vxf);
1479 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001480}
1481
Bryce Harrington3f650b82015-12-23 11:01:58 -08001482/**
1483 * \param surface The surface to be repainted
1484 *
1485 * Marks the output(s) that the surface is shown on as needing to be
1486 * repainted. See weston_output_schedule_repaint().
1487 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001488WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001489weston_surface_schedule_repaint(struct weston_surface *surface)
1490{
1491 struct weston_output *output;
1492
1493 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001494 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001495 weston_output_schedule_repaint(output);
1496}
1497
Bryce Harrington3f650b82015-12-23 11:01:58 -08001498/**
1499 * \param view The view to be repainted
1500 *
1501 * Marks the output(s) that the view is shown on as needing to be
1502 * repainted. See weston_output_schedule_repaint().
1503 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001504WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001505weston_view_schedule_repaint(struct weston_view *view)
1506{
1507 struct weston_output *output;
1508
1509 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001510 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 weston_output_schedule_repaint(output);
1512}
1513
Pekka Paalanene508ce62015-02-19 13:59:55 +02001514/**
1515 * XXX: This function does it the wrong way.
1516 * surface->damage is the damage from the client, and causes
1517 * surface_flush_damage() to copy pixels. No window management action can
1518 * cause damage to the client-provided content, warranting re-upload!
1519 *
1520 * Instead of surface->damage, this function should record the damage
1521 * with all the views for this surface to avoid extraneous texture
1522 * uploads.
1523 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001524WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001525weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001526{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001527 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 0, 0, surface->width,
1529 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001530
Kristian Høgsberg98238702012-08-03 16:29:12 -04001531 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001532}
1533
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001534WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001536{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001537 if (view->geometry.x == x && view->geometry.y == y)
1538 return;
1539
Jason Ekstranda7af7042013-10-12 22:38:11 -05001540 view->geometry.x = x;
1541 view->geometry.y = y;
1542 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001543}
1544
Pekka Paalanen483243f2013-03-08 14:56:50 +02001545static void
1546transform_parent_handle_parent_destroy(struct wl_listener *listener,
1547 void *data)
1548{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001549 struct weston_view *view =
1550 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001551 geometry.parent_destroy_listener);
1552
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001554}
1555
1556WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001558 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001559{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 if (view->geometry.parent) {
1561 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1562 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001563
1564 if (!parent)
1565 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001566 }
1567
Jason Ekstranda7af7042013-10-12 22:38:11 -05001568 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001569
Jason Ekstranda7af7042013-10-12 22:38:11 -05001570 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001571 transform_parent_handle_parent_destroy;
1572 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001573 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001575 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001577 }
1578
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 weston_view_geometry_dirty(view);
1580}
1581
Pekka Paalanen380adf52015-02-16 14:39:11 +02001582/** Set a clip mask rectangle on a view
1583 *
1584 * \param view The view to set the clip mask on.
1585 * \param x Top-left corner X coordinate of the clip rectangle.
1586 * \param y Top-left corner Y coordinate of the clip rectangle.
1587 * \param width Width of the clip rectangle, non-negative.
1588 * \param height Height of the clip rectangle, non-negative.
1589 *
1590 * A shell may set a clip mask rectangle on a view. Everything outside
1591 * the rectangle is cut away for input and output purposes: it is
1592 * not drawn and cannot be hit by hit-test based input like pointer
1593 * motion or touch-downs. Everything inside the rectangle will behave
1594 * normally. Clients are unaware of clipping.
1595 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001596 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001597 * mask rectangle does not affect the view position, the view is positioned
1598 * as it would be without a clip. The clip also does not change
1599 * weston_surface::width,height.
1600 *
1601 * The clip mask rectangle is part of transformation inheritance
1602 * (weston_view_set_transform_parent()). A clip set in the root of the
1603 * transformation inheritance tree will affect all views in the tree.
1604 * A clip can be set only on the root view. Attempting to set a clip
1605 * on view that has a transformation parent will fail. Assigning a parent
1606 * to a view that has a clip set will cause the clip to be forgotten.
1607 *
1608 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1609 * on the additional transformations in the child views. These transformations
1610 * may not rotate the coordinate axes, i.e., only translation and scaling
1611 * are allowed. Violating this restriction causes the clipping to malfunction.
1612 * Furthermore, using scaling may cause rounding errors in child clipping.
1613 *
1614 * The clip mask rectangle is not automatically adjusted based on
1615 * wl_surface.attach dx and dy arguments.
1616 *
1617 * A clip mask rectangle can be set only if the compositor capability
1618 * WESTON_CAP_VIEW_CLIP_MASK is present.
1619 *
1620 * This function sets the clip mask rectangle and schedules a repaint for
1621 * the view.
1622 */
1623WL_EXPORT void
1624weston_view_set_mask(struct weston_view *view,
1625 int x, int y, int width, int height)
1626{
1627 struct weston_compositor *compositor = view->surface->compositor;
1628
1629 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1630 weston_log("%s not allowed without capability!\n", __func__);
1631 return;
1632 }
1633
1634 if (view->geometry.parent) {
1635 weston_log("view %p has a parent, clip forbidden!\n", view);
1636 return;
1637 }
1638
1639 if (width < 0 || height < 0) {
1640 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1641 x, y, width, height);
1642 return;
1643 }
1644
1645 pixman_region32_fini(&view->geometry.scissor);
1646 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1647 view->geometry.scissor_enabled = true;
1648 weston_view_geometry_dirty(view);
1649 weston_view_schedule_repaint(view);
1650}
1651
1652/** Remove the clip mask from a view
1653 *
1654 * \param view The view to remove the clip mask from.
1655 *
1656 * Removed the clip mask rectangle and schedules a repaint.
1657 *
1658 * \sa weston_view_set_mask
1659 */
1660WL_EXPORT void
1661weston_view_set_mask_infinite(struct weston_view *view)
1662{
1663 view->geometry.scissor_enabled = false;
1664 weston_view_geometry_dirty(view);
1665 weston_view_schedule_repaint(view);
1666}
1667
Armin Krezović0da12b82016-06-30 06:04:33 +02001668/* Check if view should be displayed
1669 *
1670 * The indicator is set manually when assigning
1671 * a view to a surface.
1672 *
1673 * This needs reworking. See the thread starting at:
1674 *
1675 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1676 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001677WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001678weston_view_is_mapped(struct weston_view *view)
1679{
Armin Krezović0da12b82016-06-30 06:04:33 +02001680 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001681}
1682
Armin Krezović0da12b82016-06-30 06:04:33 +02001683/* Check if a surface has a view assigned to it
1684 *
1685 * The indicator is set manually when mapping
1686 * a surface and creating a view for it.
1687 *
1688 * This needs to go. See the thread starting at:
1689 *
1690 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1691 *
1692 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001693WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001694weston_surface_is_mapped(struct weston_surface *surface)
1695{
Armin Krezović0da12b82016-06-30 06:04:33 +02001696 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001697}
1698
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001699static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001700surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001701{
1702 struct weston_view *view;
1703
1704 if (surface->width == width && surface->height == height)
1705 return;
1706
1707 surface->width = width;
1708 surface->height = height;
1709
1710 wl_list_for_each(view, &surface->views, surface_link)
1711 weston_view_geometry_dirty(view);
1712}
1713
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001714WL_EXPORT void
1715weston_surface_set_size(struct weston_surface *surface,
1716 int32_t width, int32_t height)
1717{
1718 assert(!surface->resource);
1719 surface_set_size(surface, width, height);
1720}
1721
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001722static int
1723fixed_round_up_to_int(wl_fixed_t f)
1724{
1725 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1726}
1727
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001728static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001729convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1730 int32_t width, int32_t height,
1731 uint32_t transform,
1732 int32_t scale)
1733{
1734 assert(scale > 0);
1735
1736 switch (transform) {
1737 case WL_OUTPUT_TRANSFORM_NORMAL:
1738 case WL_OUTPUT_TRANSFORM_180:
1739 case WL_OUTPUT_TRANSFORM_FLIPPED:
1740 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1741 *width_out = width / scale;
1742 *height_out = height / scale;
1743 break;
1744 case WL_OUTPUT_TRANSFORM_90:
1745 case WL_OUTPUT_TRANSFORM_270:
1746 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1747 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1748 *width_out = height / scale;
1749 *height_out = width / scale;
1750 break;
1751 default:
1752 assert(0 && "invalid transform");
1753 }
1754}
1755
1756static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001757weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001758{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001759 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001760
1761 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001762 surface->width_from_buffer = 0;
1763 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001764 return;
1765 }
1766
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001767 convert_size_by_transform_scale(&surface->width_from_buffer,
1768 &surface->height_from_buffer,
1769 surface->buffer_ref.buffer->width,
1770 surface->buffer_ref.buffer->height,
1771 vp->buffer.transform,
1772 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001773}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001774
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001775static void
1776weston_surface_update_size(struct weston_surface *surface)
1777{
1778 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1779 int32_t width, height;
1780
1781 width = surface->width_from_buffer;
1782 height = surface->height_from_buffer;
1783
1784 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001785 surface_set_size(surface,
1786 vp->surface.width, vp->surface.height);
1787 return;
1788 }
1789
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001790 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001791 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1792 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1793
1794 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001795 return;
1796 }
1797
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001798 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001799}
1800
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001801WL_EXPORT void
1802weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001803{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001804 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001805}
1806
Jason Ekstranda7af7042013-10-12 22:38:11 -05001807WL_EXPORT struct weston_view *
1808weston_compositor_pick_view(struct weston_compositor *compositor,
1809 wl_fixed_t x, wl_fixed_t y,
1810 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001811{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001812 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001813 wl_fixed_t view_x, view_y;
1814 int view_ix, view_iy;
1815 int ix = wl_fixed_to_int(x);
1816 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001817
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001819 if (!pixman_region32_contains_point(
1820 &view->transform.boundingbox, ix, iy, NULL))
1821 continue;
1822
1823 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1824 view_ix = wl_fixed_to_int(view_x);
1825 view_iy = wl_fixed_to_int(view_y);
1826
1827 if (!pixman_region32_contains_point(&view->surface->input,
1828 view_ix, view_iy, NULL))
1829 continue;
1830
Pekka Paalanen380adf52015-02-16 14:39:11 +02001831 if (view->geometry.scissor_enabled &&
1832 !pixman_region32_contains_point(&view->geometry.scissor,
1833 view_ix, view_iy, NULL))
1834 continue;
1835
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001836 *vx = view_x;
1837 *vy = view_y;
1838 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001839 }
1840
Derek Foremanf9318d12015-05-11 15:40:11 -05001841 *vx = wl_fixed_from_int(-1000000);
1842 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001843 return NULL;
1844}
1845
1846static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001847weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001848{
Daniel Stone37816df2012-05-16 18:45:18 +01001849 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001850
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001851 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001852 return;
1853
Daniel Stone37816df2012-05-16 18:45:18 +01001854 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001855 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001856}
1857
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001858WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001859weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001860{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001861 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001862
Jason Ekstranda7af7042013-10-12 22:38:11 -05001863 if (!weston_view_is_mapped(view))
1864 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001865
Jason Ekstranda7af7042013-10-12 22:38:11 -05001866 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02001867 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08001868 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001869 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001870 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001871 wl_list_remove(&view->link);
1872 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001873 view->output_mask = 0;
1874 weston_surface_assign_output(view->surface);
1875
1876 if (weston_surface_is_mapped(view->surface))
1877 return;
1878
1879 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001880 struct weston_touch *touch = weston_seat_get_touch(seat);
1881 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1882 struct weston_keyboard *keyboard =
1883 weston_seat_get_keyboard(seat);
1884
1885 if (keyboard && keyboard->focus == view->surface)
1886 weston_keyboard_set_focus(keyboard, NULL);
1887 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001888 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001889 if (touch && touch->focus == view)
1890 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001891 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001892}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001893
Jason Ekstranda7af7042013-10-12 22:38:11 -05001894WL_EXPORT void
1895weston_surface_unmap(struct weston_surface *surface)
1896{
1897 struct weston_view *view;
1898
Armin Krezovićf8486c32016-06-30 06:04:28 +02001899 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001900 wl_list_for_each(view, &surface->views, surface_link)
1901 weston_view_unmap(view);
1902 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001903}
1904
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001905static void
1906weston_surface_reset_pending_buffer(struct weston_surface *surface)
1907{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001908 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001909 surface->pending.sx = 0;
1910 surface->pending.sy = 0;
1911 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001912 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001913}
1914
Jason Ekstranda7af7042013-10-12 22:38:11 -05001915WL_EXPORT void
1916weston_view_destroy(struct weston_view *view)
1917{
1918 wl_signal_emit(&view->destroy_signal, view);
1919
1920 assert(wl_list_empty(&view->geometry.child_list));
1921
1922 if (weston_view_is_mapped(view)) {
1923 weston_view_unmap(view);
1924 weston_compositor_build_view_list(view->surface->compositor);
1925 }
1926
1927 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001928 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001929
1930 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001931 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001932 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001933 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001934
1935 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03001936 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001937
Jason Ekstranda7af7042013-10-12 22:38:11 -05001938 wl_list_remove(&view->surface_link);
1939
1940 free(view);
1941}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001942
1943WL_EXPORT void
1944weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001945{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001946 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001947 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001948 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001949
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001950 if (--surface->ref_count > 0)
1951 return;
1952
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001953 assert(surface->resource == NULL);
1954
Pekka Paalanenca790762015-04-17 14:23:38 +03001955 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001956
Pekka Paalanene67858b2013-04-25 13:57:42 +03001957 assert(wl_list_empty(&surface->subsurface_list_pending));
1958 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001959
Jason Ekstranda7af7042013-10-12 22:38:11 -05001960 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1961 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001962
Jason Ekstrand7b982072014-05-20 14:33:03 -05001963 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001964
Pekka Paalanende685b82012-12-04 15:58:12 +02001965 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001966
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001967 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001968 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001969 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001970
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001971 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001972 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001973
Pekka Paalanen133e4392014-09-23 22:08:46 -04001974 weston_presentation_feedback_discard_list(&surface->feedback_list);
1975
Jonas Ådahld3414f22016-07-22 17:56:31 +08001976 wl_list_for_each_safe(constraint, next_constraint,
1977 &surface->pointer_constraints,
1978 link)
1979 weston_pointer_constraint_destroy(constraint);
1980
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001981 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001982}
1983
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001984static void
1985destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001986{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001987 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001988
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001989 assert(surface);
1990
Giulio Camuffo0d379742013-11-15 22:06:15 +01001991 /* Set the resource to NULL, since we don't want to leave a
1992 * dangling pointer if the surface was refcounted and survives
1993 * the weston_surface_destroy() call. */
1994 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001995
1996 if (surface->viewport_resource)
1997 wl_resource_set_user_data(surface->viewport_resource, NULL);
1998
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001999 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002000}
2001
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002002static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002003weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2004{
2005 struct weston_buffer *buffer =
2006 container_of(listener, struct weston_buffer, destroy_listener);
2007
2008 wl_signal_emit(&buffer->destroy_signal, buffer);
2009 free(buffer);
2010}
2011
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002012WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002013weston_buffer_from_resource(struct wl_resource *resource)
2014{
2015 struct weston_buffer *buffer;
2016 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002017
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002018 listener = wl_resource_get_destroy_listener(resource,
2019 weston_buffer_destroy_handler);
2020
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002021 if (listener)
2022 return container_of(listener, struct weston_buffer,
2023 destroy_listener);
2024
2025 buffer = zalloc(sizeof *buffer);
2026 if (buffer == NULL)
2027 return NULL;
2028
2029 buffer->resource = resource;
2030 wl_signal_init(&buffer->destroy_signal);
2031 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002032 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002033 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002034
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002035 return buffer;
2036}
2037
2038static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002039weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2040 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002041{
Pekka Paalanende685b82012-12-04 15:58:12 +02002042 struct weston_buffer_reference *ref =
2043 container_of(listener, struct weston_buffer_reference,
2044 destroy_listener);
2045
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002046 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002047 ref->buffer = NULL;
2048}
2049
2050WL_EXPORT void
2051weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002052 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002053{
2054 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002055 ref->buffer->busy_count--;
2056 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002057 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002058 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002059 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002060 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002061 }
2062
Pekka Paalanende685b82012-12-04 15:58:12 +02002063 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002064 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002065 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002066 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002067 }
2068
Pekka Paalanende685b82012-12-04 15:58:12 +02002069 ref->buffer = buffer;
2070 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2071}
2072
2073static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002074weston_surface_attach(struct weston_surface *surface,
2075 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002076{
2077 weston_buffer_reference(&surface->buffer_ref, buffer);
2078
Pekka Paalanena6421c42012-12-04 15:58:10 +02002079 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002080 if (weston_surface_is_mapped(surface))
2081 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002082 }
2083
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002084 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002085
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002086 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002087 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002088}
2089
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002090WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002091weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002092{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002093 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002094
2095 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002096 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002097}
2098
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002099WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002100weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002101{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002102 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002103
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002104 pixman_region32_union(&compositor->primary_plane.damage,
2105 &compositor->primary_plane.damage,
2106 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002107 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002108}
2109
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002110static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002111surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002112{
Pekka Paalanende685b82012-12-04 15:58:12 +02002113 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002114 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002115 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002116
Pekka Paalanenb5026542014-11-12 15:09:24 +02002117 if (weston_timeline_enabled_ &&
2118 pixman_region32_not_empty(&surface->damage))
2119 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2120 TLP_OUTPUT(surface->output), TLP_END);
2121
Jason Ekstrandef540082014-06-26 10:37:36 -07002122 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002123}
2124
2125static void
2126view_accumulate_damage(struct weston_view *view,
2127 pixman_region32_t *opaque)
2128{
2129 pixman_region32_t damage;
2130
2131 pixman_region32_init(&damage);
2132 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002133 pixman_box32_t *extents;
2134
Jason Ekstranda7af7042013-10-12 22:38:11 -05002135 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002136 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002137 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002138 pixman_region32_copy(&damage, &view->surface->damage);
2139 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002140 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002141 }
2142
Pekka Paalanen380adf52015-02-16 14:39:11 +02002143 pixman_region32_intersect(&damage, &damage,
2144 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002145 pixman_region32_subtract(&damage, &damage, opaque);
2146 pixman_region32_union(&view->plane->damage,
2147 &view->plane->damage, &damage);
2148 pixman_region32_fini(&damage);
2149 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002150 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002151}
2152
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002153static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002154compositor_accumulate_damage(struct weston_compositor *ec)
2155{
2156 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002157 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002158 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002159
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002160 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002161
2162 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002163 pixman_region32_copy(&plane->clip, &clip);
2164
2165 pixman_region32_init(&opaque);
2166
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167 wl_list_for_each(ev, &ec->view_list, link) {
2168 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002169 continue;
2170
Jason Ekstranda7af7042013-10-12 22:38:11 -05002171 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002172 }
2173
2174 pixman_region32_union(&clip, &clip, &opaque);
2175 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002176 }
2177
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002178 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002179
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002181 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002182
2183 wl_list_for_each(ev, &ec->view_list, link) {
2184 if (ev->surface->touched)
2185 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002186 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002187
2188 surface_flush_damage(ev->surface);
2189
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002190 /* Both the renderer and the backend have seen the buffer
2191 * by now. If renderer needs the buffer, it has its own
2192 * reference set. If the backend wants to keep the buffer
2193 * around for migrating the surface into a non-primary plane
2194 * later, keep_buffer is true. Otherwise, drop the core
2195 * reference now, and allow early buffer release. This enables
2196 * clients to use single-buffering.
2197 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002198 if (!ev->surface->keep_buffer)
2199 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002200 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002201}
2202
2203static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002204surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002205{
2206 struct weston_subsurface *sub;
2207
Pekka Paalanene67858b2013-04-25 13:57:42 +03002208 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002209 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002210 continue;
2211
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2213 wl_list_init(&sub->surface->views);
2214
2215 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002216 }
2217}
2218
2219static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002220surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002221{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002222 struct weston_subsurface *sub;
2223 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002224
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2226 if (sub->surface == surface)
2227 continue;
2228
George Kiagiadakised04d382014-06-13 18:10:26 +02002229 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2230 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002231 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002232 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002233
2234 surface_free_unused_subsurface_views(sub->surface);
2235 }
2236}
2237
2238static void
2239view_list_add_subsurface_view(struct weston_compositor *compositor,
2240 struct weston_subsurface *sub,
2241 struct weston_view *parent)
2242{
2243 struct weston_subsurface *child;
2244 struct weston_view *view = NULL, *iv;
2245
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002246 if (!weston_surface_is_mapped(sub->surface))
2247 return;
2248
Jason Ekstranda7af7042013-10-12 22:38:11 -05002249 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2250 if (iv->geometry.parent == parent) {
2251 view = iv;
2252 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002253 }
2254 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002255
2256 if (view) {
2257 /* Put it back in the surface's list of views */
2258 wl_list_remove(&view->surface_link);
2259 wl_list_insert(&sub->surface->views, &view->surface_link);
2260 } else {
2261 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002262 weston_view_set_position(view,
2263 sub->position.x,
2264 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002265 weston_view_set_transform_parent(view, parent);
2266 }
2267
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002268 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002269 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002270 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002271
Pekka Paalanenb188e912013-11-19 14:03:35 +02002272 if (wl_list_empty(&sub->surface->subsurface_list)) {
2273 wl_list_insert(compositor->view_list.prev, &view->link);
2274 return;
2275 }
2276
2277 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2278 if (child->surface == sub->surface)
2279 wl_list_insert(compositor->view_list.prev, &view->link);
2280 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002281 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002282 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002283}
2284
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002285/* This recursively adds the sub-surfaces for a view, relying on the
2286 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2287 * change first happens to the sub-surface list, and then automatically
2288 * propagates here. See weston_surface_damage_subsurfaces() for how the
2289 * sub-surfaces receive damage when the client changes the state.
2290 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002291static void
2292view_list_add(struct weston_compositor *compositor,
2293 struct weston_view *view)
2294{
2295 struct weston_subsurface *sub;
2296
2297 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002298
Pekka Paalanenb188e912013-11-19 14:03:35 +02002299 if (wl_list_empty(&view->surface->subsurface_list)) {
2300 wl_list_insert(compositor->view_list.prev, &view->link);
2301 return;
2302 }
2303
2304 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2305 if (sub->surface == view->surface)
2306 wl_list_insert(compositor->view_list.prev, &view->link);
2307 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002308 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002309 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002310}
2311
2312static void
2313weston_compositor_build_view_list(struct weston_compositor *compositor)
2314{
2315 struct weston_view *view;
2316 struct weston_layer *layer;
2317
2318 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002319 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002320 surface_stash_subsurface_views(view->surface);
2321
2322 wl_list_init(&compositor->view_list);
2323 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002324 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002325 view_list_add(compositor, view);
2326 }
2327 }
2328
2329 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002330 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002331 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002332}
2333
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002334static void
2335weston_output_take_feedback_list(struct weston_output *output,
2336 struct weston_surface *surface)
2337{
2338 struct weston_view *view;
2339 struct weston_presentation_feedback *feedback;
2340 uint32_t flags = 0xffffffff;
2341
2342 if (wl_list_empty(&surface->feedback_list))
2343 return;
2344
2345 /* All views must have the flag for the flag to survive. */
2346 wl_list_for_each(view, &surface->views, surface_link) {
2347 /* ignore views that are not on this output at all */
2348 if (view->output_mask & (1u << output->id))
2349 flags &= view->psf_flags;
2350 }
2351
2352 wl_list_for_each(feedback, &surface->feedback_list, link)
2353 feedback->psf_flags = flags;
2354
2355 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2356 wl_list_init(&surface->feedback_list);
2357}
2358
David Herrmann1edf44c2013-10-22 17:11:26 +02002359static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002360weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002361{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002362 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002363 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002364 struct weston_animation *animation, *next;
2365 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002366 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002367 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002368 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002369 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002370
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002371 if (output->destroying)
2372 return 0;
2373
Pekka Paalanenb5026542014-11-12 15:09:24 +02002374 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2375
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002376 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002377 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002378
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002379 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002380 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002381 } else {
2382 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002383 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002384 ev->psf_flags = 0;
2385 }
2386 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002387
Pekka Paalanene67858b2013-04-25 13:57:42 +03002388 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002389 wl_list_for_each(ev, &ec->view_list, link) {
2390 /* Note: This operation is safe to do multiple times on the
2391 * same surface.
2392 */
2393 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002394 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002395 &ev->surface->frame_callback_list);
2396 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002397
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002398 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002399 }
2400 }
2401
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002402 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002403
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002404 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002405 pixman_region32_intersect(&output_damage,
2406 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002407 pixman_region32_subtract(&output_damage,
2408 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002409
Scott Moreauccbf29d2012-02-22 14:21:41 -07002410 if (output->dirty)
2411 weston_output_update_matrix(output);
2412
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002413 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002414
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002415 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002416
Daniel Stone09a97e22017-03-01 11:34:06 +00002417 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002418 if (r == 0)
2419 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002420
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002421 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002422
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002423 frame_time_msec = timespec_to_msec(&output->frame_time);
2424
Jonas Ådahldb773762012-06-13 00:01:21 +02002425 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002426 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002427 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002428 }
2429
Scott Moreaud64cf212012-06-08 19:40:54 -06002430 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002431 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002432 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002433 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002434
Pekka Paalanenb5026542014-11-12 15:09:24 +02002435 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2436
David Herrmann1edf44c2013-10-22 17:11:26 +02002437 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002438}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002439
Pekka Paalanen82919792014-05-21 13:51:49 +03002440static void
2441weston_output_schedule_repaint_reset(struct weston_output *output)
2442{
Daniel Stone05df8c12017-03-03 16:59:42 +00002443 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002444 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002445}
2446
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002447static int
2448weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2449 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002450{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002451 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002452 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002453 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002454
Daniel Stone6847b852017-03-01 11:34:08 +00002455 /* We're not ready yet; come back to make a decision later. */
2456 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002457 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002458
2459 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2460 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002461 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002462
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002463 /* If we're sleeping, drop the repaint machinery entirely; we will
2464 * explicitly repaint all outputs when we come back. */
2465 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2466 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2467 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002468
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002469 /* We don't actually need to repaint this output; drop it from
2470 * repaint until something causes damage. */
2471 if (!output->repaint_needed)
2472 goto err;
2473
2474 /* If repaint fails, we aren't going to get weston_output_finish_frame
2475 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002476 * something schedules a successful repaint later. As repainting may
2477 * take some time, re-read our clock as a courtesy to the next
2478 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002479 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002480 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002481 if (ret != 0)
2482 goto err;
2483
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002484 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002485 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002486
2487err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002488 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002489 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002490}
2491
2492static void
2493output_repaint_timer_arm(struct weston_compositor *compositor)
2494{
2495 struct weston_output *output;
2496 bool any_should_repaint = false;
2497 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002498 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002499
2500 weston_compositor_read_presentation_clock(compositor, &now);
2501
2502 wl_list_for_each(output, &compositor->output_list, link) {
2503 int64_t msec_to_this;
2504
2505 if (output->repaint_status != REPAINT_SCHEDULED)
2506 continue;
2507
2508 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2509 &now);
2510 if (!any_should_repaint || msec_to_this < msec_to_next)
2511 msec_to_next = msec_to_this;
2512
2513 any_should_repaint = true;
2514 }
2515
2516 if (!any_should_repaint)
2517 return;
2518
2519 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2520 * This is a workaround to allow coalescing multiple output repaints
2521 * particularly from weston_output_finish_frame()
2522 * into the same call, which would not happen if we called
2523 * output_repaint_timer_handler() directly.
2524 */
2525 if (msec_to_next < 1)
2526 msec_to_next = 1;
2527
2528 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2529}
2530
2531static int
2532output_repaint_timer_handler(void *data)
2533{
2534 struct weston_compositor *compositor = data;
2535 struct weston_output *output;
2536 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002537 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002538 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002539
2540 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002541
2542 if (compositor->backend->repaint_begin)
2543 repaint_data = compositor->backend->repaint_begin(compositor);
2544
2545 wl_list_for_each(output, &compositor->output_list, link) {
2546 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2547 if (ret)
2548 break;
2549 }
2550
2551 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002552 if (compositor->backend->repaint_flush)
2553 compositor->backend->repaint_flush(compositor,
2554 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002555 } else {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002556 wl_list_for_each(output, &compositor->output_list, link) {
2557 if (output->repainted)
2558 weston_output_schedule_repaint_reset(output);
2559 }
2560
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002561 if (compositor->backend->repaint_cancel)
2562 compositor->backend->repaint_cancel(compositor,
2563 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002564 }
Daniel Stone6847b852017-03-01 11:34:08 +00002565
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002566 wl_list_for_each(output, &compositor->output_list, link)
2567 output->repainted = false;
2568
Daniel Stone6847b852017-03-01 11:34:08 +00002569 output_repaint_timer_arm(compositor);
2570
Pekka Paalanen0513a952014-05-21 16:17:27 +03002571 return 0;
2572}
2573
Kristian Høgsbergef044142011-06-21 15:02:12 -04002574WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002575weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002576 const struct timespec *stamp,
2577 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002578{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002579 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002580 int32_t refresh_nsec;
2581 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002582 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002583
Pekka Paalanenb5026542014-11-12 15:09:24 +02002584
Daniel Stone05df8c12017-03-03 16:59:42 +00002585 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002586 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2587
Daniel Stone6847b852017-03-01 11:34:08 +00002588 weston_compositor_read_presentation_clock(compositor, &now);
2589
Daniel Stone3615ce12017-03-01 11:34:05 +00002590 /* If we haven't been supplied any timestamp at all, we don't have a
2591 * timebase to work against, so any delay just wastes time. Push a
2592 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002593 if (!stamp) {
2594 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002595 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002596 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002597
Marius Vladdf9278a2018-03-06 18:56:23 +02002598 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2599 TLP_VBLANK(stamp), TLP_END);
2600
Pekka Paalanend7894d02015-07-03 15:08:53 +03002601 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002602 weston_presentation_feedback_present_list(&output->feedback_list,
2603 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002604 output->msc,
2605 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002606
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002607 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002608
Daniel Stone6847b852017-03-01 11:34:08 +00002609 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2610 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2611 -compositor->repaint_msec);
2612 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002613
2614 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002615 static bool warned;
2616
2617 if (!warned)
2618 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002619 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002620 warned = true;
2621
Daniel Stone6847b852017-03-01 11:34:08 +00002622 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002623 }
2624
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002625 /* Called from restart_repaint_loop and restart happens already after
2626 * the deadline given by repaint_msec? In that case we delay until
2627 * the deadline of the next frame, to give clients a more predictable
2628 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002629 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2630 msec_rel < 0) {
2631 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2632 timespec_add_nsec(&output->next_repaint,
2633 &output->next_repaint,
2634 refresh_nsec);
2635 }
2636 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002637
Daniel Stone3615ce12017-03-01 11:34:05 +00002638out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002639 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002640 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002641}
2642
2643static void
2644idle_repaint(void *data)
2645{
2646 struct weston_output *output = data;
2647
Daniel Stone05df8c12017-03-03 16:59:42 +00002648 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2649 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002650 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002651 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002652}
2653
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002654WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002655weston_layer_entry_insert(struct weston_layer_entry *list,
2656 struct weston_layer_entry *entry)
2657{
2658 wl_list_insert(&list->link, &entry->link);
2659 entry->layer = list->layer;
2660}
2661
2662WL_EXPORT void
2663weston_layer_entry_remove(struct weston_layer_entry *entry)
2664{
2665 wl_list_remove(&entry->link);
2666 wl_list_init(&entry->link);
2667 entry->layer = NULL;
2668}
2669
Quentin Glidic82681572016-12-17 13:40:51 +01002670
2671/** Initialize the weston_layer struct.
2672 *
2673 * \param compositor The compositor instance
2674 * \param layer The layer to initialize
2675 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002676WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002677weston_layer_init(struct weston_layer *layer,
2678 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002679{
Quentin Glidic82681572016-12-17 13:40:51 +01002680 layer->compositor = compositor;
2681 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002682 wl_list_init(&layer->view_list.link);
2683 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002684 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002685}
2686
2687/** Sets the position of the layer in the layer list. The layer will be placed
2688 * below any layer with the same position value, if any.
2689 * This function is safe to call if the layer is already on the list, but the
2690 * layer may be moved below other layers at the same position, if any.
2691 *
2692 * \param layer The layer to modify
2693 * \param position The position the layer will be placed at
2694 */
2695WL_EXPORT void
2696weston_layer_set_position(struct weston_layer *layer,
2697 enum weston_layer_position position)
2698{
2699 struct weston_layer *below;
2700
2701 wl_list_remove(&layer->link);
2702
2703 /* layer_list is ordered from top to bottom, the last layer being the
2704 * background with the smallest position value */
2705
2706 layer->position = position;
2707 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2708 if (below->position >= layer->position) {
2709 wl_list_insert(&below->link, &layer->link);
2710 return;
2711 }
2712 }
2713 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2714}
2715
2716/** Hide a layer by taking it off the layer list.
2717 * This function is safe to call if the layer is not on the list.
2718 *
2719 * \param layer The layer to hide
2720 */
2721WL_EXPORT void
2722weston_layer_unset_position(struct weston_layer *layer)
2723{
2724 wl_list_remove(&layer->link);
2725 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002726}
2727
2728WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002729weston_layer_set_mask(struct weston_layer *layer,
2730 int x, int y, int width, int height)
2731{
2732 struct weston_view *view;
2733
2734 layer->mask.x1 = x;
2735 layer->mask.x2 = x + width;
2736 layer->mask.y1 = y;
2737 layer->mask.y2 = y + height;
2738
2739 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2740 weston_view_geometry_dirty(view);
2741 }
2742}
2743
2744WL_EXPORT void
2745weston_layer_set_mask_infinite(struct weston_layer *layer)
2746{
2747 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2748 UINT32_MAX, UINT32_MAX);
2749}
2750
Daniel Stone3b775632018-07-20 08:38:25 +01002751WL_EXPORT bool
2752weston_layer_mask_is_infinite(struct weston_layer *layer)
2753{
2754 return layer->mask.x1 == INT32_MIN &&
2755 layer->mask.y1 == INT32_MIN &&
2756 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
2757 layer->mask.y2 == INT32_MIN + UINT32_MAX;
2758}
2759
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002760WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002761weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002762{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002763 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002764 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002765
Bryce Harrington08976ac2016-08-30 12:05:16 -07002766 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2767 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002768 return;
2769
Pekka Paalanenb5026542014-11-12 15:09:24 +02002770 if (!output->repaint_needed)
2771 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2772
Kristian Høgsbergef044142011-06-21 15:02:12 -04002773 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002774 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002775
2776 /* If we already have a repaint scheduled for our idle handler,
2777 * no need to set it again. If the repaint has been called but
2778 * not finished, then weston_output_finish_frame() will notice
2779 * that a repaint is needed and schedule one. */
2780 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002781 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002782
Daniel Stone05df8c12017-03-03 16:59:42 +00002783 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002784 assert(!output->idle_repaint_source);
2785 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2786 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002787 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002788}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002789
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002790WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002791weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2792{
2793 struct weston_output *output;
2794
2795 wl_list_for_each(output, &compositor->output_list, link)
2796 weston_output_schedule_repaint(output);
2797}
2798
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002799static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002800surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002801{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002802 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002803}
2804
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002805static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002806surface_attach(struct wl_client *client,
2807 struct wl_resource *resource,
2808 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2809{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002810 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002811 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002812
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002813 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002814 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002815 if (buffer == NULL) {
2816 wl_client_post_no_memory(client);
2817 return;
2818 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002819 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002820
Pekka Paalanende685b82012-12-04 15:58:12 +02002821 /* Attach, attach, without commit in between does not send
2822 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002823 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002824
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002825 surface->pending.sx = sx;
2826 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002827 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002828}
2829
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002830static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002831surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002832 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002833 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002834{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002835 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002836
Derek Foreman57e92ed2015-11-17 14:11:35 -06002837 if (width <= 0 || height <= 0)
2838 return;
2839
Derek Foreman152254b2015-11-26 14:17:48 -06002840 pixman_region32_union_rect(&surface->pending.damage_surface,
2841 &surface->pending.damage_surface,
2842 x, y, width, height);
2843}
2844
2845static void
2846surface_damage_buffer(struct wl_client *client,
2847 struct wl_resource *resource,
2848 int32_t x, int32_t y, int32_t width, int32_t height)
2849{
2850 struct weston_surface *surface = wl_resource_get_user_data(resource);
2851
2852 if (width <= 0 || height <= 0)
2853 return;
2854
2855 pixman_region32_union_rect(&surface->pending.damage_buffer,
2856 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002857 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002858}
2859
Kristian Høgsberg33418202011-08-16 23:01:28 -04002860static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002861destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002862{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002863 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002864
2865 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002866 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002867}
2868
2869static void
2870surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002871 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002872{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002873 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002874 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002875
2876 cb = malloc(sizeof *cb);
2877 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002878 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002879 return;
2880 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002881
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002882 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2883 callback);
2884 if (cb->resource == NULL) {
2885 free(cb);
2886 wl_resource_post_no_memory(resource);
2887 return;
2888 }
2889
Jason Ekstranda85118c2013-06-27 20:17:02 -05002890 wl_resource_set_implementation(cb->resource, NULL, cb,
2891 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002892
Pekka Paalanenbc106382012-10-10 12:49:31 +03002893 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002894}
2895
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002896static void
2897surface_set_opaque_region(struct wl_client *client,
2898 struct wl_resource *resource,
2899 struct wl_resource *region_resource)
2900{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002901 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002902 struct weston_region *region;
2903
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002904 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002905 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002906 pixman_region32_copy(&surface->pending.opaque,
2907 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002908 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002909 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002910 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002911}
2912
2913static void
2914surface_set_input_region(struct wl_client *client,
2915 struct wl_resource *resource,
2916 struct wl_resource *region_resource)
2917{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002918 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002919 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002920
2921 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002922 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002923 pixman_region32_copy(&surface->pending.input,
2924 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002925 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002926 pixman_region32_fini(&surface->pending.input);
2927 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002928 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002929}
2930
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002931/* Cause damage to this sub-surface and all its children.
2932 *
2933 * This is useful when there are state changes that need an implicit
2934 * damage, e.g. a z-order change.
2935 */
2936static void
2937weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2938{
2939 struct weston_subsurface *child;
2940
2941 weston_surface_damage(sub->surface);
2942 sub->reordered = false;
2943
2944 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2945 if (child != sub)
2946 weston_surface_damage_subsurfaces(child);
2947}
2948
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002949static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002950weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002951{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002952 struct weston_subsurface *sub;
2953
2954 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2955 parent_link_pending) {
2956 wl_list_remove(&sub->parent_link);
2957 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002958
2959 if (sub->reordered)
2960 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002961 }
2962}
2963
2964static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002965weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002966 struct weston_matrix *matrix)
2967{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002968 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002969 double src_width, src_height, dest_width, dest_height;
2970
2971 weston_matrix_init(matrix);
2972
2973 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2974 src_width = surface->width_from_buffer;
2975 src_height = surface->height_from_buffer;
2976 } else {
2977 src_width = wl_fixed_to_double(vp->buffer.src_width);
2978 src_height = wl_fixed_to_double(vp->buffer.src_height);
2979 }
2980
2981 if (vp->surface.width == -1) {
2982 dest_width = src_width;
2983 dest_height = src_height;
2984 } else {
2985 dest_width = vp->surface.width;
2986 dest_height = vp->surface.height;
2987 }
2988
2989 if (src_width != dest_width || src_height != dest_height)
2990 weston_matrix_scale(matrix,
2991 src_width / dest_width,
2992 src_height / dest_height, 1);
2993
2994 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2995 weston_matrix_translate(matrix,
2996 wl_fixed_to_double(vp->buffer.src_x),
2997 wl_fixed_to_double(vp->buffer.src_y),
2998 0);
2999
3000 switch (vp->buffer.transform) {
3001 case WL_OUTPUT_TRANSFORM_FLIPPED:
3002 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3003 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3004 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3005 weston_matrix_scale(matrix, -1, 1, 1);
3006 weston_matrix_translate(matrix,
3007 surface->width_from_buffer, 0, 0);
3008 break;
3009 }
3010
3011 switch (vp->buffer.transform) {
3012 default:
3013 case WL_OUTPUT_TRANSFORM_NORMAL:
3014 case WL_OUTPUT_TRANSFORM_FLIPPED:
3015 break;
3016 case WL_OUTPUT_TRANSFORM_90:
3017 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3018 weston_matrix_rotate_xy(matrix, 0, 1);
3019 weston_matrix_translate(matrix,
3020 surface->height_from_buffer, 0, 0);
3021 break;
3022 case WL_OUTPUT_TRANSFORM_180:
3023 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3024 weston_matrix_rotate_xy(matrix, -1, 0);
3025 weston_matrix_translate(matrix,
3026 surface->width_from_buffer,
3027 surface->height_from_buffer, 0);
3028 break;
3029 case WL_OUTPUT_TRANSFORM_270:
3030 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3031 weston_matrix_rotate_xy(matrix, 0, -1);
3032 weston_matrix_translate(matrix,
3033 0, surface->width_from_buffer, 0);
3034 break;
3035 }
3036
3037 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3038}
3039
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003040/**
3041 * Compute a + b > c while being safe to overflows.
3042 */
3043static bool
3044fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3045{
3046 return (int64_t)a + (int64_t)b > (int64_t)c;
3047}
3048
3049static bool
3050weston_surface_is_pending_viewport_source_valid(
3051 const struct weston_surface *surface)
3052{
3053 const struct weston_surface_state *pend = &surface->pending;
3054 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3055 int width_from_buffer = 0;
3056 int height_from_buffer = 0;
3057 wl_fixed_t w;
3058 wl_fixed_t h;
3059
3060 /* If viewport source rect is not set, it is always ok. */
3061 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3062 return true;
3063
3064 if (pend->newly_attached) {
3065 if (pend->buffer) {
3066 convert_size_by_transform_scale(&width_from_buffer,
3067 &height_from_buffer,
3068 pend->buffer->width,
3069 pend->buffer->height,
3070 vp->buffer.transform,
3071 vp->buffer.scale);
3072 } else {
3073 /* No buffer: viewport is irrelevant. */
3074 return true;
3075 }
3076 } else {
3077 width_from_buffer = surface->width_from_buffer;
3078 height_from_buffer = surface->height_from_buffer;
3079 }
3080
3081 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3082 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3083
3084 /* No buffer: viewport is irrelevant. */
3085 if (width_from_buffer == 0 || height_from_buffer == 0)
3086 return true;
3087
3088 /* overflow checks for wl_fixed_from_int() */
3089 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3090 return false;
3091 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3092 return false;
3093
3094 w = wl_fixed_from_int(width_from_buffer);
3095 h = wl_fixed_from_int(height_from_buffer);
3096
3097 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3098 return false;
3099 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3100 return false;
3101
3102 return true;
3103}
3104
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003105static bool
3106fixed_is_integer(wl_fixed_t v)
3107{
3108 return (v & 0xff) == 0;
3109}
3110
3111static bool
3112weston_surface_is_pending_viewport_dst_size_int(
3113 const struct weston_surface *surface)
3114{
3115 const struct weston_buffer_viewport *vp =
3116 &surface->pending.buffer_viewport;
3117
3118 if (vp->surface.width != -1) {
3119 assert(vp->surface.width > 0 && vp->surface.height > 0);
3120 return true;
3121 }
3122
3123 return fixed_is_integer(vp->buffer.src_width) &&
3124 fixed_is_integer(vp->buffer.src_height);
3125}
3126
Derek Foreman152254b2015-11-26 14:17:48 -06003127/* Translate pending damage in buffer co-ordinates to surface
3128 * co-ordinates and union it with a pixman_region32_t.
3129 * This should only be called after the buffer is attached.
3130 */
3131static void
3132apply_damage_buffer(pixman_region32_t *dest,
3133 struct weston_surface *surface,
3134 struct weston_surface_state *state)
3135{
3136 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3137
3138 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3139 * translated into surface co-ordinates and unioned with
3140 * any other surface damage.
3141 * None of this makes sense if there is no buffer though.
3142 */
3143 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3144 pixman_region32_t buffer_damage;
3145
3146 pixman_region32_intersect_rect(&state->damage_buffer,
3147 &state->damage_buffer,
3148 0, 0, buffer->width,
3149 buffer->height);
3150 pixman_region32_init(&buffer_damage);
3151 weston_matrix_transform_region(&buffer_damage,
3152 &surface->buffer_to_surface_matrix,
3153 &state->damage_buffer);
3154 pixman_region32_union(dest, dest, &buffer_damage);
3155 pixman_region32_fini(&buffer_damage);
3156 }
3157 /* We should clear this on commit even if there was no buffer */
3158 pixman_region32_clear(&state->damage_buffer);
3159}
3160
Jason Ekstrand1e059042014-10-16 10:55:19 -05003161static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003162weston_surface_commit_state(struct weston_surface *surface,
3163 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003164{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003165 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003166 pixman_region32_t opaque;
3167
Alexander Larsson4ea95522013-05-22 14:41:37 +02003168 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003169 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003170 /* wp_viewport.set_source */
3171 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003172 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003173
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003174 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003175 if (state->newly_attached)
3176 weston_surface_attach(surface, state->buffer);
3177 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003178
Jason Ekstrand1e059042014-10-16 10:55:19 -05003179 weston_surface_build_buffer_matrix(surface,
3180 &surface->surface_to_buffer_matrix);
3181 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3182 &surface->surface_to_buffer_matrix);
3183
Jason Ekstrand7b982072014-05-20 14:33:03 -05003184 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003185 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003186 if (surface->committed)
3187 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003188 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003189
Jason Ekstrand7b982072014-05-20 14:33:03 -05003190 state->sx = 0;
3191 state->sy = 0;
3192 state->newly_attached = 0;
3193 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003194
Derek Foreman152254b2015-11-26 14:17:48 -06003195 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003196 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003197 (pixman_region32_not_empty(&state->damage_surface) ||
3198 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003199 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003200
Pekka Paalanen8e159182012-10-10 12:49:25 +03003201 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003202 &state->damage_surface);
3203
3204 apply_damage_buffer(&surface->damage, surface, state);
3205
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003206 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003207 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003208 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003209
3210 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003211 pixman_region32_init(&opaque);
3212 pixman_region32_intersect_rect(&opaque, &state->opaque,
3213 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003214
3215 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3216 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003217 wl_list_for_each(view, &surface->views, surface_link)
3218 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003219 }
3220
3221 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003222
3223 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003224 pixman_region32_intersect_rect(&surface->input, &state->input,
3225 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003226
Pekka Paalanenbc106382012-10-10 12:49:31 +03003227 /* wl_surface.frame */
3228 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003229 &state->frame_callback_list);
3230 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003231
3232 /* XXX:
3233 * What should happen with a feedback request, if there
3234 * is no wl_buffer attached for this commit?
3235 */
3236
3237 /* presentation.feedback */
3238 wl_list_insert_list(&surface->feedback_list,
3239 &state->feedback_list);
3240 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003241
3242 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003243}
3244
3245static void
3246weston_surface_commit(struct weston_surface *surface)
3247{
3248 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003249
Pekka Paalanene67858b2013-04-25 13:57:42 +03003250 weston_surface_commit_subsurface_order(surface);
3251
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003252 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003253}
3254
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003255static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003256weston_subsurface_commit(struct weston_subsurface *sub);
3257
3258static void
3259weston_subsurface_parent_commit(struct weston_subsurface *sub,
3260 int parent_is_synchronized);
3261
3262static void
3263surface_commit(struct wl_client *client, struct wl_resource *resource)
3264{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003265 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003266 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3267
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003268 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3269 assert(surface->viewport_resource);
3270
3271 wl_resource_post_error(surface->viewport_resource,
3272 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3273 "wl_surface@%d has viewport source outside buffer",
3274 wl_resource_get_id(resource));
3275 return;
3276 }
3277
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003278 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3279 assert(surface->viewport_resource);
3280
3281 wl_resource_post_error(surface->viewport_resource,
3282 WP_VIEWPORT_ERROR_BAD_SIZE,
3283 "wl_surface@%d viewport dst size not integer",
3284 wl_resource_get_id(resource));
3285 return;
3286 }
3287
Pekka Paalanene67858b2013-04-25 13:57:42 +03003288 if (sub) {
3289 weston_subsurface_commit(sub);
3290 return;
3291 }
3292
3293 weston_surface_commit(surface);
3294
3295 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3296 if (sub->surface != surface)
3297 weston_subsurface_parent_commit(sub, 0);
3298 }
3299}
3300
3301static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003302surface_set_buffer_transform(struct wl_client *client,
3303 struct wl_resource *resource, int transform)
3304{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003305 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003306
Jonny Lamba55f1392014-05-30 12:07:15 +02003307 /* if wl_output.transform grows more members this will need to be updated. */
3308 if (transform < 0 ||
3309 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3310 wl_resource_post_error(resource,
3311 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3312 "buffer transform must be a valid transform "
3313 "('%d' specified)", transform);
3314 return;
3315 }
3316
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003317 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003318 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003319}
3320
Alexander Larsson4ea95522013-05-22 14:41:37 +02003321static void
3322surface_set_buffer_scale(struct wl_client *client,
3323 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003324 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003325{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003326 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003327
Jonny Lamba55f1392014-05-30 12:07:15 +02003328 if (scale < 1) {
3329 wl_resource_post_error(resource,
3330 WL_SURFACE_ERROR_INVALID_SCALE,
3331 "buffer scale must be at least one "
3332 "('%d' specified)", scale);
3333 return;
3334 }
3335
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003336 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003337 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003338}
3339
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003340static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003341 surface_destroy,
3342 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003343 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003344 surface_frame,
3345 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003346 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003347 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003348 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003349 surface_set_buffer_scale,
3350 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003351};
3352
3353static void
3354compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003355 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003356{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003357 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003358 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003359
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003360 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003361 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003362 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003363 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003364 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003365
Jason Ekstranda85118c2013-06-27 20:17:02 -05003366 surface->resource =
3367 wl_resource_create(client, &wl_surface_interface,
3368 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003369 if (surface->resource == NULL) {
3370 weston_surface_destroy(surface);
3371 wl_resource_post_no_memory(resource);
3372 return;
3373 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003374 wl_resource_set_implementation(surface->resource, &surface_interface,
3375 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003376
3377 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003378}
3379
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003380static void
3381destroy_region(struct wl_resource *resource)
3382{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003383 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003384
3385 pixman_region32_fini(&region->region);
3386 free(region);
3387}
3388
3389static void
3390region_destroy(struct wl_client *client, struct wl_resource *resource)
3391{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003392 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003393}
3394
3395static void
3396region_add(struct wl_client *client, struct wl_resource *resource,
3397 int32_t x, int32_t y, int32_t width, int32_t height)
3398{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003399 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003400
3401 pixman_region32_union_rect(&region->region, &region->region,
3402 x, y, width, height);
3403}
3404
3405static void
3406region_subtract(struct wl_client *client, struct wl_resource *resource,
3407 int32_t x, int32_t y, int32_t width, int32_t height)
3408{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003409 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003410 pixman_region32_t rect;
3411
3412 pixman_region32_init_rect(&rect, x, y, width, height);
3413 pixman_region32_subtract(&region->region, &region->region, &rect);
3414 pixman_region32_fini(&rect);
3415}
3416
3417static const struct wl_region_interface region_interface = {
3418 region_destroy,
3419 region_add,
3420 region_subtract
3421};
3422
3423static void
3424compositor_create_region(struct wl_client *client,
3425 struct wl_resource *resource, uint32_t id)
3426{
3427 struct weston_region *region;
3428
3429 region = malloc(sizeof *region);
3430 if (region == NULL) {
3431 wl_resource_post_no_memory(resource);
3432 return;
3433 }
3434
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003435 pixman_region32_init(&region->region);
3436
Jason Ekstranda85118c2013-06-27 20:17:02 -05003437 region->resource =
3438 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003439 if (region->resource == NULL) {
3440 free(region);
3441 wl_resource_post_no_memory(resource);
3442 return;
3443 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003444 wl_resource_set_implementation(region->resource, &region_interface,
3445 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003446}
3447
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003448static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003449 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003450 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003451};
3452
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003453static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003454weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3455{
3456 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003457
Jason Ekstrand7b982072014-05-20 14:33:03 -05003458 weston_surface_commit_state(surface, &sub->cached);
3459 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003460
3461 weston_surface_commit_subsurface_order(surface);
3462
3463 weston_surface_schedule_repaint(surface);
3464
Jason Ekstrand7b982072014-05-20 14:33:03 -05003465 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003466}
3467
3468static void
3469weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3470{
3471 struct weston_surface *surface = sub->surface;
3472
3473 /*
3474 * If this commit would cause the surface to move by the
3475 * attach(dx, dy) parameters, the old damage region must be
3476 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003477 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003478 */
Derek Foreman152254b2015-11-26 14:17:48 -06003479 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003480 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003481 pixman_region32_union(&sub->cached.damage_surface,
3482 &sub->cached.damage_surface,
3483 &surface->pending.damage_surface);
3484 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003485
3486 if (surface->pending.newly_attached) {
3487 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003488 weston_surface_state_set_buffer(&sub->cached,
3489 surface->pending.buffer);
3490 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003491 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003492 weston_presentation_feedback_discard_list(
3493 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003494 }
3495 sub->cached.sx += surface->pending.sx;
3496 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003497
Derek Foreman152254b2015-11-26 14:17:48 -06003498 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3499
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003500 sub->cached.buffer_viewport.changed |=
3501 surface->pending.buffer_viewport.changed;
3502 sub->cached.buffer_viewport.buffer =
3503 surface->pending.buffer_viewport.buffer;
3504 sub->cached.buffer_viewport.surface =
3505 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003506
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003507 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003508
3509 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3510
3511 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3512
3513 wl_list_insert_list(&sub->cached.frame_callback_list,
3514 &surface->pending.frame_callback_list);
3515 wl_list_init(&surface->pending.frame_callback_list);
3516
Pekka Paalanen133e4392014-09-23 22:08:46 -04003517 wl_list_insert_list(&sub->cached.feedback_list,
3518 &surface->pending.feedback_list);
3519 wl_list_init(&surface->pending.feedback_list);
3520
Jason Ekstrand7b982072014-05-20 14:33:03 -05003521 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003522}
3523
Derek Foreman280e7dd2014-10-03 13:13:42 -05003524static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003525weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3526{
3527 while (sub) {
3528 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003529 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003530
3531 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003532 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003533
3534 sub = weston_surface_to_subsurface(sub->parent);
3535 }
3536
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003537 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003538}
3539
3540static void
3541weston_subsurface_commit(struct weston_subsurface *sub)
3542{
3543 struct weston_surface *surface = sub->surface;
3544 struct weston_subsurface *tmp;
3545
3546 /* Recursive check for effectively synchronized. */
3547 if (weston_subsurface_is_synchronized(sub)) {
3548 weston_subsurface_commit_to_cache(sub);
3549 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003550 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551 /* flush accumulated state from cache */
3552 weston_subsurface_commit_to_cache(sub);
3553 weston_subsurface_commit_from_cache(sub);
3554 } else {
3555 weston_surface_commit(surface);
3556 }
3557
3558 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3559 if (tmp->surface != surface)
3560 weston_subsurface_parent_commit(tmp, 0);
3561 }
3562 }
3563}
3564
3565static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003566weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003567{
3568 struct weston_surface *surface = sub->surface;
3569 struct weston_subsurface *tmp;
3570
Pekka Paalanene67858b2013-04-25 13:57:42 +03003571 /* From now on, commit_from_cache the whole sub-tree, regardless of
3572 * the synchronized mode of each child. This sub-surface or some
3573 * of its ancestors were synchronized, so we are synchronized
3574 * all the way down.
3575 */
3576
Jason Ekstrand7b982072014-05-20 14:33:03 -05003577 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003578 weston_subsurface_commit_from_cache(sub);
3579
3580 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3581 if (tmp->surface != surface)
3582 weston_subsurface_parent_commit(tmp, 1);
3583 }
3584}
3585
3586static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003587weston_subsurface_parent_commit(struct weston_subsurface *sub,
3588 int parent_is_synchronized)
3589{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003590 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003591 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003592 wl_list_for_each(view, &sub->surface->views, surface_link)
3593 weston_view_set_position(view,
3594 sub->position.x,
3595 sub->position.y);
3596
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003597 sub->position.set = 0;
3598 }
3599
3600 if (parent_is_synchronized || sub->synchronized)
3601 weston_subsurface_synchronized_commit(sub);
3602}
3603
Pekka Paalanen8274d902014-08-06 19:36:51 +03003604static int
3605subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3606{
3607 return snprintf(buf, len, "sub-surface");
3608}
3609
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003610static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003611subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003612{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003613 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003614
Jason Ekstranda7af7042013-10-12 22:38:11 -05003615 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003616 weston_view_set_position(view,
3617 view->geometry.x + dx,
3618 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003619
3620 /* No need to check parent mappedness, because if parent is not
3621 * mapped, parent is not in a visible layer, so this sub-surface
3622 * will not be drawn either.
3623 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003624
Pekka Paalanene67858b2013-04-25 13:57:42 +03003625 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003626 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003627
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003628 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003629 * because that would call it also for the parent surface,
3630 * which might not be mapped yet. That would lead to
3631 * inconsistent state, where the window could never be
3632 * mapped.
3633 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003634 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003635 * weston_surface_is_mapped() return true, so that when the
3636 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003637 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003638 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003639 }
3640}
3641
3642static struct weston_subsurface *
3643weston_surface_to_subsurface(struct weston_surface *surface)
3644{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003645 if (surface->committed == subsurface_committed)
3646 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003647
3648 return NULL;
3649}
3650
Pekka Paalanen01388e22013-04-25 13:57:44 +03003651WL_EXPORT struct weston_surface *
3652weston_surface_get_main_surface(struct weston_surface *surface)
3653{
3654 struct weston_subsurface *sub;
3655
3656 while (surface && (sub = weston_surface_to_subsurface(surface)))
3657 surface = sub->parent;
3658
3659 return surface;
3660}
3661
Pekka Paalanen50b67472014-10-01 15:02:41 +03003662WL_EXPORT int
3663weston_surface_set_role(struct weston_surface *surface,
3664 const char *role_name,
3665 struct wl_resource *error_resource,
3666 uint32_t error_code)
3667{
3668 assert(role_name);
3669
3670 if (surface->role_name == NULL ||
3671 surface->role_name == role_name ||
3672 strcmp(surface->role_name, role_name) == 0) {
3673 surface->role_name = role_name;
3674
3675 return 0;
3676 }
3677
3678 wl_resource_post_error(error_resource, error_code,
3679 "Cannot assign role %s to wl_surface@%d,"
3680 " already has role %s\n",
3681 role_name,
3682 wl_resource_get_id(surface->resource),
3683 surface->role_name);
3684 return -1;
3685}
3686
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003687WL_EXPORT const char *
3688weston_surface_get_role(struct weston_surface *surface)
3689{
3690 return surface->role_name;
3691}
3692
Pekka Paalanen8274d902014-08-06 19:36:51 +03003693WL_EXPORT void
3694weston_surface_set_label_func(struct weston_surface *surface,
3695 int (*desc)(struct weston_surface *,
3696 char *, size_t))
3697{
3698 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003699 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003700}
3701
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003702/** Get the size of surface contents
3703 *
3704 * \param surface The surface to query.
3705 * \param width Returns the width of raw contents.
3706 * \param height Returns the height of raw contents.
3707 *
3708 * Retrieves the raw surface content size in pixels for the given surface.
3709 * This is the whole content size in buffer pixels. If the surface
3710 * has no content or the renderer does not implement this feature,
3711 * zeroes are returned.
3712 *
3713 * This function is used to determine the buffer size needed for
3714 * a weston_surface_copy_content() call.
3715 */
3716WL_EXPORT void
3717weston_surface_get_content_size(struct weston_surface *surface,
3718 int *width, int *height)
3719{
3720 struct weston_renderer *rer = surface->compositor->renderer;
3721
3722 if (!rer->surface_get_content_size) {
3723 *width = 0;
3724 *height = 0;
3725 return;
3726 }
3727
3728 rer->surface_get_content_size(surface, width, height);
3729}
3730
Quentin Glidic248dd102016-08-12 10:41:34 +02003731/** Get the bounding box of a surface and its subsurfaces
3732 *
3733 * \param surface The surface to query.
3734 * \return The bounding box relative to the surface origin.
3735 *
3736 */
3737WL_EXPORT struct weston_geometry
3738weston_surface_get_bounding_box(struct weston_surface *surface)
3739{
3740 pixman_region32_t region;
3741 pixman_box32_t *box;
3742 struct weston_subsurface *subsurface;
3743
3744 pixman_region32_init_rect(&region,
3745 0, 0,
3746 surface->width, surface->height);
3747
3748 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3749 pixman_region32_union_rect(&region, &region,
3750 subsurface->position.x,
3751 subsurface->position.y,
3752 subsurface->surface->width,
3753 subsurface->surface->height);
3754
3755 box = pixman_region32_extents(&region);
3756 struct weston_geometry geometry = {
3757 .x = box->x1,
3758 .y = box->y1,
3759 .width = box->x2 - box->x1,
3760 .height = box->y2 - box->y1,
3761 };
3762
3763 pixman_region32_fini(&region);
3764
3765 return geometry;
3766}
3767
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003768/** Copy surface contents to system memory.
3769 *
3770 * \param surface The surface to copy from.
3771 * \param target Pointer to the target memory buffer.
3772 * \param size Size of the target buffer in bytes.
3773 * \param src_x X location on contents to copy from.
3774 * \param src_y Y location on contents to copy from.
3775 * \param width Width in pixels of the area to copy.
3776 * \param height Height in pixels of the area to copy.
3777 * \return 0 for success, -1 for failure.
3778 *
3779 * Surface contents are maintained by the renderer. They can be in a
3780 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3781 * else.
3782 *
3783 * Surface contents are copied into memory pointed to by target,
3784 * which has size bytes of space available. The target memory
3785 * may be larger than needed, but being smaller returns an error.
3786 * The extra bytes in target may or may not be written; their content is
3787 * unspecified. Size must be large enough to hold the image.
3788 *
3789 * The image in the target memory will be arranged in rows from
3790 * top to bottom, and pixels on a row from left to right. The pixel
3791 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3792 * width * 4.
3793 *
3794 * Parameters src_x and src_y define the upper-left corner in buffer
3795 * coordinates (pixels) to copy from. Parameters width and height
3796 * define the size of the area to copy in pixels.
3797 *
3798 * The rectangle defined by src_x, src_y, width, height must fit in
3799 * the surface contents. Otherwise an error is returned.
3800 *
3801 * Use surface_get_data_size to determine the content size; the
3802 * needed target buffer size and rectangle limits.
3803 *
3804 * CURRENT IMPLEMENTATION RESTRICTIONS:
3805 * - the machine must be little-endian due to Pixman formats.
3806 *
3807 * NOTE: Pixman formats are premultiplied.
3808 */
3809WL_EXPORT int
3810weston_surface_copy_content(struct weston_surface *surface,
3811 void *target, size_t size,
3812 int src_x, int src_y,
3813 int width, int height)
3814{
3815 struct weston_renderer *rer = surface->compositor->renderer;
3816 int cw, ch;
3817 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3818
3819 if (!rer->surface_copy_content)
3820 return -1;
3821
3822 weston_surface_get_content_size(surface, &cw, &ch);
3823
3824 if (src_x < 0 || src_y < 0)
3825 return -1;
3826
3827 if (width <= 0 || height <= 0)
3828 return -1;
3829
3830 if (src_x + width > cw || src_y + height > ch)
3831 return -1;
3832
3833 if (width * bytespp * height > size)
3834 return -1;
3835
3836 return rer->surface_copy_content(surface, target, size,
3837 src_x, src_y, width, height);
3838}
3839
Pekka Paalanene67858b2013-04-25 13:57:42 +03003840static void
3841subsurface_set_position(struct wl_client *client,
3842 struct wl_resource *resource, int32_t x, int32_t y)
3843{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003844 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003845
3846 if (!sub)
3847 return;
3848
3849 sub->position.x = x;
3850 sub->position.y = y;
3851 sub->position.set = 1;
3852}
3853
3854static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003855subsurface_find_sibling(struct weston_subsurface *sub,
3856 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003857{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003858 struct weston_surface *parent = sub->parent;
3859 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003860
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003861 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3862 if (sibling->surface == surface && sibling != sub)
3863 return sibling;
3864 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003865
3866 return NULL;
3867}
3868
3869static struct weston_subsurface *
3870subsurface_sibling_check(struct weston_subsurface *sub,
3871 struct weston_surface *surface,
3872 const char *request)
3873{
3874 struct weston_subsurface *sibling;
3875
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003876 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003877 if (!sibling) {
3878 wl_resource_post_error(sub->resource,
3879 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3880 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003881 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003882 return NULL;
3883 }
3884
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003885 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003886
3887 return sibling;
3888}
3889
3890static void
3891subsurface_place_above(struct wl_client *client,
3892 struct wl_resource *resource,
3893 struct wl_resource *sibling_resource)
3894{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003895 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003896 struct weston_surface *surface =
3897 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003898 struct weston_subsurface *sibling;
3899
3900 if (!sub)
3901 return;
3902
3903 sibling = subsurface_sibling_check(sub, surface, "place_above");
3904 if (!sibling)
3905 return;
3906
3907 wl_list_remove(&sub->parent_link_pending);
3908 wl_list_insert(sibling->parent_link_pending.prev,
3909 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003910
3911 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003912}
3913
3914static void
3915subsurface_place_below(struct wl_client *client,
3916 struct wl_resource *resource,
3917 struct wl_resource *sibling_resource)
3918{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003919 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003920 struct weston_surface *surface =
3921 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003922 struct weston_subsurface *sibling;
3923
3924 if (!sub)
3925 return;
3926
3927 sibling = subsurface_sibling_check(sub, surface, "place_below");
3928 if (!sibling)
3929 return;
3930
3931 wl_list_remove(&sub->parent_link_pending);
3932 wl_list_insert(&sibling->parent_link_pending,
3933 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003934
3935 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003936}
3937
3938static void
3939subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3940{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003941 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003942
3943 if (sub)
3944 sub->synchronized = 1;
3945}
3946
3947static void
3948subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3949{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003950 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003951
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003952 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003953 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003954
3955 /* If sub became effectively desynchronized, flush. */
3956 if (!weston_subsurface_is_synchronized(sub))
3957 weston_subsurface_synchronized_commit(sub);
3958 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003959}
3960
3961static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003962weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3963{
3964 wl_list_remove(&sub->parent_link);
3965 wl_list_remove(&sub->parent_link_pending);
3966 wl_list_remove(&sub->parent_destroy_listener.link);
3967 sub->parent = NULL;
3968}
3969
3970static void
3971weston_subsurface_destroy(struct weston_subsurface *sub);
3972
3973static void
3974subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3975{
3976 struct weston_subsurface *sub =
3977 container_of(listener, struct weston_subsurface,
3978 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003979 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003980
3981 /* The protocol object (wl_resource) is left inert. */
3982 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003983 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003984
3985 weston_subsurface_destroy(sub);
3986}
3987
3988static void
3989subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3990{
3991 struct weston_subsurface *sub =
3992 container_of(listener, struct weston_subsurface,
3993 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003994 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003995 assert(sub->surface != sub->parent);
3996
3997 if (weston_surface_is_mapped(sub->surface))
3998 weston_surface_unmap(sub->surface);
3999
4000 weston_subsurface_unlink_parent(sub);
4001}
4002
4003static void
4004subsurface_resource_destroy(struct wl_resource *resource)
4005{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004006 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004007
4008 if (sub)
4009 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004010}
4011
4012static void
4013subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4014{
4015 wl_resource_destroy(resource);
4016}
4017
4018static void
4019weston_subsurface_link_parent(struct weston_subsurface *sub,
4020 struct weston_surface *parent)
4021{
4022 sub->parent = parent;
4023 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004024 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004025 &sub->parent_destroy_listener);
4026
4027 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4028 wl_list_insert(&parent->subsurface_list_pending,
4029 &sub->parent_link_pending);
4030}
4031
4032static void
4033weston_subsurface_link_surface(struct weston_subsurface *sub,
4034 struct weston_surface *surface)
4035{
4036 sub->surface = surface;
4037 sub->surface_destroy_listener.notify =
4038 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004039 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004040 &sub->surface_destroy_listener);
4041}
4042
4043static void
4044weston_subsurface_destroy(struct weston_subsurface *sub)
4045{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004046 struct weston_view *view, *next;
4047
Pekka Paalanene67858b2013-04-25 13:57:42 +03004048 assert(sub->surface);
4049
4050 if (sub->resource) {
4051 assert(weston_surface_to_subsurface(sub->surface) == sub);
4052 assert(sub->parent_destroy_listener.notify ==
4053 subsurface_handle_parent_destroy);
4054
George Kiagiadakised04d382014-06-13 18:10:26 +02004055 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4056 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004057 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004058 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004059
Pekka Paalanene67858b2013-04-25 13:57:42 +03004060 if (sub->parent)
4061 weston_subsurface_unlink_parent(sub);
4062
Jason Ekstrand7b982072014-05-20 14:33:03 -05004063 weston_surface_state_fini(&sub->cached);
4064 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004065
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004066 sub->surface->committed = NULL;
4067 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004068 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004069 } else {
4070 /* the dummy weston_subsurface for the parent itself */
4071 assert(sub->parent_destroy_listener.notify == NULL);
4072 wl_list_remove(&sub->parent_link);
4073 wl_list_remove(&sub->parent_link_pending);
4074 }
4075
4076 wl_list_remove(&sub->surface_destroy_listener.link);
4077 free(sub);
4078}
4079
4080static const struct wl_subsurface_interface subsurface_implementation = {
4081 subsurface_destroy,
4082 subsurface_set_position,
4083 subsurface_place_above,
4084 subsurface_place_below,
4085 subsurface_set_sync,
4086 subsurface_set_desync
4087};
4088
4089static struct weston_subsurface *
4090weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4091 struct weston_surface *parent)
4092{
4093 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004094 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004095
Bryce Harringtonde16d892014-11-20 22:21:57 -08004096 sub = zalloc(sizeof *sub);
4097 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004098 return NULL;
4099
Jason Ekstranda7af7042013-10-12 22:38:11 -05004100 wl_list_init(&sub->unused_views);
4101
Jason Ekstranda85118c2013-06-27 20:17:02 -05004102 sub->resource =
4103 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004104 if (!sub->resource) {
4105 free(sub);
4106 return NULL;
4107 }
4108
Jason Ekstranda85118c2013-06-27 20:17:02 -05004109 wl_resource_set_implementation(sub->resource,
4110 &subsurface_implementation,
4111 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004112 weston_subsurface_link_surface(sub, surface);
4113 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004114 weston_surface_state_init(&sub->cached);
4115 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004116 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004117
4118 return sub;
4119}
4120
4121/* Create a dummy subsurface for having the parent itself in its
4122 * sub-surface lists. Makes stacking order manipulation easy.
4123 */
4124static struct weston_subsurface *
4125weston_subsurface_create_for_parent(struct weston_surface *parent)
4126{
4127 struct weston_subsurface *sub;
4128
Bryce Harringtonde16d892014-11-20 22:21:57 -08004129 sub = zalloc(sizeof *sub);
4130 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004131 return NULL;
4132
4133 weston_subsurface_link_surface(sub, parent);
4134 sub->parent = parent;
4135 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4136 wl_list_insert(&parent->subsurface_list_pending,
4137 &sub->parent_link_pending);
4138
4139 return sub;
4140}
4141
4142static void
4143subcompositor_get_subsurface(struct wl_client *client,
4144 struct wl_resource *resource,
4145 uint32_t id,
4146 struct wl_resource *surface_resource,
4147 struct wl_resource *parent_resource)
4148{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004149 struct weston_surface *surface =
4150 wl_resource_get_user_data(surface_resource);
4151 struct weston_surface *parent =
4152 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004153 struct weston_subsurface *sub;
4154 static const char where[] = "get_subsurface: wl_subsurface@";
4155
4156 if (surface == parent) {
4157 wl_resource_post_error(resource,
4158 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4159 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004160 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004161 return;
4162 }
4163
4164 if (weston_surface_to_subsurface(surface)) {
4165 wl_resource_post_error(resource,
4166 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4167 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004168 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004169 return;
4170 }
4171
Pekka Paalanen50b67472014-10-01 15:02:41 +03004172 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4173 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004174 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004175
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004176 if (weston_surface_get_main_surface(parent) == surface) {
4177 wl_resource_post_error(resource,
4178 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4179 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004180 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004181 return;
4182 }
4183
Pekka Paalanene67858b2013-04-25 13:57:42 +03004184 /* make sure the parent is in its own list */
4185 if (wl_list_empty(&parent->subsurface_list)) {
4186 if (!weston_subsurface_create_for_parent(parent)) {
4187 wl_resource_post_no_memory(resource);
4188 return;
4189 }
4190 }
4191
4192 sub = weston_subsurface_create(id, surface, parent);
4193 if (!sub) {
4194 wl_resource_post_no_memory(resource);
4195 return;
4196 }
4197
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004198 surface->committed = subsurface_committed;
4199 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004200 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004201}
4202
4203static void
4204subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4205{
4206 wl_resource_destroy(resource);
4207}
4208
4209static const struct wl_subcompositor_interface subcompositor_interface = {
4210 subcompositor_destroy,
4211 subcompositor_get_subsurface
4212};
4213
4214static void
4215bind_subcompositor(struct wl_client *client,
4216 void *data, uint32_t version, uint32_t id)
4217{
4218 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004219 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004220
Jason Ekstranda85118c2013-06-27 20:17:02 -05004221 resource =
4222 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004223 if (resource == NULL) {
4224 wl_client_post_no_memory(client);
4225 return;
4226 }
4227 wl_resource_set_implementation(resource, &subcompositor_interface,
4228 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004229}
4230
Bryce Harrington0795ece2016-08-30 12:04:26 -07004231/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004232 *
4233 * \param compositor The compositor instance
4234 * \param state The DPMS state the outputs will be set to
4235 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004236static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004237weston_compositor_dpms(struct weston_compositor *compositor,
4238 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004239{
4240 struct weston_output *output;
4241
Bryce Harrington08976ac2016-08-30 12:05:16 -07004242 wl_list_for_each(output, &compositor->output_list, link)
4243 if (output->set_dpms)
4244 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004245}
4246
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004247/** Restores the compositor to active status
4248 *
4249 * \param compositor The compositor instance
4250 *
4251 * If the compositor was in a sleeping mode, all outputs are powered
4252 * back on via DPMS. Otherwise if the compositor was inactive
4253 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4254 * signal will fire.
4255 *
4256 * Restarts the idle timer.
4257 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004258WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004259weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004260{
Neil Roberts8b62e202013-09-30 13:14:47 +01004261 uint32_t old_state = compositor->state;
4262
4263 /* The state needs to be changed before emitting the wake
4264 * signal because that may try to schedule a repaint which
4265 * will not work if the compositor is still sleeping */
4266 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4267
4268 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004269 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004270 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004271 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004272 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004273 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004274 /* fall through */
4275 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004276 wl_event_source_timer_update(compositor->idle_source,
4277 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004278 }
4279}
4280
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004281/** Turns off rendering and frame events for the compositor.
4282 *
4283 * \param compositor The compositor instance
4284 *
4285 * This is used for example to prevent further rendering while the
4286 * compositor is shutting down.
4287 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004288 * Stops the idle timer.
4289 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004290WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004291weston_compositor_offscreen(struct weston_compositor *compositor)
4292{
4293 switch (compositor->state) {
4294 case WESTON_COMPOSITOR_OFFSCREEN:
4295 return;
4296 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004297 default:
4298 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4299 wl_event_source_timer_update(compositor->idle_source, 0);
4300 }
4301}
4302
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004303/** Powers down all attached output devices
4304 *
4305 * \param compositor The compositor instance
4306 *
4307 * Causes rendering to the outputs to cease, and no frame events to be
4308 * sent. Only powers down the outputs if the compositor is not already
4309 * in sleep mode.
4310 *
4311 * Stops the idle timer.
4312 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004313WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004314weston_compositor_sleep(struct weston_compositor *compositor)
4315{
4316 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4317 return;
4318
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004319 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004320 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4321 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4322}
4323
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004324/** Sets compositor to idle mode
4325 *
4326 * \param data The compositor instance
4327 *
4328 * This is called when the idle timer fires. Once the compositor is in
4329 * idle mode it requires a wake action (e.g. via
4330 * weston_compositor_wake()) to restore it. The compositor's
4331 * idle_signal will be triggered when the idle event occurs.
4332 *
4333 * Idleness can be inhibited by setting the compositor's idle_inhibit
4334 * property.
4335 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004336static int
4337idle_handler(void *data)
4338{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004339 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004340
4341 if (compositor->idle_inhibit)
4342 return 1;
4343
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004344 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004345 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004346
4347 return 1;
4348}
4349
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004350WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004351weston_plane_init(struct weston_plane *plane,
4352 struct weston_compositor *ec,
4353 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004354{
4355 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004356 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004357 plane->x = x;
4358 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004359 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004360
4361 /* Init the link so that the call to wl_list_remove() when releasing
4362 * the plane without ever stacking doesn't lead to a crash */
4363 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004364}
4365
4366WL_EXPORT void
4367weston_plane_release(struct weston_plane *plane)
4368{
Xiong Zhang97116532013-10-23 13:58:31 +08004369 struct weston_view *view;
4370
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004371 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004372 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004373
Xiong Zhang97116532013-10-23 13:58:31 +08004374 wl_list_for_each(view, &plane->compositor->view_list, link) {
4375 if (view->plane == plane)
4376 view->plane = NULL;
4377 }
4378
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004379 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004380}
4381
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004382WL_EXPORT void
4383weston_compositor_stack_plane(struct weston_compositor *ec,
4384 struct weston_plane *plane,
4385 struct weston_plane *above)
4386{
4387 if (above)
4388 wl_list_insert(above->link.prev, &plane->link);
4389 else
4390 wl_list_insert(&ec->plane_list, &plane->link);
4391}
4392
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004393static void
4394output_release(struct wl_client *client, struct wl_resource *resource)
4395{
4396 wl_resource_destroy(resource);
4397}
4398
4399static const struct wl_output_interface output_interface = {
4400 output_release,
4401};
4402
4403
Casey Dahlin9074db52012-04-19 22:50:09 -04004404static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004405{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004406 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004407}
4408
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004409static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004410bind_output(struct wl_client *client,
4411 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004412{
Pekka Paalanen05347622017-03-27 12:24:34 +03004413 struct weston_head *head = data;
4414 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004415 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004416 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004417
Jason Ekstranda85118c2013-06-27 20:17:02 -05004418 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004419 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004420 if (resource == NULL) {
4421 wl_client_post_no_memory(client);
4422 return;
4423 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004424
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004425 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004426 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004427 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004428
Pekka Paalanen05347622017-03-27 12:24:34 +03004429 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004430 wl_output_send_geometry(resource,
4431 output->x,
4432 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004433 head->mm_width,
4434 head->mm_height,
4435 head->subpixel,
4436 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004437 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004438 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004439 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004440 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004441
4442 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004443 wl_output_send_mode(resource,
4444 mode->flags,
4445 mode->width,
4446 mode->height,
4447 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004448 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004449
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004450 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004451 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004452}
4453
Pekka Paalanendcac3512017-12-08 14:13:34 +02004454static void
4455weston_head_add_global(struct weston_head *head)
4456{
4457 head->global = wl_global_create(head->compositor->wl_display,
4458 &wl_output_interface, 3,
4459 head, bind_output);
4460}
4461
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004462/** Remove the global wl_output protocol object
4463 *
4464 * \param head The head whose global to remove.
4465 *
4466 * Also orphans the wl_resources for this head (wl_output).
4467 */
4468static void
4469weston_head_remove_global(struct weston_head *head)
4470{
4471 struct wl_resource *resource, *tmp;
4472
4473 if (head->global)
4474 wl_global_destroy(head->global);
4475 head->global = NULL;
4476
4477 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4478 unbind_resource(resource);
4479 wl_resource_set_destructor(resource, NULL);
4480 wl_resource_set_user_data(resource, NULL);
4481 }
4482}
4483
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004484/** Get the backing object of wl_output
4485 *
4486 * \param resource A wl_output protocol object.
4487 * \return The backing object (user data) of a wl_resource representing a
4488 * wl_output protocol object.
4489 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004490WL_EXPORT struct weston_head *
4491weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004492{
4493 assert(wl_resource_instance_of(resource, &wl_output_interface,
4494 &output_interface));
4495
4496 return wl_resource_get_user_data(resource);
4497}
4498
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004499/** Initialize a pre-allocated weston_head
4500 *
4501 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004502 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004503 *
4504 * The head will be safe to attach, detach and release.
4505 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004506 * The name is used in logs, and can be used by compositors as a configuration
4507 * identifier.
4508 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004509 * \memberof weston_head
4510 * \internal
4511 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004512WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004513weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004514{
4515 /* Add some (in)sane defaults which can be used
4516 * for checking if an output was properly configured
4517 */
4518 memset(head, 0, sizeof *head);
4519
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004520 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004521 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004522 wl_list_init(&head->output_link);
4523 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004524 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004525}
4526
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004527/** Send output heads changed signal
4528 *
4529 * \param output The output that changed.
4530 *
4531 * Notify that the enabled output gained and/or lost heads, or that the
4532 * associated heads may have changed their connection status. This does not
4533 * include cases where the output becomes enabled or disabled. The registered
4534 * callbacks are called after the change has successfully happened.
4535 *
4536 * If connection status change causes the compositor to attach or detach a head
4537 * to an enabled output, the registered callbacks may be called multiple times.
4538 */
4539static void
4540weston_output_emit_heads_changed(struct weston_output *output)
4541{
4542 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4543 output);
4544}
4545
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004546/** Idle task for emitting heads_changed_signal */
4547static void
4548weston_compositor_call_heads_changed(void *data)
4549{
4550 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004551 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004552
4553 compositor->heads_changed_source = NULL;
4554
4555 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004556
4557 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4558 if (head->output && head->output->enabled)
4559 weston_output_emit_heads_changed(head->output);
4560 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004561}
4562
4563/** Schedule a call on idle to heads_changed callback
4564 *
4565 * \param compositor The Compositor.
4566 *
4567 * \memberof weston_compositor
4568 * \internal
4569 */
4570static void
4571weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4572{
4573 struct wl_event_loop *loop;
4574
4575 if (compositor->heads_changed_source)
4576 return;
4577
4578 loop = wl_display_get_event_loop(compositor->wl_display);
4579 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4580 weston_compositor_call_heads_changed, compositor);
4581}
4582
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004583/** Register a new head
4584 *
4585 * \param compositor The compositor.
4586 * \param head The head to register, must not be already registered.
4587 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004588 * This signals the core that a new head has become available, leading to
4589 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004590 *
4591 * \memberof weston_compositor
4592 * \internal
4593 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004594WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004595weston_compositor_add_head(struct weston_compositor *compositor,
4596 struct weston_head *head)
4597{
4598 assert(wl_list_empty(&head->compositor_link));
4599 assert(head->name);
4600
4601 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4602 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004603 weston_compositor_schedule_heads_changed(compositor);
4604}
4605
4606/** Adds a listener to be called when heads change
4607 *
4608 * \param compositor The compositor.
4609 * \param listener The listener to add.
4610 *
4611 * The listener notify function argument is the \var compositor.
4612 *
4613 * The listener function will be called after heads are added or their
4614 * connection status has changed. Several changes may be accumulated into a
4615 * single call. The user is expected to iterate over the existing heads and
4616 * check their statuses to find out what changed.
4617 *
4618 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4619 * weston_head_is_enabled
4620 * \memberof weston_compositor
4621 */
4622WL_EXPORT void
4623weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4624 struct wl_listener *listener)
4625{
4626 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004627}
4628
4629/** Iterate over available heads
4630 *
4631 * \param compositor The compositor.
4632 * \param item The iterator, or NULL for start.
4633 * \return The next available head in the list.
4634 *
4635 * Returns all available heads, regardless of being connected or enabled.
4636 *
4637 * You can iterate over all heads as follows:
4638 * \code
4639 * struct weston_head *head = NULL;
4640 *
4641 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4642 * ...
4643 * }
4644 * \endcode
4645 *
4646 * If you cause \c iter to be removed from the list, you cannot use it to
4647 * continue iterating. Removing any other item is safe.
4648 *
4649 * \memberof weston_compositor
4650 */
4651WL_EXPORT struct weston_head *
4652weston_compositor_iterate_heads(struct weston_compositor *compositor,
4653 struct weston_head *iter)
4654{
4655 struct wl_list *list = &compositor->head_list;
4656 struct wl_list *node;
4657
4658 assert(compositor);
4659 assert(!iter || iter->compositor == compositor);
4660
4661 if (iter)
4662 node = iter->compositor_link.next;
4663 else
4664 node = list->next;
4665
4666 assert(node);
4667 assert(!iter || node != &iter->compositor_link);
4668
4669 if (node == list)
4670 return NULL;
4671
4672 return container_of(node, struct weston_head, compositor_link);
4673}
4674
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004675/** Iterate over attached heads
4676 *
4677 * \param output The output whose heads to iterate.
4678 * \param item The iterator, or NULL for start.
4679 * \return The next attached head in the list.
4680 *
4681 * Returns all heads currently attached to the output.
4682 *
4683 * You can iterate over all heads as follows:
4684 * \code
4685 * struct weston_head *head = NULL;
4686 *
4687 * while ((head = weston_output_iterate_heads(output, head))) {
4688 * ...
4689 * }
4690 * \endcode
4691 *
4692 * If you cause \c iter to be removed from the list, you cannot use it to
4693 * continue iterating. Removing any other item is safe.
4694 *
4695 * \memberof weston_compositor
4696 */
4697WL_EXPORT struct weston_head *
4698weston_output_iterate_heads(struct weston_output *output,
4699 struct weston_head *iter)
4700{
4701 struct wl_list *list = &output->head_list;
4702 struct wl_list *node;
4703
4704 assert(output);
4705 assert(!iter || iter->output == output);
4706
4707 if (iter)
4708 node = iter->output_link.next;
4709 else
4710 node = list->next;
4711
4712 assert(node);
4713 assert(!iter || node != &iter->output_link);
4714
4715 if (node == list)
4716 return NULL;
4717
4718 return container_of(node, struct weston_head, output_link);
4719}
4720
Pekka Paalanendcac3512017-12-08 14:13:34 +02004721/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004722 *
4723 * \param output The output to attach to.
4724 * \param head The head that is not yet attached.
4725 * \return 0 on success, -1 on failure.
4726 *
4727 * Attaches the given head to the output. All heads of an output are clones
4728 * and share the resolution and timings.
4729 *
4730 * Cloning heads this way uses less resources than creating an output for
4731 * each head, but is not always possible due to environment, driver and hardware
4732 * limitations.
4733 *
4734 * On failure, the head remains unattached. Success of this function does not
4735 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004736 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004737 *
4738 * \memberof weston_output
4739 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004740WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004741weston_output_attach_head(struct weston_output *output,
4742 struct weston_head *head)
4743{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004744 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004745
4746 if (!wl_list_empty(&head->output_link))
4747 return -1;
4748
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004749 if (output->attach_head) {
4750 if (output->attach_head(output, head) < 0)
4751 return -1;
4752 } else if (!wl_list_empty(&output->head_list)) {
4753 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004754 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004755 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004756
4757 head->output = output;
4758 wl_list_insert(output->head_list.prev, &head->output_link);
4759
Pekka Paalanendcac3512017-12-08 14:13:34 +02004760 if (output->enabled) {
4761 weston_head_add_global(head);
4762
4763 head_names = weston_output_create_heads_string(output);
4764 weston_log("Output '%s' updated to have head(s) %s\n",
4765 output->name, head_names);
4766 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004767
4768 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02004769 }
4770
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004771 return 0;
4772}
4773
4774/** Detach a head from its output
4775 *
4776 * \param head The head to detach.
4777 *
4778 * It is safe to detach a non-attached head.
4779 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004780 * If the head is attached to an enabled output and the output will be left
4781 * with no heads, the output will be disabled.
4782 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004783 * \memberof weston_head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004784 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004785 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004786WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004787weston_head_detach(struct weston_head *head)
4788{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004789 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02004790 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004791
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004792 wl_list_remove(&head->output_link);
4793 wl_list_init(&head->output_link);
4794 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004795
4796 if (!output)
4797 return;
4798
4799 if (output->detach_head)
4800 output->detach_head(output, head);
4801
4802 if (output->enabled) {
4803 weston_head_remove_global(head);
4804
Pekka Paalanena0106992017-12-08 16:11:17 +02004805 if (wl_list_empty(&output->head_list)) {
4806 weston_log("Output '%s' no heads left, disabling.\n",
4807 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004808 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004809 } else {
4810 head_names = weston_output_create_heads_string(output);
4811 weston_log("Output '%s' updated to have head(s) %s\n",
4812 output->name, head_names);
4813 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004814
4815 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004816 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004817 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004818}
4819
4820/** Destroy a head
4821 *
4822 * \param head The head to be released.
4823 *
4824 * Destroys the head. The caller is responsible for freeing the memory pointed
4825 * to by \c head.
4826 *
4827 * \memberof weston_head
4828 * \internal
4829 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004830WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004831weston_head_release(struct weston_head *head)
4832{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004833 wl_signal_emit(&head->destroy_signal, head);
4834
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004835 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004836
4837 free(head->make);
4838 free(head->model);
4839 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004840 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004841
4842 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004843}
4844
Pekka Paalanene19970f2017-08-28 14:11:02 +03004845static void
4846weston_head_set_device_changed(struct weston_head *head)
4847{
4848 head->device_changed = true;
4849
4850 if (head->compositor)
4851 weston_compositor_schedule_heads_changed(head->compositor);
4852}
4853
4854/** String equal comparison with NULLs being equal */
4855static bool
4856str_null_eq(const char *a, const char *b)
4857{
4858 if (!a && !b)
4859 return true;
4860
4861 if (!!a != !!b)
4862 return false;
4863
4864 return strcmp(a, b) == 0;
4865}
4866
Pekka Paalanen01f60212017-03-24 15:39:24 +02004867/** Store monitor make, model and serial number
4868 *
4869 * \param head The head to modify.
4870 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
4871 * any string, or NULL for none.
4872 * \param model The monitor model or name, or a made-up string, or NULL for
4873 * none.
4874 * \param serialno The monitor serial number, a made-up string, or NULL for
4875 * none.
4876 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004877 * This may set the device_changed flag.
4878 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004879 * \memberof weston_head
4880 * \internal
4881 */
4882WL_EXPORT void
4883weston_head_set_monitor_strings(struct weston_head *head,
4884 const char *make,
4885 const char *model,
4886 const char *serialno)
4887{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004888 if (str_null_eq(head->make, make) &&
4889 str_null_eq(head->model, model) &&
4890 str_null_eq(head->serial_number, serialno))
4891 return;
4892
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004893 free(head->make);
4894 free(head->model);
4895 free(head->serial_number);
4896
4897 head->make = make ? strdup(make) : NULL;
4898 head->model = model ? strdup(model) : NULL;
4899 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004900
4901 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004902}
4903
4904/** Store physical image size
4905 *
4906 * \param head The head to modify.
4907 * \param mm_width Image area width in millimeters.
4908 * \param mm_height Image area height in millimeters.
4909 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004910 * This may set the device_changed flag.
4911 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004912 * \memberof weston_head
4913 * \internal
4914 */
4915WL_EXPORT void
4916weston_head_set_physical_size(struct weston_head *head,
4917 int32_t mm_width, int32_t mm_height)
4918{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004919 if (head->mm_width == mm_width &&
4920 head->mm_height == mm_height)
4921 return;
4922
Pekka Paalanen01f60212017-03-24 15:39:24 +02004923 head->mm_width = mm_width;
4924 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004925
4926 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004927}
4928
4929/** Store monitor sub-pixel layout
4930 *
4931 * \param head The head to modify.
4932 * \param sp Sub-pixel layout. The possible values are:
4933 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
4934 * - WL_OUTPUT_SUBPIXEL_NONE,
4935 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
4936 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
4937 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
4938 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
4939 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004940 * This may set the device_changed flag.
4941 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004942 * \memberof weston_head
4943 * \internal
4944 */
4945WL_EXPORT void
4946weston_head_set_subpixel(struct weston_head *head,
4947 enum wl_output_subpixel sp)
4948{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004949 if (head->subpixel == sp)
4950 return;
4951
Pekka Paalanen01f60212017-03-24 15:39:24 +02004952 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004953
4954 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004955}
4956
4957/** Mark the monitor as internal
4958 *
4959 * This is used for embedded screens, like laptop panels.
4960 *
4961 * \param head The head to mark as internal.
4962 *
4963 * By default a head is external. The type is often inferred from the physical
4964 * connector type.
4965 *
4966 * \memberof weston_head
4967 * \internal
4968 */
4969WL_EXPORT void
4970weston_head_set_internal(struct weston_head *head)
4971{
4972 head->connection_internal = true;
4973}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004974
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004975/** Store connector status
4976 *
4977 * \param head The head to modify.
4978 * \param connected Whether the head is connected.
4979 *
4980 * Connectors are created as disconnected. This function can be used to
4981 * set the connector status.
4982 *
4983 * The status should be set to true when a physical connector is connected to
4984 * a video sink device like a monitor and to false when the connector is
4985 * disconnected. For nested backends, the connection status should reflect the
4986 * connection to the parent display server.
4987 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004988 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03004989 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004990 *
4991 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004992 * \memberof weston_head
4993 * \internal
4994 */
4995WL_EXPORT void
4996weston_head_set_connection_status(struct weston_head *head, bool connected)
4997{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004998 if (head->connected == connected)
4999 return;
5000
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005001 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005002
Pekka Paalanene19970f2017-08-28 14:11:02 +03005003 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005004}
5005
5006/** Is the head currently connected?
5007 *
5008 * \param head The head to query.
5009 * \return Connection status.
5010 *
5011 * Returns true if the head is physically connected to a monitor, or in
5012 * case of a nested backend returns true when there is a connection to the
5013 * parent display server.
5014 *
5015 * This is independent from the head being enabled.
5016 *
5017 * \sa weston_head_is_enabled
5018 * \memberof weston_head
5019 */
5020WL_EXPORT bool
5021weston_head_is_connected(struct weston_head *head)
5022{
5023 return head->connected;
5024}
5025
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005026/** Is the head currently enabled?
5027 *
5028 * \param head The head to query.
5029 * \return Video status.
5030 *
5031 * Returns true if the head is currently transmitting a video stream.
5032 *
5033 * This is independent of the head being connected.
5034 *
5035 * \sa weston_head_is_connected
5036 * \memberof weston_head
5037 */
5038WL_EXPORT bool
5039weston_head_is_enabled(struct weston_head *head)
5040{
5041 if (!head->output)
5042 return false;
5043
5044 return head->output->enabled;
5045}
5046
Pekka Paalanene19970f2017-08-28 14:11:02 +03005047/** Has the device information changed?
5048 *
5049 * \param head The head to query.
5050 * \return True if the device information has changed since last reset.
5051 *
5052 * The information about the connected display device, e.g. a monitor, may
5053 * change without being disconnected in between. Changing information
5054 * causes a call to the heads_changed hook.
5055 *
5056 * The information includes make, model, serial number, physical size,
5057 * and sub-pixel type. The connection status is also included.
5058 *
5059 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
5060 * \memberof weston_head
5061 */
5062WL_EXPORT bool
5063weston_head_is_device_changed(struct weston_head *head)
5064{
5065 return head->device_changed;
5066}
5067
5068/** Acknowledge device information change
5069 *
5070 * \param head The head to acknowledge.
5071 *
5072 * Clears the device changed flag on this head. When a compositor has processed
5073 * device information, it should call this to be able to notice further
5074 * changes.
5075 *
5076 * \sa weston_head_is_device_changed
5077 * \memberof weston_head
5078 */
5079WL_EXPORT void
5080weston_head_reset_device_changed(struct weston_head *head)
5081{
5082 head->device_changed = false;
5083}
5084
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005085/** Get the name of a head
5086 *
5087 * \param head The head to query.
5088 * \return The head's name, not NULL.
5089 *
5090 * The name depends on the backend. The DRM backend uses connector names,
5091 * other backends may use hardcoded names or user-given names.
5092 */
5093WL_EXPORT const char *
5094weston_head_get_name(struct weston_head *head)
5095{
5096 return head->name;
5097}
5098
5099/** Get the output the head is attached to
5100 *
5101 * \param head The head to query.
5102 * \return The output the head is attached to, or NULL if detached.
5103 */
5104WL_EXPORT struct weston_output *
5105weston_head_get_output(struct weston_head *head)
5106{
5107 return head->output;
5108}
5109
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005110/** Add destroy callback for a head
5111 *
5112 * \param head The head to watch for.
5113 * \param listener The listener to add. The \c notify member must be set.
5114 *
5115 * Heads may get destroyed for various reasons by the backends. If a head is
5116 * attached to an output, the compositor should listen for head destruction
5117 * and reconfigure or destroy the output if necessary.
5118 *
5119 * The destroy callbacks will be called on weston_head destruction before any
5120 * automatic detaching from an associated weston_output and before any
5121 * weston_head information is lost.
5122 *
5123 * The \c data argument to the notify callback is the weston_head being
5124 * destroyed.
5125 */
5126WL_EXPORT void
5127weston_head_add_destroy_listener(struct weston_head *head,
5128 struct wl_listener *listener)
5129{
5130 wl_signal_add(&head->destroy_signal, listener);
5131}
5132
5133/** Look up destroy listener for a head
5134 *
5135 * \param head The head to query.
5136 * \param notify The notify function used used for the added destroy listener.
5137 * \return The listener, or NULL if not found.
5138 *
5139 * This looks up the previously added destroy listener struct based on the
5140 * notify function it has. The listener can be used to access user data
5141 * through \c container_of().
5142 *
5143 * \sa wl_signal_get()
5144 */
5145WL_EXPORT struct wl_listener *
5146weston_head_get_destroy_listener(struct weston_head *head,
5147 wl_notify_func_t notify)
5148{
5149 return wl_signal_get(&head->destroy_signal, notify);
5150}
5151
David Fort0de859e2016-05-27 23:22:57 +02005152/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005153static void
David Fort0de859e2016-05-27 23:22:57 +02005154weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5155 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005156{
5157 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005158 bool start_resizing = false;
5159
5160 if (!delta_width)
5161 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005162
5163 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005164 if (output == resized_output) {
5165 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005166 continue;
5167 }
5168
David Fort0de859e2016-05-27 23:22:57 +02005169 if (start_resizing) {
5170 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005171 output->dirty = 1;
5172 }
5173 }
5174}
5175
Pekka Paalanend72bad22017-03-29 17:01:41 +03005176static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005177weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005178{
Scott Moreau850ca422012-05-21 15:21:25 -06005179 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005180
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005181 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005182 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005183
Scott Moreauccbf29d2012-02-22 14:21:41 -07005184 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005185 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005186 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005187 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005188 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005189 weston_matrix_scale(&output->matrix, magnification,
5190 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005191 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005192
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005193 switch (output->transform) {
5194 case WL_OUTPUT_TRANSFORM_FLIPPED:
5195 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5196 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5197 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5198 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5199 weston_matrix_scale(&output->matrix, -1, 1, 1);
5200 break;
5201 }
5202
5203 switch (output->transform) {
5204 default:
5205 case WL_OUTPUT_TRANSFORM_NORMAL:
5206 case WL_OUTPUT_TRANSFORM_FLIPPED:
5207 break;
5208 case WL_OUTPUT_TRANSFORM_90:
5209 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5210 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5211 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5212 break;
5213 case WL_OUTPUT_TRANSFORM_180:
5214 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5215 weston_matrix_translate(&output->matrix,
5216 -output->width, -output->height, 0);
5217 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5218 break;
5219 case WL_OUTPUT_TRANSFORM_270:
5220 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5221 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5222 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5223 break;
5224 }
5225
5226 if (output->current_scale != 1)
5227 weston_matrix_scale(&output->matrix,
5228 output->current_scale,
5229 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005230
Scott Moreauccbf29d2012-02-22 14:21:41 -07005231 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005232
5233 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005234}
5235
Scott Moreau1bad5db2012-08-18 01:04:05 -06005236static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005237weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005238{
5239 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005240 output->native_scale = scale;
5241 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005242
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005243 convert_size_by_transform_scale(&output->width, &output->height,
5244 output->current_mode->width,
5245 output->current_mode->height,
5246 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005247}
5248
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005249static void
5250weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005251{
5252 output->x = x;
5253 output->y = y;
5254
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005255 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005256 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005257
5258 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005259 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005260 output->width,
5261 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005262}
5263
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005264WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005265weston_output_move(struct weston_output *output, int x, int y)
5266{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005267 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005268 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005269 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005270
5271 output->move_x = x - output->x;
5272 output->move_y = y - output->y;
5273
5274 if (output->move_x == 0 && output->move_y == 0)
5275 return;
5276
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005277 weston_output_init_geometry(output, x, y);
5278
5279 output->dirty = 1;
5280
5281 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005282 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005283
5284 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005285 wl_list_for_each(head, &output->head_list, output_link) {
5286 wl_resource_for_each(resource, &head->resource_list) {
5287 wl_output_send_geometry(resource,
5288 output->x,
5289 output->y,
5290 head->mm_width,
5291 head->mm_height,
5292 head->subpixel,
5293 head->make,
5294 head->model,
5295 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005296
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005297 ver = wl_resource_get_version(resource);
5298 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5299 wl_output_send_done(resource);
5300 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005301 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005302}
5303
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005304/** Signal that a pending output is taken into use.
5305 *
5306 * Removes the output from the pending list and adds it to the compositor's
5307 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005308 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005309 * The output gets an internal ID assigned, and the wl_output global is
5310 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005311 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005312 * \param compositor The compositor instance.
5313 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005314 *
5315 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005316 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005317static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005318weston_compositor_add_output(struct weston_compositor *compositor,
5319 struct weston_output *output)
5320{
Armin Krezoviće5403842016-08-05 15:28:29 +02005321 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005322 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005323
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005324 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005325
5326 /* Verify we haven't reached the limit of 32 available output IDs */
5327 assert(ffs(~compositor->output_id_pool) > 0);
5328
5329 /* Invert the output id pool and look for the lowest numbered
5330 * switch (the least significant bit). Take that bit's position
5331 * as our ID, and mark it used in the compositor's output_id_pool.
5332 */
5333 output->id = ffs(~compositor->output_id_pool) - 1;
5334 compositor->output_id_pool |= 1u << output->id;
5335
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005336 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005337 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005338 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005339
Pekka Paalanendcac3512017-12-08 14:13:34 +02005340 wl_list_for_each(head, &output->head_list, output_link)
5341 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005342
Giulio Camuffob1147152015-05-06 21:41:57 +03005343 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005344
5345 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5346 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005347}
5348
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005349/** Transform device coordinates into global coordinates
5350 *
5351 * \param device_x[in] X coordinate in device units.
5352 * \param device_y[in] Y coordinate in device units.
5353 * \param x[out] X coordinate in the global space.
5354 * \param y[out] Y coordinate in the global space.
5355 *
5356 * Transforms coordinates from the device coordinate space
5357 * (physical pixel units) to the global coordinate space (logical pixel units).
5358 * This takes into account output transform and scale.
5359 *
5360 * \memberof weston_output
5361 * \internal
5362 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005363WL_EXPORT void
5364weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005365 double device_x, double device_y,
5366 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005367{
Derek Foreman0f679412014-10-02 13:41:17 -05005368 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005369 device_x,
5370 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005371 0.0,
5372 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005373
Derek Foreman67a18b92015-03-24 11:36:14 -05005374 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005375
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005376 *x = p.f[0] / p.f[3];
5377 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005378}
5379
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005380/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005381 *
5382 * \param output The weston_output object that is being removed.
5383 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005384 * The following happens:
5385 *
5386 * - The output assignments of all views in the current scenegraph are
5387 * recomputed.
5388 *
5389 * - Presentation feedback is discarded.
5390 *
5391 * - Compositor is notified that outputs were changed and
5392 * applies the necessary changes to re-layout outputs.
5393 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005394 * - The output is put back in the pending outputs list.
5395 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005396 * - Signal is emitted to notify all users of the weston_output
5397 * object that the output is being destroyed.
5398 *
5399 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005400 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005401 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005402 * - The output's internal ID is released.
5403 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005404 * \memberof weston_output
5405 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005406 */
5407static void
5408weston_compositor_remove_output(struct weston_output *output)
5409{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005410 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005411 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005412 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005413
5414 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005415 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005416
Pekka Paalanenbccda712017-03-29 16:16:04 +03005417 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005418 if (view->output_mask & (1u << output->id))
5419 weston_view_assign_output(view);
5420 }
5421
5422 weston_presentation_feedback_discard_list(&output->feedback_list);
5423
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005424 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005425
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005426 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005427 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005428 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005429
Pekka Paalanenbccda712017-03-29 16:16:04 +03005430 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005431 wl_signal_emit(&output->destroy_signal, output);
5432
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005433 wl_list_for_each(head, &output->head_list, output_link)
5434 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005435
5436 compositor->output_id_pool &= ~(1u << output->id);
5437 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005438}
5439
5440/** Sets the output scale for a given output.
5441 *
5442 * \param output The weston_output object that the scale is set for.
5443 * \param scale Scale factor for the given output.
5444 *
5445 * It only supports setting scale for an output that
5446 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005447 *
5448 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005449 */
5450WL_EXPORT void
5451weston_output_set_scale(struct weston_output *output,
5452 int32_t scale)
5453{
5454 /* We can only set scale on a disabled output */
5455 assert(!output->enabled);
5456
5457 /* We only want to set scale once */
5458 assert(!output->scale);
5459
5460 output->scale = scale;
5461}
5462
5463/** Sets the output transform for a given output.
5464 *
5465 * \param output The weston_output object that the transform is set for.
5466 * \param transform Transform value for the given output.
5467 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005468 * Refer to wl_output::transform section located at
5469 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5470 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005471 *
5472 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005473 */
5474WL_EXPORT void
5475weston_output_set_transform(struct weston_output *output,
5476 uint32_t transform)
5477{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005478 struct weston_pointer_motion_event ev;
5479 struct wl_resource *resource;
5480 struct weston_seat *seat;
5481 pixman_region32_t old_region;
5482 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005483 struct weston_head *head;
5484 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005485
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005486 if (!output->enabled && output->transform == UINT32_MAX) {
5487 output->transform = transform;
5488 return;
5489 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005490
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005491 weston_output_transform_scale_init(output, transform, output->scale);
5492
5493 pixman_region32_init(&old_region);
5494 pixman_region32_copy(&old_region, &output->region);
5495
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005496 weston_output_init_geometry(output, output->x, output->y);
5497
5498 output->dirty = 1;
5499
5500 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005501 wl_list_for_each(head, &output->head_list, output_link) {
5502 wl_resource_for_each(resource, &head->resource_list) {
5503 wl_output_send_geometry(resource,
5504 output->x,
5505 output->y,
5506 head->mm_width,
5507 head->mm_height,
5508 head->subpixel,
5509 head->make,
5510 head->model,
5511 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005512
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005513 ver = wl_resource_get_version(resource);
5514 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5515 wl_output_send_done(resource);
5516 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005517 }
5518
5519 /* we must ensure that pointers are inside output, otherwise they disappear */
5520 mid_x = output->x + output->width / 2;
5521 mid_y = output->y + output->height / 2;
5522
5523 ev.mask = WESTON_POINTER_MOTION_ABS;
5524 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5525 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5526
5527 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5528 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5529
5530 if (pointer && pixman_region32_contains_point(&old_region,
5531 wl_fixed_to_int(pointer->x),
5532 wl_fixed_to_int(pointer->y),
5533 NULL))
5534 weston_pointer_move(pointer, &ev);
5535 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005536}
5537
5538/** Initializes a weston_output object with enough data so
5539 ** an output can be configured.
5540 *
5541 * \param output The weston_output object to initialize
5542 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005543 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005544 *
5545 * Sets initial values for fields that are expected to be
5546 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005547 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005548 * The name is used in logs, and can be used by compositors as a configuration
5549 * identifier.
5550 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005551 * \memberof weston_output
5552 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005553 */
5554WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005555weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005556 struct weston_compositor *compositor,
5557 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005558{
5559 output->compositor = compositor;
5560 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005561 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005562 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005563 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005564 output->enabled = false;
5565
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005566 wl_list_init(&output->head_list);
5567
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005568 /* Add some (in)sane defaults which can be used
5569 * for checking if an output was properly configured
5570 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005571 output->scale = 0;
5572 /* Can't use -1 on uint32_t and 0 is valid enum value */
5573 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005574
5575 pixman_region32_init(&output->previous_damage);
5576 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005577 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005578}
5579
5580/** Adds weston_output object to pending output list.
5581 *
5582 * \param output The weston_output object to add
5583 * \param compositor The compositor instance.
5584 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005585 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005586 *
5587 * \memberof weston_output
5588 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005589 */
5590WL_EXPORT void
5591weston_compositor_add_pending_output(struct weston_output *output,
5592 struct weston_compositor *compositor)
5593{
Pekka Paalanene952a012017-03-29 17:14:00 +03005594 assert(output->disable);
5595 assert(output->enable);
5596
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005597 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005598 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005599}
5600
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005601/** Create a string with the attached heads' names.
5602 *
5603 * The string must be free()'d.
5604 */
5605static char *
5606weston_output_create_heads_string(struct weston_output *output)
5607{
5608 FILE *fp;
5609 char *str = NULL;
5610 size_t size = 0;
5611 struct weston_head *head;
5612 const char *sep = "";
5613
5614 fp = open_memstream(&str, &size);
5615 if (!fp)
5616 return NULL;
5617
5618 wl_list_for_each(head, &output->head_list, output_link) {
5619 fprintf(fp, "%s%s", sep, head->name);
5620 sep = ", ";
5621 }
5622 fclose(fp);
5623
5624 return str;
5625}
5626
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005627/** Constructs a weston_output object that can be used by the compositor.
5628 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005629 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005630 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005631 *
5632 * Output coordinates are calculated and each new output is by default
5633 * assigned to the right of previous one.
5634 *
5635 * Sets up the transformation, zoom, and geometry of the output using
5636 * the properties that need to be configured by the compositor.
5637 *
5638 * Establishes a repaint timer for the output with the relevant display
5639 * object's event loop. See output_repaint_timer_handler().
5640 *
5641 * The output is assigned an ID. Weston can support up to 32 distinct
5642 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5643 * is referred to and used to find the first available ID number, and
5644 * then this ID is marked as used in output_id_pool.
5645 *
5646 * The output is also assigned a Wayland global with the wl_output
5647 * external interface.
5648 *
5649 * Backend specific function is called to set up the output output.
5650 *
5651 * Output is added to the compositor's output list
5652 *
5653 * If the backend specific function fails, the weston_output object
5654 * is returned to a state it was before calling this function and
5655 * is added to the compositor's pending_output_list in case it needs
5656 * to be reconfigured or just so it can be destroyed at shutdown.
5657 *
5658 * 0 is returned on success, -1 on failure.
5659 */
5660WL_EXPORT int
5661weston_output_enable(struct weston_output *output)
5662{
Armin Krezović782f5df2016-09-30 14:11:11 +02005663 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005664 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005665 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005666 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005667 int x = 0, y = 0;
5668
Pekka Paalanencc201e42017-03-30 15:11:25 +03005669 if (output->enabled) {
5670 weston_log("Error: attempt to enable an enabled output '%s'\n",
5671 output->name);
5672 return -1;
5673 }
5674
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005675 if (wl_list_empty(&output->head_list)) {
5676 weston_log("Error: cannot enable output '%s' without heads.\n",
5677 output->name);
5678 return -1;
5679 }
5680
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005681 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5682 weston_log("Error: no video mode for output '%s'.\n",
5683 output->name);
5684 return -1;
5685 }
5686
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005687 wl_list_for_each(head, &output->head_list, output_link) {
5688 assert(head->make);
5689 assert(head->model);
5690 }
5691
Armin Krezović782f5df2016-09-30 14:11:11 +02005692 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005693 struct weston_output, link);
5694
Armin Krezović782f5df2016-09-30 14:11:11 +02005695 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005696 x = iterator->x + iterator->width;
5697
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005698 /* Make sure the scale is set up */
5699 assert(output->scale);
5700
5701 /* Make sure we have a transform set */
5702 assert(output->transform != UINT32_MAX);
5703
Armin Krezović782f5df2016-09-30 14:11:11 +02005704 output->x = x;
5705 output->y = y;
5706 output->dirty = 1;
5707 output->original_scale = output->scale;
5708
5709 weston_output_transform_scale_init(output, output->transform, output->scale);
5710 weston_output_init_zoom(output);
5711
5712 weston_output_init_geometry(output, x, y);
5713 weston_output_damage(output);
5714
5715 wl_signal_init(&output->frame_signal);
5716 wl_signal_init(&output->destroy_signal);
5717 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005718 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005719
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005720 /* Enable the output (set up the crtc or create a
5721 * window representing the output, set up the
5722 * renderer, etc)
5723 */
5724 if (output->enable(output) < 0) {
5725 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005726 return -1;
5727 }
5728
5729 weston_compositor_add_output(output->compositor, output);
5730
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005731 head_names = weston_output_create_heads_string(output);
5732 weston_log("Output '%s' enabled with head(s) %s\n",
5733 output->name, head_names);
5734 free(head_names);
5735
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005736 return 0;
5737}
5738
5739/** Converts a weston_output object to a pending output state, so it
5740 ** can be configured again or destroyed.
5741 *
5742 * \param output The weston_output object that needs to be disabled.
5743 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005744 * Calls a backend specific function to disable an output, in case
5745 * such function exists.
5746 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005747 * The backend specific disable function may choose to postpone the disabling
5748 * by returning a negative value, in which case this function returns early.
5749 * In that case the backend will guarantee the output will be disabled soon
5750 * by the backend calling this function again. One must not attempt to re-enable
5751 * the output until that happens.
5752 *
5753 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005754 * from weston's output_list (see weston_compositor_remove_output())
5755 * and is returned to a state it was before weston_output_enable()
5756 * was ran (see weston_output_enable_undo()).
5757 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005758 * See weston_output_init() for more information on the
5759 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005760 *
5761 * If the output has never been enabled yet, this function can still be
5762 * called to ensure that the output is actually turned off rather than left
5763 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005764 */
5765WL_EXPORT void
5766weston_output_disable(struct weston_output *output)
5767{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005768 /* Should we rename this? */
5769 output->destroying = 1;
5770
Pekka Paalanenc65df642017-03-29 15:45:46 +03005771 /* Disable is called unconditionally also for not-enabled outputs,
5772 * because at compositor start-up, if there is an output that is
5773 * already on but the compositor wants to turn it off, we have to
5774 * forward the turn-off to the backend so it knows to do it.
5775 * The backend cannot initially turn off everything, because it
5776 * would cause unnecessary mode-sets for all outputs the compositor
5777 * wants to be on.
5778 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005779 if (output->disable(output) < 0)
5780 return;
5781
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005782 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005783 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005784
5785 output->destroying = 0;
5786}
5787
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005788/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005789 *
5790 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005791 *
5792 * If there are new or changed heads, calls the heads_changed hook and
5793 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005794 */
5795WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005796weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005797{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005798 if (compositor->heads_changed_source) {
5799 wl_event_source_remove(compositor->heads_changed_source);
5800 weston_compositor_call_heads_changed(compositor);
5801 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005802}
5803
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005804/** Add destroy callback for an output
5805 *
5806 * \param output The output to watch.
5807 * \param listener The listener to add. The \c notify member must be set.
5808 *
5809 * The listener callback will be called when user destroys an output. This
5810 * may be delayed by a backend in some cases. The main purpose of the
5811 * listener is to allow hooking up custom data to the output. The custom data
5812 * can be fetched via weston_output_get_destroy_listener() followed by
5813 * container_of().
5814 *
5815 * The \c data argument to the notify callback is the weston_output being
5816 * destroyed.
5817 *
5818 * @note This is for the final destruction of an output, not when it gets
5819 * disabled. If you want to keep track of enabled outputs, this is not it.
5820 */
5821WL_EXPORT void
5822weston_output_add_destroy_listener(struct weston_output *output,
5823 struct wl_listener *listener)
5824{
5825 wl_signal_add(&output->user_destroy_signal, listener);
5826}
5827
5828/** Look up destroy listener for an output
5829 *
5830 * \param output The output to query.
5831 * \param notify The notify function used used for the added destroy listener.
5832 * \return The listener, or NULL if not found.
5833 *
5834 * This looks up the previously added destroy listener struct based on the
5835 * notify function it has. The listener can be used to access user data
5836 * through \c container_of().
5837 *
5838 * \sa wl_signal_get() weston_output_add_destroy_listener()
5839 */
5840WL_EXPORT struct wl_listener *
5841weston_output_get_destroy_listener(struct weston_output *output,
5842 wl_notify_func_t notify)
5843{
5844 return wl_signal_get(&output->user_destroy_signal, notify);
5845}
5846
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005847/** Uninitialize an output
5848 *
5849 * Removes the output from the list of enabled outputs if necessary, but
5850 * does not call the backend's output disable function. The output will no
5851 * longer be in the list of pending outputs either.
5852 *
5853 * All fields of weston_output become uninitialized, i.e. should not be used
5854 * anymore. The caller can free the memory after this.
5855 *
5856 * \memberof weston_output
5857 * \internal
5858 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005859WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005860weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005861{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005862 struct weston_head *head, *tmp;
5863
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005864 output->destroying = 1;
5865
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005866 wl_signal_emit(&output->user_destroy_signal, output);
5867
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03005868 if (output->idle_repaint_source)
5869 wl_event_source_remove(output->idle_repaint_source);
5870
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005871 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005872 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005873
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005874 pixman_region32_fini(&output->region);
5875 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005876 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005877
5878 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
5879 weston_head_detach(head);
5880
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005881 free(output->name);
5882}
5883
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005884/** Find an output by its given name
5885 *
5886 * \param compositor The compositor to search in.
5887 * \param name The output name to search for.
5888 * \return An existing output with the given name, or NULL if not found.
5889 *
5890 * \memberof weston_compositor
5891 */
5892WL_EXPORT struct weston_output *
5893weston_compositor_find_output_by_name(struct weston_compositor *compositor,
5894 const char *name)
5895{
5896 struct weston_output *output;
5897
5898 wl_list_for_each(output, &compositor->output_list, link)
5899 if (strcmp(output->name, name) == 0)
5900 return output;
5901
5902 wl_list_for_each(output, &compositor->pending_output_list, link)
5903 if (strcmp(output->name, name) == 0)
5904 return output;
5905
5906 return NULL;
5907}
5908
5909/** Create a named output
5910 *
5911 * \param compositor The compositor.
5912 * \param name The name for the output.
5913 * \return A new \c weston_output, or NULL on failure.
5914 *
5915 * This creates a new weston_output that starts with no heads attached.
5916 *
5917 * An output must be configured and it must have at least one head before
5918 * it can be enabled.
5919 *
5920 * \memberof weston_compositor
5921 */
5922WL_EXPORT struct weston_output *
5923weston_compositor_create_output(struct weston_compositor *compositor,
5924 const char *name)
5925{
5926 assert(compositor->backend->create_output);
5927
5928 if (weston_compositor_find_output_by_name(compositor, name)) {
5929 weston_log("Warning: attempted to create an output with a "
5930 "duplicate name '%s'.\n", name);
5931 return NULL;
5932 }
5933
5934 return compositor->backend->create_output(compositor, name);
5935}
5936
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005937/** Create an output for an unused head
5938 *
5939 * \param compositor The compositor.
5940 * \param head The head to attach to the output.
5941 * \return A new \c weston_output, or NULL on failure.
5942 *
5943 * This creates a new weston_output that starts with the given head attached.
5944 * The output inherits the name of the head. The head must not be already
5945 * attached to another output.
5946 *
5947 * An output must be configured before it can be enabled.
5948 *
5949 * \memberof weston_compositor
5950 */
5951WL_EXPORT struct weston_output *
5952weston_compositor_create_output_with_head(struct weston_compositor *compositor,
5953 struct weston_head *head)
5954{
5955 struct weston_output *output;
5956
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005957 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005958 if (!output)
5959 return NULL;
5960
5961 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03005962 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005963 return NULL;
5964 }
5965
5966 return output;
5967}
5968
5969/** Destroy an output
5970 *
5971 * \param output The output to destroy.
5972 *
5973 * The heads attached to the given output are detached and become unused again.
5974 *
5975 * It is not necessary to explicitly destroy all outputs at compositor exit.
5976 * weston_compositor_destroy() will automatically destroy any remaining
5977 * outputs.
5978 *
5979 * \memberof weston_output
5980 */
5981WL_EXPORT void
5982weston_output_destroy(struct weston_output *output)
5983{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005984 output->destroy(output);
5985}
5986
Pekka Paalanencf0a4762017-04-04 16:36:07 +03005987/** When you need a head...
5988 *
5989 * This function is a hack, used until all code has been converted to become
5990 * multi-head aware.
5991 *
5992 * \param output The weston_output whose head to get.
5993 * \return The first head in the output's list.
5994 */
5995WL_EXPORT struct weston_head *
5996weston_output_get_first_head(struct weston_output *output)
5997{
5998 if (wl_list_empty(&output->head_list))
5999 return NULL;
6000
6001 return container_of(output->head_list.next,
6002 struct weston_head, output_link);
6003}
6004
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006005static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006006destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006007{
Jonny Lamb74130762013-11-26 18:19:46 +01006008 struct weston_surface *surface =
6009 wl_resource_get_user_data(resource);
6010
Pekka Paalanen4826f872016-04-22 14:14:38 +03006011 if (!surface)
6012 return;
6013
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006014 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006015 surface->pending.buffer_viewport.buffer.src_width =
6016 wl_fixed_from_int(-1);
6017 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006018 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006019}
6020
6021static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006022viewport_destroy(struct wl_client *client,
6023 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006024{
6025 wl_resource_destroy(resource);
6026}
6027
6028static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006029viewport_set_source(struct wl_client *client,
6030 struct wl_resource *resource,
6031 wl_fixed_t src_x,
6032 wl_fixed_t src_y,
6033 wl_fixed_t src_width,
6034 wl_fixed_t src_height)
6035{
6036 struct weston_surface *surface =
6037 wl_resource_get_user_data(resource);
6038
Pekka Paalanen4826f872016-04-22 14:14:38 +03006039 if (!surface) {
6040 wl_resource_post_error(resource,
6041 WP_VIEWPORT_ERROR_NO_SURFACE,
6042 "wl_surface for this viewport is no longer exists");
6043 return;
6044 }
6045
6046 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006047 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006048
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006049 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006050 src_height == wl_fixed_from_int(-1) &&
6051 src_x == wl_fixed_from_int(-1) &&
6052 src_y == wl_fixed_from_int(-1)) {
6053 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006054 surface->pending.buffer_viewport.buffer.src_width =
6055 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006056 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006057 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006058 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006059
Pekka Paalanen201769a2016-04-26 14:42:11 +03006060 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006061 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006062 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006063 "wl_surface@%d viewport source "
6064 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6065 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006066 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006067 wl_fixed_to_double(src_height),
6068 wl_fixed_to_double(src_x),
6069 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006070 return;
6071 }
6072
6073 surface->pending.buffer_viewport.buffer.src_x = src_x;
6074 surface->pending.buffer_viewport.buffer.src_y = src_y;
6075 surface->pending.buffer_viewport.buffer.src_width = src_width;
6076 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006077 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006078}
6079
6080static void
6081viewport_set_destination(struct wl_client *client,
6082 struct wl_resource *resource,
6083 int32_t dst_width,
6084 int32_t dst_height)
6085{
6086 struct weston_surface *surface =
6087 wl_resource_get_user_data(resource);
6088
Pekka Paalanen4826f872016-04-22 14:14:38 +03006089 if (!surface) {
6090 wl_resource_post_error(resource,
6091 WP_VIEWPORT_ERROR_NO_SURFACE,
6092 "wl_surface for this viewport no longer exists");
6093 return;
6094 }
6095
6096 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006097
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006098 if (dst_width == -1 && dst_height == -1) {
6099 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006100 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006101 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006102 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006103 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006104
6105 if (dst_width <= 0 || dst_height <= 0) {
6106 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006107 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006108 "destination size must be positive (%dx%d)",
6109 dst_width, dst_height);
6110 return;
6111 }
6112
6113 surface->pending.buffer_viewport.surface.width = dst_width;
6114 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006115 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006116}
6117
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006118static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006119 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006120 viewport_set_source,
6121 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006122};
6123
6124static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006125viewporter_destroy(struct wl_client *client,
6126 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006127{
6128 wl_resource_destroy(resource);
6129}
6130
6131static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006132viewporter_get_viewport(struct wl_client *client,
6133 struct wl_resource *viewporter,
6134 uint32_t id,
6135 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006136{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006137 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006138 struct weston_surface *surface =
6139 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006140 struct wl_resource *resource;
6141
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006142 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006143 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006144 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006145 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006146 return;
6147 }
6148
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006149 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006150 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006151 if (resource == NULL) {
6152 wl_client_post_no_memory(client);
6153 return;
6154 }
6155
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006156 wl_resource_set_implementation(resource, &viewport_interface,
6157 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006158
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006159 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006160}
6161
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006162static const struct wp_viewporter_interface viewporter_interface = {
6163 viewporter_destroy,
6164 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006165};
6166
6167static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006168bind_viewporter(struct wl_client *client,
6169 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006170{
6171 struct wl_resource *resource;
6172
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006173 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006174 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006175 if (resource == NULL) {
6176 wl_client_post_no_memory(client);
6177 return;
6178 }
6179
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006180 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006181 NULL, NULL);
6182}
6183
6184static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006185destroy_presentation_feedback(struct wl_resource *feedback_resource)
6186{
6187 struct weston_presentation_feedback *feedback;
6188
6189 feedback = wl_resource_get_user_data(feedback_resource);
6190
6191 wl_list_remove(&feedback->link);
6192 free(feedback);
6193}
6194
6195static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006196presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6197{
6198 wl_resource_destroy(resource);
6199}
6200
6201static void
6202presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006203 struct wl_resource *presentation_resource,
6204 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006205 uint32_t callback)
6206{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006207 struct weston_surface *surface;
6208 struct weston_presentation_feedback *feedback;
6209
6210 surface = wl_resource_get_user_data(surface_resource);
6211
Bryce Harringtonde16d892014-11-20 22:21:57 -08006212 feedback = zalloc(sizeof *feedback);
6213 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006214 goto err_calloc;
6215
6216 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006217 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006218 1, callback);
6219 if (!feedback->resource)
6220 goto err_create;
6221
6222 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6223 destroy_presentation_feedback);
6224
6225 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6226
6227 return;
6228
6229err_create:
6230 free(feedback);
6231
6232err_calloc:
6233 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006234}
6235
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006236static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006237 presentation_destroy,
6238 presentation_feedback
6239};
6240
6241static void
6242bind_presentation(struct wl_client *client,
6243 void *data, uint32_t version, uint32_t id)
6244{
6245 struct weston_compositor *compositor = data;
6246 struct wl_resource *resource;
6247
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006248 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006249 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006250 if (resource == NULL) {
6251 wl_client_post_no_memory(client);
6252 return;
6253 }
6254
6255 wl_resource_set_implementation(resource, &presentation_implementation,
6256 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006257 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006258}
6259
6260static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006261compositor_bind(struct wl_client *client,
6262 void *data, uint32_t version, uint32_t id)
6263{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006264 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006265 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006266
Jason Ekstranda85118c2013-06-27 20:17:02 -05006267 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006268 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006269 if (resource == NULL) {
6270 wl_client_post_no_memory(client);
6271 return;
6272 }
6273
6274 wl_resource_set_implementation(resource, &compositor_interface,
6275 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006276}
6277
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006278WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006279weston_environment_get_fd(const char *env)
6280{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006281 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006282 int fd, flags;
6283
6284 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006285 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006286 return -1;
6287
6288 flags = fcntl(fd, F_GETFD);
6289 if (flags == -1)
6290 return -1;
6291
6292 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6293 unsetenv(env);
6294
6295 return fd;
6296}
6297
Pekka Paalanenb5026542014-11-12 15:09:24 +02006298static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006299timeline_key_binding_handler(struct weston_keyboard *keyboard,
6300 const struct timespec *time, uint32_t key,
6301 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006302{
6303 struct weston_compositor *compositor = data;
6304
6305 if (weston_timeline_enabled_)
6306 weston_timeline_close();
6307 else
6308 weston_timeline_open(compositor);
6309}
6310
Daniel Stonece62cb32018-07-20 09:46:24 +01006311static const char *
6312output_repaint_status_text(struct weston_output *output)
6313{
6314 switch (output->repaint_status) {
6315 case REPAINT_NOT_SCHEDULED:
6316 return "no repaint";
6317 case REPAINT_BEGIN_FROM_IDLE:
6318 return "start_repaint_loop scheduled";
6319 case REPAINT_SCHEDULED:
6320 return "repaint scheduled";
6321 case REPAINT_AWAITING_COMPLETION:
6322 return "awaiting completion";
6323 }
6324
6325 assert(!"output_repaint_status_text missing enum");
6326 return NULL;
6327}
6328
6329static void
6330debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6331{
6332 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6333 struct wl_shm_buffer *shm;
6334 struct linux_dmabuf_buffer *dmabuf;
6335
6336 if (!buffer) {
6337 fprintf(fp, "\t\t[buffer not available]\n");
6338 return;
6339 }
6340
6341 shm = wl_shm_buffer_get(buffer->resource);
6342 if (shm) {
6343 fprintf(fp, "\t\tSHM buffer\n");
6344 fprintf(fp, "\t\t\tformat: 0x%lx\n",
6345 (unsigned long) wl_shm_buffer_get_format(shm));
6346 return;
6347 }
6348
6349 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6350 if (dmabuf) {
6351 fprintf(fp, "\t\tdmabuf buffer\n");
6352 fprintf(fp, "\t\t\tformat: 0x%lx\n",
6353 (unsigned long) dmabuf->attributes.format);
6354 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6355 (unsigned long long) dmabuf->attributes.modifier[0]);
6356 return;
6357 }
6358
6359 fprintf(fp, "\t\tEGL buffer");
6360}
6361
6362static void
6363debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6364{
6365 struct weston_compositor *ec = view->surface->compositor;
6366 struct weston_output *output;
6367 char desc[512];
6368 pixman_box32_t *box;
6369 uint32_t surface_id = 0;
6370 pid_t pid = 0;
6371
6372 if (view->surface->resource) {
6373 struct wl_resource *resource = view->surface->resource;
6374 wl_client_get_credentials(wl_resource_get_client(resource),
6375 &pid, NULL, NULL);
6376 surface_id = wl_resource_get_id(view->surface->resource);
6377 }
6378
6379 if (!view->surface->get_label ||
6380 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6381 strcpy(desc, "[no description available]");
6382 }
6383 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6384 view_idx, view->surface->role_name, pid, surface_id,
6385 desc, view);
6386
6387 box = pixman_region32_extents(&view->transform.boundingbox);
6388 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6389 box->x1, box->y1, box->x2, box->y2);
6390 box = pixman_region32_extents(&view->transform.opaque);
6391
6392 if (pixman_region32_equal(&view->transform.opaque,
6393 &view->transform.boundingbox)) {
6394 fprintf(fp, "\t\t[fully opaque]\n");
6395 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6396 fprintf(fp, "\t\t[not opaque]\n");
6397 } else {
6398 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6399 box->x1, box->y1, box->x2, box->y2);
6400 }
6401
6402 if (view->alpha < 1.0)
6403 fprintf(fp, "\t\talpha: %f\n", view->alpha);
6404
6405 if (view->output_mask != 0) {
6406 bool first_output = true;
6407 fprintf(fp, "\t\toutputs: ");
6408 wl_list_for_each(output, &ec->output_list, link) {
6409 if (!(view->output_mask & (1 << output->id)))
6410 continue;
6411 fprintf(fp, "%s%d (%s)%s",
6412 (first_output) ? "" : ", ",
6413 output->id, output->name,
6414 (view->output == output) ? " (primary)" : "");
6415 first_output = false;
6416 }
6417 } else {
6418 fprintf(fp, "\t\t[no outputs]");
6419 }
6420
6421 fprintf(fp, "\n");
6422
6423 debug_scene_view_print_buffer(fp, view);
6424}
6425
6426/**
6427 * Output information on how libweston is currently composing the scene
6428 * graph.
6429 */
6430WL_EXPORT char *
6431weston_compositor_print_scene_graph(struct weston_compositor *ec)
6432{
6433 struct weston_output *output;
6434 struct weston_layer *layer;
6435 struct timespec now;
6436 int layer_idx = 0;
6437 FILE *fp;
6438 char *ret;
6439 size_t len;
6440 int err;
6441
6442 fp = open_memstream(&ret, &len);
6443 assert(fp);
6444
6445 weston_compositor_read_presentation_clock(ec, &now);
6446 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
6447 now.tv_sec, now.tv_nsec);
6448
6449 wl_list_for_each(output, &ec->output_list, link) {
6450 struct weston_head *head;
6451 int head_idx = 0;
6452
6453 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
6454 assert(output->enabled);
6455
6456 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
6457 output->x, output->y,
6458 output->x + output->width,
6459 output->y + output->height);
6460 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
6461 output->current_mode->width,
6462 output->current_mode->height,
6463 output->current_mode->refresh / 1000.0);
6464 fprintf(fp, "\tscale: %d\n", output->scale);
6465
6466 fprintf(fp, "\trepaint status: %s\n",
6467 output_repaint_status_text(output));
6468 if (output->repaint_status == REPAINT_SCHEDULED)
6469 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
6470 output->next_repaint.tv_sec,
6471 output->next_repaint.tv_nsec);
6472
6473 wl_list_for_each(head, &output->head_list, output_link) {
6474 fprintf(fp, "\tHead %d (%s): %sconnected\n",
6475 head_idx++, head->name,
6476 (head->connected) ? "" : "not ");
6477 }
6478 }
6479
6480 fprintf(fp, "\n");
6481
6482 wl_list_for_each(layer, &ec->layer_list, link) {
6483 struct weston_view *view;
6484 int view_idx = 0;
6485
6486 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
6487 (unsigned long) layer->position);
6488
6489 if (!weston_layer_mask_is_infinite(layer)) {
6490 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
6491 layer->mask.x1, layer->mask.y1,
6492 layer->mask.x2, layer->mask.y2);
6493 }
6494
6495 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
6496 debug_scene_view_print(fp, view, view_idx++);
6497
6498 if (wl_list_empty(&layer->view_list.link))
6499 fprintf(fp, "\t[no views]\n");
6500
6501 fprintf(fp, "\n");
6502 }
6503
6504 err = fclose(fp);
6505 assert(err == 0);
6506
6507 return ret;
6508}
6509
6510/**
6511 * Called when the 'scene-graph' debug scope is bound by a client. This
6512 * one-shot weston-debug scope prints the current scene graph when bound,
6513 * and then terminates the stream.
6514 */
6515static void
6516debug_scene_graph_cb(struct weston_debug_stream *stream, void *data)
6517{
6518 struct weston_compositor *ec = data;
6519 char *str = weston_compositor_print_scene_graph(ec);
6520
6521 weston_debug_stream_printf(stream, "%s", str);
6522 free(str);
6523 weston_debug_stream_complete(stream);
6524}
6525
Giulio Camuffo459137b2014-10-11 23:56:24 +03006526/** Create the compositor.
6527 *
6528 * This functions creates and initializes a compositor instance.
6529 *
6530 * \param display The Wayland display to be used.
6531 * \param user_data A pointer to an object that can later be retrieved
6532 * using the \ref weston_compositor_get_user_data function.
6533 * \return The compositor instance on success or NULL on failure.
6534 */
6535WL_EXPORT struct weston_compositor *
6536weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006537{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006538 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006539 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006540
Giulio Camuffo459137b2014-10-11 23:56:24 +03006541 ec = zalloc(sizeof *ec);
6542 if (!ec)
6543 return NULL;
6544
6545 ec->wl_display = display;
6546 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006547 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006548 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006549 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006550 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006551 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006552 wl_signal_init(&ec->idle_signal);
6553 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006554 wl_signal_init(&ec->show_input_panel_signal);
6555 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006556 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006557 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006558 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006559 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006560 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006561 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006562 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02006563 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006564 wl_signal_init(&ec->session_signal);
6565 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006566
Casey Dahlin58ba1372012-04-19 22:50:08 -04006567 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006568 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006569
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006570 ec->activate_serial = 1;
6571
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05006572 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
6573
Derek Foreman152254b2015-11-26 14:17:48 -06006574 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006575 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006576 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006577
Giulio Camuffo954f1832014-10-11 18:27:30 +03006578 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006579 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006580 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006581
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006582 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006583 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006584 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006585
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006586 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006587 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006588 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006589
Pekka Paalanena5630ea2017-10-12 13:13:42 +02006590 if (weston_debug_compositor_create(ec) < 0)
6591 goto fail;
6592
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006593 if (weston_input_init(ec) != 0)
6594 goto fail;
6595
Jason Ekstranda7af7042013-10-12 22:38:11 -05006596 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006597 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006598 wl_list_init(&ec->layer_list);
6599 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006600 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006601 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006602 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006603 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006604 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006605 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006606 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006607 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006608 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006609
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006610 wl_list_init(&ec->plugin_api_list);
6611
Xiong Zhang97116532013-10-23 13:58:31 +08006612 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006613 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006614
Giulio Camuffo459137b2014-10-11 23:56:24 +03006615 wl_data_device_manager_init(ec->wl_display);
6616
6617 wl_display_init_shm(ec->wl_display);
6618
6619 loop = wl_display_get_event_loop(ec->wl_display);
6620 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006621 ec->repaint_timer =
6622 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6623 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006624
Quentin Glidic82681572016-12-17 13:40:51 +01006625 weston_layer_init(&ec->fade_layer, ec);
6626 weston_layer_init(&ec->cursor_layer, ec);
6627
6628 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6629 weston_layer_set_position(&ec->cursor_layer,
6630 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006631
6632 weston_compositor_add_debug_binding(ec, KEY_T,
6633 timeline_key_binding_handler, ec);
6634
Daniel Stonece62cb32018-07-20 09:46:24 +01006635 ec->debug_scene =
6636 weston_compositor_add_debug_scope(ec, "scene-graph",
6637 "Scene graph details\n",
6638 debug_scene_graph_cb,
6639 ec);
6640
Giulio Camuffo459137b2014-10-11 23:56:24 +03006641 return ec;
6642
6643fail:
6644 free(ec);
6645 return NULL;
6646}
6647
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006648WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006649weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006650{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006651 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006652
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006653 wl_event_source_remove(ec->idle_source);
6654
Matt Roper361d2ad2011-08-29 13:52:23 -07006655 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006656 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006657 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006658
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006659 /* Destroy all pending outputs associated with this compositor */
6660 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6661 output->destroy(output);
6662
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006663 if (ec->renderer)
6664 ec->renderer->destroy(ec);
6665
Daniel Stone325fc2d2012-05-30 16:31:58 +01006666 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006667 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006668 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006669 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006670 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006671 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006672
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006673 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006674}
6675
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006676WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006677weston_compositor_exit_with_code(struct weston_compositor *compositor,
6678 int exit_code)
6679{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006680 if (compositor->exit_code == EXIT_SUCCESS)
6681 compositor->exit_code = exit_code;
6682
Giulio Camuffo459137b2014-10-11 23:56:24 +03006683 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006684}
6685
6686WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006687weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6688 const struct weston_pointer_grab_interface *interface)
6689{
6690 struct weston_seat *seat;
6691
6692 ec->default_pointer_grab = interface;
6693 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006694 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6695
6696 if (pointer)
6697 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006698 }
6699}
6700
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006701WL_EXPORT int
6702weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6703 clockid_t clk_id)
6704{
6705 struct timespec ts;
6706
6707 if (clock_gettime(clk_id, &ts) < 0)
6708 return -1;
6709
6710 compositor->presentation_clock = clk_id;
6711
6712 return 0;
6713}
6714
6715/*
6716 * For choosing the software clock, when the display hardware or API
6717 * does not expose a compatible presentation timestamp.
6718 */
6719WL_EXPORT int
6720weston_compositor_set_presentation_clock_software(
6721 struct weston_compositor *compositor)
6722{
6723 /* In order of preference */
6724 static const clockid_t clocks[] = {
6725 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6726 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6727 CLOCK_MONOTONIC, /* no jumps, may crawl */
6728 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
6729 CLOCK_REALTIME /* may jump and crawl */
6730 };
6731 unsigned i;
6732
6733 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
6734 if (weston_compositor_set_presentation_clock(compositor,
6735 clocks[i]) == 0)
6736 return 0;
6737
6738 weston_log("Error: no suitable presentation clock available.\n");
6739
6740 return -1;
6741}
6742
Pekka Paalanen662f3842015-03-18 12:17:26 +02006743/** Read the current time from the Presentation clock
6744 *
6745 * \param compositor
6746 * \param ts[out] The current time.
6747 *
6748 * \note Reading the current time in user space is always imprecise to some
6749 * degree.
6750 *
6751 * This function is never meant to fail. If reading the clock does fail,
6752 * an error message is logged and a zero time is returned. Callers are not
6753 * supposed to detect or react to failures.
6754 */
6755WL_EXPORT void
6756weston_compositor_read_presentation_clock(
6757 const struct weston_compositor *compositor,
6758 struct timespec *ts)
6759{
6760 static bool warned;
6761 int ret;
6762
6763 ret = clock_gettime(compositor->presentation_clock, ts);
6764 if (ret < 0) {
6765 ts->tv_sec = 0;
6766 ts->tv_nsec = 0;
6767
6768 if (!warned)
6769 weston_log("Error: failure to read "
6770 "the presentation clock %#x: '%m' (%d)\n",
6771 compositor->presentation_clock, errno);
6772 warned = true;
6773 }
6774}
6775
Pekka Paalanen230f3b12014-09-29 14:18:40 -04006776/** Import dmabuf buffer into current renderer
6777 *
6778 * \param compositor
6779 * \param buffer the dmabuf buffer to import
6780 * \return true on usable buffers, false otherwise
6781 *
6782 * This function tests that the linux_dmabuf_buffer is usable
6783 * for the current renderer. Returns false on unusable buffers. Usually
6784 * usability is tested by importing the dmabufs for composition.
6785 *
6786 * This hook is also used for detecting if the renderer supports
6787 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
6788 * supported.
6789 * */
6790WL_EXPORT bool
6791weston_compositor_import_dmabuf(struct weston_compositor *compositor,
6792 struct linux_dmabuf_buffer *buffer)
6793{
6794 struct weston_renderer *renderer;
6795
6796 renderer = compositor->renderer;
6797
6798 if (renderer->import_dmabuf == NULL)
6799 return false;
6800
6801 return renderer->import_dmabuf(compositor, buffer);
6802}
6803
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006804WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006805weston_version(int *major, int *minor, int *micro)
6806{
6807 *major = WESTON_VERSION_MAJOR;
6808 *minor = WESTON_VERSION_MINOR;
6809 *micro = WESTON_VERSION_MICRO;
6810}
6811
Daniel Stonee03c1112016-11-24 20:45:45 +00006812/**
6813 * Attempts to find a module path from the module map specified in the
6814 * environment. If found, writes the full path into the path variable.
6815 *
6816 * The module map is a string in environment variable WESTON_MODULE_MAP, where
6817 * each entry is of the form "name=path" and entries are separated by
6818 * semicolons. Whitespace is significant.
6819 *
6820 * \param name The name to search for.
6821 * \param path Where the path is written to if found.
6822 * \param path_len Allocated bytes at \c path .
6823 * \returns The length of the string written to path on success, or 0 if the
6824 * module was not specified in the environment map or path_len was too small.
6825 */
6826WL_EXPORT size_t
6827weston_module_path_from_env(const char *name, char *path, size_t path_len)
6828{
6829 const char *mapping = getenv("WESTON_MODULE_MAP");
6830 const char *end;
6831 const int name_len = strlen(name);
6832
6833 if (!mapping)
6834 return 0;
6835
6836 end = mapping + strlen(mapping);
6837 while (mapping < end && *mapping) {
6838 const char *filename, *next;
6839
6840 /* early out: impossibly short string */
6841 if (end - mapping < name_len + 1)
6842 return 0;
6843
6844 filename = &mapping[name_len + 1];
6845 next = strchrnul(mapping, ';');
6846
6847 if (strncmp(mapping, name, name_len) == 0 &&
6848 mapping[name_len] == '=') {
6849 size_t file_len = next - filename; /* no trailing NUL */
6850 if (file_len >= path_len)
6851 return 0;
6852 strncpy(path, filename, file_len);
6853 path[file_len] = '\0';
6854 return file_len;
6855 }
6856
6857 mapping = next + 1;
6858 }
6859
6860 return 0;
6861}
6862
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03006863WL_EXPORT void *
6864weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006865{
6866 char path[PATH_MAX];
6867 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00006868 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006869
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08006870 if (name == NULL)
6871 return NULL;
6872
Derek Foreman3f86e502015-06-08 11:46:54 -05006873 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00006874 len = weston_module_path_from_env(name, path, sizeof path);
6875 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00006876 len = snprintf(path, sizeof path, "%s/%s",
6877 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006878 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00006879 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006880 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006881
Daniel Stonebeb97e52016-11-28 12:13:54 +00006882 /* snprintf returns the length of the string it would've written,
6883 * _excluding_ the NUL byte. So even being equal to the size of
6884 * our buffer is an error here. */
6885 if (len >= sizeof path)
6886 return NULL;
6887
Kristian Høgsberga6813d22012-09-12 12:21:01 -04006888 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
6889 if (module) {
6890 weston_log("Module '%s' already loaded\n", path);
6891 dlclose(module);
6892 return NULL;
6893 }
6894
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006895 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04006896 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006897 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006898 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006899 return NULL;
6900 }
6901
6902 init = dlsym(module, entrypoint);
6903 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006904 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00006905 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006906 return NULL;
6907 }
6908
6909 return init;
6910}
6911
Giulio Camuffo459137b2014-10-11 23:56:24 +03006912
6913/** Destroys the compositor.
6914 *
6915 * This function cleans up the compositor state and destroys it.
6916 *
6917 * \param compositor The compositor to be destroyed.
6918 */
6919WL_EXPORT void
6920weston_compositor_destroy(struct weston_compositor *compositor)
6921{
6922 /* prevent further rendering while shutting down */
6923 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
6924
6925 wl_signal_emit(&compositor->destroy_signal, compositor);
6926
6927 weston_compositor_xkb_destroy(compositor);
6928
Giulio Camuffo2d24e642015-10-03 16:25:15 +03006929 if (compositor->backend)
6930 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006931
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006932 /* The backend is responsible for destroying the heads. */
6933 assert(wl_list_empty(&compositor->head_list));
6934
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006935 weston_plugin_api_destroy_list(compositor);
6936
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006937 if (compositor->heads_changed_source)
6938 wl_event_source_remove(compositor->heads_changed_source);
6939
Daniel Stonece62cb32018-07-20 09:46:24 +01006940 weston_debug_scope_destroy(compositor->debug_scene);
6941 compositor->debug_scene = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02006942 weston_debug_compositor_destroy(compositor);
6943
Giulio Camuffo459137b2014-10-11 23:56:24 +03006944 free(compositor);
6945}
6946
6947/** Instruct the compositor to exit.
6948 *
6949 * This functions does not directly destroy the compositor object, it merely
6950 * command it to start the tear down process. It is not guaranteed that the
6951 * tear down will happen immediately.
6952 *
6953 * \param compositor The compositor to tear down.
6954 */
6955WL_EXPORT void
6956weston_compositor_exit(struct weston_compositor *compositor)
6957{
6958 compositor->exit(compositor);
6959}
6960
6961/** Return the user data stored in the compositor.
6962 *
6963 * This function returns the user data pointer set with user_data parameter
6964 * to the \ref weston_compositor_create function.
6965 */
6966WL_EXPORT void *
6967weston_compositor_get_user_data(struct weston_compositor *compositor)
6968{
6969 return compositor->user_data;
6970}
Pekka Paalanendd186732016-06-03 14:49:54 +03006971
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006972static const char * const backend_map[] = {
6973 [WESTON_BACKEND_DRM] = "drm-backend.so",
6974 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
6975 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
6976 [WESTON_BACKEND_RDP] = "rdp-backend.so",
6977 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
6978 [WESTON_BACKEND_X11] = "x11-backend.so",
6979};
6980
Pekka Paalanendd186732016-06-03 14:49:54 +03006981/** Load a backend into a weston_compositor
6982 *
6983 * A backend must be loaded to make a weston_compositor work. A backend
6984 * provides input and output capabilities, and determines the renderer to use.
6985 *
6986 * \param compositor A compositor that has not had a backend loaded yet.
6987 * \param backend Name of the backend file.
6988 * \param config_base A pointer to a backend-specific configuration
6989 * structure's 'base' member.
6990 *
6991 * \return 0 on success, or -1 on error.
6992 */
6993WL_EXPORT int
6994weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006995 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03006996 struct weston_backend_config *config_base)
6997{
6998 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03006999 struct weston_backend_config *config_base);
7000
Pekka Paalanend7e35112017-08-29 17:04:12 +03007001 if (compositor->backend) {
7002 weston_log("Error: attempt to load a backend when one is already loaded\n");
7003 return -1;
7004 }
7005
Quentin Glidic887c0182016-07-10 11:00:53 +02007006 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007007 return -1;
7008
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007009 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007010 if (!backend_init)
7011 return -1;
7012
Pekka Paalanend7e35112017-08-29 17:04:12 +03007013 if (backend_init(compositor, config_base) < 0) {
7014 compositor->backend = NULL;
7015 return -1;
7016 }
7017
7018 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007019}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007020
7021WL_EXPORT int
7022weston_compositor_load_xwayland(struct weston_compositor *compositor)
7023{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007024 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007025
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007026 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007027 if (!module_init)
7028 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007029 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007030 return -1;
7031 return 0;
7032}