blob: 23e5ef0e1df00321e297d482fd6116e5052f88e5 [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 Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050026 */
27
Kristian Høgsberga9410222011-01-14 17:22:35 -050028#include "config.h"
29
Daniel Stoneb7452fe2012-06-01 12:14:06 +010030#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010035#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020037#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010039#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040040#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020041#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020042#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020043#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050045#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040046#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040047#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040048#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050049#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040050#include <sys/time.h>
51#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030052#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050053
Pekka Paalanenb5026542014-11-12 15:09:24 +020054#include "timeline.h"
55
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030057#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020058#include "presentation-time-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070059#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070060#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070061#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030062#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040063#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050064#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020065#include "plugin-registry.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066
Pekka Paalanen0513a952014-05-21 16:17:27 +030067#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
68
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020069static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030070weston_output_update_matrix(struct weston_output *output);
71
72static void
Alexander Larsson0b135062013-05-28 16:23:36 +020073weston_output_transform_scale_init(struct weston_output *output,
74 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020075
Rob Bradford27b17932013-06-26 18:08:46 +010076static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050077weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010078
Pekka Paalanen6528c032017-03-28 15:27:10 +030079/** Send wl_output events for mode and scale changes
80 *
81 * \param head Send on all resources bound to this head.
82 * \param mode_changed If true, send the current mode.
83 * \param scale_changed If true, send the current scale.
84 */
85static void
86weston_mode_switch_send_events(struct weston_head *head,
87 bool mode_changed, bool scale_changed)
88{
89 struct weston_output *output = head->output;
90 struct wl_resource *resource;
91 int version;
92
93 wl_resource_for_each(resource, &head->resource_list) {
94 if (mode_changed) {
95 wl_output_send_mode(resource,
96 output->current_mode->flags,
97 output->current_mode->width,
98 output->current_mode->height,
99 output->current_mode->refresh);
100 }
101
102 version = wl_resource_get_version(resource);
103 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
104 wl_output_send_scale(resource, output->current_scale);
105
106 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
107 wl_output_send_done(resource);
108 }
109}
110
111static void
112weston_mode_switch_finish(struct weston_output *output,
113 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800114{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200115 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300116 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200117 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200118
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200119 pixman_region32_init(&old_output_region);
120 pixman_region32_copy(&old_output_region, &output->region);
121
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200122 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200123 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200124
125 pixman_region32_init(&output->previous_damage);
126 pixman_region32_init_rect(&output->region, output->x, output->y,
127 output->width, output->height);
128
129 weston_output_update_matrix(output);
130
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200131 /* If a pointer falls outside the outputs new geometry, move it to its
132 * lower-right corner */
133 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500134 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200135 int32_t x, y;
136
137 if (!pointer)
138 continue;
139
140 x = wl_fixed_to_int(pointer->x);
141 y = wl_fixed_to_int(pointer->y);
142
143 if (!pixman_region32_contains_point(&old_output_region,
144 x, y, NULL) ||
145 pixman_region32_contains_point(&output->region,
146 x, y, NULL))
147 continue;
148
149 if (x >= output->x + output->width)
150 x = output->x + output->width - 1;
151 if (y >= output->y + output->height)
152 y = output->y + output->height - 1;
153
154 pointer->x = wl_fixed_from_int(x);
155 pointer->y = wl_fixed_from_int(y);
156 }
157
158 pixman_region32_fini(&old_output_region);
159
Derek Foremandd4cd332014-11-10 10:29:59 -0600160 if (!mode_changed && !scale_changed)
161 return;
162
Hardening57388e42013-09-18 23:56:36 +0200163 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300164 wl_list_for_each(head, &output->head_list, output_link)
165 weston_mode_switch_send_events(head,
166 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600167}
168
David Fort0de859e2016-05-27 23:22:57 +0200169static void
170weston_compositor_reflow_outputs(struct weston_compositor *compositor,
171 struct weston_output *resized_output, int delta_width);
172
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600173WL_EXPORT int
174weston_output_mode_set_native(struct weston_output *output,
175 struct weston_mode *mode,
176 int32_t scale)
177{
178 int ret;
179 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200180 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600181
182 if (!output->switch_mode)
183 return -1;
184
185 if (!output->original_mode) {
186 mode_changed = 1;
187 ret = output->switch_mode(output, mode);
188 if (ret < 0)
189 return ret;
190 if (output->current_scale != scale) {
191 scale_changed = 1;
192 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200193 }
194 }
195
David Fort0de859e2016-05-27 23:22:57 +0200196 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600197 output->native_mode = mode;
198 output->native_scale = scale;
199
200 weston_mode_switch_finish(output, mode_changed, scale_changed);
201
David Fort0de859e2016-05-27 23:22:57 +0200202 if (mode_changed || scale_changed) {
203 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
204
205 wl_signal_emit(&output->compositor->output_resized_signal, output);
206 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600207 return 0;
208}
209
210WL_EXPORT int
211weston_output_mode_switch_to_native(struct weston_output *output)
212{
213 int ret;
214 int mode_changed = 0, scale_changed = 0;
215
216 if (!output->switch_mode)
217 return -1;
218
219 if (!output->original_mode) {
220 weston_log("already in the native mode\n");
221 return -1;
222 }
223 /* the non fullscreen clients haven't seen a mode set since we
224 * switched into a temporary, so we need to notify them if the
225 * mode at that time is different from the native mode now.
226 */
227 mode_changed = (output->original_mode != output->native_mode);
228 scale_changed = (output->original_scale != output->native_scale);
229
230 ret = output->switch_mode(output, output->native_mode);
231 if (ret < 0)
232 return ret;
233
234 output->current_scale = output->native_scale;
235
236 output->original_mode = NULL;
237 output->original_scale = 0;
238
239 weston_mode_switch_finish(output, mode_changed, scale_changed);
240
241 return 0;
242}
243
244WL_EXPORT int
245weston_output_mode_switch_to_temporary(struct weston_output *output,
246 struct weston_mode *mode,
247 int32_t scale)
248{
249 int ret;
250
251 if (!output->switch_mode)
252 return -1;
253
254 /* original_mode is the last mode non full screen clients have seen,
255 * so we shouldn't change it if we already have one set.
256 */
257 if (!output->original_mode) {
258 output->original_mode = output->native_mode;
259 output->original_scale = output->native_scale;
260 }
261 ret = output->switch_mode(output, mode);
262 if (ret < 0)
263 return ret;
264
265 output->current_scale = scale;
266
267 weston_mode_switch_finish(output, 0, 0);
268
269 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800270}
271
Benjamin Franzke06286262011-05-06 19:12:33 +0200272static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300273region_init_infinite(pixman_region32_t *region)
274{
275 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
276 UINT32_MAX, UINT32_MAX);
277}
278
Pekka Paalanene67858b2013-04-25 13:57:42 +0300279static struct weston_subsurface *
280weston_surface_to_subsurface(struct weston_surface *surface);
281
Jason Ekstranda7af7042013-10-12 22:38:11 -0500282WL_EXPORT struct weston_view *
283weston_view_create(struct weston_surface *surface)
284{
285 struct weston_view *view;
286
Bryce Harringtonde16d892014-11-20 22:21:57 -0800287 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500288 if (view == NULL)
289 return NULL;
290
291 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000292 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500293
Jason Ekstranda7af7042013-10-12 22:38:11 -0500294 /* Assign to surface */
295 wl_list_insert(&surface->views, &view->surface_link);
296
297 wl_signal_init(&view->destroy_signal);
298 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300299 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500300
Jason Ekstranda7af7042013-10-12 22:38:11 -0500301 pixman_region32_init(&view->clip);
302
303 view->alpha = 1.0;
304 pixman_region32_init(&view->transform.opaque);
305
306 wl_list_init(&view->geometry.transformation_list);
307 wl_list_insert(&view->geometry.transformation_list,
308 &view->transform.position.link);
309 weston_matrix_init(&view->transform.position.matrix);
310 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200311 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500312 pixman_region32_init(&view->transform.boundingbox);
313 view->transform.dirty = 1;
314
Jason Ekstranda7af7042013-10-12 22:38:11 -0500315 return view;
316}
317
Jason Ekstrand108865d2014-06-26 10:04:49 -0700318struct weston_frame_callback {
319 struct wl_resource *resource;
320 struct wl_list link;
321};
322
Pekka Paalanen133e4392014-09-23 22:08:46 -0400323struct weston_presentation_feedback {
324 struct wl_resource *resource;
325
326 /* XXX: could use just wl_resource_get_link() instead */
327 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200328
329 /* The per-surface feedback flags */
330 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400331};
332
333static void
334weston_presentation_feedback_discard(
335 struct weston_presentation_feedback *feedback)
336{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200337 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400338 wl_resource_destroy(feedback->resource);
339}
340
341static void
342weston_presentation_feedback_discard_list(struct wl_list *list)
343{
344 struct weston_presentation_feedback *feedback, *tmp;
345
346 wl_list_for_each_safe(feedback, tmp, list, link)
347 weston_presentation_feedback_discard(feedback);
348}
349
350static void
351weston_presentation_feedback_present(
352 struct weston_presentation_feedback *feedback,
353 struct weston_output *output,
354 uint32_t refresh_nsec,
355 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200356 uint64_t seq,
357 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400358{
359 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300360 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400361 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200362 uint32_t tv_sec_hi;
363 uint32_t tv_sec_lo;
364 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300365 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400366
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300367 wl_list_for_each(head, &output->head_list, output_link) {
368 wl_resource_for_each(o, &head->resource_list) {
369 if (wl_resource_get_client(o) != client)
370 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400371
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300372 wp_presentation_feedback_send_sync_output(feedback->resource, o);
373 done = true;
374 }
375
376 /* For clone mode, send it for just one wl_output global,
377 * they are all equivalent anyway.
378 */
379 if (done)
380 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400381 }
382
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200383 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200384 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200385 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200386 refresh_nsec,
387 seq >> 32, seq & 0xffffffff,
388 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400389 wl_resource_destroy(feedback->resource);
390}
391
392static void
393weston_presentation_feedback_present_list(struct wl_list *list,
394 struct weston_output *output,
395 uint32_t refresh_nsec,
396 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200397 uint64_t seq,
398 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400399{
400 struct weston_presentation_feedback *feedback, *tmp;
401
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200402 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200403 wl_list_empty(list));
404
Pekka Paalanen133e4392014-09-23 22:08:46 -0400405 wl_list_for_each_safe(feedback, tmp, list, link)
406 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200407 refresh_nsec, ts, seq,
408 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400409}
410
Jason Ekstrand7b982072014-05-20 14:33:03 -0500411static void
412surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
413{
414 struct weston_surface_state *state =
415 container_of(listener, struct weston_surface_state,
416 buffer_destroy_listener);
417
418 state->buffer = NULL;
419}
420
421static void
422weston_surface_state_init(struct weston_surface_state *state)
423{
424 state->newly_attached = 0;
425 state->buffer = NULL;
426 state->buffer_destroy_listener.notify =
427 surface_state_handle_buffer_destroy;
428 state->sx = 0;
429 state->sy = 0;
430
Derek Foreman152254b2015-11-26 14:17:48 -0600431 pixman_region32_init(&state->damage_surface);
432 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500433 pixman_region32_init(&state->opaque);
434 region_init_infinite(&state->input);
435
436 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400437 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500438
439 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
440 state->buffer_viewport.buffer.scale = 1;
441 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
442 state->buffer_viewport.surface.width = -1;
443 state->buffer_viewport.changed = 0;
444}
445
446static void
447weston_surface_state_fini(struct weston_surface_state *state)
448{
449 struct weston_frame_callback *cb, *next;
450
451 wl_list_for_each_safe(cb, next,
452 &state->frame_callback_list, link)
453 wl_resource_destroy(cb->resource);
454
Pekka Paalanen133e4392014-09-23 22:08:46 -0400455 weston_presentation_feedback_discard_list(&state->feedback_list);
456
Jason Ekstrand7b982072014-05-20 14:33:03 -0500457 pixman_region32_fini(&state->input);
458 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600459 pixman_region32_fini(&state->damage_surface);
460 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500461
462 if (state->buffer)
463 wl_list_remove(&state->buffer_destroy_listener.link);
464 state->buffer = NULL;
465}
466
467static void
468weston_surface_state_set_buffer(struct weston_surface_state *state,
469 struct weston_buffer *buffer)
470{
471 if (state->buffer == buffer)
472 return;
473
474 if (state->buffer)
475 wl_list_remove(&state->buffer_destroy_listener.link);
476 state->buffer = buffer;
477 if (state->buffer)
478 wl_signal_add(&state->buffer->destroy_signal,
479 &state->buffer_destroy_listener);
480}
481
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500482WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500483weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500484{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500485 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400486
Bryce Harringtonde16d892014-11-20 22:21:57 -0800487 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400488 if (surface == NULL)
489 return NULL;
490
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500491 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800492 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500493
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500494 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200495 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400496
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200497 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
498 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200499 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
500 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500501
502 weston_surface_state_init(&surface->pending);
503
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400504 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500505 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300506 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400507
Jason Ekstranda7af7042013-10-12 22:38:11 -0500508 wl_list_init(&surface->views);
509
510 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400511 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500512
Pekka Paalanene67858b2013-04-25 13:57:42 +0300513 wl_list_init(&surface->subsurface_list);
514 wl_list_init(&surface->subsurface_list_pending);
515
Jason Ekstrand1e059042014-10-16 10:55:19 -0500516 weston_matrix_init(&surface->buffer_to_surface_matrix);
517 weston_matrix_init(&surface->surface_to_buffer_matrix);
518
Jonas Ådahld3414f22016-07-22 17:56:31 +0800519 wl_list_init(&surface->pointer_constraints);
520
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400521 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500522}
523
Alex Wu8811bf92012-02-28 18:07:54 +0800524WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500525weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200526 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500527{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100528 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500529}
530
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400531WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500532weston_view_to_global_float(struct weston_view *view,
533 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200534{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500535 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200536 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
537
Jason Ekstranda7af7042013-10-12 22:38:11 -0500538 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200539
540 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200541 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700542 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200543 v.f[3]);
544 *x = 0;
545 *y = 0;
546 return;
547 }
548
549 *x = v.f[0] / v.f[3];
550 *y = v.f[1] / v.f[3];
551 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500552 *x = sx + view->geometry.x;
553 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200554 }
555}
556
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500557WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200558weston_transformed_coord(int width, int height,
559 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200560 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200561 float sx, float sy, float *bx, float *by)
562{
563 switch (transform) {
564 case WL_OUTPUT_TRANSFORM_NORMAL:
565 default:
566 *bx = sx;
567 *by = sy;
568 break;
569 case WL_OUTPUT_TRANSFORM_FLIPPED:
570 *bx = width - sx;
571 *by = sy;
572 break;
573 case WL_OUTPUT_TRANSFORM_90:
574 *bx = height - sy;
575 *by = sx;
576 break;
577 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
578 *bx = height - sy;
579 *by = width - sx;
580 break;
581 case WL_OUTPUT_TRANSFORM_180:
582 *bx = width - sx;
583 *by = height - sy;
584 break;
585 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
586 *bx = sx;
587 *by = height - sy;
588 break;
589 case WL_OUTPUT_TRANSFORM_270:
590 *bx = sy;
591 *by = width - sx;
592 break;
593 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
594 *bx = sy;
595 *by = sx;
596 break;
597 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200598
599 *bx *= scale;
600 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200601}
602
603WL_EXPORT pixman_box32_t
604weston_transformed_rect(int width, int height,
605 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200606 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200607 pixman_box32_t rect)
608{
609 float x1, x2, y1, y2;
610
611 pixman_box32_t ret;
612
Alexander Larsson4ea95522013-05-22 14:41:37 +0200613 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200614 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200615 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200616 rect.x2, rect.y2, &x2, &y2);
617
618 if (x1 <= x2) {
619 ret.x1 = x1;
620 ret.x2 = x2;
621 } else {
622 ret.x1 = x2;
623 ret.x2 = x1;
624 }
625
626 if (y1 <= y2) {
627 ret.y1 = y1;
628 ret.y2 = y2;
629 } else {
630 ret.y1 = y2;
631 ret.y2 = y1;
632 }
633
634 return ret;
635}
636
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600637/** Transform a region by a matrix, restricted to axis-aligned transformations
638 *
639 * Warning: This function does not work for projective, affine, or matrices
640 * that encode arbitrary rotations. Only 90-degree step rotations are
641 * supported.
642 */
643WL_EXPORT void
644weston_matrix_transform_region(pixman_region32_t *dest,
645 struct weston_matrix *matrix,
646 pixman_region32_t *src)
647{
648 pixman_box32_t *src_rects, *dest_rects;
649 int nrects, i;
650
651 src_rects = pixman_region32_rectangles(src, &nrects);
652 dest_rects = malloc(nrects * sizeof(*dest_rects));
653 if (!dest_rects)
654 return;
655
656 for (i = 0; i < nrects; i++) {
657 struct weston_vector vec1 = {{
658 src_rects[i].x1, src_rects[i].y1, 0, 1
659 }};
660 weston_matrix_transform(matrix, &vec1);
661 vec1.f[0] /= vec1.f[3];
662 vec1.f[1] /= vec1.f[3];
663
664 struct weston_vector vec2 = {{
665 src_rects[i].x2, src_rects[i].y2, 0, 1
666 }};
667 weston_matrix_transform(matrix, &vec2);
668 vec2.f[0] /= vec2.f[3];
669 vec2.f[1] /= vec2.f[3];
670
671 if (vec1.f[0] < vec2.f[0]) {
672 dest_rects[i].x1 = floor(vec1.f[0]);
673 dest_rects[i].x2 = ceil(vec2.f[0]);
674 } else {
675 dest_rects[i].x1 = floor(vec2.f[0]);
676 dest_rects[i].x2 = ceil(vec1.f[0]);
677 }
678
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600679 if (vec1.f[1] < vec2.f[1]) {
680 dest_rects[i].y1 = floor(vec1.f[1]);
681 dest_rects[i].y2 = ceil(vec2.f[1]);
682 } else {
683 dest_rects[i].y1 = floor(vec2.f[1]);
684 dest_rects[i].y2 = ceil(vec1.f[1]);
685 }
686 }
687
688 pixman_region32_clear(dest);
689 pixman_region32_init_rects(dest, dest_rects, nrects);
690 free(dest_rects);
691}
692
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200693WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500694weston_transformed_region(int width, int height,
695 enum wl_output_transform transform,
696 int32_t scale,
697 pixman_region32_t *src, pixman_region32_t *dest)
698{
699 pixman_box32_t *src_rects, *dest_rects;
700 int nrects, i;
701
702 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
703 if (src != dest)
704 pixman_region32_copy(dest, src);
705 return;
706 }
707
708 src_rects = pixman_region32_rectangles(src, &nrects);
709 dest_rects = malloc(nrects * sizeof(*dest_rects));
710 if (!dest_rects)
711 return;
712
713 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
714 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
715 } else {
716 for (i = 0; i < nrects; i++) {
717 switch (transform) {
718 default:
719 case WL_OUTPUT_TRANSFORM_NORMAL:
720 dest_rects[i].x1 = src_rects[i].x1;
721 dest_rects[i].y1 = src_rects[i].y1;
722 dest_rects[i].x2 = src_rects[i].x2;
723 dest_rects[i].y2 = src_rects[i].y2;
724 break;
725 case WL_OUTPUT_TRANSFORM_90:
726 dest_rects[i].x1 = height - src_rects[i].y2;
727 dest_rects[i].y1 = src_rects[i].x1;
728 dest_rects[i].x2 = height - src_rects[i].y1;
729 dest_rects[i].y2 = src_rects[i].x2;
730 break;
731 case WL_OUTPUT_TRANSFORM_180:
732 dest_rects[i].x1 = width - src_rects[i].x2;
733 dest_rects[i].y1 = height - src_rects[i].y2;
734 dest_rects[i].x2 = width - src_rects[i].x1;
735 dest_rects[i].y2 = height - src_rects[i].y1;
736 break;
737 case WL_OUTPUT_TRANSFORM_270:
738 dest_rects[i].x1 = src_rects[i].y1;
739 dest_rects[i].y1 = width - src_rects[i].x2;
740 dest_rects[i].x2 = src_rects[i].y2;
741 dest_rects[i].y2 = width - src_rects[i].x1;
742 break;
743 case WL_OUTPUT_TRANSFORM_FLIPPED:
744 dest_rects[i].x1 = width - src_rects[i].x2;
745 dest_rects[i].y1 = src_rects[i].y1;
746 dest_rects[i].x2 = width - src_rects[i].x1;
747 dest_rects[i].y2 = src_rects[i].y2;
748 break;
749 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
750 dest_rects[i].x1 = height - src_rects[i].y2;
751 dest_rects[i].y1 = width - src_rects[i].x2;
752 dest_rects[i].x2 = height - src_rects[i].y1;
753 dest_rects[i].y2 = width - src_rects[i].x1;
754 break;
755 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
756 dest_rects[i].x1 = src_rects[i].x1;
757 dest_rects[i].y1 = height - src_rects[i].y2;
758 dest_rects[i].x2 = src_rects[i].x2;
759 dest_rects[i].y2 = height - src_rects[i].y1;
760 break;
761 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
762 dest_rects[i].x1 = src_rects[i].y1;
763 dest_rects[i].y1 = src_rects[i].x1;
764 dest_rects[i].x2 = src_rects[i].y2;
765 dest_rects[i].y2 = src_rects[i].x2;
766 break;
767 }
768 }
769 }
770
771 if (scale != 1) {
772 for (i = 0; i < nrects; i++) {
773 dest_rects[i].x1 *= scale;
774 dest_rects[i].x2 *= scale;
775 dest_rects[i].y1 *= scale;
776 dest_rects[i].y2 *= scale;
777 }
778 }
779
780 pixman_region32_clear(dest);
781 pixman_region32_init_rects(dest, dest_rects, nrects);
782 free(dest_rects);
783}
784
Jonny Lamb74130762013-11-26 18:19:46 +0100785static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300786viewport_surface_to_buffer(struct weston_surface *surface,
787 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100788{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200789 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200790 double src_width, src_height;
791 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200792
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200793 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
794 if (vp->surface.width == -1) {
795 *bx = sx;
796 *by = sy;
797 return;
798 }
Jonny Lamb74130762013-11-26 18:19:46 +0100799
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200800 src_x = 0.0;
801 src_y = 0.0;
802 src_width = surface->width_from_buffer;
803 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100804 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200805 src_x = wl_fixed_to_double(vp->buffer.src_x);
806 src_y = wl_fixed_to_double(vp->buffer.src_y);
807 src_width = wl_fixed_to_double(vp->buffer.src_width);
808 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100809 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200810
811 *bx = sx * src_width / surface->width + src_x;
812 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100813}
814
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500815WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200816weston_surface_to_buffer_float(struct weston_surface *surface,
817 float sx, float sy, float *bx, float *by)
818{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200819 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
820
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300821 /* first transform coordinates if the viewport is set */
822 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100823
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500824 weston_transformed_coord(surface->width_from_buffer,
825 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200826 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100827 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200828}
829
Derek Foreman99739672015-12-03 16:38:11 -0600830/** Transform a rectangle from surface coordinates to buffer coordinates
831 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300832 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600833 * from.
834 * \param rect The rectangle to transform.
835 * \return The transformed rectangle.
836 *
837 * Viewport and buffer transformations can only do translation, scaling,
838 * and rotations in 90-degree steps. Therefore the only loss in the
839 * conversion is coordinate rounding.
840 *
841 * However, some coordinate rounding takes place as an intermediate
842 * step before the buffer scale factor is applied, so the rectangle
843 * boundary may not be exactly as expected.
844 *
845 * This is OK for damage tracking since a little extra coverage is
846 * not a problem.
847 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200848WL_EXPORT pixman_box32_t
849weston_surface_to_buffer_rect(struct weston_surface *surface,
850 pixman_box32_t rect)
851{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200852 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100853 float xf, yf;
854
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300855 /* first transform box coordinates if the viewport is set */
856 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100857 rect.x1 = floorf(xf);
858 rect.y1 = floorf(yf);
859
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300860 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600861 rect.x2 = ceilf(xf);
862 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100863
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500864 return weston_transformed_rect(surface->width_from_buffer,
865 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200866 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200867 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200868}
869
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200870/** Transform a region from surface coordinates to buffer coordinates
871 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300872 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200873 * from.
874 * \param surface_region[in] The region in surface coordinates.
875 * \param buffer_region[out] The region converted to buffer coordinates.
876 *
877 * Buffer_region must be init'd, but will be completely overwritten.
878 *
879 * Viewport and buffer transformations can only do translation, scaling,
880 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600881 * conversion is from the coordinate rounding that takes place in
882 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200883 */
884WL_EXPORT void
885weston_surface_to_buffer_region(struct weston_surface *surface,
886 pixman_region32_t *surface_region,
887 pixman_region32_t *buffer_region)
888{
889 pixman_box32_t *src_rects, *dest_rects;
890 int nrects, i;
891
892 src_rects = pixman_region32_rectangles(surface_region, &nrects);
893 dest_rects = malloc(nrects * sizeof(*dest_rects));
894 if (!dest_rects)
895 return;
896
897 for (i = 0; i < nrects; i++) {
898 dest_rects[i] = weston_surface_to_buffer_rect(surface,
899 src_rects[i]);
900 }
901
902 pixman_region32_fini(buffer_region);
903 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
904 free(dest_rects);
905}
906
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200907WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500908weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400909 struct weston_plane *plane)
910{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400912 return;
913
Jason Ekstranda7af7042013-10-12 22:38:11 -0500914 weston_view_damage_below(view);
915 view->plane = plane;
916 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400917}
918
Pekka Paalanen51723d52015-02-17 13:10:01 +0200919/** Inflict damage on the plane where the view is visible.
920 *
921 * \param view The view that causes the damage.
922 *
923 * If the view is currently on a plane (including the primary plane),
924 * take the view's boundingbox, subtract all the opaque views that cover it,
925 * and add the remaining region as damage to the plane. This corresponds
926 * to the damage inflicted to the plane if this view disappeared.
927 *
928 * A repaint is scheduled for this view.
929 *
930 * The region of all opaque views covering this view is stored in
931 * weston_view::clip and updated by view_accumulate_damage() during
932 * weston_output_repaint(). Specifically, that region matches the
933 * scenegraph as it was last painted.
934 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400935WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500936weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200937{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500938 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200939
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500940 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200941 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500942 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800943 if (view->plane)
944 pixman_region32_union(&view->plane->damage,
945 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500946 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800947 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200948}
949
Pekka Paalanen01e00682017-03-24 16:21:06 +0200950/** Send wl_surface.enter/leave events
951 *
952 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300953 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200954 * \param enter True if entered.
955 * \param left True if left.
956 *
957 * Send the enter/leave events for all protocol objects bound to the given
958 * output by the client owning the surface.
959 */
960static void
961weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300962 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +0200963 bool enter,
964 bool leave)
965{
966 struct wl_resource *wloutput;
967 struct wl_client *client;
968
969 assert(enter != leave);
970
971 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300972 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +0200973 if (wl_resource_get_client(wloutput) != client)
974 continue;
975
976 if (enter)
977 wl_surface_send_enter(surface->resource, wloutput);
978 if (leave)
979 wl_surface_send_leave(surface->resource, wloutput);
980 }
981}
982
Bryce Harrington3f650b82015-12-23 11:01:58 -0800983/**
984 * \param es The surface
985 * \param mask The new set of outputs for the surface
986 *
987 * Sets the surface's set of outputs to the ones specified by
988 * the new output mask provided. Identifies the outputs that
989 * have changed, the posts enter and leave events for these
990 * outputs as appropriate.
991 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400992static void
993weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
994{
995 uint32_t different = es->output_mask ^ mask;
996 uint32_t entered = mask & different;
997 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +0200998 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400999 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001000 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001001
1002 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001003 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001004 return;
1005 if (different == 0)
1006 return;
1007
1008 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001009 output_bit = 1u << output->id;
1010 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001011 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001012
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001013 wl_list_for_each(head, &output->head_list, output_link) {
1014 weston_surface_send_enter_leave(es, head,
1015 output_bit & entered,
1016 output_bit & left);
1017 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001018 }
1019}
1020
Bryce Harrington3f650b82015-12-23 11:01:58 -08001021/** Recalculate which output(s) the surface has views displayed on
1022 *
1023 * \param es The surface to remap to outputs
1024 *
1025 * Finds the output that is showing the largest amount of one
1026 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001027 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001028 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001029 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001030 * in the output_mask for the surface.
1031 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001032static void
1033weston_surface_assign_output(struct weston_surface *es)
1034{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035 struct weston_output *new_output;
1036 struct weston_view *view;
1037 pixman_region32_t region;
1038 uint32_t max, area, mask;
1039 pixman_box32_t *e;
1040
1041 new_output = NULL;
1042 max = 0;
1043 mask = 0;
1044 pixman_region32_init(&region);
1045 wl_list_for_each(view, &es->views, surface_link) {
1046 if (!view->output)
1047 continue;
1048
1049 pixman_region32_intersect(&region, &view->transform.boundingbox,
1050 &view->output->region);
1051
1052 e = pixman_region32_extents(&region);
1053 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1054
1055 mask |= view->output_mask;
1056
1057 if (area >= max) {
1058 new_output = view->output;
1059 max = area;
1060 }
1061 }
1062 pixman_region32_fini(&region);
1063
1064 es->output = new_output;
1065 weston_surface_update_output_mask(es, mask);
1066}
1067
Bryce Harrington3f650b82015-12-23 11:01:58 -08001068/** Recalculate which output(s) the view is displayed on
1069 *
1070 * \param ev The view to remap to outputs
1071 *
1072 * Identifies the set of outputs that the view is visible on,
1073 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001074 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001075 *
1076 * Also does the same for the view's surface. See
1077 * weston_surface_assign_output().
1078 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079static void
1080weston_view_assign_output(struct weston_view *ev)
1081{
1082 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001083 struct weston_output *output, *new_output;
1084 pixman_region32_t region;
1085 uint32_t max, area, mask;
1086 pixman_box32_t *e;
1087
1088 new_output = NULL;
1089 max = 0;
1090 mask = 0;
1091 pixman_region32_init(&region);
1092 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001093 if (output->destroying)
1094 continue;
1095
Jason Ekstranda7af7042013-10-12 22:38:11 -05001096 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001097 &output->region);
1098
1099 e = pixman_region32_extents(&region);
1100 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1101
1102 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001103 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001104
1105 if (area >= max) {
1106 new_output = output;
1107 max = area;
1108 }
1109 }
1110 pixman_region32_fini(&region);
1111
Jason Ekstranda7af7042013-10-12 22:38:11 -05001112 ev->output = new_output;
1113 ev->output_mask = mask;
1114
1115 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001116}
1117
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001118static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001119weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1120 int from_x, int from_y, int *to_x, int *to_y)
1121{
1122 float x, y;
1123
1124 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1125 weston_view_from_global_float(to, x, y, &x, &y);
1126
1127 *to_x = round(x);
1128 *to_y = round(y);
1129}
1130
1131static void
1132weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1133{
1134 pixman_box32_t *a;
1135 pixman_box32_t b;
1136
1137 a = pixman_region32_extents(&from->geometry.scissor);
1138
1139 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1140 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1141
1142 pixman_region32_fini(&to->geometry.scissor);
1143 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1144}
1145
1146static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001147view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001149{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001150 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1151 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001152 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001153 { inbox->x1, inbox->y1 },
1154 { inbox->x1, inbox->y2 },
1155 { inbox->x2, inbox->y1 },
1156 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001157 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001158 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001159 int i;
1160
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001161 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001162 /* avoid rounding empty bbox to 1x1 */
1163 pixman_region32_init(bbox);
1164 return;
1165 }
1166
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001167 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001168 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001170 if (x < min_x)
1171 min_x = x;
1172 if (x > max_x)
1173 max_x = x;
1174 if (y < min_y)
1175 min_y = y;
1176 if (y > max_y)
1177 max_y = y;
1178 }
1179
Pekka Paalanen219b9822012-02-08 15:38:37 +02001180 int_x = floorf(min_x);
1181 int_y = floorf(min_y);
1182 pixman_region32_init_rect(bbox, int_x, int_y,
1183 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001184}
1185
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001186static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001188{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001190
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001191 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 view->geometry.x = roundf(view->geometry.x);
1193 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001194
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001195 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1197 view->transform.position.matrix.d[12] = view->geometry.x;
1198 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001199
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001201
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202 view->transform.inverse = view->transform.position.matrix;
1203 view->transform.inverse.d[12] = -view->geometry.x;
1204 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001205
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001207 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001208 view->surface->width,
1209 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001210 if (view->geometry.scissor_enabled)
1211 pixman_region32_intersect(&view->transform.boundingbox,
1212 &view->transform.boundingbox,
1213 &view->geometry.scissor);
1214
1215 pixman_region32_translate(&view->transform.boundingbox,
1216 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001217
Jason Ekstranda7af7042013-10-12 22:38:11 -05001218 if (view->alpha == 1.0) {
1219 pixman_region32_copy(&view->transform.opaque,
1220 &view->surface->opaque);
1221 pixman_region32_translate(&view->transform.opaque,
1222 view->geometry.x,
1223 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001224 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001225}
1226
1227static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001229{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 struct weston_view *parent = view->geometry.parent;
1231 struct weston_matrix *matrix = &view->transform.matrix;
1232 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001233 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001234 pixman_region32_t surfregion;
1235 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001236
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001238
1239 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1241 view->transform.position.matrix.d[12] = view->geometry.x;
1242 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001243
1244 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001246 weston_matrix_multiply(matrix, &tform->matrix);
1247
Pekka Paalanen483243f2013-03-08 14:56:50 +02001248 if (parent)
1249 weston_matrix_multiply(matrix, &parent->transform.matrix);
1250
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001251 if (weston_matrix_invert(inverse, matrix) < 0) {
1252 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 weston_log("error: weston_view %p"
1254 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001255 return -1;
1256 }
1257
Giulio Camuffo148c1992016-09-04 18:50:46 +03001258 if (view->alpha == 1.0 &&
1259 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1260 pixman_region32_copy(&view->transform.opaque,
1261 &view->surface->opaque);
1262 pixman_region32_translate(&view->transform.opaque,
1263 matrix->d[12],
1264 matrix->d[13]);
1265 }
1266
Pekka Paalanen380adf52015-02-16 14:39:11 +02001267 pixman_region32_init_rect(&surfregion, 0, 0,
1268 view->surface->width, view->surface->height);
1269 if (view->geometry.scissor_enabled)
1270 pixman_region32_intersect(&surfregion, &surfregion,
1271 &view->geometry.scissor);
1272 surfbox = pixman_region32_extents(&surfregion);
1273
1274 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1275 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001276
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001277 return 0;
1278}
1279
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001280static struct weston_layer *
1281get_view_layer(struct weston_view *view)
1282{
1283 if (view->parent_view)
1284 return get_view_layer(view->parent_view);
1285 return view->layer_link.layer;
1286}
1287
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001288WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001290{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001292 struct weston_layer *layer;
1293 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001294
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001296 return;
1297
Pekka Paalanen483243f2013-03-08 14:56:50 +02001298 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001302
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001304
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 pixman_region32_fini(&view->transform.boundingbox);
1306 pixman_region32_fini(&view->transform.opaque);
1307 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001308
Pekka Paalanencd403622012-01-25 13:37:39 +02001309 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 if (view->geometry.transformation_list.next ==
1311 &view->transform.position.link &&
1312 view->geometry.transformation_list.prev ==
1313 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001314 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001316 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317 if (weston_view_update_transform_enable(view) < 0)
1318 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001319 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001320
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001321 layer = get_view_layer(view);
1322 if (layer) {
1323 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001324 pixman_region32_intersect(&view->transform.boundingbox,
1325 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001326 pixman_region32_intersect(&view->transform.opaque,
1327 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001328 pixman_region32_fini(&mask);
1329 }
1330
Pekka Paalanen380adf52015-02-16 14:39:11 +02001331 if (parent) {
1332 if (parent->geometry.scissor_enabled) {
1333 view->geometry.scissor_enabled = true;
1334 weston_view_transfer_scissor(parent, view);
1335 } else {
1336 view->geometry.scissor_enabled = false;
1337 }
1338 }
1339
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001341
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001343
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 wl_signal_emit(&view->surface->compositor->transform_signal,
1345 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001346}
1347
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001348WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001350{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001351 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001352
1353 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354 * The invariant: if view->geometry.dirty, then all views
1355 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001356 * Corollary: if not parent->geometry.dirty, then all ancestors
1357 * are not dirty.
1358 */
1359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001361 return;
1362
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001364
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001366 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001368}
1369
1370WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371weston_view_to_global_fixed(struct weston_view *view,
1372 wl_fixed_t vx, wl_fixed_t vy,
1373 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001374{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001375 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001376
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 weston_view_to_global_float(view,
1378 wl_fixed_to_double(vx),
1379 wl_fixed_to_double(vy),
1380 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001381 *x = wl_fixed_from_double(xf);
1382 *y = wl_fixed_from_double(yf);
1383}
1384
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001385WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386weston_view_from_global_float(struct weston_view *view,
1387 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001388{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001390 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1391
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001393
1394 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001395 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001397 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 *vx = 0;
1399 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001400 return;
1401 }
1402
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403 *vx = v.f[0] / v.f[3];
1404 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001405 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 *vx = x - view->geometry.x;
1407 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001408 }
1409}
1410
1411WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001412weston_view_from_global_fixed(struct weston_view *view,
1413 wl_fixed_t x, wl_fixed_t y,
1414 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001415{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001417
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 weston_view_from_global_float(view,
1419 wl_fixed_to_double(x),
1420 wl_fixed_to_double(y),
1421 &vxf, &vyf);
1422 *vx = wl_fixed_from_double(vxf);
1423 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001424}
1425
1426WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001427weston_view_from_global(struct weston_view *view,
1428 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001429{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001430 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1433 *vx = floorf(vxf);
1434 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001435}
1436
Bryce Harrington3f650b82015-12-23 11:01:58 -08001437/**
1438 * \param surface The surface to be repainted
1439 *
1440 * Marks the output(s) that the surface is shown on as needing to be
1441 * repainted. See weston_output_schedule_repaint().
1442 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001443WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001444weston_surface_schedule_repaint(struct weston_surface *surface)
1445{
1446 struct weston_output *output;
1447
1448 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001449 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001450 weston_output_schedule_repaint(output);
1451}
1452
Bryce Harrington3f650b82015-12-23 11:01:58 -08001453/**
1454 * \param view The view to be repainted
1455 *
1456 * Marks the output(s) that the view is shown on as needing to be
1457 * repainted. See weston_output_schedule_repaint().
1458 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001459WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460weston_view_schedule_repaint(struct weston_view *view)
1461{
1462 struct weston_output *output;
1463
1464 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001465 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 weston_output_schedule_repaint(output);
1467}
1468
Pekka Paalanene508ce62015-02-19 13:59:55 +02001469/**
1470 * XXX: This function does it the wrong way.
1471 * surface->damage is the damage from the client, and causes
1472 * surface_flush_damage() to copy pixels. No window management action can
1473 * cause damage to the client-provided content, warranting re-upload!
1474 *
1475 * Instead of surface->damage, this function should record the damage
1476 * with all the views for this surface to avoid extraneous texture
1477 * uploads.
1478 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001479WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001480weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001481{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001482 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 0, 0, surface->width,
1484 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001485
Kristian Høgsberg98238702012-08-03 16:29:12 -04001486 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001487}
1488
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001489WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001491{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001492 if (view->geometry.x == x && view->geometry.y == y)
1493 return;
1494
Jason Ekstranda7af7042013-10-12 22:38:11 -05001495 view->geometry.x = x;
1496 view->geometry.y = y;
1497 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001498}
1499
Pekka Paalanen483243f2013-03-08 14:56:50 +02001500static void
1501transform_parent_handle_parent_destroy(struct wl_listener *listener,
1502 void *data)
1503{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001504 struct weston_view *view =
1505 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001506 geometry.parent_destroy_listener);
1507
Jason Ekstranda7af7042013-10-12 22:38:11 -05001508 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001509}
1510
1511WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001512weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001513 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001514{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515 if (view->geometry.parent) {
1516 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1517 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001518
1519 if (!parent)
1520 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001521 }
1522
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001524
Jason Ekstranda7af7042013-10-12 22:38:11 -05001525 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001526 transform_parent_handle_parent_destroy;
1527 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001528 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001530 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001532 }
1533
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 weston_view_geometry_dirty(view);
1535}
1536
Pekka Paalanen380adf52015-02-16 14:39:11 +02001537/** Set a clip mask rectangle on a view
1538 *
1539 * \param view The view to set the clip mask on.
1540 * \param x Top-left corner X coordinate of the clip rectangle.
1541 * \param y Top-left corner Y coordinate of the clip rectangle.
1542 * \param width Width of the clip rectangle, non-negative.
1543 * \param height Height of the clip rectangle, non-negative.
1544 *
1545 * A shell may set a clip mask rectangle on a view. Everything outside
1546 * the rectangle is cut away for input and output purposes: it is
1547 * not drawn and cannot be hit by hit-test based input like pointer
1548 * motion or touch-downs. Everything inside the rectangle will behave
1549 * normally. Clients are unaware of clipping.
1550 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001551 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001552 * mask rectangle does not affect the view position, the view is positioned
1553 * as it would be without a clip. The clip also does not change
1554 * weston_surface::width,height.
1555 *
1556 * The clip mask rectangle is part of transformation inheritance
1557 * (weston_view_set_transform_parent()). A clip set in the root of the
1558 * transformation inheritance tree will affect all views in the tree.
1559 * A clip can be set only on the root view. Attempting to set a clip
1560 * on view that has a transformation parent will fail. Assigning a parent
1561 * to a view that has a clip set will cause the clip to be forgotten.
1562 *
1563 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1564 * on the additional transformations in the child views. These transformations
1565 * may not rotate the coordinate axes, i.e., only translation and scaling
1566 * are allowed. Violating this restriction causes the clipping to malfunction.
1567 * Furthermore, using scaling may cause rounding errors in child clipping.
1568 *
1569 * The clip mask rectangle is not automatically adjusted based on
1570 * wl_surface.attach dx and dy arguments.
1571 *
1572 * A clip mask rectangle can be set only if the compositor capability
1573 * WESTON_CAP_VIEW_CLIP_MASK is present.
1574 *
1575 * This function sets the clip mask rectangle and schedules a repaint for
1576 * the view.
1577 */
1578WL_EXPORT void
1579weston_view_set_mask(struct weston_view *view,
1580 int x, int y, int width, int height)
1581{
1582 struct weston_compositor *compositor = view->surface->compositor;
1583
1584 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1585 weston_log("%s not allowed without capability!\n", __func__);
1586 return;
1587 }
1588
1589 if (view->geometry.parent) {
1590 weston_log("view %p has a parent, clip forbidden!\n", view);
1591 return;
1592 }
1593
1594 if (width < 0 || height < 0) {
1595 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1596 x, y, width, height);
1597 return;
1598 }
1599
1600 pixman_region32_fini(&view->geometry.scissor);
1601 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1602 view->geometry.scissor_enabled = true;
1603 weston_view_geometry_dirty(view);
1604 weston_view_schedule_repaint(view);
1605}
1606
1607/** Remove the clip mask from a view
1608 *
1609 * \param view The view to remove the clip mask from.
1610 *
1611 * Removed the clip mask rectangle and schedules a repaint.
1612 *
1613 * \sa weston_view_set_mask
1614 */
1615WL_EXPORT void
1616weston_view_set_mask_infinite(struct weston_view *view)
1617{
1618 view->geometry.scissor_enabled = false;
1619 weston_view_geometry_dirty(view);
1620 weston_view_schedule_repaint(view);
1621}
1622
Armin Krezović0da12b82016-06-30 06:04:33 +02001623/* Check if view should be displayed
1624 *
1625 * The indicator is set manually when assigning
1626 * a view to a surface.
1627 *
1628 * This needs reworking. See the thread starting at:
1629 *
1630 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1631 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001632WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001633weston_view_is_mapped(struct weston_view *view)
1634{
Armin Krezović0da12b82016-06-30 06:04:33 +02001635 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001636}
1637
Armin Krezović0da12b82016-06-30 06:04:33 +02001638/* Check if a surface has a view assigned to it
1639 *
1640 * The indicator is set manually when mapping
1641 * a surface and creating a view for it.
1642 *
1643 * This needs to go. See the thread starting at:
1644 *
1645 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1646 *
1647 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001648WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001649weston_surface_is_mapped(struct weston_surface *surface)
1650{
Armin Krezović0da12b82016-06-30 06:04:33 +02001651 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001652}
1653
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001654static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001655surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001656{
1657 struct weston_view *view;
1658
1659 if (surface->width == width && surface->height == height)
1660 return;
1661
1662 surface->width = width;
1663 surface->height = height;
1664
1665 wl_list_for_each(view, &surface->views, surface_link)
1666 weston_view_geometry_dirty(view);
1667}
1668
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001669WL_EXPORT void
1670weston_surface_set_size(struct weston_surface *surface,
1671 int32_t width, int32_t height)
1672{
1673 assert(!surface->resource);
1674 surface_set_size(surface, width, height);
1675}
1676
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001677static int
1678fixed_round_up_to_int(wl_fixed_t f)
1679{
1680 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1681}
1682
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001683static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001684convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1685 int32_t width, int32_t height,
1686 uint32_t transform,
1687 int32_t scale)
1688{
1689 assert(scale > 0);
1690
1691 switch (transform) {
1692 case WL_OUTPUT_TRANSFORM_NORMAL:
1693 case WL_OUTPUT_TRANSFORM_180:
1694 case WL_OUTPUT_TRANSFORM_FLIPPED:
1695 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1696 *width_out = width / scale;
1697 *height_out = height / scale;
1698 break;
1699 case WL_OUTPUT_TRANSFORM_90:
1700 case WL_OUTPUT_TRANSFORM_270:
1701 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1702 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1703 *width_out = height / scale;
1704 *height_out = width / scale;
1705 break;
1706 default:
1707 assert(0 && "invalid transform");
1708 }
1709}
1710
1711static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001712weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001713{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001714 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001715
1716 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001717 surface->width_from_buffer = 0;
1718 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001719 return;
1720 }
1721
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001722 convert_size_by_transform_scale(&surface->width_from_buffer,
1723 &surface->height_from_buffer,
1724 surface->buffer_ref.buffer->width,
1725 surface->buffer_ref.buffer->height,
1726 vp->buffer.transform,
1727 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001728}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001729
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001730static void
1731weston_surface_update_size(struct weston_surface *surface)
1732{
1733 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1734 int32_t width, height;
1735
1736 width = surface->width_from_buffer;
1737 height = surface->height_from_buffer;
1738
1739 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001740 surface_set_size(surface,
1741 vp->surface.width, vp->surface.height);
1742 return;
1743 }
1744
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001745 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001746 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1747 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1748
1749 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001750 return;
1751 }
1752
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001753 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001754}
1755
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001756WL_EXPORT void
1757weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001758{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001759 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001760}
1761
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762WL_EXPORT struct weston_view *
1763weston_compositor_pick_view(struct weston_compositor *compositor,
1764 wl_fixed_t x, wl_fixed_t y,
1765 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001766{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001768 wl_fixed_t view_x, view_y;
1769 int view_ix, view_iy;
1770 int ix = wl_fixed_to_int(x);
1771 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001772
Jason Ekstranda7af7042013-10-12 22:38:11 -05001773 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001774 if (!pixman_region32_contains_point(
1775 &view->transform.boundingbox, ix, iy, NULL))
1776 continue;
1777
1778 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1779 view_ix = wl_fixed_to_int(view_x);
1780 view_iy = wl_fixed_to_int(view_y);
1781
1782 if (!pixman_region32_contains_point(&view->surface->input,
1783 view_ix, view_iy, NULL))
1784 continue;
1785
Pekka Paalanen380adf52015-02-16 14:39:11 +02001786 if (view->geometry.scissor_enabled &&
1787 !pixman_region32_contains_point(&view->geometry.scissor,
1788 view_ix, view_iy, NULL))
1789 continue;
1790
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001791 *vx = view_x;
1792 *vy = view_y;
1793 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001794 }
1795
Derek Foremanf9318d12015-05-11 15:40:11 -05001796 *vx = wl_fixed_from_int(-1000000);
1797 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001798 return NULL;
1799}
1800
1801static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001802weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001803{
Daniel Stone37816df2012-05-16 18:45:18 +01001804 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001805
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001806 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001807 return;
1808
Daniel Stone37816df2012-05-16 18:45:18 +01001809 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001810 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001811}
1812
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001813WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001814weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001815{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001816 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001817
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 if (!weston_view_is_mapped(view))
1819 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 weston_view_damage_below(view);
1822 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001823 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001824 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001825 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826 wl_list_remove(&view->link);
1827 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 view->output_mask = 0;
1829 weston_surface_assign_output(view->surface);
1830
1831 if (weston_surface_is_mapped(view->surface))
1832 return;
1833
1834 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001835 struct weston_touch *touch = weston_seat_get_touch(seat);
1836 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1837 struct weston_keyboard *keyboard =
1838 weston_seat_get_keyboard(seat);
1839
1840 if (keyboard && keyboard->focus == view->surface)
1841 weston_keyboard_set_focus(keyboard, NULL);
1842 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001843 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001844 if (touch && touch->focus == view)
1845 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001846 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001847}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001848
Jason Ekstranda7af7042013-10-12 22:38:11 -05001849WL_EXPORT void
1850weston_surface_unmap(struct weston_surface *surface)
1851{
1852 struct weston_view *view;
1853
Armin Krezovićf8486c32016-06-30 06:04:28 +02001854 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001855 wl_list_for_each(view, &surface->views, surface_link)
1856 weston_view_unmap(view);
1857 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001858}
1859
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001860static void
1861weston_surface_reset_pending_buffer(struct weston_surface *surface)
1862{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001863 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001864 surface->pending.sx = 0;
1865 surface->pending.sy = 0;
1866 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001867 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001868}
1869
Jason Ekstranda7af7042013-10-12 22:38:11 -05001870WL_EXPORT void
1871weston_view_destroy(struct weston_view *view)
1872{
1873 wl_signal_emit(&view->destroy_signal, view);
1874
1875 assert(wl_list_empty(&view->geometry.child_list));
1876
1877 if (weston_view_is_mapped(view)) {
1878 weston_view_unmap(view);
1879 weston_compositor_build_view_list(view->surface->compositor);
1880 }
1881
1882 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001883 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001884
1885 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001886 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001887 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001888 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001889
1890 weston_view_set_transform_parent(view, NULL);
1891
Jason Ekstranda7af7042013-10-12 22:38:11 -05001892 wl_list_remove(&view->surface_link);
1893
1894 free(view);
1895}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001896
1897WL_EXPORT void
1898weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001899{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001900 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001901 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001902 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001903
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001904 if (--surface->ref_count > 0)
1905 return;
1906
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001907 assert(surface->resource == NULL);
1908
Pekka Paalanenca790762015-04-17 14:23:38 +03001909 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001910
Pekka Paalanene67858b2013-04-25 13:57:42 +03001911 assert(wl_list_empty(&surface->subsurface_list_pending));
1912 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001913
Jason Ekstranda7af7042013-10-12 22:38:11 -05001914 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1915 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001916
Jason Ekstrand7b982072014-05-20 14:33:03 -05001917 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001918
Pekka Paalanende685b82012-12-04 15:58:12 +02001919 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001920
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001921 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001922 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001923 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001924
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001925 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001926 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001927
Pekka Paalanen133e4392014-09-23 22:08:46 -04001928 weston_presentation_feedback_discard_list(&surface->feedback_list);
1929
Jonas Ådahld3414f22016-07-22 17:56:31 +08001930 wl_list_for_each_safe(constraint, next_constraint,
1931 &surface->pointer_constraints,
1932 link)
1933 weston_pointer_constraint_destroy(constraint);
1934
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001935 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001936}
1937
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001938static void
1939destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001940{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001941 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001942
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001943 assert(surface);
1944
Giulio Camuffo0d379742013-11-15 22:06:15 +01001945 /* Set the resource to NULL, since we don't want to leave a
1946 * dangling pointer if the surface was refcounted and survives
1947 * the weston_surface_destroy() call. */
1948 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001949
1950 if (surface->viewport_resource)
1951 wl_resource_set_user_data(surface->viewport_resource, NULL);
1952
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001953 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001954}
1955
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001956static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001957weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1958{
1959 struct weston_buffer *buffer =
1960 container_of(listener, struct weston_buffer, destroy_listener);
1961
1962 wl_signal_emit(&buffer->destroy_signal, buffer);
1963 free(buffer);
1964}
1965
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001966WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001967weston_buffer_from_resource(struct wl_resource *resource)
1968{
1969 struct weston_buffer *buffer;
1970 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001971
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001972 listener = wl_resource_get_destroy_listener(resource,
1973 weston_buffer_destroy_handler);
1974
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001975 if (listener)
1976 return container_of(listener, struct weston_buffer,
1977 destroy_listener);
1978
1979 buffer = zalloc(sizeof *buffer);
1980 if (buffer == NULL)
1981 return NULL;
1982
1983 buffer->resource = resource;
1984 wl_signal_init(&buffer->destroy_signal);
1985 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001986 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001987 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001988
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001989 return buffer;
1990}
1991
1992static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001993weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1994 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001995{
Pekka Paalanende685b82012-12-04 15:58:12 +02001996 struct weston_buffer_reference *ref =
1997 container_of(listener, struct weston_buffer_reference,
1998 destroy_listener);
1999
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002000 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002001 ref->buffer = NULL;
2002}
2003
2004WL_EXPORT void
2005weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002006 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002007{
2008 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002009 ref->buffer->busy_count--;
2010 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002011 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002012 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002013 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002014 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002015 }
2016
Pekka Paalanende685b82012-12-04 15:58:12 +02002017 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002018 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002019 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002020 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002021 }
2022
Pekka Paalanende685b82012-12-04 15:58:12 +02002023 ref->buffer = buffer;
2024 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2025}
2026
2027static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002028weston_surface_attach(struct weston_surface *surface,
2029 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002030{
2031 weston_buffer_reference(&surface->buffer_ref, buffer);
2032
Pekka Paalanena6421c42012-12-04 15:58:10 +02002033 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002034 if (weston_surface_is_mapped(surface))
2035 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002036 }
2037
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002038 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002039
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002040 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002041 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002042}
2043
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002044WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002045weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002046{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002047 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002048
2049 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002050 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002051}
2052
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002053WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002054weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002055{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002056 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002057
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002058 pixman_region32_union(&compositor->primary_plane.damage,
2059 &compositor->primary_plane.damage,
2060 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002061 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002062}
2063
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002064static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002065surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002066{
Pekka Paalanende685b82012-12-04 15:58:12 +02002067 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002068 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002069 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002070
Pekka Paalanenb5026542014-11-12 15:09:24 +02002071 if (weston_timeline_enabled_ &&
2072 pixman_region32_not_empty(&surface->damage))
2073 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2074 TLP_OUTPUT(surface->output), TLP_END);
2075
Jason Ekstrandef540082014-06-26 10:37:36 -07002076 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002077}
2078
2079static void
2080view_accumulate_damage(struct weston_view *view,
2081 pixman_region32_t *opaque)
2082{
2083 pixman_region32_t damage;
2084
2085 pixman_region32_init(&damage);
2086 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002087 pixman_box32_t *extents;
2088
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002090 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002091 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002092 pixman_region32_copy(&damage, &view->surface->damage);
2093 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002094 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002095 }
2096
Pekka Paalanen380adf52015-02-16 14:39:11 +02002097 pixman_region32_intersect(&damage, &damage,
2098 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002099 pixman_region32_subtract(&damage, &damage, opaque);
2100 pixman_region32_union(&view->plane->damage,
2101 &view->plane->damage, &damage);
2102 pixman_region32_fini(&damage);
2103 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002104 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002105}
2106
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002107static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002108compositor_accumulate_damage(struct weston_compositor *ec)
2109{
2110 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002111 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002112 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002113
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002114 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002115
2116 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002117 pixman_region32_copy(&plane->clip, &clip);
2118
2119 pixman_region32_init(&opaque);
2120
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 wl_list_for_each(ev, &ec->view_list, link) {
2122 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002123 continue;
2124
Jason Ekstranda7af7042013-10-12 22:38:11 -05002125 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002126 }
2127
2128 pixman_region32_union(&clip, &clip, &opaque);
2129 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002130 }
2131
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002132 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002133
Jason Ekstranda7af7042013-10-12 22:38:11 -05002134 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002135 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136
2137 wl_list_for_each(ev, &ec->view_list, link) {
2138 if (ev->surface->touched)
2139 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002140 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002141
2142 surface_flush_damage(ev->surface);
2143
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002144 /* Both the renderer and the backend have seen the buffer
2145 * by now. If renderer needs the buffer, it has its own
2146 * reference set. If the backend wants to keep the buffer
2147 * around for migrating the surface into a non-primary plane
2148 * later, keep_buffer is true. Otherwise, drop the core
2149 * reference now, and allow early buffer release. This enables
2150 * clients to use single-buffering.
2151 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002152 if (!ev->surface->keep_buffer)
2153 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002154 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002155}
2156
2157static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002158surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002159{
2160 struct weston_subsurface *sub;
2161
Pekka Paalanene67858b2013-04-25 13:57:42 +03002162 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002163 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002164 continue;
2165
Jason Ekstranda7af7042013-10-12 22:38:11 -05002166 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2167 wl_list_init(&sub->surface->views);
2168
2169 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002170 }
2171}
2172
2173static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002174surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002175{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002176 struct weston_subsurface *sub;
2177 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002178
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2180 if (sub->surface == surface)
2181 continue;
2182
George Kiagiadakised04d382014-06-13 18:10:26 +02002183 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2184 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002185 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002186 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002187
2188 surface_free_unused_subsurface_views(sub->surface);
2189 }
2190}
2191
2192static void
2193view_list_add_subsurface_view(struct weston_compositor *compositor,
2194 struct weston_subsurface *sub,
2195 struct weston_view *parent)
2196{
2197 struct weston_subsurface *child;
2198 struct weston_view *view = NULL, *iv;
2199
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002200 if (!weston_surface_is_mapped(sub->surface))
2201 return;
2202
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2204 if (iv->geometry.parent == parent) {
2205 view = iv;
2206 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002207 }
2208 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002209
2210 if (view) {
2211 /* Put it back in the surface's list of views */
2212 wl_list_remove(&view->surface_link);
2213 wl_list_insert(&sub->surface->views, &view->surface_link);
2214 } else {
2215 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002216 weston_view_set_position(view,
2217 sub->position.x,
2218 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002219 weston_view_set_transform_parent(view, parent);
2220 }
2221
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002222 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002224 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225
Pekka Paalanenb188e912013-11-19 14:03:35 +02002226 if (wl_list_empty(&sub->surface->subsurface_list)) {
2227 wl_list_insert(compositor->view_list.prev, &view->link);
2228 return;
2229 }
2230
2231 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2232 if (child->surface == sub->surface)
2233 wl_list_insert(compositor->view_list.prev, &view->link);
2234 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002235 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002236 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002237}
2238
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002239/* This recursively adds the sub-surfaces for a view, relying on the
2240 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2241 * change first happens to the sub-surface list, and then automatically
2242 * propagates here. See weston_surface_damage_subsurfaces() for how the
2243 * sub-surfaces receive damage when the client changes the state.
2244 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002245static void
2246view_list_add(struct weston_compositor *compositor,
2247 struct weston_view *view)
2248{
2249 struct weston_subsurface *sub;
2250
2251 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002252
Pekka Paalanenb188e912013-11-19 14:03:35 +02002253 if (wl_list_empty(&view->surface->subsurface_list)) {
2254 wl_list_insert(compositor->view_list.prev, &view->link);
2255 return;
2256 }
2257
2258 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2259 if (sub->surface == view->surface)
2260 wl_list_insert(compositor->view_list.prev, &view->link);
2261 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002262 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002263 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002264}
2265
2266static void
2267weston_compositor_build_view_list(struct weston_compositor *compositor)
2268{
2269 struct weston_view *view;
2270 struct weston_layer *layer;
2271
2272 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002273 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002274 surface_stash_subsurface_views(view->surface);
2275
2276 wl_list_init(&compositor->view_list);
2277 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002278 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002279 view_list_add(compositor, view);
2280 }
2281 }
2282
2283 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002284 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002285 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002286}
2287
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002288static void
2289weston_output_take_feedback_list(struct weston_output *output,
2290 struct weston_surface *surface)
2291{
2292 struct weston_view *view;
2293 struct weston_presentation_feedback *feedback;
2294 uint32_t flags = 0xffffffff;
2295
2296 if (wl_list_empty(&surface->feedback_list))
2297 return;
2298
2299 /* All views must have the flag for the flag to survive. */
2300 wl_list_for_each(view, &surface->views, surface_link) {
2301 /* ignore views that are not on this output at all */
2302 if (view->output_mask & (1u << output->id))
2303 flags &= view->psf_flags;
2304 }
2305
2306 wl_list_for_each(feedback, &surface->feedback_list, link)
2307 feedback->psf_flags = flags;
2308
2309 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2310 wl_list_init(&surface->feedback_list);
2311}
2312
David Herrmann1edf44c2013-10-22 17:11:26 +02002313static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002314weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002315{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002316 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002317 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002318 struct weston_animation *animation, *next;
2319 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002320 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002321 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002322 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002323 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002324
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002325 if (output->destroying)
2326 return 0;
2327
Pekka Paalanenb5026542014-11-12 15:09:24 +02002328 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2329
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002330 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002331 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002332
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002333 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002334 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002335 } else {
2336 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002337 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002338 ev->psf_flags = 0;
2339 }
2340 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002341
Pekka Paalanene67858b2013-04-25 13:57:42 +03002342 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002343 wl_list_for_each(ev, &ec->view_list, link) {
2344 /* Note: This operation is safe to do multiple times on the
2345 * same surface.
2346 */
2347 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002348 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002349 &ev->surface->frame_callback_list);
2350 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002351
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002352 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002353 }
2354 }
2355
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002356 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002357
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002358 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002359 pixman_region32_intersect(&output_damage,
2360 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002361 pixman_region32_subtract(&output_damage,
2362 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002363
Scott Moreauccbf29d2012-02-22 14:21:41 -07002364 if (output->dirty)
2365 weston_output_update_matrix(output);
2366
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002367 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002368
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002369 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002370
Daniel Stone09a97e22017-03-01 11:34:06 +00002371 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002372 if (r == 0)
2373 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002374
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002375 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002376
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002377 frame_time_msec = timespec_to_msec(&output->frame_time);
2378
Jonas Ådahldb773762012-06-13 00:01:21 +02002379 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002380 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002381 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002382 }
2383
Scott Moreaud64cf212012-06-08 19:40:54 -06002384 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002385 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002386 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002387 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002388
Pekka Paalanenb5026542014-11-12 15:09:24 +02002389 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2390
David Herrmann1edf44c2013-10-22 17:11:26 +02002391 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002392}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002393
Pekka Paalanen82919792014-05-21 13:51:49 +03002394static void
2395weston_output_schedule_repaint_reset(struct weston_output *output)
2396{
Daniel Stone05df8c12017-03-03 16:59:42 +00002397 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002398 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002399}
2400
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002401static int
2402weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2403 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002404{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002405 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002406 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002407 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002408
Daniel Stone6847b852017-03-01 11:34:08 +00002409 /* We're not ready yet; come back to make a decision later. */
2410 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002411 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002412
2413 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2414 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002415 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002416
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002417 /* If we're sleeping, drop the repaint machinery entirely; we will
2418 * explicitly repaint all outputs when we come back. */
2419 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2420 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2421 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002422
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002423 /* We don't actually need to repaint this output; drop it from
2424 * repaint until something causes damage. */
2425 if (!output->repaint_needed)
2426 goto err;
2427
2428 /* If repaint fails, we aren't going to get weston_output_finish_frame
2429 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002430 * something schedules a successful repaint later. As repainting may
2431 * take some time, re-read our clock as a courtesy to the next
2432 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002433 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002434 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002435 if (ret != 0)
2436 goto err;
2437
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002438 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002439
2440err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002441 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002442 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002443}
2444
2445static void
2446output_repaint_timer_arm(struct weston_compositor *compositor)
2447{
2448 struct weston_output *output;
2449 bool any_should_repaint = false;
2450 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002451 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002452
2453 weston_compositor_read_presentation_clock(compositor, &now);
2454
2455 wl_list_for_each(output, &compositor->output_list, link) {
2456 int64_t msec_to_this;
2457
2458 if (output->repaint_status != REPAINT_SCHEDULED)
2459 continue;
2460
2461 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2462 &now);
2463 if (!any_should_repaint || msec_to_this < msec_to_next)
2464 msec_to_next = msec_to_this;
2465
2466 any_should_repaint = true;
2467 }
2468
2469 if (!any_should_repaint)
2470 return;
2471
2472 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2473 * This is a workaround to allow coalescing multiple output repaints
2474 * particularly from weston_output_finish_frame()
2475 * into the same call, which would not happen if we called
2476 * output_repaint_timer_handler() directly.
2477 */
2478 if (msec_to_next < 1)
2479 msec_to_next = 1;
2480
2481 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2482}
2483
2484static int
2485output_repaint_timer_handler(void *data)
2486{
2487 struct weston_compositor *compositor = data;
2488 struct weston_output *output;
2489 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002490 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002491 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002492
2493 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002494
2495 if (compositor->backend->repaint_begin)
2496 repaint_data = compositor->backend->repaint_begin(compositor);
2497
2498 wl_list_for_each(output, &compositor->output_list, link) {
2499 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2500 if (ret)
2501 break;
2502 }
2503
2504 if (ret == 0) {
2505 if (compositor->backend->repaint_flush)
2506 compositor->backend->repaint_flush(compositor,
2507 repaint_data);
2508 } else {
2509 if (compositor->backend->repaint_cancel)
2510 compositor->backend->repaint_cancel(compositor,
2511 repaint_data);
2512 }
Daniel Stone6847b852017-03-01 11:34:08 +00002513
2514 output_repaint_timer_arm(compositor);
2515
Pekka Paalanen0513a952014-05-21 16:17:27 +03002516 return 0;
2517}
2518
Kristian Høgsbergef044142011-06-21 15:02:12 -04002519WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002520weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002521 const struct timespec *stamp,
2522 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002523{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002524 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002525 int32_t refresh_nsec;
2526 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002527 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002528
Pekka Paalanenb5026542014-11-12 15:09:24 +02002529
Daniel Stone05df8c12017-03-03 16:59:42 +00002530 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002531 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2532
Daniel Stone6847b852017-03-01 11:34:08 +00002533 weston_compositor_read_presentation_clock(compositor, &now);
2534
Daniel Stone3615ce12017-03-01 11:34:05 +00002535 /* If we haven't been supplied any timestamp at all, we don't have a
2536 * timebase to work against, so any delay just wastes time. Push a
2537 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002538 if (!stamp) {
2539 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002540 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002541 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002542
Marius Vladdf9278a2018-03-06 18:56:23 +02002543 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2544 TLP_VBLANK(stamp), TLP_END);
2545
Pekka Paalanend7894d02015-07-03 15:08:53 +03002546 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002547 weston_presentation_feedback_present_list(&output->feedback_list,
2548 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002549 output->msc,
2550 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002551
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002552 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002553
Daniel Stone6847b852017-03-01 11:34:08 +00002554 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2555 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2556 -compositor->repaint_msec);
2557 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002558
2559 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002560 static bool warned;
2561
2562 if (!warned)
2563 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002564 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002565 warned = true;
2566
Daniel Stone6847b852017-03-01 11:34:08 +00002567 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002568 }
2569
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002570 /* Called from restart_repaint_loop and restart happens already after
2571 * the deadline given by repaint_msec? In that case we delay until
2572 * the deadline of the next frame, to give clients a more predictable
2573 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002574 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2575 msec_rel < 0) {
2576 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2577 timespec_add_nsec(&output->next_repaint,
2578 &output->next_repaint,
2579 refresh_nsec);
2580 }
2581 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002582
Daniel Stone3615ce12017-03-01 11:34:05 +00002583out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002584 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002585 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002586}
2587
2588static void
2589idle_repaint(void *data)
2590{
2591 struct weston_output *output = data;
2592
Daniel Stone05df8c12017-03-03 16:59:42 +00002593 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2594 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002595 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002596}
2597
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002598WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002599weston_layer_entry_insert(struct weston_layer_entry *list,
2600 struct weston_layer_entry *entry)
2601{
2602 wl_list_insert(&list->link, &entry->link);
2603 entry->layer = list->layer;
2604}
2605
2606WL_EXPORT void
2607weston_layer_entry_remove(struct weston_layer_entry *entry)
2608{
2609 wl_list_remove(&entry->link);
2610 wl_list_init(&entry->link);
2611 entry->layer = NULL;
2612}
2613
Quentin Glidic82681572016-12-17 13:40:51 +01002614
2615/** Initialize the weston_layer struct.
2616 *
2617 * \param compositor The compositor instance
2618 * \param layer The layer to initialize
2619 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002620WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002621weston_layer_init(struct weston_layer *layer,
2622 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002623{
Quentin Glidic82681572016-12-17 13:40:51 +01002624 layer->compositor = compositor;
2625 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002626 wl_list_init(&layer->view_list.link);
2627 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002628 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002629}
2630
2631/** Sets the position of the layer in the layer list. The layer will be placed
2632 * below any layer with the same position value, if any.
2633 * This function is safe to call if the layer is already on the list, but the
2634 * layer may be moved below other layers at the same position, if any.
2635 *
2636 * \param layer The layer to modify
2637 * \param position The position the layer will be placed at
2638 */
2639WL_EXPORT void
2640weston_layer_set_position(struct weston_layer *layer,
2641 enum weston_layer_position position)
2642{
2643 struct weston_layer *below;
2644
2645 wl_list_remove(&layer->link);
2646
2647 /* layer_list is ordered from top to bottom, the last layer being the
2648 * background with the smallest position value */
2649
2650 layer->position = position;
2651 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2652 if (below->position >= layer->position) {
2653 wl_list_insert(&below->link, &layer->link);
2654 return;
2655 }
2656 }
2657 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2658}
2659
2660/** Hide a layer by taking it off the layer list.
2661 * This function is safe to call if the layer is not on the list.
2662 *
2663 * \param layer The layer to hide
2664 */
2665WL_EXPORT void
2666weston_layer_unset_position(struct weston_layer *layer)
2667{
2668 wl_list_remove(&layer->link);
2669 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002670}
2671
2672WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002673weston_layer_set_mask(struct weston_layer *layer,
2674 int x, int y, int width, int height)
2675{
2676 struct weston_view *view;
2677
2678 layer->mask.x1 = x;
2679 layer->mask.x2 = x + width;
2680 layer->mask.y1 = y;
2681 layer->mask.y2 = y + height;
2682
2683 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2684 weston_view_geometry_dirty(view);
2685 }
2686}
2687
2688WL_EXPORT void
2689weston_layer_set_mask_infinite(struct weston_layer *layer)
2690{
2691 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2692 UINT32_MAX, UINT32_MAX);
2693}
2694
2695WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002696weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002697{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002698 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002699 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002700
Bryce Harrington08976ac2016-08-30 12:05:16 -07002701 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2702 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002703 return;
2704
Pekka Paalanenb5026542014-11-12 15:09:24 +02002705 if (!output->repaint_needed)
2706 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2707
Kristian Høgsbergef044142011-06-21 15:02:12 -04002708 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002709 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002710
2711 /* If we already have a repaint scheduled for our idle handler,
2712 * no need to set it again. If the repaint has been called but
2713 * not finished, then weston_output_finish_frame() will notice
2714 * that a repaint is needed and schedule one. */
2715 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002716 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002717
Daniel Stone05df8c12017-03-03 16:59:42 +00002718 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002719 wl_event_loop_add_idle(loop, idle_repaint, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002720 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002721}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002722
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002723WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002724weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2725{
2726 struct weston_output *output;
2727
2728 wl_list_for_each(output, &compositor->output_list, link)
2729 weston_output_schedule_repaint(output);
2730}
2731
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002732static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002733surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002734{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002735 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002736}
2737
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002738static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002739surface_attach(struct wl_client *client,
2740 struct wl_resource *resource,
2741 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2742{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002743 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002744 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002745
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002746 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002747 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002748 if (buffer == NULL) {
2749 wl_client_post_no_memory(client);
2750 return;
2751 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002752 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002753
Pekka Paalanende685b82012-12-04 15:58:12 +02002754 /* Attach, attach, without commit in between does not send
2755 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002756 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002757
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002758 surface->pending.sx = sx;
2759 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002760 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002761}
2762
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002763static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002764surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002765 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002766 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002767{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002768 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002769
Derek Foreman57e92ed2015-11-17 14:11:35 -06002770 if (width <= 0 || height <= 0)
2771 return;
2772
Derek Foreman152254b2015-11-26 14:17:48 -06002773 pixman_region32_union_rect(&surface->pending.damage_surface,
2774 &surface->pending.damage_surface,
2775 x, y, width, height);
2776}
2777
2778static void
2779surface_damage_buffer(struct wl_client *client,
2780 struct wl_resource *resource,
2781 int32_t x, int32_t y, int32_t width, int32_t height)
2782{
2783 struct weston_surface *surface = wl_resource_get_user_data(resource);
2784
2785 if (width <= 0 || height <= 0)
2786 return;
2787
2788 pixman_region32_union_rect(&surface->pending.damage_buffer,
2789 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002790 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002791}
2792
Kristian Høgsberg33418202011-08-16 23:01:28 -04002793static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002794destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002795{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002796 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002797
2798 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002799 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002800}
2801
2802static void
2803surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002804 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002805{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002806 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002807 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002808
2809 cb = malloc(sizeof *cb);
2810 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002811 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002812 return;
2813 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002814
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002815 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2816 callback);
2817 if (cb->resource == NULL) {
2818 free(cb);
2819 wl_resource_post_no_memory(resource);
2820 return;
2821 }
2822
Jason Ekstranda85118c2013-06-27 20:17:02 -05002823 wl_resource_set_implementation(cb->resource, NULL, cb,
2824 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002825
Pekka Paalanenbc106382012-10-10 12:49:31 +03002826 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002827}
2828
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002829static void
2830surface_set_opaque_region(struct wl_client *client,
2831 struct wl_resource *resource,
2832 struct wl_resource *region_resource)
2833{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002834 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002835 struct weston_region *region;
2836
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002837 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002838 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002839 pixman_region32_copy(&surface->pending.opaque,
2840 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002841 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002842 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002843 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002844}
2845
2846static void
2847surface_set_input_region(struct wl_client *client,
2848 struct wl_resource *resource,
2849 struct wl_resource *region_resource)
2850{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002851 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002852 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002853
2854 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002855 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002856 pixman_region32_copy(&surface->pending.input,
2857 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002858 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002859 pixman_region32_fini(&surface->pending.input);
2860 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002861 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002862}
2863
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002864/* Cause damage to this sub-surface and all its children.
2865 *
2866 * This is useful when there are state changes that need an implicit
2867 * damage, e.g. a z-order change.
2868 */
2869static void
2870weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2871{
2872 struct weston_subsurface *child;
2873
2874 weston_surface_damage(sub->surface);
2875 sub->reordered = false;
2876
2877 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2878 if (child != sub)
2879 weston_surface_damage_subsurfaces(child);
2880}
2881
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002882static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002883weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002884{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002885 struct weston_subsurface *sub;
2886
2887 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2888 parent_link_pending) {
2889 wl_list_remove(&sub->parent_link);
2890 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002891
2892 if (sub->reordered)
2893 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002894 }
2895}
2896
2897static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002898weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002899 struct weston_matrix *matrix)
2900{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002901 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002902 double src_width, src_height, dest_width, dest_height;
2903
2904 weston_matrix_init(matrix);
2905
2906 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2907 src_width = surface->width_from_buffer;
2908 src_height = surface->height_from_buffer;
2909 } else {
2910 src_width = wl_fixed_to_double(vp->buffer.src_width);
2911 src_height = wl_fixed_to_double(vp->buffer.src_height);
2912 }
2913
2914 if (vp->surface.width == -1) {
2915 dest_width = src_width;
2916 dest_height = src_height;
2917 } else {
2918 dest_width = vp->surface.width;
2919 dest_height = vp->surface.height;
2920 }
2921
2922 if (src_width != dest_width || src_height != dest_height)
2923 weston_matrix_scale(matrix,
2924 src_width / dest_width,
2925 src_height / dest_height, 1);
2926
2927 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2928 weston_matrix_translate(matrix,
2929 wl_fixed_to_double(vp->buffer.src_x),
2930 wl_fixed_to_double(vp->buffer.src_y),
2931 0);
2932
2933 switch (vp->buffer.transform) {
2934 case WL_OUTPUT_TRANSFORM_FLIPPED:
2935 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2936 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2937 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2938 weston_matrix_scale(matrix, -1, 1, 1);
2939 weston_matrix_translate(matrix,
2940 surface->width_from_buffer, 0, 0);
2941 break;
2942 }
2943
2944 switch (vp->buffer.transform) {
2945 default:
2946 case WL_OUTPUT_TRANSFORM_NORMAL:
2947 case WL_OUTPUT_TRANSFORM_FLIPPED:
2948 break;
2949 case WL_OUTPUT_TRANSFORM_90:
2950 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2951 weston_matrix_rotate_xy(matrix, 0, 1);
2952 weston_matrix_translate(matrix,
2953 surface->height_from_buffer, 0, 0);
2954 break;
2955 case WL_OUTPUT_TRANSFORM_180:
2956 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2957 weston_matrix_rotate_xy(matrix, -1, 0);
2958 weston_matrix_translate(matrix,
2959 surface->width_from_buffer,
2960 surface->height_from_buffer, 0);
2961 break;
2962 case WL_OUTPUT_TRANSFORM_270:
2963 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2964 weston_matrix_rotate_xy(matrix, 0, -1);
2965 weston_matrix_translate(matrix,
2966 0, surface->width_from_buffer, 0);
2967 break;
2968 }
2969
2970 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2971}
2972
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002973/**
2974 * Compute a + b > c while being safe to overflows.
2975 */
2976static bool
2977fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2978{
2979 return (int64_t)a + (int64_t)b > (int64_t)c;
2980}
2981
2982static bool
2983weston_surface_is_pending_viewport_source_valid(
2984 const struct weston_surface *surface)
2985{
2986 const struct weston_surface_state *pend = &surface->pending;
2987 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2988 int width_from_buffer = 0;
2989 int height_from_buffer = 0;
2990 wl_fixed_t w;
2991 wl_fixed_t h;
2992
2993 /* If viewport source rect is not set, it is always ok. */
2994 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2995 return true;
2996
2997 if (pend->newly_attached) {
2998 if (pend->buffer) {
2999 convert_size_by_transform_scale(&width_from_buffer,
3000 &height_from_buffer,
3001 pend->buffer->width,
3002 pend->buffer->height,
3003 vp->buffer.transform,
3004 vp->buffer.scale);
3005 } else {
3006 /* No buffer: viewport is irrelevant. */
3007 return true;
3008 }
3009 } else {
3010 width_from_buffer = surface->width_from_buffer;
3011 height_from_buffer = surface->height_from_buffer;
3012 }
3013
3014 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3015 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3016
3017 /* No buffer: viewport is irrelevant. */
3018 if (width_from_buffer == 0 || height_from_buffer == 0)
3019 return true;
3020
3021 /* overflow checks for wl_fixed_from_int() */
3022 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3023 return false;
3024 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3025 return false;
3026
3027 w = wl_fixed_from_int(width_from_buffer);
3028 h = wl_fixed_from_int(height_from_buffer);
3029
3030 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3031 return false;
3032 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3033 return false;
3034
3035 return true;
3036}
3037
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003038static bool
3039fixed_is_integer(wl_fixed_t v)
3040{
3041 return (v & 0xff) == 0;
3042}
3043
3044static bool
3045weston_surface_is_pending_viewport_dst_size_int(
3046 const struct weston_surface *surface)
3047{
3048 const struct weston_buffer_viewport *vp =
3049 &surface->pending.buffer_viewport;
3050
3051 if (vp->surface.width != -1) {
3052 assert(vp->surface.width > 0 && vp->surface.height > 0);
3053 return true;
3054 }
3055
3056 return fixed_is_integer(vp->buffer.src_width) &&
3057 fixed_is_integer(vp->buffer.src_height);
3058}
3059
Derek Foreman152254b2015-11-26 14:17:48 -06003060/* Translate pending damage in buffer co-ordinates to surface
3061 * co-ordinates and union it with a pixman_region32_t.
3062 * This should only be called after the buffer is attached.
3063 */
3064static void
3065apply_damage_buffer(pixman_region32_t *dest,
3066 struct weston_surface *surface,
3067 struct weston_surface_state *state)
3068{
3069 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3070
3071 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3072 * translated into surface co-ordinates and unioned with
3073 * any other surface damage.
3074 * None of this makes sense if there is no buffer though.
3075 */
3076 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3077 pixman_region32_t buffer_damage;
3078
3079 pixman_region32_intersect_rect(&state->damage_buffer,
3080 &state->damage_buffer,
3081 0, 0, buffer->width,
3082 buffer->height);
3083 pixman_region32_init(&buffer_damage);
3084 weston_matrix_transform_region(&buffer_damage,
3085 &surface->buffer_to_surface_matrix,
3086 &state->damage_buffer);
3087 pixman_region32_union(dest, dest, &buffer_damage);
3088 pixman_region32_fini(&buffer_damage);
3089 }
3090 /* We should clear this on commit even if there was no buffer */
3091 pixman_region32_clear(&state->damage_buffer);
3092}
3093
Jason Ekstrand1e059042014-10-16 10:55:19 -05003094static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003095weston_surface_commit_state(struct weston_surface *surface,
3096 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003097{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003098 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003099 pixman_region32_t opaque;
3100
Alexander Larsson4ea95522013-05-22 14:41:37 +02003101 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003102 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003103 /* wp_viewport.set_source */
3104 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003105 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003106
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003107 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003108 if (state->newly_attached)
3109 weston_surface_attach(surface, state->buffer);
3110 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003111
Jason Ekstrand1e059042014-10-16 10:55:19 -05003112 weston_surface_build_buffer_matrix(surface,
3113 &surface->surface_to_buffer_matrix);
3114 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3115 &surface->surface_to_buffer_matrix);
3116
Jason Ekstrand7b982072014-05-20 14:33:03 -05003117 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003118 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003119 if (surface->committed)
3120 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003121 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003122
Jason Ekstrand7b982072014-05-20 14:33:03 -05003123 state->sx = 0;
3124 state->sy = 0;
3125 state->newly_attached = 0;
3126 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003127
Derek Foreman152254b2015-11-26 14:17:48 -06003128 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003129 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003130 (pixman_region32_not_empty(&state->damage_surface) ||
3131 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003132 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003133
Pekka Paalanen8e159182012-10-10 12:49:25 +03003134 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003135 &state->damage_surface);
3136
3137 apply_damage_buffer(&surface->damage, surface, state);
3138
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003139 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003140 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003141 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003142
3143 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003144 pixman_region32_init(&opaque);
3145 pixman_region32_intersect_rect(&opaque, &state->opaque,
3146 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003147
3148 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3149 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003150 wl_list_for_each(view, &surface->views, surface_link)
3151 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003152 }
3153
3154 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003155
3156 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003157 pixman_region32_intersect_rect(&surface->input, &state->input,
3158 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003159
Pekka Paalanenbc106382012-10-10 12:49:31 +03003160 /* wl_surface.frame */
3161 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003162 &state->frame_callback_list);
3163 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003164
3165 /* XXX:
3166 * What should happen with a feedback request, if there
3167 * is no wl_buffer attached for this commit?
3168 */
3169
3170 /* presentation.feedback */
3171 wl_list_insert_list(&surface->feedback_list,
3172 &state->feedback_list);
3173 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003174
3175 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003176}
3177
3178static void
3179weston_surface_commit(struct weston_surface *surface)
3180{
3181 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003182
Pekka Paalanene67858b2013-04-25 13:57:42 +03003183 weston_surface_commit_subsurface_order(surface);
3184
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003185 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003186}
3187
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003188static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189weston_subsurface_commit(struct weston_subsurface *sub);
3190
3191static void
3192weston_subsurface_parent_commit(struct weston_subsurface *sub,
3193 int parent_is_synchronized);
3194
3195static void
3196surface_commit(struct wl_client *client, struct wl_resource *resource)
3197{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003198 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003199 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3200
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003201 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3202 assert(surface->viewport_resource);
3203
3204 wl_resource_post_error(surface->viewport_resource,
3205 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3206 "wl_surface@%d has viewport source outside buffer",
3207 wl_resource_get_id(resource));
3208 return;
3209 }
3210
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003211 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3212 assert(surface->viewport_resource);
3213
3214 wl_resource_post_error(surface->viewport_resource,
3215 WP_VIEWPORT_ERROR_BAD_SIZE,
3216 "wl_surface@%d viewport dst size not integer",
3217 wl_resource_get_id(resource));
3218 return;
3219 }
3220
Pekka Paalanene67858b2013-04-25 13:57:42 +03003221 if (sub) {
3222 weston_subsurface_commit(sub);
3223 return;
3224 }
3225
3226 weston_surface_commit(surface);
3227
3228 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3229 if (sub->surface != surface)
3230 weston_subsurface_parent_commit(sub, 0);
3231 }
3232}
3233
3234static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003235surface_set_buffer_transform(struct wl_client *client,
3236 struct wl_resource *resource, int transform)
3237{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003238 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003239
Jonny Lamba55f1392014-05-30 12:07:15 +02003240 /* if wl_output.transform grows more members this will need to be updated. */
3241 if (transform < 0 ||
3242 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3243 wl_resource_post_error(resource,
3244 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3245 "buffer transform must be a valid transform "
3246 "('%d' specified)", transform);
3247 return;
3248 }
3249
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003250 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003251 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003252}
3253
Alexander Larsson4ea95522013-05-22 14:41:37 +02003254static void
3255surface_set_buffer_scale(struct wl_client *client,
3256 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003257 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003258{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003259 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003260
Jonny Lamba55f1392014-05-30 12:07:15 +02003261 if (scale < 1) {
3262 wl_resource_post_error(resource,
3263 WL_SURFACE_ERROR_INVALID_SCALE,
3264 "buffer scale must be at least one "
3265 "('%d' specified)", scale);
3266 return;
3267 }
3268
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003269 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003270 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003271}
3272
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003273static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003274 surface_destroy,
3275 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003276 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003277 surface_frame,
3278 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003279 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003280 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003281 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003282 surface_set_buffer_scale,
3283 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003284};
3285
3286static void
3287compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003288 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003289{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003290 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003291 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003292
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003293 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003294 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003295 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003296 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003297 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003298
Jason Ekstranda85118c2013-06-27 20:17:02 -05003299 surface->resource =
3300 wl_resource_create(client, &wl_surface_interface,
3301 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003302 if (surface->resource == NULL) {
3303 weston_surface_destroy(surface);
3304 wl_resource_post_no_memory(resource);
3305 return;
3306 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003307 wl_resource_set_implementation(surface->resource, &surface_interface,
3308 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003309
3310 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003311}
3312
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003313static void
3314destroy_region(struct wl_resource *resource)
3315{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003316 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003317
3318 pixman_region32_fini(&region->region);
3319 free(region);
3320}
3321
3322static void
3323region_destroy(struct wl_client *client, struct wl_resource *resource)
3324{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003325 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003326}
3327
3328static void
3329region_add(struct wl_client *client, struct wl_resource *resource,
3330 int32_t x, int32_t y, int32_t width, int32_t height)
3331{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003332 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003333
3334 pixman_region32_union_rect(&region->region, &region->region,
3335 x, y, width, height);
3336}
3337
3338static void
3339region_subtract(struct wl_client *client, struct wl_resource *resource,
3340 int32_t x, int32_t y, int32_t width, int32_t height)
3341{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003342 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003343 pixman_region32_t rect;
3344
3345 pixman_region32_init_rect(&rect, x, y, width, height);
3346 pixman_region32_subtract(&region->region, &region->region, &rect);
3347 pixman_region32_fini(&rect);
3348}
3349
3350static const struct wl_region_interface region_interface = {
3351 region_destroy,
3352 region_add,
3353 region_subtract
3354};
3355
3356static void
3357compositor_create_region(struct wl_client *client,
3358 struct wl_resource *resource, uint32_t id)
3359{
3360 struct weston_region *region;
3361
3362 region = malloc(sizeof *region);
3363 if (region == NULL) {
3364 wl_resource_post_no_memory(resource);
3365 return;
3366 }
3367
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003368 pixman_region32_init(&region->region);
3369
Jason Ekstranda85118c2013-06-27 20:17:02 -05003370 region->resource =
3371 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003372 if (region->resource == NULL) {
3373 free(region);
3374 wl_resource_post_no_memory(resource);
3375 return;
3376 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003377 wl_resource_set_implementation(region->resource, &region_interface,
3378 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003379}
3380
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003381static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003382 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003383 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003384};
3385
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003386static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003387weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3388{
3389 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003390
Jason Ekstrand7b982072014-05-20 14:33:03 -05003391 weston_surface_commit_state(surface, &sub->cached);
3392 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003393
3394 weston_surface_commit_subsurface_order(surface);
3395
3396 weston_surface_schedule_repaint(surface);
3397
Jason Ekstrand7b982072014-05-20 14:33:03 -05003398 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003399}
3400
3401static void
3402weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3403{
3404 struct weston_surface *surface = sub->surface;
3405
3406 /*
3407 * If this commit would cause the surface to move by the
3408 * attach(dx, dy) parameters, the old damage region must be
3409 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003410 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003411 */
Derek Foreman152254b2015-11-26 14:17:48 -06003412 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003413 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003414 pixman_region32_union(&sub->cached.damage_surface,
3415 &sub->cached.damage_surface,
3416 &surface->pending.damage_surface);
3417 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003418
3419 if (surface->pending.newly_attached) {
3420 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003421 weston_surface_state_set_buffer(&sub->cached,
3422 surface->pending.buffer);
3423 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003424 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003425 weston_presentation_feedback_discard_list(
3426 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003427 }
3428 sub->cached.sx += surface->pending.sx;
3429 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003430
Derek Foreman152254b2015-11-26 14:17:48 -06003431 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3432
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003433 sub->cached.buffer_viewport.changed |=
3434 surface->pending.buffer_viewport.changed;
3435 sub->cached.buffer_viewport.buffer =
3436 surface->pending.buffer_viewport.buffer;
3437 sub->cached.buffer_viewport.surface =
3438 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003439
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003440 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003441
3442 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3443
3444 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3445
3446 wl_list_insert_list(&sub->cached.frame_callback_list,
3447 &surface->pending.frame_callback_list);
3448 wl_list_init(&surface->pending.frame_callback_list);
3449
Pekka Paalanen133e4392014-09-23 22:08:46 -04003450 wl_list_insert_list(&sub->cached.feedback_list,
3451 &surface->pending.feedback_list);
3452 wl_list_init(&surface->pending.feedback_list);
3453
Jason Ekstrand7b982072014-05-20 14:33:03 -05003454 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003455}
3456
Derek Foreman280e7dd2014-10-03 13:13:42 -05003457static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003458weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3459{
3460 while (sub) {
3461 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003462 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003463
3464 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003465 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003466
3467 sub = weston_surface_to_subsurface(sub->parent);
3468 }
3469
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003470 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003471}
3472
3473static void
3474weston_subsurface_commit(struct weston_subsurface *sub)
3475{
3476 struct weston_surface *surface = sub->surface;
3477 struct weston_subsurface *tmp;
3478
3479 /* Recursive check for effectively synchronized. */
3480 if (weston_subsurface_is_synchronized(sub)) {
3481 weston_subsurface_commit_to_cache(sub);
3482 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003483 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003484 /* flush accumulated state from cache */
3485 weston_subsurface_commit_to_cache(sub);
3486 weston_subsurface_commit_from_cache(sub);
3487 } else {
3488 weston_surface_commit(surface);
3489 }
3490
3491 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3492 if (tmp->surface != surface)
3493 weston_subsurface_parent_commit(tmp, 0);
3494 }
3495 }
3496}
3497
3498static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003499weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003500{
3501 struct weston_surface *surface = sub->surface;
3502 struct weston_subsurface *tmp;
3503
Pekka Paalanene67858b2013-04-25 13:57:42 +03003504 /* From now on, commit_from_cache the whole sub-tree, regardless of
3505 * the synchronized mode of each child. This sub-surface or some
3506 * of its ancestors were synchronized, so we are synchronized
3507 * all the way down.
3508 */
3509
Jason Ekstrand7b982072014-05-20 14:33:03 -05003510 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003511 weston_subsurface_commit_from_cache(sub);
3512
3513 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3514 if (tmp->surface != surface)
3515 weston_subsurface_parent_commit(tmp, 1);
3516 }
3517}
3518
3519static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003520weston_subsurface_parent_commit(struct weston_subsurface *sub,
3521 int parent_is_synchronized)
3522{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003523 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003524 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003525 wl_list_for_each(view, &sub->surface->views, surface_link)
3526 weston_view_set_position(view,
3527 sub->position.x,
3528 sub->position.y);
3529
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003530 sub->position.set = 0;
3531 }
3532
3533 if (parent_is_synchronized || sub->synchronized)
3534 weston_subsurface_synchronized_commit(sub);
3535}
3536
Pekka Paalanen8274d902014-08-06 19:36:51 +03003537static int
3538subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3539{
3540 return snprintf(buf, len, "sub-surface");
3541}
3542
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003543static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003544subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003545{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003546 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003547
Jason Ekstranda7af7042013-10-12 22:38:11 -05003548 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003549 weston_view_set_position(view,
3550 view->geometry.x + dx,
3551 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003552
3553 /* No need to check parent mappedness, because if parent is not
3554 * mapped, parent is not in a visible layer, so this sub-surface
3555 * will not be drawn either.
3556 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003557
Pekka Paalanene67858b2013-04-25 13:57:42 +03003558 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003559 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003560
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003561 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003562 * because that would call it also for the parent surface,
3563 * which might not be mapped yet. That would lead to
3564 * inconsistent state, where the window could never be
3565 * mapped.
3566 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003567 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003568 * weston_surface_is_mapped() return true, so that when the
3569 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003570 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003571 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003572 }
3573}
3574
3575static struct weston_subsurface *
3576weston_surface_to_subsurface(struct weston_surface *surface)
3577{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003578 if (surface->committed == subsurface_committed)
3579 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003580
3581 return NULL;
3582}
3583
Pekka Paalanen01388e22013-04-25 13:57:44 +03003584WL_EXPORT struct weston_surface *
3585weston_surface_get_main_surface(struct weston_surface *surface)
3586{
3587 struct weston_subsurface *sub;
3588
3589 while (surface && (sub = weston_surface_to_subsurface(surface)))
3590 surface = sub->parent;
3591
3592 return surface;
3593}
3594
Pekka Paalanen50b67472014-10-01 15:02:41 +03003595WL_EXPORT int
3596weston_surface_set_role(struct weston_surface *surface,
3597 const char *role_name,
3598 struct wl_resource *error_resource,
3599 uint32_t error_code)
3600{
3601 assert(role_name);
3602
3603 if (surface->role_name == NULL ||
3604 surface->role_name == role_name ||
3605 strcmp(surface->role_name, role_name) == 0) {
3606 surface->role_name = role_name;
3607
3608 return 0;
3609 }
3610
3611 wl_resource_post_error(error_resource, error_code,
3612 "Cannot assign role %s to wl_surface@%d,"
3613 " already has role %s\n",
3614 role_name,
3615 wl_resource_get_id(surface->resource),
3616 surface->role_name);
3617 return -1;
3618}
3619
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003620WL_EXPORT const char *
3621weston_surface_get_role(struct weston_surface *surface)
3622{
3623 return surface->role_name;
3624}
3625
Pekka Paalanen8274d902014-08-06 19:36:51 +03003626WL_EXPORT void
3627weston_surface_set_label_func(struct weston_surface *surface,
3628 int (*desc)(struct weston_surface *,
3629 char *, size_t))
3630{
3631 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003632 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003633}
3634
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003635/** Get the size of surface contents
3636 *
3637 * \param surface The surface to query.
3638 * \param width Returns the width of raw contents.
3639 * \param height Returns the height of raw contents.
3640 *
3641 * Retrieves the raw surface content size in pixels for the given surface.
3642 * This is the whole content size in buffer pixels. If the surface
3643 * has no content or the renderer does not implement this feature,
3644 * zeroes are returned.
3645 *
3646 * This function is used to determine the buffer size needed for
3647 * a weston_surface_copy_content() call.
3648 */
3649WL_EXPORT void
3650weston_surface_get_content_size(struct weston_surface *surface,
3651 int *width, int *height)
3652{
3653 struct weston_renderer *rer = surface->compositor->renderer;
3654
3655 if (!rer->surface_get_content_size) {
3656 *width = 0;
3657 *height = 0;
3658 return;
3659 }
3660
3661 rer->surface_get_content_size(surface, width, height);
3662}
3663
Quentin Glidic248dd102016-08-12 10:41:34 +02003664/** Get the bounding box of a surface and its subsurfaces
3665 *
3666 * \param surface The surface to query.
3667 * \return The bounding box relative to the surface origin.
3668 *
3669 */
3670WL_EXPORT struct weston_geometry
3671weston_surface_get_bounding_box(struct weston_surface *surface)
3672{
3673 pixman_region32_t region;
3674 pixman_box32_t *box;
3675 struct weston_subsurface *subsurface;
3676
3677 pixman_region32_init_rect(&region,
3678 0, 0,
3679 surface->width, surface->height);
3680
3681 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3682 pixman_region32_union_rect(&region, &region,
3683 subsurface->position.x,
3684 subsurface->position.y,
3685 subsurface->surface->width,
3686 subsurface->surface->height);
3687
3688 box = pixman_region32_extents(&region);
3689 struct weston_geometry geometry = {
3690 .x = box->x1,
3691 .y = box->y1,
3692 .width = box->x2 - box->x1,
3693 .height = box->y2 - box->y1,
3694 };
3695
3696 pixman_region32_fini(&region);
3697
3698 return geometry;
3699}
3700
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003701/** Copy surface contents to system memory.
3702 *
3703 * \param surface The surface to copy from.
3704 * \param target Pointer to the target memory buffer.
3705 * \param size Size of the target buffer in bytes.
3706 * \param src_x X location on contents to copy from.
3707 * \param src_y Y location on contents to copy from.
3708 * \param width Width in pixels of the area to copy.
3709 * \param height Height in pixels of the area to copy.
3710 * \return 0 for success, -1 for failure.
3711 *
3712 * Surface contents are maintained by the renderer. They can be in a
3713 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3714 * else.
3715 *
3716 * Surface contents are copied into memory pointed to by target,
3717 * which has size bytes of space available. The target memory
3718 * may be larger than needed, but being smaller returns an error.
3719 * The extra bytes in target may or may not be written; their content is
3720 * unspecified. Size must be large enough to hold the image.
3721 *
3722 * The image in the target memory will be arranged in rows from
3723 * top to bottom, and pixels on a row from left to right. The pixel
3724 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3725 * width * 4.
3726 *
3727 * Parameters src_x and src_y define the upper-left corner in buffer
3728 * coordinates (pixels) to copy from. Parameters width and height
3729 * define the size of the area to copy in pixels.
3730 *
3731 * The rectangle defined by src_x, src_y, width, height must fit in
3732 * the surface contents. Otherwise an error is returned.
3733 *
3734 * Use surface_get_data_size to determine the content size; the
3735 * needed target buffer size and rectangle limits.
3736 *
3737 * CURRENT IMPLEMENTATION RESTRICTIONS:
3738 * - the machine must be little-endian due to Pixman formats.
3739 *
3740 * NOTE: Pixman formats are premultiplied.
3741 */
3742WL_EXPORT int
3743weston_surface_copy_content(struct weston_surface *surface,
3744 void *target, size_t size,
3745 int src_x, int src_y,
3746 int width, int height)
3747{
3748 struct weston_renderer *rer = surface->compositor->renderer;
3749 int cw, ch;
3750 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3751
3752 if (!rer->surface_copy_content)
3753 return -1;
3754
3755 weston_surface_get_content_size(surface, &cw, &ch);
3756
3757 if (src_x < 0 || src_y < 0)
3758 return -1;
3759
3760 if (width <= 0 || height <= 0)
3761 return -1;
3762
3763 if (src_x + width > cw || src_y + height > ch)
3764 return -1;
3765
3766 if (width * bytespp * height > size)
3767 return -1;
3768
3769 return rer->surface_copy_content(surface, target, size,
3770 src_x, src_y, width, height);
3771}
3772
Pekka Paalanene67858b2013-04-25 13:57:42 +03003773static void
3774subsurface_set_position(struct wl_client *client,
3775 struct wl_resource *resource, int32_t x, int32_t y)
3776{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003777 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003778
3779 if (!sub)
3780 return;
3781
3782 sub->position.x = x;
3783 sub->position.y = y;
3784 sub->position.set = 1;
3785}
3786
3787static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003788subsurface_find_sibling(struct weston_subsurface *sub,
3789 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003790{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003791 struct weston_surface *parent = sub->parent;
3792 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003793
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003794 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3795 if (sibling->surface == surface && sibling != sub)
3796 return sibling;
3797 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003798
3799 return NULL;
3800}
3801
3802static struct weston_subsurface *
3803subsurface_sibling_check(struct weston_subsurface *sub,
3804 struct weston_surface *surface,
3805 const char *request)
3806{
3807 struct weston_subsurface *sibling;
3808
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003809 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003810 if (!sibling) {
3811 wl_resource_post_error(sub->resource,
3812 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3813 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003814 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003815 return NULL;
3816 }
3817
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003818 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003819
3820 return sibling;
3821}
3822
3823static void
3824subsurface_place_above(struct wl_client *client,
3825 struct wl_resource *resource,
3826 struct wl_resource *sibling_resource)
3827{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003828 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003829 struct weston_surface *surface =
3830 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003831 struct weston_subsurface *sibling;
3832
3833 if (!sub)
3834 return;
3835
3836 sibling = subsurface_sibling_check(sub, surface, "place_above");
3837 if (!sibling)
3838 return;
3839
3840 wl_list_remove(&sub->parent_link_pending);
3841 wl_list_insert(sibling->parent_link_pending.prev,
3842 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003843
3844 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003845}
3846
3847static void
3848subsurface_place_below(struct wl_client *client,
3849 struct wl_resource *resource,
3850 struct wl_resource *sibling_resource)
3851{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003852 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003853 struct weston_surface *surface =
3854 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003855 struct weston_subsurface *sibling;
3856
3857 if (!sub)
3858 return;
3859
3860 sibling = subsurface_sibling_check(sub, surface, "place_below");
3861 if (!sibling)
3862 return;
3863
3864 wl_list_remove(&sub->parent_link_pending);
3865 wl_list_insert(&sibling->parent_link_pending,
3866 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003867
3868 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003869}
3870
3871static void
3872subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3873{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003874 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003875
3876 if (sub)
3877 sub->synchronized = 1;
3878}
3879
3880static void
3881subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3882{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003883 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003884
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003885 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003886 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003887
3888 /* If sub became effectively desynchronized, flush. */
3889 if (!weston_subsurface_is_synchronized(sub))
3890 weston_subsurface_synchronized_commit(sub);
3891 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003892}
3893
3894static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003895weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3896{
3897 wl_list_remove(&sub->parent_link);
3898 wl_list_remove(&sub->parent_link_pending);
3899 wl_list_remove(&sub->parent_destroy_listener.link);
3900 sub->parent = NULL;
3901}
3902
3903static void
3904weston_subsurface_destroy(struct weston_subsurface *sub);
3905
3906static void
3907subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3908{
3909 struct weston_subsurface *sub =
3910 container_of(listener, struct weston_subsurface,
3911 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003912 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003913
3914 /* The protocol object (wl_resource) is left inert. */
3915 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003916 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003917
3918 weston_subsurface_destroy(sub);
3919}
3920
3921static void
3922subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3923{
3924 struct weston_subsurface *sub =
3925 container_of(listener, struct weston_subsurface,
3926 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003927 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003928 assert(sub->surface != sub->parent);
3929
3930 if (weston_surface_is_mapped(sub->surface))
3931 weston_surface_unmap(sub->surface);
3932
3933 weston_subsurface_unlink_parent(sub);
3934}
3935
3936static void
3937subsurface_resource_destroy(struct wl_resource *resource)
3938{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003939 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003940
3941 if (sub)
3942 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003943}
3944
3945static void
3946subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3947{
3948 wl_resource_destroy(resource);
3949}
3950
3951static void
3952weston_subsurface_link_parent(struct weston_subsurface *sub,
3953 struct weston_surface *parent)
3954{
3955 sub->parent = parent;
3956 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003957 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003958 &sub->parent_destroy_listener);
3959
3960 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3961 wl_list_insert(&parent->subsurface_list_pending,
3962 &sub->parent_link_pending);
3963}
3964
3965static void
3966weston_subsurface_link_surface(struct weston_subsurface *sub,
3967 struct weston_surface *surface)
3968{
3969 sub->surface = surface;
3970 sub->surface_destroy_listener.notify =
3971 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003972 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003973 &sub->surface_destroy_listener);
3974}
3975
3976static void
3977weston_subsurface_destroy(struct weston_subsurface *sub)
3978{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003979 struct weston_view *view, *next;
3980
Pekka Paalanene67858b2013-04-25 13:57:42 +03003981 assert(sub->surface);
3982
3983 if (sub->resource) {
3984 assert(weston_surface_to_subsurface(sub->surface) == sub);
3985 assert(sub->parent_destroy_listener.notify ==
3986 subsurface_handle_parent_destroy);
3987
George Kiagiadakised04d382014-06-13 18:10:26 +02003988 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3989 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003990 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003991 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003992
Pekka Paalanene67858b2013-04-25 13:57:42 +03003993 if (sub->parent)
3994 weston_subsurface_unlink_parent(sub);
3995
Jason Ekstrand7b982072014-05-20 14:33:03 -05003996 weston_surface_state_fini(&sub->cached);
3997 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003998
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003999 sub->surface->committed = NULL;
4000 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004001 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004002 } else {
4003 /* the dummy weston_subsurface for the parent itself */
4004 assert(sub->parent_destroy_listener.notify == NULL);
4005 wl_list_remove(&sub->parent_link);
4006 wl_list_remove(&sub->parent_link_pending);
4007 }
4008
4009 wl_list_remove(&sub->surface_destroy_listener.link);
4010 free(sub);
4011}
4012
4013static const struct wl_subsurface_interface subsurface_implementation = {
4014 subsurface_destroy,
4015 subsurface_set_position,
4016 subsurface_place_above,
4017 subsurface_place_below,
4018 subsurface_set_sync,
4019 subsurface_set_desync
4020};
4021
4022static struct weston_subsurface *
4023weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4024 struct weston_surface *parent)
4025{
4026 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004027 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004028
Bryce Harringtonde16d892014-11-20 22:21:57 -08004029 sub = zalloc(sizeof *sub);
4030 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004031 return NULL;
4032
Jason Ekstranda7af7042013-10-12 22:38:11 -05004033 wl_list_init(&sub->unused_views);
4034
Jason Ekstranda85118c2013-06-27 20:17:02 -05004035 sub->resource =
4036 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004037 if (!sub->resource) {
4038 free(sub);
4039 return NULL;
4040 }
4041
Jason Ekstranda85118c2013-06-27 20:17:02 -05004042 wl_resource_set_implementation(sub->resource,
4043 &subsurface_implementation,
4044 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004045 weston_subsurface_link_surface(sub, surface);
4046 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004047 weston_surface_state_init(&sub->cached);
4048 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004049 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004050
4051 return sub;
4052}
4053
4054/* Create a dummy subsurface for having the parent itself in its
4055 * sub-surface lists. Makes stacking order manipulation easy.
4056 */
4057static struct weston_subsurface *
4058weston_subsurface_create_for_parent(struct weston_surface *parent)
4059{
4060 struct weston_subsurface *sub;
4061
Bryce Harringtonde16d892014-11-20 22:21:57 -08004062 sub = zalloc(sizeof *sub);
4063 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004064 return NULL;
4065
4066 weston_subsurface_link_surface(sub, parent);
4067 sub->parent = parent;
4068 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4069 wl_list_insert(&parent->subsurface_list_pending,
4070 &sub->parent_link_pending);
4071
4072 return sub;
4073}
4074
4075static void
4076subcompositor_get_subsurface(struct wl_client *client,
4077 struct wl_resource *resource,
4078 uint32_t id,
4079 struct wl_resource *surface_resource,
4080 struct wl_resource *parent_resource)
4081{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004082 struct weston_surface *surface =
4083 wl_resource_get_user_data(surface_resource);
4084 struct weston_surface *parent =
4085 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004086 struct weston_subsurface *sub;
4087 static const char where[] = "get_subsurface: wl_subsurface@";
4088
4089 if (surface == parent) {
4090 wl_resource_post_error(resource,
4091 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4092 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004093 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004094 return;
4095 }
4096
4097 if (weston_surface_to_subsurface(surface)) {
4098 wl_resource_post_error(resource,
4099 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4100 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004101 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004102 return;
4103 }
4104
Pekka Paalanen50b67472014-10-01 15:02:41 +03004105 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4106 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004107 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004108
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004109 if (weston_surface_get_main_surface(parent) == surface) {
4110 wl_resource_post_error(resource,
4111 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4112 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004113 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004114 return;
4115 }
4116
Pekka Paalanene67858b2013-04-25 13:57:42 +03004117 /* make sure the parent is in its own list */
4118 if (wl_list_empty(&parent->subsurface_list)) {
4119 if (!weston_subsurface_create_for_parent(parent)) {
4120 wl_resource_post_no_memory(resource);
4121 return;
4122 }
4123 }
4124
4125 sub = weston_subsurface_create(id, surface, parent);
4126 if (!sub) {
4127 wl_resource_post_no_memory(resource);
4128 return;
4129 }
4130
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004131 surface->committed = subsurface_committed;
4132 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004133 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004134}
4135
4136static void
4137subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4138{
4139 wl_resource_destroy(resource);
4140}
4141
4142static const struct wl_subcompositor_interface subcompositor_interface = {
4143 subcompositor_destroy,
4144 subcompositor_get_subsurface
4145};
4146
4147static void
4148bind_subcompositor(struct wl_client *client,
4149 void *data, uint32_t version, uint32_t id)
4150{
4151 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004152 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004153
Jason Ekstranda85118c2013-06-27 20:17:02 -05004154 resource =
4155 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004156 if (resource == NULL) {
4157 wl_client_post_no_memory(client);
4158 return;
4159 }
4160 wl_resource_set_implementation(resource, &subcompositor_interface,
4161 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004162}
4163
Bryce Harrington0795ece2016-08-30 12:04:26 -07004164/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004165 *
4166 * \param compositor The compositor instance
4167 * \param state The DPMS state the outputs will be set to
4168 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004169static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004170weston_compositor_dpms(struct weston_compositor *compositor,
4171 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004172{
4173 struct weston_output *output;
4174
Bryce Harrington08976ac2016-08-30 12:05:16 -07004175 wl_list_for_each(output, &compositor->output_list, link)
4176 if (output->set_dpms)
4177 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004178}
4179
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004180/** Restores the compositor to active status
4181 *
4182 * \param compositor The compositor instance
4183 *
4184 * If the compositor was in a sleeping mode, all outputs are powered
4185 * back on via DPMS. Otherwise if the compositor was inactive
4186 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4187 * signal will fire.
4188 *
4189 * Restarts the idle timer.
4190 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004191WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004192weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004193{
Neil Roberts8b62e202013-09-30 13:14:47 +01004194 uint32_t old_state = compositor->state;
4195
4196 /* The state needs to be changed before emitting the wake
4197 * signal because that may try to schedule a repaint which
4198 * will not work if the compositor is still sleeping */
4199 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4200
4201 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004202 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004203 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004204 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004205 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004206 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004207 /* fall through */
4208 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004209 wl_event_source_timer_update(compositor->idle_source,
4210 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004211 }
4212}
4213
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004214/** Turns off rendering and frame events for the compositor.
4215 *
4216 * \param compositor The compositor instance
4217 *
4218 * This is used for example to prevent further rendering while the
4219 * compositor is shutting down.
4220 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004221 * Stops the idle timer.
4222 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004223WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004224weston_compositor_offscreen(struct weston_compositor *compositor)
4225{
4226 switch (compositor->state) {
4227 case WESTON_COMPOSITOR_OFFSCREEN:
4228 return;
4229 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004230 default:
4231 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4232 wl_event_source_timer_update(compositor->idle_source, 0);
4233 }
4234}
4235
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004236/** Powers down all attached output devices
4237 *
4238 * \param compositor The compositor instance
4239 *
4240 * Causes rendering to the outputs to cease, and no frame events to be
4241 * sent. Only powers down the outputs if the compositor is not already
4242 * in sleep mode.
4243 *
4244 * Stops the idle timer.
4245 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004246WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004247weston_compositor_sleep(struct weston_compositor *compositor)
4248{
4249 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4250 return;
4251
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004252 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004253 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4254 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4255}
4256
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004257/** Sets compositor to idle mode
4258 *
4259 * \param data The compositor instance
4260 *
4261 * This is called when the idle timer fires. Once the compositor is in
4262 * idle mode it requires a wake action (e.g. via
4263 * weston_compositor_wake()) to restore it. The compositor's
4264 * idle_signal will be triggered when the idle event occurs.
4265 *
4266 * Idleness can be inhibited by setting the compositor's idle_inhibit
4267 * property.
4268 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004269static int
4270idle_handler(void *data)
4271{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004272 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004273
4274 if (compositor->idle_inhibit)
4275 return 1;
4276
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004277 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004278 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004279
4280 return 1;
4281}
4282
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004283WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004284weston_plane_init(struct weston_plane *plane,
4285 struct weston_compositor *ec,
4286 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004287{
4288 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004289 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004290 plane->x = x;
4291 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004292 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004293
4294 /* Init the link so that the call to wl_list_remove() when releasing
4295 * the plane without ever stacking doesn't lead to a crash */
4296 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004297}
4298
4299WL_EXPORT void
4300weston_plane_release(struct weston_plane *plane)
4301{
Xiong Zhang97116532013-10-23 13:58:31 +08004302 struct weston_view *view;
4303
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004304 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004305 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004306
Xiong Zhang97116532013-10-23 13:58:31 +08004307 wl_list_for_each(view, &plane->compositor->view_list, link) {
4308 if (view->plane == plane)
4309 view->plane = NULL;
4310 }
4311
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004312 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004313}
4314
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004315WL_EXPORT void
4316weston_compositor_stack_plane(struct weston_compositor *ec,
4317 struct weston_plane *plane,
4318 struct weston_plane *above)
4319{
4320 if (above)
4321 wl_list_insert(above->link.prev, &plane->link);
4322 else
4323 wl_list_insert(&ec->plane_list, &plane->link);
4324}
4325
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004326static void
4327output_release(struct wl_client *client, struct wl_resource *resource)
4328{
4329 wl_resource_destroy(resource);
4330}
4331
4332static const struct wl_output_interface output_interface = {
4333 output_release,
4334};
4335
4336
Casey Dahlin9074db52012-04-19 22:50:09 -04004337static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004338{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004339 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004340}
4341
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004342static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004343bind_output(struct wl_client *client,
4344 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004345{
Pekka Paalanen05347622017-03-27 12:24:34 +03004346 struct weston_head *head = data;
4347 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004348 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004349 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004350
Jason Ekstranda85118c2013-06-27 20:17:02 -05004351 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004352 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004353 if (resource == NULL) {
4354 wl_client_post_no_memory(client);
4355 return;
4356 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004357
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004358 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004359 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004360 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004361
Pekka Paalanen05347622017-03-27 12:24:34 +03004362 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004363 wl_output_send_geometry(resource,
4364 output->x,
4365 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004366 head->mm_width,
4367 head->mm_height,
4368 head->subpixel,
4369 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004370 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004371 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004372 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004373 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004374
4375 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004376 wl_output_send_mode(resource,
4377 mode->flags,
4378 mode->width,
4379 mode->height,
4380 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004381 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004382
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004383 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004384 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004385}
4386
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004387/** Remove the global wl_output protocol object
4388 *
4389 * \param head The head whose global to remove.
4390 *
4391 * Also orphans the wl_resources for this head (wl_output).
4392 */
4393static void
4394weston_head_remove_global(struct weston_head *head)
4395{
4396 struct wl_resource *resource, *tmp;
4397
4398 if (head->global)
4399 wl_global_destroy(head->global);
4400 head->global = NULL;
4401
4402 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4403 unbind_resource(resource);
4404 wl_resource_set_destructor(resource, NULL);
4405 wl_resource_set_user_data(resource, NULL);
4406 }
4407}
4408
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004409/** Get the backing object of wl_output
4410 *
4411 * \param resource A wl_output protocol object.
4412 * \return The backing object (user data) of a wl_resource representing a
4413 * wl_output protocol object.
4414 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004415WL_EXPORT struct weston_head *
4416weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004417{
4418 assert(wl_resource_instance_of(resource, &wl_output_interface,
4419 &output_interface));
4420
4421 return wl_resource_get_user_data(resource);
4422}
4423
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004424/** Initialize a pre-allocated weston_head
4425 *
4426 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004427 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004428 *
4429 * The head will be safe to attach, detach and release.
4430 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004431 * The name is used in logs, and can be used by compositors as a configuration
4432 * identifier.
4433 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004434 * \memberof weston_head
4435 * \internal
4436 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004437WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004438weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004439{
4440 /* Add some (in)sane defaults which can be used
4441 * for checking if an output was properly configured
4442 */
4443 memset(head, 0, sizeof *head);
4444
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004445 wl_list_init(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004446 wl_list_init(&head->output_link);
4447 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004448 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004449}
4450
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004451/** Idle task for emitting heads_changed_signal */
4452static void
4453weston_compositor_call_heads_changed(void *data)
4454{
4455 struct weston_compositor *compositor = data;
4456
4457 compositor->heads_changed_source = NULL;
4458
4459 wl_signal_emit(&compositor->heads_changed_signal, compositor);
4460}
4461
4462/** Schedule a call on idle to heads_changed callback
4463 *
4464 * \param compositor The Compositor.
4465 *
4466 * \memberof weston_compositor
4467 * \internal
4468 */
4469static void
4470weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4471{
4472 struct wl_event_loop *loop;
4473
4474 if (compositor->heads_changed_source)
4475 return;
4476
4477 loop = wl_display_get_event_loop(compositor->wl_display);
4478 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4479 weston_compositor_call_heads_changed, compositor);
4480}
4481
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004482/** Register a new head
4483 *
4484 * \param compositor The compositor.
4485 * \param head The head to register, must not be already registered.
4486 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004487 * This signals the core that a new head has become available, leading to
4488 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004489 *
4490 * \memberof weston_compositor
4491 * \internal
4492 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004493WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004494weston_compositor_add_head(struct weston_compositor *compositor,
4495 struct weston_head *head)
4496{
4497 assert(wl_list_empty(&head->compositor_link));
4498 assert(head->name);
4499
4500 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4501 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004502 weston_compositor_schedule_heads_changed(compositor);
4503}
4504
4505/** Adds a listener to be called when heads change
4506 *
4507 * \param compositor The compositor.
4508 * \param listener The listener to add.
4509 *
4510 * The listener notify function argument is the \var compositor.
4511 *
4512 * The listener function will be called after heads are added or their
4513 * connection status has changed. Several changes may be accumulated into a
4514 * single call. The user is expected to iterate over the existing heads and
4515 * check their statuses to find out what changed.
4516 *
4517 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4518 * weston_head_is_enabled
4519 * \memberof weston_compositor
4520 */
4521WL_EXPORT void
4522weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4523 struct wl_listener *listener)
4524{
4525 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004526}
4527
4528/** Iterate over available heads
4529 *
4530 * \param compositor The compositor.
4531 * \param item The iterator, or NULL for start.
4532 * \return The next available head in the list.
4533 *
4534 * Returns all available heads, regardless of being connected or enabled.
4535 *
4536 * You can iterate over all heads as follows:
4537 * \code
4538 * struct weston_head *head = NULL;
4539 *
4540 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4541 * ...
4542 * }
4543 * \endcode
4544 *
4545 * If you cause \c iter to be removed from the list, you cannot use it to
4546 * continue iterating. Removing any other item is safe.
4547 *
4548 * \memberof weston_compositor
4549 */
4550WL_EXPORT struct weston_head *
4551weston_compositor_iterate_heads(struct weston_compositor *compositor,
4552 struct weston_head *iter)
4553{
4554 struct wl_list *list = &compositor->head_list;
4555 struct wl_list *node;
4556
4557 assert(compositor);
4558 assert(!iter || iter->compositor == compositor);
4559
4560 if (iter)
4561 node = iter->compositor_link.next;
4562 else
4563 node = list->next;
4564
4565 assert(node);
4566 assert(!iter || node != &iter->compositor_link);
4567
4568 if (node == list)
4569 return NULL;
4570
4571 return container_of(node, struct weston_head, compositor_link);
4572}
4573
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004574/** Iterate over attached heads
4575 *
4576 * \param output The output whose heads to iterate.
4577 * \param item The iterator, or NULL for start.
4578 * \return The next attached head in the list.
4579 *
4580 * Returns all heads currently attached to the output.
4581 *
4582 * You can iterate over all heads as follows:
4583 * \code
4584 * struct weston_head *head = NULL;
4585 *
4586 * while ((head = weston_output_iterate_heads(output, head))) {
4587 * ...
4588 * }
4589 * \endcode
4590 *
4591 * If you cause \c iter to be removed from the list, you cannot use it to
4592 * continue iterating. Removing any other item is safe.
4593 *
4594 * \memberof weston_compositor
4595 */
4596WL_EXPORT struct weston_head *
4597weston_output_iterate_heads(struct weston_output *output,
4598 struct weston_head *iter)
4599{
4600 struct wl_list *list = &output->head_list;
4601 struct wl_list *node;
4602
4603 assert(output);
4604 assert(!iter || iter->output == output);
4605
4606 if (iter)
4607 node = iter->output_link.next;
4608 else
4609 node = list->next;
4610
4611 assert(node);
4612 assert(!iter || node != &iter->output_link);
4613
4614 if (node == list)
4615 return NULL;
4616
4617 return container_of(node, struct weston_head, output_link);
4618}
4619
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004620/** Attach a head to an inactive output
4621 *
4622 * \param output The output to attach to.
4623 * \param head The head that is not yet attached.
4624 * \return 0 on success, -1 on failure.
4625 *
4626 * Attaches the given head to the output. All heads of an output are clones
4627 * and share the resolution and timings.
4628 *
4629 * Cloning heads this way uses less resources than creating an output for
4630 * each head, but is not always possible due to environment, driver and hardware
4631 * limitations.
4632 *
4633 * On failure, the head remains unattached. Success of this function does not
4634 * guarantee the output configuration is actually valid. The final checks are
4635 * made on weston_output_enable().
4636 *
4637 * \memberof weston_output
4638 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004639WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004640weston_output_attach_head(struct weston_output *output,
4641 struct weston_head *head)
4642{
4643 if (output->enabled)
4644 return -1;
4645
4646 if (!wl_list_empty(&head->output_link))
4647 return -1;
4648
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004649 if (output->attach_head) {
4650 if (output->attach_head(output, head) < 0)
4651 return -1;
4652 } else if (!wl_list_empty(&output->head_list)) {
4653 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004654 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004655 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004656
4657 head->output = output;
4658 wl_list_insert(output->head_list.prev, &head->output_link);
4659
4660 return 0;
4661}
4662
4663/** Detach a head from its output
4664 *
4665 * \param head The head to detach.
4666 *
4667 * It is safe to detach a non-attached head.
4668 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004669 * If the head is attached to an enabled output and the output will be left
4670 * with no heads, the output will be disabled.
4671 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004672 * \memberof weston_head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004673 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004674 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004675WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004676weston_head_detach(struct weston_head *head)
4677{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004678 struct weston_output *output = head->output;
4679
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004680 wl_list_remove(&head->output_link);
4681 wl_list_init(&head->output_link);
4682 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004683
4684 if (!output)
4685 return;
4686
4687 if (output->detach_head)
4688 output->detach_head(output, head);
4689
4690 if (output->enabled) {
4691 weston_head_remove_global(head);
4692
4693 if (wl_list_empty(&output->head_list))
4694 weston_output_disable(output);
4695 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004696}
4697
4698/** Destroy a head
4699 *
4700 * \param head The head to be released.
4701 *
4702 * Destroys the head. The caller is responsible for freeing the memory pointed
4703 * to by \c head.
4704 *
4705 * \memberof weston_head
4706 * \internal
4707 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004708WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004709weston_head_release(struct weston_head *head)
4710{
4711 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004712
4713 free(head->make);
4714 free(head->model);
4715 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004716 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004717
4718 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004719}
4720
Pekka Paalanen01f60212017-03-24 15:39:24 +02004721/** Store monitor make, model and serial number
4722 *
4723 * \param head The head to modify.
4724 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
4725 * any string, or NULL for none.
4726 * \param model The monitor model or name, or a made-up string, or NULL for
4727 * none.
4728 * \param serialno The monitor serial number, a made-up string, or NULL for
4729 * none.
4730 *
4731 * \memberof weston_head
4732 * \internal
4733 */
4734WL_EXPORT void
4735weston_head_set_monitor_strings(struct weston_head *head,
4736 const char *make,
4737 const char *model,
4738 const char *serialno)
4739{
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004740 free(head->make);
4741 free(head->model);
4742 free(head->serial_number);
4743
4744 head->make = make ? strdup(make) : NULL;
4745 head->model = model ? strdup(model) : NULL;
4746 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanen01f60212017-03-24 15:39:24 +02004747}
4748
4749/** Store physical image size
4750 *
4751 * \param head The head to modify.
4752 * \param mm_width Image area width in millimeters.
4753 * \param mm_height Image area height in millimeters.
4754 *
4755 * \memberof weston_head
4756 * \internal
4757 */
4758WL_EXPORT void
4759weston_head_set_physical_size(struct weston_head *head,
4760 int32_t mm_width, int32_t mm_height)
4761{
4762 head->mm_width = mm_width;
4763 head->mm_height = mm_height;
4764}
4765
4766/** Store monitor sub-pixel layout
4767 *
4768 * \param head The head to modify.
4769 * \param sp Sub-pixel layout. The possible values are:
4770 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
4771 * - WL_OUTPUT_SUBPIXEL_NONE,
4772 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
4773 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
4774 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
4775 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
4776 *
4777 * \memberof weston_head
4778 * \internal
4779 */
4780WL_EXPORT void
4781weston_head_set_subpixel(struct weston_head *head,
4782 enum wl_output_subpixel sp)
4783{
4784 head->subpixel = sp;
4785}
4786
4787/** Mark the monitor as internal
4788 *
4789 * This is used for embedded screens, like laptop panels.
4790 *
4791 * \param head The head to mark as internal.
4792 *
4793 * By default a head is external. The type is often inferred from the physical
4794 * connector type.
4795 *
4796 * \memberof weston_head
4797 * \internal
4798 */
4799WL_EXPORT void
4800weston_head_set_internal(struct weston_head *head)
4801{
4802 head->connection_internal = true;
4803}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004804
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004805/** Store connector status
4806 *
4807 * \param head The head to modify.
4808 * \param connected Whether the head is connected.
4809 *
4810 * Connectors are created as disconnected. This function can be used to
4811 * set the connector status.
4812 *
4813 * The status should be set to true when a physical connector is connected to
4814 * a video sink device like a monitor and to false when the connector is
4815 * disconnected. For nested backends, the connection status should reflect the
4816 * connection to the parent display server.
4817 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004818 * When the connection status changes, it schedules a call to the heads_changed
4819 * hook.
4820 *
4821 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004822 * \memberof weston_head
4823 * \internal
4824 */
4825WL_EXPORT void
4826weston_head_set_connection_status(struct weston_head *head, bool connected)
4827{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004828 if (head->connected == connected)
4829 return;
4830
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004831 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004832
4833 if (head->compositor)
4834 weston_compositor_schedule_heads_changed(head->compositor);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004835}
4836
4837/** Is the head currently connected?
4838 *
4839 * \param head The head to query.
4840 * \return Connection status.
4841 *
4842 * Returns true if the head is physically connected to a monitor, or in
4843 * case of a nested backend returns true when there is a connection to the
4844 * parent display server.
4845 *
4846 * This is independent from the head being enabled.
4847 *
4848 * \sa weston_head_is_enabled
4849 * \memberof weston_head
4850 */
4851WL_EXPORT bool
4852weston_head_is_connected(struct weston_head *head)
4853{
4854 return head->connected;
4855}
4856
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02004857/** Is the head currently enabled?
4858 *
4859 * \param head The head to query.
4860 * \return Video status.
4861 *
4862 * Returns true if the head is currently transmitting a video stream.
4863 *
4864 * This is independent of the head being connected.
4865 *
4866 * \sa weston_head_is_connected
4867 * \memberof weston_head
4868 */
4869WL_EXPORT bool
4870weston_head_is_enabled(struct weston_head *head)
4871{
4872 if (!head->output)
4873 return false;
4874
4875 return head->output->enabled;
4876}
4877
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004878/** Get the name of a head
4879 *
4880 * \param head The head to query.
4881 * \return The head's name, not NULL.
4882 *
4883 * The name depends on the backend. The DRM backend uses connector names,
4884 * other backends may use hardcoded names or user-given names.
4885 */
4886WL_EXPORT const char *
4887weston_head_get_name(struct weston_head *head)
4888{
4889 return head->name;
4890}
4891
4892/** Get the output the head is attached to
4893 *
4894 * \param head The head to query.
4895 * \return The output the head is attached to, or NULL if detached.
4896 */
4897WL_EXPORT struct weston_output *
4898weston_head_get_output(struct weston_head *head)
4899{
4900 return head->output;
4901}
4902
David Fort0de859e2016-05-27 23:22:57 +02004903/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004904static void
David Fort0de859e2016-05-27 23:22:57 +02004905weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4906 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004907{
4908 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004909 bool start_resizing = false;
4910
4911 if (!delta_width)
4912 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004913
4914 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004915 if (output == resized_output) {
4916 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004917 continue;
4918 }
4919
David Fort0de859e2016-05-27 23:22:57 +02004920 if (start_resizing) {
4921 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004922 output->dirty = 1;
4923 }
4924 }
4925}
4926
Pekka Paalanend72bad22017-03-29 17:01:41 +03004927static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004928weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004929{
Scott Moreau850ca422012-05-21 15:21:25 -06004930 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004931
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004932 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004933 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004934
Scott Moreauccbf29d2012-02-22 14:21:41 -07004935 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004936 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004937 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004938 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004939 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004940 weston_matrix_scale(&output->matrix, magnification,
4941 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004942 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004943
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004944 switch (output->transform) {
4945 case WL_OUTPUT_TRANSFORM_FLIPPED:
4946 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4947 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4948 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4949 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4950 weston_matrix_scale(&output->matrix, -1, 1, 1);
4951 break;
4952 }
4953
4954 switch (output->transform) {
4955 default:
4956 case WL_OUTPUT_TRANSFORM_NORMAL:
4957 case WL_OUTPUT_TRANSFORM_FLIPPED:
4958 break;
4959 case WL_OUTPUT_TRANSFORM_90:
4960 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4961 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4962 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4963 break;
4964 case WL_OUTPUT_TRANSFORM_180:
4965 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4966 weston_matrix_translate(&output->matrix,
4967 -output->width, -output->height, 0);
4968 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4969 break;
4970 case WL_OUTPUT_TRANSFORM_270:
4971 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4972 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4973 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4974 break;
4975 }
4976
4977 if (output->current_scale != 1)
4978 weston_matrix_scale(&output->matrix,
4979 output->current_scale,
4980 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004981
Scott Moreauccbf29d2012-02-22 14:21:41 -07004982 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004983
4984 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004985}
4986
Scott Moreau1bad5db2012-08-18 01:04:05 -06004987static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004988weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004989{
4990 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004991 output->native_scale = scale;
4992 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004993
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004994 convert_size_by_transform_scale(&output->width, &output->height,
4995 output->current_mode->width,
4996 output->current_mode->height,
4997 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004998}
4999
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005000static void
5001weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005002{
5003 output->x = x;
5004 output->y = y;
5005
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005006 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005007 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005008
5009 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005010 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005011 output->width,
5012 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005013}
5014
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005015WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005016weston_output_move(struct weston_output *output, int x, int y)
5017{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005018 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005019 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005020 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005021
5022 output->move_x = x - output->x;
5023 output->move_y = y - output->y;
5024
5025 if (output->move_x == 0 && output->move_y == 0)
5026 return;
5027
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005028 weston_output_init_geometry(output, x, y);
5029
5030 output->dirty = 1;
5031
5032 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005033 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005034
5035 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005036 wl_list_for_each(head, &output->head_list, output_link) {
5037 wl_resource_for_each(resource, &head->resource_list) {
5038 wl_output_send_geometry(resource,
5039 output->x,
5040 output->y,
5041 head->mm_width,
5042 head->mm_height,
5043 head->subpixel,
5044 head->make,
5045 head->model,
5046 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005047
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005048 ver = wl_resource_get_version(resource);
5049 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5050 wl_output_send_done(resource);
5051 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005052 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005053}
5054
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005055/** Signal that a pending output is taken into use.
5056 *
5057 * Removes the output from the pending list and adds it to the compositor's
5058 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005059 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005060 * The output gets an internal ID assigned, and the wl_output global is
5061 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005062 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005063 * \param compositor The compositor instance.
5064 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005065 *
5066 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005067 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005068static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005069weston_compositor_add_output(struct weston_compositor *compositor,
5070 struct weston_output *output)
5071{
Armin Krezoviće5403842016-08-05 15:28:29 +02005072 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005073 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005074
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005075 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005076
5077 /* Verify we haven't reached the limit of 32 available output IDs */
5078 assert(ffs(~compositor->output_id_pool) > 0);
5079
5080 /* Invert the output id pool and look for the lowest numbered
5081 * switch (the least significant bit). Take that bit's position
5082 * as our ID, and mark it used in the compositor's output_id_pool.
5083 */
5084 output->id = ffs(~compositor->output_id_pool) - 1;
5085 compositor->output_id_pool |= 1u << output->id;
5086
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005087 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005088 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005089 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005090
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005091 wl_list_for_each(head, &output->head_list, output_link) {
5092 head->global = wl_global_create(compositor->wl_display,
5093 &wl_output_interface, 3,
5094 head, bind_output);
5095 }
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005096
Giulio Camuffob1147152015-05-06 21:41:57 +03005097 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005098
5099 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5100 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005101}
5102
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005103/** Transform device coordinates into global coordinates
5104 *
5105 * \param device_x[in] X coordinate in device units.
5106 * \param device_y[in] Y coordinate in device units.
5107 * \param x[out] X coordinate in the global space.
5108 * \param y[out] Y coordinate in the global space.
5109 *
5110 * Transforms coordinates from the device coordinate space
5111 * (physical pixel units) to the global coordinate space (logical pixel units).
5112 * This takes into account output transform and scale.
5113 *
5114 * \memberof weston_output
5115 * \internal
5116 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005117WL_EXPORT void
5118weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005119 double device_x, double device_y,
5120 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005121{
Derek Foreman0f679412014-10-02 13:41:17 -05005122 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005123 device_x,
5124 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005125 0.0,
5126 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005127
Derek Foreman67a18b92015-03-24 11:36:14 -05005128 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005129
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005130 *x = p.f[0] / p.f[3];
5131 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005132}
5133
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005134/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005135 *
5136 * \param output The weston_output object that is being removed.
5137 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005138 * The following happens:
5139 *
5140 * - The output assignments of all views in the current scenegraph are
5141 * recomputed.
5142 *
5143 * - Presentation feedback is discarded.
5144 *
5145 * - Compositor is notified that outputs were changed and
5146 * applies the necessary changes to re-layout outputs.
5147 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005148 * - The output is put back in the pending outputs list.
5149 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005150 * - Signal is emitted to notify all users of the weston_output
5151 * object that the output is being destroyed.
5152 *
5153 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005154 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005155 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005156 * - The output's internal ID is released.
5157 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005158 * \memberof weston_output
5159 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005160 */
5161static void
5162weston_compositor_remove_output(struct weston_output *output)
5163{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005164 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005165 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005166 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005167
5168 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005169 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005170
Pekka Paalanenbccda712017-03-29 16:16:04 +03005171 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005172 if (view->output_mask & (1u << output->id))
5173 weston_view_assign_output(view);
5174 }
5175
5176 weston_presentation_feedback_discard_list(&output->feedback_list);
5177
Pekka Paalanenbccda712017-03-29 16:16:04 +03005178 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005179
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005180 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005181 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005182 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005183
Pekka Paalanenbccda712017-03-29 16:16:04 +03005184 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005185 wl_signal_emit(&output->destroy_signal, output);
5186
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005187 wl_list_for_each(head, &output->head_list, output_link)
5188 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005189
5190 compositor->output_id_pool &= ~(1u << output->id);
5191 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005192}
5193
5194/** Sets the output scale for a given output.
5195 *
5196 * \param output The weston_output object that the scale is set for.
5197 * \param scale Scale factor for the given output.
5198 *
5199 * It only supports setting scale for an output that
5200 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005201 *
5202 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005203 */
5204WL_EXPORT void
5205weston_output_set_scale(struct weston_output *output,
5206 int32_t scale)
5207{
5208 /* We can only set scale on a disabled output */
5209 assert(!output->enabled);
5210
5211 /* We only want to set scale once */
5212 assert(!output->scale);
5213
5214 output->scale = scale;
5215}
5216
5217/** Sets the output transform for a given output.
5218 *
5219 * \param output The weston_output object that the transform is set for.
5220 * \param transform Transform value for the given output.
5221 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005222 * Refer to wl_output::transform section located at
5223 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5224 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005225 *
5226 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005227 */
5228WL_EXPORT void
5229weston_output_set_transform(struct weston_output *output,
5230 uint32_t transform)
5231{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005232 struct weston_pointer_motion_event ev;
5233 struct wl_resource *resource;
5234 struct weston_seat *seat;
5235 pixman_region32_t old_region;
5236 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005237 struct weston_head *head;
5238 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005239
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005240 if (!output->enabled && output->transform == UINT32_MAX) {
5241 output->transform = transform;
5242 return;
5243 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005244
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005245 weston_output_transform_scale_init(output, transform, output->scale);
5246
5247 pixman_region32_init(&old_region);
5248 pixman_region32_copy(&old_region, &output->region);
5249
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005250 weston_output_init_geometry(output, output->x, output->y);
5251
5252 output->dirty = 1;
5253
5254 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005255 wl_list_for_each(head, &output->head_list, output_link) {
5256 wl_resource_for_each(resource, &head->resource_list) {
5257 wl_output_send_geometry(resource,
5258 output->x,
5259 output->y,
5260 head->mm_width,
5261 head->mm_height,
5262 head->subpixel,
5263 head->make,
5264 head->model,
5265 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005266
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005267 ver = wl_resource_get_version(resource);
5268 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5269 wl_output_send_done(resource);
5270 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005271 }
5272
5273 /* we must ensure that pointers are inside output, otherwise they disappear */
5274 mid_x = output->x + output->width / 2;
5275 mid_y = output->y + output->height / 2;
5276
5277 ev.mask = WESTON_POINTER_MOTION_ABS;
5278 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5279 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5280
5281 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5282 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5283
5284 if (pointer && pixman_region32_contains_point(&old_region,
5285 wl_fixed_to_int(pointer->x),
5286 wl_fixed_to_int(pointer->y),
5287 NULL))
5288 weston_pointer_move(pointer, &ev);
5289 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005290}
5291
5292/** Initializes a weston_output object with enough data so
5293 ** an output can be configured.
5294 *
5295 * \param output The weston_output object to initialize
5296 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005297 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005298 *
5299 * Sets initial values for fields that are expected to be
5300 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005301 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005302 * The name is used in logs, and can be used by compositors as a configuration
5303 * identifier.
5304 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005305 * \memberof weston_output
5306 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005307 */
5308WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005309weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005310 struct weston_compositor *compositor,
5311 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005312{
5313 output->compositor = compositor;
5314 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005315 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005316 wl_list_init(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005317 output->enabled = false;
5318
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005319 wl_list_init(&output->head_list);
5320
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005321 weston_head_init(&output->head, name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005322 output->head.allocator_output = output;
5323 if (!compositor->backend->create_output) {
5324 weston_head_set_connection_status(&output->head, true);
5325 weston_compositor_add_head(compositor, &output->head);
5326 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005327
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005328 /* Add some (in)sane defaults which can be used
5329 * for checking if an output was properly configured
5330 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005331 output->scale = 0;
5332 /* Can't use -1 on uint32_t and 0 is valid enum value */
5333 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005334
5335 pixman_region32_init(&output->previous_damage);
5336 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005337 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005338}
5339
5340/** Adds weston_output object to pending output list.
5341 *
5342 * \param output The weston_output object to add
5343 * \param compositor The compositor instance.
5344 *
5345 * Also notifies the compositor that an output is pending for
5346 * configuration.
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005347 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005348 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005349 *
5350 * \memberof weston_output
5351 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005352 */
5353WL_EXPORT void
5354weston_compositor_add_pending_output(struct weston_output *output,
5355 struct weston_compositor *compositor)
5356{
Pekka Paalanene952a012017-03-29 17:14:00 +03005357 assert(output->disable);
5358 assert(output->enable);
5359
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005360 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005361 wl_list_insert(compositor->pending_output_list.prev, &output->link);
5362 wl_signal_emit(&compositor->output_pending_signal, output);
5363}
5364
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005365/** Constructs a weston_output object that can be used by the compositor.
5366 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005367 * \param output The weston_output object that needs to be enabled. Must not
5368 * be enabled already.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005369 *
5370 * Output coordinates are calculated and each new output is by default
5371 * assigned to the right of previous one.
5372 *
5373 * Sets up the transformation, zoom, and geometry of the output using
5374 * the properties that need to be configured by the compositor.
5375 *
5376 * Establishes a repaint timer for the output with the relevant display
5377 * object's event loop. See output_repaint_timer_handler().
5378 *
5379 * The output is assigned an ID. Weston can support up to 32 distinct
5380 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5381 * is referred to and used to find the first available ID number, and
5382 * then this ID is marked as used in output_id_pool.
5383 *
5384 * The output is also assigned a Wayland global with the wl_output
5385 * external interface.
5386 *
5387 * Backend specific function is called to set up the output output.
5388 *
5389 * Output is added to the compositor's output list
5390 *
5391 * If the backend specific function fails, the weston_output object
5392 * is returned to a state it was before calling this function and
5393 * is added to the compositor's pending_output_list in case it needs
5394 * to be reconfigured or just so it can be destroyed at shutdown.
5395 *
5396 * 0 is returned on success, -1 on failure.
5397 */
5398WL_EXPORT int
5399weston_output_enable(struct weston_output *output)
5400{
Armin Krezović782f5df2016-09-30 14:11:11 +02005401 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005402 struct weston_output *iterator;
5403 int x = 0, y = 0;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005404 int ret;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005405
Pekka Paalanencc201e42017-03-30 15:11:25 +03005406 if (output->enabled) {
5407 weston_log("Error: attempt to enable an enabled output '%s'\n",
5408 output->name);
5409 return -1;
5410 }
5411
Armin Krezović782f5df2016-09-30 14:11:11 +02005412 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005413 struct weston_output, link);
5414
Armin Krezović782f5df2016-09-30 14:11:11 +02005415 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005416 x = iterator->x + iterator->width;
5417
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005418 /* Make sure the scale is set up */
5419 assert(output->scale);
5420
5421 /* Make sure we have a transform set */
5422 assert(output->transform != UINT32_MAX);
5423
Armin Krezović782f5df2016-09-30 14:11:11 +02005424 output->x = x;
5425 output->y = y;
5426 output->dirty = 1;
5427 output->original_scale = output->scale;
5428
5429 weston_output_transform_scale_init(output, output->transform, output->scale);
5430 weston_output_init_zoom(output);
5431
5432 weston_output_init_geometry(output, x, y);
5433 weston_output_damage(output);
5434
5435 wl_signal_init(&output->frame_signal);
5436 wl_signal_init(&output->destroy_signal);
5437 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005438 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005439
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005440 /* XXX: Temporary until all backends are converted. */
5441 if (wl_list_empty(&output->head_list)) {
5442 ret = weston_output_attach_head(output, &output->head);
5443 assert(ret == 0);
5444 }
5445
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005446 /* Enable the output (set up the crtc or create a
5447 * window representing the output, set up the
5448 * renderer, etc)
5449 */
5450 if (output->enable(output) < 0) {
5451 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005452 return -1;
5453 }
5454
5455 weston_compositor_add_output(output->compositor, output);
5456
5457 return 0;
5458}
5459
5460/** Converts a weston_output object to a pending output state, so it
5461 ** can be configured again or destroyed.
5462 *
5463 * \param output The weston_output object that needs to be disabled.
5464 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005465 * Calls a backend specific function to disable an output, in case
5466 * such function exists.
5467 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005468 * The backend specific disable function may choose to postpone the disabling
5469 * by returning a negative value, in which case this function returns early.
5470 * In that case the backend will guarantee the output will be disabled soon
5471 * by the backend calling this function again. One must not attempt to re-enable
5472 * the output until that happens.
5473 *
5474 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005475 * from weston's output_list (see weston_compositor_remove_output())
5476 * and is returned to a state it was before weston_output_enable()
5477 * was ran (see weston_output_enable_undo()).
5478 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005479 * See weston_output_init() for more information on the
5480 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005481 *
5482 * If the output has never been enabled yet, this function can still be
5483 * called to ensure that the output is actually turned off rather than left
5484 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005485 */
5486WL_EXPORT void
5487weston_output_disable(struct weston_output *output)
5488{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005489 /* Should we rename this? */
5490 output->destroying = 1;
5491
Pekka Paalanenc65df642017-03-29 15:45:46 +03005492 /* Disable is called unconditionally also for not-enabled outputs,
5493 * because at compositor start-up, if there is an output that is
5494 * already on but the compositor wants to turn it off, we have to
5495 * forward the turn-off to the backend so it knows to do it.
5496 * The backend cannot initially turn off everything, because it
5497 * would cause unnecessary mode-sets for all outputs the compositor
5498 * wants to be on.
5499 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005500 if (output->disable(output) < 0)
5501 return;
5502
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005503 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005504 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005505
5506 output->destroying = 0;
5507}
5508
5509/** Emits a signal to indicate that there are outputs waiting to be configured.
5510 *
5511 * \param compositor The compositor instance
5512 */
5513WL_EXPORT void
5514weston_pending_output_coldplug(struct weston_compositor *compositor)
5515{
5516 struct weston_output *output, *next;
5517
5518 wl_list_for_each_safe(output, next, &compositor->pending_output_list, link)
5519 wl_signal_emit(&compositor->output_pending_signal, output);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005520
5521 /* Execute the heads changed callback manually to ensure it is
5522 * processed before any plugins get their start-up idle tasks ran.
5523 * This ensures the plugins see all the initial outputs.
5524 */
5525 if (compositor->heads_changed_source) {
5526 wl_event_source_remove(compositor->heads_changed_source);
5527 weston_compositor_call_heads_changed(compositor);
5528 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005529}
5530
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005531/** Uninitialize an output
5532 *
5533 * Removes the output from the list of enabled outputs if necessary, but
5534 * does not call the backend's output disable function. The output will no
5535 * longer be in the list of pending outputs either.
5536 *
5537 * All fields of weston_output become uninitialized, i.e. should not be used
5538 * anymore. The caller can free the memory after this.
5539 *
5540 * \memberof weston_output
5541 * \internal
5542 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005543WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005544weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005545{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005546 struct weston_head *head, *tmp;
5547
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005548 output->destroying = 1;
5549
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005550 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005551 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005552
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005553 pixman_region32_fini(&output->region);
5554 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005555 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005556
5557 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
5558 weston_head_detach(head);
5559
5560 weston_head_release(&output->head);
5561
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005562 free(output->name);
5563}
5564
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005565/** Create an output for an unused head
5566 *
5567 * \param compositor The compositor.
5568 * \param head The head to attach to the output.
5569 * \return A new \c weston_output, or NULL on failure.
5570 *
5571 * This creates a new weston_output that starts with the given head attached.
5572 * The output inherits the name of the head. The head must not be already
5573 * attached to another output.
5574 *
5575 * An output must be configured before it can be enabled.
5576 *
5577 * \memberof weston_compositor
5578 */
5579WL_EXPORT struct weston_output *
5580weston_compositor_create_output_with_head(struct weston_compositor *compositor,
5581 struct weston_head *head)
5582{
5583 struct weston_output *output;
5584
5585 if (head->allocator_output) {
5586 /* XXX: compatibility path to be removed after all converted */
5587 output = head->allocator_output;
5588 } else {
5589 assert(compositor->backend->create_output);
5590 output = compositor->backend->create_output(compositor,
5591 head->name);
5592 }
5593
5594 if (!output)
5595 return NULL;
5596
5597 if (weston_output_attach_head(output, head) < 0) {
5598 if (!head->allocator_output)
5599 output->destroy(output);
5600
5601 return NULL;
5602 }
5603
5604 return output;
5605}
5606
5607/** Destroy an output
5608 *
5609 * \param output The output to destroy.
5610 *
5611 * The heads attached to the given output are detached and become unused again.
5612 *
5613 * It is not necessary to explicitly destroy all outputs at compositor exit.
5614 * weston_compositor_destroy() will automatically destroy any remaining
5615 * outputs.
5616 *
5617 * \memberof weston_output
5618 */
5619WL_EXPORT void
5620weston_output_destroy(struct weston_output *output)
5621{
5622 struct weston_head *head;
5623
5624 /* XXX: compatibility path to be removed after all converted */
5625 head = weston_output_get_first_head(output);
5626 if (head->allocator_output) {
5627 /* The old design: backend is responsible for destroying the
5628 * output, so just undo create_output_with_head()
5629 */
5630 weston_head_detach(head);
5631 return;
5632 }
5633
5634 output->destroy(output);
5635}
5636
Pekka Paalanencf0a4762017-04-04 16:36:07 +03005637/** When you need a head...
5638 *
5639 * This function is a hack, used until all code has been converted to become
5640 * multi-head aware.
5641 *
5642 * \param output The weston_output whose head to get.
5643 * \return The first head in the output's list.
5644 */
5645WL_EXPORT struct weston_head *
5646weston_output_get_first_head(struct weston_output *output)
5647{
5648 if (wl_list_empty(&output->head_list))
5649 return NULL;
5650
5651 return container_of(output->head_list.next,
5652 struct weston_head, output_link);
5653}
5654
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01005655static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005656destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005657{
Jonny Lamb74130762013-11-26 18:19:46 +01005658 struct weston_surface *surface =
5659 wl_resource_get_user_data(resource);
5660
Pekka Paalanen4826f872016-04-22 14:14:38 +03005661 if (!surface)
5662 return;
5663
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005664 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02005665 surface->pending.buffer_viewport.buffer.src_width =
5666 wl_fixed_from_int(-1);
5667 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005668 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005669}
5670
5671static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005672viewport_destroy(struct wl_client *client,
5673 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005674{
5675 wl_resource_destroy(resource);
5676}
5677
5678static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005679viewport_set_source(struct wl_client *client,
5680 struct wl_resource *resource,
5681 wl_fixed_t src_x,
5682 wl_fixed_t src_y,
5683 wl_fixed_t src_width,
5684 wl_fixed_t src_height)
5685{
5686 struct weston_surface *surface =
5687 wl_resource_get_user_data(resource);
5688
Pekka Paalanen4826f872016-04-22 14:14:38 +03005689 if (!surface) {
5690 wl_resource_post_error(resource,
5691 WP_VIEWPORT_ERROR_NO_SURFACE,
5692 "wl_surface for this viewport is no longer exists");
5693 return;
5694 }
5695
5696 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03005697 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005698
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005699 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03005700 src_height == wl_fixed_from_int(-1) &&
5701 src_x == wl_fixed_from_int(-1) &&
5702 src_y == wl_fixed_from_int(-1)) {
5703 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005704 surface->pending.buffer_viewport.buffer.src_width =
5705 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005706 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005707 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005708 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005709
Pekka Paalanen201769a2016-04-26 14:42:11 +03005710 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005711 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005712 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03005713 "wl_surface@%d viewport source "
5714 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
5715 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005716 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03005717 wl_fixed_to_double(src_height),
5718 wl_fixed_to_double(src_x),
5719 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005720 return;
5721 }
5722
5723 surface->pending.buffer_viewport.buffer.src_x = src_x;
5724 surface->pending.buffer_viewport.buffer.src_y = src_y;
5725 surface->pending.buffer_viewport.buffer.src_width = src_width;
5726 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005727 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005728}
5729
5730static void
5731viewport_set_destination(struct wl_client *client,
5732 struct wl_resource *resource,
5733 int32_t dst_width,
5734 int32_t dst_height)
5735{
5736 struct weston_surface *surface =
5737 wl_resource_get_user_data(resource);
5738
Pekka Paalanen4826f872016-04-22 14:14:38 +03005739 if (!surface) {
5740 wl_resource_post_error(resource,
5741 WP_VIEWPORT_ERROR_NO_SURFACE,
5742 "wl_surface for this viewport no longer exists");
5743 return;
5744 }
5745
5746 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005747
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005748 if (dst_width == -1 && dst_height == -1) {
5749 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005750 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005751 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005752 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005753 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005754
5755 if (dst_width <= 0 || dst_height <= 0) {
5756 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005757 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005758 "destination size must be positive (%dx%d)",
5759 dst_width, dst_height);
5760 return;
5761 }
5762
5763 surface->pending.buffer_viewport.surface.width = dst_width;
5764 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005765 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005766}
5767
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005768static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005769 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005770 viewport_set_source,
5771 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01005772};
5773
5774static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005775viewporter_destroy(struct wl_client *client,
5776 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005777{
5778 wl_resource_destroy(resource);
5779}
5780
5781static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005782viewporter_get_viewport(struct wl_client *client,
5783 struct wl_resource *viewporter,
5784 uint32_t id,
5785 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005786{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005787 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005788 struct weston_surface *surface =
5789 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005790 struct wl_resource *resource;
5791
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005792 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005793 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005794 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005795 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01005796 return;
5797 }
5798
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005799 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005800 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005801 if (resource == NULL) {
5802 wl_client_post_no_memory(client);
5803 return;
5804 }
5805
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005806 wl_resource_set_implementation(resource, &viewport_interface,
5807 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01005808
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005809 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005810}
5811
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005812static const struct wp_viewporter_interface viewporter_interface = {
5813 viewporter_destroy,
5814 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01005815};
5816
5817static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005818bind_viewporter(struct wl_client *client,
5819 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005820{
5821 struct wl_resource *resource;
5822
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005823 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005824 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005825 if (resource == NULL) {
5826 wl_client_post_no_memory(client);
5827 return;
5828 }
5829
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005830 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01005831 NULL, NULL);
5832}
5833
5834static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04005835destroy_presentation_feedback(struct wl_resource *feedback_resource)
5836{
5837 struct weston_presentation_feedback *feedback;
5838
5839 feedback = wl_resource_get_user_data(feedback_resource);
5840
5841 wl_list_remove(&feedback->link);
5842 free(feedback);
5843}
5844
5845static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005846presentation_destroy(struct wl_client *client, struct wl_resource *resource)
5847{
5848 wl_resource_destroy(resource);
5849}
5850
5851static void
5852presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005853 struct wl_resource *presentation_resource,
5854 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005855 uint32_t callback)
5856{
Pekka Paalanen133e4392014-09-23 22:08:46 -04005857 struct weston_surface *surface;
5858 struct weston_presentation_feedback *feedback;
5859
5860 surface = wl_resource_get_user_data(surface_resource);
5861
Bryce Harringtonde16d892014-11-20 22:21:57 -08005862 feedback = zalloc(sizeof *feedback);
5863 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04005864 goto err_calloc;
5865
5866 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005867 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005868 1, callback);
5869 if (!feedback->resource)
5870 goto err_create;
5871
5872 wl_resource_set_implementation(feedback->resource, NULL, feedback,
5873 destroy_presentation_feedback);
5874
5875 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
5876
5877 return;
5878
5879err_create:
5880 free(feedback);
5881
5882err_calloc:
5883 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005884}
5885
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005886static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005887 presentation_destroy,
5888 presentation_feedback
5889};
5890
5891static void
5892bind_presentation(struct wl_client *client,
5893 void *data, uint32_t version, uint32_t id)
5894{
5895 struct weston_compositor *compositor = data;
5896 struct wl_resource *resource;
5897
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005898 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005899 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005900 if (resource == NULL) {
5901 wl_client_post_no_memory(client);
5902 return;
5903 }
5904
5905 wl_resource_set_implementation(resource, &presentation_implementation,
5906 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005907 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005908}
5909
5910static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05005911compositor_bind(struct wl_client *client,
5912 void *data, uint32_t version, uint32_t id)
5913{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005914 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005915 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05005916
Jason Ekstranda85118c2013-06-27 20:17:02 -05005917 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005918 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005919 if (resource == NULL) {
5920 wl_client_post_no_memory(client);
5921 return;
5922 }
5923
5924 wl_resource_set_implementation(resource, &compositor_interface,
5925 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05005926}
5927
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005928WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005929weston_environment_get_fd(const char *env)
5930{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005931 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005932 int fd, flags;
5933
5934 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005935 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005936 return -1;
5937
5938 flags = fcntl(fd, F_GETFD);
5939 if (flags == -1)
5940 return -1;
5941
5942 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
5943 unsetenv(env);
5944
5945 return fd;
5946}
5947
Pekka Paalanenb5026542014-11-12 15:09:24 +02005948static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005949timeline_key_binding_handler(struct weston_keyboard *keyboard,
5950 const struct timespec *time, uint32_t key,
5951 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02005952{
5953 struct weston_compositor *compositor = data;
5954
5955 if (weston_timeline_enabled_)
5956 weston_timeline_close();
5957 else
5958 weston_timeline_open(compositor);
5959}
5960
Giulio Camuffo459137b2014-10-11 23:56:24 +03005961/** Create the compositor.
5962 *
5963 * This functions creates and initializes a compositor instance.
5964 *
5965 * \param display The Wayland display to be used.
5966 * \param user_data A pointer to an object that can later be retrieved
5967 * using the \ref weston_compositor_get_user_data function.
5968 * \return The compositor instance on success or NULL on failure.
5969 */
5970WL_EXPORT struct weston_compositor *
5971weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005972{
Giulio Camuffo459137b2014-10-11 23:56:24 +03005973 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05005974 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07005975
Giulio Camuffo459137b2014-10-11 23:56:24 +03005976 ec = zalloc(sizeof *ec);
5977 if (!ec)
5978 return NULL;
5979
5980 ec->wl_display = display;
5981 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005982 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07005983 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005984 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005985 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005986 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005987 wl_signal_init(&ec->idle_signal);
5988 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005989 wl_signal_init(&ec->show_input_panel_signal);
5990 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005991 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01005992 wl_signal_init(&ec->seat_created_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005993 wl_signal_init(&ec->output_pending_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01005994 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02005995 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005996 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02005997 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005998 wl_signal_init(&ec->heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005999 wl_signal_init(&ec->session_signal);
6000 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006001
Casey Dahlin58ba1372012-04-19 22:50:08 -04006002 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006003 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006004
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006005 ec->activate_serial = 1;
6006
Derek Foreman152254b2015-11-26 14:17:48 -06006007 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006008 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006009 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006010
Giulio Camuffo954f1832014-10-11 18:27:30 +03006011 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006012 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006013 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006014
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006015 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006016 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006017 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006018
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006019 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006020 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006021 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006022
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006023 if (weston_input_init(ec) != 0)
6024 goto fail;
6025
Jason Ekstranda7af7042013-10-12 22:38:11 -05006026 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006027 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006028 wl_list_init(&ec->layer_list);
6029 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006030 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006031 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006032 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006033 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006034 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006035 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006036 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006037 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006038 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006039
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006040 wl_list_init(&ec->plugin_api_list);
6041
Xiong Zhang97116532013-10-23 13:58:31 +08006042 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006043 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006044
Giulio Camuffo459137b2014-10-11 23:56:24 +03006045 wl_data_device_manager_init(ec->wl_display);
6046
6047 wl_display_init_shm(ec->wl_display);
6048
6049 loop = wl_display_get_event_loop(ec->wl_display);
6050 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006051 ec->repaint_timer =
6052 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6053 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006054
Quentin Glidic82681572016-12-17 13:40:51 +01006055 weston_layer_init(&ec->fade_layer, ec);
6056 weston_layer_init(&ec->cursor_layer, ec);
6057
6058 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6059 weston_layer_set_position(&ec->cursor_layer,
6060 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006061
6062 weston_compositor_add_debug_binding(ec, KEY_T,
6063 timeline_key_binding_handler, ec);
6064
Giulio Camuffo459137b2014-10-11 23:56:24 +03006065 return ec;
6066
6067fail:
6068 free(ec);
6069 return NULL;
6070}
6071
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006072WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006073weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006074{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006075 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006076
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006077 wl_event_source_remove(ec->idle_source);
6078
Matt Roper361d2ad2011-08-29 13:52:23 -07006079 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006080 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006081 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006082
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006083 /* Destroy all pending outputs associated with this compositor */
6084 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6085 output->destroy(output);
6086
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006087 if (ec->renderer)
6088 ec->renderer->destroy(ec);
6089
Daniel Stone325fc2d2012-05-30 16:31:58 +01006090 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006091 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006092 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006093 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006094 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006095 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006096
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006097 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006098}
6099
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006100WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006101weston_compositor_exit_with_code(struct weston_compositor *compositor,
6102 int exit_code)
6103{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006104 if (compositor->exit_code == EXIT_SUCCESS)
6105 compositor->exit_code = exit_code;
6106
Giulio Camuffo459137b2014-10-11 23:56:24 +03006107 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006108}
6109
6110WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006111weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6112 const struct weston_pointer_grab_interface *interface)
6113{
6114 struct weston_seat *seat;
6115
6116 ec->default_pointer_grab = interface;
6117 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006118 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6119
6120 if (pointer)
6121 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006122 }
6123}
6124
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006125WL_EXPORT int
6126weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6127 clockid_t clk_id)
6128{
6129 struct timespec ts;
6130
6131 if (clock_gettime(clk_id, &ts) < 0)
6132 return -1;
6133
6134 compositor->presentation_clock = clk_id;
6135
6136 return 0;
6137}
6138
6139/*
6140 * For choosing the software clock, when the display hardware or API
6141 * does not expose a compatible presentation timestamp.
6142 */
6143WL_EXPORT int
6144weston_compositor_set_presentation_clock_software(
6145 struct weston_compositor *compositor)
6146{
6147 /* In order of preference */
6148 static const clockid_t clocks[] = {
6149 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6150 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6151 CLOCK_MONOTONIC, /* no jumps, may crawl */
6152 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
6153 CLOCK_REALTIME /* may jump and crawl */
6154 };
6155 unsigned i;
6156
6157 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
6158 if (weston_compositor_set_presentation_clock(compositor,
6159 clocks[i]) == 0)
6160 return 0;
6161
6162 weston_log("Error: no suitable presentation clock available.\n");
6163
6164 return -1;
6165}
6166
Pekka Paalanen662f3842015-03-18 12:17:26 +02006167/** Read the current time from the Presentation clock
6168 *
6169 * \param compositor
6170 * \param ts[out] The current time.
6171 *
6172 * \note Reading the current time in user space is always imprecise to some
6173 * degree.
6174 *
6175 * This function is never meant to fail. If reading the clock does fail,
6176 * an error message is logged and a zero time is returned. Callers are not
6177 * supposed to detect or react to failures.
6178 */
6179WL_EXPORT void
6180weston_compositor_read_presentation_clock(
6181 const struct weston_compositor *compositor,
6182 struct timespec *ts)
6183{
6184 static bool warned;
6185 int ret;
6186
6187 ret = clock_gettime(compositor->presentation_clock, ts);
6188 if (ret < 0) {
6189 ts->tv_sec = 0;
6190 ts->tv_nsec = 0;
6191
6192 if (!warned)
6193 weston_log("Error: failure to read "
6194 "the presentation clock %#x: '%m' (%d)\n",
6195 compositor->presentation_clock, errno);
6196 warned = true;
6197 }
6198}
6199
Pekka Paalanen230f3b12014-09-29 14:18:40 -04006200/** Import dmabuf buffer into current renderer
6201 *
6202 * \param compositor
6203 * \param buffer the dmabuf buffer to import
6204 * \return true on usable buffers, false otherwise
6205 *
6206 * This function tests that the linux_dmabuf_buffer is usable
6207 * for the current renderer. Returns false on unusable buffers. Usually
6208 * usability is tested by importing the dmabufs for composition.
6209 *
6210 * This hook is also used for detecting if the renderer supports
6211 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
6212 * supported.
6213 * */
6214WL_EXPORT bool
6215weston_compositor_import_dmabuf(struct weston_compositor *compositor,
6216 struct linux_dmabuf_buffer *buffer)
6217{
6218 struct weston_renderer *renderer;
6219
6220 renderer = compositor->renderer;
6221
6222 if (renderer->import_dmabuf == NULL)
6223 return false;
6224
6225 return renderer->import_dmabuf(compositor, buffer);
6226}
6227
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006228WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006229weston_version(int *major, int *minor, int *micro)
6230{
6231 *major = WESTON_VERSION_MAJOR;
6232 *minor = WESTON_VERSION_MINOR;
6233 *micro = WESTON_VERSION_MICRO;
6234}
6235
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03006236WL_EXPORT void *
6237weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006238{
Derek Foreman3f86e502015-06-08 11:46:54 -05006239 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006240 char path[PATH_MAX];
6241 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00006242 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006243
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08006244 if (name == NULL)
6245 return NULL;
6246
Derek Foreman3f86e502015-06-08 11:46:54 -05006247 if (name[0] != '/') {
6248 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00006249 len = snprintf(path, sizeof path, "%s/.libs/%s",
6250 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006251 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00006252 len = snprintf(path, sizeof path, "%s/%s",
6253 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006254 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00006255 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006256 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006257
Daniel Stonebeb97e52016-11-28 12:13:54 +00006258 /* snprintf returns the length of the string it would've written,
6259 * _excluding_ the NUL byte. So even being equal to the size of
6260 * our buffer is an error here. */
6261 if (len >= sizeof path)
6262 return NULL;
6263
Kristian Høgsberga6813d22012-09-12 12:21:01 -04006264 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
6265 if (module) {
6266 weston_log("Module '%s' already loaded\n", path);
6267 dlclose(module);
6268 return NULL;
6269 }
6270
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006271 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04006272 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006273 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006274 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006275 return NULL;
6276 }
6277
6278 init = dlsym(module, entrypoint);
6279 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006280 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00006281 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006282 return NULL;
6283 }
6284
6285 return init;
6286}
6287
Giulio Camuffo459137b2014-10-11 23:56:24 +03006288
6289/** Destroys the compositor.
6290 *
6291 * This function cleans up the compositor state and destroys it.
6292 *
6293 * \param compositor The compositor to be destroyed.
6294 */
6295WL_EXPORT void
6296weston_compositor_destroy(struct weston_compositor *compositor)
6297{
6298 /* prevent further rendering while shutting down */
6299 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
6300
6301 wl_signal_emit(&compositor->destroy_signal, compositor);
6302
6303 weston_compositor_xkb_destroy(compositor);
6304
Giulio Camuffo2d24e642015-10-03 16:25:15 +03006305 if (compositor->backend)
6306 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006307
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006308 /* The backend is responsible for destroying the heads. */
6309 assert(wl_list_empty(&compositor->head_list));
6310
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006311 weston_plugin_api_destroy_list(compositor);
6312
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006313 if (compositor->heads_changed_source)
6314 wl_event_source_remove(compositor->heads_changed_source);
6315
Giulio Camuffo459137b2014-10-11 23:56:24 +03006316 free(compositor);
6317}
6318
6319/** Instruct the compositor to exit.
6320 *
6321 * This functions does not directly destroy the compositor object, it merely
6322 * command it to start the tear down process. It is not guaranteed that the
6323 * tear down will happen immediately.
6324 *
6325 * \param compositor The compositor to tear down.
6326 */
6327WL_EXPORT void
6328weston_compositor_exit(struct weston_compositor *compositor)
6329{
6330 compositor->exit(compositor);
6331}
6332
6333/** Return the user data stored in the compositor.
6334 *
6335 * This function returns the user data pointer set with user_data parameter
6336 * to the \ref weston_compositor_create function.
6337 */
6338WL_EXPORT void *
6339weston_compositor_get_user_data(struct weston_compositor *compositor)
6340{
6341 return compositor->user_data;
6342}
Pekka Paalanendd186732016-06-03 14:49:54 +03006343
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006344static const char * const backend_map[] = {
6345 [WESTON_BACKEND_DRM] = "drm-backend.so",
6346 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
6347 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
6348 [WESTON_BACKEND_RDP] = "rdp-backend.so",
6349 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
6350 [WESTON_BACKEND_X11] = "x11-backend.so",
6351};
6352
Pekka Paalanendd186732016-06-03 14:49:54 +03006353/** Load a backend into a weston_compositor
6354 *
6355 * A backend must be loaded to make a weston_compositor work. A backend
6356 * provides input and output capabilities, and determines the renderer to use.
6357 *
6358 * \param compositor A compositor that has not had a backend loaded yet.
6359 * \param backend Name of the backend file.
6360 * \param config_base A pointer to a backend-specific configuration
6361 * structure's 'base' member.
6362 *
6363 * \return 0 on success, or -1 on error.
6364 */
6365WL_EXPORT int
6366weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006367 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03006368 struct weston_backend_config *config_base)
6369{
6370 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03006371 struct weston_backend_config *config_base);
6372
Pekka Paalanend7e35112017-08-29 17:04:12 +03006373 if (compositor->backend) {
6374 weston_log("Error: attempt to load a backend when one is already loaded\n");
6375 return -1;
6376 }
6377
Quentin Glidic887c0182016-07-10 11:00:53 +02006378 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006379 return -1;
6380
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01006381 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03006382 if (!backend_init)
6383 return -1;
6384
Pekka Paalanend7e35112017-08-29 17:04:12 +03006385 if (backend_init(compositor, config_base) < 0) {
6386 compositor->backend = NULL;
6387 return -1;
6388 }
6389
6390 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03006391}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006392
6393WL_EXPORT int
6394weston_compositor_load_xwayland(struct weston_compositor *compositor)
6395{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006396 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006397
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006398 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006399 if (!module_init)
6400 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006401 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006402 return -1;
6403 return 0;
6404}