blob: 4816f33e6a09e24750a394aaacbf2e23344e84c2 [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
Derek Foreman6ae7bc92014-11-04 10:47:33 -060079static void weston_mode_switch_finish(struct weston_output *output,
80 int mode_changed,
81 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080082{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020083 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020084 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020085 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060086 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020087
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020088 pixman_region32_init(&old_output_region);
89 pixman_region32_copy(&old_output_region, &output->region);
90
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020092 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020093
94 pixman_region32_init(&output->previous_damage);
95 pixman_region32_init_rect(&output->region, output->x, output->y,
96 output->width, output->height);
97
98 weston_output_update_matrix(output);
99
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200100 /* If a pointer falls outside the outputs new geometry, move it to its
101 * lower-right corner */
102 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500103 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200104 int32_t x, y;
105
106 if (!pointer)
107 continue;
108
109 x = wl_fixed_to_int(pointer->x);
110 y = wl_fixed_to_int(pointer->y);
111
112 if (!pixman_region32_contains_point(&old_output_region,
113 x, y, NULL) ||
114 pixman_region32_contains_point(&output->region,
115 x, y, NULL))
116 continue;
117
118 if (x >= output->x + output->width)
119 x = output->x + output->width - 1;
120 if (y >= output->y + output->height)
121 y = output->y + output->height - 1;
122
123 pointer->x = wl_fixed_from_int(x);
124 pointer->y = wl_fixed_from_int(y);
125 }
126
127 pixman_region32_fini(&old_output_region);
128
Derek Foremandd4cd332014-11-10 10:29:59 -0600129 if (!mode_changed && !scale_changed)
130 return;
131
Hardening57388e42013-09-18 23:56:36 +0200132 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600133 wl_resource_for_each(resource, &output->resource_list) {
134 if (mode_changed) {
135 wl_output_send_mode(resource,
136 output->current_mode->flags,
137 output->current_mode->width,
138 output->current_mode->height,
139 output->current_mode->refresh);
140 }
Hardening57388e42013-09-18 23:56:36 +0200141
Derek Foreman41bdc272014-11-05 13:26:57 -0600142 version = wl_resource_get_version(resource);
143 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600144 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200145
Derek Foreman41bdc272014-11-05 13:26:57 -0600146 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
147 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600148 }
149}
150
David Fort0de859e2016-05-27 23:22:57 +0200151
152static void
153weston_compositor_reflow_outputs(struct weston_compositor *compositor,
154 struct weston_output *resized_output, int delta_width);
155
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600156WL_EXPORT int
157weston_output_mode_set_native(struct weston_output *output,
158 struct weston_mode *mode,
159 int32_t scale)
160{
161 int ret;
162 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200163 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600164
165 if (!output->switch_mode)
166 return -1;
167
168 if (!output->original_mode) {
169 mode_changed = 1;
170 ret = output->switch_mode(output, mode);
171 if (ret < 0)
172 return ret;
173 if (output->current_scale != scale) {
174 scale_changed = 1;
175 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200176 }
177 }
178
David Fort0de859e2016-05-27 23:22:57 +0200179 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600180 output->native_mode = mode;
181 output->native_scale = scale;
182
183 weston_mode_switch_finish(output, mode_changed, scale_changed);
184
David Fort0de859e2016-05-27 23:22:57 +0200185 if (mode_changed || scale_changed) {
186 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
187
188 wl_signal_emit(&output->compositor->output_resized_signal, output);
189 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600190 return 0;
191}
192
193WL_EXPORT int
194weston_output_mode_switch_to_native(struct weston_output *output)
195{
196 int ret;
197 int mode_changed = 0, scale_changed = 0;
198
199 if (!output->switch_mode)
200 return -1;
201
202 if (!output->original_mode) {
203 weston_log("already in the native mode\n");
204 return -1;
205 }
206 /* the non fullscreen clients haven't seen a mode set since we
207 * switched into a temporary, so we need to notify them if the
208 * mode at that time is different from the native mode now.
209 */
210 mode_changed = (output->original_mode != output->native_mode);
211 scale_changed = (output->original_scale != output->native_scale);
212
213 ret = output->switch_mode(output, output->native_mode);
214 if (ret < 0)
215 return ret;
216
217 output->current_scale = output->native_scale;
218
219 output->original_mode = NULL;
220 output->original_scale = 0;
221
222 weston_mode_switch_finish(output, mode_changed, scale_changed);
223
224 return 0;
225}
226
227WL_EXPORT int
228weston_output_mode_switch_to_temporary(struct weston_output *output,
229 struct weston_mode *mode,
230 int32_t scale)
231{
232 int ret;
233
234 if (!output->switch_mode)
235 return -1;
236
237 /* original_mode is the last mode non full screen clients have seen,
238 * so we shouldn't change it if we already have one set.
239 */
240 if (!output->original_mode) {
241 output->original_mode = output->native_mode;
242 output->original_scale = output->native_scale;
243 }
244 ret = output->switch_mode(output, mode);
245 if (ret < 0)
246 return ret;
247
248 output->current_scale = scale;
249
250 weston_mode_switch_finish(output, 0, 0);
251
252 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800253}
254
Benjamin Franzke06286262011-05-06 19:12:33 +0200255static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300256region_init_infinite(pixman_region32_t *region)
257{
258 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
259 UINT32_MAX, UINT32_MAX);
260}
261
Pekka Paalanene67858b2013-04-25 13:57:42 +0300262static struct weston_subsurface *
263weston_surface_to_subsurface(struct weston_surface *surface);
264
Jason Ekstranda7af7042013-10-12 22:38:11 -0500265WL_EXPORT struct weston_view *
266weston_view_create(struct weston_surface *surface)
267{
268 struct weston_view *view;
269
Bryce Harringtonde16d892014-11-20 22:21:57 -0800270 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 if (view == NULL)
272 return NULL;
273
274 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000275 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500276
Jason Ekstranda7af7042013-10-12 22:38:11 -0500277 /* Assign to surface */
278 wl_list_insert(&surface->views, &view->surface_link);
279
280 wl_signal_init(&view->destroy_signal);
281 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300282 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500283
Jason Ekstranda7af7042013-10-12 22:38:11 -0500284 pixman_region32_init(&view->clip);
285
286 view->alpha = 1.0;
287 pixman_region32_init(&view->transform.opaque);
288
289 wl_list_init(&view->geometry.transformation_list);
290 wl_list_insert(&view->geometry.transformation_list,
291 &view->transform.position.link);
292 weston_matrix_init(&view->transform.position.matrix);
293 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200294 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500295 pixman_region32_init(&view->transform.boundingbox);
296 view->transform.dirty = 1;
297
Jason Ekstranda7af7042013-10-12 22:38:11 -0500298 return view;
299}
300
Jason Ekstrand108865d2014-06-26 10:04:49 -0700301struct weston_frame_callback {
302 struct wl_resource *resource;
303 struct wl_list link;
304};
305
Pekka Paalanen133e4392014-09-23 22:08:46 -0400306struct weston_presentation_feedback {
307 struct wl_resource *resource;
308
309 /* XXX: could use just wl_resource_get_link() instead */
310 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200311
312 /* The per-surface feedback flags */
313 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400314};
315
316static void
317weston_presentation_feedback_discard(
318 struct weston_presentation_feedback *feedback)
319{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200320 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400321 wl_resource_destroy(feedback->resource);
322}
323
324static void
325weston_presentation_feedback_discard_list(struct wl_list *list)
326{
327 struct weston_presentation_feedback *feedback, *tmp;
328
329 wl_list_for_each_safe(feedback, tmp, list, link)
330 weston_presentation_feedback_discard(feedback);
331}
332
333static void
334weston_presentation_feedback_present(
335 struct weston_presentation_feedback *feedback,
336 struct weston_output *output,
337 uint32_t refresh_nsec,
338 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200339 uint64_t seq,
340 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400341{
342 struct wl_client *client = wl_resource_get_client(feedback->resource);
343 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200344 uint32_t tv_sec_hi;
345 uint32_t tv_sec_lo;
346 uint32_t tv_nsec;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400347
348 wl_resource_for_each(o, &output->resource_list) {
349 if (wl_resource_get_client(o) != client)
350 continue;
351
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200352 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400353 }
354
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200355 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200356 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200357 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200358 refresh_nsec,
359 seq >> 32, seq & 0xffffffff,
360 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400361 wl_resource_destroy(feedback->resource);
362}
363
364static void
365weston_presentation_feedback_present_list(struct wl_list *list,
366 struct weston_output *output,
367 uint32_t refresh_nsec,
368 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200369 uint64_t seq,
370 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400371{
372 struct weston_presentation_feedback *feedback, *tmp;
373
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200374 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200375 wl_list_empty(list));
376
Pekka Paalanen133e4392014-09-23 22:08:46 -0400377 wl_list_for_each_safe(feedback, tmp, list, link)
378 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200379 refresh_nsec, ts, seq,
380 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400381}
382
Jason Ekstrand7b982072014-05-20 14:33:03 -0500383static void
384surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
385{
386 struct weston_surface_state *state =
387 container_of(listener, struct weston_surface_state,
388 buffer_destroy_listener);
389
390 state->buffer = NULL;
391}
392
393static void
394weston_surface_state_init(struct weston_surface_state *state)
395{
396 state->newly_attached = 0;
397 state->buffer = NULL;
398 state->buffer_destroy_listener.notify =
399 surface_state_handle_buffer_destroy;
400 state->sx = 0;
401 state->sy = 0;
402
Derek Foreman152254b2015-11-26 14:17:48 -0600403 pixman_region32_init(&state->damage_surface);
404 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500405 pixman_region32_init(&state->opaque);
406 region_init_infinite(&state->input);
407
408 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400409 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500410
411 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
412 state->buffer_viewport.buffer.scale = 1;
413 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
414 state->buffer_viewport.surface.width = -1;
415 state->buffer_viewport.changed = 0;
416}
417
418static void
419weston_surface_state_fini(struct weston_surface_state *state)
420{
421 struct weston_frame_callback *cb, *next;
422
423 wl_list_for_each_safe(cb, next,
424 &state->frame_callback_list, link)
425 wl_resource_destroy(cb->resource);
426
Pekka Paalanen133e4392014-09-23 22:08:46 -0400427 weston_presentation_feedback_discard_list(&state->feedback_list);
428
Jason Ekstrand7b982072014-05-20 14:33:03 -0500429 pixman_region32_fini(&state->input);
430 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600431 pixman_region32_fini(&state->damage_surface);
432 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500433
434 if (state->buffer)
435 wl_list_remove(&state->buffer_destroy_listener.link);
436 state->buffer = NULL;
437}
438
439static void
440weston_surface_state_set_buffer(struct weston_surface_state *state,
441 struct weston_buffer *buffer)
442{
443 if (state->buffer == buffer)
444 return;
445
446 if (state->buffer)
447 wl_list_remove(&state->buffer_destroy_listener.link);
448 state->buffer = buffer;
449 if (state->buffer)
450 wl_signal_add(&state->buffer->destroy_signal,
451 &state->buffer_destroy_listener);
452}
453
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500454WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500455weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500456{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500457 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400458
Bryce Harringtonde16d892014-11-20 22:21:57 -0800459 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400460 if (surface == NULL)
461 return NULL;
462
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500463 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800464 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500465
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500466 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200467 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400468
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200469 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
470 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200471 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
472 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500473
474 weston_surface_state_init(&surface->pending);
475
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400476 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500477 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300478 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400479
Jason Ekstranda7af7042013-10-12 22:38:11 -0500480 wl_list_init(&surface->views);
481
482 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400483 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500484
Pekka Paalanene67858b2013-04-25 13:57:42 +0300485 wl_list_init(&surface->subsurface_list);
486 wl_list_init(&surface->subsurface_list_pending);
487
Jason Ekstrand1e059042014-10-16 10:55:19 -0500488 weston_matrix_init(&surface->buffer_to_surface_matrix);
489 weston_matrix_init(&surface->surface_to_buffer_matrix);
490
Jonas Ådahld3414f22016-07-22 17:56:31 +0800491 wl_list_init(&surface->pointer_constraints);
492
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400493 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500494}
495
Alex Wu8811bf92012-02-28 18:07:54 +0800496WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500497weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200498 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500499{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100500 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500501}
502
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400503WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500504weston_view_to_global_float(struct weston_view *view,
505 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200506{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500507 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200508 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
509
Jason Ekstranda7af7042013-10-12 22:38:11 -0500510 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200511
512 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200513 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700514 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200515 v.f[3]);
516 *x = 0;
517 *y = 0;
518 return;
519 }
520
521 *x = v.f[0] / v.f[3];
522 *y = v.f[1] / v.f[3];
523 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500524 *x = sx + view->geometry.x;
525 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200526 }
527}
528
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500529WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200530weston_transformed_coord(int width, int height,
531 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200532 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200533 float sx, float sy, float *bx, float *by)
534{
535 switch (transform) {
536 case WL_OUTPUT_TRANSFORM_NORMAL:
537 default:
538 *bx = sx;
539 *by = sy;
540 break;
541 case WL_OUTPUT_TRANSFORM_FLIPPED:
542 *bx = width - sx;
543 *by = sy;
544 break;
545 case WL_OUTPUT_TRANSFORM_90:
546 *bx = height - sy;
547 *by = sx;
548 break;
549 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
550 *bx = height - sy;
551 *by = width - sx;
552 break;
553 case WL_OUTPUT_TRANSFORM_180:
554 *bx = width - sx;
555 *by = height - sy;
556 break;
557 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
558 *bx = sx;
559 *by = height - sy;
560 break;
561 case WL_OUTPUT_TRANSFORM_270:
562 *bx = sy;
563 *by = width - sx;
564 break;
565 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
566 *bx = sy;
567 *by = sx;
568 break;
569 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200570
571 *bx *= scale;
572 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200573}
574
575WL_EXPORT pixman_box32_t
576weston_transformed_rect(int width, int height,
577 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200578 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200579 pixman_box32_t rect)
580{
581 float x1, x2, y1, y2;
582
583 pixman_box32_t ret;
584
Alexander Larsson4ea95522013-05-22 14:41:37 +0200585 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200586 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200587 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200588 rect.x2, rect.y2, &x2, &y2);
589
590 if (x1 <= x2) {
591 ret.x1 = x1;
592 ret.x2 = x2;
593 } else {
594 ret.x1 = x2;
595 ret.x2 = x1;
596 }
597
598 if (y1 <= y2) {
599 ret.y1 = y1;
600 ret.y2 = y2;
601 } else {
602 ret.y1 = y2;
603 ret.y2 = y1;
604 }
605
606 return ret;
607}
608
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600609/** Transform a region by a matrix, restricted to axis-aligned transformations
610 *
611 * Warning: This function does not work for projective, affine, or matrices
612 * that encode arbitrary rotations. Only 90-degree step rotations are
613 * supported.
614 */
615WL_EXPORT void
616weston_matrix_transform_region(pixman_region32_t *dest,
617 struct weston_matrix *matrix,
618 pixman_region32_t *src)
619{
620 pixman_box32_t *src_rects, *dest_rects;
621 int nrects, i;
622
623 src_rects = pixman_region32_rectangles(src, &nrects);
624 dest_rects = malloc(nrects * sizeof(*dest_rects));
625 if (!dest_rects)
626 return;
627
628 for (i = 0; i < nrects; i++) {
629 struct weston_vector vec1 = {{
630 src_rects[i].x1, src_rects[i].y1, 0, 1
631 }};
632 weston_matrix_transform(matrix, &vec1);
633 vec1.f[0] /= vec1.f[3];
634 vec1.f[1] /= vec1.f[3];
635
636 struct weston_vector vec2 = {{
637 src_rects[i].x2, src_rects[i].y2, 0, 1
638 }};
639 weston_matrix_transform(matrix, &vec2);
640 vec2.f[0] /= vec2.f[3];
641 vec2.f[1] /= vec2.f[3];
642
643 if (vec1.f[0] < vec2.f[0]) {
644 dest_rects[i].x1 = floor(vec1.f[0]);
645 dest_rects[i].x2 = ceil(vec2.f[0]);
646 } else {
647 dest_rects[i].x1 = floor(vec2.f[0]);
648 dest_rects[i].x2 = ceil(vec1.f[0]);
649 }
650
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600651 if (vec1.f[1] < vec2.f[1]) {
652 dest_rects[i].y1 = floor(vec1.f[1]);
653 dest_rects[i].y2 = ceil(vec2.f[1]);
654 } else {
655 dest_rects[i].y1 = floor(vec2.f[1]);
656 dest_rects[i].y2 = ceil(vec1.f[1]);
657 }
658 }
659
660 pixman_region32_clear(dest);
661 pixman_region32_init_rects(dest, dest_rects, nrects);
662 free(dest_rects);
663}
664
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200665WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500666weston_transformed_region(int width, int height,
667 enum wl_output_transform transform,
668 int32_t scale,
669 pixman_region32_t *src, pixman_region32_t *dest)
670{
671 pixman_box32_t *src_rects, *dest_rects;
672 int nrects, i;
673
674 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
675 if (src != dest)
676 pixman_region32_copy(dest, src);
677 return;
678 }
679
680 src_rects = pixman_region32_rectangles(src, &nrects);
681 dest_rects = malloc(nrects * sizeof(*dest_rects));
682 if (!dest_rects)
683 return;
684
685 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
686 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
687 } else {
688 for (i = 0; i < nrects; i++) {
689 switch (transform) {
690 default:
691 case WL_OUTPUT_TRANSFORM_NORMAL:
692 dest_rects[i].x1 = src_rects[i].x1;
693 dest_rects[i].y1 = src_rects[i].y1;
694 dest_rects[i].x2 = src_rects[i].x2;
695 dest_rects[i].y2 = src_rects[i].y2;
696 break;
697 case WL_OUTPUT_TRANSFORM_90:
698 dest_rects[i].x1 = height - src_rects[i].y2;
699 dest_rects[i].y1 = src_rects[i].x1;
700 dest_rects[i].x2 = height - src_rects[i].y1;
701 dest_rects[i].y2 = src_rects[i].x2;
702 break;
703 case WL_OUTPUT_TRANSFORM_180:
704 dest_rects[i].x1 = width - src_rects[i].x2;
705 dest_rects[i].y1 = height - src_rects[i].y2;
706 dest_rects[i].x2 = width - src_rects[i].x1;
707 dest_rects[i].y2 = height - src_rects[i].y1;
708 break;
709 case WL_OUTPUT_TRANSFORM_270:
710 dest_rects[i].x1 = src_rects[i].y1;
711 dest_rects[i].y1 = width - src_rects[i].x2;
712 dest_rects[i].x2 = src_rects[i].y2;
713 dest_rects[i].y2 = width - src_rects[i].x1;
714 break;
715 case WL_OUTPUT_TRANSFORM_FLIPPED:
716 dest_rects[i].x1 = width - src_rects[i].x2;
717 dest_rects[i].y1 = src_rects[i].y1;
718 dest_rects[i].x2 = width - src_rects[i].x1;
719 dest_rects[i].y2 = src_rects[i].y2;
720 break;
721 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
722 dest_rects[i].x1 = height - src_rects[i].y2;
723 dest_rects[i].y1 = width - src_rects[i].x2;
724 dest_rects[i].x2 = height - src_rects[i].y1;
725 dest_rects[i].y2 = width - src_rects[i].x1;
726 break;
727 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
728 dest_rects[i].x1 = src_rects[i].x1;
729 dest_rects[i].y1 = height - src_rects[i].y2;
730 dest_rects[i].x2 = src_rects[i].x2;
731 dest_rects[i].y2 = height - src_rects[i].y1;
732 break;
733 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
734 dest_rects[i].x1 = src_rects[i].y1;
735 dest_rects[i].y1 = src_rects[i].x1;
736 dest_rects[i].x2 = src_rects[i].y2;
737 dest_rects[i].y2 = src_rects[i].x2;
738 break;
739 }
740 }
741 }
742
743 if (scale != 1) {
744 for (i = 0; i < nrects; i++) {
745 dest_rects[i].x1 *= scale;
746 dest_rects[i].x2 *= scale;
747 dest_rects[i].y1 *= scale;
748 dest_rects[i].y2 *= scale;
749 }
750 }
751
752 pixman_region32_clear(dest);
753 pixman_region32_init_rects(dest, dest_rects, nrects);
754 free(dest_rects);
755}
756
Jonny Lamb74130762013-11-26 18:19:46 +0100757static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300758viewport_surface_to_buffer(struct weston_surface *surface,
759 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100760{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200761 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200762 double src_width, src_height;
763 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200764
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200765 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
766 if (vp->surface.width == -1) {
767 *bx = sx;
768 *by = sy;
769 return;
770 }
Jonny Lamb74130762013-11-26 18:19:46 +0100771
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200772 src_x = 0.0;
773 src_y = 0.0;
774 src_width = surface->width_from_buffer;
775 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100776 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200777 src_x = wl_fixed_to_double(vp->buffer.src_x);
778 src_y = wl_fixed_to_double(vp->buffer.src_y);
779 src_width = wl_fixed_to_double(vp->buffer.src_width);
780 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100781 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200782
783 *bx = sx * src_width / surface->width + src_x;
784 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100785}
786
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500787WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200788weston_surface_to_buffer_float(struct weston_surface *surface,
789 float sx, float sy, float *bx, float *by)
790{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200791 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
792
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300793 /* first transform coordinates if the viewport is set */
794 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100795
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500796 weston_transformed_coord(surface->width_from_buffer,
797 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200798 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100799 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200800}
801
Derek Foreman99739672015-12-03 16:38:11 -0600802/** Transform a rectangle from surface coordinates to buffer coordinates
803 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300804 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600805 * from.
806 * \param rect The rectangle to transform.
807 * \return The transformed rectangle.
808 *
809 * Viewport and buffer transformations can only do translation, scaling,
810 * and rotations in 90-degree steps. Therefore the only loss in the
811 * conversion is coordinate rounding.
812 *
813 * However, some coordinate rounding takes place as an intermediate
814 * step before the buffer scale factor is applied, so the rectangle
815 * boundary may not be exactly as expected.
816 *
817 * This is OK for damage tracking since a little extra coverage is
818 * not a problem.
819 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200820WL_EXPORT pixman_box32_t
821weston_surface_to_buffer_rect(struct weston_surface *surface,
822 pixman_box32_t rect)
823{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200824 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100825 float xf, yf;
826
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300827 /* first transform box coordinates if the viewport is set */
828 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100829 rect.x1 = floorf(xf);
830 rect.y1 = floorf(yf);
831
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300832 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600833 rect.x2 = ceilf(xf);
834 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100835
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500836 return weston_transformed_rect(surface->width_from_buffer,
837 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200838 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200839 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200840}
841
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200842/** Transform a region from surface coordinates to buffer coordinates
843 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300844 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200845 * from.
846 * \param surface_region[in] The region in surface coordinates.
847 * \param buffer_region[out] The region converted to buffer coordinates.
848 *
849 * Buffer_region must be init'd, but will be completely overwritten.
850 *
851 * Viewport and buffer transformations can only do translation, scaling,
852 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600853 * conversion is from the coordinate rounding that takes place in
854 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200855 */
856WL_EXPORT void
857weston_surface_to_buffer_region(struct weston_surface *surface,
858 pixman_region32_t *surface_region,
859 pixman_region32_t *buffer_region)
860{
861 pixman_box32_t *src_rects, *dest_rects;
862 int nrects, i;
863
864 src_rects = pixman_region32_rectangles(surface_region, &nrects);
865 dest_rects = malloc(nrects * sizeof(*dest_rects));
866 if (!dest_rects)
867 return;
868
869 for (i = 0; i < nrects; i++) {
870 dest_rects[i] = weston_surface_to_buffer_rect(surface,
871 src_rects[i]);
872 }
873
874 pixman_region32_fini(buffer_region);
875 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
876 free(dest_rects);
877}
878
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200879WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500880weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400881 struct weston_plane *plane)
882{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500883 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400884 return;
885
Jason Ekstranda7af7042013-10-12 22:38:11 -0500886 weston_view_damage_below(view);
887 view->plane = plane;
888 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400889}
890
Pekka Paalanen51723d52015-02-17 13:10:01 +0200891/** Inflict damage on the plane where the view is visible.
892 *
893 * \param view The view that causes the damage.
894 *
895 * If the view is currently on a plane (including the primary plane),
896 * take the view's boundingbox, subtract all the opaque views that cover it,
897 * and add the remaining region as damage to the plane. This corresponds
898 * to the damage inflicted to the plane if this view disappeared.
899 *
900 * A repaint is scheduled for this view.
901 *
902 * The region of all opaque views covering this view is stored in
903 * weston_view::clip and updated by view_accumulate_damage() during
904 * weston_output_repaint(). Specifically, that region matches the
905 * scenegraph as it was last painted.
906 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400907WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500908weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200909{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500910 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200911
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500912 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200913 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500914 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800915 if (view->plane)
916 pixman_region32_union(&view->plane->damage,
917 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500918 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800919 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200920}
921
Pekka Paalanen01e00682017-03-24 16:21:06 +0200922/** Send wl_surface.enter/leave events
923 *
924 * \param surface The surface.
925 * \param output The entered/left output.
926 * \param enter True if entered.
927 * \param left True if left.
928 *
929 * Send the enter/leave events for all protocol objects bound to the given
930 * output by the client owning the surface.
931 */
932static void
933weston_surface_send_enter_leave(struct weston_surface *surface,
934 struct weston_output *output,
935 bool enter,
936 bool leave)
937{
938 struct wl_resource *wloutput;
939 struct wl_client *client;
940
941 assert(enter != leave);
942
943 client = wl_resource_get_client(surface->resource);
944 wl_resource_for_each(wloutput, &output->resource_list) {
945 if (wl_resource_get_client(wloutput) != client)
946 continue;
947
948 if (enter)
949 wl_surface_send_enter(surface->resource, wloutput);
950 if (leave)
951 wl_surface_send_leave(surface->resource, wloutput);
952 }
953}
954
Bryce Harrington3f650b82015-12-23 11:01:58 -0800955/**
956 * \param es The surface
957 * \param mask The new set of outputs for the surface
958 *
959 * Sets the surface's set of outputs to the ones specified by
960 * the new output mask provided. Identifies the outputs that
961 * have changed, the posts enter and leave events for these
962 * outputs as appropriate.
963 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400964static void
965weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
966{
967 uint32_t different = es->output_mask ^ mask;
968 uint32_t entered = mask & different;
969 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +0200970 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400971 struct weston_output *output;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400972
973 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500974 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400975 return;
976 if (different == 0)
977 return;
978
979 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +0200980 output_bit = 1u << output->id;
981 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400982 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +0200983
984 weston_surface_send_enter_leave(es, output,
985 output_bit & entered,
986 output_bit & left);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400987 }
988}
989
Bryce Harrington3f650b82015-12-23 11:01:58 -0800990/** Recalculate which output(s) the surface has views displayed on
991 *
992 * \param es The surface to remap to outputs
993 *
994 * Finds the output that is showing the largest amount of one
995 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300996 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800997 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300998 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800999 * in the output_mask for the surface.
1000 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001001static void
1002weston_surface_assign_output(struct weston_surface *es)
1003{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001004 struct weston_output *new_output;
1005 struct weston_view *view;
1006 pixman_region32_t region;
1007 uint32_t max, area, mask;
1008 pixman_box32_t *e;
1009
1010 new_output = NULL;
1011 max = 0;
1012 mask = 0;
1013 pixman_region32_init(&region);
1014 wl_list_for_each(view, &es->views, surface_link) {
1015 if (!view->output)
1016 continue;
1017
1018 pixman_region32_intersect(&region, &view->transform.boundingbox,
1019 &view->output->region);
1020
1021 e = pixman_region32_extents(&region);
1022 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1023
1024 mask |= view->output_mask;
1025
1026 if (area >= max) {
1027 new_output = view->output;
1028 max = area;
1029 }
1030 }
1031 pixman_region32_fini(&region);
1032
1033 es->output = new_output;
1034 weston_surface_update_output_mask(es, mask);
1035}
1036
Bryce Harrington3f650b82015-12-23 11:01:58 -08001037/** Recalculate which output(s) the view is displayed on
1038 *
1039 * \param ev The view to remap to outputs
1040 *
1041 * Identifies the set of outputs that the view is visible on,
1042 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001043 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001044 *
1045 * Also does the same for the view's surface. See
1046 * weston_surface_assign_output().
1047 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001048static void
1049weston_view_assign_output(struct weston_view *ev)
1050{
1051 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001052 struct weston_output *output, *new_output;
1053 pixman_region32_t region;
1054 uint32_t max, area, mask;
1055 pixman_box32_t *e;
1056
1057 new_output = NULL;
1058 max = 0;
1059 mask = 0;
1060 pixman_region32_init(&region);
1061 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001062 if (output->destroying)
1063 continue;
1064
Jason Ekstranda7af7042013-10-12 22:38:11 -05001065 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001066 &output->region);
1067
1068 e = pixman_region32_extents(&region);
1069 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1070
1071 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001072 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001073
1074 if (area >= max) {
1075 new_output = output;
1076 max = area;
1077 }
1078 }
1079 pixman_region32_fini(&region);
1080
Jason Ekstranda7af7042013-10-12 22:38:11 -05001081 ev->output = new_output;
1082 ev->output_mask = mask;
1083
1084 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001085}
1086
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001087static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001088weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1089 int from_x, int from_y, int *to_x, int *to_y)
1090{
1091 float x, y;
1092
1093 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1094 weston_view_from_global_float(to, x, y, &x, &y);
1095
1096 *to_x = round(x);
1097 *to_y = round(y);
1098}
1099
1100static void
1101weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1102{
1103 pixman_box32_t *a;
1104 pixman_box32_t b;
1105
1106 a = pixman_region32_extents(&from->geometry.scissor);
1107
1108 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1109 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1110
1111 pixman_region32_fini(&to->geometry.scissor);
1112 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1113}
1114
1115static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001116view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001117 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001118{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001119 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1120 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001121 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001122 { inbox->x1, inbox->y1 },
1123 { inbox->x1, inbox->y2 },
1124 { inbox->x2, inbox->y1 },
1125 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001126 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001127 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001128 int i;
1129
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001130 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001131 /* avoid rounding empty bbox to 1x1 */
1132 pixman_region32_init(bbox);
1133 return;
1134 }
1135
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001136 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001137 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001139 if (x < min_x)
1140 min_x = x;
1141 if (x > max_x)
1142 max_x = x;
1143 if (y < min_y)
1144 min_y = y;
1145 if (y > max_y)
1146 max_y = y;
1147 }
1148
Pekka Paalanen219b9822012-02-08 15:38:37 +02001149 int_x = floorf(min_x);
1150 int_y = floorf(min_y);
1151 pixman_region32_init_rect(bbox, int_x, int_y,
1152 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001153}
1154
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001155static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001157{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001159
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001160 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 view->geometry.x = roundf(view->geometry.x);
1162 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001163
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001164 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1166 view->transform.position.matrix.d[12] = view->geometry.x;
1167 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001168
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001170
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171 view->transform.inverse = view->transform.position.matrix;
1172 view->transform.inverse.d[12] = -view->geometry.x;
1173 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001174
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001176 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001177 view->surface->width,
1178 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001179 if (view->geometry.scissor_enabled)
1180 pixman_region32_intersect(&view->transform.boundingbox,
1181 &view->transform.boundingbox,
1182 &view->geometry.scissor);
1183
1184 pixman_region32_translate(&view->transform.boundingbox,
1185 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001186
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 if (view->alpha == 1.0) {
1188 pixman_region32_copy(&view->transform.opaque,
1189 &view->surface->opaque);
1190 pixman_region32_translate(&view->transform.opaque,
1191 view->geometry.x,
1192 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001193 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001194}
1195
1196static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001198{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 struct weston_view *parent = view->geometry.parent;
1200 struct weston_matrix *matrix = &view->transform.matrix;
1201 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001202 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001203 pixman_region32_t surfregion;
1204 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001205
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001207
1208 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001209 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1210 view->transform.position.matrix.d[12] = view->geometry.x;
1211 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001212
1213 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001215 weston_matrix_multiply(matrix, &tform->matrix);
1216
Pekka Paalanen483243f2013-03-08 14:56:50 +02001217 if (parent)
1218 weston_matrix_multiply(matrix, &parent->transform.matrix);
1219
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001220 if (weston_matrix_invert(inverse, matrix) < 0) {
1221 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 weston_log("error: weston_view %p"
1223 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001224 return -1;
1225 }
1226
Giulio Camuffo148c1992016-09-04 18:50:46 +03001227 if (view->alpha == 1.0 &&
1228 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1229 pixman_region32_copy(&view->transform.opaque,
1230 &view->surface->opaque);
1231 pixman_region32_translate(&view->transform.opaque,
1232 matrix->d[12],
1233 matrix->d[13]);
1234 }
1235
Pekka Paalanen380adf52015-02-16 14:39:11 +02001236 pixman_region32_init_rect(&surfregion, 0, 0,
1237 view->surface->width, view->surface->height);
1238 if (view->geometry.scissor_enabled)
1239 pixman_region32_intersect(&surfregion, &surfregion,
1240 &view->geometry.scissor);
1241 surfbox = pixman_region32_extents(&surfregion);
1242
1243 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1244 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001245
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001246 return 0;
1247}
1248
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001249static struct weston_layer *
1250get_view_layer(struct weston_view *view)
1251{
1252 if (view->parent_view)
1253 return get_view_layer(view->parent_view);
1254 return view->layer_link.layer;
1255}
1256
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001257WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001259{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001261 struct weston_layer *layer;
1262 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001263
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001265 return;
1266
Pekka Paalanen483243f2013-03-08 14:56:50 +02001267 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001269
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001271
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001273
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 pixman_region32_fini(&view->transform.boundingbox);
1275 pixman_region32_fini(&view->transform.opaque);
1276 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001277
Pekka Paalanencd403622012-01-25 13:37:39 +02001278 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 if (view->geometry.transformation_list.next ==
1280 &view->transform.position.link &&
1281 view->geometry.transformation_list.prev ==
1282 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001283 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001285 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 if (weston_view_update_transform_enable(view) < 0)
1287 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001288 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001289
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001290 layer = get_view_layer(view);
1291 if (layer) {
1292 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001293 pixman_region32_intersect(&view->transform.boundingbox,
1294 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001295 pixman_region32_intersect(&view->transform.opaque,
1296 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001297 pixman_region32_fini(&mask);
1298 }
1299
Pekka Paalanen380adf52015-02-16 14:39:11 +02001300 if (parent) {
1301 if (parent->geometry.scissor_enabled) {
1302 view->geometry.scissor_enabled = true;
1303 weston_view_transfer_scissor(parent, view);
1304 } else {
1305 view->geometry.scissor_enabled = false;
1306 }
1307 }
1308
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001310
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001312
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313 wl_signal_emit(&view->surface->compositor->transform_signal,
1314 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001315}
1316
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001317WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001319{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001321
1322 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323 * The invariant: if view->geometry.dirty, then all views
1324 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001325 * Corollary: if not parent->geometry.dirty, then all ancestors
1326 * are not dirty.
1327 */
1328
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001330 return;
1331
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001333
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001335 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001337}
1338
1339WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340weston_view_to_global_fixed(struct weston_view *view,
1341 wl_fixed_t vx, wl_fixed_t vy,
1342 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001343{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001344 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001345
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346 weston_view_to_global_float(view,
1347 wl_fixed_to_double(vx),
1348 wl_fixed_to_double(vy),
1349 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001350 *x = wl_fixed_from_double(xf);
1351 *y = wl_fixed_from_double(yf);
1352}
1353
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001354WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355weston_view_from_global_float(struct weston_view *view,
1356 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001357{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001359 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1360
Jason Ekstranda7af7042013-10-12 22:38:11 -05001361 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001362
1363 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001364 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001366 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 *vx = 0;
1368 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001369 return;
1370 }
1371
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 *vx = v.f[0] / v.f[3];
1373 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001374 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 *vx = x - view->geometry.x;
1376 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001377 }
1378}
1379
1380WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001381weston_view_from_global_fixed(struct weston_view *view,
1382 wl_fixed_t x, wl_fixed_t y,
1383 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001384{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001386
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 weston_view_from_global_float(view,
1388 wl_fixed_to_double(x),
1389 wl_fixed_to_double(y),
1390 &vxf, &vyf);
1391 *vx = wl_fixed_from_double(vxf);
1392 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001393}
1394
1395WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396weston_view_from_global(struct weston_view *view,
1397 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001398{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001400
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1402 *vx = floorf(vxf);
1403 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001404}
1405
Bryce Harrington3f650b82015-12-23 11:01:58 -08001406/**
1407 * \param surface The surface to be repainted
1408 *
1409 * Marks the output(s) that the surface is shown on as needing to be
1410 * repainted. See weston_output_schedule_repaint().
1411 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001412WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001413weston_surface_schedule_repaint(struct weston_surface *surface)
1414{
1415 struct weston_output *output;
1416
1417 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001418 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001419 weston_output_schedule_repaint(output);
1420}
1421
Bryce Harrington3f650b82015-12-23 11:01:58 -08001422/**
1423 * \param view The view to be repainted
1424 *
1425 * Marks the output(s) that the view is shown on as needing to be
1426 * repainted. See weston_output_schedule_repaint().
1427 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001428WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429weston_view_schedule_repaint(struct weston_view *view)
1430{
1431 struct weston_output *output;
1432
1433 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001434 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 weston_output_schedule_repaint(output);
1436}
1437
Pekka Paalanene508ce62015-02-19 13:59:55 +02001438/**
1439 * XXX: This function does it the wrong way.
1440 * surface->damage is the damage from the client, and causes
1441 * surface_flush_damage() to copy pixels. No window management action can
1442 * cause damage to the client-provided content, warranting re-upload!
1443 *
1444 * Instead of surface->damage, this function should record the damage
1445 * with all the views for this surface to avoid extraneous texture
1446 * uploads.
1447 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001449weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001450{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001451 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 0, 0, surface->width,
1453 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001454
Kristian Høgsberg98238702012-08-03 16:29:12 -04001455 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001456}
1457
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001458WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001460{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001461 if (view->geometry.x == x && view->geometry.y == y)
1462 return;
1463
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 view->geometry.x = x;
1465 view->geometry.y = y;
1466 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001467}
1468
Pekka Paalanen483243f2013-03-08 14:56:50 +02001469static void
1470transform_parent_handle_parent_destroy(struct wl_listener *listener,
1471 void *data)
1472{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 struct weston_view *view =
1474 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001475 geometry.parent_destroy_listener);
1476
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001478}
1479
1480WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001482 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001483{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001484 if (view->geometry.parent) {
1485 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1486 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001487
1488 if (!parent)
1489 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001490 }
1491
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001493
Jason Ekstranda7af7042013-10-12 22:38:11 -05001494 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001495 transform_parent_handle_parent_destroy;
1496 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001497 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001499 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001500 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001501 }
1502
Jason Ekstranda7af7042013-10-12 22:38:11 -05001503 weston_view_geometry_dirty(view);
1504}
1505
Pekka Paalanen380adf52015-02-16 14:39:11 +02001506/** Set a clip mask rectangle on a view
1507 *
1508 * \param view The view to set the clip mask on.
1509 * \param x Top-left corner X coordinate of the clip rectangle.
1510 * \param y Top-left corner Y coordinate of the clip rectangle.
1511 * \param width Width of the clip rectangle, non-negative.
1512 * \param height Height of the clip rectangle, non-negative.
1513 *
1514 * A shell may set a clip mask rectangle on a view. Everything outside
1515 * the rectangle is cut away for input and output purposes: it is
1516 * not drawn and cannot be hit by hit-test based input like pointer
1517 * motion or touch-downs. Everything inside the rectangle will behave
1518 * normally. Clients are unaware of clipping.
1519 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001520 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001521 * mask rectangle does not affect the view position, the view is positioned
1522 * as it would be without a clip. The clip also does not change
1523 * weston_surface::width,height.
1524 *
1525 * The clip mask rectangle is part of transformation inheritance
1526 * (weston_view_set_transform_parent()). A clip set in the root of the
1527 * transformation inheritance tree will affect all views in the tree.
1528 * A clip can be set only on the root view. Attempting to set a clip
1529 * on view that has a transformation parent will fail. Assigning a parent
1530 * to a view that has a clip set will cause the clip to be forgotten.
1531 *
1532 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1533 * on the additional transformations in the child views. These transformations
1534 * may not rotate the coordinate axes, i.e., only translation and scaling
1535 * are allowed. Violating this restriction causes the clipping to malfunction.
1536 * Furthermore, using scaling may cause rounding errors in child clipping.
1537 *
1538 * The clip mask rectangle is not automatically adjusted based on
1539 * wl_surface.attach dx and dy arguments.
1540 *
1541 * A clip mask rectangle can be set only if the compositor capability
1542 * WESTON_CAP_VIEW_CLIP_MASK is present.
1543 *
1544 * This function sets the clip mask rectangle and schedules a repaint for
1545 * the view.
1546 */
1547WL_EXPORT void
1548weston_view_set_mask(struct weston_view *view,
1549 int x, int y, int width, int height)
1550{
1551 struct weston_compositor *compositor = view->surface->compositor;
1552
1553 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1554 weston_log("%s not allowed without capability!\n", __func__);
1555 return;
1556 }
1557
1558 if (view->geometry.parent) {
1559 weston_log("view %p has a parent, clip forbidden!\n", view);
1560 return;
1561 }
1562
1563 if (width < 0 || height < 0) {
1564 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1565 x, y, width, height);
1566 return;
1567 }
1568
1569 pixman_region32_fini(&view->geometry.scissor);
1570 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1571 view->geometry.scissor_enabled = true;
1572 weston_view_geometry_dirty(view);
1573 weston_view_schedule_repaint(view);
1574}
1575
1576/** Remove the clip mask from a view
1577 *
1578 * \param view The view to remove the clip mask from.
1579 *
1580 * Removed the clip mask rectangle and schedules a repaint.
1581 *
1582 * \sa weston_view_set_mask
1583 */
1584WL_EXPORT void
1585weston_view_set_mask_infinite(struct weston_view *view)
1586{
1587 view->geometry.scissor_enabled = false;
1588 weston_view_geometry_dirty(view);
1589 weston_view_schedule_repaint(view);
1590}
1591
Armin Krezović0da12b82016-06-30 06:04:33 +02001592/* Check if view should be displayed
1593 *
1594 * The indicator is set manually when assigning
1595 * a view to a surface.
1596 *
1597 * This needs reworking. See the thread starting at:
1598 *
1599 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1600 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001601WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001602weston_view_is_mapped(struct weston_view *view)
1603{
Armin Krezović0da12b82016-06-30 06:04:33 +02001604 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001605}
1606
Armin Krezović0da12b82016-06-30 06:04:33 +02001607/* Check if a surface has a view assigned to it
1608 *
1609 * The indicator is set manually when mapping
1610 * a surface and creating a view for it.
1611 *
1612 * This needs to go. See the thread starting at:
1613 *
1614 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1615 *
1616 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001617WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001618weston_surface_is_mapped(struct weston_surface *surface)
1619{
Armin Krezović0da12b82016-06-30 06:04:33 +02001620 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001621}
1622
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001623static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001624surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001625{
1626 struct weston_view *view;
1627
1628 if (surface->width == width && surface->height == height)
1629 return;
1630
1631 surface->width = width;
1632 surface->height = height;
1633
1634 wl_list_for_each(view, &surface->views, surface_link)
1635 weston_view_geometry_dirty(view);
1636}
1637
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001638WL_EXPORT void
1639weston_surface_set_size(struct weston_surface *surface,
1640 int32_t width, int32_t height)
1641{
1642 assert(!surface->resource);
1643 surface_set_size(surface, width, height);
1644}
1645
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001646static int
1647fixed_round_up_to_int(wl_fixed_t f)
1648{
1649 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1650}
1651
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001652static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001653convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1654 int32_t width, int32_t height,
1655 uint32_t transform,
1656 int32_t scale)
1657{
1658 assert(scale > 0);
1659
1660 switch (transform) {
1661 case WL_OUTPUT_TRANSFORM_NORMAL:
1662 case WL_OUTPUT_TRANSFORM_180:
1663 case WL_OUTPUT_TRANSFORM_FLIPPED:
1664 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1665 *width_out = width / scale;
1666 *height_out = height / scale;
1667 break;
1668 case WL_OUTPUT_TRANSFORM_90:
1669 case WL_OUTPUT_TRANSFORM_270:
1670 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1671 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1672 *width_out = height / scale;
1673 *height_out = width / scale;
1674 break;
1675 default:
1676 assert(0 && "invalid transform");
1677 }
1678}
1679
1680static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001681weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001682{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001683 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001684
1685 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001686 surface->width_from_buffer = 0;
1687 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001688 return;
1689 }
1690
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001691 convert_size_by_transform_scale(&surface->width_from_buffer,
1692 &surface->height_from_buffer,
1693 surface->buffer_ref.buffer->width,
1694 surface->buffer_ref.buffer->height,
1695 vp->buffer.transform,
1696 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001697}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001698
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001699static void
1700weston_surface_update_size(struct weston_surface *surface)
1701{
1702 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1703 int32_t width, height;
1704
1705 width = surface->width_from_buffer;
1706 height = surface->height_from_buffer;
1707
1708 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001709 surface_set_size(surface,
1710 vp->surface.width, vp->surface.height);
1711 return;
1712 }
1713
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001714 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001715 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1716 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1717
1718 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001719 return;
1720 }
1721
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001722 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001723}
1724
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001725WL_EXPORT void
1726weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001727{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001728 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001729}
1730
Jason Ekstranda7af7042013-10-12 22:38:11 -05001731WL_EXPORT struct weston_view *
1732weston_compositor_pick_view(struct weston_compositor *compositor,
1733 wl_fixed_t x, wl_fixed_t y,
1734 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001735{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001737 wl_fixed_t view_x, view_y;
1738 int view_ix, view_iy;
1739 int ix = wl_fixed_to_int(x);
1740 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001741
Jason Ekstranda7af7042013-10-12 22:38:11 -05001742 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001743 if (!pixman_region32_contains_point(
1744 &view->transform.boundingbox, ix, iy, NULL))
1745 continue;
1746
1747 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1748 view_ix = wl_fixed_to_int(view_x);
1749 view_iy = wl_fixed_to_int(view_y);
1750
1751 if (!pixman_region32_contains_point(&view->surface->input,
1752 view_ix, view_iy, NULL))
1753 continue;
1754
Pekka Paalanen380adf52015-02-16 14:39:11 +02001755 if (view->geometry.scissor_enabled &&
1756 !pixman_region32_contains_point(&view->geometry.scissor,
1757 view_ix, view_iy, NULL))
1758 continue;
1759
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001760 *vx = view_x;
1761 *vy = view_y;
1762 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001763 }
1764
Derek Foremanf9318d12015-05-11 15:40:11 -05001765 *vx = wl_fixed_from_int(-1000000);
1766 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001767 return NULL;
1768}
1769
1770static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001771weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001772{
Daniel Stone37816df2012-05-16 18:45:18 +01001773 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001774
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001775 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001776 return;
1777
Daniel Stone37816df2012-05-16 18:45:18 +01001778 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001779 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001780}
1781
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001782WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001784{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001785 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001786
Jason Ekstranda7af7042013-10-12 22:38:11 -05001787 if (!weston_view_is_mapped(view))
1788 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001789
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790 weston_view_damage_below(view);
1791 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001792 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001793 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001794 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001795 wl_list_remove(&view->link);
1796 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001797 view->output_mask = 0;
1798 weston_surface_assign_output(view->surface);
1799
1800 if (weston_surface_is_mapped(view->surface))
1801 return;
1802
1803 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001804 struct weston_touch *touch = weston_seat_get_touch(seat);
1805 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1806 struct weston_keyboard *keyboard =
1807 weston_seat_get_keyboard(seat);
1808
1809 if (keyboard && keyboard->focus == view->surface)
1810 weston_keyboard_set_focus(keyboard, NULL);
1811 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001812 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001813 if (touch && touch->focus == view)
1814 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001815 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001817
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818WL_EXPORT void
1819weston_surface_unmap(struct weston_surface *surface)
1820{
1821 struct weston_view *view;
1822
Armin Krezovićf8486c32016-06-30 06:04:28 +02001823 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001824 wl_list_for_each(view, &surface->views, surface_link)
1825 weston_view_unmap(view);
1826 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001827}
1828
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001829static void
1830weston_surface_reset_pending_buffer(struct weston_surface *surface)
1831{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001832 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001833 surface->pending.sx = 0;
1834 surface->pending.sy = 0;
1835 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001836 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001837}
1838
Jason Ekstranda7af7042013-10-12 22:38:11 -05001839WL_EXPORT void
1840weston_view_destroy(struct weston_view *view)
1841{
1842 wl_signal_emit(&view->destroy_signal, view);
1843
1844 assert(wl_list_empty(&view->geometry.child_list));
1845
1846 if (weston_view_is_mapped(view)) {
1847 weston_view_unmap(view);
1848 weston_compositor_build_view_list(view->surface->compositor);
1849 }
1850
1851 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001852 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001853
1854 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001855 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001856 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001857 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858
1859 weston_view_set_transform_parent(view, NULL);
1860
Jason Ekstranda7af7042013-10-12 22:38:11 -05001861 wl_list_remove(&view->surface_link);
1862
1863 free(view);
1864}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001865
1866WL_EXPORT void
1867weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001868{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001869 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001870 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001871 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001872
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001873 if (--surface->ref_count > 0)
1874 return;
1875
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001876 assert(surface->resource == NULL);
1877
Pekka Paalanenca790762015-04-17 14:23:38 +03001878 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001879
Pekka Paalanene67858b2013-04-25 13:57:42 +03001880 assert(wl_list_empty(&surface->subsurface_list_pending));
1881 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001882
Jason Ekstranda7af7042013-10-12 22:38:11 -05001883 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1884 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001885
Jason Ekstrand7b982072014-05-20 14:33:03 -05001886 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001887
Pekka Paalanende685b82012-12-04 15:58:12 +02001888 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001889
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001890 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001891 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001892 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001893
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001894 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001895 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001896
Pekka Paalanen133e4392014-09-23 22:08:46 -04001897 weston_presentation_feedback_discard_list(&surface->feedback_list);
1898
Jonas Ådahld3414f22016-07-22 17:56:31 +08001899 wl_list_for_each_safe(constraint, next_constraint,
1900 &surface->pointer_constraints,
1901 link)
1902 weston_pointer_constraint_destroy(constraint);
1903
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001904 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001905}
1906
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001907static void
1908destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001909{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001910 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001911
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001912 assert(surface);
1913
Giulio Camuffo0d379742013-11-15 22:06:15 +01001914 /* Set the resource to NULL, since we don't want to leave a
1915 * dangling pointer if the surface was refcounted and survives
1916 * the weston_surface_destroy() call. */
1917 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001918
1919 if (surface->viewport_resource)
1920 wl_resource_set_user_data(surface->viewport_resource, NULL);
1921
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001922 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001923}
1924
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001925static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001926weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1927{
1928 struct weston_buffer *buffer =
1929 container_of(listener, struct weston_buffer, destroy_listener);
1930
1931 wl_signal_emit(&buffer->destroy_signal, buffer);
1932 free(buffer);
1933}
1934
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001935WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001936weston_buffer_from_resource(struct wl_resource *resource)
1937{
1938 struct weston_buffer *buffer;
1939 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001940
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001941 listener = wl_resource_get_destroy_listener(resource,
1942 weston_buffer_destroy_handler);
1943
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001944 if (listener)
1945 return container_of(listener, struct weston_buffer,
1946 destroy_listener);
1947
1948 buffer = zalloc(sizeof *buffer);
1949 if (buffer == NULL)
1950 return NULL;
1951
1952 buffer->resource = resource;
1953 wl_signal_init(&buffer->destroy_signal);
1954 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001955 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001956 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001957
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001958 return buffer;
1959}
1960
1961static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001962weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1963 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001964{
Pekka Paalanende685b82012-12-04 15:58:12 +02001965 struct weston_buffer_reference *ref =
1966 container_of(listener, struct weston_buffer_reference,
1967 destroy_listener);
1968
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001969 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001970 ref->buffer = NULL;
1971}
1972
1973WL_EXPORT void
1974weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001975 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001976{
1977 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001978 ref->buffer->busy_count--;
1979 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001980 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05001981 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05001982 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001983 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001984 }
1985
Pekka Paalanende685b82012-12-04 15:58:12 +02001986 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001987 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001988 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001989 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001990 }
1991
Pekka Paalanende685b82012-12-04 15:58:12 +02001992 ref->buffer = buffer;
1993 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1994}
1995
1996static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001997weston_surface_attach(struct weston_surface *surface,
1998 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001999{
2000 weston_buffer_reference(&surface->buffer_ref, buffer);
2001
Pekka Paalanena6421c42012-12-04 15:58:10 +02002002 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002003 if (weston_surface_is_mapped(surface))
2004 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002005 }
2006
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002007 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002008
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002009 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002010 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002011}
2012
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002013WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002014weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002015{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002016 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002017
2018 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002019 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002020}
2021
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002022WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002023weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002024{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002025 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002026
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002027 pixman_region32_union(&compositor->primary_plane.damage,
2028 &compositor->primary_plane.damage,
2029 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002030 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002031}
2032
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002033static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002034surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002035{
Pekka Paalanende685b82012-12-04 15:58:12 +02002036 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002037 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002038 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002039
Pekka Paalanenb5026542014-11-12 15:09:24 +02002040 if (weston_timeline_enabled_ &&
2041 pixman_region32_not_empty(&surface->damage))
2042 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2043 TLP_OUTPUT(surface->output), TLP_END);
2044
Jason Ekstrandef540082014-06-26 10:37:36 -07002045 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002046}
2047
2048static void
2049view_accumulate_damage(struct weston_view *view,
2050 pixman_region32_t *opaque)
2051{
2052 pixman_region32_t damage;
2053
2054 pixman_region32_init(&damage);
2055 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002056 pixman_box32_t *extents;
2057
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002059 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002060 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002061 pixman_region32_copy(&damage, &view->surface->damage);
2062 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002063 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002064 }
2065
Pekka Paalanen380adf52015-02-16 14:39:11 +02002066 pixman_region32_intersect(&damage, &damage,
2067 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002068 pixman_region32_subtract(&damage, &damage, opaque);
2069 pixman_region32_union(&view->plane->damage,
2070 &view->plane->damage, &damage);
2071 pixman_region32_fini(&damage);
2072 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002073 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002074}
2075
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002076static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002077compositor_accumulate_damage(struct weston_compositor *ec)
2078{
2079 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002080 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002081 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002082
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002083 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002084
2085 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002086 pixman_region32_copy(&plane->clip, &clip);
2087
2088 pixman_region32_init(&opaque);
2089
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090 wl_list_for_each(ev, &ec->view_list, link) {
2091 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002092 continue;
2093
Jason Ekstranda7af7042013-10-12 22:38:11 -05002094 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002095 }
2096
2097 pixman_region32_union(&clip, &clip, &opaque);
2098 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002099 }
2100
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002101 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002102
Jason Ekstranda7af7042013-10-12 22:38:11 -05002103 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002104 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002105
2106 wl_list_for_each(ev, &ec->view_list, link) {
2107 if (ev->surface->touched)
2108 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002109 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002110
2111 surface_flush_damage(ev->surface);
2112
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002113 /* Both the renderer and the backend have seen the buffer
2114 * by now. If renderer needs the buffer, it has its own
2115 * reference set. If the backend wants to keep the buffer
2116 * around for migrating the surface into a non-primary plane
2117 * later, keep_buffer is true. Otherwise, drop the core
2118 * reference now, and allow early buffer release. This enables
2119 * clients to use single-buffering.
2120 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 if (!ev->surface->keep_buffer)
2122 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002123 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002124}
2125
2126static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002128{
2129 struct weston_subsurface *sub;
2130
Pekka Paalanene67858b2013-04-25 13:57:42 +03002131 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002132 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002133 continue;
2134
Jason Ekstranda7af7042013-10-12 22:38:11 -05002135 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2136 wl_list_init(&sub->surface->views);
2137
2138 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002139 }
2140}
2141
2142static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002144{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002145 struct weston_subsurface *sub;
2146 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002147
Jason Ekstranda7af7042013-10-12 22:38:11 -05002148 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2149 if (sub->surface == surface)
2150 continue;
2151
George Kiagiadakised04d382014-06-13 18:10:26 +02002152 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2153 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002154 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002155 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002156
2157 surface_free_unused_subsurface_views(sub->surface);
2158 }
2159}
2160
2161static void
2162view_list_add_subsurface_view(struct weston_compositor *compositor,
2163 struct weston_subsurface *sub,
2164 struct weston_view *parent)
2165{
2166 struct weston_subsurface *child;
2167 struct weston_view *view = NULL, *iv;
2168
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002169 if (!weston_surface_is_mapped(sub->surface))
2170 return;
2171
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2173 if (iv->geometry.parent == parent) {
2174 view = iv;
2175 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002176 }
2177 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178
2179 if (view) {
2180 /* Put it back in the surface's list of views */
2181 wl_list_remove(&view->surface_link);
2182 wl_list_insert(&sub->surface->views, &view->surface_link);
2183 } else {
2184 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002185 weston_view_set_position(view,
2186 sub->position.x,
2187 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002188 weston_view_set_transform_parent(view, parent);
2189 }
2190
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002191 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002192 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002193 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194
Pekka Paalanenb188e912013-11-19 14:03:35 +02002195 if (wl_list_empty(&sub->surface->subsurface_list)) {
2196 wl_list_insert(compositor->view_list.prev, &view->link);
2197 return;
2198 }
2199
2200 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2201 if (child->surface == sub->surface)
2202 wl_list_insert(compositor->view_list.prev, &view->link);
2203 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002204 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002205 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002206}
2207
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002208/* This recursively adds the sub-surfaces for a view, relying on the
2209 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2210 * change first happens to the sub-surface list, and then automatically
2211 * propagates here. See weston_surface_damage_subsurfaces() for how the
2212 * sub-surfaces receive damage when the client changes the state.
2213 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002214static void
2215view_list_add(struct weston_compositor *compositor,
2216 struct weston_view *view)
2217{
2218 struct weston_subsurface *sub;
2219
2220 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002221
Pekka Paalanenb188e912013-11-19 14:03:35 +02002222 if (wl_list_empty(&view->surface->subsurface_list)) {
2223 wl_list_insert(compositor->view_list.prev, &view->link);
2224 return;
2225 }
2226
2227 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2228 if (sub->surface == view->surface)
2229 wl_list_insert(compositor->view_list.prev, &view->link);
2230 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002231 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002232 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002233}
2234
2235static void
2236weston_compositor_build_view_list(struct weston_compositor *compositor)
2237{
2238 struct weston_view *view;
2239 struct weston_layer *layer;
2240
2241 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002242 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243 surface_stash_subsurface_views(view->surface);
2244
2245 wl_list_init(&compositor->view_list);
2246 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002247 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002248 view_list_add(compositor, view);
2249 }
2250 }
2251
2252 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002253 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002254 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002255}
2256
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002257static void
2258weston_output_take_feedback_list(struct weston_output *output,
2259 struct weston_surface *surface)
2260{
2261 struct weston_view *view;
2262 struct weston_presentation_feedback *feedback;
2263 uint32_t flags = 0xffffffff;
2264
2265 if (wl_list_empty(&surface->feedback_list))
2266 return;
2267
2268 /* All views must have the flag for the flag to survive. */
2269 wl_list_for_each(view, &surface->views, surface_link) {
2270 /* ignore views that are not on this output at all */
2271 if (view->output_mask & (1u << output->id))
2272 flags &= view->psf_flags;
2273 }
2274
2275 wl_list_for_each(feedback, &surface->feedback_list, link)
2276 feedback->psf_flags = flags;
2277
2278 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2279 wl_list_init(&surface->feedback_list);
2280}
2281
David Herrmann1edf44c2013-10-22 17:11:26 +02002282static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002283weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002284{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002285 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002286 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002287 struct weston_animation *animation, *next;
2288 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002289 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002290 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002291 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002292 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002293
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002294 if (output->destroying)
2295 return 0;
2296
Pekka Paalanenb5026542014-11-12 15:09:24 +02002297 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2298
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002299 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002300 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002301
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002302 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002303 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002304 } else {
2305 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002306 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002307 ev->psf_flags = 0;
2308 }
2309 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002310
Pekka Paalanene67858b2013-04-25 13:57:42 +03002311 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002312 wl_list_for_each(ev, &ec->view_list, link) {
2313 /* Note: This operation is safe to do multiple times on the
2314 * same surface.
2315 */
2316 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002317 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002318 &ev->surface->frame_callback_list);
2319 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002320
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002321 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002322 }
2323 }
2324
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002325 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002326
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002327 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002328 pixman_region32_intersect(&output_damage,
2329 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002330 pixman_region32_subtract(&output_damage,
2331 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002332
Scott Moreauccbf29d2012-02-22 14:21:41 -07002333 if (output->dirty)
2334 weston_output_update_matrix(output);
2335
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002336 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002337
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002338 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002339
Daniel Stone09a97e22017-03-01 11:34:06 +00002340 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002341 if (r == 0)
2342 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002343
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002344 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002345
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002346 frame_time_msec = timespec_to_msec(&output->frame_time);
2347
Jonas Ådahldb773762012-06-13 00:01:21 +02002348 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002349 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002350 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002351 }
2352
Scott Moreaud64cf212012-06-08 19:40:54 -06002353 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002354 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002355 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002356 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002357
Pekka Paalanenb5026542014-11-12 15:09:24 +02002358 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2359
David Herrmann1edf44c2013-10-22 17:11:26 +02002360 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002361}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002362
Pekka Paalanen82919792014-05-21 13:51:49 +03002363static void
2364weston_output_schedule_repaint_reset(struct weston_output *output)
2365{
Daniel Stone05df8c12017-03-03 16:59:42 +00002366 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002367 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002368}
2369
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002370static int
2371weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2372 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002373{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002374 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002375 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002376 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002377
Daniel Stone6847b852017-03-01 11:34:08 +00002378 /* We're not ready yet; come back to make a decision later. */
2379 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002380 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002381
2382 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2383 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002384 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002385
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002386 /* If we're sleeping, drop the repaint machinery entirely; we will
2387 * explicitly repaint all outputs when we come back. */
2388 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2389 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2390 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002391
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002392 /* We don't actually need to repaint this output; drop it from
2393 * repaint until something causes damage. */
2394 if (!output->repaint_needed)
2395 goto err;
2396
2397 /* If repaint fails, we aren't going to get weston_output_finish_frame
2398 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002399 * something schedules a successful repaint later. As repainting may
2400 * take some time, re-read our clock as a courtesy to the next
2401 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002402 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002403 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002404 if (ret != 0)
2405 goto err;
2406
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002407 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002408
2409err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002410 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002411 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002412}
2413
2414static void
2415output_repaint_timer_arm(struct weston_compositor *compositor)
2416{
2417 struct weston_output *output;
2418 bool any_should_repaint = false;
2419 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002420 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002421
2422 weston_compositor_read_presentation_clock(compositor, &now);
2423
2424 wl_list_for_each(output, &compositor->output_list, link) {
2425 int64_t msec_to_this;
2426
2427 if (output->repaint_status != REPAINT_SCHEDULED)
2428 continue;
2429
2430 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2431 &now);
2432 if (!any_should_repaint || msec_to_this < msec_to_next)
2433 msec_to_next = msec_to_this;
2434
2435 any_should_repaint = true;
2436 }
2437
2438 if (!any_should_repaint)
2439 return;
2440
2441 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2442 * This is a workaround to allow coalescing multiple output repaints
2443 * particularly from weston_output_finish_frame()
2444 * into the same call, which would not happen if we called
2445 * output_repaint_timer_handler() directly.
2446 */
2447 if (msec_to_next < 1)
2448 msec_to_next = 1;
2449
2450 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2451}
2452
2453static int
2454output_repaint_timer_handler(void *data)
2455{
2456 struct weston_compositor *compositor = data;
2457 struct weston_output *output;
2458 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002459 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002460 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002461
2462 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002463
2464 if (compositor->backend->repaint_begin)
2465 repaint_data = compositor->backend->repaint_begin(compositor);
2466
2467 wl_list_for_each(output, &compositor->output_list, link) {
2468 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2469 if (ret)
2470 break;
2471 }
2472
2473 if (ret == 0) {
2474 if (compositor->backend->repaint_flush)
2475 compositor->backend->repaint_flush(compositor,
2476 repaint_data);
2477 } else {
2478 if (compositor->backend->repaint_cancel)
2479 compositor->backend->repaint_cancel(compositor,
2480 repaint_data);
2481 }
Daniel Stone6847b852017-03-01 11:34:08 +00002482
2483 output_repaint_timer_arm(compositor);
2484
Pekka Paalanen0513a952014-05-21 16:17:27 +03002485 return 0;
2486}
2487
Kristian Høgsbergef044142011-06-21 15:02:12 -04002488WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002489weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002490 const struct timespec *stamp,
2491 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002492{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002493 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002494 int32_t refresh_nsec;
2495 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002496 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002497
Pekka Paalanenb5026542014-11-12 15:09:24 +02002498
Daniel Stone05df8c12017-03-03 16:59:42 +00002499 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002500 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2501
Daniel Stone6847b852017-03-01 11:34:08 +00002502 weston_compositor_read_presentation_clock(compositor, &now);
2503
Daniel Stone3615ce12017-03-01 11:34:05 +00002504 /* If we haven't been supplied any timestamp at all, we don't have a
2505 * timebase to work against, so any delay just wastes time. Push a
2506 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002507 if (!stamp) {
2508 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002509 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002510 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002511
Marius Vladdf9278a2018-03-06 18:56:23 +02002512 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2513 TLP_VBLANK(stamp), TLP_END);
2514
Pekka Paalanend7894d02015-07-03 15:08:53 +03002515 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002516 weston_presentation_feedback_present_list(&output->feedback_list,
2517 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002518 output->msc,
2519 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002520
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002521 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002522
Daniel Stone6847b852017-03-01 11:34:08 +00002523 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2524 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2525 -compositor->repaint_msec);
2526 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002527
2528 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002529 static bool warned;
2530
2531 if (!warned)
2532 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002533 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002534 warned = true;
2535
Daniel Stone6847b852017-03-01 11:34:08 +00002536 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002537 }
2538
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002539 /* Called from restart_repaint_loop and restart happens already after
2540 * the deadline given by repaint_msec? In that case we delay until
2541 * the deadline of the next frame, to give clients a more predictable
2542 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002543 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2544 msec_rel < 0) {
2545 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2546 timespec_add_nsec(&output->next_repaint,
2547 &output->next_repaint,
2548 refresh_nsec);
2549 }
2550 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002551
Daniel Stone3615ce12017-03-01 11:34:05 +00002552out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002553 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002554 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002555}
2556
2557static void
2558idle_repaint(void *data)
2559{
2560 struct weston_output *output = data;
2561
Daniel Stone05df8c12017-03-03 16:59:42 +00002562 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2563 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002564 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002565}
2566
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002567WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002568weston_layer_entry_insert(struct weston_layer_entry *list,
2569 struct weston_layer_entry *entry)
2570{
2571 wl_list_insert(&list->link, &entry->link);
2572 entry->layer = list->layer;
2573}
2574
2575WL_EXPORT void
2576weston_layer_entry_remove(struct weston_layer_entry *entry)
2577{
2578 wl_list_remove(&entry->link);
2579 wl_list_init(&entry->link);
2580 entry->layer = NULL;
2581}
2582
Quentin Glidic82681572016-12-17 13:40:51 +01002583
2584/** Initialize the weston_layer struct.
2585 *
2586 * \param compositor The compositor instance
2587 * \param layer The layer to initialize
2588 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002589WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002590weston_layer_init(struct weston_layer *layer,
2591 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002592{
Quentin Glidic82681572016-12-17 13:40:51 +01002593 layer->compositor = compositor;
2594 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002595 wl_list_init(&layer->view_list.link);
2596 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002597 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002598}
2599
2600/** Sets the position of the layer in the layer list. The layer will be placed
2601 * below any layer with the same position value, if any.
2602 * This function is safe to call if the layer is already on the list, but the
2603 * layer may be moved below other layers at the same position, if any.
2604 *
2605 * \param layer The layer to modify
2606 * \param position The position the layer will be placed at
2607 */
2608WL_EXPORT void
2609weston_layer_set_position(struct weston_layer *layer,
2610 enum weston_layer_position position)
2611{
2612 struct weston_layer *below;
2613
2614 wl_list_remove(&layer->link);
2615
2616 /* layer_list is ordered from top to bottom, the last layer being the
2617 * background with the smallest position value */
2618
2619 layer->position = position;
2620 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2621 if (below->position >= layer->position) {
2622 wl_list_insert(&below->link, &layer->link);
2623 return;
2624 }
2625 }
2626 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2627}
2628
2629/** Hide a layer by taking it off the layer list.
2630 * This function is safe to call if the layer is not on the list.
2631 *
2632 * \param layer The layer to hide
2633 */
2634WL_EXPORT void
2635weston_layer_unset_position(struct weston_layer *layer)
2636{
2637 wl_list_remove(&layer->link);
2638 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002639}
2640
2641WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002642weston_layer_set_mask(struct weston_layer *layer,
2643 int x, int y, int width, int height)
2644{
2645 struct weston_view *view;
2646
2647 layer->mask.x1 = x;
2648 layer->mask.x2 = x + width;
2649 layer->mask.y1 = y;
2650 layer->mask.y2 = y + height;
2651
2652 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2653 weston_view_geometry_dirty(view);
2654 }
2655}
2656
2657WL_EXPORT void
2658weston_layer_set_mask_infinite(struct weston_layer *layer)
2659{
2660 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2661 UINT32_MAX, UINT32_MAX);
2662}
2663
2664WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002665weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002666{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002667 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002668 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002669
Bryce Harrington08976ac2016-08-30 12:05:16 -07002670 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2671 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002672 return;
2673
Pekka Paalanenb5026542014-11-12 15:09:24 +02002674 if (!output->repaint_needed)
2675 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2676
Kristian Høgsbergef044142011-06-21 15:02:12 -04002677 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002678 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002679
2680 /* If we already have a repaint scheduled for our idle handler,
2681 * no need to set it again. If the repaint has been called but
2682 * not finished, then weston_output_finish_frame() will notice
2683 * that a repaint is needed and schedule one. */
2684 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002685 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002686
Daniel Stone05df8c12017-03-03 16:59:42 +00002687 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002688 wl_event_loop_add_idle(loop, idle_repaint, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002689 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002690}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002691
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002692WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002693weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2694{
2695 struct weston_output *output;
2696
2697 wl_list_for_each(output, &compositor->output_list, link)
2698 weston_output_schedule_repaint(output);
2699}
2700
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002701static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002702surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002703{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002704 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002705}
2706
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002707static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002708surface_attach(struct wl_client *client,
2709 struct wl_resource *resource,
2710 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2711{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002712 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002713 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002714
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002715 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002716 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002717 if (buffer == NULL) {
2718 wl_client_post_no_memory(client);
2719 return;
2720 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002721 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002722
Pekka Paalanende685b82012-12-04 15:58:12 +02002723 /* Attach, attach, without commit in between does not send
2724 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002725 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002726
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002727 surface->pending.sx = sx;
2728 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002729 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002730}
2731
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002732static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002733surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002734 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002735 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002736{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002737 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002738
Derek Foreman57e92ed2015-11-17 14:11:35 -06002739 if (width <= 0 || height <= 0)
2740 return;
2741
Derek Foreman152254b2015-11-26 14:17:48 -06002742 pixman_region32_union_rect(&surface->pending.damage_surface,
2743 &surface->pending.damage_surface,
2744 x, y, width, height);
2745}
2746
2747static void
2748surface_damage_buffer(struct wl_client *client,
2749 struct wl_resource *resource,
2750 int32_t x, int32_t y, int32_t width, int32_t height)
2751{
2752 struct weston_surface *surface = wl_resource_get_user_data(resource);
2753
2754 if (width <= 0 || height <= 0)
2755 return;
2756
2757 pixman_region32_union_rect(&surface->pending.damage_buffer,
2758 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002759 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002760}
2761
Kristian Høgsberg33418202011-08-16 23:01:28 -04002762static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002763destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002764{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002765 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002766
2767 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002768 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002769}
2770
2771static void
2772surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002773 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002774{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002775 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002776 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002777
2778 cb = malloc(sizeof *cb);
2779 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002780 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002781 return;
2782 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002783
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002784 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2785 callback);
2786 if (cb->resource == NULL) {
2787 free(cb);
2788 wl_resource_post_no_memory(resource);
2789 return;
2790 }
2791
Jason Ekstranda85118c2013-06-27 20:17:02 -05002792 wl_resource_set_implementation(cb->resource, NULL, cb,
2793 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002794
Pekka Paalanenbc106382012-10-10 12:49:31 +03002795 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002796}
2797
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002798static void
2799surface_set_opaque_region(struct wl_client *client,
2800 struct wl_resource *resource,
2801 struct wl_resource *region_resource)
2802{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002803 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002804 struct weston_region *region;
2805
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002806 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002807 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002808 pixman_region32_copy(&surface->pending.opaque,
2809 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002810 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002811 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002812 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002813}
2814
2815static void
2816surface_set_input_region(struct wl_client *client,
2817 struct wl_resource *resource,
2818 struct wl_resource *region_resource)
2819{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002820 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002821 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002822
2823 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002824 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002825 pixman_region32_copy(&surface->pending.input,
2826 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002827 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002828 pixman_region32_fini(&surface->pending.input);
2829 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002830 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002831}
2832
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002833/* Cause damage to this sub-surface and all its children.
2834 *
2835 * This is useful when there are state changes that need an implicit
2836 * damage, e.g. a z-order change.
2837 */
2838static void
2839weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2840{
2841 struct weston_subsurface *child;
2842
2843 weston_surface_damage(sub->surface);
2844 sub->reordered = false;
2845
2846 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2847 if (child != sub)
2848 weston_surface_damage_subsurfaces(child);
2849}
2850
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002851static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002852weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002853{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002854 struct weston_subsurface *sub;
2855
2856 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2857 parent_link_pending) {
2858 wl_list_remove(&sub->parent_link);
2859 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002860
2861 if (sub->reordered)
2862 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002863 }
2864}
2865
2866static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002867weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002868 struct weston_matrix *matrix)
2869{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002870 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002871 double src_width, src_height, dest_width, dest_height;
2872
2873 weston_matrix_init(matrix);
2874
2875 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2876 src_width = surface->width_from_buffer;
2877 src_height = surface->height_from_buffer;
2878 } else {
2879 src_width = wl_fixed_to_double(vp->buffer.src_width);
2880 src_height = wl_fixed_to_double(vp->buffer.src_height);
2881 }
2882
2883 if (vp->surface.width == -1) {
2884 dest_width = src_width;
2885 dest_height = src_height;
2886 } else {
2887 dest_width = vp->surface.width;
2888 dest_height = vp->surface.height;
2889 }
2890
2891 if (src_width != dest_width || src_height != dest_height)
2892 weston_matrix_scale(matrix,
2893 src_width / dest_width,
2894 src_height / dest_height, 1);
2895
2896 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2897 weston_matrix_translate(matrix,
2898 wl_fixed_to_double(vp->buffer.src_x),
2899 wl_fixed_to_double(vp->buffer.src_y),
2900 0);
2901
2902 switch (vp->buffer.transform) {
2903 case WL_OUTPUT_TRANSFORM_FLIPPED:
2904 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2905 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2906 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2907 weston_matrix_scale(matrix, -1, 1, 1);
2908 weston_matrix_translate(matrix,
2909 surface->width_from_buffer, 0, 0);
2910 break;
2911 }
2912
2913 switch (vp->buffer.transform) {
2914 default:
2915 case WL_OUTPUT_TRANSFORM_NORMAL:
2916 case WL_OUTPUT_TRANSFORM_FLIPPED:
2917 break;
2918 case WL_OUTPUT_TRANSFORM_90:
2919 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2920 weston_matrix_rotate_xy(matrix, 0, 1);
2921 weston_matrix_translate(matrix,
2922 surface->height_from_buffer, 0, 0);
2923 break;
2924 case WL_OUTPUT_TRANSFORM_180:
2925 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2926 weston_matrix_rotate_xy(matrix, -1, 0);
2927 weston_matrix_translate(matrix,
2928 surface->width_from_buffer,
2929 surface->height_from_buffer, 0);
2930 break;
2931 case WL_OUTPUT_TRANSFORM_270:
2932 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2933 weston_matrix_rotate_xy(matrix, 0, -1);
2934 weston_matrix_translate(matrix,
2935 0, surface->width_from_buffer, 0);
2936 break;
2937 }
2938
2939 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2940}
2941
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002942/**
2943 * Compute a + b > c while being safe to overflows.
2944 */
2945static bool
2946fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2947{
2948 return (int64_t)a + (int64_t)b > (int64_t)c;
2949}
2950
2951static bool
2952weston_surface_is_pending_viewport_source_valid(
2953 const struct weston_surface *surface)
2954{
2955 const struct weston_surface_state *pend = &surface->pending;
2956 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2957 int width_from_buffer = 0;
2958 int height_from_buffer = 0;
2959 wl_fixed_t w;
2960 wl_fixed_t h;
2961
2962 /* If viewport source rect is not set, it is always ok. */
2963 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2964 return true;
2965
2966 if (pend->newly_attached) {
2967 if (pend->buffer) {
2968 convert_size_by_transform_scale(&width_from_buffer,
2969 &height_from_buffer,
2970 pend->buffer->width,
2971 pend->buffer->height,
2972 vp->buffer.transform,
2973 vp->buffer.scale);
2974 } else {
2975 /* No buffer: viewport is irrelevant. */
2976 return true;
2977 }
2978 } else {
2979 width_from_buffer = surface->width_from_buffer;
2980 height_from_buffer = surface->height_from_buffer;
2981 }
2982
2983 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2984 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2985
2986 /* No buffer: viewport is irrelevant. */
2987 if (width_from_buffer == 0 || height_from_buffer == 0)
2988 return true;
2989
2990 /* overflow checks for wl_fixed_from_int() */
2991 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2992 return false;
2993 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2994 return false;
2995
2996 w = wl_fixed_from_int(width_from_buffer);
2997 h = wl_fixed_from_int(height_from_buffer);
2998
2999 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3000 return false;
3001 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3002 return false;
3003
3004 return true;
3005}
3006
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003007static bool
3008fixed_is_integer(wl_fixed_t v)
3009{
3010 return (v & 0xff) == 0;
3011}
3012
3013static bool
3014weston_surface_is_pending_viewport_dst_size_int(
3015 const struct weston_surface *surface)
3016{
3017 const struct weston_buffer_viewport *vp =
3018 &surface->pending.buffer_viewport;
3019
3020 if (vp->surface.width != -1) {
3021 assert(vp->surface.width > 0 && vp->surface.height > 0);
3022 return true;
3023 }
3024
3025 return fixed_is_integer(vp->buffer.src_width) &&
3026 fixed_is_integer(vp->buffer.src_height);
3027}
3028
Derek Foreman152254b2015-11-26 14:17:48 -06003029/* Translate pending damage in buffer co-ordinates to surface
3030 * co-ordinates and union it with a pixman_region32_t.
3031 * This should only be called after the buffer is attached.
3032 */
3033static void
3034apply_damage_buffer(pixman_region32_t *dest,
3035 struct weston_surface *surface,
3036 struct weston_surface_state *state)
3037{
3038 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3039
3040 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3041 * translated into surface co-ordinates and unioned with
3042 * any other surface damage.
3043 * None of this makes sense if there is no buffer though.
3044 */
3045 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3046 pixman_region32_t buffer_damage;
3047
3048 pixman_region32_intersect_rect(&state->damage_buffer,
3049 &state->damage_buffer,
3050 0, 0, buffer->width,
3051 buffer->height);
3052 pixman_region32_init(&buffer_damage);
3053 weston_matrix_transform_region(&buffer_damage,
3054 &surface->buffer_to_surface_matrix,
3055 &state->damage_buffer);
3056 pixman_region32_union(dest, dest, &buffer_damage);
3057 pixman_region32_fini(&buffer_damage);
3058 }
3059 /* We should clear this on commit even if there was no buffer */
3060 pixman_region32_clear(&state->damage_buffer);
3061}
3062
Jason Ekstrand1e059042014-10-16 10:55:19 -05003063static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003064weston_surface_commit_state(struct weston_surface *surface,
3065 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003066{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003067 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003068 pixman_region32_t opaque;
3069
Alexander Larsson4ea95522013-05-22 14:41:37 +02003070 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003071 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003072 /* wp_viewport.set_source */
3073 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003074 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003075
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003076 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003077 if (state->newly_attached)
3078 weston_surface_attach(surface, state->buffer);
3079 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003080
Jason Ekstrand1e059042014-10-16 10:55:19 -05003081 weston_surface_build_buffer_matrix(surface,
3082 &surface->surface_to_buffer_matrix);
3083 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3084 &surface->surface_to_buffer_matrix);
3085
Jason Ekstrand7b982072014-05-20 14:33:03 -05003086 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003087 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003088 if (surface->committed)
3089 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003090 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003091
Jason Ekstrand7b982072014-05-20 14:33:03 -05003092 state->sx = 0;
3093 state->sy = 0;
3094 state->newly_attached = 0;
3095 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003096
Derek Foreman152254b2015-11-26 14:17:48 -06003097 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003098 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003099 (pixman_region32_not_empty(&state->damage_surface) ||
3100 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003101 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003102
Pekka Paalanen8e159182012-10-10 12:49:25 +03003103 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003104 &state->damage_surface);
3105
3106 apply_damage_buffer(&surface->damage, surface, state);
3107
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003108 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003109 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003110 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003111
3112 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003113 pixman_region32_init(&opaque);
3114 pixman_region32_intersect_rect(&opaque, &state->opaque,
3115 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003116
3117 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3118 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003119 wl_list_for_each(view, &surface->views, surface_link)
3120 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003121 }
3122
3123 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003124
3125 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003126 pixman_region32_intersect_rect(&surface->input, &state->input,
3127 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003128
Pekka Paalanenbc106382012-10-10 12:49:31 +03003129 /* wl_surface.frame */
3130 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003131 &state->frame_callback_list);
3132 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003133
3134 /* XXX:
3135 * What should happen with a feedback request, if there
3136 * is no wl_buffer attached for this commit?
3137 */
3138
3139 /* presentation.feedback */
3140 wl_list_insert_list(&surface->feedback_list,
3141 &state->feedback_list);
3142 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003143
3144 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003145}
3146
3147static void
3148weston_surface_commit(struct weston_surface *surface)
3149{
3150 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003151
Pekka Paalanene67858b2013-04-25 13:57:42 +03003152 weston_surface_commit_subsurface_order(surface);
3153
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003154 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003155}
3156
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003157static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003158weston_subsurface_commit(struct weston_subsurface *sub);
3159
3160static void
3161weston_subsurface_parent_commit(struct weston_subsurface *sub,
3162 int parent_is_synchronized);
3163
3164static void
3165surface_commit(struct wl_client *client, struct wl_resource *resource)
3166{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003167 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003168 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3169
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003170 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3171 assert(surface->viewport_resource);
3172
3173 wl_resource_post_error(surface->viewport_resource,
3174 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3175 "wl_surface@%d has viewport source outside buffer",
3176 wl_resource_get_id(resource));
3177 return;
3178 }
3179
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003180 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3181 assert(surface->viewport_resource);
3182
3183 wl_resource_post_error(surface->viewport_resource,
3184 WP_VIEWPORT_ERROR_BAD_SIZE,
3185 "wl_surface@%d viewport dst size not integer",
3186 wl_resource_get_id(resource));
3187 return;
3188 }
3189
Pekka Paalanene67858b2013-04-25 13:57:42 +03003190 if (sub) {
3191 weston_subsurface_commit(sub);
3192 return;
3193 }
3194
3195 weston_surface_commit(surface);
3196
3197 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3198 if (sub->surface != surface)
3199 weston_subsurface_parent_commit(sub, 0);
3200 }
3201}
3202
3203static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003204surface_set_buffer_transform(struct wl_client *client,
3205 struct wl_resource *resource, int transform)
3206{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003207 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003208
Jonny Lamba55f1392014-05-30 12:07:15 +02003209 /* if wl_output.transform grows more members this will need to be updated. */
3210 if (transform < 0 ||
3211 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3212 wl_resource_post_error(resource,
3213 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3214 "buffer transform must be a valid transform "
3215 "('%d' specified)", transform);
3216 return;
3217 }
3218
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003219 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003220 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003221}
3222
Alexander Larsson4ea95522013-05-22 14:41:37 +02003223static void
3224surface_set_buffer_scale(struct wl_client *client,
3225 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003226 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003227{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003228 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003229
Jonny Lamba55f1392014-05-30 12:07:15 +02003230 if (scale < 1) {
3231 wl_resource_post_error(resource,
3232 WL_SURFACE_ERROR_INVALID_SCALE,
3233 "buffer scale must be at least one "
3234 "('%d' specified)", scale);
3235 return;
3236 }
3237
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003238 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003239 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003240}
3241
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003242static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003243 surface_destroy,
3244 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003245 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003246 surface_frame,
3247 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003248 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003249 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003250 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003251 surface_set_buffer_scale,
3252 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003253};
3254
3255static void
3256compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003257 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003258{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003259 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003260 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003261
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003262 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003263 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003264 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003265 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003266 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003267
Jason Ekstranda85118c2013-06-27 20:17:02 -05003268 surface->resource =
3269 wl_resource_create(client, &wl_surface_interface,
3270 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003271 if (surface->resource == NULL) {
3272 weston_surface_destroy(surface);
3273 wl_resource_post_no_memory(resource);
3274 return;
3275 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003276 wl_resource_set_implementation(surface->resource, &surface_interface,
3277 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003278
3279 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003280}
3281
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003282static void
3283destroy_region(struct wl_resource *resource)
3284{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003285 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003286
3287 pixman_region32_fini(&region->region);
3288 free(region);
3289}
3290
3291static void
3292region_destroy(struct wl_client *client, struct wl_resource *resource)
3293{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003294 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003295}
3296
3297static void
3298region_add(struct wl_client *client, struct wl_resource *resource,
3299 int32_t x, int32_t y, int32_t width, int32_t height)
3300{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003301 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003302
3303 pixman_region32_union_rect(&region->region, &region->region,
3304 x, y, width, height);
3305}
3306
3307static void
3308region_subtract(struct wl_client *client, struct wl_resource *resource,
3309 int32_t x, int32_t y, int32_t width, int32_t height)
3310{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003311 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003312 pixman_region32_t rect;
3313
3314 pixman_region32_init_rect(&rect, x, y, width, height);
3315 pixman_region32_subtract(&region->region, &region->region, &rect);
3316 pixman_region32_fini(&rect);
3317}
3318
3319static const struct wl_region_interface region_interface = {
3320 region_destroy,
3321 region_add,
3322 region_subtract
3323};
3324
3325static void
3326compositor_create_region(struct wl_client *client,
3327 struct wl_resource *resource, uint32_t id)
3328{
3329 struct weston_region *region;
3330
3331 region = malloc(sizeof *region);
3332 if (region == NULL) {
3333 wl_resource_post_no_memory(resource);
3334 return;
3335 }
3336
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003337 pixman_region32_init(&region->region);
3338
Jason Ekstranda85118c2013-06-27 20:17:02 -05003339 region->resource =
3340 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003341 if (region->resource == NULL) {
3342 free(region);
3343 wl_resource_post_no_memory(resource);
3344 return;
3345 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003346 wl_resource_set_implementation(region->resource, &region_interface,
3347 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003348}
3349
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003350static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003351 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003352 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003353};
3354
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003355static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003356weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3357{
3358 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003359
Jason Ekstrand7b982072014-05-20 14:33:03 -05003360 weston_surface_commit_state(surface, &sub->cached);
3361 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003362
3363 weston_surface_commit_subsurface_order(surface);
3364
3365 weston_surface_schedule_repaint(surface);
3366
Jason Ekstrand7b982072014-05-20 14:33:03 -05003367 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003368}
3369
3370static void
3371weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3372{
3373 struct weston_surface *surface = sub->surface;
3374
3375 /*
3376 * If this commit would cause the surface to move by the
3377 * attach(dx, dy) parameters, the old damage region must be
3378 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003379 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003380 */
Derek Foreman152254b2015-11-26 14:17:48 -06003381 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003382 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003383 pixman_region32_union(&sub->cached.damage_surface,
3384 &sub->cached.damage_surface,
3385 &surface->pending.damage_surface);
3386 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003387
3388 if (surface->pending.newly_attached) {
3389 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003390 weston_surface_state_set_buffer(&sub->cached,
3391 surface->pending.buffer);
3392 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003393 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003394 weston_presentation_feedback_discard_list(
3395 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003396 }
3397 sub->cached.sx += surface->pending.sx;
3398 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003399
Derek Foreman152254b2015-11-26 14:17:48 -06003400 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3401
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003402 sub->cached.buffer_viewport.changed |=
3403 surface->pending.buffer_viewport.changed;
3404 sub->cached.buffer_viewport.buffer =
3405 surface->pending.buffer_viewport.buffer;
3406 sub->cached.buffer_viewport.surface =
3407 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003408
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003409 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003410
3411 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3412
3413 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3414
3415 wl_list_insert_list(&sub->cached.frame_callback_list,
3416 &surface->pending.frame_callback_list);
3417 wl_list_init(&surface->pending.frame_callback_list);
3418
Pekka Paalanen133e4392014-09-23 22:08:46 -04003419 wl_list_insert_list(&sub->cached.feedback_list,
3420 &surface->pending.feedback_list);
3421 wl_list_init(&surface->pending.feedback_list);
3422
Jason Ekstrand7b982072014-05-20 14:33:03 -05003423 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003424}
3425
Derek Foreman280e7dd2014-10-03 13:13:42 -05003426static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003427weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3428{
3429 while (sub) {
3430 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003431 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003432
3433 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003434 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003435
3436 sub = weston_surface_to_subsurface(sub->parent);
3437 }
3438
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003439 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003440}
3441
3442static void
3443weston_subsurface_commit(struct weston_subsurface *sub)
3444{
3445 struct weston_surface *surface = sub->surface;
3446 struct weston_subsurface *tmp;
3447
3448 /* Recursive check for effectively synchronized. */
3449 if (weston_subsurface_is_synchronized(sub)) {
3450 weston_subsurface_commit_to_cache(sub);
3451 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003452 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003453 /* flush accumulated state from cache */
3454 weston_subsurface_commit_to_cache(sub);
3455 weston_subsurface_commit_from_cache(sub);
3456 } else {
3457 weston_surface_commit(surface);
3458 }
3459
3460 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3461 if (tmp->surface != surface)
3462 weston_subsurface_parent_commit(tmp, 0);
3463 }
3464 }
3465}
3466
3467static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003468weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003469{
3470 struct weston_surface *surface = sub->surface;
3471 struct weston_subsurface *tmp;
3472
Pekka Paalanene67858b2013-04-25 13:57:42 +03003473 /* From now on, commit_from_cache the whole sub-tree, regardless of
3474 * the synchronized mode of each child. This sub-surface or some
3475 * of its ancestors were synchronized, so we are synchronized
3476 * all the way down.
3477 */
3478
Jason Ekstrand7b982072014-05-20 14:33:03 -05003479 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003480 weston_subsurface_commit_from_cache(sub);
3481
3482 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3483 if (tmp->surface != surface)
3484 weston_subsurface_parent_commit(tmp, 1);
3485 }
3486}
3487
3488static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003489weston_subsurface_parent_commit(struct weston_subsurface *sub,
3490 int parent_is_synchronized)
3491{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003492 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003493 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003494 wl_list_for_each(view, &sub->surface->views, surface_link)
3495 weston_view_set_position(view,
3496 sub->position.x,
3497 sub->position.y);
3498
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003499 sub->position.set = 0;
3500 }
3501
3502 if (parent_is_synchronized || sub->synchronized)
3503 weston_subsurface_synchronized_commit(sub);
3504}
3505
Pekka Paalanen8274d902014-08-06 19:36:51 +03003506static int
3507subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3508{
3509 return snprintf(buf, len, "sub-surface");
3510}
3511
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003512static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003513subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003514{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003515 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003516
Jason Ekstranda7af7042013-10-12 22:38:11 -05003517 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003518 weston_view_set_position(view,
3519 view->geometry.x + dx,
3520 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003521
3522 /* No need to check parent mappedness, because if parent is not
3523 * mapped, parent is not in a visible layer, so this sub-surface
3524 * will not be drawn either.
3525 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003526
Pekka Paalanene67858b2013-04-25 13:57:42 +03003527 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003528 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003529
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003530 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003531 * because that would call it also for the parent surface,
3532 * which might not be mapped yet. That would lead to
3533 * inconsistent state, where the window could never be
3534 * mapped.
3535 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003536 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003537 * weston_surface_is_mapped() return true, so that when the
3538 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003539 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003540 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003541 }
3542}
3543
3544static struct weston_subsurface *
3545weston_surface_to_subsurface(struct weston_surface *surface)
3546{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003547 if (surface->committed == subsurface_committed)
3548 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003549
3550 return NULL;
3551}
3552
Pekka Paalanen01388e22013-04-25 13:57:44 +03003553WL_EXPORT struct weston_surface *
3554weston_surface_get_main_surface(struct weston_surface *surface)
3555{
3556 struct weston_subsurface *sub;
3557
3558 while (surface && (sub = weston_surface_to_subsurface(surface)))
3559 surface = sub->parent;
3560
3561 return surface;
3562}
3563
Pekka Paalanen50b67472014-10-01 15:02:41 +03003564WL_EXPORT int
3565weston_surface_set_role(struct weston_surface *surface,
3566 const char *role_name,
3567 struct wl_resource *error_resource,
3568 uint32_t error_code)
3569{
3570 assert(role_name);
3571
3572 if (surface->role_name == NULL ||
3573 surface->role_name == role_name ||
3574 strcmp(surface->role_name, role_name) == 0) {
3575 surface->role_name = role_name;
3576
3577 return 0;
3578 }
3579
3580 wl_resource_post_error(error_resource, error_code,
3581 "Cannot assign role %s to wl_surface@%d,"
3582 " already has role %s\n",
3583 role_name,
3584 wl_resource_get_id(surface->resource),
3585 surface->role_name);
3586 return -1;
3587}
3588
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003589WL_EXPORT const char *
3590weston_surface_get_role(struct weston_surface *surface)
3591{
3592 return surface->role_name;
3593}
3594
Pekka Paalanen8274d902014-08-06 19:36:51 +03003595WL_EXPORT void
3596weston_surface_set_label_func(struct weston_surface *surface,
3597 int (*desc)(struct weston_surface *,
3598 char *, size_t))
3599{
3600 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003601 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003602}
3603
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003604/** Get the size of surface contents
3605 *
3606 * \param surface The surface to query.
3607 * \param width Returns the width of raw contents.
3608 * \param height Returns the height of raw contents.
3609 *
3610 * Retrieves the raw surface content size in pixels for the given surface.
3611 * This is the whole content size in buffer pixels. If the surface
3612 * has no content or the renderer does not implement this feature,
3613 * zeroes are returned.
3614 *
3615 * This function is used to determine the buffer size needed for
3616 * a weston_surface_copy_content() call.
3617 */
3618WL_EXPORT void
3619weston_surface_get_content_size(struct weston_surface *surface,
3620 int *width, int *height)
3621{
3622 struct weston_renderer *rer = surface->compositor->renderer;
3623
3624 if (!rer->surface_get_content_size) {
3625 *width = 0;
3626 *height = 0;
3627 return;
3628 }
3629
3630 rer->surface_get_content_size(surface, width, height);
3631}
3632
Quentin Glidic248dd102016-08-12 10:41:34 +02003633/** Get the bounding box of a surface and its subsurfaces
3634 *
3635 * \param surface The surface to query.
3636 * \return The bounding box relative to the surface origin.
3637 *
3638 */
3639WL_EXPORT struct weston_geometry
3640weston_surface_get_bounding_box(struct weston_surface *surface)
3641{
3642 pixman_region32_t region;
3643 pixman_box32_t *box;
3644 struct weston_subsurface *subsurface;
3645
3646 pixman_region32_init_rect(&region,
3647 0, 0,
3648 surface->width, surface->height);
3649
3650 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3651 pixman_region32_union_rect(&region, &region,
3652 subsurface->position.x,
3653 subsurface->position.y,
3654 subsurface->surface->width,
3655 subsurface->surface->height);
3656
3657 box = pixman_region32_extents(&region);
3658 struct weston_geometry geometry = {
3659 .x = box->x1,
3660 .y = box->y1,
3661 .width = box->x2 - box->x1,
3662 .height = box->y2 - box->y1,
3663 };
3664
3665 pixman_region32_fini(&region);
3666
3667 return geometry;
3668}
3669
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003670/** Copy surface contents to system memory.
3671 *
3672 * \param surface The surface to copy from.
3673 * \param target Pointer to the target memory buffer.
3674 * \param size Size of the target buffer in bytes.
3675 * \param src_x X location on contents to copy from.
3676 * \param src_y Y location on contents to copy from.
3677 * \param width Width in pixels of the area to copy.
3678 * \param height Height in pixels of the area to copy.
3679 * \return 0 for success, -1 for failure.
3680 *
3681 * Surface contents are maintained by the renderer. They can be in a
3682 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3683 * else.
3684 *
3685 * Surface contents are copied into memory pointed to by target,
3686 * which has size bytes of space available. The target memory
3687 * may be larger than needed, but being smaller returns an error.
3688 * The extra bytes in target may or may not be written; their content is
3689 * unspecified. Size must be large enough to hold the image.
3690 *
3691 * The image in the target memory will be arranged in rows from
3692 * top to bottom, and pixels on a row from left to right. The pixel
3693 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3694 * width * 4.
3695 *
3696 * Parameters src_x and src_y define the upper-left corner in buffer
3697 * coordinates (pixels) to copy from. Parameters width and height
3698 * define the size of the area to copy in pixels.
3699 *
3700 * The rectangle defined by src_x, src_y, width, height must fit in
3701 * the surface contents. Otherwise an error is returned.
3702 *
3703 * Use surface_get_data_size to determine the content size; the
3704 * needed target buffer size and rectangle limits.
3705 *
3706 * CURRENT IMPLEMENTATION RESTRICTIONS:
3707 * - the machine must be little-endian due to Pixman formats.
3708 *
3709 * NOTE: Pixman formats are premultiplied.
3710 */
3711WL_EXPORT int
3712weston_surface_copy_content(struct weston_surface *surface,
3713 void *target, size_t size,
3714 int src_x, int src_y,
3715 int width, int height)
3716{
3717 struct weston_renderer *rer = surface->compositor->renderer;
3718 int cw, ch;
3719 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3720
3721 if (!rer->surface_copy_content)
3722 return -1;
3723
3724 weston_surface_get_content_size(surface, &cw, &ch);
3725
3726 if (src_x < 0 || src_y < 0)
3727 return -1;
3728
3729 if (width <= 0 || height <= 0)
3730 return -1;
3731
3732 if (src_x + width > cw || src_y + height > ch)
3733 return -1;
3734
3735 if (width * bytespp * height > size)
3736 return -1;
3737
3738 return rer->surface_copy_content(surface, target, size,
3739 src_x, src_y, width, height);
3740}
3741
Pekka Paalanene67858b2013-04-25 13:57:42 +03003742static void
3743subsurface_set_position(struct wl_client *client,
3744 struct wl_resource *resource, int32_t x, int32_t y)
3745{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003746 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003747
3748 if (!sub)
3749 return;
3750
3751 sub->position.x = x;
3752 sub->position.y = y;
3753 sub->position.set = 1;
3754}
3755
3756static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003757subsurface_find_sibling(struct weston_subsurface *sub,
3758 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003759{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003760 struct weston_surface *parent = sub->parent;
3761 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003762
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003763 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3764 if (sibling->surface == surface && sibling != sub)
3765 return sibling;
3766 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003767
3768 return NULL;
3769}
3770
3771static struct weston_subsurface *
3772subsurface_sibling_check(struct weston_subsurface *sub,
3773 struct weston_surface *surface,
3774 const char *request)
3775{
3776 struct weston_subsurface *sibling;
3777
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003778 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003779 if (!sibling) {
3780 wl_resource_post_error(sub->resource,
3781 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3782 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003783 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003784 return NULL;
3785 }
3786
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003787 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003788
3789 return sibling;
3790}
3791
3792static void
3793subsurface_place_above(struct wl_client *client,
3794 struct wl_resource *resource,
3795 struct wl_resource *sibling_resource)
3796{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003797 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003798 struct weston_surface *surface =
3799 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003800 struct weston_subsurface *sibling;
3801
3802 if (!sub)
3803 return;
3804
3805 sibling = subsurface_sibling_check(sub, surface, "place_above");
3806 if (!sibling)
3807 return;
3808
3809 wl_list_remove(&sub->parent_link_pending);
3810 wl_list_insert(sibling->parent_link_pending.prev,
3811 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003812
3813 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003814}
3815
3816static void
3817subsurface_place_below(struct wl_client *client,
3818 struct wl_resource *resource,
3819 struct wl_resource *sibling_resource)
3820{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003821 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003822 struct weston_surface *surface =
3823 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003824 struct weston_subsurface *sibling;
3825
3826 if (!sub)
3827 return;
3828
3829 sibling = subsurface_sibling_check(sub, surface, "place_below");
3830 if (!sibling)
3831 return;
3832
3833 wl_list_remove(&sub->parent_link_pending);
3834 wl_list_insert(&sibling->parent_link_pending,
3835 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003836
3837 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003838}
3839
3840static void
3841subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3842{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003843 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003844
3845 if (sub)
3846 sub->synchronized = 1;
3847}
3848
3849static void
3850subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3851{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003852 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003853
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003854 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003855 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003856
3857 /* If sub became effectively desynchronized, flush. */
3858 if (!weston_subsurface_is_synchronized(sub))
3859 weston_subsurface_synchronized_commit(sub);
3860 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003861}
3862
3863static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003864weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3865{
3866 wl_list_remove(&sub->parent_link);
3867 wl_list_remove(&sub->parent_link_pending);
3868 wl_list_remove(&sub->parent_destroy_listener.link);
3869 sub->parent = NULL;
3870}
3871
3872static void
3873weston_subsurface_destroy(struct weston_subsurface *sub);
3874
3875static void
3876subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3877{
3878 struct weston_subsurface *sub =
3879 container_of(listener, struct weston_subsurface,
3880 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003881 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003882
3883 /* The protocol object (wl_resource) is left inert. */
3884 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003885 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003886
3887 weston_subsurface_destroy(sub);
3888}
3889
3890static void
3891subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3892{
3893 struct weston_subsurface *sub =
3894 container_of(listener, struct weston_subsurface,
3895 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003896 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003897 assert(sub->surface != sub->parent);
3898
3899 if (weston_surface_is_mapped(sub->surface))
3900 weston_surface_unmap(sub->surface);
3901
3902 weston_subsurface_unlink_parent(sub);
3903}
3904
3905static void
3906subsurface_resource_destroy(struct wl_resource *resource)
3907{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003908 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003909
3910 if (sub)
3911 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003912}
3913
3914static void
3915subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3916{
3917 wl_resource_destroy(resource);
3918}
3919
3920static void
3921weston_subsurface_link_parent(struct weston_subsurface *sub,
3922 struct weston_surface *parent)
3923{
3924 sub->parent = parent;
3925 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003926 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003927 &sub->parent_destroy_listener);
3928
3929 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3930 wl_list_insert(&parent->subsurface_list_pending,
3931 &sub->parent_link_pending);
3932}
3933
3934static void
3935weston_subsurface_link_surface(struct weston_subsurface *sub,
3936 struct weston_surface *surface)
3937{
3938 sub->surface = surface;
3939 sub->surface_destroy_listener.notify =
3940 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003941 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003942 &sub->surface_destroy_listener);
3943}
3944
3945static void
3946weston_subsurface_destroy(struct weston_subsurface *sub)
3947{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003948 struct weston_view *view, *next;
3949
Pekka Paalanene67858b2013-04-25 13:57:42 +03003950 assert(sub->surface);
3951
3952 if (sub->resource) {
3953 assert(weston_surface_to_subsurface(sub->surface) == sub);
3954 assert(sub->parent_destroy_listener.notify ==
3955 subsurface_handle_parent_destroy);
3956
George Kiagiadakised04d382014-06-13 18:10:26 +02003957 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3958 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003959 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003960 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003961
Pekka Paalanene67858b2013-04-25 13:57:42 +03003962 if (sub->parent)
3963 weston_subsurface_unlink_parent(sub);
3964
Jason Ekstrand7b982072014-05-20 14:33:03 -05003965 weston_surface_state_fini(&sub->cached);
3966 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003967
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003968 sub->surface->committed = NULL;
3969 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003970 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003971 } else {
3972 /* the dummy weston_subsurface for the parent itself */
3973 assert(sub->parent_destroy_listener.notify == NULL);
3974 wl_list_remove(&sub->parent_link);
3975 wl_list_remove(&sub->parent_link_pending);
3976 }
3977
3978 wl_list_remove(&sub->surface_destroy_listener.link);
3979 free(sub);
3980}
3981
3982static const struct wl_subsurface_interface subsurface_implementation = {
3983 subsurface_destroy,
3984 subsurface_set_position,
3985 subsurface_place_above,
3986 subsurface_place_below,
3987 subsurface_set_sync,
3988 subsurface_set_desync
3989};
3990
3991static struct weston_subsurface *
3992weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3993 struct weston_surface *parent)
3994{
3995 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003996 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003997
Bryce Harringtonde16d892014-11-20 22:21:57 -08003998 sub = zalloc(sizeof *sub);
3999 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004000 return NULL;
4001
Jason Ekstranda7af7042013-10-12 22:38:11 -05004002 wl_list_init(&sub->unused_views);
4003
Jason Ekstranda85118c2013-06-27 20:17:02 -05004004 sub->resource =
4005 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004006 if (!sub->resource) {
4007 free(sub);
4008 return NULL;
4009 }
4010
Jason Ekstranda85118c2013-06-27 20:17:02 -05004011 wl_resource_set_implementation(sub->resource,
4012 &subsurface_implementation,
4013 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004014 weston_subsurface_link_surface(sub, surface);
4015 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004016 weston_surface_state_init(&sub->cached);
4017 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004018 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004019
4020 return sub;
4021}
4022
4023/* Create a dummy subsurface for having the parent itself in its
4024 * sub-surface lists. Makes stacking order manipulation easy.
4025 */
4026static struct weston_subsurface *
4027weston_subsurface_create_for_parent(struct weston_surface *parent)
4028{
4029 struct weston_subsurface *sub;
4030
Bryce Harringtonde16d892014-11-20 22:21:57 -08004031 sub = zalloc(sizeof *sub);
4032 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004033 return NULL;
4034
4035 weston_subsurface_link_surface(sub, parent);
4036 sub->parent = parent;
4037 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4038 wl_list_insert(&parent->subsurface_list_pending,
4039 &sub->parent_link_pending);
4040
4041 return sub;
4042}
4043
4044static void
4045subcompositor_get_subsurface(struct wl_client *client,
4046 struct wl_resource *resource,
4047 uint32_t id,
4048 struct wl_resource *surface_resource,
4049 struct wl_resource *parent_resource)
4050{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004051 struct weston_surface *surface =
4052 wl_resource_get_user_data(surface_resource);
4053 struct weston_surface *parent =
4054 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004055 struct weston_subsurface *sub;
4056 static const char where[] = "get_subsurface: wl_subsurface@";
4057
4058 if (surface == parent) {
4059 wl_resource_post_error(resource,
4060 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4061 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004062 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004063 return;
4064 }
4065
4066 if (weston_surface_to_subsurface(surface)) {
4067 wl_resource_post_error(resource,
4068 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4069 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004070 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004071 return;
4072 }
4073
Pekka Paalanen50b67472014-10-01 15:02:41 +03004074 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4075 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004076 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004077
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004078 if (weston_surface_get_main_surface(parent) == surface) {
4079 wl_resource_post_error(resource,
4080 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4081 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004082 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004083 return;
4084 }
4085
Pekka Paalanene67858b2013-04-25 13:57:42 +03004086 /* make sure the parent is in its own list */
4087 if (wl_list_empty(&parent->subsurface_list)) {
4088 if (!weston_subsurface_create_for_parent(parent)) {
4089 wl_resource_post_no_memory(resource);
4090 return;
4091 }
4092 }
4093
4094 sub = weston_subsurface_create(id, surface, parent);
4095 if (!sub) {
4096 wl_resource_post_no_memory(resource);
4097 return;
4098 }
4099
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004100 surface->committed = subsurface_committed;
4101 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004102 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004103}
4104
4105static void
4106subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4107{
4108 wl_resource_destroy(resource);
4109}
4110
4111static const struct wl_subcompositor_interface subcompositor_interface = {
4112 subcompositor_destroy,
4113 subcompositor_get_subsurface
4114};
4115
4116static void
4117bind_subcompositor(struct wl_client *client,
4118 void *data, uint32_t version, uint32_t id)
4119{
4120 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004121 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004122
Jason Ekstranda85118c2013-06-27 20:17:02 -05004123 resource =
4124 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004125 if (resource == NULL) {
4126 wl_client_post_no_memory(client);
4127 return;
4128 }
4129 wl_resource_set_implementation(resource, &subcompositor_interface,
4130 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004131}
4132
Bryce Harrington0795ece2016-08-30 12:04:26 -07004133/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004134 *
4135 * \param compositor The compositor instance
4136 * \param state The DPMS state the outputs will be set to
4137 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004138static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004139weston_compositor_dpms(struct weston_compositor *compositor,
4140 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004141{
4142 struct weston_output *output;
4143
Bryce Harrington08976ac2016-08-30 12:05:16 -07004144 wl_list_for_each(output, &compositor->output_list, link)
4145 if (output->set_dpms)
4146 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004147}
4148
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004149/** Restores the compositor to active status
4150 *
4151 * \param compositor The compositor instance
4152 *
4153 * If the compositor was in a sleeping mode, all outputs are powered
4154 * back on via DPMS. Otherwise if the compositor was inactive
4155 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4156 * signal will fire.
4157 *
4158 * Restarts the idle timer.
4159 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004160WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004161weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004162{
Neil Roberts8b62e202013-09-30 13:14:47 +01004163 uint32_t old_state = compositor->state;
4164
4165 /* The state needs to be changed before emitting the wake
4166 * signal because that may try to schedule a repaint which
4167 * will not work if the compositor is still sleeping */
4168 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4169
4170 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004171 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004172 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004173 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004174 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004175 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004176 /* fall through */
4177 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004178 wl_event_source_timer_update(compositor->idle_source,
4179 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004180 }
4181}
4182
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004183/** Turns off rendering and frame events for the compositor.
4184 *
4185 * \param compositor The compositor instance
4186 *
4187 * This is used for example to prevent further rendering while the
4188 * compositor is shutting down.
4189 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004190 * Stops the idle timer.
4191 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004192WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004193weston_compositor_offscreen(struct weston_compositor *compositor)
4194{
4195 switch (compositor->state) {
4196 case WESTON_COMPOSITOR_OFFSCREEN:
4197 return;
4198 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004199 default:
4200 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4201 wl_event_source_timer_update(compositor->idle_source, 0);
4202 }
4203}
4204
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004205/** Powers down all attached output devices
4206 *
4207 * \param compositor The compositor instance
4208 *
4209 * Causes rendering to the outputs to cease, and no frame events to be
4210 * sent. Only powers down the outputs if the compositor is not already
4211 * in sleep mode.
4212 *
4213 * Stops the idle timer.
4214 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004215WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004216weston_compositor_sleep(struct weston_compositor *compositor)
4217{
4218 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4219 return;
4220
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004221 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004222 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4223 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4224}
4225
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004226/** Sets compositor to idle mode
4227 *
4228 * \param data The compositor instance
4229 *
4230 * This is called when the idle timer fires. Once the compositor is in
4231 * idle mode it requires a wake action (e.g. via
4232 * weston_compositor_wake()) to restore it. The compositor's
4233 * idle_signal will be triggered when the idle event occurs.
4234 *
4235 * Idleness can be inhibited by setting the compositor's idle_inhibit
4236 * property.
4237 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004238static int
4239idle_handler(void *data)
4240{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004241 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004242
4243 if (compositor->idle_inhibit)
4244 return 1;
4245
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004246 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004247 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004248
4249 return 1;
4250}
4251
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004252WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004253weston_plane_init(struct weston_plane *plane,
4254 struct weston_compositor *ec,
4255 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004256{
4257 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004258 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004259 plane->x = x;
4260 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004261 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004262
4263 /* Init the link so that the call to wl_list_remove() when releasing
4264 * the plane without ever stacking doesn't lead to a crash */
4265 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004266}
4267
4268WL_EXPORT void
4269weston_plane_release(struct weston_plane *plane)
4270{
Xiong Zhang97116532013-10-23 13:58:31 +08004271 struct weston_view *view;
4272
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004273 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004274 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004275
Xiong Zhang97116532013-10-23 13:58:31 +08004276 wl_list_for_each(view, &plane->compositor->view_list, link) {
4277 if (view->plane == plane)
4278 view->plane = NULL;
4279 }
4280
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004281 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004282}
4283
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004284WL_EXPORT void
4285weston_compositor_stack_plane(struct weston_compositor *ec,
4286 struct weston_plane *plane,
4287 struct weston_plane *above)
4288{
4289 if (above)
4290 wl_list_insert(above->link.prev, &plane->link);
4291 else
4292 wl_list_insert(&ec->plane_list, &plane->link);
4293}
4294
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004295static void
4296output_release(struct wl_client *client, struct wl_resource *resource)
4297{
4298 wl_resource_destroy(resource);
4299}
4300
4301static const struct wl_output_interface output_interface = {
4302 output_release,
4303};
4304
4305
Casey Dahlin9074db52012-04-19 22:50:09 -04004306static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004307{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004308 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004309}
4310
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004311static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004312bind_output(struct wl_client *client,
4313 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004314{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004315 struct weston_output *output = data;
4316 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004317 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004318
Jason Ekstranda85118c2013-06-27 20:17:02 -05004319 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004320 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004321 if (resource == NULL) {
4322 wl_client_post_no_memory(client);
4323 return;
4324 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004325
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004326 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004327 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004328
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004329 wl_output_send_geometry(resource,
4330 output->x,
4331 output->y,
4332 output->mm_width,
4333 output->mm_height,
4334 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004335 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004336 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004337 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004338 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004339 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004340
4341 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004342 wl_output_send_mode(resource,
4343 mode->flags,
4344 mode->width,
4345 mode->height,
4346 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004347 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004348
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004349 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004350 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004351}
4352
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004353/** Get the backing object of wl_output
4354 *
4355 * \param resource A wl_output protocol object.
4356 * \return The backing object (user data) of a wl_resource representing a
4357 * wl_output protocol object.
4358 */
4359WL_EXPORT struct weston_output *
4360weston_output_from_resource(struct wl_resource *resource)
4361{
4362 assert(wl_resource_instance_of(resource, &wl_output_interface,
4363 &output_interface));
4364
4365 return wl_resource_get_user_data(resource);
4366}
4367
4368
David Fort0de859e2016-05-27 23:22:57 +02004369/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004370static void
David Fort0de859e2016-05-27 23:22:57 +02004371weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4372 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004373{
4374 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004375 bool start_resizing = false;
4376
4377 if (!delta_width)
4378 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004379
4380 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004381 if (output == resized_output) {
4382 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004383 continue;
4384 }
4385
David Fort0de859e2016-05-27 23:22:57 +02004386 if (start_resizing) {
4387 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004388 output->dirty = 1;
4389 }
4390 }
4391}
4392
Pekka Paalanend72bad22017-03-29 17:01:41 +03004393static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004394weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004395{
Scott Moreau850ca422012-05-21 15:21:25 -06004396 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004397
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004398 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004399 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004400
Scott Moreauccbf29d2012-02-22 14:21:41 -07004401 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004402 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004403 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004404 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004405 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004406 weston_matrix_scale(&output->matrix, magnification,
4407 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004408 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004409
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004410 switch (output->transform) {
4411 case WL_OUTPUT_TRANSFORM_FLIPPED:
4412 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4413 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4414 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4415 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4416 weston_matrix_scale(&output->matrix, -1, 1, 1);
4417 break;
4418 }
4419
4420 switch (output->transform) {
4421 default:
4422 case WL_OUTPUT_TRANSFORM_NORMAL:
4423 case WL_OUTPUT_TRANSFORM_FLIPPED:
4424 break;
4425 case WL_OUTPUT_TRANSFORM_90:
4426 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4427 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4428 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4429 break;
4430 case WL_OUTPUT_TRANSFORM_180:
4431 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4432 weston_matrix_translate(&output->matrix,
4433 -output->width, -output->height, 0);
4434 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4435 break;
4436 case WL_OUTPUT_TRANSFORM_270:
4437 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4438 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4439 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4440 break;
4441 }
4442
4443 if (output->current_scale != 1)
4444 weston_matrix_scale(&output->matrix,
4445 output->current_scale,
4446 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004447
Scott Moreauccbf29d2012-02-22 14:21:41 -07004448 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004449
4450 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004451}
4452
Scott Moreau1bad5db2012-08-18 01:04:05 -06004453static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004454weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004455{
4456 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004457 output->native_scale = scale;
4458 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004459
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004460 convert_size_by_transform_scale(&output->width, &output->height,
4461 output->current_mode->width,
4462 output->current_mode->height,
4463 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004464}
4465
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004466static void
4467weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004468{
4469 output->x = x;
4470 output->y = y;
4471
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004472 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004473 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004474
4475 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004476 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004477 output->width,
4478 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004479}
4480
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004481WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004482weston_output_move(struct weston_output *output, int x, int y)
4483{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004484 struct wl_resource *resource;
4485
4486 output->move_x = x - output->x;
4487 output->move_y = y - output->y;
4488
4489 if (output->move_x == 0 && output->move_y == 0)
4490 return;
4491
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004492 weston_output_init_geometry(output, x, y);
4493
4494 output->dirty = 1;
4495
4496 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004497 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004498
4499 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004500 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004501 wl_output_send_geometry(resource,
4502 output->x,
4503 output->y,
4504 output->mm_width,
4505 output->mm_height,
4506 output->subpixel,
4507 output->make,
4508 output->model,
4509 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004510
FORT David8a120692016-04-26 23:34:06 +02004511 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004512 wl_output_send_done(resource);
4513 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004514}
4515
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004516/** Signal that a pending output is taken into use.
4517 *
4518 * Removes the output from the pending list and adds it to the compositor's
4519 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03004520 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004521 * The output gets an internal ID assigned, and the wl_output global is
4522 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004523 *
Giulio Camuffob1147152015-05-06 21:41:57 +03004524 * \param compositor The compositor instance.
4525 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004526 *
4527 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03004528 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03004529static void
Giulio Camuffob1147152015-05-06 21:41:57 +03004530weston_compositor_add_output(struct weston_compositor *compositor,
4531 struct weston_output *output)
4532{
Armin Krezoviće5403842016-08-05 15:28:29 +02004533 struct weston_view *view, *next;
4534
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004535 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004536
4537 /* Verify we haven't reached the limit of 32 available output IDs */
4538 assert(ffs(~compositor->output_id_pool) > 0);
4539
4540 /* Invert the output id pool and look for the lowest numbered
4541 * switch (the least significant bit). Take that bit's position
4542 * as our ID, and mark it used in the compositor's output_id_pool.
4543 */
4544 output->id = ffs(~compositor->output_id_pool) - 1;
4545 compositor->output_id_pool |= 1u << output->id;
4546
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004547 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03004548 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004549 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004550
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004551 output->global = wl_global_create(compositor->wl_display,
4552 &wl_output_interface, 3,
4553 output, bind_output);
4554
Giulio Camuffob1147152015-05-06 21:41:57 +03004555 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02004556
4557 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4558 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004559}
4560
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004561/** Transform device coordinates into global coordinates
4562 *
4563 * \param device_x[in] X coordinate in device units.
4564 * \param device_y[in] Y coordinate in device units.
4565 * \param x[out] X coordinate in the global space.
4566 * \param y[out] Y coordinate in the global space.
4567 *
4568 * Transforms coordinates from the device coordinate space
4569 * (physical pixel units) to the global coordinate space (logical pixel units).
4570 * This takes into account output transform and scale.
4571 *
4572 * \memberof weston_output
4573 * \internal
4574 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004575WL_EXPORT void
4576weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004577 double device_x, double device_y,
4578 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004579{
Derek Foreman0f679412014-10-02 13:41:17 -05004580 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004581 device_x,
4582 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004583 0.0,
4584 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004585
Derek Foreman67a18b92015-03-24 11:36:14 -05004586 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004587
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004588 *x = p.f[0] / p.f[3];
4589 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004590}
4591
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004592/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004593 *
4594 * \param output The weston_output object that is being removed.
4595 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004596 * The following happens:
4597 *
4598 * - The output assignments of all views in the current scenegraph are
4599 * recomputed.
4600 *
4601 * - Presentation feedback is discarded.
4602 *
4603 * - Compositor is notified that outputs were changed and
4604 * applies the necessary changes to re-layout outputs.
4605 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004606 * - The output is put back in the pending outputs list.
4607 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004608 * - Signal is emitted to notify all users of the weston_output
4609 * object that the output is being destroyed.
4610 *
4611 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004612 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004613 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004614 * - The output's internal ID is released.
4615 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004616 * \memberof weston_output
4617 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004618 */
4619static void
4620weston_compositor_remove_output(struct weston_output *output)
4621{
Pekka Paalanenbccda712017-03-29 16:16:04 +03004622 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004623 struct wl_resource *resource;
4624 struct weston_view *view;
4625
4626 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004627 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004628
Pekka Paalanenbccda712017-03-29 16:16:04 +03004629 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004630 if (view->output_mask & (1u << output->id))
4631 weston_view_assign_output(view);
4632 }
4633
4634 weston_presentation_feedback_discard_list(&output->feedback_list);
4635
Pekka Paalanenbccda712017-03-29 16:16:04 +03004636 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004637
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004638 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004639 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004640 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004641
Pekka Paalanenbccda712017-03-29 16:16:04 +03004642 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004643 wl_signal_emit(&output->destroy_signal, output);
4644
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004645 wl_global_destroy(output->global);
4646 output->global = NULL;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004647 wl_resource_for_each(resource, &output->resource_list) {
4648 wl_resource_set_destructor(resource, NULL);
4649 }
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004650
4651 compositor->output_id_pool &= ~(1u << output->id);
4652 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004653}
4654
4655/** Sets the output scale for a given output.
4656 *
4657 * \param output The weston_output object that the scale is set for.
4658 * \param scale Scale factor for the given output.
4659 *
4660 * It only supports setting scale for an output that
4661 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004662 *
4663 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004664 */
4665WL_EXPORT void
4666weston_output_set_scale(struct weston_output *output,
4667 int32_t scale)
4668{
4669 /* We can only set scale on a disabled output */
4670 assert(!output->enabled);
4671
4672 /* We only want to set scale once */
4673 assert(!output->scale);
4674
4675 output->scale = scale;
4676}
4677
4678/** Sets the output transform for a given output.
4679 *
4680 * \param output The weston_output object that the transform is set for.
4681 * \param transform Transform value for the given output.
4682 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004683 * Refer to wl_output::transform section located at
4684 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
4685 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004686 *
4687 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004688 */
4689WL_EXPORT void
4690weston_output_set_transform(struct weston_output *output,
4691 uint32_t transform)
4692{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00004693 struct weston_pointer_motion_event ev;
4694 struct wl_resource *resource;
4695 struct weston_seat *seat;
4696 pixman_region32_t old_region;
4697 int mid_x, mid_y;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004698
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00004699 if (!output->enabled && output->transform == UINT32_MAX) {
4700 output->transform = transform;
4701 return;
4702 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004703
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00004704 weston_output_transform_scale_init(output, transform, output->scale);
4705
4706 pixman_region32_init(&old_region);
4707 pixman_region32_copy(&old_region, &output->region);
4708
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00004709 weston_output_init_geometry(output, output->x, output->y);
4710
4711 output->dirty = 1;
4712
4713 /* Notify clients of the change for output transform. */
4714 wl_resource_for_each(resource, &output->resource_list) {
4715 wl_output_send_geometry(resource,
4716 output->x,
4717 output->y,
4718 output->mm_width,
4719 output->mm_height,
4720 output->subpixel,
4721 output->make,
4722 output->model,
4723 output->transform);
4724
4725 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
4726 wl_output_send_done(resource);
4727 }
4728
4729 /* we must ensure that pointers are inside output, otherwise they disappear */
4730 mid_x = output->x + output->width / 2;
4731 mid_y = output->y + output->height / 2;
4732
4733 ev.mask = WESTON_POINTER_MOTION_ABS;
4734 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
4735 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
4736
4737 wl_list_for_each(seat, &output->compositor->seat_list, link) {
4738 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4739
4740 if (pointer && pixman_region32_contains_point(&old_region,
4741 wl_fixed_to_int(pointer->x),
4742 wl_fixed_to_int(pointer->y),
4743 NULL))
4744 weston_pointer_move(pointer, &ev);
4745 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004746}
4747
4748/** Initializes a weston_output object with enough data so
4749 ** an output can be configured.
4750 *
4751 * \param output The weston_output object to initialize
4752 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004753 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004754 *
4755 * Sets initial values for fields that are expected to be
4756 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004757 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004758 * The name is used in logs, and can be used by compositors as a configuration
4759 * identifier.
4760 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004761 * \memberof weston_output
4762 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004763 */
4764WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02004765weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004766 struct weston_compositor *compositor,
4767 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004768{
4769 output->compositor = compositor;
4770 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004771 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004772 wl_list_init(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004773 output->enabled = false;
4774
4775 /* Add some (in)sane defaults which can be used
4776 * for checking if an output was properly configured
4777 */
4778 output->mm_width = 0;
4779 output->mm_height = 0;
4780 output->scale = 0;
4781 /* Can't use -1 on uint32_t and 0 is valid enum value */
4782 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004783
4784 pixman_region32_init(&output->previous_damage);
4785 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03004786 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004787}
4788
4789/** Adds weston_output object to pending output list.
4790 *
4791 * \param output The weston_output object to add
4792 * \param compositor The compositor instance.
4793 *
4794 * Also notifies the compositor that an output is pending for
4795 * configuration.
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004796 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03004797 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004798 *
4799 * \memberof weston_output
4800 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004801 */
4802WL_EXPORT void
4803weston_compositor_add_pending_output(struct weston_output *output,
4804 struct weston_compositor *compositor)
4805{
Pekka Paalanene952a012017-03-29 17:14:00 +03004806 assert(output->disable);
4807 assert(output->enable);
4808
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004809 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004810 wl_list_insert(compositor->pending_output_list.prev, &output->link);
4811 wl_signal_emit(&compositor->output_pending_signal, output);
4812}
4813
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004814/** Constructs a weston_output object that can be used by the compositor.
4815 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03004816 * \param output The weston_output object that needs to be enabled. Must not
4817 * be enabled already.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004818 *
4819 * Output coordinates are calculated and each new output is by default
4820 * assigned to the right of previous one.
4821 *
4822 * Sets up the transformation, zoom, and geometry of the output using
4823 * the properties that need to be configured by the compositor.
4824 *
4825 * Establishes a repaint timer for the output with the relevant display
4826 * object's event loop. See output_repaint_timer_handler().
4827 *
4828 * The output is assigned an ID. Weston can support up to 32 distinct
4829 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4830 * is referred to and used to find the first available ID number, and
4831 * then this ID is marked as used in output_id_pool.
4832 *
4833 * The output is also assigned a Wayland global with the wl_output
4834 * external interface.
4835 *
4836 * Backend specific function is called to set up the output output.
4837 *
4838 * Output is added to the compositor's output list
4839 *
4840 * If the backend specific function fails, the weston_output object
4841 * is returned to a state it was before calling this function and
4842 * is added to the compositor's pending_output_list in case it needs
4843 * to be reconfigured or just so it can be destroyed at shutdown.
4844 *
4845 * 0 is returned on success, -1 on failure.
4846 */
4847WL_EXPORT int
4848weston_output_enable(struct weston_output *output)
4849{
Armin Krezović782f5df2016-09-30 14:11:11 +02004850 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004851 struct weston_output *iterator;
4852 int x = 0, y = 0;
4853
Pekka Paalanencc201e42017-03-30 15:11:25 +03004854 if (output->enabled) {
4855 weston_log("Error: attempt to enable an enabled output '%s'\n",
4856 output->name);
4857 return -1;
4858 }
4859
Armin Krezović782f5df2016-09-30 14:11:11 +02004860 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004861 struct weston_output, link);
4862
Armin Krezović782f5df2016-09-30 14:11:11 +02004863 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004864 x = iterator->x + iterator->width;
4865
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004866 /* Make sure the scale is set up */
4867 assert(output->scale);
4868
4869 /* Make sure we have a transform set */
4870 assert(output->transform != UINT32_MAX);
4871
Armin Krezović782f5df2016-09-30 14:11:11 +02004872 output->x = x;
4873 output->y = y;
4874 output->dirty = 1;
4875 output->original_scale = output->scale;
4876
4877 weston_output_transform_scale_init(output, output->transform, output->scale);
4878 weston_output_init_zoom(output);
4879
4880 weston_output_init_geometry(output, x, y);
4881 weston_output_damage(output);
4882
4883 wl_signal_init(&output->frame_signal);
4884 wl_signal_init(&output->destroy_signal);
4885 wl_list_init(&output->animation_list);
4886 wl_list_init(&output->resource_list);
4887 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02004888
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004889 /* Enable the output (set up the crtc or create a
4890 * window representing the output, set up the
4891 * renderer, etc)
4892 */
4893 if (output->enable(output) < 0) {
4894 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004895 return -1;
4896 }
4897
4898 weston_compositor_add_output(output->compositor, output);
4899
4900 return 0;
4901}
4902
4903/** Converts a weston_output object to a pending output state, so it
4904 ** can be configured again or destroyed.
4905 *
4906 * \param output The weston_output object that needs to be disabled.
4907 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004908 * Calls a backend specific function to disable an output, in case
4909 * such function exists.
4910 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004911 * The backend specific disable function may choose to postpone the disabling
4912 * by returning a negative value, in which case this function returns early.
4913 * In that case the backend will guarantee the output will be disabled soon
4914 * by the backend calling this function again. One must not attempt to re-enable
4915 * the output until that happens.
4916 *
4917 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004918 * from weston's output_list (see weston_compositor_remove_output())
4919 * and is returned to a state it was before weston_output_enable()
4920 * was ran (see weston_output_enable_undo()).
4921 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004922 * See weston_output_init() for more information on the
4923 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03004924 *
4925 * If the output has never been enabled yet, this function can still be
4926 * called to ensure that the output is actually turned off rather than left
4927 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004928 */
4929WL_EXPORT void
4930weston_output_disable(struct weston_output *output)
4931{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004932 /* Should we rename this? */
4933 output->destroying = 1;
4934
Pekka Paalanenc65df642017-03-29 15:45:46 +03004935 /* Disable is called unconditionally also for not-enabled outputs,
4936 * because at compositor start-up, if there is an output that is
4937 * already on but the compositor wants to turn it off, we have to
4938 * forward the turn-off to the backend so it knows to do it.
4939 * The backend cannot initially turn off everything, because it
4940 * would cause unnecessary mode-sets for all outputs the compositor
4941 * wants to be on.
4942 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004943 if (output->disable(output) < 0)
4944 return;
4945
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004946 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004947 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004948
4949 output->destroying = 0;
4950}
4951
4952/** Emits a signal to indicate that there are outputs waiting to be configured.
4953 *
4954 * \param compositor The compositor instance
4955 */
4956WL_EXPORT void
4957weston_pending_output_coldplug(struct weston_compositor *compositor)
4958{
4959 struct weston_output *output, *next;
4960
4961 wl_list_for_each_safe(output, next, &compositor->pending_output_list, link)
4962 wl_signal_emit(&compositor->output_pending_signal, output);
4963}
4964
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004965/** Uninitialize an output
4966 *
4967 * Removes the output from the list of enabled outputs if necessary, but
4968 * does not call the backend's output disable function. The output will no
4969 * longer be in the list of pending outputs either.
4970 *
4971 * All fields of weston_output become uninitialized, i.e. should not be used
4972 * anymore. The caller can free the memory after this.
4973 *
4974 * \memberof weston_output
4975 * \internal
4976 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004977WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03004978weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004979{
4980 output->destroying = 1;
4981
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004982 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004983 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004984
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004985 pixman_region32_fini(&output->region);
4986 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004987 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004988 free(output->name);
4989}
4990
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004991static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004992destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004993{
Jonny Lamb74130762013-11-26 18:19:46 +01004994 struct weston_surface *surface =
4995 wl_resource_get_user_data(resource);
4996
Pekka Paalanen4826f872016-04-22 14:14:38 +03004997 if (!surface)
4998 return;
4999
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005000 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02005001 surface->pending.buffer_viewport.buffer.src_width =
5002 wl_fixed_from_int(-1);
5003 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005004 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005005}
5006
5007static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005008viewport_destroy(struct wl_client *client,
5009 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005010{
5011 wl_resource_destroy(resource);
5012}
5013
5014static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005015viewport_set_source(struct wl_client *client,
5016 struct wl_resource *resource,
5017 wl_fixed_t src_x,
5018 wl_fixed_t src_y,
5019 wl_fixed_t src_width,
5020 wl_fixed_t src_height)
5021{
5022 struct weston_surface *surface =
5023 wl_resource_get_user_data(resource);
5024
Pekka Paalanen4826f872016-04-22 14:14:38 +03005025 if (!surface) {
5026 wl_resource_post_error(resource,
5027 WP_VIEWPORT_ERROR_NO_SURFACE,
5028 "wl_surface for this viewport is no longer exists");
5029 return;
5030 }
5031
5032 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03005033 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005034
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005035 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03005036 src_height == wl_fixed_from_int(-1) &&
5037 src_x == wl_fixed_from_int(-1) &&
5038 src_y == wl_fixed_from_int(-1)) {
5039 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005040 surface->pending.buffer_viewport.buffer.src_width =
5041 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005042 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005043 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005044 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005045
Pekka Paalanen201769a2016-04-26 14:42:11 +03005046 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005047 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005048 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03005049 "wl_surface@%d viewport source "
5050 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
5051 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005052 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03005053 wl_fixed_to_double(src_height),
5054 wl_fixed_to_double(src_x),
5055 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005056 return;
5057 }
5058
5059 surface->pending.buffer_viewport.buffer.src_x = src_x;
5060 surface->pending.buffer_viewport.buffer.src_y = src_y;
5061 surface->pending.buffer_viewport.buffer.src_width = src_width;
5062 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005063 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005064}
5065
5066static void
5067viewport_set_destination(struct wl_client *client,
5068 struct wl_resource *resource,
5069 int32_t dst_width,
5070 int32_t dst_height)
5071{
5072 struct weston_surface *surface =
5073 wl_resource_get_user_data(resource);
5074
Pekka Paalanen4826f872016-04-22 14:14:38 +03005075 if (!surface) {
5076 wl_resource_post_error(resource,
5077 WP_VIEWPORT_ERROR_NO_SURFACE,
5078 "wl_surface for this viewport no longer exists");
5079 return;
5080 }
5081
5082 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005083
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005084 if (dst_width == -1 && dst_height == -1) {
5085 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005086 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005087 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005088 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005089 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005090
5091 if (dst_width <= 0 || dst_height <= 0) {
5092 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005093 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005094 "destination size must be positive (%dx%d)",
5095 dst_width, dst_height);
5096 return;
5097 }
5098
5099 surface->pending.buffer_viewport.surface.width = dst_width;
5100 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005101 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005102}
5103
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005104static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005105 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005106 viewport_set_source,
5107 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01005108};
5109
5110static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005111viewporter_destroy(struct wl_client *client,
5112 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005113{
5114 wl_resource_destroy(resource);
5115}
5116
5117static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005118viewporter_get_viewport(struct wl_client *client,
5119 struct wl_resource *viewporter,
5120 uint32_t id,
5121 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005122{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005123 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005124 struct weston_surface *surface =
5125 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005126 struct wl_resource *resource;
5127
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005128 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005129 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005130 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005131 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01005132 return;
5133 }
5134
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005135 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005136 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005137 if (resource == NULL) {
5138 wl_client_post_no_memory(client);
5139 return;
5140 }
5141
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005142 wl_resource_set_implementation(resource, &viewport_interface,
5143 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01005144
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005145 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005146}
5147
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005148static const struct wp_viewporter_interface viewporter_interface = {
5149 viewporter_destroy,
5150 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01005151};
5152
5153static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005154bind_viewporter(struct wl_client *client,
5155 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005156{
5157 struct wl_resource *resource;
5158
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005159 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005160 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005161 if (resource == NULL) {
5162 wl_client_post_no_memory(client);
5163 return;
5164 }
5165
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005166 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01005167 NULL, NULL);
5168}
5169
5170static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04005171destroy_presentation_feedback(struct wl_resource *feedback_resource)
5172{
5173 struct weston_presentation_feedback *feedback;
5174
5175 feedback = wl_resource_get_user_data(feedback_resource);
5176
5177 wl_list_remove(&feedback->link);
5178 free(feedback);
5179}
5180
5181static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005182presentation_destroy(struct wl_client *client, struct wl_resource *resource)
5183{
5184 wl_resource_destroy(resource);
5185}
5186
5187static void
5188presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005189 struct wl_resource *presentation_resource,
5190 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005191 uint32_t callback)
5192{
Pekka Paalanen133e4392014-09-23 22:08:46 -04005193 struct weston_surface *surface;
5194 struct weston_presentation_feedback *feedback;
5195
5196 surface = wl_resource_get_user_data(surface_resource);
5197
Bryce Harringtonde16d892014-11-20 22:21:57 -08005198 feedback = zalloc(sizeof *feedback);
5199 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04005200 goto err_calloc;
5201
5202 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005203 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005204 1, callback);
5205 if (!feedback->resource)
5206 goto err_create;
5207
5208 wl_resource_set_implementation(feedback->resource, NULL, feedback,
5209 destroy_presentation_feedback);
5210
5211 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
5212
5213 return;
5214
5215err_create:
5216 free(feedback);
5217
5218err_calloc:
5219 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005220}
5221
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005222static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005223 presentation_destroy,
5224 presentation_feedback
5225};
5226
5227static void
5228bind_presentation(struct wl_client *client,
5229 void *data, uint32_t version, uint32_t id)
5230{
5231 struct weston_compositor *compositor = data;
5232 struct wl_resource *resource;
5233
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005234 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005235 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005236 if (resource == NULL) {
5237 wl_client_post_no_memory(client);
5238 return;
5239 }
5240
5241 wl_resource_set_implementation(resource, &presentation_implementation,
5242 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005243 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005244}
5245
5246static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05005247compositor_bind(struct wl_client *client,
5248 void *data, uint32_t version, uint32_t id)
5249{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005250 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005251 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05005252
Jason Ekstranda85118c2013-06-27 20:17:02 -05005253 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005254 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005255 if (resource == NULL) {
5256 wl_client_post_no_memory(client);
5257 return;
5258 }
5259
5260 wl_resource_set_implementation(resource, &compositor_interface,
5261 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05005262}
5263
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005264WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005265weston_environment_get_fd(const char *env)
5266{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005267 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005268 int fd, flags;
5269
5270 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005271 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005272 return -1;
5273
5274 flags = fcntl(fd, F_GETFD);
5275 if (flags == -1)
5276 return -1;
5277
5278 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
5279 unsetenv(env);
5280
5281 return fd;
5282}
5283
Pekka Paalanenb5026542014-11-12 15:09:24 +02005284static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005285timeline_key_binding_handler(struct weston_keyboard *keyboard,
5286 const struct timespec *time, uint32_t key,
5287 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02005288{
5289 struct weston_compositor *compositor = data;
5290
5291 if (weston_timeline_enabled_)
5292 weston_timeline_close();
5293 else
5294 weston_timeline_open(compositor);
5295}
5296
Giulio Camuffo459137b2014-10-11 23:56:24 +03005297/** Create the compositor.
5298 *
5299 * This functions creates and initializes a compositor instance.
5300 *
5301 * \param display The Wayland display to be used.
5302 * \param user_data A pointer to an object that can later be retrieved
5303 * using the \ref weston_compositor_get_user_data function.
5304 * \return The compositor instance on success or NULL on failure.
5305 */
5306WL_EXPORT struct weston_compositor *
5307weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005308{
Giulio Camuffo459137b2014-10-11 23:56:24 +03005309 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05005310 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07005311
Giulio Camuffo459137b2014-10-11 23:56:24 +03005312 ec = zalloc(sizeof *ec);
5313 if (!ec)
5314 return NULL;
5315
5316 ec->wl_display = display;
5317 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005318 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07005319 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005320 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005321 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005322 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005323 wl_signal_init(&ec->idle_signal);
5324 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005325 wl_signal_init(&ec->show_input_panel_signal);
5326 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005327 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01005328 wl_signal_init(&ec->seat_created_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005329 wl_signal_init(&ec->output_pending_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01005330 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02005331 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005332 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02005333 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005334 wl_signal_init(&ec->session_signal);
5335 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005336
Casey Dahlin58ba1372012-04-19 22:50:08 -04005337 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03005338 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04005339
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02005340 ec->activate_serial = 1;
5341
Derek Foreman152254b2015-11-26 14:17:48 -06005342 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005343 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005344 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05005345
Giulio Camuffo954f1832014-10-11 18:27:30 +03005346 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005347 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005348 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03005349
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005350 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005351 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005352 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005353
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005354 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005355 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005356 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005357
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005358 if (weston_input_init(ec) != 0)
5359 goto fail;
5360
Jason Ekstranda7af7042013-10-12 22:38:11 -05005361 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005362 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005363 wl_list_init(&ec->layer_list);
5364 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005365 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005366 wl_list_init(&ec->output_list);
5367 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01005368 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005369 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005370 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005371 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005372 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005373
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005374 wl_list_init(&ec->plugin_api_list);
5375
Xiong Zhang97116532013-10-23 13:58:31 +08005376 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005377 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005378
Giulio Camuffo459137b2014-10-11 23:56:24 +03005379 wl_data_device_manager_init(ec->wl_display);
5380
5381 wl_display_init_shm(ec->wl_display);
5382
5383 loop = wl_display_get_event_loop(ec->wl_display);
5384 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00005385 ec->repaint_timer =
5386 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
5387 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005388
Quentin Glidic82681572016-12-17 13:40:51 +01005389 weston_layer_init(&ec->fade_layer, ec);
5390 weston_layer_init(&ec->cursor_layer, ec);
5391
5392 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
5393 weston_layer_set_position(&ec->cursor_layer,
5394 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005395
5396 weston_compositor_add_debug_binding(ec, KEY_T,
5397 timeline_key_binding_handler, ec);
5398
Giulio Camuffo459137b2014-10-11 23:56:24 +03005399 return ec;
5400
5401fail:
5402 free(ec);
5403 return NULL;
5404}
5405
Benjamin Franzkeb8263022011-08-30 11:32:47 +02005406WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005407weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07005408{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005409 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07005410
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005411 wl_event_source_remove(ec->idle_source);
5412
Matt Roper361d2ad2011-08-29 13:52:23 -07005413 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02005414 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07005415 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02005416
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005417 /* Destroy all pending outputs associated with this compositor */
5418 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
5419 output->destroy(output);
5420
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02005421 if (ec->renderer)
5422 ec->renderer->destroy(ec);
5423
Daniel Stone325fc2d2012-05-30 16:31:58 +01005424 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09005425 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005426 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005427 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005428 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005429 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005430
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005431 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07005432}
5433
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005434WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04005435weston_compositor_exit_with_code(struct weston_compositor *compositor,
5436 int exit_code)
5437{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02005438 if (compositor->exit_code == EXIT_SUCCESS)
5439 compositor->exit_code = exit_code;
5440
Giulio Camuffo459137b2014-10-11 23:56:24 +03005441 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04005442}
5443
5444WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005445weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
5446 const struct weston_pointer_grab_interface *interface)
5447{
5448 struct weston_seat *seat;
5449
5450 ec->default_pointer_grab = interface;
5451 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005452 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5453
5454 if (pointer)
5455 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005456 }
5457}
5458
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04005459WL_EXPORT int
5460weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
5461 clockid_t clk_id)
5462{
5463 struct timespec ts;
5464
5465 if (clock_gettime(clk_id, &ts) < 0)
5466 return -1;
5467
5468 compositor->presentation_clock = clk_id;
5469
5470 return 0;
5471}
5472
5473/*
5474 * For choosing the software clock, when the display hardware or API
5475 * does not expose a compatible presentation timestamp.
5476 */
5477WL_EXPORT int
5478weston_compositor_set_presentation_clock_software(
5479 struct weston_compositor *compositor)
5480{
5481 /* In order of preference */
5482 static const clockid_t clocks[] = {
5483 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
5484 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
5485 CLOCK_MONOTONIC, /* no jumps, may crawl */
5486 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
5487 CLOCK_REALTIME /* may jump and crawl */
5488 };
5489 unsigned i;
5490
5491 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
5492 if (weston_compositor_set_presentation_clock(compositor,
5493 clocks[i]) == 0)
5494 return 0;
5495
5496 weston_log("Error: no suitable presentation clock available.\n");
5497
5498 return -1;
5499}
5500
Pekka Paalanen662f3842015-03-18 12:17:26 +02005501/** Read the current time from the Presentation clock
5502 *
5503 * \param compositor
5504 * \param ts[out] The current time.
5505 *
5506 * \note Reading the current time in user space is always imprecise to some
5507 * degree.
5508 *
5509 * This function is never meant to fail. If reading the clock does fail,
5510 * an error message is logged and a zero time is returned. Callers are not
5511 * supposed to detect or react to failures.
5512 */
5513WL_EXPORT void
5514weston_compositor_read_presentation_clock(
5515 const struct weston_compositor *compositor,
5516 struct timespec *ts)
5517{
5518 static bool warned;
5519 int ret;
5520
5521 ret = clock_gettime(compositor->presentation_clock, ts);
5522 if (ret < 0) {
5523 ts->tv_sec = 0;
5524 ts->tv_nsec = 0;
5525
5526 if (!warned)
5527 weston_log("Error: failure to read "
5528 "the presentation clock %#x: '%m' (%d)\n",
5529 compositor->presentation_clock, errno);
5530 warned = true;
5531 }
5532}
5533
Pekka Paalanen230f3b12014-09-29 14:18:40 -04005534/** Import dmabuf buffer into current renderer
5535 *
5536 * \param compositor
5537 * \param buffer the dmabuf buffer to import
5538 * \return true on usable buffers, false otherwise
5539 *
5540 * This function tests that the linux_dmabuf_buffer is usable
5541 * for the current renderer. Returns false on unusable buffers. Usually
5542 * usability is tested by importing the dmabufs for composition.
5543 *
5544 * This hook is also used for detecting if the renderer supports
5545 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
5546 * supported.
5547 * */
5548WL_EXPORT bool
5549weston_compositor_import_dmabuf(struct weston_compositor *compositor,
5550 struct linux_dmabuf_buffer *buffer)
5551{
5552 struct weston_renderer *renderer;
5553
5554 renderer = compositor->renderer;
5555
5556 if (renderer->import_dmabuf == NULL)
5557 return false;
5558
5559 return renderer->import_dmabuf(compositor, buffer);
5560}
5561
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005562WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005563weston_version(int *major, int *minor, int *micro)
5564{
5565 *major = WESTON_VERSION_MAJOR;
5566 *minor = WESTON_VERSION_MINOR;
5567 *micro = WESTON_VERSION_MICRO;
5568}
5569
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005570WL_EXPORT void *
5571weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005572{
Derek Foreman3f86e502015-06-08 11:46:54 -05005573 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005574 char path[PATH_MAX];
5575 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00005576 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005577
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005578 if (name == NULL)
5579 return NULL;
5580
Derek Foreman3f86e502015-06-08 11:46:54 -05005581 if (name[0] != '/') {
5582 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00005583 len = snprintf(path, sizeof path, "%s/.libs/%s",
5584 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005585 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00005586 len = snprintf(path, sizeof path, "%s/%s",
5587 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005588 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00005589 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005590 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005591
Daniel Stonebeb97e52016-11-28 12:13:54 +00005592 /* snprintf returns the length of the string it would've written,
5593 * _excluding_ the NUL byte. So even being equal to the size of
5594 * our buffer is an error here. */
5595 if (len >= sizeof path)
5596 return NULL;
5597
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005598 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
5599 if (module) {
5600 weston_log("Module '%s' already loaded\n", path);
5601 dlclose(module);
5602 return NULL;
5603 }
5604
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005605 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04005606 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005607 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005608 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005609 return NULL;
5610 }
5611
5612 init = dlsym(module, entrypoint);
5613 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005614 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00005615 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005616 return NULL;
5617 }
5618
5619 return init;
5620}
5621
Giulio Camuffo459137b2014-10-11 23:56:24 +03005622
5623/** Destroys the compositor.
5624 *
5625 * This function cleans up the compositor state and destroys it.
5626 *
5627 * \param compositor The compositor to be destroyed.
5628 */
5629WL_EXPORT void
5630weston_compositor_destroy(struct weston_compositor *compositor)
5631{
5632 /* prevent further rendering while shutting down */
5633 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5634
5635 wl_signal_emit(&compositor->destroy_signal, compositor);
5636
5637 weston_compositor_xkb_destroy(compositor);
5638
Giulio Camuffo2d24e642015-10-03 16:25:15 +03005639 if (compositor->backend)
5640 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005641
5642 weston_plugin_api_destroy_list(compositor);
5643
Giulio Camuffo459137b2014-10-11 23:56:24 +03005644 free(compositor);
5645}
5646
5647/** Instruct the compositor to exit.
5648 *
5649 * This functions does not directly destroy the compositor object, it merely
5650 * command it to start the tear down process. It is not guaranteed that the
5651 * tear down will happen immediately.
5652 *
5653 * \param compositor The compositor to tear down.
5654 */
5655WL_EXPORT void
5656weston_compositor_exit(struct weston_compositor *compositor)
5657{
5658 compositor->exit(compositor);
5659}
5660
5661/** Return the user data stored in the compositor.
5662 *
5663 * This function returns the user data pointer set with user_data parameter
5664 * to the \ref weston_compositor_create function.
5665 */
5666WL_EXPORT void *
5667weston_compositor_get_user_data(struct weston_compositor *compositor)
5668{
5669 return compositor->user_data;
5670}
Pekka Paalanendd186732016-06-03 14:49:54 +03005671
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005672static const char * const backend_map[] = {
5673 [WESTON_BACKEND_DRM] = "drm-backend.so",
5674 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5675 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5676 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5677 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5678 [WESTON_BACKEND_X11] = "x11-backend.so",
5679};
5680
Pekka Paalanendd186732016-06-03 14:49:54 +03005681/** Load a backend into a weston_compositor
5682 *
5683 * A backend must be loaded to make a weston_compositor work. A backend
5684 * provides input and output capabilities, and determines the renderer to use.
5685 *
5686 * \param compositor A compositor that has not had a backend loaded yet.
5687 * \param backend Name of the backend file.
5688 * \param config_base A pointer to a backend-specific configuration
5689 * structure's 'base' member.
5690 *
5691 * \return 0 on success, or -1 on error.
5692 */
5693WL_EXPORT int
5694weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005695 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005696 struct weston_backend_config *config_base)
5697{
5698 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005699 struct weston_backend_config *config_base);
5700
Pekka Paalanend7e35112017-08-29 17:04:12 +03005701 if (compositor->backend) {
5702 weston_log("Error: attempt to load a backend when one is already loaded\n");
5703 return -1;
5704 }
5705
Quentin Glidic887c0182016-07-10 11:00:53 +02005706 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005707 return -1;
5708
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01005709 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005710 if (!backend_init)
5711 return -1;
5712
Pekka Paalanend7e35112017-08-29 17:04:12 +03005713 if (backend_init(compositor, config_base) < 0) {
5714 compositor->backend = NULL;
5715 return -1;
5716 }
5717
5718 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03005719}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005720
5721WL_EXPORT int
5722weston_compositor_load_xwayland(struct weston_compositor *compositor)
5723{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005724 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005725
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005726 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005727 if (!module_init)
5728 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005729 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005730 return -1;
5731 return 0;
5732}