blob: 14fa12620d94e20bfe1e5bdb7668fea5738b1cd8 [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 Paalanendcac3512017-12-08 14:13:34 +020079static char *
80weston_output_create_heads_string(struct weston_output *output);
81
Pekka Paalanen6528c032017-03-28 15:27:10 +030082/** Send wl_output events for mode and scale changes
83 *
84 * \param head Send on all resources bound to this head.
85 * \param mode_changed If true, send the current mode.
86 * \param scale_changed If true, send the current scale.
87 */
88static void
89weston_mode_switch_send_events(struct weston_head *head,
90 bool mode_changed, bool scale_changed)
91{
92 struct weston_output *output = head->output;
93 struct wl_resource *resource;
94 int version;
95
96 wl_resource_for_each(resource, &head->resource_list) {
97 if (mode_changed) {
98 wl_output_send_mode(resource,
99 output->current_mode->flags,
100 output->current_mode->width,
101 output->current_mode->height,
102 output->current_mode->refresh);
103 }
104
105 version = wl_resource_get_version(resource);
106 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
107 wl_output_send_scale(resource, output->current_scale);
108
109 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
110 wl_output_send_done(resource);
111 }
112}
113
114static void
115weston_mode_switch_finish(struct weston_output *output,
116 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800117{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200118 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300119 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200120 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200121
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200122 pixman_region32_init(&old_output_region);
123 pixman_region32_copy(&old_output_region, &output->region);
124
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200125 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200126 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200127
128 pixman_region32_init(&output->previous_damage);
129 pixman_region32_init_rect(&output->region, output->x, output->y,
130 output->width, output->height);
131
132 weston_output_update_matrix(output);
133
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200134 /* If a pointer falls outside the outputs new geometry, move it to its
135 * lower-right corner */
136 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500137 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200138 int32_t x, y;
139
140 if (!pointer)
141 continue;
142
143 x = wl_fixed_to_int(pointer->x);
144 y = wl_fixed_to_int(pointer->y);
145
146 if (!pixman_region32_contains_point(&old_output_region,
147 x, y, NULL) ||
148 pixman_region32_contains_point(&output->region,
149 x, y, NULL))
150 continue;
151
152 if (x >= output->x + output->width)
153 x = output->x + output->width - 1;
154 if (y >= output->y + output->height)
155 y = output->y + output->height - 1;
156
157 pointer->x = wl_fixed_from_int(x);
158 pointer->y = wl_fixed_from_int(y);
159 }
160
161 pixman_region32_fini(&old_output_region);
162
Derek Foremandd4cd332014-11-10 10:29:59 -0600163 if (!mode_changed && !scale_changed)
164 return;
165
Hardening57388e42013-09-18 23:56:36 +0200166 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300167 wl_list_for_each(head, &output->head_list, output_link)
168 weston_mode_switch_send_events(head,
169 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600170}
171
David Fort0de859e2016-05-27 23:22:57 +0200172static void
173weston_compositor_reflow_outputs(struct weston_compositor *compositor,
174 struct weston_output *resized_output, int delta_width);
175
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600176WL_EXPORT int
177weston_output_mode_set_native(struct weston_output *output,
178 struct weston_mode *mode,
179 int32_t scale)
180{
181 int ret;
182 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200183 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600184
185 if (!output->switch_mode)
186 return -1;
187
188 if (!output->original_mode) {
189 mode_changed = 1;
190 ret = output->switch_mode(output, mode);
191 if (ret < 0)
192 return ret;
193 if (output->current_scale != scale) {
194 scale_changed = 1;
195 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200196 }
197 }
198
David Fort0de859e2016-05-27 23:22:57 +0200199 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600200 output->native_mode = mode;
201 output->native_scale = scale;
202
203 weston_mode_switch_finish(output, mode_changed, scale_changed);
204
David Fort0de859e2016-05-27 23:22:57 +0200205 if (mode_changed || scale_changed) {
206 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
207
208 wl_signal_emit(&output->compositor->output_resized_signal, output);
209 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600210 return 0;
211}
212
213WL_EXPORT int
214weston_output_mode_switch_to_native(struct weston_output *output)
215{
216 int ret;
217 int mode_changed = 0, scale_changed = 0;
218
219 if (!output->switch_mode)
220 return -1;
221
222 if (!output->original_mode) {
223 weston_log("already in the native mode\n");
224 return -1;
225 }
226 /* the non fullscreen clients haven't seen a mode set since we
227 * switched into a temporary, so we need to notify them if the
228 * mode at that time is different from the native mode now.
229 */
230 mode_changed = (output->original_mode != output->native_mode);
231 scale_changed = (output->original_scale != output->native_scale);
232
233 ret = output->switch_mode(output, output->native_mode);
234 if (ret < 0)
235 return ret;
236
237 output->current_scale = output->native_scale;
238
239 output->original_mode = NULL;
240 output->original_scale = 0;
241
242 weston_mode_switch_finish(output, mode_changed, scale_changed);
243
244 return 0;
245}
246
247WL_EXPORT int
248weston_output_mode_switch_to_temporary(struct weston_output *output,
249 struct weston_mode *mode,
250 int32_t scale)
251{
252 int ret;
253
254 if (!output->switch_mode)
255 return -1;
256
257 /* original_mode is the last mode non full screen clients have seen,
258 * so we shouldn't change it if we already have one set.
259 */
260 if (!output->original_mode) {
261 output->original_mode = output->native_mode;
262 output->original_scale = output->native_scale;
263 }
264 ret = output->switch_mode(output, mode);
265 if (ret < 0)
266 return ret;
267
268 output->current_scale = scale;
269
270 weston_mode_switch_finish(output, 0, 0);
271
272 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800273}
274
Benjamin Franzke06286262011-05-06 19:12:33 +0200275static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300276region_init_infinite(pixman_region32_t *region)
277{
278 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
279 UINT32_MAX, UINT32_MAX);
280}
281
Pekka Paalanene67858b2013-04-25 13:57:42 +0300282static struct weston_subsurface *
283weston_surface_to_subsurface(struct weston_surface *surface);
284
Jason Ekstranda7af7042013-10-12 22:38:11 -0500285WL_EXPORT struct weston_view *
286weston_view_create(struct weston_surface *surface)
287{
288 struct weston_view *view;
289
Bryce Harringtonde16d892014-11-20 22:21:57 -0800290 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500291 if (view == NULL)
292 return NULL;
293
294 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000295 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500296
Jason Ekstranda7af7042013-10-12 22:38:11 -0500297 /* Assign to surface */
298 wl_list_insert(&surface->views, &view->surface_link);
299
300 wl_signal_init(&view->destroy_signal);
301 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300302 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500303
Jason Ekstranda7af7042013-10-12 22:38:11 -0500304 pixman_region32_init(&view->clip);
305
306 view->alpha = 1.0;
307 pixman_region32_init(&view->transform.opaque);
308
309 wl_list_init(&view->geometry.transformation_list);
310 wl_list_insert(&view->geometry.transformation_list,
311 &view->transform.position.link);
312 weston_matrix_init(&view->transform.position.matrix);
313 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200314 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500315 pixman_region32_init(&view->transform.boundingbox);
316 view->transform.dirty = 1;
317
Jason Ekstranda7af7042013-10-12 22:38:11 -0500318 return view;
319}
320
Jason Ekstrand108865d2014-06-26 10:04:49 -0700321struct weston_frame_callback {
322 struct wl_resource *resource;
323 struct wl_list link;
324};
325
Pekka Paalanen133e4392014-09-23 22:08:46 -0400326struct weston_presentation_feedback {
327 struct wl_resource *resource;
328
329 /* XXX: could use just wl_resource_get_link() instead */
330 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200331
332 /* The per-surface feedback flags */
333 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400334};
335
336static void
337weston_presentation_feedback_discard(
338 struct weston_presentation_feedback *feedback)
339{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200340 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400341 wl_resource_destroy(feedback->resource);
342}
343
344static void
345weston_presentation_feedback_discard_list(struct wl_list *list)
346{
347 struct weston_presentation_feedback *feedback, *tmp;
348
349 wl_list_for_each_safe(feedback, tmp, list, link)
350 weston_presentation_feedback_discard(feedback);
351}
352
353static void
354weston_presentation_feedback_present(
355 struct weston_presentation_feedback *feedback,
356 struct weston_output *output,
357 uint32_t refresh_nsec,
358 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200359 uint64_t seq,
360 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400361{
362 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300363 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400364 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200365 uint32_t tv_sec_hi;
366 uint32_t tv_sec_lo;
367 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300368 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400369
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300370 wl_list_for_each(head, &output->head_list, output_link) {
371 wl_resource_for_each(o, &head->resource_list) {
372 if (wl_resource_get_client(o) != client)
373 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400374
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300375 wp_presentation_feedback_send_sync_output(feedback->resource, o);
376 done = true;
377 }
378
379 /* For clone mode, send it for just one wl_output global,
380 * they are all equivalent anyway.
381 */
382 if (done)
383 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400384 }
385
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200386 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200387 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200388 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200389 refresh_nsec,
390 seq >> 32, seq & 0xffffffff,
391 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400392 wl_resource_destroy(feedback->resource);
393}
394
395static void
396weston_presentation_feedback_present_list(struct wl_list *list,
397 struct weston_output *output,
398 uint32_t refresh_nsec,
399 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200400 uint64_t seq,
401 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400402{
403 struct weston_presentation_feedback *feedback, *tmp;
404
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200405 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200406 wl_list_empty(list));
407
Pekka Paalanen133e4392014-09-23 22:08:46 -0400408 wl_list_for_each_safe(feedback, tmp, list, link)
409 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200410 refresh_nsec, ts, seq,
411 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400412}
413
Jason Ekstrand7b982072014-05-20 14:33:03 -0500414static void
415surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
416{
417 struct weston_surface_state *state =
418 container_of(listener, struct weston_surface_state,
419 buffer_destroy_listener);
420
421 state->buffer = NULL;
422}
423
424static void
425weston_surface_state_init(struct weston_surface_state *state)
426{
427 state->newly_attached = 0;
428 state->buffer = NULL;
429 state->buffer_destroy_listener.notify =
430 surface_state_handle_buffer_destroy;
431 state->sx = 0;
432 state->sy = 0;
433
Derek Foreman152254b2015-11-26 14:17:48 -0600434 pixman_region32_init(&state->damage_surface);
435 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500436 pixman_region32_init(&state->opaque);
437 region_init_infinite(&state->input);
438
439 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400440 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500441
442 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
443 state->buffer_viewport.buffer.scale = 1;
444 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
445 state->buffer_viewport.surface.width = -1;
446 state->buffer_viewport.changed = 0;
447}
448
449static void
450weston_surface_state_fini(struct weston_surface_state *state)
451{
452 struct weston_frame_callback *cb, *next;
453
454 wl_list_for_each_safe(cb, next,
455 &state->frame_callback_list, link)
456 wl_resource_destroy(cb->resource);
457
Pekka Paalanen133e4392014-09-23 22:08:46 -0400458 weston_presentation_feedback_discard_list(&state->feedback_list);
459
Jason Ekstrand7b982072014-05-20 14:33:03 -0500460 pixman_region32_fini(&state->input);
461 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600462 pixman_region32_fini(&state->damage_surface);
463 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500464
465 if (state->buffer)
466 wl_list_remove(&state->buffer_destroy_listener.link);
467 state->buffer = NULL;
468}
469
470static void
471weston_surface_state_set_buffer(struct weston_surface_state *state,
472 struct weston_buffer *buffer)
473{
474 if (state->buffer == buffer)
475 return;
476
477 if (state->buffer)
478 wl_list_remove(&state->buffer_destroy_listener.link);
479 state->buffer = buffer;
480 if (state->buffer)
481 wl_signal_add(&state->buffer->destroy_signal,
482 &state->buffer_destroy_listener);
483}
484
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500485WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500486weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500487{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500488 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400489
Bryce Harringtonde16d892014-11-20 22:21:57 -0800490 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400491 if (surface == NULL)
492 return NULL;
493
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500494 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800495 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500496
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500497 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200498 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400499
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200500 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
501 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200502 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
503 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500504
505 weston_surface_state_init(&surface->pending);
506
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400507 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500508 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300509 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400510
Jason Ekstranda7af7042013-10-12 22:38:11 -0500511 wl_list_init(&surface->views);
512
513 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400514 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500515
Pekka Paalanene67858b2013-04-25 13:57:42 +0300516 wl_list_init(&surface->subsurface_list);
517 wl_list_init(&surface->subsurface_list_pending);
518
Jason Ekstrand1e059042014-10-16 10:55:19 -0500519 weston_matrix_init(&surface->buffer_to_surface_matrix);
520 weston_matrix_init(&surface->surface_to_buffer_matrix);
521
Jonas Ådahld3414f22016-07-22 17:56:31 +0800522 wl_list_init(&surface->pointer_constraints);
523
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400524 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500525}
526
Alex Wu8811bf92012-02-28 18:07:54 +0800527WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500528weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200529 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500530{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100531 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500532}
533
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400534WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500535weston_view_to_global_float(struct weston_view *view,
536 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200537{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500538 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200539 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
540
Jason Ekstranda7af7042013-10-12 22:38:11 -0500541 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200542
543 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200544 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700545 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200546 v.f[3]);
547 *x = 0;
548 *y = 0;
549 return;
550 }
551
552 *x = v.f[0] / v.f[3];
553 *y = v.f[1] / v.f[3];
554 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500555 *x = sx + view->geometry.x;
556 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200557 }
558}
559
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500560WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200561weston_transformed_coord(int width, int height,
562 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200563 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200564 float sx, float sy, float *bx, float *by)
565{
566 switch (transform) {
567 case WL_OUTPUT_TRANSFORM_NORMAL:
568 default:
569 *bx = sx;
570 *by = sy;
571 break;
572 case WL_OUTPUT_TRANSFORM_FLIPPED:
573 *bx = width - sx;
574 *by = sy;
575 break;
576 case WL_OUTPUT_TRANSFORM_90:
577 *bx = height - sy;
578 *by = sx;
579 break;
580 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
581 *bx = height - sy;
582 *by = width - sx;
583 break;
584 case WL_OUTPUT_TRANSFORM_180:
585 *bx = width - sx;
586 *by = height - sy;
587 break;
588 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
589 *bx = sx;
590 *by = height - sy;
591 break;
592 case WL_OUTPUT_TRANSFORM_270:
593 *bx = sy;
594 *by = width - sx;
595 break;
596 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
597 *bx = sy;
598 *by = sx;
599 break;
600 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200601
602 *bx *= scale;
603 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200604}
605
606WL_EXPORT pixman_box32_t
607weston_transformed_rect(int width, int height,
608 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200609 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200610 pixman_box32_t rect)
611{
612 float x1, x2, y1, y2;
613
614 pixman_box32_t ret;
615
Alexander Larsson4ea95522013-05-22 14:41:37 +0200616 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200617 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200618 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200619 rect.x2, rect.y2, &x2, &y2);
620
621 if (x1 <= x2) {
622 ret.x1 = x1;
623 ret.x2 = x2;
624 } else {
625 ret.x1 = x2;
626 ret.x2 = x1;
627 }
628
629 if (y1 <= y2) {
630 ret.y1 = y1;
631 ret.y2 = y2;
632 } else {
633 ret.y1 = y2;
634 ret.y2 = y1;
635 }
636
637 return ret;
638}
639
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600640/** Transform a region by a matrix, restricted to axis-aligned transformations
641 *
642 * Warning: This function does not work for projective, affine, or matrices
643 * that encode arbitrary rotations. Only 90-degree step rotations are
644 * supported.
645 */
646WL_EXPORT void
647weston_matrix_transform_region(pixman_region32_t *dest,
648 struct weston_matrix *matrix,
649 pixman_region32_t *src)
650{
651 pixman_box32_t *src_rects, *dest_rects;
652 int nrects, i;
653
654 src_rects = pixman_region32_rectangles(src, &nrects);
655 dest_rects = malloc(nrects * sizeof(*dest_rects));
656 if (!dest_rects)
657 return;
658
659 for (i = 0; i < nrects; i++) {
660 struct weston_vector vec1 = {{
661 src_rects[i].x1, src_rects[i].y1, 0, 1
662 }};
663 weston_matrix_transform(matrix, &vec1);
664 vec1.f[0] /= vec1.f[3];
665 vec1.f[1] /= vec1.f[3];
666
667 struct weston_vector vec2 = {{
668 src_rects[i].x2, src_rects[i].y2, 0, 1
669 }};
670 weston_matrix_transform(matrix, &vec2);
671 vec2.f[0] /= vec2.f[3];
672 vec2.f[1] /= vec2.f[3];
673
674 if (vec1.f[0] < vec2.f[0]) {
675 dest_rects[i].x1 = floor(vec1.f[0]);
676 dest_rects[i].x2 = ceil(vec2.f[0]);
677 } else {
678 dest_rects[i].x1 = floor(vec2.f[0]);
679 dest_rects[i].x2 = ceil(vec1.f[0]);
680 }
681
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600682 if (vec1.f[1] < vec2.f[1]) {
683 dest_rects[i].y1 = floor(vec1.f[1]);
684 dest_rects[i].y2 = ceil(vec2.f[1]);
685 } else {
686 dest_rects[i].y1 = floor(vec2.f[1]);
687 dest_rects[i].y2 = ceil(vec1.f[1]);
688 }
689 }
690
691 pixman_region32_clear(dest);
692 pixman_region32_init_rects(dest, dest_rects, nrects);
693 free(dest_rects);
694}
695
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200696WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500697weston_transformed_region(int width, int height,
698 enum wl_output_transform transform,
699 int32_t scale,
700 pixman_region32_t *src, pixman_region32_t *dest)
701{
702 pixman_box32_t *src_rects, *dest_rects;
703 int nrects, i;
704
705 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
706 if (src != dest)
707 pixman_region32_copy(dest, src);
708 return;
709 }
710
711 src_rects = pixman_region32_rectangles(src, &nrects);
712 dest_rects = malloc(nrects * sizeof(*dest_rects));
713 if (!dest_rects)
714 return;
715
716 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
717 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
718 } else {
719 for (i = 0; i < nrects; i++) {
720 switch (transform) {
721 default:
722 case WL_OUTPUT_TRANSFORM_NORMAL:
723 dest_rects[i].x1 = src_rects[i].x1;
724 dest_rects[i].y1 = src_rects[i].y1;
725 dest_rects[i].x2 = src_rects[i].x2;
726 dest_rects[i].y2 = src_rects[i].y2;
727 break;
728 case WL_OUTPUT_TRANSFORM_90:
729 dest_rects[i].x1 = height - src_rects[i].y2;
730 dest_rects[i].y1 = src_rects[i].x1;
731 dest_rects[i].x2 = height - src_rects[i].y1;
732 dest_rects[i].y2 = src_rects[i].x2;
733 break;
734 case WL_OUTPUT_TRANSFORM_180:
735 dest_rects[i].x1 = width - src_rects[i].x2;
736 dest_rects[i].y1 = height - src_rects[i].y2;
737 dest_rects[i].x2 = width - src_rects[i].x1;
738 dest_rects[i].y2 = height - src_rects[i].y1;
739 break;
740 case WL_OUTPUT_TRANSFORM_270:
741 dest_rects[i].x1 = src_rects[i].y1;
742 dest_rects[i].y1 = width - src_rects[i].x2;
743 dest_rects[i].x2 = src_rects[i].y2;
744 dest_rects[i].y2 = width - src_rects[i].x1;
745 break;
746 case WL_OUTPUT_TRANSFORM_FLIPPED:
747 dest_rects[i].x1 = width - src_rects[i].x2;
748 dest_rects[i].y1 = src_rects[i].y1;
749 dest_rects[i].x2 = width - src_rects[i].x1;
750 dest_rects[i].y2 = src_rects[i].y2;
751 break;
752 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
753 dest_rects[i].x1 = height - src_rects[i].y2;
754 dest_rects[i].y1 = width - src_rects[i].x2;
755 dest_rects[i].x2 = height - src_rects[i].y1;
756 dest_rects[i].y2 = width - src_rects[i].x1;
757 break;
758 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
759 dest_rects[i].x1 = src_rects[i].x1;
760 dest_rects[i].y1 = height - src_rects[i].y2;
761 dest_rects[i].x2 = src_rects[i].x2;
762 dest_rects[i].y2 = height - src_rects[i].y1;
763 break;
764 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
765 dest_rects[i].x1 = src_rects[i].y1;
766 dest_rects[i].y1 = src_rects[i].x1;
767 dest_rects[i].x2 = src_rects[i].y2;
768 dest_rects[i].y2 = src_rects[i].x2;
769 break;
770 }
771 }
772 }
773
774 if (scale != 1) {
775 for (i = 0; i < nrects; i++) {
776 dest_rects[i].x1 *= scale;
777 dest_rects[i].x2 *= scale;
778 dest_rects[i].y1 *= scale;
779 dest_rects[i].y2 *= scale;
780 }
781 }
782
783 pixman_region32_clear(dest);
784 pixman_region32_init_rects(dest, dest_rects, nrects);
785 free(dest_rects);
786}
787
Jonny Lamb74130762013-11-26 18:19:46 +0100788static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300789viewport_surface_to_buffer(struct weston_surface *surface,
790 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100791{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200792 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200793 double src_width, src_height;
794 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200795
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200796 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
797 if (vp->surface.width == -1) {
798 *bx = sx;
799 *by = sy;
800 return;
801 }
Jonny Lamb74130762013-11-26 18:19:46 +0100802
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200803 src_x = 0.0;
804 src_y = 0.0;
805 src_width = surface->width_from_buffer;
806 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100807 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200808 src_x = wl_fixed_to_double(vp->buffer.src_x);
809 src_y = wl_fixed_to_double(vp->buffer.src_y);
810 src_width = wl_fixed_to_double(vp->buffer.src_width);
811 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100812 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200813
814 *bx = sx * src_width / surface->width + src_x;
815 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100816}
817
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500818WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200819weston_surface_to_buffer_float(struct weston_surface *surface,
820 float sx, float sy, float *bx, float *by)
821{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200822 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
823
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300824 /* first transform coordinates if the viewport is set */
825 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100826
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500827 weston_transformed_coord(surface->width_from_buffer,
828 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200829 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100830 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200831}
832
Derek Foreman99739672015-12-03 16:38:11 -0600833/** Transform a rectangle from surface coordinates to buffer coordinates
834 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300835 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600836 * from.
837 * \param rect The rectangle to transform.
838 * \return The transformed rectangle.
839 *
840 * Viewport and buffer transformations can only do translation, scaling,
841 * and rotations in 90-degree steps. Therefore the only loss in the
842 * conversion is coordinate rounding.
843 *
844 * However, some coordinate rounding takes place as an intermediate
845 * step before the buffer scale factor is applied, so the rectangle
846 * boundary may not be exactly as expected.
847 *
848 * This is OK for damage tracking since a little extra coverage is
849 * not a problem.
850 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200851WL_EXPORT pixman_box32_t
852weston_surface_to_buffer_rect(struct weston_surface *surface,
853 pixman_box32_t rect)
854{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200855 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100856 float xf, yf;
857
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300858 /* first transform box coordinates if the viewport is set */
859 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100860 rect.x1 = floorf(xf);
861 rect.y1 = floorf(yf);
862
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300863 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600864 rect.x2 = ceilf(xf);
865 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100866
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500867 return weston_transformed_rect(surface->width_from_buffer,
868 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200869 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200870 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200871}
872
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200873/** Transform a region from surface coordinates to buffer coordinates
874 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300875 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200876 * from.
877 * \param surface_region[in] The region in surface coordinates.
878 * \param buffer_region[out] The region converted to buffer coordinates.
879 *
880 * Buffer_region must be init'd, but will be completely overwritten.
881 *
882 * Viewport and buffer transformations can only do translation, scaling,
883 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600884 * conversion is from the coordinate rounding that takes place in
885 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200886 */
887WL_EXPORT void
888weston_surface_to_buffer_region(struct weston_surface *surface,
889 pixman_region32_t *surface_region,
890 pixman_region32_t *buffer_region)
891{
892 pixman_box32_t *src_rects, *dest_rects;
893 int nrects, i;
894
895 src_rects = pixman_region32_rectangles(surface_region, &nrects);
896 dest_rects = malloc(nrects * sizeof(*dest_rects));
897 if (!dest_rects)
898 return;
899
900 for (i = 0; i < nrects; i++) {
901 dest_rects[i] = weston_surface_to_buffer_rect(surface,
902 src_rects[i]);
903 }
904
905 pixman_region32_fini(buffer_region);
906 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
907 free(dest_rects);
908}
909
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200910WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400912 struct weston_plane *plane)
913{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500914 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400915 return;
916
Jason Ekstranda7af7042013-10-12 22:38:11 -0500917 weston_view_damage_below(view);
918 view->plane = plane;
919 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400920}
921
Pekka Paalanen51723d52015-02-17 13:10:01 +0200922/** Inflict damage on the plane where the view is visible.
923 *
924 * \param view The view that causes the damage.
925 *
926 * If the view is currently on a plane (including the primary plane),
927 * take the view's boundingbox, subtract all the opaque views that cover it,
928 * and add the remaining region as damage to the plane. This corresponds
929 * to the damage inflicted to the plane if this view disappeared.
930 *
931 * A repaint is scheduled for this view.
932 *
933 * The region of all opaque views covering this view is stored in
934 * weston_view::clip and updated by view_accumulate_damage() during
935 * weston_output_repaint(). Specifically, that region matches the
936 * scenegraph as it was last painted.
937 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400938WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200940{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500941 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200942
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500943 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200944 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500945 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800946 if (view->plane)
947 pixman_region32_union(&view->plane->damage,
948 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500949 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800950 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200951}
952
Pekka Paalanen01e00682017-03-24 16:21:06 +0200953/** Send wl_surface.enter/leave events
954 *
955 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300956 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200957 * \param enter True if entered.
958 * \param left True if left.
959 *
960 * Send the enter/leave events for all protocol objects bound to the given
961 * output by the client owning the surface.
962 */
963static void
964weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300965 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +0200966 bool enter,
967 bool leave)
968{
969 struct wl_resource *wloutput;
970 struct wl_client *client;
971
972 assert(enter != leave);
973
974 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300975 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +0200976 if (wl_resource_get_client(wloutput) != client)
977 continue;
978
979 if (enter)
980 wl_surface_send_enter(surface->resource, wloutput);
981 if (leave)
982 wl_surface_send_leave(surface->resource, wloutput);
983 }
984}
985
Bryce Harrington3f650b82015-12-23 11:01:58 -0800986/**
987 * \param es The surface
988 * \param mask The new set of outputs for the surface
989 *
990 * Sets the surface's set of outputs to the ones specified by
991 * the new output mask provided. Identifies the outputs that
992 * have changed, the posts enter and leave events for these
993 * outputs as appropriate.
994 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400995static void
996weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
997{
998 uint32_t different = es->output_mask ^ mask;
999 uint32_t entered = mask & different;
1000 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001001 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001002 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001003 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001004
1005 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001006 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001007 return;
1008 if (different == 0)
1009 return;
1010
1011 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001012 output_bit = 1u << output->id;
1013 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001014 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001015
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001016 wl_list_for_each(head, &output->head_list, output_link) {
1017 weston_surface_send_enter_leave(es, head,
1018 output_bit & entered,
1019 output_bit & left);
1020 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001021 }
1022}
1023
Bryce Harrington3f650b82015-12-23 11:01:58 -08001024/** Recalculate which output(s) the surface has views displayed on
1025 *
1026 * \param es The surface to remap to outputs
1027 *
1028 * Finds the output that is showing the largest amount of one
1029 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001030 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001031 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001032 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001033 * in the output_mask for the surface.
1034 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001035static void
1036weston_surface_assign_output(struct weston_surface *es)
1037{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 struct weston_output *new_output;
1039 struct weston_view *view;
1040 pixman_region32_t region;
1041 uint32_t max, area, mask;
1042 pixman_box32_t *e;
1043
1044 new_output = NULL;
1045 max = 0;
1046 mask = 0;
1047 pixman_region32_init(&region);
1048 wl_list_for_each(view, &es->views, surface_link) {
1049 if (!view->output)
1050 continue;
1051
1052 pixman_region32_intersect(&region, &view->transform.boundingbox,
1053 &view->output->region);
1054
1055 e = pixman_region32_extents(&region);
1056 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1057
1058 mask |= view->output_mask;
1059
1060 if (area >= max) {
1061 new_output = view->output;
1062 max = area;
1063 }
1064 }
1065 pixman_region32_fini(&region);
1066
1067 es->output = new_output;
1068 weston_surface_update_output_mask(es, mask);
1069}
1070
Bryce Harrington3f650b82015-12-23 11:01:58 -08001071/** Recalculate which output(s) the view is displayed on
1072 *
1073 * \param ev The view to remap to outputs
1074 *
1075 * Identifies the set of outputs that the view is visible on,
1076 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001077 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001078 *
1079 * Also does the same for the view's surface. See
1080 * weston_surface_assign_output().
1081 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082static void
1083weston_view_assign_output(struct weston_view *ev)
1084{
1085 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001086 struct weston_output *output, *new_output;
1087 pixman_region32_t region;
1088 uint32_t max, area, mask;
1089 pixman_box32_t *e;
1090
1091 new_output = NULL;
1092 max = 0;
1093 mask = 0;
1094 pixman_region32_init(&region);
1095 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001096 if (output->destroying)
1097 continue;
1098
Jason Ekstranda7af7042013-10-12 22:38:11 -05001099 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001100 &output->region);
1101
1102 e = pixman_region32_extents(&region);
1103 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1104
1105 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001106 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001107
1108 if (area >= max) {
1109 new_output = output;
1110 max = area;
1111 }
1112 }
1113 pixman_region32_fini(&region);
1114
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 ev->output = new_output;
1116 ev->output_mask = mask;
1117
1118 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001119}
1120
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001121static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001122weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1123 int from_x, int from_y, int *to_x, int *to_y)
1124{
1125 float x, y;
1126
1127 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1128 weston_view_from_global_float(to, x, y, &x, &y);
1129
1130 *to_x = round(x);
1131 *to_y = round(y);
1132}
1133
1134static void
1135weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1136{
1137 pixman_box32_t *a;
1138 pixman_box32_t b;
1139
1140 a = pixman_region32_extents(&from->geometry.scissor);
1141
1142 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1143 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1144
1145 pixman_region32_fini(&to->geometry.scissor);
1146 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1147}
1148
1149static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001150view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001152{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001153 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1154 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001155 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001156 { inbox->x1, inbox->y1 },
1157 { inbox->x1, inbox->y2 },
1158 { inbox->x2, inbox->y1 },
1159 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001160 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001161 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001162 int i;
1163
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001164 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001165 /* avoid rounding empty bbox to 1x1 */
1166 pixman_region32_init(bbox);
1167 return;
1168 }
1169
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001170 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001171 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001173 if (x < min_x)
1174 min_x = x;
1175 if (x > max_x)
1176 max_x = x;
1177 if (y < min_y)
1178 min_y = y;
1179 if (y > max_y)
1180 max_y = y;
1181 }
1182
Pekka Paalanen219b9822012-02-08 15:38:37 +02001183 int_x = floorf(min_x);
1184 int_y = floorf(min_y);
1185 pixman_region32_init_rect(bbox, int_x, int_y,
1186 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001187}
1188
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001189static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001191{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001193
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001194 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001195 view->geometry.x = roundf(view->geometry.x);
1196 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001197
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001198 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1200 view->transform.position.matrix.d[12] = view->geometry.x;
1201 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001204
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 view->transform.inverse = view->transform.position.matrix;
1206 view->transform.inverse.d[12] = -view->geometry.x;
1207 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001208
Jason Ekstranda7af7042013-10-12 22:38:11 -05001209 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001210 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001211 view->surface->width,
1212 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001213 if (view->geometry.scissor_enabled)
1214 pixman_region32_intersect(&view->transform.boundingbox,
1215 &view->transform.boundingbox,
1216 &view->geometry.scissor);
1217
1218 pixman_region32_translate(&view->transform.boundingbox,
1219 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001220
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 if (view->alpha == 1.0) {
1222 pixman_region32_copy(&view->transform.opaque,
1223 &view->surface->opaque);
1224 pixman_region32_translate(&view->transform.opaque,
1225 view->geometry.x,
1226 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001227 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001228}
1229
1230static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001232{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 struct weston_view *parent = view->geometry.parent;
1234 struct weston_matrix *matrix = &view->transform.matrix;
1235 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001236 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001237 pixman_region32_t surfregion;
1238 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001239
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001241
1242 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1244 view->transform.position.matrix.d[12] = view->geometry.x;
1245 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001246
1247 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001249 weston_matrix_multiply(matrix, &tform->matrix);
1250
Pekka Paalanen483243f2013-03-08 14:56:50 +02001251 if (parent)
1252 weston_matrix_multiply(matrix, &parent->transform.matrix);
1253
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001254 if (weston_matrix_invert(inverse, matrix) < 0) {
1255 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 weston_log("error: weston_view %p"
1257 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001258 return -1;
1259 }
1260
Giulio Camuffo148c1992016-09-04 18:50:46 +03001261 if (view->alpha == 1.0 &&
1262 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1263 pixman_region32_copy(&view->transform.opaque,
1264 &view->surface->opaque);
1265 pixman_region32_translate(&view->transform.opaque,
1266 matrix->d[12],
1267 matrix->d[13]);
1268 }
1269
Pekka Paalanen380adf52015-02-16 14:39:11 +02001270 pixman_region32_init_rect(&surfregion, 0, 0,
1271 view->surface->width, view->surface->height);
1272 if (view->geometry.scissor_enabled)
1273 pixman_region32_intersect(&surfregion, &surfregion,
1274 &view->geometry.scissor);
1275 surfbox = pixman_region32_extents(&surfregion);
1276
1277 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1278 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001279
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001280 return 0;
1281}
1282
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001283static struct weston_layer *
1284get_view_layer(struct weston_view *view)
1285{
1286 if (view->parent_view)
1287 return get_view_layer(view->parent_view);
1288 return view->layer_link.layer;
1289}
1290
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001291WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001293{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001295 struct weston_layer *layer;
1296 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001297
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001299 return;
1300
Pekka Paalanen483243f2013-03-08 14:56:50 +02001301 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001303
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001305
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001307
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 pixman_region32_fini(&view->transform.boundingbox);
1309 pixman_region32_fini(&view->transform.opaque);
1310 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001311
Pekka Paalanencd403622012-01-25 13:37:39 +02001312 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313 if (view->geometry.transformation_list.next ==
1314 &view->transform.position.link &&
1315 view->geometry.transformation_list.prev ==
1316 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001317 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001319 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 if (weston_view_update_transform_enable(view) < 0)
1321 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001322 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001323
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001324 layer = get_view_layer(view);
1325 if (layer) {
1326 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001327 pixman_region32_intersect(&view->transform.boundingbox,
1328 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001329 pixman_region32_intersect(&view->transform.opaque,
1330 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001331 pixman_region32_fini(&mask);
1332 }
1333
Pekka Paalanen380adf52015-02-16 14:39:11 +02001334 if (parent) {
1335 if (parent->geometry.scissor_enabled) {
1336 view->geometry.scissor_enabled = true;
1337 weston_view_transfer_scissor(parent, view);
1338 } else {
1339 view->geometry.scissor_enabled = false;
1340 }
1341 }
1342
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001344
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001346
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 wl_signal_emit(&view->surface->compositor->transform_signal,
1348 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001349}
1350
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001351WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001352weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001353{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001355
1356 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 * The invariant: if view->geometry.dirty, then all views
1358 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001359 * Corollary: if not parent->geometry.dirty, then all ancestors
1360 * are not dirty.
1361 */
1362
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001364 return;
1365
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001367
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001369 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001370 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001371}
1372
1373WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374weston_view_to_global_fixed(struct weston_view *view,
1375 wl_fixed_t vx, wl_fixed_t vy,
1376 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001377{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001378 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001379
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380 weston_view_to_global_float(view,
1381 wl_fixed_to_double(vx),
1382 wl_fixed_to_double(vy),
1383 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001384 *x = wl_fixed_from_double(xf);
1385 *y = wl_fixed_from_double(yf);
1386}
1387
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001388WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389weston_view_from_global_float(struct weston_view *view,
1390 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001391{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001393 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1394
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001396
1397 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001398 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001400 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 *vx = 0;
1402 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001403 return;
1404 }
1405
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 *vx = v.f[0] / v.f[3];
1407 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001408 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 *vx = x - view->geometry.x;
1410 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001411 }
1412}
1413
1414WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415weston_view_from_global_fixed(struct weston_view *view,
1416 wl_fixed_t x, wl_fixed_t y,
1417 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001418{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001420
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 weston_view_from_global_float(view,
1422 wl_fixed_to_double(x),
1423 wl_fixed_to_double(y),
1424 &vxf, &vyf);
1425 *vx = wl_fixed_from_double(vxf);
1426 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001427}
1428
1429WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001430weston_view_from_global(struct weston_view *view,
1431 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001432{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001434
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1436 *vx = floorf(vxf);
1437 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001438}
1439
Bryce Harrington3f650b82015-12-23 11:01:58 -08001440/**
1441 * \param surface The surface to be repainted
1442 *
1443 * Marks the output(s) that the surface is shown on as needing to be
1444 * repainted. See weston_output_schedule_repaint().
1445 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001446WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001447weston_surface_schedule_repaint(struct weston_surface *surface)
1448{
1449 struct weston_output *output;
1450
1451 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001452 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001453 weston_output_schedule_repaint(output);
1454}
1455
Bryce Harrington3f650b82015-12-23 11:01:58 -08001456/**
1457 * \param view The view to be repainted
1458 *
1459 * Marks the output(s) that the view is shown on as needing to be
1460 * repainted. See weston_output_schedule_repaint().
1461 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001462WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463weston_view_schedule_repaint(struct weston_view *view)
1464{
1465 struct weston_output *output;
1466
1467 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001468 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469 weston_output_schedule_repaint(output);
1470}
1471
Pekka Paalanene508ce62015-02-19 13:59:55 +02001472/**
1473 * XXX: This function does it the wrong way.
1474 * surface->damage is the damage from the client, and causes
1475 * surface_flush_damage() to copy pixels. No window management action can
1476 * cause damage to the client-provided content, warranting re-upload!
1477 *
1478 * Instead of surface->damage, this function should record the damage
1479 * with all the views for this surface to avoid extraneous texture
1480 * uploads.
1481 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001482WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001483weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001484{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001485 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486 0, 0, surface->width,
1487 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001488
Kristian Høgsberg98238702012-08-03 16:29:12 -04001489 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001490}
1491
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001492WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001493weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001494{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001495 if (view->geometry.x == x && view->geometry.y == y)
1496 return;
1497
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 view->geometry.x = x;
1499 view->geometry.y = y;
1500 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001501}
1502
Pekka Paalanen483243f2013-03-08 14:56:50 +02001503static void
1504transform_parent_handle_parent_destroy(struct wl_listener *listener,
1505 void *data)
1506{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 struct weston_view *view =
1508 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001509 geometry.parent_destroy_listener);
1510
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001512}
1513
1514WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001516 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001517{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518 if (view->geometry.parent) {
1519 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1520 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001521
1522 if (!parent)
1523 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001524 }
1525
Jason Ekstranda7af7042013-10-12 22:38:11 -05001526 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001527
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001529 transform_parent_handle_parent_destroy;
1530 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001531 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001532 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001533 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001535 }
1536
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 weston_view_geometry_dirty(view);
1538}
1539
Pekka Paalanen380adf52015-02-16 14:39:11 +02001540/** Set a clip mask rectangle on a view
1541 *
1542 * \param view The view to set the clip mask on.
1543 * \param x Top-left corner X coordinate of the clip rectangle.
1544 * \param y Top-left corner Y coordinate of the clip rectangle.
1545 * \param width Width of the clip rectangle, non-negative.
1546 * \param height Height of the clip rectangle, non-negative.
1547 *
1548 * A shell may set a clip mask rectangle on a view. Everything outside
1549 * the rectangle is cut away for input and output purposes: it is
1550 * not drawn and cannot be hit by hit-test based input like pointer
1551 * motion or touch-downs. Everything inside the rectangle will behave
1552 * normally. Clients are unaware of clipping.
1553 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001554 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001555 * mask rectangle does not affect the view position, the view is positioned
1556 * as it would be without a clip. The clip also does not change
1557 * weston_surface::width,height.
1558 *
1559 * The clip mask rectangle is part of transformation inheritance
1560 * (weston_view_set_transform_parent()). A clip set in the root of the
1561 * transformation inheritance tree will affect all views in the tree.
1562 * A clip can be set only on the root view. Attempting to set a clip
1563 * on view that has a transformation parent will fail. Assigning a parent
1564 * to a view that has a clip set will cause the clip to be forgotten.
1565 *
1566 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1567 * on the additional transformations in the child views. These transformations
1568 * may not rotate the coordinate axes, i.e., only translation and scaling
1569 * are allowed. Violating this restriction causes the clipping to malfunction.
1570 * Furthermore, using scaling may cause rounding errors in child clipping.
1571 *
1572 * The clip mask rectangle is not automatically adjusted based on
1573 * wl_surface.attach dx and dy arguments.
1574 *
1575 * A clip mask rectangle can be set only if the compositor capability
1576 * WESTON_CAP_VIEW_CLIP_MASK is present.
1577 *
1578 * This function sets the clip mask rectangle and schedules a repaint for
1579 * the view.
1580 */
1581WL_EXPORT void
1582weston_view_set_mask(struct weston_view *view,
1583 int x, int y, int width, int height)
1584{
1585 struct weston_compositor *compositor = view->surface->compositor;
1586
1587 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1588 weston_log("%s not allowed without capability!\n", __func__);
1589 return;
1590 }
1591
1592 if (view->geometry.parent) {
1593 weston_log("view %p has a parent, clip forbidden!\n", view);
1594 return;
1595 }
1596
1597 if (width < 0 || height < 0) {
1598 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1599 x, y, width, height);
1600 return;
1601 }
1602
1603 pixman_region32_fini(&view->geometry.scissor);
1604 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1605 view->geometry.scissor_enabled = true;
1606 weston_view_geometry_dirty(view);
1607 weston_view_schedule_repaint(view);
1608}
1609
1610/** Remove the clip mask from a view
1611 *
1612 * \param view The view to remove the clip mask from.
1613 *
1614 * Removed the clip mask rectangle and schedules a repaint.
1615 *
1616 * \sa weston_view_set_mask
1617 */
1618WL_EXPORT void
1619weston_view_set_mask_infinite(struct weston_view *view)
1620{
1621 view->geometry.scissor_enabled = false;
1622 weston_view_geometry_dirty(view);
1623 weston_view_schedule_repaint(view);
1624}
1625
Armin Krezović0da12b82016-06-30 06:04:33 +02001626/* Check if view should be displayed
1627 *
1628 * The indicator is set manually when assigning
1629 * a view to a surface.
1630 *
1631 * This needs reworking. See the thread starting at:
1632 *
1633 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1634 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001635WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001636weston_view_is_mapped(struct weston_view *view)
1637{
Armin Krezović0da12b82016-06-30 06:04:33 +02001638 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001639}
1640
Armin Krezović0da12b82016-06-30 06:04:33 +02001641/* Check if a surface has a view assigned to it
1642 *
1643 * The indicator is set manually when mapping
1644 * a surface and creating a view for it.
1645 *
1646 * This needs to go. See the thread starting at:
1647 *
1648 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1649 *
1650 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001651WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001652weston_surface_is_mapped(struct weston_surface *surface)
1653{
Armin Krezović0da12b82016-06-30 06:04:33 +02001654 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001655}
1656
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001657static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001658surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001659{
1660 struct weston_view *view;
1661
1662 if (surface->width == width && surface->height == height)
1663 return;
1664
1665 surface->width = width;
1666 surface->height = height;
1667
1668 wl_list_for_each(view, &surface->views, surface_link)
1669 weston_view_geometry_dirty(view);
1670}
1671
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001672WL_EXPORT void
1673weston_surface_set_size(struct weston_surface *surface,
1674 int32_t width, int32_t height)
1675{
1676 assert(!surface->resource);
1677 surface_set_size(surface, width, height);
1678}
1679
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001680static int
1681fixed_round_up_to_int(wl_fixed_t f)
1682{
1683 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1684}
1685
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001686static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001687convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1688 int32_t width, int32_t height,
1689 uint32_t transform,
1690 int32_t scale)
1691{
1692 assert(scale > 0);
1693
1694 switch (transform) {
1695 case WL_OUTPUT_TRANSFORM_NORMAL:
1696 case WL_OUTPUT_TRANSFORM_180:
1697 case WL_OUTPUT_TRANSFORM_FLIPPED:
1698 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1699 *width_out = width / scale;
1700 *height_out = height / scale;
1701 break;
1702 case WL_OUTPUT_TRANSFORM_90:
1703 case WL_OUTPUT_TRANSFORM_270:
1704 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1705 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1706 *width_out = height / scale;
1707 *height_out = width / scale;
1708 break;
1709 default:
1710 assert(0 && "invalid transform");
1711 }
1712}
1713
1714static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001715weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001716{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001717 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001718
1719 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001720 surface->width_from_buffer = 0;
1721 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001722 return;
1723 }
1724
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001725 convert_size_by_transform_scale(&surface->width_from_buffer,
1726 &surface->height_from_buffer,
1727 surface->buffer_ref.buffer->width,
1728 surface->buffer_ref.buffer->height,
1729 vp->buffer.transform,
1730 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001731}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001732
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001733static void
1734weston_surface_update_size(struct weston_surface *surface)
1735{
1736 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1737 int32_t width, height;
1738
1739 width = surface->width_from_buffer;
1740 height = surface->height_from_buffer;
1741
1742 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001743 surface_set_size(surface,
1744 vp->surface.width, vp->surface.height);
1745 return;
1746 }
1747
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001748 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001749 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1750 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1751
1752 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001753 return;
1754 }
1755
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001756 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001757}
1758
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001759WL_EXPORT void
1760weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001761{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001762 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001763}
1764
Jason Ekstranda7af7042013-10-12 22:38:11 -05001765WL_EXPORT struct weston_view *
1766weston_compositor_pick_view(struct weston_compositor *compositor,
1767 wl_fixed_t x, wl_fixed_t y,
1768 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001769{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001771 wl_fixed_t view_x, view_y;
1772 int view_ix, view_iy;
1773 int ix = wl_fixed_to_int(x);
1774 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001775
Jason Ekstranda7af7042013-10-12 22:38:11 -05001776 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001777 if (!pixman_region32_contains_point(
1778 &view->transform.boundingbox, ix, iy, NULL))
1779 continue;
1780
1781 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1782 view_ix = wl_fixed_to_int(view_x);
1783 view_iy = wl_fixed_to_int(view_y);
1784
1785 if (!pixman_region32_contains_point(&view->surface->input,
1786 view_ix, view_iy, NULL))
1787 continue;
1788
Pekka Paalanen380adf52015-02-16 14:39:11 +02001789 if (view->geometry.scissor_enabled &&
1790 !pixman_region32_contains_point(&view->geometry.scissor,
1791 view_ix, view_iy, NULL))
1792 continue;
1793
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001794 *vx = view_x;
1795 *vy = view_y;
1796 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001797 }
1798
Derek Foremanf9318d12015-05-11 15:40:11 -05001799 *vx = wl_fixed_from_int(-1000000);
1800 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001801 return NULL;
1802}
1803
1804static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001805weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001806{
Daniel Stone37816df2012-05-16 18:45:18 +01001807 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001808
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001809 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001810 return;
1811
Daniel Stone37816df2012-05-16 18:45:18 +01001812 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001813 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001814}
1815
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001816WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001817weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001818{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001819 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 if (!weston_view_is_mapped(view))
1822 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001823
Jason Ekstranda7af7042013-10-12 22:38:11 -05001824 weston_view_damage_below(view);
1825 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001826 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001827 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001828 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 wl_list_remove(&view->link);
1830 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001831 view->output_mask = 0;
1832 weston_surface_assign_output(view->surface);
1833
1834 if (weston_surface_is_mapped(view->surface))
1835 return;
1836
1837 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001838 struct weston_touch *touch = weston_seat_get_touch(seat);
1839 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1840 struct weston_keyboard *keyboard =
1841 weston_seat_get_keyboard(seat);
1842
1843 if (keyboard && keyboard->focus == view->surface)
1844 weston_keyboard_set_focus(keyboard, NULL);
1845 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001846 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001847 if (touch && touch->focus == view)
1848 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001849 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001850}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001851
Jason Ekstranda7af7042013-10-12 22:38:11 -05001852WL_EXPORT void
1853weston_surface_unmap(struct weston_surface *surface)
1854{
1855 struct weston_view *view;
1856
Armin Krezovićf8486c32016-06-30 06:04:28 +02001857 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858 wl_list_for_each(view, &surface->views, surface_link)
1859 weston_view_unmap(view);
1860 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001861}
1862
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001863static void
1864weston_surface_reset_pending_buffer(struct weston_surface *surface)
1865{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001866 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001867 surface->pending.sx = 0;
1868 surface->pending.sy = 0;
1869 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001870 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001871}
1872
Jason Ekstranda7af7042013-10-12 22:38:11 -05001873WL_EXPORT void
1874weston_view_destroy(struct weston_view *view)
1875{
1876 wl_signal_emit(&view->destroy_signal, view);
1877
1878 assert(wl_list_empty(&view->geometry.child_list));
1879
1880 if (weston_view_is_mapped(view)) {
1881 weston_view_unmap(view);
1882 weston_compositor_build_view_list(view->surface->compositor);
1883 }
1884
1885 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001886 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001887
1888 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001889 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001890 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001891 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001892
1893 weston_view_set_transform_parent(view, NULL);
1894
Jason Ekstranda7af7042013-10-12 22:38:11 -05001895 wl_list_remove(&view->surface_link);
1896
1897 free(view);
1898}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001899
1900WL_EXPORT void
1901weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001902{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001903 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001904 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001905 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001906
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001907 if (--surface->ref_count > 0)
1908 return;
1909
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001910 assert(surface->resource == NULL);
1911
Pekka Paalanenca790762015-04-17 14:23:38 +03001912 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001913
Pekka Paalanene67858b2013-04-25 13:57:42 +03001914 assert(wl_list_empty(&surface->subsurface_list_pending));
1915 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001916
Jason Ekstranda7af7042013-10-12 22:38:11 -05001917 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1918 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001919
Jason Ekstrand7b982072014-05-20 14:33:03 -05001920 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001921
Pekka Paalanende685b82012-12-04 15:58:12 +02001922 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001923
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001924 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001925 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001926 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001927
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001928 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001929 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001930
Pekka Paalanen133e4392014-09-23 22:08:46 -04001931 weston_presentation_feedback_discard_list(&surface->feedback_list);
1932
Jonas Ådahld3414f22016-07-22 17:56:31 +08001933 wl_list_for_each_safe(constraint, next_constraint,
1934 &surface->pointer_constraints,
1935 link)
1936 weston_pointer_constraint_destroy(constraint);
1937
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001938 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001939}
1940
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001941static void
1942destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001943{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001944 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001945
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001946 assert(surface);
1947
Giulio Camuffo0d379742013-11-15 22:06:15 +01001948 /* Set the resource to NULL, since we don't want to leave a
1949 * dangling pointer if the surface was refcounted and survives
1950 * the weston_surface_destroy() call. */
1951 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001952
1953 if (surface->viewport_resource)
1954 wl_resource_set_user_data(surface->viewport_resource, NULL);
1955
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001956 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001957}
1958
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001959static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001960weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1961{
1962 struct weston_buffer *buffer =
1963 container_of(listener, struct weston_buffer, destroy_listener);
1964
1965 wl_signal_emit(&buffer->destroy_signal, buffer);
1966 free(buffer);
1967}
1968
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001969WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001970weston_buffer_from_resource(struct wl_resource *resource)
1971{
1972 struct weston_buffer *buffer;
1973 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001974
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001975 listener = wl_resource_get_destroy_listener(resource,
1976 weston_buffer_destroy_handler);
1977
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001978 if (listener)
1979 return container_of(listener, struct weston_buffer,
1980 destroy_listener);
1981
1982 buffer = zalloc(sizeof *buffer);
1983 if (buffer == NULL)
1984 return NULL;
1985
1986 buffer->resource = resource;
1987 wl_signal_init(&buffer->destroy_signal);
1988 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001989 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001990 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001991
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001992 return buffer;
1993}
1994
1995static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001996weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1997 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001998{
Pekka Paalanende685b82012-12-04 15:58:12 +02001999 struct weston_buffer_reference *ref =
2000 container_of(listener, struct weston_buffer_reference,
2001 destroy_listener);
2002
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002003 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002004 ref->buffer = NULL;
2005}
2006
2007WL_EXPORT void
2008weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002009 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002010{
2011 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002012 ref->buffer->busy_count--;
2013 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002014 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002015 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002016 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002017 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002018 }
2019
Pekka Paalanende685b82012-12-04 15:58:12 +02002020 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002021 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002022 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002023 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002024 }
2025
Pekka Paalanende685b82012-12-04 15:58:12 +02002026 ref->buffer = buffer;
2027 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2028}
2029
2030static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002031weston_surface_attach(struct weston_surface *surface,
2032 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002033{
2034 weston_buffer_reference(&surface->buffer_ref, buffer);
2035
Pekka Paalanena6421c42012-12-04 15:58:10 +02002036 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002037 if (weston_surface_is_mapped(surface))
2038 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002039 }
2040
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002041 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002042
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002043 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002044 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002045}
2046
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002047WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002048weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002049{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002050 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002051
2052 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002053 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002054}
2055
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002056WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002057weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002058{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002059 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002060
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002061 pixman_region32_union(&compositor->primary_plane.damage,
2062 &compositor->primary_plane.damage,
2063 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002064 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002065}
2066
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002067static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002068surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002069{
Pekka Paalanende685b82012-12-04 15:58:12 +02002070 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002071 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002072 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002073
Pekka Paalanenb5026542014-11-12 15:09:24 +02002074 if (weston_timeline_enabled_ &&
2075 pixman_region32_not_empty(&surface->damage))
2076 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2077 TLP_OUTPUT(surface->output), TLP_END);
2078
Jason Ekstrandef540082014-06-26 10:37:36 -07002079 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002080}
2081
2082static void
2083view_accumulate_damage(struct weston_view *view,
2084 pixman_region32_t *opaque)
2085{
2086 pixman_region32_t damage;
2087
2088 pixman_region32_init(&damage);
2089 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002090 pixman_box32_t *extents;
2091
Jason Ekstranda7af7042013-10-12 22:38:11 -05002092 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002093 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002094 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002095 pixman_region32_copy(&damage, &view->surface->damage);
2096 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002097 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002098 }
2099
Pekka Paalanen380adf52015-02-16 14:39:11 +02002100 pixman_region32_intersect(&damage, &damage,
2101 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002102 pixman_region32_subtract(&damage, &damage, opaque);
2103 pixman_region32_union(&view->plane->damage,
2104 &view->plane->damage, &damage);
2105 pixman_region32_fini(&damage);
2106 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002107 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002108}
2109
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002110static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002111compositor_accumulate_damage(struct weston_compositor *ec)
2112{
2113 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002114 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002115 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002116
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002117 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002118
2119 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002120 pixman_region32_copy(&plane->clip, &clip);
2121
2122 pixman_region32_init(&opaque);
2123
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124 wl_list_for_each(ev, &ec->view_list, link) {
2125 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002126 continue;
2127
Jason Ekstranda7af7042013-10-12 22:38:11 -05002128 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002129 }
2130
2131 pixman_region32_union(&clip, &clip, &opaque);
2132 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002133 }
2134
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002135 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002136
Jason Ekstranda7af7042013-10-12 22:38:11 -05002137 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002138 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002139
2140 wl_list_for_each(ev, &ec->view_list, link) {
2141 if (ev->surface->touched)
2142 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002143 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144
2145 surface_flush_damage(ev->surface);
2146
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002147 /* Both the renderer and the backend have seen the buffer
2148 * by now. If renderer needs the buffer, it has its own
2149 * reference set. If the backend wants to keep the buffer
2150 * around for migrating the surface into a non-primary plane
2151 * later, keep_buffer is true. Otherwise, drop the core
2152 * reference now, and allow early buffer release. This enables
2153 * clients to use single-buffering.
2154 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002155 if (!ev->surface->keep_buffer)
2156 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002157 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002158}
2159
2160static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002162{
2163 struct weston_subsurface *sub;
2164
Pekka Paalanene67858b2013-04-25 13:57:42 +03002165 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002166 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002167 continue;
2168
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2170 wl_list_init(&sub->surface->views);
2171
2172 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002173 }
2174}
2175
2176static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002178{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179 struct weston_subsurface *sub;
2180 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002181
Jason Ekstranda7af7042013-10-12 22:38:11 -05002182 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2183 if (sub->surface == surface)
2184 continue;
2185
George Kiagiadakised04d382014-06-13 18:10:26 +02002186 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2187 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002188 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002189 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002190
2191 surface_free_unused_subsurface_views(sub->surface);
2192 }
2193}
2194
2195static void
2196view_list_add_subsurface_view(struct weston_compositor *compositor,
2197 struct weston_subsurface *sub,
2198 struct weston_view *parent)
2199{
2200 struct weston_subsurface *child;
2201 struct weston_view *view = NULL, *iv;
2202
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002203 if (!weston_surface_is_mapped(sub->surface))
2204 return;
2205
Jason Ekstranda7af7042013-10-12 22:38:11 -05002206 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2207 if (iv->geometry.parent == parent) {
2208 view = iv;
2209 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002210 }
2211 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212
2213 if (view) {
2214 /* Put it back in the surface's list of views */
2215 wl_list_remove(&view->surface_link);
2216 wl_list_insert(&sub->surface->views, &view->surface_link);
2217 } else {
2218 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002219 weston_view_set_position(view,
2220 sub->position.x,
2221 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002222 weston_view_set_transform_parent(view, parent);
2223 }
2224
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002225 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002227 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002228
Pekka Paalanenb188e912013-11-19 14:03:35 +02002229 if (wl_list_empty(&sub->surface->subsurface_list)) {
2230 wl_list_insert(compositor->view_list.prev, &view->link);
2231 return;
2232 }
2233
2234 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2235 if (child->surface == sub->surface)
2236 wl_list_insert(compositor->view_list.prev, &view->link);
2237 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002238 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002239 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002240}
2241
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002242/* This recursively adds the sub-surfaces for a view, relying on the
2243 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2244 * change first happens to the sub-surface list, and then automatically
2245 * propagates here. See weston_surface_damage_subsurfaces() for how the
2246 * sub-surfaces receive damage when the client changes the state.
2247 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002248static void
2249view_list_add(struct weston_compositor *compositor,
2250 struct weston_view *view)
2251{
2252 struct weston_subsurface *sub;
2253
2254 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002255
Pekka Paalanenb188e912013-11-19 14:03:35 +02002256 if (wl_list_empty(&view->surface->subsurface_list)) {
2257 wl_list_insert(compositor->view_list.prev, &view->link);
2258 return;
2259 }
2260
2261 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2262 if (sub->surface == view->surface)
2263 wl_list_insert(compositor->view_list.prev, &view->link);
2264 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002265 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002266 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267}
2268
2269static void
2270weston_compositor_build_view_list(struct weston_compositor *compositor)
2271{
2272 struct weston_view *view;
2273 struct weston_layer *layer;
2274
2275 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002276 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002277 surface_stash_subsurface_views(view->surface);
2278
2279 wl_list_init(&compositor->view_list);
2280 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002281 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002282 view_list_add(compositor, view);
2283 }
2284 }
2285
2286 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002287 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002289}
2290
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002291static void
2292weston_output_take_feedback_list(struct weston_output *output,
2293 struct weston_surface *surface)
2294{
2295 struct weston_view *view;
2296 struct weston_presentation_feedback *feedback;
2297 uint32_t flags = 0xffffffff;
2298
2299 if (wl_list_empty(&surface->feedback_list))
2300 return;
2301
2302 /* All views must have the flag for the flag to survive. */
2303 wl_list_for_each(view, &surface->views, surface_link) {
2304 /* ignore views that are not on this output at all */
2305 if (view->output_mask & (1u << output->id))
2306 flags &= view->psf_flags;
2307 }
2308
2309 wl_list_for_each(feedback, &surface->feedback_list, link)
2310 feedback->psf_flags = flags;
2311
2312 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2313 wl_list_init(&surface->feedback_list);
2314}
2315
David Herrmann1edf44c2013-10-22 17:11:26 +02002316static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002317weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002318{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002319 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002320 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002321 struct weston_animation *animation, *next;
2322 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002323 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002324 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002325 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002326 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002327
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002328 if (output->destroying)
2329 return 0;
2330
Pekka Paalanenb5026542014-11-12 15:09:24 +02002331 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2332
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002333 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002334 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002335
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002336 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002337 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002338 } else {
2339 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002340 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002341 ev->psf_flags = 0;
2342 }
2343 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002344
Pekka Paalanene67858b2013-04-25 13:57:42 +03002345 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002346 wl_list_for_each(ev, &ec->view_list, link) {
2347 /* Note: This operation is safe to do multiple times on the
2348 * same surface.
2349 */
2350 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002351 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002352 &ev->surface->frame_callback_list);
2353 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002354
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002355 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002356 }
2357 }
2358
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002359 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002360
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002361 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002362 pixman_region32_intersect(&output_damage,
2363 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002364 pixman_region32_subtract(&output_damage,
2365 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002366
Scott Moreauccbf29d2012-02-22 14:21:41 -07002367 if (output->dirty)
2368 weston_output_update_matrix(output);
2369
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002370 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002371
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002372 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002373
Daniel Stone09a97e22017-03-01 11:34:06 +00002374 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002375 if (r == 0)
2376 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002377
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002378 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002379
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002380 frame_time_msec = timespec_to_msec(&output->frame_time);
2381
Jonas Ådahldb773762012-06-13 00:01:21 +02002382 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002383 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002384 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002385 }
2386
Scott Moreaud64cf212012-06-08 19:40:54 -06002387 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002388 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002389 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002390 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002391
Pekka Paalanenb5026542014-11-12 15:09:24 +02002392 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2393
David Herrmann1edf44c2013-10-22 17:11:26 +02002394 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002395}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002396
Pekka Paalanen82919792014-05-21 13:51:49 +03002397static void
2398weston_output_schedule_repaint_reset(struct weston_output *output)
2399{
Daniel Stone05df8c12017-03-03 16:59:42 +00002400 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002401 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002402}
2403
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002404static int
2405weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2406 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002407{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002408 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002409 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002410 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002411
Daniel Stone6847b852017-03-01 11:34:08 +00002412 /* We're not ready yet; come back to make a decision later. */
2413 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002414 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002415
2416 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2417 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002418 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002419
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002420 /* If we're sleeping, drop the repaint machinery entirely; we will
2421 * explicitly repaint all outputs when we come back. */
2422 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2423 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2424 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002425
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002426 /* We don't actually need to repaint this output; drop it from
2427 * repaint until something causes damage. */
2428 if (!output->repaint_needed)
2429 goto err;
2430
2431 /* If repaint fails, we aren't going to get weston_output_finish_frame
2432 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002433 * something schedules a successful repaint later. As repainting may
2434 * take some time, re-read our clock as a courtesy to the next
2435 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002436 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002437 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002438 if (ret != 0)
2439 goto err;
2440
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002441 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002442
2443err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002444 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002445 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002446}
2447
2448static void
2449output_repaint_timer_arm(struct weston_compositor *compositor)
2450{
2451 struct weston_output *output;
2452 bool any_should_repaint = false;
2453 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002454 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002455
2456 weston_compositor_read_presentation_clock(compositor, &now);
2457
2458 wl_list_for_each(output, &compositor->output_list, link) {
2459 int64_t msec_to_this;
2460
2461 if (output->repaint_status != REPAINT_SCHEDULED)
2462 continue;
2463
2464 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2465 &now);
2466 if (!any_should_repaint || msec_to_this < msec_to_next)
2467 msec_to_next = msec_to_this;
2468
2469 any_should_repaint = true;
2470 }
2471
2472 if (!any_should_repaint)
2473 return;
2474
2475 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2476 * This is a workaround to allow coalescing multiple output repaints
2477 * particularly from weston_output_finish_frame()
2478 * into the same call, which would not happen if we called
2479 * output_repaint_timer_handler() directly.
2480 */
2481 if (msec_to_next < 1)
2482 msec_to_next = 1;
2483
2484 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2485}
2486
2487static int
2488output_repaint_timer_handler(void *data)
2489{
2490 struct weston_compositor *compositor = data;
2491 struct weston_output *output;
2492 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002493 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002494 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002495
2496 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002497
2498 if (compositor->backend->repaint_begin)
2499 repaint_data = compositor->backend->repaint_begin(compositor);
2500
2501 wl_list_for_each(output, &compositor->output_list, link) {
2502 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2503 if (ret)
2504 break;
2505 }
2506
2507 if (ret == 0) {
2508 if (compositor->backend->repaint_flush)
2509 compositor->backend->repaint_flush(compositor,
2510 repaint_data);
2511 } else {
2512 if (compositor->backend->repaint_cancel)
2513 compositor->backend->repaint_cancel(compositor,
2514 repaint_data);
2515 }
Daniel Stone6847b852017-03-01 11:34:08 +00002516
2517 output_repaint_timer_arm(compositor);
2518
Pekka Paalanen0513a952014-05-21 16:17:27 +03002519 return 0;
2520}
2521
Kristian Høgsbergef044142011-06-21 15:02:12 -04002522WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002523weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002524 const struct timespec *stamp,
2525 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002526{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002527 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002528 int32_t refresh_nsec;
2529 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002530 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002531
Pekka Paalanenb5026542014-11-12 15:09:24 +02002532
Daniel Stone05df8c12017-03-03 16:59:42 +00002533 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002534 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2535
Daniel Stone6847b852017-03-01 11:34:08 +00002536 weston_compositor_read_presentation_clock(compositor, &now);
2537
Daniel Stone3615ce12017-03-01 11:34:05 +00002538 /* If we haven't been supplied any timestamp at all, we don't have a
2539 * timebase to work against, so any delay just wastes time. Push a
2540 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002541 if (!stamp) {
2542 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002543 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002544 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002545
Marius Vladdf9278a2018-03-06 18:56:23 +02002546 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2547 TLP_VBLANK(stamp), TLP_END);
2548
Pekka Paalanend7894d02015-07-03 15:08:53 +03002549 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002550 weston_presentation_feedback_present_list(&output->feedback_list,
2551 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002552 output->msc,
2553 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002554
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002555 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002556
Daniel Stone6847b852017-03-01 11:34:08 +00002557 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2558 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2559 -compositor->repaint_msec);
2560 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002561
2562 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002563 static bool warned;
2564
2565 if (!warned)
2566 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002567 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002568 warned = true;
2569
Daniel Stone6847b852017-03-01 11:34:08 +00002570 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002571 }
2572
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002573 /* Called from restart_repaint_loop and restart happens already after
2574 * the deadline given by repaint_msec? In that case we delay until
2575 * the deadline of the next frame, to give clients a more predictable
2576 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002577 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2578 msec_rel < 0) {
2579 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2580 timespec_add_nsec(&output->next_repaint,
2581 &output->next_repaint,
2582 refresh_nsec);
2583 }
2584 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002585
Daniel Stone3615ce12017-03-01 11:34:05 +00002586out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002587 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002588 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002589}
2590
2591static void
2592idle_repaint(void *data)
2593{
2594 struct weston_output *output = data;
2595
Daniel Stone05df8c12017-03-03 16:59:42 +00002596 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2597 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002598 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002599 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002600}
2601
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002602WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002603weston_layer_entry_insert(struct weston_layer_entry *list,
2604 struct weston_layer_entry *entry)
2605{
2606 wl_list_insert(&list->link, &entry->link);
2607 entry->layer = list->layer;
2608}
2609
2610WL_EXPORT void
2611weston_layer_entry_remove(struct weston_layer_entry *entry)
2612{
2613 wl_list_remove(&entry->link);
2614 wl_list_init(&entry->link);
2615 entry->layer = NULL;
2616}
2617
Quentin Glidic82681572016-12-17 13:40:51 +01002618
2619/** Initialize the weston_layer struct.
2620 *
2621 * \param compositor The compositor instance
2622 * \param layer The layer to initialize
2623 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002624WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002625weston_layer_init(struct weston_layer *layer,
2626 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002627{
Quentin Glidic82681572016-12-17 13:40:51 +01002628 layer->compositor = compositor;
2629 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002630 wl_list_init(&layer->view_list.link);
2631 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002632 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002633}
2634
2635/** Sets the position of the layer in the layer list. The layer will be placed
2636 * below any layer with the same position value, if any.
2637 * This function is safe to call if the layer is already on the list, but the
2638 * layer may be moved below other layers at the same position, if any.
2639 *
2640 * \param layer The layer to modify
2641 * \param position The position the layer will be placed at
2642 */
2643WL_EXPORT void
2644weston_layer_set_position(struct weston_layer *layer,
2645 enum weston_layer_position position)
2646{
2647 struct weston_layer *below;
2648
2649 wl_list_remove(&layer->link);
2650
2651 /* layer_list is ordered from top to bottom, the last layer being the
2652 * background with the smallest position value */
2653
2654 layer->position = position;
2655 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2656 if (below->position >= layer->position) {
2657 wl_list_insert(&below->link, &layer->link);
2658 return;
2659 }
2660 }
2661 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2662}
2663
2664/** Hide a layer by taking it off the layer list.
2665 * This function is safe to call if the layer is not on the list.
2666 *
2667 * \param layer The layer to hide
2668 */
2669WL_EXPORT void
2670weston_layer_unset_position(struct weston_layer *layer)
2671{
2672 wl_list_remove(&layer->link);
2673 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002674}
2675
2676WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002677weston_layer_set_mask(struct weston_layer *layer,
2678 int x, int y, int width, int height)
2679{
2680 struct weston_view *view;
2681
2682 layer->mask.x1 = x;
2683 layer->mask.x2 = x + width;
2684 layer->mask.y1 = y;
2685 layer->mask.y2 = y + height;
2686
2687 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2688 weston_view_geometry_dirty(view);
2689 }
2690}
2691
2692WL_EXPORT void
2693weston_layer_set_mask_infinite(struct weston_layer *layer)
2694{
2695 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2696 UINT32_MAX, UINT32_MAX);
2697}
2698
2699WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002700weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002701{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002702 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002703 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002704
Bryce Harrington08976ac2016-08-30 12:05:16 -07002705 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2706 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002707 return;
2708
Pekka Paalanenb5026542014-11-12 15:09:24 +02002709 if (!output->repaint_needed)
2710 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2711
Kristian Høgsbergef044142011-06-21 15:02:12 -04002712 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002713 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002714
2715 /* If we already have a repaint scheduled for our idle handler,
2716 * no need to set it again. If the repaint has been called but
2717 * not finished, then weston_output_finish_frame() will notice
2718 * that a repaint is needed and schedule one. */
2719 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002720 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002721
Daniel Stone05df8c12017-03-03 16:59:42 +00002722 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002723 assert(!output->idle_repaint_source);
2724 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2725 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002726 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002727}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002728
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002729WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002730weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2731{
2732 struct weston_output *output;
2733
2734 wl_list_for_each(output, &compositor->output_list, link)
2735 weston_output_schedule_repaint(output);
2736}
2737
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002738static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002739surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002740{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002741 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002742}
2743
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002744static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002745surface_attach(struct wl_client *client,
2746 struct wl_resource *resource,
2747 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2748{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002749 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002750 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002751
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002752 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002753 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002754 if (buffer == NULL) {
2755 wl_client_post_no_memory(client);
2756 return;
2757 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002758 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002759
Pekka Paalanende685b82012-12-04 15:58:12 +02002760 /* Attach, attach, without commit in between does not send
2761 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002762 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002763
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002764 surface->pending.sx = sx;
2765 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002766 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002767}
2768
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002769static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002770surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002771 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002772 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002773{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002774 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002775
Derek Foreman57e92ed2015-11-17 14:11:35 -06002776 if (width <= 0 || height <= 0)
2777 return;
2778
Derek Foreman152254b2015-11-26 14:17:48 -06002779 pixman_region32_union_rect(&surface->pending.damage_surface,
2780 &surface->pending.damage_surface,
2781 x, y, width, height);
2782}
2783
2784static void
2785surface_damage_buffer(struct wl_client *client,
2786 struct wl_resource *resource,
2787 int32_t x, int32_t y, int32_t width, int32_t height)
2788{
2789 struct weston_surface *surface = wl_resource_get_user_data(resource);
2790
2791 if (width <= 0 || height <= 0)
2792 return;
2793
2794 pixman_region32_union_rect(&surface->pending.damage_buffer,
2795 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002796 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002797}
2798
Kristian Høgsberg33418202011-08-16 23:01:28 -04002799static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002800destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002801{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002802 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002803
2804 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002805 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002806}
2807
2808static void
2809surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002810 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002811{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002812 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002813 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002814
2815 cb = malloc(sizeof *cb);
2816 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002817 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002818 return;
2819 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002820
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002821 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2822 callback);
2823 if (cb->resource == NULL) {
2824 free(cb);
2825 wl_resource_post_no_memory(resource);
2826 return;
2827 }
2828
Jason Ekstranda85118c2013-06-27 20:17:02 -05002829 wl_resource_set_implementation(cb->resource, NULL, cb,
2830 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002831
Pekka Paalanenbc106382012-10-10 12:49:31 +03002832 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002833}
2834
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002835static void
2836surface_set_opaque_region(struct wl_client *client,
2837 struct wl_resource *resource,
2838 struct wl_resource *region_resource)
2839{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002840 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002841 struct weston_region *region;
2842
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002843 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002844 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002845 pixman_region32_copy(&surface->pending.opaque,
2846 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002847 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002848 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002849 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002850}
2851
2852static void
2853surface_set_input_region(struct wl_client *client,
2854 struct wl_resource *resource,
2855 struct wl_resource *region_resource)
2856{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002857 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002858 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002859
2860 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002861 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002862 pixman_region32_copy(&surface->pending.input,
2863 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002864 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002865 pixman_region32_fini(&surface->pending.input);
2866 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002867 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002868}
2869
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002870/* Cause damage to this sub-surface and all its children.
2871 *
2872 * This is useful when there are state changes that need an implicit
2873 * damage, e.g. a z-order change.
2874 */
2875static void
2876weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2877{
2878 struct weston_subsurface *child;
2879
2880 weston_surface_damage(sub->surface);
2881 sub->reordered = false;
2882
2883 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2884 if (child != sub)
2885 weston_surface_damage_subsurfaces(child);
2886}
2887
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002888static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002889weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002890{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002891 struct weston_subsurface *sub;
2892
2893 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2894 parent_link_pending) {
2895 wl_list_remove(&sub->parent_link);
2896 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002897
2898 if (sub->reordered)
2899 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002900 }
2901}
2902
2903static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002904weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002905 struct weston_matrix *matrix)
2906{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002907 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002908 double src_width, src_height, dest_width, dest_height;
2909
2910 weston_matrix_init(matrix);
2911
2912 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2913 src_width = surface->width_from_buffer;
2914 src_height = surface->height_from_buffer;
2915 } else {
2916 src_width = wl_fixed_to_double(vp->buffer.src_width);
2917 src_height = wl_fixed_to_double(vp->buffer.src_height);
2918 }
2919
2920 if (vp->surface.width == -1) {
2921 dest_width = src_width;
2922 dest_height = src_height;
2923 } else {
2924 dest_width = vp->surface.width;
2925 dest_height = vp->surface.height;
2926 }
2927
2928 if (src_width != dest_width || src_height != dest_height)
2929 weston_matrix_scale(matrix,
2930 src_width / dest_width,
2931 src_height / dest_height, 1);
2932
2933 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2934 weston_matrix_translate(matrix,
2935 wl_fixed_to_double(vp->buffer.src_x),
2936 wl_fixed_to_double(vp->buffer.src_y),
2937 0);
2938
2939 switch (vp->buffer.transform) {
2940 case WL_OUTPUT_TRANSFORM_FLIPPED:
2941 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2942 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2943 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2944 weston_matrix_scale(matrix, -1, 1, 1);
2945 weston_matrix_translate(matrix,
2946 surface->width_from_buffer, 0, 0);
2947 break;
2948 }
2949
2950 switch (vp->buffer.transform) {
2951 default:
2952 case WL_OUTPUT_TRANSFORM_NORMAL:
2953 case WL_OUTPUT_TRANSFORM_FLIPPED:
2954 break;
2955 case WL_OUTPUT_TRANSFORM_90:
2956 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2957 weston_matrix_rotate_xy(matrix, 0, 1);
2958 weston_matrix_translate(matrix,
2959 surface->height_from_buffer, 0, 0);
2960 break;
2961 case WL_OUTPUT_TRANSFORM_180:
2962 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2963 weston_matrix_rotate_xy(matrix, -1, 0);
2964 weston_matrix_translate(matrix,
2965 surface->width_from_buffer,
2966 surface->height_from_buffer, 0);
2967 break;
2968 case WL_OUTPUT_TRANSFORM_270:
2969 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2970 weston_matrix_rotate_xy(matrix, 0, -1);
2971 weston_matrix_translate(matrix,
2972 0, surface->width_from_buffer, 0);
2973 break;
2974 }
2975
2976 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2977}
2978
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002979/**
2980 * Compute a + b > c while being safe to overflows.
2981 */
2982static bool
2983fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2984{
2985 return (int64_t)a + (int64_t)b > (int64_t)c;
2986}
2987
2988static bool
2989weston_surface_is_pending_viewport_source_valid(
2990 const struct weston_surface *surface)
2991{
2992 const struct weston_surface_state *pend = &surface->pending;
2993 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2994 int width_from_buffer = 0;
2995 int height_from_buffer = 0;
2996 wl_fixed_t w;
2997 wl_fixed_t h;
2998
2999 /* If viewport source rect is not set, it is always ok. */
3000 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3001 return true;
3002
3003 if (pend->newly_attached) {
3004 if (pend->buffer) {
3005 convert_size_by_transform_scale(&width_from_buffer,
3006 &height_from_buffer,
3007 pend->buffer->width,
3008 pend->buffer->height,
3009 vp->buffer.transform,
3010 vp->buffer.scale);
3011 } else {
3012 /* No buffer: viewport is irrelevant. */
3013 return true;
3014 }
3015 } else {
3016 width_from_buffer = surface->width_from_buffer;
3017 height_from_buffer = surface->height_from_buffer;
3018 }
3019
3020 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3021 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3022
3023 /* No buffer: viewport is irrelevant. */
3024 if (width_from_buffer == 0 || height_from_buffer == 0)
3025 return true;
3026
3027 /* overflow checks for wl_fixed_from_int() */
3028 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3029 return false;
3030 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3031 return false;
3032
3033 w = wl_fixed_from_int(width_from_buffer);
3034 h = wl_fixed_from_int(height_from_buffer);
3035
3036 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3037 return false;
3038 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3039 return false;
3040
3041 return true;
3042}
3043
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003044static bool
3045fixed_is_integer(wl_fixed_t v)
3046{
3047 return (v & 0xff) == 0;
3048}
3049
3050static bool
3051weston_surface_is_pending_viewport_dst_size_int(
3052 const struct weston_surface *surface)
3053{
3054 const struct weston_buffer_viewport *vp =
3055 &surface->pending.buffer_viewport;
3056
3057 if (vp->surface.width != -1) {
3058 assert(vp->surface.width > 0 && vp->surface.height > 0);
3059 return true;
3060 }
3061
3062 return fixed_is_integer(vp->buffer.src_width) &&
3063 fixed_is_integer(vp->buffer.src_height);
3064}
3065
Derek Foreman152254b2015-11-26 14:17:48 -06003066/* Translate pending damage in buffer co-ordinates to surface
3067 * co-ordinates and union it with a pixman_region32_t.
3068 * This should only be called after the buffer is attached.
3069 */
3070static void
3071apply_damage_buffer(pixman_region32_t *dest,
3072 struct weston_surface *surface,
3073 struct weston_surface_state *state)
3074{
3075 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3076
3077 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3078 * translated into surface co-ordinates and unioned with
3079 * any other surface damage.
3080 * None of this makes sense if there is no buffer though.
3081 */
3082 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3083 pixman_region32_t buffer_damage;
3084
3085 pixman_region32_intersect_rect(&state->damage_buffer,
3086 &state->damage_buffer,
3087 0, 0, buffer->width,
3088 buffer->height);
3089 pixman_region32_init(&buffer_damage);
3090 weston_matrix_transform_region(&buffer_damage,
3091 &surface->buffer_to_surface_matrix,
3092 &state->damage_buffer);
3093 pixman_region32_union(dest, dest, &buffer_damage);
3094 pixman_region32_fini(&buffer_damage);
3095 }
3096 /* We should clear this on commit even if there was no buffer */
3097 pixman_region32_clear(&state->damage_buffer);
3098}
3099
Jason Ekstrand1e059042014-10-16 10:55:19 -05003100static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003101weston_surface_commit_state(struct weston_surface *surface,
3102 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003103{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003104 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003105 pixman_region32_t opaque;
3106
Alexander Larsson4ea95522013-05-22 14:41:37 +02003107 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003108 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003109 /* wp_viewport.set_source */
3110 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003111 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003112
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003113 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003114 if (state->newly_attached)
3115 weston_surface_attach(surface, state->buffer);
3116 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003117
Jason Ekstrand1e059042014-10-16 10:55:19 -05003118 weston_surface_build_buffer_matrix(surface,
3119 &surface->surface_to_buffer_matrix);
3120 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3121 &surface->surface_to_buffer_matrix);
3122
Jason Ekstrand7b982072014-05-20 14:33:03 -05003123 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003124 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003125 if (surface->committed)
3126 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003127 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003128
Jason Ekstrand7b982072014-05-20 14:33:03 -05003129 state->sx = 0;
3130 state->sy = 0;
3131 state->newly_attached = 0;
3132 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003133
Derek Foreman152254b2015-11-26 14:17:48 -06003134 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003135 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003136 (pixman_region32_not_empty(&state->damage_surface) ||
3137 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003138 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003139
Pekka Paalanen8e159182012-10-10 12:49:25 +03003140 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003141 &state->damage_surface);
3142
3143 apply_damage_buffer(&surface->damage, surface, state);
3144
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003145 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003146 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003147 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003148
3149 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003150 pixman_region32_init(&opaque);
3151 pixman_region32_intersect_rect(&opaque, &state->opaque,
3152 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003153
3154 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3155 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003156 wl_list_for_each(view, &surface->views, surface_link)
3157 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003158 }
3159
3160 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003161
3162 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003163 pixman_region32_intersect_rect(&surface->input, &state->input,
3164 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003165
Pekka Paalanenbc106382012-10-10 12:49:31 +03003166 /* wl_surface.frame */
3167 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003168 &state->frame_callback_list);
3169 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003170
3171 /* XXX:
3172 * What should happen with a feedback request, if there
3173 * is no wl_buffer attached for this commit?
3174 */
3175
3176 /* presentation.feedback */
3177 wl_list_insert_list(&surface->feedback_list,
3178 &state->feedback_list);
3179 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003180
3181 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003182}
3183
3184static void
3185weston_surface_commit(struct weston_surface *surface)
3186{
3187 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003188
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189 weston_surface_commit_subsurface_order(surface);
3190
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003191 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003192}
3193
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003194static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003195weston_subsurface_commit(struct weston_subsurface *sub);
3196
3197static void
3198weston_subsurface_parent_commit(struct weston_subsurface *sub,
3199 int parent_is_synchronized);
3200
3201static void
3202surface_commit(struct wl_client *client, struct wl_resource *resource)
3203{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003204 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003205 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3206
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003207 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3208 assert(surface->viewport_resource);
3209
3210 wl_resource_post_error(surface->viewport_resource,
3211 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3212 "wl_surface@%d has viewport source outside buffer",
3213 wl_resource_get_id(resource));
3214 return;
3215 }
3216
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003217 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3218 assert(surface->viewport_resource);
3219
3220 wl_resource_post_error(surface->viewport_resource,
3221 WP_VIEWPORT_ERROR_BAD_SIZE,
3222 "wl_surface@%d viewport dst size not integer",
3223 wl_resource_get_id(resource));
3224 return;
3225 }
3226
Pekka Paalanene67858b2013-04-25 13:57:42 +03003227 if (sub) {
3228 weston_subsurface_commit(sub);
3229 return;
3230 }
3231
3232 weston_surface_commit(surface);
3233
3234 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3235 if (sub->surface != surface)
3236 weston_subsurface_parent_commit(sub, 0);
3237 }
3238}
3239
3240static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003241surface_set_buffer_transform(struct wl_client *client,
3242 struct wl_resource *resource, int transform)
3243{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003244 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003245
Jonny Lamba55f1392014-05-30 12:07:15 +02003246 /* if wl_output.transform grows more members this will need to be updated. */
3247 if (transform < 0 ||
3248 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3249 wl_resource_post_error(resource,
3250 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3251 "buffer transform must be a valid transform "
3252 "('%d' specified)", transform);
3253 return;
3254 }
3255
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003256 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003257 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003258}
3259
Alexander Larsson4ea95522013-05-22 14:41:37 +02003260static void
3261surface_set_buffer_scale(struct wl_client *client,
3262 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003263 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003264{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003265 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003266
Jonny Lamba55f1392014-05-30 12:07:15 +02003267 if (scale < 1) {
3268 wl_resource_post_error(resource,
3269 WL_SURFACE_ERROR_INVALID_SCALE,
3270 "buffer scale must be at least one "
3271 "('%d' specified)", scale);
3272 return;
3273 }
3274
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003275 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003276 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003277}
3278
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003279static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003280 surface_destroy,
3281 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003282 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003283 surface_frame,
3284 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003285 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003286 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003287 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003288 surface_set_buffer_scale,
3289 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003290};
3291
3292static void
3293compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003294 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003295{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003296 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003297 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003298
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003299 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003300 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003301 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003302 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003303 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003304
Jason Ekstranda85118c2013-06-27 20:17:02 -05003305 surface->resource =
3306 wl_resource_create(client, &wl_surface_interface,
3307 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003308 if (surface->resource == NULL) {
3309 weston_surface_destroy(surface);
3310 wl_resource_post_no_memory(resource);
3311 return;
3312 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003313 wl_resource_set_implementation(surface->resource, &surface_interface,
3314 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003315
3316 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003317}
3318
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003319static void
3320destroy_region(struct wl_resource *resource)
3321{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003322 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003323
3324 pixman_region32_fini(&region->region);
3325 free(region);
3326}
3327
3328static void
3329region_destroy(struct wl_client *client, struct wl_resource *resource)
3330{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003331 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003332}
3333
3334static void
3335region_add(struct wl_client *client, struct wl_resource *resource,
3336 int32_t x, int32_t y, int32_t width, int32_t height)
3337{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003338 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003339
3340 pixman_region32_union_rect(&region->region, &region->region,
3341 x, y, width, height);
3342}
3343
3344static void
3345region_subtract(struct wl_client *client, struct wl_resource *resource,
3346 int32_t x, int32_t y, int32_t width, int32_t height)
3347{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003348 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003349 pixman_region32_t rect;
3350
3351 pixman_region32_init_rect(&rect, x, y, width, height);
3352 pixman_region32_subtract(&region->region, &region->region, &rect);
3353 pixman_region32_fini(&rect);
3354}
3355
3356static const struct wl_region_interface region_interface = {
3357 region_destroy,
3358 region_add,
3359 region_subtract
3360};
3361
3362static void
3363compositor_create_region(struct wl_client *client,
3364 struct wl_resource *resource, uint32_t id)
3365{
3366 struct weston_region *region;
3367
3368 region = malloc(sizeof *region);
3369 if (region == NULL) {
3370 wl_resource_post_no_memory(resource);
3371 return;
3372 }
3373
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003374 pixman_region32_init(&region->region);
3375
Jason Ekstranda85118c2013-06-27 20:17:02 -05003376 region->resource =
3377 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003378 if (region->resource == NULL) {
3379 free(region);
3380 wl_resource_post_no_memory(resource);
3381 return;
3382 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003383 wl_resource_set_implementation(region->resource, &region_interface,
3384 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003385}
3386
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003387static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003388 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003389 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003390};
3391
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003392static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003393weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3394{
3395 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003396
Jason Ekstrand7b982072014-05-20 14:33:03 -05003397 weston_surface_commit_state(surface, &sub->cached);
3398 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003399
3400 weston_surface_commit_subsurface_order(surface);
3401
3402 weston_surface_schedule_repaint(surface);
3403
Jason Ekstrand7b982072014-05-20 14:33:03 -05003404 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003405}
3406
3407static void
3408weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3409{
3410 struct weston_surface *surface = sub->surface;
3411
3412 /*
3413 * If this commit would cause the surface to move by the
3414 * attach(dx, dy) parameters, the old damage region must be
3415 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003416 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003417 */
Derek Foreman152254b2015-11-26 14:17:48 -06003418 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003419 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003420 pixman_region32_union(&sub->cached.damage_surface,
3421 &sub->cached.damage_surface,
3422 &surface->pending.damage_surface);
3423 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003424
3425 if (surface->pending.newly_attached) {
3426 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003427 weston_surface_state_set_buffer(&sub->cached,
3428 surface->pending.buffer);
3429 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003430 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003431 weston_presentation_feedback_discard_list(
3432 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003433 }
3434 sub->cached.sx += surface->pending.sx;
3435 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003436
Derek Foreman152254b2015-11-26 14:17:48 -06003437 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3438
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003439 sub->cached.buffer_viewport.changed |=
3440 surface->pending.buffer_viewport.changed;
3441 sub->cached.buffer_viewport.buffer =
3442 surface->pending.buffer_viewport.buffer;
3443 sub->cached.buffer_viewport.surface =
3444 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003445
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003446 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003447
3448 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3449
3450 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3451
3452 wl_list_insert_list(&sub->cached.frame_callback_list,
3453 &surface->pending.frame_callback_list);
3454 wl_list_init(&surface->pending.frame_callback_list);
3455
Pekka Paalanen133e4392014-09-23 22:08:46 -04003456 wl_list_insert_list(&sub->cached.feedback_list,
3457 &surface->pending.feedback_list);
3458 wl_list_init(&surface->pending.feedback_list);
3459
Jason Ekstrand7b982072014-05-20 14:33:03 -05003460 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003461}
3462
Derek Foreman280e7dd2014-10-03 13:13:42 -05003463static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003464weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3465{
3466 while (sub) {
3467 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003468 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003469
3470 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003471 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003472
3473 sub = weston_surface_to_subsurface(sub->parent);
3474 }
3475
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003476 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003477}
3478
3479static void
3480weston_subsurface_commit(struct weston_subsurface *sub)
3481{
3482 struct weston_surface *surface = sub->surface;
3483 struct weston_subsurface *tmp;
3484
3485 /* Recursive check for effectively synchronized. */
3486 if (weston_subsurface_is_synchronized(sub)) {
3487 weston_subsurface_commit_to_cache(sub);
3488 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003489 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003490 /* flush accumulated state from cache */
3491 weston_subsurface_commit_to_cache(sub);
3492 weston_subsurface_commit_from_cache(sub);
3493 } else {
3494 weston_surface_commit(surface);
3495 }
3496
3497 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3498 if (tmp->surface != surface)
3499 weston_subsurface_parent_commit(tmp, 0);
3500 }
3501 }
3502}
3503
3504static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003505weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003506{
3507 struct weston_surface *surface = sub->surface;
3508 struct weston_subsurface *tmp;
3509
Pekka Paalanene67858b2013-04-25 13:57:42 +03003510 /* From now on, commit_from_cache the whole sub-tree, regardless of
3511 * the synchronized mode of each child. This sub-surface or some
3512 * of its ancestors were synchronized, so we are synchronized
3513 * all the way down.
3514 */
3515
Jason Ekstrand7b982072014-05-20 14:33:03 -05003516 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003517 weston_subsurface_commit_from_cache(sub);
3518
3519 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3520 if (tmp->surface != surface)
3521 weston_subsurface_parent_commit(tmp, 1);
3522 }
3523}
3524
3525static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003526weston_subsurface_parent_commit(struct weston_subsurface *sub,
3527 int parent_is_synchronized)
3528{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003529 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003530 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003531 wl_list_for_each(view, &sub->surface->views, surface_link)
3532 weston_view_set_position(view,
3533 sub->position.x,
3534 sub->position.y);
3535
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003536 sub->position.set = 0;
3537 }
3538
3539 if (parent_is_synchronized || sub->synchronized)
3540 weston_subsurface_synchronized_commit(sub);
3541}
3542
Pekka Paalanen8274d902014-08-06 19:36:51 +03003543static int
3544subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3545{
3546 return snprintf(buf, len, "sub-surface");
3547}
3548
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003549static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003550subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003552 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003553
Jason Ekstranda7af7042013-10-12 22:38:11 -05003554 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003555 weston_view_set_position(view,
3556 view->geometry.x + dx,
3557 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003558
3559 /* No need to check parent mappedness, because if parent is not
3560 * mapped, parent is not in a visible layer, so this sub-surface
3561 * will not be drawn either.
3562 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003563
Pekka Paalanene67858b2013-04-25 13:57:42 +03003564 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003565 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003566
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003567 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003568 * because that would call it also for the parent surface,
3569 * which might not be mapped yet. That would lead to
3570 * inconsistent state, where the window could never be
3571 * mapped.
3572 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003573 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003574 * weston_surface_is_mapped() return true, so that when the
3575 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003576 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003577 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003578 }
3579}
3580
3581static struct weston_subsurface *
3582weston_surface_to_subsurface(struct weston_surface *surface)
3583{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003584 if (surface->committed == subsurface_committed)
3585 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003586
3587 return NULL;
3588}
3589
Pekka Paalanen01388e22013-04-25 13:57:44 +03003590WL_EXPORT struct weston_surface *
3591weston_surface_get_main_surface(struct weston_surface *surface)
3592{
3593 struct weston_subsurface *sub;
3594
3595 while (surface && (sub = weston_surface_to_subsurface(surface)))
3596 surface = sub->parent;
3597
3598 return surface;
3599}
3600
Pekka Paalanen50b67472014-10-01 15:02:41 +03003601WL_EXPORT int
3602weston_surface_set_role(struct weston_surface *surface,
3603 const char *role_name,
3604 struct wl_resource *error_resource,
3605 uint32_t error_code)
3606{
3607 assert(role_name);
3608
3609 if (surface->role_name == NULL ||
3610 surface->role_name == role_name ||
3611 strcmp(surface->role_name, role_name) == 0) {
3612 surface->role_name = role_name;
3613
3614 return 0;
3615 }
3616
3617 wl_resource_post_error(error_resource, error_code,
3618 "Cannot assign role %s to wl_surface@%d,"
3619 " already has role %s\n",
3620 role_name,
3621 wl_resource_get_id(surface->resource),
3622 surface->role_name);
3623 return -1;
3624}
3625
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003626WL_EXPORT const char *
3627weston_surface_get_role(struct weston_surface *surface)
3628{
3629 return surface->role_name;
3630}
3631
Pekka Paalanen8274d902014-08-06 19:36:51 +03003632WL_EXPORT void
3633weston_surface_set_label_func(struct weston_surface *surface,
3634 int (*desc)(struct weston_surface *,
3635 char *, size_t))
3636{
3637 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003638 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003639}
3640
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003641/** Get the size of surface contents
3642 *
3643 * \param surface The surface to query.
3644 * \param width Returns the width of raw contents.
3645 * \param height Returns the height of raw contents.
3646 *
3647 * Retrieves the raw surface content size in pixels for the given surface.
3648 * This is the whole content size in buffer pixels. If the surface
3649 * has no content or the renderer does not implement this feature,
3650 * zeroes are returned.
3651 *
3652 * This function is used to determine the buffer size needed for
3653 * a weston_surface_copy_content() call.
3654 */
3655WL_EXPORT void
3656weston_surface_get_content_size(struct weston_surface *surface,
3657 int *width, int *height)
3658{
3659 struct weston_renderer *rer = surface->compositor->renderer;
3660
3661 if (!rer->surface_get_content_size) {
3662 *width = 0;
3663 *height = 0;
3664 return;
3665 }
3666
3667 rer->surface_get_content_size(surface, width, height);
3668}
3669
Quentin Glidic248dd102016-08-12 10:41:34 +02003670/** Get the bounding box of a surface and its subsurfaces
3671 *
3672 * \param surface The surface to query.
3673 * \return The bounding box relative to the surface origin.
3674 *
3675 */
3676WL_EXPORT struct weston_geometry
3677weston_surface_get_bounding_box(struct weston_surface *surface)
3678{
3679 pixman_region32_t region;
3680 pixman_box32_t *box;
3681 struct weston_subsurface *subsurface;
3682
3683 pixman_region32_init_rect(&region,
3684 0, 0,
3685 surface->width, surface->height);
3686
3687 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3688 pixman_region32_union_rect(&region, &region,
3689 subsurface->position.x,
3690 subsurface->position.y,
3691 subsurface->surface->width,
3692 subsurface->surface->height);
3693
3694 box = pixman_region32_extents(&region);
3695 struct weston_geometry geometry = {
3696 .x = box->x1,
3697 .y = box->y1,
3698 .width = box->x2 - box->x1,
3699 .height = box->y2 - box->y1,
3700 };
3701
3702 pixman_region32_fini(&region);
3703
3704 return geometry;
3705}
3706
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003707/** Copy surface contents to system memory.
3708 *
3709 * \param surface The surface to copy from.
3710 * \param target Pointer to the target memory buffer.
3711 * \param size Size of the target buffer in bytes.
3712 * \param src_x X location on contents to copy from.
3713 * \param src_y Y location on contents to copy from.
3714 * \param width Width in pixels of the area to copy.
3715 * \param height Height in pixels of the area to copy.
3716 * \return 0 for success, -1 for failure.
3717 *
3718 * Surface contents are maintained by the renderer. They can be in a
3719 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3720 * else.
3721 *
3722 * Surface contents are copied into memory pointed to by target,
3723 * which has size bytes of space available. The target memory
3724 * may be larger than needed, but being smaller returns an error.
3725 * The extra bytes in target may or may not be written; their content is
3726 * unspecified. Size must be large enough to hold the image.
3727 *
3728 * The image in the target memory will be arranged in rows from
3729 * top to bottom, and pixels on a row from left to right. The pixel
3730 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3731 * width * 4.
3732 *
3733 * Parameters src_x and src_y define the upper-left corner in buffer
3734 * coordinates (pixels) to copy from. Parameters width and height
3735 * define the size of the area to copy in pixels.
3736 *
3737 * The rectangle defined by src_x, src_y, width, height must fit in
3738 * the surface contents. Otherwise an error is returned.
3739 *
3740 * Use surface_get_data_size to determine the content size; the
3741 * needed target buffer size and rectangle limits.
3742 *
3743 * CURRENT IMPLEMENTATION RESTRICTIONS:
3744 * - the machine must be little-endian due to Pixman formats.
3745 *
3746 * NOTE: Pixman formats are premultiplied.
3747 */
3748WL_EXPORT int
3749weston_surface_copy_content(struct weston_surface *surface,
3750 void *target, size_t size,
3751 int src_x, int src_y,
3752 int width, int height)
3753{
3754 struct weston_renderer *rer = surface->compositor->renderer;
3755 int cw, ch;
3756 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3757
3758 if (!rer->surface_copy_content)
3759 return -1;
3760
3761 weston_surface_get_content_size(surface, &cw, &ch);
3762
3763 if (src_x < 0 || src_y < 0)
3764 return -1;
3765
3766 if (width <= 0 || height <= 0)
3767 return -1;
3768
3769 if (src_x + width > cw || src_y + height > ch)
3770 return -1;
3771
3772 if (width * bytespp * height > size)
3773 return -1;
3774
3775 return rer->surface_copy_content(surface, target, size,
3776 src_x, src_y, width, height);
3777}
3778
Pekka Paalanene67858b2013-04-25 13:57:42 +03003779static void
3780subsurface_set_position(struct wl_client *client,
3781 struct wl_resource *resource, int32_t x, int32_t y)
3782{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003783 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003784
3785 if (!sub)
3786 return;
3787
3788 sub->position.x = x;
3789 sub->position.y = y;
3790 sub->position.set = 1;
3791}
3792
3793static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003794subsurface_find_sibling(struct weston_subsurface *sub,
3795 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003796{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003797 struct weston_surface *parent = sub->parent;
3798 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003799
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003800 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3801 if (sibling->surface == surface && sibling != sub)
3802 return sibling;
3803 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003804
3805 return NULL;
3806}
3807
3808static struct weston_subsurface *
3809subsurface_sibling_check(struct weston_subsurface *sub,
3810 struct weston_surface *surface,
3811 const char *request)
3812{
3813 struct weston_subsurface *sibling;
3814
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003815 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003816 if (!sibling) {
3817 wl_resource_post_error(sub->resource,
3818 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3819 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003820 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003821 return NULL;
3822 }
3823
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003824 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003825
3826 return sibling;
3827}
3828
3829static void
3830subsurface_place_above(struct wl_client *client,
3831 struct wl_resource *resource,
3832 struct wl_resource *sibling_resource)
3833{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003834 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003835 struct weston_surface *surface =
3836 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003837 struct weston_subsurface *sibling;
3838
3839 if (!sub)
3840 return;
3841
3842 sibling = subsurface_sibling_check(sub, surface, "place_above");
3843 if (!sibling)
3844 return;
3845
3846 wl_list_remove(&sub->parent_link_pending);
3847 wl_list_insert(sibling->parent_link_pending.prev,
3848 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003849
3850 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003851}
3852
3853static void
3854subsurface_place_below(struct wl_client *client,
3855 struct wl_resource *resource,
3856 struct wl_resource *sibling_resource)
3857{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003858 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003859 struct weston_surface *surface =
3860 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003861 struct weston_subsurface *sibling;
3862
3863 if (!sub)
3864 return;
3865
3866 sibling = subsurface_sibling_check(sub, surface, "place_below");
3867 if (!sibling)
3868 return;
3869
3870 wl_list_remove(&sub->parent_link_pending);
3871 wl_list_insert(&sibling->parent_link_pending,
3872 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003873
3874 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003875}
3876
3877static void
3878subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3879{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003880 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003881
3882 if (sub)
3883 sub->synchronized = 1;
3884}
3885
3886static void
3887subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3888{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003889 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003890
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003891 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003892 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003893
3894 /* If sub became effectively desynchronized, flush. */
3895 if (!weston_subsurface_is_synchronized(sub))
3896 weston_subsurface_synchronized_commit(sub);
3897 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003898}
3899
3900static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003901weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3902{
3903 wl_list_remove(&sub->parent_link);
3904 wl_list_remove(&sub->parent_link_pending);
3905 wl_list_remove(&sub->parent_destroy_listener.link);
3906 sub->parent = NULL;
3907}
3908
3909static void
3910weston_subsurface_destroy(struct weston_subsurface *sub);
3911
3912static void
3913subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3914{
3915 struct weston_subsurface *sub =
3916 container_of(listener, struct weston_subsurface,
3917 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003918 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003919
3920 /* The protocol object (wl_resource) is left inert. */
3921 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003922 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003923
3924 weston_subsurface_destroy(sub);
3925}
3926
3927static void
3928subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3929{
3930 struct weston_subsurface *sub =
3931 container_of(listener, struct weston_subsurface,
3932 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003933 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003934 assert(sub->surface != sub->parent);
3935
3936 if (weston_surface_is_mapped(sub->surface))
3937 weston_surface_unmap(sub->surface);
3938
3939 weston_subsurface_unlink_parent(sub);
3940}
3941
3942static void
3943subsurface_resource_destroy(struct wl_resource *resource)
3944{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003945 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003946
3947 if (sub)
3948 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003949}
3950
3951static void
3952subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3953{
3954 wl_resource_destroy(resource);
3955}
3956
3957static void
3958weston_subsurface_link_parent(struct weston_subsurface *sub,
3959 struct weston_surface *parent)
3960{
3961 sub->parent = parent;
3962 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003963 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003964 &sub->parent_destroy_listener);
3965
3966 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3967 wl_list_insert(&parent->subsurface_list_pending,
3968 &sub->parent_link_pending);
3969}
3970
3971static void
3972weston_subsurface_link_surface(struct weston_subsurface *sub,
3973 struct weston_surface *surface)
3974{
3975 sub->surface = surface;
3976 sub->surface_destroy_listener.notify =
3977 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003978 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003979 &sub->surface_destroy_listener);
3980}
3981
3982static void
3983weston_subsurface_destroy(struct weston_subsurface *sub)
3984{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003985 struct weston_view *view, *next;
3986
Pekka Paalanene67858b2013-04-25 13:57:42 +03003987 assert(sub->surface);
3988
3989 if (sub->resource) {
3990 assert(weston_surface_to_subsurface(sub->surface) == sub);
3991 assert(sub->parent_destroy_listener.notify ==
3992 subsurface_handle_parent_destroy);
3993
George Kiagiadakised04d382014-06-13 18:10:26 +02003994 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3995 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003996 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003997 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003998
Pekka Paalanene67858b2013-04-25 13:57:42 +03003999 if (sub->parent)
4000 weston_subsurface_unlink_parent(sub);
4001
Jason Ekstrand7b982072014-05-20 14:33:03 -05004002 weston_surface_state_fini(&sub->cached);
4003 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004004
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004005 sub->surface->committed = NULL;
4006 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004007 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004008 } else {
4009 /* the dummy weston_subsurface for the parent itself */
4010 assert(sub->parent_destroy_listener.notify == NULL);
4011 wl_list_remove(&sub->parent_link);
4012 wl_list_remove(&sub->parent_link_pending);
4013 }
4014
4015 wl_list_remove(&sub->surface_destroy_listener.link);
4016 free(sub);
4017}
4018
4019static const struct wl_subsurface_interface subsurface_implementation = {
4020 subsurface_destroy,
4021 subsurface_set_position,
4022 subsurface_place_above,
4023 subsurface_place_below,
4024 subsurface_set_sync,
4025 subsurface_set_desync
4026};
4027
4028static struct weston_subsurface *
4029weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4030 struct weston_surface *parent)
4031{
4032 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004033 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004034
Bryce Harringtonde16d892014-11-20 22:21:57 -08004035 sub = zalloc(sizeof *sub);
4036 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004037 return NULL;
4038
Jason Ekstranda7af7042013-10-12 22:38:11 -05004039 wl_list_init(&sub->unused_views);
4040
Jason Ekstranda85118c2013-06-27 20:17:02 -05004041 sub->resource =
4042 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004043 if (!sub->resource) {
4044 free(sub);
4045 return NULL;
4046 }
4047
Jason Ekstranda85118c2013-06-27 20:17:02 -05004048 wl_resource_set_implementation(sub->resource,
4049 &subsurface_implementation,
4050 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004051 weston_subsurface_link_surface(sub, surface);
4052 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004053 weston_surface_state_init(&sub->cached);
4054 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004055 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004056
4057 return sub;
4058}
4059
4060/* Create a dummy subsurface for having the parent itself in its
4061 * sub-surface lists. Makes stacking order manipulation easy.
4062 */
4063static struct weston_subsurface *
4064weston_subsurface_create_for_parent(struct weston_surface *parent)
4065{
4066 struct weston_subsurface *sub;
4067
Bryce Harringtonde16d892014-11-20 22:21:57 -08004068 sub = zalloc(sizeof *sub);
4069 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004070 return NULL;
4071
4072 weston_subsurface_link_surface(sub, parent);
4073 sub->parent = parent;
4074 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4075 wl_list_insert(&parent->subsurface_list_pending,
4076 &sub->parent_link_pending);
4077
4078 return sub;
4079}
4080
4081static void
4082subcompositor_get_subsurface(struct wl_client *client,
4083 struct wl_resource *resource,
4084 uint32_t id,
4085 struct wl_resource *surface_resource,
4086 struct wl_resource *parent_resource)
4087{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004088 struct weston_surface *surface =
4089 wl_resource_get_user_data(surface_resource);
4090 struct weston_surface *parent =
4091 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004092 struct weston_subsurface *sub;
4093 static const char where[] = "get_subsurface: wl_subsurface@";
4094
4095 if (surface == parent) {
4096 wl_resource_post_error(resource,
4097 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4098 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004099 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004100 return;
4101 }
4102
4103 if (weston_surface_to_subsurface(surface)) {
4104 wl_resource_post_error(resource,
4105 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4106 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004107 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004108 return;
4109 }
4110
Pekka Paalanen50b67472014-10-01 15:02:41 +03004111 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4112 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004113 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004114
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004115 if (weston_surface_get_main_surface(parent) == surface) {
4116 wl_resource_post_error(resource,
4117 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4118 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004119 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004120 return;
4121 }
4122
Pekka Paalanene67858b2013-04-25 13:57:42 +03004123 /* make sure the parent is in its own list */
4124 if (wl_list_empty(&parent->subsurface_list)) {
4125 if (!weston_subsurface_create_for_parent(parent)) {
4126 wl_resource_post_no_memory(resource);
4127 return;
4128 }
4129 }
4130
4131 sub = weston_subsurface_create(id, surface, parent);
4132 if (!sub) {
4133 wl_resource_post_no_memory(resource);
4134 return;
4135 }
4136
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004137 surface->committed = subsurface_committed;
4138 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004139 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004140}
4141
4142static void
4143subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4144{
4145 wl_resource_destroy(resource);
4146}
4147
4148static const struct wl_subcompositor_interface subcompositor_interface = {
4149 subcompositor_destroy,
4150 subcompositor_get_subsurface
4151};
4152
4153static void
4154bind_subcompositor(struct wl_client *client,
4155 void *data, uint32_t version, uint32_t id)
4156{
4157 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004158 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004159
Jason Ekstranda85118c2013-06-27 20:17:02 -05004160 resource =
4161 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004162 if (resource == NULL) {
4163 wl_client_post_no_memory(client);
4164 return;
4165 }
4166 wl_resource_set_implementation(resource, &subcompositor_interface,
4167 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004168}
4169
Bryce Harrington0795ece2016-08-30 12:04:26 -07004170/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004171 *
4172 * \param compositor The compositor instance
4173 * \param state The DPMS state the outputs will be set to
4174 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004175static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004176weston_compositor_dpms(struct weston_compositor *compositor,
4177 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004178{
4179 struct weston_output *output;
4180
Bryce Harrington08976ac2016-08-30 12:05:16 -07004181 wl_list_for_each(output, &compositor->output_list, link)
4182 if (output->set_dpms)
4183 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004184}
4185
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004186/** Restores the compositor to active status
4187 *
4188 * \param compositor The compositor instance
4189 *
4190 * If the compositor was in a sleeping mode, all outputs are powered
4191 * back on via DPMS. Otherwise if the compositor was inactive
4192 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4193 * signal will fire.
4194 *
4195 * Restarts the idle timer.
4196 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004197WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004198weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004199{
Neil Roberts8b62e202013-09-30 13:14:47 +01004200 uint32_t old_state = compositor->state;
4201
4202 /* The state needs to be changed before emitting the wake
4203 * signal because that may try to schedule a repaint which
4204 * will not work if the compositor is still sleeping */
4205 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4206
4207 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004208 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004209 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004210 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004211 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004212 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004213 /* fall through */
4214 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004215 wl_event_source_timer_update(compositor->idle_source,
4216 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004217 }
4218}
4219
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004220/** Turns off rendering and frame events for the compositor.
4221 *
4222 * \param compositor The compositor instance
4223 *
4224 * This is used for example to prevent further rendering while the
4225 * compositor is shutting down.
4226 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004227 * Stops the idle timer.
4228 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004229WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004230weston_compositor_offscreen(struct weston_compositor *compositor)
4231{
4232 switch (compositor->state) {
4233 case WESTON_COMPOSITOR_OFFSCREEN:
4234 return;
4235 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004236 default:
4237 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4238 wl_event_source_timer_update(compositor->idle_source, 0);
4239 }
4240}
4241
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004242/** Powers down all attached output devices
4243 *
4244 * \param compositor The compositor instance
4245 *
4246 * Causes rendering to the outputs to cease, and no frame events to be
4247 * sent. Only powers down the outputs if the compositor is not already
4248 * in sleep mode.
4249 *
4250 * Stops the idle timer.
4251 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004252WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004253weston_compositor_sleep(struct weston_compositor *compositor)
4254{
4255 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4256 return;
4257
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004258 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004259 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4260 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4261}
4262
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004263/** Sets compositor to idle mode
4264 *
4265 * \param data The compositor instance
4266 *
4267 * This is called when the idle timer fires. Once the compositor is in
4268 * idle mode it requires a wake action (e.g. via
4269 * weston_compositor_wake()) to restore it. The compositor's
4270 * idle_signal will be triggered when the idle event occurs.
4271 *
4272 * Idleness can be inhibited by setting the compositor's idle_inhibit
4273 * property.
4274 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004275static int
4276idle_handler(void *data)
4277{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004278 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004279
4280 if (compositor->idle_inhibit)
4281 return 1;
4282
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004283 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004284 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004285
4286 return 1;
4287}
4288
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004289WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004290weston_plane_init(struct weston_plane *plane,
4291 struct weston_compositor *ec,
4292 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004293{
4294 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004295 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004296 plane->x = x;
4297 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004298 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004299
4300 /* Init the link so that the call to wl_list_remove() when releasing
4301 * the plane without ever stacking doesn't lead to a crash */
4302 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004303}
4304
4305WL_EXPORT void
4306weston_plane_release(struct weston_plane *plane)
4307{
Xiong Zhang97116532013-10-23 13:58:31 +08004308 struct weston_view *view;
4309
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004310 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004311 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004312
Xiong Zhang97116532013-10-23 13:58:31 +08004313 wl_list_for_each(view, &plane->compositor->view_list, link) {
4314 if (view->plane == plane)
4315 view->plane = NULL;
4316 }
4317
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004318 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004319}
4320
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004321WL_EXPORT void
4322weston_compositor_stack_plane(struct weston_compositor *ec,
4323 struct weston_plane *plane,
4324 struct weston_plane *above)
4325{
4326 if (above)
4327 wl_list_insert(above->link.prev, &plane->link);
4328 else
4329 wl_list_insert(&ec->plane_list, &plane->link);
4330}
4331
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004332static void
4333output_release(struct wl_client *client, struct wl_resource *resource)
4334{
4335 wl_resource_destroy(resource);
4336}
4337
4338static const struct wl_output_interface output_interface = {
4339 output_release,
4340};
4341
4342
Casey Dahlin9074db52012-04-19 22:50:09 -04004343static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004344{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004345 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004346}
4347
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004348static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004349bind_output(struct wl_client *client,
4350 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004351{
Pekka Paalanen05347622017-03-27 12:24:34 +03004352 struct weston_head *head = data;
4353 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004354 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004355 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004356
Jason Ekstranda85118c2013-06-27 20:17:02 -05004357 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004358 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004359 if (resource == NULL) {
4360 wl_client_post_no_memory(client);
4361 return;
4362 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004363
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004364 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004365 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004366 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004367
Pekka Paalanen05347622017-03-27 12:24:34 +03004368 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004369 wl_output_send_geometry(resource,
4370 output->x,
4371 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004372 head->mm_width,
4373 head->mm_height,
4374 head->subpixel,
4375 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004376 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004377 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004378 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004379 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004380
4381 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004382 wl_output_send_mode(resource,
4383 mode->flags,
4384 mode->width,
4385 mode->height,
4386 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004387 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004388
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004389 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004390 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004391}
4392
Pekka Paalanendcac3512017-12-08 14:13:34 +02004393static void
4394weston_head_add_global(struct weston_head *head)
4395{
4396 head->global = wl_global_create(head->compositor->wl_display,
4397 &wl_output_interface, 3,
4398 head, bind_output);
4399}
4400
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004401/** Remove the global wl_output protocol object
4402 *
4403 * \param head The head whose global to remove.
4404 *
4405 * Also orphans the wl_resources for this head (wl_output).
4406 */
4407static void
4408weston_head_remove_global(struct weston_head *head)
4409{
4410 struct wl_resource *resource, *tmp;
4411
4412 if (head->global)
4413 wl_global_destroy(head->global);
4414 head->global = NULL;
4415
4416 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4417 unbind_resource(resource);
4418 wl_resource_set_destructor(resource, NULL);
4419 wl_resource_set_user_data(resource, NULL);
4420 }
4421}
4422
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004423/** Get the backing object of wl_output
4424 *
4425 * \param resource A wl_output protocol object.
4426 * \return The backing object (user data) of a wl_resource representing a
4427 * wl_output protocol object.
4428 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004429WL_EXPORT struct weston_head *
4430weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004431{
4432 assert(wl_resource_instance_of(resource, &wl_output_interface,
4433 &output_interface));
4434
4435 return wl_resource_get_user_data(resource);
4436}
4437
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004438/** Initialize a pre-allocated weston_head
4439 *
4440 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004441 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004442 *
4443 * The head will be safe to attach, detach and release.
4444 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004445 * The name is used in logs, and can be used by compositors as a configuration
4446 * identifier.
4447 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004448 * \memberof weston_head
4449 * \internal
4450 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004451WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004452weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004453{
4454 /* Add some (in)sane defaults which can be used
4455 * for checking if an output was properly configured
4456 */
4457 memset(head, 0, sizeof *head);
4458
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004459 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004460 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004461 wl_list_init(&head->output_link);
4462 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004463 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004464}
4465
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004466/** Idle task for emitting heads_changed_signal */
4467static void
4468weston_compositor_call_heads_changed(void *data)
4469{
4470 struct weston_compositor *compositor = data;
4471
4472 compositor->heads_changed_source = NULL;
4473
4474 wl_signal_emit(&compositor->heads_changed_signal, compositor);
4475}
4476
4477/** Schedule a call on idle to heads_changed callback
4478 *
4479 * \param compositor The Compositor.
4480 *
4481 * \memberof weston_compositor
4482 * \internal
4483 */
4484static void
4485weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4486{
4487 struct wl_event_loop *loop;
4488
4489 if (compositor->heads_changed_source)
4490 return;
4491
4492 loop = wl_display_get_event_loop(compositor->wl_display);
4493 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4494 weston_compositor_call_heads_changed, compositor);
4495}
4496
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004497/** Register a new head
4498 *
4499 * \param compositor The compositor.
4500 * \param head The head to register, must not be already registered.
4501 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004502 * This signals the core that a new head has become available, leading to
4503 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004504 *
4505 * \memberof weston_compositor
4506 * \internal
4507 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004508WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004509weston_compositor_add_head(struct weston_compositor *compositor,
4510 struct weston_head *head)
4511{
4512 assert(wl_list_empty(&head->compositor_link));
4513 assert(head->name);
4514
4515 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4516 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004517 weston_compositor_schedule_heads_changed(compositor);
4518}
4519
4520/** Adds a listener to be called when heads change
4521 *
4522 * \param compositor The compositor.
4523 * \param listener The listener to add.
4524 *
4525 * The listener notify function argument is the \var compositor.
4526 *
4527 * The listener function will be called after heads are added or their
4528 * connection status has changed. Several changes may be accumulated into a
4529 * single call. The user is expected to iterate over the existing heads and
4530 * check their statuses to find out what changed.
4531 *
4532 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4533 * weston_head_is_enabled
4534 * \memberof weston_compositor
4535 */
4536WL_EXPORT void
4537weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4538 struct wl_listener *listener)
4539{
4540 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004541}
4542
4543/** Iterate over available heads
4544 *
4545 * \param compositor The compositor.
4546 * \param item The iterator, or NULL for start.
4547 * \return The next available head in the list.
4548 *
4549 * Returns all available heads, regardless of being connected or enabled.
4550 *
4551 * You can iterate over all heads as follows:
4552 * \code
4553 * struct weston_head *head = NULL;
4554 *
4555 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4556 * ...
4557 * }
4558 * \endcode
4559 *
4560 * If you cause \c iter to be removed from the list, you cannot use it to
4561 * continue iterating. Removing any other item is safe.
4562 *
4563 * \memberof weston_compositor
4564 */
4565WL_EXPORT struct weston_head *
4566weston_compositor_iterate_heads(struct weston_compositor *compositor,
4567 struct weston_head *iter)
4568{
4569 struct wl_list *list = &compositor->head_list;
4570 struct wl_list *node;
4571
4572 assert(compositor);
4573 assert(!iter || iter->compositor == compositor);
4574
4575 if (iter)
4576 node = iter->compositor_link.next;
4577 else
4578 node = list->next;
4579
4580 assert(node);
4581 assert(!iter || node != &iter->compositor_link);
4582
4583 if (node == list)
4584 return NULL;
4585
4586 return container_of(node, struct weston_head, compositor_link);
4587}
4588
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004589/** Iterate over attached heads
4590 *
4591 * \param output The output whose heads to iterate.
4592 * \param item The iterator, or NULL for start.
4593 * \return The next attached head in the list.
4594 *
4595 * Returns all heads currently attached to the output.
4596 *
4597 * You can iterate over all heads as follows:
4598 * \code
4599 * struct weston_head *head = NULL;
4600 *
4601 * while ((head = weston_output_iterate_heads(output, head))) {
4602 * ...
4603 * }
4604 * \endcode
4605 *
4606 * If you cause \c iter to be removed from the list, you cannot use it to
4607 * continue iterating. Removing any other item is safe.
4608 *
4609 * \memberof weston_compositor
4610 */
4611WL_EXPORT struct weston_head *
4612weston_output_iterate_heads(struct weston_output *output,
4613 struct weston_head *iter)
4614{
4615 struct wl_list *list = &output->head_list;
4616 struct wl_list *node;
4617
4618 assert(output);
4619 assert(!iter || iter->output == output);
4620
4621 if (iter)
4622 node = iter->output_link.next;
4623 else
4624 node = list->next;
4625
4626 assert(node);
4627 assert(!iter || node != &iter->output_link);
4628
4629 if (node == list)
4630 return NULL;
4631
4632 return container_of(node, struct weston_head, output_link);
4633}
4634
Pekka Paalanendcac3512017-12-08 14:13:34 +02004635/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004636 *
4637 * \param output The output to attach to.
4638 * \param head The head that is not yet attached.
4639 * \return 0 on success, -1 on failure.
4640 *
4641 * Attaches the given head to the output. All heads of an output are clones
4642 * and share the resolution and timings.
4643 *
4644 * Cloning heads this way uses less resources than creating an output for
4645 * each head, but is not always possible due to environment, driver and hardware
4646 * limitations.
4647 *
4648 * On failure, the head remains unattached. Success of this function does not
4649 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004650 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004651 *
4652 * \memberof weston_output
4653 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004654WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004655weston_output_attach_head(struct weston_output *output,
4656 struct weston_head *head)
4657{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004658 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004659
4660 if (!wl_list_empty(&head->output_link))
4661 return -1;
4662
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004663 if (output->attach_head) {
4664 if (output->attach_head(output, head) < 0)
4665 return -1;
4666 } else if (!wl_list_empty(&output->head_list)) {
4667 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004668 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004669 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004670
4671 head->output = output;
4672 wl_list_insert(output->head_list.prev, &head->output_link);
4673
Pekka Paalanendcac3512017-12-08 14:13:34 +02004674 if (output->enabled) {
4675 weston_head_add_global(head);
4676
4677 head_names = weston_output_create_heads_string(output);
4678 weston_log("Output '%s' updated to have head(s) %s\n",
4679 output->name, head_names);
4680 free(head_names);
4681 }
4682
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004683 return 0;
4684}
4685
4686/** Detach a head from its output
4687 *
4688 * \param head The head to detach.
4689 *
4690 * It is safe to detach a non-attached head.
4691 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004692 * If the head is attached to an enabled output and the output will be left
4693 * with no heads, the output will be disabled.
4694 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004695 * \memberof weston_head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004696 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004697 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004698WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004699weston_head_detach(struct weston_head *head)
4700{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004701 struct weston_output *output = head->output;
4702
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004703 wl_list_remove(&head->output_link);
4704 wl_list_init(&head->output_link);
4705 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004706
4707 if (!output)
4708 return;
4709
4710 if (output->detach_head)
4711 output->detach_head(output, head);
4712
4713 if (output->enabled) {
4714 weston_head_remove_global(head);
4715
4716 if (wl_list_empty(&output->head_list))
4717 weston_output_disable(output);
4718 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004719}
4720
4721/** Destroy a head
4722 *
4723 * \param head The head to be released.
4724 *
4725 * Destroys the head. The caller is responsible for freeing the memory pointed
4726 * to by \c head.
4727 *
4728 * \memberof weston_head
4729 * \internal
4730 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004731WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004732weston_head_release(struct weston_head *head)
4733{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004734 wl_signal_emit(&head->destroy_signal, head);
4735
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004736 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004737
4738 free(head->make);
4739 free(head->model);
4740 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004741 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004742
4743 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004744}
4745
Pekka Paalanene19970f2017-08-28 14:11:02 +03004746static void
4747weston_head_set_device_changed(struct weston_head *head)
4748{
4749 head->device_changed = true;
4750
4751 if (head->compositor)
4752 weston_compositor_schedule_heads_changed(head->compositor);
4753}
4754
4755/** String equal comparison with NULLs being equal */
4756static bool
4757str_null_eq(const char *a, const char *b)
4758{
4759 if (!a && !b)
4760 return true;
4761
4762 if (!!a != !!b)
4763 return false;
4764
4765 return strcmp(a, b) == 0;
4766}
4767
Pekka Paalanen01f60212017-03-24 15:39:24 +02004768/** Store monitor make, model and serial number
4769 *
4770 * \param head The head to modify.
4771 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
4772 * any string, or NULL for none.
4773 * \param model The monitor model or name, or a made-up string, or NULL for
4774 * none.
4775 * \param serialno The monitor serial number, a made-up string, or NULL for
4776 * none.
4777 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004778 * This may set the device_changed flag.
4779 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004780 * \memberof weston_head
4781 * \internal
4782 */
4783WL_EXPORT void
4784weston_head_set_monitor_strings(struct weston_head *head,
4785 const char *make,
4786 const char *model,
4787 const char *serialno)
4788{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004789 if (str_null_eq(head->make, make) &&
4790 str_null_eq(head->model, model) &&
4791 str_null_eq(head->serial_number, serialno))
4792 return;
4793
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004794 free(head->make);
4795 free(head->model);
4796 free(head->serial_number);
4797
4798 head->make = make ? strdup(make) : NULL;
4799 head->model = model ? strdup(model) : NULL;
4800 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004801
4802 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004803}
4804
4805/** Store physical image size
4806 *
4807 * \param head The head to modify.
4808 * \param mm_width Image area width in millimeters.
4809 * \param mm_height Image area height in millimeters.
4810 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004811 * This may set the device_changed flag.
4812 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004813 * \memberof weston_head
4814 * \internal
4815 */
4816WL_EXPORT void
4817weston_head_set_physical_size(struct weston_head *head,
4818 int32_t mm_width, int32_t mm_height)
4819{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004820 if (head->mm_width == mm_width &&
4821 head->mm_height == mm_height)
4822 return;
4823
Pekka Paalanen01f60212017-03-24 15:39:24 +02004824 head->mm_width = mm_width;
4825 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004826
4827 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004828}
4829
4830/** Store monitor sub-pixel layout
4831 *
4832 * \param head The head to modify.
4833 * \param sp Sub-pixel layout. The possible values are:
4834 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
4835 * - WL_OUTPUT_SUBPIXEL_NONE,
4836 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
4837 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
4838 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
4839 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
4840 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004841 * This may set the device_changed flag.
4842 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004843 * \memberof weston_head
4844 * \internal
4845 */
4846WL_EXPORT void
4847weston_head_set_subpixel(struct weston_head *head,
4848 enum wl_output_subpixel sp)
4849{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004850 if (head->subpixel == sp)
4851 return;
4852
Pekka Paalanen01f60212017-03-24 15:39:24 +02004853 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004854
4855 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004856}
4857
4858/** Mark the monitor as internal
4859 *
4860 * This is used for embedded screens, like laptop panels.
4861 *
4862 * \param head The head to mark as internal.
4863 *
4864 * By default a head is external. The type is often inferred from the physical
4865 * connector type.
4866 *
4867 * \memberof weston_head
4868 * \internal
4869 */
4870WL_EXPORT void
4871weston_head_set_internal(struct weston_head *head)
4872{
4873 head->connection_internal = true;
4874}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004875
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004876/** Store connector status
4877 *
4878 * \param head The head to modify.
4879 * \param connected Whether the head is connected.
4880 *
4881 * Connectors are created as disconnected. This function can be used to
4882 * set the connector status.
4883 *
4884 * The status should be set to true when a physical connector is connected to
4885 * a video sink device like a monitor and to false when the connector is
4886 * disconnected. For nested backends, the connection status should reflect the
4887 * connection to the parent display server.
4888 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004889 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03004890 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004891 *
4892 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004893 * \memberof weston_head
4894 * \internal
4895 */
4896WL_EXPORT void
4897weston_head_set_connection_status(struct weston_head *head, bool connected)
4898{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004899 if (head->connected == connected)
4900 return;
4901
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004902 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004903
Pekka Paalanene19970f2017-08-28 14:11:02 +03004904 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004905}
4906
4907/** Is the head currently connected?
4908 *
4909 * \param head The head to query.
4910 * \return Connection status.
4911 *
4912 * Returns true if the head is physically connected to a monitor, or in
4913 * case of a nested backend returns true when there is a connection to the
4914 * parent display server.
4915 *
4916 * This is independent from the head being enabled.
4917 *
4918 * \sa weston_head_is_enabled
4919 * \memberof weston_head
4920 */
4921WL_EXPORT bool
4922weston_head_is_connected(struct weston_head *head)
4923{
4924 return head->connected;
4925}
4926
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02004927/** Is the head currently enabled?
4928 *
4929 * \param head The head to query.
4930 * \return Video status.
4931 *
4932 * Returns true if the head is currently transmitting a video stream.
4933 *
4934 * This is independent of the head being connected.
4935 *
4936 * \sa weston_head_is_connected
4937 * \memberof weston_head
4938 */
4939WL_EXPORT bool
4940weston_head_is_enabled(struct weston_head *head)
4941{
4942 if (!head->output)
4943 return false;
4944
4945 return head->output->enabled;
4946}
4947
Pekka Paalanene19970f2017-08-28 14:11:02 +03004948/** Has the device information changed?
4949 *
4950 * \param head The head to query.
4951 * \return True if the device information has changed since last reset.
4952 *
4953 * The information about the connected display device, e.g. a monitor, may
4954 * change without being disconnected in between. Changing information
4955 * causes a call to the heads_changed hook.
4956 *
4957 * The information includes make, model, serial number, physical size,
4958 * and sub-pixel type. The connection status is also included.
4959 *
4960 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
4961 * \memberof weston_head
4962 */
4963WL_EXPORT bool
4964weston_head_is_device_changed(struct weston_head *head)
4965{
4966 return head->device_changed;
4967}
4968
4969/** Acknowledge device information change
4970 *
4971 * \param head The head to acknowledge.
4972 *
4973 * Clears the device changed flag on this head. When a compositor has processed
4974 * device information, it should call this to be able to notice further
4975 * changes.
4976 *
4977 * \sa weston_head_is_device_changed
4978 * \memberof weston_head
4979 */
4980WL_EXPORT void
4981weston_head_reset_device_changed(struct weston_head *head)
4982{
4983 head->device_changed = false;
4984}
4985
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004986/** Get the name of a head
4987 *
4988 * \param head The head to query.
4989 * \return The head's name, not NULL.
4990 *
4991 * The name depends on the backend. The DRM backend uses connector names,
4992 * other backends may use hardcoded names or user-given names.
4993 */
4994WL_EXPORT const char *
4995weston_head_get_name(struct weston_head *head)
4996{
4997 return head->name;
4998}
4999
5000/** Get the output the head is attached to
5001 *
5002 * \param head The head to query.
5003 * \return The output the head is attached to, or NULL if detached.
5004 */
5005WL_EXPORT struct weston_output *
5006weston_head_get_output(struct weston_head *head)
5007{
5008 return head->output;
5009}
5010
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005011/** Add destroy callback for a head
5012 *
5013 * \param head The head to watch for.
5014 * \param listener The listener to add. The \c notify member must be set.
5015 *
5016 * Heads may get destroyed for various reasons by the backends. If a head is
5017 * attached to an output, the compositor should listen for head destruction
5018 * and reconfigure or destroy the output if necessary.
5019 *
5020 * The destroy callbacks will be called on weston_head destruction before any
5021 * automatic detaching from an associated weston_output and before any
5022 * weston_head information is lost.
5023 *
5024 * The \c data argument to the notify callback is the weston_head being
5025 * destroyed.
5026 */
5027WL_EXPORT void
5028weston_head_add_destroy_listener(struct weston_head *head,
5029 struct wl_listener *listener)
5030{
5031 wl_signal_add(&head->destroy_signal, listener);
5032}
5033
5034/** Look up destroy listener for a head
5035 *
5036 * \param head The head to query.
5037 * \param notify The notify function used used for the added destroy listener.
5038 * \return The listener, or NULL if not found.
5039 *
5040 * This looks up the previously added destroy listener struct based on the
5041 * notify function it has. The listener can be used to access user data
5042 * through \c container_of().
5043 *
5044 * \sa wl_signal_get()
5045 */
5046WL_EXPORT struct wl_listener *
5047weston_head_get_destroy_listener(struct weston_head *head,
5048 wl_notify_func_t notify)
5049{
5050 return wl_signal_get(&head->destroy_signal, notify);
5051}
5052
David Fort0de859e2016-05-27 23:22:57 +02005053/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005054static void
David Fort0de859e2016-05-27 23:22:57 +02005055weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5056 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005057{
5058 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005059 bool start_resizing = false;
5060
5061 if (!delta_width)
5062 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005063
5064 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005065 if (output == resized_output) {
5066 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005067 continue;
5068 }
5069
David Fort0de859e2016-05-27 23:22:57 +02005070 if (start_resizing) {
5071 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005072 output->dirty = 1;
5073 }
5074 }
5075}
5076
Pekka Paalanend72bad22017-03-29 17:01:41 +03005077static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005078weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005079{
Scott Moreau850ca422012-05-21 15:21:25 -06005080 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005081
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005082 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005083 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005084
Scott Moreauccbf29d2012-02-22 14:21:41 -07005085 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005086 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005087 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005088 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005089 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005090 weston_matrix_scale(&output->matrix, magnification,
5091 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005092 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005093
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005094 switch (output->transform) {
5095 case WL_OUTPUT_TRANSFORM_FLIPPED:
5096 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5097 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5098 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5099 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5100 weston_matrix_scale(&output->matrix, -1, 1, 1);
5101 break;
5102 }
5103
5104 switch (output->transform) {
5105 default:
5106 case WL_OUTPUT_TRANSFORM_NORMAL:
5107 case WL_OUTPUT_TRANSFORM_FLIPPED:
5108 break;
5109 case WL_OUTPUT_TRANSFORM_90:
5110 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5111 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5112 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5113 break;
5114 case WL_OUTPUT_TRANSFORM_180:
5115 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5116 weston_matrix_translate(&output->matrix,
5117 -output->width, -output->height, 0);
5118 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5119 break;
5120 case WL_OUTPUT_TRANSFORM_270:
5121 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5122 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5123 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5124 break;
5125 }
5126
5127 if (output->current_scale != 1)
5128 weston_matrix_scale(&output->matrix,
5129 output->current_scale,
5130 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005131
Scott Moreauccbf29d2012-02-22 14:21:41 -07005132 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005133
5134 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005135}
5136
Scott Moreau1bad5db2012-08-18 01:04:05 -06005137static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005138weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005139{
5140 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005141 output->native_scale = scale;
5142 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005143
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005144 convert_size_by_transform_scale(&output->width, &output->height,
5145 output->current_mode->width,
5146 output->current_mode->height,
5147 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005148}
5149
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005150static void
5151weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005152{
5153 output->x = x;
5154 output->y = y;
5155
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005156 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005157 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005158
5159 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005160 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005161 output->width,
5162 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005163}
5164
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005165WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005166weston_output_move(struct weston_output *output, int x, int y)
5167{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005168 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005169 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005170 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005171
5172 output->move_x = x - output->x;
5173 output->move_y = y - output->y;
5174
5175 if (output->move_x == 0 && output->move_y == 0)
5176 return;
5177
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005178 weston_output_init_geometry(output, x, y);
5179
5180 output->dirty = 1;
5181
5182 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005183 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005184
5185 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005186 wl_list_for_each(head, &output->head_list, output_link) {
5187 wl_resource_for_each(resource, &head->resource_list) {
5188 wl_output_send_geometry(resource,
5189 output->x,
5190 output->y,
5191 head->mm_width,
5192 head->mm_height,
5193 head->subpixel,
5194 head->make,
5195 head->model,
5196 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005197
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005198 ver = wl_resource_get_version(resource);
5199 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5200 wl_output_send_done(resource);
5201 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005202 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005203}
5204
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005205/** Signal that a pending output is taken into use.
5206 *
5207 * Removes the output from the pending list and adds it to the compositor's
5208 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005209 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005210 * The output gets an internal ID assigned, and the wl_output global is
5211 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005212 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005213 * \param compositor The compositor instance.
5214 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005215 *
5216 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005217 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005218static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005219weston_compositor_add_output(struct weston_compositor *compositor,
5220 struct weston_output *output)
5221{
Armin Krezoviće5403842016-08-05 15:28:29 +02005222 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005223 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005224
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005225 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005226
5227 /* Verify we haven't reached the limit of 32 available output IDs */
5228 assert(ffs(~compositor->output_id_pool) > 0);
5229
5230 /* Invert the output id pool and look for the lowest numbered
5231 * switch (the least significant bit). Take that bit's position
5232 * as our ID, and mark it used in the compositor's output_id_pool.
5233 */
5234 output->id = ffs(~compositor->output_id_pool) - 1;
5235 compositor->output_id_pool |= 1u << output->id;
5236
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005237 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005238 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005239 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005240
Pekka Paalanendcac3512017-12-08 14:13:34 +02005241 wl_list_for_each(head, &output->head_list, output_link)
5242 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005243
Giulio Camuffob1147152015-05-06 21:41:57 +03005244 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005245
5246 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5247 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005248}
5249
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005250/** Transform device coordinates into global coordinates
5251 *
5252 * \param device_x[in] X coordinate in device units.
5253 * \param device_y[in] Y coordinate in device units.
5254 * \param x[out] X coordinate in the global space.
5255 * \param y[out] Y coordinate in the global space.
5256 *
5257 * Transforms coordinates from the device coordinate space
5258 * (physical pixel units) to the global coordinate space (logical pixel units).
5259 * This takes into account output transform and scale.
5260 *
5261 * \memberof weston_output
5262 * \internal
5263 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005264WL_EXPORT void
5265weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005266 double device_x, double device_y,
5267 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005268{
Derek Foreman0f679412014-10-02 13:41:17 -05005269 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005270 device_x,
5271 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005272 0.0,
5273 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005274
Derek Foreman67a18b92015-03-24 11:36:14 -05005275 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005276
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005277 *x = p.f[0] / p.f[3];
5278 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005279}
5280
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005281/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005282 *
5283 * \param output The weston_output object that is being removed.
5284 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005285 * The following happens:
5286 *
5287 * - The output assignments of all views in the current scenegraph are
5288 * recomputed.
5289 *
5290 * - Presentation feedback is discarded.
5291 *
5292 * - Compositor is notified that outputs were changed and
5293 * applies the necessary changes to re-layout outputs.
5294 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005295 * - The output is put back in the pending outputs list.
5296 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005297 * - Signal is emitted to notify all users of the weston_output
5298 * object that the output is being destroyed.
5299 *
5300 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005301 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005302 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005303 * - The output's internal ID is released.
5304 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005305 * \memberof weston_output
5306 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005307 */
5308static void
5309weston_compositor_remove_output(struct weston_output *output)
5310{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005311 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005312 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005313 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005314
5315 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005316 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005317
Pekka Paalanenbccda712017-03-29 16:16:04 +03005318 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005319 if (view->output_mask & (1u << output->id))
5320 weston_view_assign_output(view);
5321 }
5322
5323 weston_presentation_feedback_discard_list(&output->feedback_list);
5324
Pekka Paalanenbccda712017-03-29 16:16:04 +03005325 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005326
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005327 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005328 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005329 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005330
Pekka Paalanenbccda712017-03-29 16:16:04 +03005331 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005332 wl_signal_emit(&output->destroy_signal, output);
5333
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005334 wl_list_for_each(head, &output->head_list, output_link)
5335 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005336
5337 compositor->output_id_pool &= ~(1u << output->id);
5338 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005339}
5340
5341/** Sets the output scale for a given output.
5342 *
5343 * \param output The weston_output object that the scale is set for.
5344 * \param scale Scale factor for the given output.
5345 *
5346 * It only supports setting scale for an output that
5347 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005348 *
5349 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005350 */
5351WL_EXPORT void
5352weston_output_set_scale(struct weston_output *output,
5353 int32_t scale)
5354{
5355 /* We can only set scale on a disabled output */
5356 assert(!output->enabled);
5357
5358 /* We only want to set scale once */
5359 assert(!output->scale);
5360
5361 output->scale = scale;
5362}
5363
5364/** Sets the output transform for a given output.
5365 *
5366 * \param output The weston_output object that the transform is set for.
5367 * \param transform Transform value for the given output.
5368 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005369 * Refer to wl_output::transform section located at
5370 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5371 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005372 *
5373 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005374 */
5375WL_EXPORT void
5376weston_output_set_transform(struct weston_output *output,
5377 uint32_t transform)
5378{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005379 struct weston_pointer_motion_event ev;
5380 struct wl_resource *resource;
5381 struct weston_seat *seat;
5382 pixman_region32_t old_region;
5383 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005384 struct weston_head *head;
5385 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005386
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005387 if (!output->enabled && output->transform == UINT32_MAX) {
5388 output->transform = transform;
5389 return;
5390 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005391
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005392 weston_output_transform_scale_init(output, transform, output->scale);
5393
5394 pixman_region32_init(&old_region);
5395 pixman_region32_copy(&old_region, &output->region);
5396
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005397 weston_output_init_geometry(output, output->x, output->y);
5398
5399 output->dirty = 1;
5400
5401 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005402 wl_list_for_each(head, &output->head_list, output_link) {
5403 wl_resource_for_each(resource, &head->resource_list) {
5404 wl_output_send_geometry(resource,
5405 output->x,
5406 output->y,
5407 head->mm_width,
5408 head->mm_height,
5409 head->subpixel,
5410 head->make,
5411 head->model,
5412 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005413
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005414 ver = wl_resource_get_version(resource);
5415 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5416 wl_output_send_done(resource);
5417 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005418 }
5419
5420 /* we must ensure that pointers are inside output, otherwise they disappear */
5421 mid_x = output->x + output->width / 2;
5422 mid_y = output->y + output->height / 2;
5423
5424 ev.mask = WESTON_POINTER_MOTION_ABS;
5425 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5426 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5427
5428 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5429 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5430
5431 if (pointer && pixman_region32_contains_point(&old_region,
5432 wl_fixed_to_int(pointer->x),
5433 wl_fixed_to_int(pointer->y),
5434 NULL))
5435 weston_pointer_move(pointer, &ev);
5436 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005437}
5438
5439/** Initializes a weston_output object with enough data so
5440 ** an output can be configured.
5441 *
5442 * \param output The weston_output object to initialize
5443 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005444 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005445 *
5446 * Sets initial values for fields that are expected to be
5447 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005448 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005449 * The name is used in logs, and can be used by compositors as a configuration
5450 * identifier.
5451 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005452 * \memberof weston_output
5453 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005454 */
5455WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005456weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005457 struct weston_compositor *compositor,
5458 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005459{
5460 output->compositor = compositor;
5461 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005462 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005463 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005464 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005465 output->enabled = false;
5466
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005467 wl_list_init(&output->head_list);
5468
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005469 /* Add some (in)sane defaults which can be used
5470 * for checking if an output was properly configured
5471 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005472 output->scale = 0;
5473 /* Can't use -1 on uint32_t and 0 is valid enum value */
5474 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005475
5476 pixman_region32_init(&output->previous_damage);
5477 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005478 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005479}
5480
5481/** Adds weston_output object to pending output list.
5482 *
5483 * \param output The weston_output object to add
5484 * \param compositor The compositor instance.
5485 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005486 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005487 *
5488 * \memberof weston_output
5489 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005490 */
5491WL_EXPORT void
5492weston_compositor_add_pending_output(struct weston_output *output,
5493 struct weston_compositor *compositor)
5494{
Pekka Paalanene952a012017-03-29 17:14:00 +03005495 assert(output->disable);
5496 assert(output->enable);
5497
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005498 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005499 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005500}
5501
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005502/** Create a string with the attached heads' names.
5503 *
5504 * The string must be free()'d.
5505 */
5506static char *
5507weston_output_create_heads_string(struct weston_output *output)
5508{
5509 FILE *fp;
5510 char *str = NULL;
5511 size_t size = 0;
5512 struct weston_head *head;
5513 const char *sep = "";
5514
5515 fp = open_memstream(&str, &size);
5516 if (!fp)
5517 return NULL;
5518
5519 wl_list_for_each(head, &output->head_list, output_link) {
5520 fprintf(fp, "%s%s", sep, head->name);
5521 sep = ", ";
5522 }
5523 fclose(fp);
5524
5525 return str;
5526}
5527
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005528/** Constructs a weston_output object that can be used by the compositor.
5529 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005530 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005531 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005532 *
5533 * Output coordinates are calculated and each new output is by default
5534 * assigned to the right of previous one.
5535 *
5536 * Sets up the transformation, zoom, and geometry of the output using
5537 * the properties that need to be configured by the compositor.
5538 *
5539 * Establishes a repaint timer for the output with the relevant display
5540 * object's event loop. See output_repaint_timer_handler().
5541 *
5542 * The output is assigned an ID. Weston can support up to 32 distinct
5543 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5544 * is referred to and used to find the first available ID number, and
5545 * then this ID is marked as used in output_id_pool.
5546 *
5547 * The output is also assigned a Wayland global with the wl_output
5548 * external interface.
5549 *
5550 * Backend specific function is called to set up the output output.
5551 *
5552 * Output is added to the compositor's output list
5553 *
5554 * If the backend specific function fails, the weston_output object
5555 * is returned to a state it was before calling this function and
5556 * is added to the compositor's pending_output_list in case it needs
5557 * to be reconfigured or just so it can be destroyed at shutdown.
5558 *
5559 * 0 is returned on success, -1 on failure.
5560 */
5561WL_EXPORT int
5562weston_output_enable(struct weston_output *output)
5563{
Armin Krezović782f5df2016-09-30 14:11:11 +02005564 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005565 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005566 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005567 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005568 int x = 0, y = 0;
5569
Pekka Paalanencc201e42017-03-30 15:11:25 +03005570 if (output->enabled) {
5571 weston_log("Error: attempt to enable an enabled output '%s'\n",
5572 output->name);
5573 return -1;
5574 }
5575
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005576 if (wl_list_empty(&output->head_list)) {
5577 weston_log("Error: cannot enable output '%s' without heads.\n",
5578 output->name);
5579 return -1;
5580 }
5581
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005582 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5583 weston_log("Error: no video mode for output '%s'.\n",
5584 output->name);
5585 return -1;
5586 }
5587
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005588 wl_list_for_each(head, &output->head_list, output_link) {
5589 assert(head->make);
5590 assert(head->model);
5591 }
5592
Armin Krezović782f5df2016-09-30 14:11:11 +02005593 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005594 struct weston_output, link);
5595
Armin Krezović782f5df2016-09-30 14:11:11 +02005596 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005597 x = iterator->x + iterator->width;
5598
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005599 /* Make sure the scale is set up */
5600 assert(output->scale);
5601
5602 /* Make sure we have a transform set */
5603 assert(output->transform != UINT32_MAX);
5604
Armin Krezović782f5df2016-09-30 14:11:11 +02005605 output->x = x;
5606 output->y = y;
5607 output->dirty = 1;
5608 output->original_scale = output->scale;
5609
5610 weston_output_transform_scale_init(output, output->transform, output->scale);
5611 weston_output_init_zoom(output);
5612
5613 weston_output_init_geometry(output, x, y);
5614 weston_output_damage(output);
5615
5616 wl_signal_init(&output->frame_signal);
5617 wl_signal_init(&output->destroy_signal);
5618 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005619 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005620
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005621 /* Enable the output (set up the crtc or create a
5622 * window representing the output, set up the
5623 * renderer, etc)
5624 */
5625 if (output->enable(output) < 0) {
5626 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005627 return -1;
5628 }
5629
5630 weston_compositor_add_output(output->compositor, output);
5631
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005632 head_names = weston_output_create_heads_string(output);
5633 weston_log("Output '%s' enabled with head(s) %s\n",
5634 output->name, head_names);
5635 free(head_names);
5636
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005637 return 0;
5638}
5639
5640/** Converts a weston_output object to a pending output state, so it
5641 ** can be configured again or destroyed.
5642 *
5643 * \param output The weston_output object that needs to be disabled.
5644 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005645 * Calls a backend specific function to disable an output, in case
5646 * such function exists.
5647 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005648 * The backend specific disable function may choose to postpone the disabling
5649 * by returning a negative value, in which case this function returns early.
5650 * In that case the backend will guarantee the output will be disabled soon
5651 * by the backend calling this function again. One must not attempt to re-enable
5652 * the output until that happens.
5653 *
5654 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005655 * from weston's output_list (see weston_compositor_remove_output())
5656 * and is returned to a state it was before weston_output_enable()
5657 * was ran (see weston_output_enable_undo()).
5658 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005659 * See weston_output_init() for more information on the
5660 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005661 *
5662 * If the output has never been enabled yet, this function can still be
5663 * called to ensure that the output is actually turned off rather than left
5664 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005665 */
5666WL_EXPORT void
5667weston_output_disable(struct weston_output *output)
5668{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005669 /* Should we rename this? */
5670 output->destroying = 1;
5671
Pekka Paalanenc65df642017-03-29 15:45:46 +03005672 /* Disable is called unconditionally also for not-enabled outputs,
5673 * because at compositor start-up, if there is an output that is
5674 * already on but the compositor wants to turn it off, we have to
5675 * forward the turn-off to the backend so it knows to do it.
5676 * The backend cannot initially turn off everything, because it
5677 * would cause unnecessary mode-sets for all outputs the compositor
5678 * wants to be on.
5679 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005680 if (output->disable(output) < 0)
5681 return;
5682
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005683 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005684 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005685
5686 output->destroying = 0;
5687}
5688
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005689/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005690 *
5691 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005692 *
5693 * If there are new or changed heads, calls the heads_changed hook and
5694 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005695 */
5696WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005697weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005698{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005699 if (compositor->heads_changed_source) {
5700 wl_event_source_remove(compositor->heads_changed_source);
5701 weston_compositor_call_heads_changed(compositor);
5702 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005703}
5704
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005705/** Add destroy callback for an output
5706 *
5707 * \param output The output to watch.
5708 * \param listener The listener to add. The \c notify member must be set.
5709 *
5710 * The listener callback will be called when user destroys an output. This
5711 * may be delayed by a backend in some cases. The main purpose of the
5712 * listener is to allow hooking up custom data to the output. The custom data
5713 * can be fetched via weston_output_get_destroy_listener() followed by
5714 * container_of().
5715 *
5716 * The \c data argument to the notify callback is the weston_output being
5717 * destroyed.
5718 *
5719 * @note This is for the final destruction of an output, not when it gets
5720 * disabled. If you want to keep track of enabled outputs, this is not it.
5721 */
5722WL_EXPORT void
5723weston_output_add_destroy_listener(struct weston_output *output,
5724 struct wl_listener *listener)
5725{
5726 wl_signal_add(&output->user_destroy_signal, listener);
5727}
5728
5729/** Look up destroy listener for an output
5730 *
5731 * \param output The output to query.
5732 * \param notify The notify function used used for the added destroy listener.
5733 * \return The listener, or NULL if not found.
5734 *
5735 * This looks up the previously added destroy listener struct based on the
5736 * notify function it has. The listener can be used to access user data
5737 * through \c container_of().
5738 *
5739 * \sa wl_signal_get() weston_output_add_destroy_listener()
5740 */
5741WL_EXPORT struct wl_listener *
5742weston_output_get_destroy_listener(struct weston_output *output,
5743 wl_notify_func_t notify)
5744{
5745 return wl_signal_get(&output->user_destroy_signal, notify);
5746}
5747
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005748/** Uninitialize an output
5749 *
5750 * Removes the output from the list of enabled outputs if necessary, but
5751 * does not call the backend's output disable function. The output will no
5752 * longer be in the list of pending outputs either.
5753 *
5754 * All fields of weston_output become uninitialized, i.e. should not be used
5755 * anymore. The caller can free the memory after this.
5756 *
5757 * \memberof weston_output
5758 * \internal
5759 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005760WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005761weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005762{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005763 struct weston_head *head, *tmp;
5764
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005765 output->destroying = 1;
5766
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005767 wl_signal_emit(&output->user_destroy_signal, output);
5768
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03005769 if (output->idle_repaint_source)
5770 wl_event_source_remove(output->idle_repaint_source);
5771
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005772 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005773 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005774
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005775 pixman_region32_fini(&output->region);
5776 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005777 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005778
5779 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
5780 weston_head_detach(head);
5781
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005782 free(output->name);
5783}
5784
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005785/** Find an output by its given name
5786 *
5787 * \param compositor The compositor to search in.
5788 * \param name The output name to search for.
5789 * \return An existing output with the given name, or NULL if not found.
5790 *
5791 * \memberof weston_compositor
5792 */
5793WL_EXPORT struct weston_output *
5794weston_compositor_find_output_by_name(struct weston_compositor *compositor,
5795 const char *name)
5796{
5797 struct weston_output *output;
5798
5799 wl_list_for_each(output, &compositor->output_list, link)
5800 if (strcmp(output->name, name) == 0)
5801 return output;
5802
5803 wl_list_for_each(output, &compositor->pending_output_list, link)
5804 if (strcmp(output->name, name) == 0)
5805 return output;
5806
5807 return NULL;
5808}
5809
5810/** Create a named output
5811 *
5812 * \param compositor The compositor.
5813 * \param name The name for the output.
5814 * \return A new \c weston_output, or NULL on failure.
5815 *
5816 * This creates a new weston_output that starts with no heads attached.
5817 *
5818 * An output must be configured and it must have at least one head before
5819 * it can be enabled.
5820 *
5821 * \memberof weston_compositor
5822 */
5823WL_EXPORT struct weston_output *
5824weston_compositor_create_output(struct weston_compositor *compositor,
5825 const char *name)
5826{
5827 assert(compositor->backend->create_output);
5828
5829 if (weston_compositor_find_output_by_name(compositor, name)) {
5830 weston_log("Warning: attempted to create an output with a "
5831 "duplicate name '%s'.\n", name);
5832 return NULL;
5833 }
5834
5835 return compositor->backend->create_output(compositor, name);
5836}
5837
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005838/** Create an output for an unused head
5839 *
5840 * \param compositor The compositor.
5841 * \param head The head to attach to the output.
5842 * \return A new \c weston_output, or NULL on failure.
5843 *
5844 * This creates a new weston_output that starts with the given head attached.
5845 * The output inherits the name of the head. The head must not be already
5846 * attached to another output.
5847 *
5848 * An output must be configured before it can be enabled.
5849 *
5850 * \memberof weston_compositor
5851 */
5852WL_EXPORT struct weston_output *
5853weston_compositor_create_output_with_head(struct weston_compositor *compositor,
5854 struct weston_head *head)
5855{
5856 struct weston_output *output;
5857
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005858 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005859 if (!output)
5860 return NULL;
5861
5862 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03005863 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005864 return NULL;
5865 }
5866
5867 return output;
5868}
5869
5870/** Destroy an output
5871 *
5872 * \param output The output to destroy.
5873 *
5874 * The heads attached to the given output are detached and become unused again.
5875 *
5876 * It is not necessary to explicitly destroy all outputs at compositor exit.
5877 * weston_compositor_destroy() will automatically destroy any remaining
5878 * outputs.
5879 *
5880 * \memberof weston_output
5881 */
5882WL_EXPORT void
5883weston_output_destroy(struct weston_output *output)
5884{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005885 output->destroy(output);
5886}
5887
Pekka Paalanencf0a4762017-04-04 16:36:07 +03005888/** When you need a head...
5889 *
5890 * This function is a hack, used until all code has been converted to become
5891 * multi-head aware.
5892 *
5893 * \param output The weston_output whose head to get.
5894 * \return The first head in the output's list.
5895 */
5896WL_EXPORT struct weston_head *
5897weston_output_get_first_head(struct weston_output *output)
5898{
5899 if (wl_list_empty(&output->head_list))
5900 return NULL;
5901
5902 return container_of(output->head_list.next,
5903 struct weston_head, output_link);
5904}
5905
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01005906static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005907destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005908{
Jonny Lamb74130762013-11-26 18:19:46 +01005909 struct weston_surface *surface =
5910 wl_resource_get_user_data(resource);
5911
Pekka Paalanen4826f872016-04-22 14:14:38 +03005912 if (!surface)
5913 return;
5914
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005915 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02005916 surface->pending.buffer_viewport.buffer.src_width =
5917 wl_fixed_from_int(-1);
5918 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005919 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005920}
5921
5922static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005923viewport_destroy(struct wl_client *client,
5924 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005925{
5926 wl_resource_destroy(resource);
5927}
5928
5929static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005930viewport_set_source(struct wl_client *client,
5931 struct wl_resource *resource,
5932 wl_fixed_t src_x,
5933 wl_fixed_t src_y,
5934 wl_fixed_t src_width,
5935 wl_fixed_t src_height)
5936{
5937 struct weston_surface *surface =
5938 wl_resource_get_user_data(resource);
5939
Pekka Paalanen4826f872016-04-22 14:14:38 +03005940 if (!surface) {
5941 wl_resource_post_error(resource,
5942 WP_VIEWPORT_ERROR_NO_SURFACE,
5943 "wl_surface for this viewport is no longer exists");
5944 return;
5945 }
5946
5947 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03005948 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005949
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005950 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03005951 src_height == wl_fixed_from_int(-1) &&
5952 src_x == wl_fixed_from_int(-1) &&
5953 src_y == wl_fixed_from_int(-1)) {
5954 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005955 surface->pending.buffer_viewport.buffer.src_width =
5956 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005957 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005958 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005959 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005960
Pekka Paalanen201769a2016-04-26 14:42:11 +03005961 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005962 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005963 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03005964 "wl_surface@%d viewport source "
5965 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
5966 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005967 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03005968 wl_fixed_to_double(src_height),
5969 wl_fixed_to_double(src_x),
5970 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005971 return;
5972 }
5973
5974 surface->pending.buffer_viewport.buffer.src_x = src_x;
5975 surface->pending.buffer_viewport.buffer.src_y = src_y;
5976 surface->pending.buffer_viewport.buffer.src_width = src_width;
5977 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005978 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005979}
5980
5981static void
5982viewport_set_destination(struct wl_client *client,
5983 struct wl_resource *resource,
5984 int32_t dst_width,
5985 int32_t dst_height)
5986{
5987 struct weston_surface *surface =
5988 wl_resource_get_user_data(resource);
5989
Pekka Paalanen4826f872016-04-22 14:14:38 +03005990 if (!surface) {
5991 wl_resource_post_error(resource,
5992 WP_VIEWPORT_ERROR_NO_SURFACE,
5993 "wl_surface for this viewport no longer exists");
5994 return;
5995 }
5996
5997 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005998
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005999 if (dst_width == -1 && dst_height == -1) {
6000 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006001 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006002 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006003 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006004 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006005
6006 if (dst_width <= 0 || dst_height <= 0) {
6007 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006008 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006009 "destination size must be positive (%dx%d)",
6010 dst_width, dst_height);
6011 return;
6012 }
6013
6014 surface->pending.buffer_viewport.surface.width = dst_width;
6015 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006016 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006017}
6018
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006019static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006020 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006021 viewport_set_source,
6022 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006023};
6024
6025static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006026viewporter_destroy(struct wl_client *client,
6027 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006028{
6029 wl_resource_destroy(resource);
6030}
6031
6032static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006033viewporter_get_viewport(struct wl_client *client,
6034 struct wl_resource *viewporter,
6035 uint32_t id,
6036 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006037{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006038 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006039 struct weston_surface *surface =
6040 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006041 struct wl_resource *resource;
6042
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006043 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006044 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006045 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006046 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006047 return;
6048 }
6049
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006050 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006051 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006052 if (resource == NULL) {
6053 wl_client_post_no_memory(client);
6054 return;
6055 }
6056
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006057 wl_resource_set_implementation(resource, &viewport_interface,
6058 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006059
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006060 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006061}
6062
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006063static const struct wp_viewporter_interface viewporter_interface = {
6064 viewporter_destroy,
6065 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006066};
6067
6068static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006069bind_viewporter(struct wl_client *client,
6070 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006071{
6072 struct wl_resource *resource;
6073
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006074 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006075 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006076 if (resource == NULL) {
6077 wl_client_post_no_memory(client);
6078 return;
6079 }
6080
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006081 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006082 NULL, NULL);
6083}
6084
6085static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006086destroy_presentation_feedback(struct wl_resource *feedback_resource)
6087{
6088 struct weston_presentation_feedback *feedback;
6089
6090 feedback = wl_resource_get_user_data(feedback_resource);
6091
6092 wl_list_remove(&feedback->link);
6093 free(feedback);
6094}
6095
6096static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006097presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6098{
6099 wl_resource_destroy(resource);
6100}
6101
6102static void
6103presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006104 struct wl_resource *presentation_resource,
6105 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006106 uint32_t callback)
6107{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006108 struct weston_surface *surface;
6109 struct weston_presentation_feedback *feedback;
6110
6111 surface = wl_resource_get_user_data(surface_resource);
6112
Bryce Harringtonde16d892014-11-20 22:21:57 -08006113 feedback = zalloc(sizeof *feedback);
6114 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006115 goto err_calloc;
6116
6117 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006118 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006119 1, callback);
6120 if (!feedback->resource)
6121 goto err_create;
6122
6123 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6124 destroy_presentation_feedback);
6125
6126 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6127
6128 return;
6129
6130err_create:
6131 free(feedback);
6132
6133err_calloc:
6134 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006135}
6136
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006137static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006138 presentation_destroy,
6139 presentation_feedback
6140};
6141
6142static void
6143bind_presentation(struct wl_client *client,
6144 void *data, uint32_t version, uint32_t id)
6145{
6146 struct weston_compositor *compositor = data;
6147 struct wl_resource *resource;
6148
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006149 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006150 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006151 if (resource == NULL) {
6152 wl_client_post_no_memory(client);
6153 return;
6154 }
6155
6156 wl_resource_set_implementation(resource, &presentation_implementation,
6157 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006158 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006159}
6160
6161static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006162compositor_bind(struct wl_client *client,
6163 void *data, uint32_t version, uint32_t id)
6164{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006165 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006166 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006167
Jason Ekstranda85118c2013-06-27 20:17:02 -05006168 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006169 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006170 if (resource == NULL) {
6171 wl_client_post_no_memory(client);
6172 return;
6173 }
6174
6175 wl_resource_set_implementation(resource, &compositor_interface,
6176 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006177}
6178
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006179WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006180weston_environment_get_fd(const char *env)
6181{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006182 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006183 int fd, flags;
6184
6185 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006186 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006187 return -1;
6188
6189 flags = fcntl(fd, F_GETFD);
6190 if (flags == -1)
6191 return -1;
6192
6193 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6194 unsetenv(env);
6195
6196 return fd;
6197}
6198
Pekka Paalanenb5026542014-11-12 15:09:24 +02006199static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006200timeline_key_binding_handler(struct weston_keyboard *keyboard,
6201 const struct timespec *time, uint32_t key,
6202 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006203{
6204 struct weston_compositor *compositor = data;
6205
6206 if (weston_timeline_enabled_)
6207 weston_timeline_close();
6208 else
6209 weston_timeline_open(compositor);
6210}
6211
Giulio Camuffo459137b2014-10-11 23:56:24 +03006212/** Create the compositor.
6213 *
6214 * This functions creates and initializes a compositor instance.
6215 *
6216 * \param display The Wayland display to be used.
6217 * \param user_data A pointer to an object that can later be retrieved
6218 * using the \ref weston_compositor_get_user_data function.
6219 * \return The compositor instance on success or NULL on failure.
6220 */
6221WL_EXPORT struct weston_compositor *
6222weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006223{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006224 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006225 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006226
Giulio Camuffo459137b2014-10-11 23:56:24 +03006227 ec = zalloc(sizeof *ec);
6228 if (!ec)
6229 return NULL;
6230
6231 ec->wl_display = display;
6232 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006233 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006234 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006235 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006236 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006237 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006238 wl_signal_init(&ec->idle_signal);
6239 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006240 wl_signal_init(&ec->show_input_panel_signal);
6241 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006242 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006243 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006244 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006245 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006246 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006247 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006248 wl_signal_init(&ec->heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006249 wl_signal_init(&ec->session_signal);
6250 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006251
Casey Dahlin58ba1372012-04-19 22:50:08 -04006252 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006253 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006254
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006255 ec->activate_serial = 1;
6256
Derek Foreman152254b2015-11-26 14:17:48 -06006257 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006258 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006259 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006260
Giulio Camuffo954f1832014-10-11 18:27:30 +03006261 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006262 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006263 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006264
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006265 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006266 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006267 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006268
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006269 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006270 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006271 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006272
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006273 if (weston_input_init(ec) != 0)
6274 goto fail;
6275
Jason Ekstranda7af7042013-10-12 22:38:11 -05006276 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006277 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006278 wl_list_init(&ec->layer_list);
6279 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006280 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006281 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006282 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006283 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006284 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006285 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006286 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006287 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006288 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006289
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006290 wl_list_init(&ec->plugin_api_list);
6291
Xiong Zhang97116532013-10-23 13:58:31 +08006292 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006293 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006294
Giulio Camuffo459137b2014-10-11 23:56:24 +03006295 wl_data_device_manager_init(ec->wl_display);
6296
6297 wl_display_init_shm(ec->wl_display);
6298
6299 loop = wl_display_get_event_loop(ec->wl_display);
6300 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006301 ec->repaint_timer =
6302 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6303 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006304
Quentin Glidic82681572016-12-17 13:40:51 +01006305 weston_layer_init(&ec->fade_layer, ec);
6306 weston_layer_init(&ec->cursor_layer, ec);
6307
6308 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6309 weston_layer_set_position(&ec->cursor_layer,
6310 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006311
6312 weston_compositor_add_debug_binding(ec, KEY_T,
6313 timeline_key_binding_handler, ec);
6314
Giulio Camuffo459137b2014-10-11 23:56:24 +03006315 return ec;
6316
6317fail:
6318 free(ec);
6319 return NULL;
6320}
6321
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006322WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006323weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006324{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006325 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006326
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006327 wl_event_source_remove(ec->idle_source);
6328
Matt Roper361d2ad2011-08-29 13:52:23 -07006329 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006330 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006331 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006332
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006333 /* Destroy all pending outputs associated with this compositor */
6334 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6335 output->destroy(output);
6336
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006337 if (ec->renderer)
6338 ec->renderer->destroy(ec);
6339
Daniel Stone325fc2d2012-05-30 16:31:58 +01006340 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006341 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006342 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006343 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006344 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006345 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006346
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006347 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006348}
6349
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006350WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006351weston_compositor_exit_with_code(struct weston_compositor *compositor,
6352 int exit_code)
6353{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006354 if (compositor->exit_code == EXIT_SUCCESS)
6355 compositor->exit_code = exit_code;
6356
Giulio Camuffo459137b2014-10-11 23:56:24 +03006357 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006358}
6359
6360WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006361weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6362 const struct weston_pointer_grab_interface *interface)
6363{
6364 struct weston_seat *seat;
6365
6366 ec->default_pointer_grab = interface;
6367 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006368 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6369
6370 if (pointer)
6371 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006372 }
6373}
6374
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006375WL_EXPORT int
6376weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6377 clockid_t clk_id)
6378{
6379 struct timespec ts;
6380
6381 if (clock_gettime(clk_id, &ts) < 0)
6382 return -1;
6383
6384 compositor->presentation_clock = clk_id;
6385
6386 return 0;
6387}
6388
6389/*
6390 * For choosing the software clock, when the display hardware or API
6391 * does not expose a compatible presentation timestamp.
6392 */
6393WL_EXPORT int
6394weston_compositor_set_presentation_clock_software(
6395 struct weston_compositor *compositor)
6396{
6397 /* In order of preference */
6398 static const clockid_t clocks[] = {
6399 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6400 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6401 CLOCK_MONOTONIC, /* no jumps, may crawl */
6402 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
6403 CLOCK_REALTIME /* may jump and crawl */
6404 };
6405 unsigned i;
6406
6407 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
6408 if (weston_compositor_set_presentation_clock(compositor,
6409 clocks[i]) == 0)
6410 return 0;
6411
6412 weston_log("Error: no suitable presentation clock available.\n");
6413
6414 return -1;
6415}
6416
Pekka Paalanen662f3842015-03-18 12:17:26 +02006417/** Read the current time from the Presentation clock
6418 *
6419 * \param compositor
6420 * \param ts[out] The current time.
6421 *
6422 * \note Reading the current time in user space is always imprecise to some
6423 * degree.
6424 *
6425 * This function is never meant to fail. If reading the clock does fail,
6426 * an error message is logged and a zero time is returned. Callers are not
6427 * supposed to detect or react to failures.
6428 */
6429WL_EXPORT void
6430weston_compositor_read_presentation_clock(
6431 const struct weston_compositor *compositor,
6432 struct timespec *ts)
6433{
6434 static bool warned;
6435 int ret;
6436
6437 ret = clock_gettime(compositor->presentation_clock, ts);
6438 if (ret < 0) {
6439 ts->tv_sec = 0;
6440 ts->tv_nsec = 0;
6441
6442 if (!warned)
6443 weston_log("Error: failure to read "
6444 "the presentation clock %#x: '%m' (%d)\n",
6445 compositor->presentation_clock, errno);
6446 warned = true;
6447 }
6448}
6449
Pekka Paalanen230f3b12014-09-29 14:18:40 -04006450/** Import dmabuf buffer into current renderer
6451 *
6452 * \param compositor
6453 * \param buffer the dmabuf buffer to import
6454 * \return true on usable buffers, false otherwise
6455 *
6456 * This function tests that the linux_dmabuf_buffer is usable
6457 * for the current renderer. Returns false on unusable buffers. Usually
6458 * usability is tested by importing the dmabufs for composition.
6459 *
6460 * This hook is also used for detecting if the renderer supports
6461 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
6462 * supported.
6463 * */
6464WL_EXPORT bool
6465weston_compositor_import_dmabuf(struct weston_compositor *compositor,
6466 struct linux_dmabuf_buffer *buffer)
6467{
6468 struct weston_renderer *renderer;
6469
6470 renderer = compositor->renderer;
6471
6472 if (renderer->import_dmabuf == NULL)
6473 return false;
6474
6475 return renderer->import_dmabuf(compositor, buffer);
6476}
6477
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006478WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006479weston_version(int *major, int *minor, int *micro)
6480{
6481 *major = WESTON_VERSION_MAJOR;
6482 *minor = WESTON_VERSION_MINOR;
6483 *micro = WESTON_VERSION_MICRO;
6484}
6485
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03006486WL_EXPORT void *
6487weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006488{
Derek Foreman3f86e502015-06-08 11:46:54 -05006489 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006490 char path[PATH_MAX];
6491 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00006492 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006493
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08006494 if (name == NULL)
6495 return NULL;
6496
Derek Foreman3f86e502015-06-08 11:46:54 -05006497 if (name[0] != '/') {
6498 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00006499 len = snprintf(path, sizeof path, "%s/.libs/%s",
6500 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006501 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00006502 len = snprintf(path, sizeof path, "%s/%s",
6503 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006504 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00006505 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006506 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006507
Daniel Stonebeb97e52016-11-28 12:13:54 +00006508 /* snprintf returns the length of the string it would've written,
6509 * _excluding_ the NUL byte. So even being equal to the size of
6510 * our buffer is an error here. */
6511 if (len >= sizeof path)
6512 return NULL;
6513
Kristian Høgsberga6813d22012-09-12 12:21:01 -04006514 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
6515 if (module) {
6516 weston_log("Module '%s' already loaded\n", path);
6517 dlclose(module);
6518 return NULL;
6519 }
6520
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006521 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04006522 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006523 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006524 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006525 return NULL;
6526 }
6527
6528 init = dlsym(module, entrypoint);
6529 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006530 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00006531 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006532 return NULL;
6533 }
6534
6535 return init;
6536}
6537
Giulio Camuffo459137b2014-10-11 23:56:24 +03006538
6539/** Destroys the compositor.
6540 *
6541 * This function cleans up the compositor state and destroys it.
6542 *
6543 * \param compositor The compositor to be destroyed.
6544 */
6545WL_EXPORT void
6546weston_compositor_destroy(struct weston_compositor *compositor)
6547{
6548 /* prevent further rendering while shutting down */
6549 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
6550
6551 wl_signal_emit(&compositor->destroy_signal, compositor);
6552
6553 weston_compositor_xkb_destroy(compositor);
6554
Giulio Camuffo2d24e642015-10-03 16:25:15 +03006555 if (compositor->backend)
6556 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006557
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006558 /* The backend is responsible for destroying the heads. */
6559 assert(wl_list_empty(&compositor->head_list));
6560
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006561 weston_plugin_api_destroy_list(compositor);
6562
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006563 if (compositor->heads_changed_source)
6564 wl_event_source_remove(compositor->heads_changed_source);
6565
Giulio Camuffo459137b2014-10-11 23:56:24 +03006566 free(compositor);
6567}
6568
6569/** Instruct the compositor to exit.
6570 *
6571 * This functions does not directly destroy the compositor object, it merely
6572 * command it to start the tear down process. It is not guaranteed that the
6573 * tear down will happen immediately.
6574 *
6575 * \param compositor The compositor to tear down.
6576 */
6577WL_EXPORT void
6578weston_compositor_exit(struct weston_compositor *compositor)
6579{
6580 compositor->exit(compositor);
6581}
6582
6583/** Return the user data stored in the compositor.
6584 *
6585 * This function returns the user data pointer set with user_data parameter
6586 * to the \ref weston_compositor_create function.
6587 */
6588WL_EXPORT void *
6589weston_compositor_get_user_data(struct weston_compositor *compositor)
6590{
6591 return compositor->user_data;
6592}
Pekka Paalanendd186732016-06-03 14:49:54 +03006593
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006594static const char * const backend_map[] = {
6595 [WESTON_BACKEND_DRM] = "drm-backend.so",
6596 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
6597 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
6598 [WESTON_BACKEND_RDP] = "rdp-backend.so",
6599 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
6600 [WESTON_BACKEND_X11] = "x11-backend.so",
6601};
6602
Pekka Paalanendd186732016-06-03 14:49:54 +03006603/** Load a backend into a weston_compositor
6604 *
6605 * A backend must be loaded to make a weston_compositor work. A backend
6606 * provides input and output capabilities, and determines the renderer to use.
6607 *
6608 * \param compositor A compositor that has not had a backend loaded yet.
6609 * \param backend Name of the backend file.
6610 * \param config_base A pointer to a backend-specific configuration
6611 * structure's 'base' member.
6612 *
6613 * \return 0 on success, or -1 on error.
6614 */
6615WL_EXPORT int
6616weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006617 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03006618 struct weston_backend_config *config_base)
6619{
6620 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03006621 struct weston_backend_config *config_base);
6622
Pekka Paalanend7e35112017-08-29 17:04:12 +03006623 if (compositor->backend) {
6624 weston_log("Error: attempt to load a backend when one is already loaded\n");
6625 return -1;
6626 }
6627
Quentin Glidic887c0182016-07-10 11:00:53 +02006628 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006629 return -1;
6630
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01006631 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03006632 if (!backend_init)
6633 return -1;
6634
Pekka Paalanend7e35112017-08-29 17:04:12 +03006635 if (backend_init(compositor, config_base) < 0) {
6636 compositor->backend = NULL;
6637 return -1;
6638 }
6639
6640 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03006641}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006642
6643WL_EXPORT int
6644weston_compositor_load_xwayland(struct weston_compositor *compositor)
6645{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006646 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006647
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006648 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006649 if (!module_init)
6650 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006651 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006652 return -1;
6653 return 0;
6654}