blob: aec937bb89a5809fb3027d5c8a9171a0ff22f390 [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;
2460 int ret;
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 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2499 TLP_VBLANK(stamp), TLP_END);
2500
Daniel Stone05df8c12017-03-03 16:59:42 +00002501 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002502 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2503
Daniel Stone6847b852017-03-01 11:34:08 +00002504 weston_compositor_read_presentation_clock(compositor, &now);
2505
Daniel Stone3615ce12017-03-01 11:34:05 +00002506 /* If we haven't been supplied any timestamp at all, we don't have a
2507 * timebase to work against, so any delay just wastes time. Push a
2508 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002509 if (!stamp) {
2510 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002511 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002512 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002513
Pekka Paalanend7894d02015-07-03 15:08:53 +03002514 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002515 weston_presentation_feedback_present_list(&output->feedback_list,
2516 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002517 output->msc,
2518 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002519
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002520 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002521
Daniel Stone6847b852017-03-01 11:34:08 +00002522 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2523 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2524 -compositor->repaint_msec);
2525 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002526
2527 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002528 static bool warned;
2529
2530 if (!warned)
2531 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002532 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002533 warned = true;
2534
Daniel Stone6847b852017-03-01 11:34:08 +00002535 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002536 }
2537
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002538 /* Called from restart_repaint_loop and restart happens already after
2539 * the deadline given by repaint_msec? In that case we delay until
2540 * the deadline of the next frame, to give clients a more predictable
2541 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002542 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2543 msec_rel < 0) {
2544 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2545 timespec_add_nsec(&output->next_repaint,
2546 &output->next_repaint,
2547 refresh_nsec);
2548 }
2549 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002550
Daniel Stone3615ce12017-03-01 11:34:05 +00002551out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002552 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002553 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002554}
2555
2556static void
2557idle_repaint(void *data)
2558{
2559 struct weston_output *output = data;
2560
Daniel Stone05df8c12017-03-03 16:59:42 +00002561 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2562 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002563 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002564}
2565
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002566WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002567weston_layer_entry_insert(struct weston_layer_entry *list,
2568 struct weston_layer_entry *entry)
2569{
2570 wl_list_insert(&list->link, &entry->link);
2571 entry->layer = list->layer;
2572}
2573
2574WL_EXPORT void
2575weston_layer_entry_remove(struct weston_layer_entry *entry)
2576{
2577 wl_list_remove(&entry->link);
2578 wl_list_init(&entry->link);
2579 entry->layer = NULL;
2580}
2581
Quentin Glidic82681572016-12-17 13:40:51 +01002582
2583/** Initialize the weston_layer struct.
2584 *
2585 * \param compositor The compositor instance
2586 * \param layer The layer to initialize
2587 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002588WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002589weston_layer_init(struct weston_layer *layer,
2590 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002591{
Quentin Glidic82681572016-12-17 13:40:51 +01002592 layer->compositor = compositor;
2593 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002594 wl_list_init(&layer->view_list.link);
2595 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002596 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002597}
2598
2599/** Sets the position of the layer in the layer list. The layer will be placed
2600 * below any layer with the same position value, if any.
2601 * This function is safe to call if the layer is already on the list, but the
2602 * layer may be moved below other layers at the same position, if any.
2603 *
2604 * \param layer The layer to modify
2605 * \param position The position the layer will be placed at
2606 */
2607WL_EXPORT void
2608weston_layer_set_position(struct weston_layer *layer,
2609 enum weston_layer_position position)
2610{
2611 struct weston_layer *below;
2612
2613 wl_list_remove(&layer->link);
2614
2615 /* layer_list is ordered from top to bottom, the last layer being the
2616 * background with the smallest position value */
2617
2618 layer->position = position;
2619 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2620 if (below->position >= layer->position) {
2621 wl_list_insert(&below->link, &layer->link);
2622 return;
2623 }
2624 }
2625 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2626}
2627
2628/** Hide a layer by taking it off the layer list.
2629 * This function is safe to call if the layer is not on the list.
2630 *
2631 * \param layer The layer to hide
2632 */
2633WL_EXPORT void
2634weston_layer_unset_position(struct weston_layer *layer)
2635{
2636 wl_list_remove(&layer->link);
2637 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002638}
2639
2640WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002641weston_layer_set_mask(struct weston_layer *layer,
2642 int x, int y, int width, int height)
2643{
2644 struct weston_view *view;
2645
2646 layer->mask.x1 = x;
2647 layer->mask.x2 = x + width;
2648 layer->mask.y1 = y;
2649 layer->mask.y2 = y + height;
2650
2651 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2652 weston_view_geometry_dirty(view);
2653 }
2654}
2655
2656WL_EXPORT void
2657weston_layer_set_mask_infinite(struct weston_layer *layer)
2658{
2659 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2660 UINT32_MAX, UINT32_MAX);
2661}
2662
2663WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002664weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002665{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002666 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002667 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002668
Bryce Harrington08976ac2016-08-30 12:05:16 -07002669 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2670 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002671 return;
2672
Pekka Paalanenb5026542014-11-12 15:09:24 +02002673 if (!output->repaint_needed)
2674 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2675
Kristian Høgsbergef044142011-06-21 15:02:12 -04002676 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002677 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002678
2679 /* If we already have a repaint scheduled for our idle handler,
2680 * no need to set it again. If the repaint has been called but
2681 * not finished, then weston_output_finish_frame() will notice
2682 * that a repaint is needed and schedule one. */
2683 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002684 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002685
Daniel Stone05df8c12017-03-03 16:59:42 +00002686 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002687 wl_event_loop_add_idle(loop, idle_repaint, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002688 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002689}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002690
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002691WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002692weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2693{
2694 struct weston_output *output;
2695
2696 wl_list_for_each(output, &compositor->output_list, link)
2697 weston_output_schedule_repaint(output);
2698}
2699
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002700static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002701surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002702{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002703 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002704}
2705
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002706static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002707surface_attach(struct wl_client *client,
2708 struct wl_resource *resource,
2709 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2710{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002711 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002712 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002713
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002714 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002715 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002716 if (buffer == NULL) {
2717 wl_client_post_no_memory(client);
2718 return;
2719 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002720 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002721
Pekka Paalanende685b82012-12-04 15:58:12 +02002722 /* Attach, attach, without commit in between does not send
2723 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002724 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002725
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002726 surface->pending.sx = sx;
2727 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002728 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002729}
2730
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002731static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002732surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002733 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002734 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002735{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002736 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002737
Derek Foreman57e92ed2015-11-17 14:11:35 -06002738 if (width <= 0 || height <= 0)
2739 return;
2740
Derek Foreman152254b2015-11-26 14:17:48 -06002741 pixman_region32_union_rect(&surface->pending.damage_surface,
2742 &surface->pending.damage_surface,
2743 x, y, width, height);
2744}
2745
2746static void
2747surface_damage_buffer(struct wl_client *client,
2748 struct wl_resource *resource,
2749 int32_t x, int32_t y, int32_t width, int32_t height)
2750{
2751 struct weston_surface *surface = wl_resource_get_user_data(resource);
2752
2753 if (width <= 0 || height <= 0)
2754 return;
2755
2756 pixman_region32_union_rect(&surface->pending.damage_buffer,
2757 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002758 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002759}
2760
Kristian Høgsberg33418202011-08-16 23:01:28 -04002761static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002762destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002763{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002764 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002765
2766 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002767 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002768}
2769
2770static void
2771surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002772 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002773{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002774 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002775 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002776
2777 cb = malloc(sizeof *cb);
2778 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002779 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002780 return;
2781 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002782
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002783 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2784 callback);
2785 if (cb->resource == NULL) {
2786 free(cb);
2787 wl_resource_post_no_memory(resource);
2788 return;
2789 }
2790
Jason Ekstranda85118c2013-06-27 20:17:02 -05002791 wl_resource_set_implementation(cb->resource, NULL, cb,
2792 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002793
Pekka Paalanenbc106382012-10-10 12:49:31 +03002794 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002795}
2796
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002797static void
2798surface_set_opaque_region(struct wl_client *client,
2799 struct wl_resource *resource,
2800 struct wl_resource *region_resource)
2801{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002802 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002803 struct weston_region *region;
2804
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002805 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002806 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002807 pixman_region32_copy(&surface->pending.opaque,
2808 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002809 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002810 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002811 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002812}
2813
2814static void
2815surface_set_input_region(struct wl_client *client,
2816 struct wl_resource *resource,
2817 struct wl_resource *region_resource)
2818{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002819 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002820 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002821
2822 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002823 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002824 pixman_region32_copy(&surface->pending.input,
2825 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002826 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002827 pixman_region32_fini(&surface->pending.input);
2828 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002829 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002830}
2831
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002832/* Cause damage to this sub-surface and all its children.
2833 *
2834 * This is useful when there are state changes that need an implicit
2835 * damage, e.g. a z-order change.
2836 */
2837static void
2838weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2839{
2840 struct weston_subsurface *child;
2841
2842 weston_surface_damage(sub->surface);
2843 sub->reordered = false;
2844
2845 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2846 if (child != sub)
2847 weston_surface_damage_subsurfaces(child);
2848}
2849
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002850static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002851weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002852{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002853 struct weston_subsurface *sub;
2854
2855 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2856 parent_link_pending) {
2857 wl_list_remove(&sub->parent_link);
2858 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002859
2860 if (sub->reordered)
2861 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002862 }
2863}
2864
2865static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002866weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002867 struct weston_matrix *matrix)
2868{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002869 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002870 double src_width, src_height, dest_width, dest_height;
2871
2872 weston_matrix_init(matrix);
2873
2874 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2875 src_width = surface->width_from_buffer;
2876 src_height = surface->height_from_buffer;
2877 } else {
2878 src_width = wl_fixed_to_double(vp->buffer.src_width);
2879 src_height = wl_fixed_to_double(vp->buffer.src_height);
2880 }
2881
2882 if (vp->surface.width == -1) {
2883 dest_width = src_width;
2884 dest_height = src_height;
2885 } else {
2886 dest_width = vp->surface.width;
2887 dest_height = vp->surface.height;
2888 }
2889
2890 if (src_width != dest_width || src_height != dest_height)
2891 weston_matrix_scale(matrix,
2892 src_width / dest_width,
2893 src_height / dest_height, 1);
2894
2895 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2896 weston_matrix_translate(matrix,
2897 wl_fixed_to_double(vp->buffer.src_x),
2898 wl_fixed_to_double(vp->buffer.src_y),
2899 0);
2900
2901 switch (vp->buffer.transform) {
2902 case WL_OUTPUT_TRANSFORM_FLIPPED:
2903 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2904 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2905 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2906 weston_matrix_scale(matrix, -1, 1, 1);
2907 weston_matrix_translate(matrix,
2908 surface->width_from_buffer, 0, 0);
2909 break;
2910 }
2911
2912 switch (vp->buffer.transform) {
2913 default:
2914 case WL_OUTPUT_TRANSFORM_NORMAL:
2915 case WL_OUTPUT_TRANSFORM_FLIPPED:
2916 break;
2917 case WL_OUTPUT_TRANSFORM_90:
2918 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2919 weston_matrix_rotate_xy(matrix, 0, 1);
2920 weston_matrix_translate(matrix,
2921 surface->height_from_buffer, 0, 0);
2922 break;
2923 case WL_OUTPUT_TRANSFORM_180:
2924 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2925 weston_matrix_rotate_xy(matrix, -1, 0);
2926 weston_matrix_translate(matrix,
2927 surface->width_from_buffer,
2928 surface->height_from_buffer, 0);
2929 break;
2930 case WL_OUTPUT_TRANSFORM_270:
2931 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2932 weston_matrix_rotate_xy(matrix, 0, -1);
2933 weston_matrix_translate(matrix,
2934 0, surface->width_from_buffer, 0);
2935 break;
2936 }
2937
2938 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2939}
2940
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002941/**
2942 * Compute a + b > c while being safe to overflows.
2943 */
2944static bool
2945fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2946{
2947 return (int64_t)a + (int64_t)b > (int64_t)c;
2948}
2949
2950static bool
2951weston_surface_is_pending_viewport_source_valid(
2952 const struct weston_surface *surface)
2953{
2954 const struct weston_surface_state *pend = &surface->pending;
2955 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2956 int width_from_buffer = 0;
2957 int height_from_buffer = 0;
2958 wl_fixed_t w;
2959 wl_fixed_t h;
2960
2961 /* If viewport source rect is not set, it is always ok. */
2962 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2963 return true;
2964
2965 if (pend->newly_attached) {
2966 if (pend->buffer) {
2967 convert_size_by_transform_scale(&width_from_buffer,
2968 &height_from_buffer,
2969 pend->buffer->width,
2970 pend->buffer->height,
2971 vp->buffer.transform,
2972 vp->buffer.scale);
2973 } else {
2974 /* No buffer: viewport is irrelevant. */
2975 return true;
2976 }
2977 } else {
2978 width_from_buffer = surface->width_from_buffer;
2979 height_from_buffer = surface->height_from_buffer;
2980 }
2981
2982 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2983 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2984
2985 /* No buffer: viewport is irrelevant. */
2986 if (width_from_buffer == 0 || height_from_buffer == 0)
2987 return true;
2988
2989 /* overflow checks for wl_fixed_from_int() */
2990 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2991 return false;
2992 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2993 return false;
2994
2995 w = wl_fixed_from_int(width_from_buffer);
2996 h = wl_fixed_from_int(height_from_buffer);
2997
2998 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2999 return false;
3000 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3001 return false;
3002
3003 return true;
3004}
3005
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003006static bool
3007fixed_is_integer(wl_fixed_t v)
3008{
3009 return (v & 0xff) == 0;
3010}
3011
3012static bool
3013weston_surface_is_pending_viewport_dst_size_int(
3014 const struct weston_surface *surface)
3015{
3016 const struct weston_buffer_viewport *vp =
3017 &surface->pending.buffer_viewport;
3018
3019 if (vp->surface.width != -1) {
3020 assert(vp->surface.width > 0 && vp->surface.height > 0);
3021 return true;
3022 }
3023
3024 return fixed_is_integer(vp->buffer.src_width) &&
3025 fixed_is_integer(vp->buffer.src_height);
3026}
3027
Derek Foreman152254b2015-11-26 14:17:48 -06003028/* Translate pending damage in buffer co-ordinates to surface
3029 * co-ordinates and union it with a pixman_region32_t.
3030 * This should only be called after the buffer is attached.
3031 */
3032static void
3033apply_damage_buffer(pixman_region32_t *dest,
3034 struct weston_surface *surface,
3035 struct weston_surface_state *state)
3036{
3037 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3038
3039 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3040 * translated into surface co-ordinates and unioned with
3041 * any other surface damage.
3042 * None of this makes sense if there is no buffer though.
3043 */
3044 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3045 pixman_region32_t buffer_damage;
3046
3047 pixman_region32_intersect_rect(&state->damage_buffer,
3048 &state->damage_buffer,
3049 0, 0, buffer->width,
3050 buffer->height);
3051 pixman_region32_init(&buffer_damage);
3052 weston_matrix_transform_region(&buffer_damage,
3053 &surface->buffer_to_surface_matrix,
3054 &state->damage_buffer);
3055 pixman_region32_union(dest, dest, &buffer_damage);
3056 pixman_region32_fini(&buffer_damage);
3057 }
3058 /* We should clear this on commit even if there was no buffer */
3059 pixman_region32_clear(&state->damage_buffer);
3060}
3061
Jason Ekstrand1e059042014-10-16 10:55:19 -05003062static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003063weston_surface_commit_state(struct weston_surface *surface,
3064 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003065{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003066 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003067 pixman_region32_t opaque;
3068
Alexander Larsson4ea95522013-05-22 14:41:37 +02003069 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003070 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003071 /* wp_viewport.set_source */
3072 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003073 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003074
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003075 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003076 if (state->newly_attached)
3077 weston_surface_attach(surface, state->buffer);
3078 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003079
Jason Ekstrand1e059042014-10-16 10:55:19 -05003080 weston_surface_build_buffer_matrix(surface,
3081 &surface->surface_to_buffer_matrix);
3082 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3083 &surface->surface_to_buffer_matrix);
3084
Jason Ekstrand7b982072014-05-20 14:33:03 -05003085 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003086 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003087 if (surface->committed)
3088 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003089 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003090
Jason Ekstrand7b982072014-05-20 14:33:03 -05003091 state->sx = 0;
3092 state->sy = 0;
3093 state->newly_attached = 0;
3094 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003095
Derek Foreman152254b2015-11-26 14:17:48 -06003096 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003097 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003098 (pixman_region32_not_empty(&state->damage_surface) ||
3099 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003100 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003101
Pekka Paalanen8e159182012-10-10 12:49:25 +03003102 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003103 &state->damage_surface);
3104
3105 apply_damage_buffer(&surface->damage, surface, state);
3106
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003107 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003108 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003109 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003110
3111 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003112 pixman_region32_init(&opaque);
3113 pixman_region32_intersect_rect(&opaque, &state->opaque,
3114 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003115
3116 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3117 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003118 wl_list_for_each(view, &surface->views, surface_link)
3119 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003120 }
3121
3122 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003123
3124 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003125 pixman_region32_intersect_rect(&surface->input, &state->input,
3126 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003127
Pekka Paalanenbc106382012-10-10 12:49:31 +03003128 /* wl_surface.frame */
3129 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003130 &state->frame_callback_list);
3131 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003132
3133 /* XXX:
3134 * What should happen with a feedback request, if there
3135 * is no wl_buffer attached for this commit?
3136 */
3137
3138 /* presentation.feedback */
3139 wl_list_insert_list(&surface->feedback_list,
3140 &state->feedback_list);
3141 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003142
3143 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003144}
3145
3146static void
3147weston_surface_commit(struct weston_surface *surface)
3148{
3149 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003150
Pekka Paalanene67858b2013-04-25 13:57:42 +03003151 weston_surface_commit_subsurface_order(surface);
3152
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003153 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003154}
3155
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003156static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003157weston_subsurface_commit(struct weston_subsurface *sub);
3158
3159static void
3160weston_subsurface_parent_commit(struct weston_subsurface *sub,
3161 int parent_is_synchronized);
3162
3163static void
3164surface_commit(struct wl_client *client, struct wl_resource *resource)
3165{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003166 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003167 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3168
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003169 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3170 assert(surface->viewport_resource);
3171
3172 wl_resource_post_error(surface->viewport_resource,
3173 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3174 "wl_surface@%d has viewport source outside buffer",
3175 wl_resource_get_id(resource));
3176 return;
3177 }
3178
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003179 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3180 assert(surface->viewport_resource);
3181
3182 wl_resource_post_error(surface->viewport_resource,
3183 WP_VIEWPORT_ERROR_BAD_SIZE,
3184 "wl_surface@%d viewport dst size not integer",
3185 wl_resource_get_id(resource));
3186 return;
3187 }
3188
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189 if (sub) {
3190 weston_subsurface_commit(sub);
3191 return;
3192 }
3193
3194 weston_surface_commit(surface);
3195
3196 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3197 if (sub->surface != surface)
3198 weston_subsurface_parent_commit(sub, 0);
3199 }
3200}
3201
3202static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003203surface_set_buffer_transform(struct wl_client *client,
3204 struct wl_resource *resource, int transform)
3205{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003206 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003207
Jonny Lamba55f1392014-05-30 12:07:15 +02003208 /* if wl_output.transform grows more members this will need to be updated. */
3209 if (transform < 0 ||
3210 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3211 wl_resource_post_error(resource,
3212 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3213 "buffer transform must be a valid transform "
3214 "('%d' specified)", transform);
3215 return;
3216 }
3217
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003218 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003219 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003220}
3221
Alexander Larsson4ea95522013-05-22 14:41:37 +02003222static void
3223surface_set_buffer_scale(struct wl_client *client,
3224 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003225 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003226{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003227 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003228
Jonny Lamba55f1392014-05-30 12:07:15 +02003229 if (scale < 1) {
3230 wl_resource_post_error(resource,
3231 WL_SURFACE_ERROR_INVALID_SCALE,
3232 "buffer scale must be at least one "
3233 "('%d' specified)", scale);
3234 return;
3235 }
3236
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003237 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003238 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003239}
3240
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003241static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003242 surface_destroy,
3243 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003244 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003245 surface_frame,
3246 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003247 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003248 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003249 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003250 surface_set_buffer_scale,
3251 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003252};
3253
3254static void
3255compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003256 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003257{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003258 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003259 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003260
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003261 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003262 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003263 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003264 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003265 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003266
Jason Ekstranda85118c2013-06-27 20:17:02 -05003267 surface->resource =
3268 wl_resource_create(client, &wl_surface_interface,
3269 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003270 if (surface->resource == NULL) {
3271 weston_surface_destroy(surface);
3272 wl_resource_post_no_memory(resource);
3273 return;
3274 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003275 wl_resource_set_implementation(surface->resource, &surface_interface,
3276 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003277
3278 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003279}
3280
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003281static void
3282destroy_region(struct wl_resource *resource)
3283{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003284 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003285
3286 pixman_region32_fini(&region->region);
3287 free(region);
3288}
3289
3290static void
3291region_destroy(struct wl_client *client, struct wl_resource *resource)
3292{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003293 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003294}
3295
3296static void
3297region_add(struct wl_client *client, struct wl_resource *resource,
3298 int32_t x, int32_t y, int32_t width, int32_t height)
3299{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003300 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003301
3302 pixman_region32_union_rect(&region->region, &region->region,
3303 x, y, width, height);
3304}
3305
3306static void
3307region_subtract(struct wl_client *client, struct wl_resource *resource,
3308 int32_t x, int32_t y, int32_t width, int32_t height)
3309{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003310 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003311 pixman_region32_t rect;
3312
3313 pixman_region32_init_rect(&rect, x, y, width, height);
3314 pixman_region32_subtract(&region->region, &region->region, &rect);
3315 pixman_region32_fini(&rect);
3316}
3317
3318static const struct wl_region_interface region_interface = {
3319 region_destroy,
3320 region_add,
3321 region_subtract
3322};
3323
3324static void
3325compositor_create_region(struct wl_client *client,
3326 struct wl_resource *resource, uint32_t id)
3327{
3328 struct weston_region *region;
3329
3330 region = malloc(sizeof *region);
3331 if (region == NULL) {
3332 wl_resource_post_no_memory(resource);
3333 return;
3334 }
3335
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003336 pixman_region32_init(&region->region);
3337
Jason Ekstranda85118c2013-06-27 20:17:02 -05003338 region->resource =
3339 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003340 if (region->resource == NULL) {
3341 free(region);
3342 wl_resource_post_no_memory(resource);
3343 return;
3344 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003345 wl_resource_set_implementation(region->resource, &region_interface,
3346 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003347}
3348
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003349static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003350 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003351 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003352};
3353
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003354static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003355weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3356{
3357 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003358
Jason Ekstrand7b982072014-05-20 14:33:03 -05003359 weston_surface_commit_state(surface, &sub->cached);
3360 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003361
3362 weston_surface_commit_subsurface_order(surface);
3363
3364 weston_surface_schedule_repaint(surface);
3365
Jason Ekstrand7b982072014-05-20 14:33:03 -05003366 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003367}
3368
3369static void
3370weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3371{
3372 struct weston_surface *surface = sub->surface;
3373
3374 /*
3375 * If this commit would cause the surface to move by the
3376 * attach(dx, dy) parameters, the old damage region must be
3377 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003378 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003379 */
Derek Foreman152254b2015-11-26 14:17:48 -06003380 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003381 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003382 pixman_region32_union(&sub->cached.damage_surface,
3383 &sub->cached.damage_surface,
3384 &surface->pending.damage_surface);
3385 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003386
3387 if (surface->pending.newly_attached) {
3388 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003389 weston_surface_state_set_buffer(&sub->cached,
3390 surface->pending.buffer);
3391 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003392 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003393 weston_presentation_feedback_discard_list(
3394 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003395 }
3396 sub->cached.sx += surface->pending.sx;
3397 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003398
Derek Foreman152254b2015-11-26 14:17:48 -06003399 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3400
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003401 sub->cached.buffer_viewport.changed |=
3402 surface->pending.buffer_viewport.changed;
3403 sub->cached.buffer_viewport.buffer =
3404 surface->pending.buffer_viewport.buffer;
3405 sub->cached.buffer_viewport.surface =
3406 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003407
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003408 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003409
3410 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3411
3412 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3413
3414 wl_list_insert_list(&sub->cached.frame_callback_list,
3415 &surface->pending.frame_callback_list);
3416 wl_list_init(&surface->pending.frame_callback_list);
3417
Pekka Paalanen133e4392014-09-23 22:08:46 -04003418 wl_list_insert_list(&sub->cached.feedback_list,
3419 &surface->pending.feedback_list);
3420 wl_list_init(&surface->pending.feedback_list);
3421
Jason Ekstrand7b982072014-05-20 14:33:03 -05003422 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003423}
3424
Derek Foreman280e7dd2014-10-03 13:13:42 -05003425static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003426weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3427{
3428 while (sub) {
3429 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003430 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003431
3432 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003433 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003434
3435 sub = weston_surface_to_subsurface(sub->parent);
3436 }
3437
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003438 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003439}
3440
3441static void
3442weston_subsurface_commit(struct weston_subsurface *sub)
3443{
3444 struct weston_surface *surface = sub->surface;
3445 struct weston_subsurface *tmp;
3446
3447 /* Recursive check for effectively synchronized. */
3448 if (weston_subsurface_is_synchronized(sub)) {
3449 weston_subsurface_commit_to_cache(sub);
3450 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003451 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003452 /* flush accumulated state from cache */
3453 weston_subsurface_commit_to_cache(sub);
3454 weston_subsurface_commit_from_cache(sub);
3455 } else {
3456 weston_surface_commit(surface);
3457 }
3458
3459 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3460 if (tmp->surface != surface)
3461 weston_subsurface_parent_commit(tmp, 0);
3462 }
3463 }
3464}
3465
3466static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003467weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003468{
3469 struct weston_surface *surface = sub->surface;
3470 struct weston_subsurface *tmp;
3471
Pekka Paalanene67858b2013-04-25 13:57:42 +03003472 /* From now on, commit_from_cache the whole sub-tree, regardless of
3473 * the synchronized mode of each child. This sub-surface or some
3474 * of its ancestors were synchronized, so we are synchronized
3475 * all the way down.
3476 */
3477
Jason Ekstrand7b982072014-05-20 14:33:03 -05003478 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003479 weston_subsurface_commit_from_cache(sub);
3480
3481 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3482 if (tmp->surface != surface)
3483 weston_subsurface_parent_commit(tmp, 1);
3484 }
3485}
3486
3487static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003488weston_subsurface_parent_commit(struct weston_subsurface *sub,
3489 int parent_is_synchronized)
3490{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003491 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003492 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003493 wl_list_for_each(view, &sub->surface->views, surface_link)
3494 weston_view_set_position(view,
3495 sub->position.x,
3496 sub->position.y);
3497
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003498 sub->position.set = 0;
3499 }
3500
3501 if (parent_is_synchronized || sub->synchronized)
3502 weston_subsurface_synchronized_commit(sub);
3503}
3504
Pekka Paalanen8274d902014-08-06 19:36:51 +03003505static int
3506subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3507{
3508 return snprintf(buf, len, "sub-surface");
3509}
3510
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003511static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003512subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003513{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003514 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003515
Jason Ekstranda7af7042013-10-12 22:38:11 -05003516 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003517 weston_view_set_position(view,
3518 view->geometry.x + dx,
3519 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003520
3521 /* No need to check parent mappedness, because if parent is not
3522 * mapped, parent is not in a visible layer, so this sub-surface
3523 * will not be drawn either.
3524 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003525
Pekka Paalanene67858b2013-04-25 13:57:42 +03003526 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003527 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003528
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003529 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003530 * because that would call it also for the parent surface,
3531 * which might not be mapped yet. That would lead to
3532 * inconsistent state, where the window could never be
3533 * mapped.
3534 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003535 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003536 * weston_surface_is_mapped() return true, so that when the
3537 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003538 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003539 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003540 }
3541}
3542
3543static struct weston_subsurface *
3544weston_surface_to_subsurface(struct weston_surface *surface)
3545{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003546 if (surface->committed == subsurface_committed)
3547 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003548
3549 return NULL;
3550}
3551
Pekka Paalanen01388e22013-04-25 13:57:44 +03003552WL_EXPORT struct weston_surface *
3553weston_surface_get_main_surface(struct weston_surface *surface)
3554{
3555 struct weston_subsurface *sub;
3556
3557 while (surface && (sub = weston_surface_to_subsurface(surface)))
3558 surface = sub->parent;
3559
3560 return surface;
3561}
3562
Pekka Paalanen50b67472014-10-01 15:02:41 +03003563WL_EXPORT int
3564weston_surface_set_role(struct weston_surface *surface,
3565 const char *role_name,
3566 struct wl_resource *error_resource,
3567 uint32_t error_code)
3568{
3569 assert(role_name);
3570
3571 if (surface->role_name == NULL ||
3572 surface->role_name == role_name ||
3573 strcmp(surface->role_name, role_name) == 0) {
3574 surface->role_name = role_name;
3575
3576 return 0;
3577 }
3578
3579 wl_resource_post_error(error_resource, error_code,
3580 "Cannot assign role %s to wl_surface@%d,"
3581 " already has role %s\n",
3582 role_name,
3583 wl_resource_get_id(surface->resource),
3584 surface->role_name);
3585 return -1;
3586}
3587
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003588WL_EXPORT const char *
3589weston_surface_get_role(struct weston_surface *surface)
3590{
3591 return surface->role_name;
3592}
3593
Pekka Paalanen8274d902014-08-06 19:36:51 +03003594WL_EXPORT void
3595weston_surface_set_label_func(struct weston_surface *surface,
3596 int (*desc)(struct weston_surface *,
3597 char *, size_t))
3598{
3599 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003600 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003601}
3602
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003603/** Get the size of surface contents
3604 *
3605 * \param surface The surface to query.
3606 * \param width Returns the width of raw contents.
3607 * \param height Returns the height of raw contents.
3608 *
3609 * Retrieves the raw surface content size in pixels for the given surface.
3610 * This is the whole content size in buffer pixels. If the surface
3611 * has no content or the renderer does not implement this feature,
3612 * zeroes are returned.
3613 *
3614 * This function is used to determine the buffer size needed for
3615 * a weston_surface_copy_content() call.
3616 */
3617WL_EXPORT void
3618weston_surface_get_content_size(struct weston_surface *surface,
3619 int *width, int *height)
3620{
3621 struct weston_renderer *rer = surface->compositor->renderer;
3622
3623 if (!rer->surface_get_content_size) {
3624 *width = 0;
3625 *height = 0;
3626 return;
3627 }
3628
3629 rer->surface_get_content_size(surface, width, height);
3630}
3631
Quentin Glidic248dd102016-08-12 10:41:34 +02003632/** Get the bounding box of a surface and its subsurfaces
3633 *
3634 * \param surface The surface to query.
3635 * \return The bounding box relative to the surface origin.
3636 *
3637 */
3638WL_EXPORT struct weston_geometry
3639weston_surface_get_bounding_box(struct weston_surface *surface)
3640{
3641 pixman_region32_t region;
3642 pixman_box32_t *box;
3643 struct weston_subsurface *subsurface;
3644
3645 pixman_region32_init_rect(&region,
3646 0, 0,
3647 surface->width, surface->height);
3648
3649 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3650 pixman_region32_union_rect(&region, &region,
3651 subsurface->position.x,
3652 subsurface->position.y,
3653 subsurface->surface->width,
3654 subsurface->surface->height);
3655
3656 box = pixman_region32_extents(&region);
3657 struct weston_geometry geometry = {
3658 .x = box->x1,
3659 .y = box->y1,
3660 .width = box->x2 - box->x1,
3661 .height = box->y2 - box->y1,
3662 };
3663
3664 pixman_region32_fini(&region);
3665
3666 return geometry;
3667}
3668
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003669/** Copy surface contents to system memory.
3670 *
3671 * \param surface The surface to copy from.
3672 * \param target Pointer to the target memory buffer.
3673 * \param size Size of the target buffer in bytes.
3674 * \param src_x X location on contents to copy from.
3675 * \param src_y Y location on contents to copy from.
3676 * \param width Width in pixels of the area to copy.
3677 * \param height Height in pixels of the area to copy.
3678 * \return 0 for success, -1 for failure.
3679 *
3680 * Surface contents are maintained by the renderer. They can be in a
3681 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3682 * else.
3683 *
3684 * Surface contents are copied into memory pointed to by target,
3685 * which has size bytes of space available. The target memory
3686 * may be larger than needed, but being smaller returns an error.
3687 * The extra bytes in target may or may not be written; their content is
3688 * unspecified. Size must be large enough to hold the image.
3689 *
3690 * The image in the target memory will be arranged in rows from
3691 * top to bottom, and pixels on a row from left to right. The pixel
3692 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3693 * width * 4.
3694 *
3695 * Parameters src_x and src_y define the upper-left corner in buffer
3696 * coordinates (pixels) to copy from. Parameters width and height
3697 * define the size of the area to copy in pixels.
3698 *
3699 * The rectangle defined by src_x, src_y, width, height must fit in
3700 * the surface contents. Otherwise an error is returned.
3701 *
3702 * Use surface_get_data_size to determine the content size; the
3703 * needed target buffer size and rectangle limits.
3704 *
3705 * CURRENT IMPLEMENTATION RESTRICTIONS:
3706 * - the machine must be little-endian due to Pixman formats.
3707 *
3708 * NOTE: Pixman formats are premultiplied.
3709 */
3710WL_EXPORT int
3711weston_surface_copy_content(struct weston_surface *surface,
3712 void *target, size_t size,
3713 int src_x, int src_y,
3714 int width, int height)
3715{
3716 struct weston_renderer *rer = surface->compositor->renderer;
3717 int cw, ch;
3718 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3719
3720 if (!rer->surface_copy_content)
3721 return -1;
3722
3723 weston_surface_get_content_size(surface, &cw, &ch);
3724
3725 if (src_x < 0 || src_y < 0)
3726 return -1;
3727
3728 if (width <= 0 || height <= 0)
3729 return -1;
3730
3731 if (src_x + width > cw || src_y + height > ch)
3732 return -1;
3733
3734 if (width * bytespp * height > size)
3735 return -1;
3736
3737 return rer->surface_copy_content(surface, target, size,
3738 src_x, src_y, width, height);
3739}
3740
Pekka Paalanene67858b2013-04-25 13:57:42 +03003741static void
3742subsurface_set_position(struct wl_client *client,
3743 struct wl_resource *resource, int32_t x, int32_t y)
3744{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003745 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003746
3747 if (!sub)
3748 return;
3749
3750 sub->position.x = x;
3751 sub->position.y = y;
3752 sub->position.set = 1;
3753}
3754
3755static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003756subsurface_find_sibling(struct weston_subsurface *sub,
3757 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003758{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003759 struct weston_surface *parent = sub->parent;
3760 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003761
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003762 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3763 if (sibling->surface == surface && sibling != sub)
3764 return sibling;
3765 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003766
3767 return NULL;
3768}
3769
3770static struct weston_subsurface *
3771subsurface_sibling_check(struct weston_subsurface *sub,
3772 struct weston_surface *surface,
3773 const char *request)
3774{
3775 struct weston_subsurface *sibling;
3776
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003777 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003778 if (!sibling) {
3779 wl_resource_post_error(sub->resource,
3780 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3781 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003782 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003783 return NULL;
3784 }
3785
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003786 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003787
3788 return sibling;
3789}
3790
3791static void
3792subsurface_place_above(struct wl_client *client,
3793 struct wl_resource *resource,
3794 struct wl_resource *sibling_resource)
3795{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003796 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003797 struct weston_surface *surface =
3798 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003799 struct weston_subsurface *sibling;
3800
3801 if (!sub)
3802 return;
3803
3804 sibling = subsurface_sibling_check(sub, surface, "place_above");
3805 if (!sibling)
3806 return;
3807
3808 wl_list_remove(&sub->parent_link_pending);
3809 wl_list_insert(sibling->parent_link_pending.prev,
3810 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003811
3812 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003813}
3814
3815static void
3816subsurface_place_below(struct wl_client *client,
3817 struct wl_resource *resource,
3818 struct wl_resource *sibling_resource)
3819{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003820 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003821 struct weston_surface *surface =
3822 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003823 struct weston_subsurface *sibling;
3824
3825 if (!sub)
3826 return;
3827
3828 sibling = subsurface_sibling_check(sub, surface, "place_below");
3829 if (!sibling)
3830 return;
3831
3832 wl_list_remove(&sub->parent_link_pending);
3833 wl_list_insert(&sibling->parent_link_pending,
3834 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003835
3836 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003837}
3838
3839static void
3840subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3841{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003842 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003843
3844 if (sub)
3845 sub->synchronized = 1;
3846}
3847
3848static void
3849subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3850{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003851 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003852
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003853 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003854 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003855
3856 /* If sub became effectively desynchronized, flush. */
3857 if (!weston_subsurface_is_synchronized(sub))
3858 weston_subsurface_synchronized_commit(sub);
3859 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003860}
3861
3862static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003863weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3864{
3865 wl_list_remove(&sub->parent_link);
3866 wl_list_remove(&sub->parent_link_pending);
3867 wl_list_remove(&sub->parent_destroy_listener.link);
3868 sub->parent = NULL;
3869}
3870
3871static void
3872weston_subsurface_destroy(struct weston_subsurface *sub);
3873
3874static void
3875subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3876{
3877 struct weston_subsurface *sub =
3878 container_of(listener, struct weston_subsurface,
3879 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003880 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003881
3882 /* The protocol object (wl_resource) is left inert. */
3883 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003884 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003885
3886 weston_subsurface_destroy(sub);
3887}
3888
3889static void
3890subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3891{
3892 struct weston_subsurface *sub =
3893 container_of(listener, struct weston_subsurface,
3894 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003895 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003896 assert(sub->surface != sub->parent);
3897
3898 if (weston_surface_is_mapped(sub->surface))
3899 weston_surface_unmap(sub->surface);
3900
3901 weston_subsurface_unlink_parent(sub);
3902}
3903
3904static void
3905subsurface_resource_destroy(struct wl_resource *resource)
3906{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003907 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003908
3909 if (sub)
3910 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003911}
3912
3913static void
3914subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3915{
3916 wl_resource_destroy(resource);
3917}
3918
3919static void
3920weston_subsurface_link_parent(struct weston_subsurface *sub,
3921 struct weston_surface *parent)
3922{
3923 sub->parent = parent;
3924 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003925 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003926 &sub->parent_destroy_listener);
3927
3928 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3929 wl_list_insert(&parent->subsurface_list_pending,
3930 &sub->parent_link_pending);
3931}
3932
3933static void
3934weston_subsurface_link_surface(struct weston_subsurface *sub,
3935 struct weston_surface *surface)
3936{
3937 sub->surface = surface;
3938 sub->surface_destroy_listener.notify =
3939 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003940 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003941 &sub->surface_destroy_listener);
3942}
3943
3944static void
3945weston_subsurface_destroy(struct weston_subsurface *sub)
3946{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003947 struct weston_view *view, *next;
3948
Pekka Paalanene67858b2013-04-25 13:57:42 +03003949 assert(sub->surface);
3950
3951 if (sub->resource) {
3952 assert(weston_surface_to_subsurface(sub->surface) == sub);
3953 assert(sub->parent_destroy_listener.notify ==
3954 subsurface_handle_parent_destroy);
3955
George Kiagiadakised04d382014-06-13 18:10:26 +02003956 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3957 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003958 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003959 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003960
Pekka Paalanene67858b2013-04-25 13:57:42 +03003961 if (sub->parent)
3962 weston_subsurface_unlink_parent(sub);
3963
Jason Ekstrand7b982072014-05-20 14:33:03 -05003964 weston_surface_state_fini(&sub->cached);
3965 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003966
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003967 sub->surface->committed = NULL;
3968 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003969 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003970 } else {
3971 /* the dummy weston_subsurface for the parent itself */
3972 assert(sub->parent_destroy_listener.notify == NULL);
3973 wl_list_remove(&sub->parent_link);
3974 wl_list_remove(&sub->parent_link_pending);
3975 }
3976
3977 wl_list_remove(&sub->surface_destroy_listener.link);
3978 free(sub);
3979}
3980
3981static const struct wl_subsurface_interface subsurface_implementation = {
3982 subsurface_destroy,
3983 subsurface_set_position,
3984 subsurface_place_above,
3985 subsurface_place_below,
3986 subsurface_set_sync,
3987 subsurface_set_desync
3988};
3989
3990static struct weston_subsurface *
3991weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3992 struct weston_surface *parent)
3993{
3994 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003995 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003996
Bryce Harringtonde16d892014-11-20 22:21:57 -08003997 sub = zalloc(sizeof *sub);
3998 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003999 return NULL;
4000
Jason Ekstranda7af7042013-10-12 22:38:11 -05004001 wl_list_init(&sub->unused_views);
4002
Jason Ekstranda85118c2013-06-27 20:17:02 -05004003 sub->resource =
4004 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004005 if (!sub->resource) {
4006 free(sub);
4007 return NULL;
4008 }
4009
Jason Ekstranda85118c2013-06-27 20:17:02 -05004010 wl_resource_set_implementation(sub->resource,
4011 &subsurface_implementation,
4012 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004013 weston_subsurface_link_surface(sub, surface);
4014 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004015 weston_surface_state_init(&sub->cached);
4016 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004017 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004018
4019 return sub;
4020}
4021
4022/* Create a dummy subsurface for having the parent itself in its
4023 * sub-surface lists. Makes stacking order manipulation easy.
4024 */
4025static struct weston_subsurface *
4026weston_subsurface_create_for_parent(struct weston_surface *parent)
4027{
4028 struct weston_subsurface *sub;
4029
Bryce Harringtonde16d892014-11-20 22:21:57 -08004030 sub = zalloc(sizeof *sub);
4031 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004032 return NULL;
4033
4034 weston_subsurface_link_surface(sub, parent);
4035 sub->parent = parent;
4036 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4037 wl_list_insert(&parent->subsurface_list_pending,
4038 &sub->parent_link_pending);
4039
4040 return sub;
4041}
4042
4043static void
4044subcompositor_get_subsurface(struct wl_client *client,
4045 struct wl_resource *resource,
4046 uint32_t id,
4047 struct wl_resource *surface_resource,
4048 struct wl_resource *parent_resource)
4049{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004050 struct weston_surface *surface =
4051 wl_resource_get_user_data(surface_resource);
4052 struct weston_surface *parent =
4053 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004054 struct weston_subsurface *sub;
4055 static const char where[] = "get_subsurface: wl_subsurface@";
4056
4057 if (surface == parent) {
4058 wl_resource_post_error(resource,
4059 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4060 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004061 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004062 return;
4063 }
4064
4065 if (weston_surface_to_subsurface(surface)) {
4066 wl_resource_post_error(resource,
4067 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4068 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004069 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004070 return;
4071 }
4072
Pekka Paalanen50b67472014-10-01 15:02:41 +03004073 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4074 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004075 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004076
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004077 if (weston_surface_get_main_surface(parent) == surface) {
4078 wl_resource_post_error(resource,
4079 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4080 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004081 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004082 return;
4083 }
4084
Pekka Paalanene67858b2013-04-25 13:57:42 +03004085 /* make sure the parent is in its own list */
4086 if (wl_list_empty(&parent->subsurface_list)) {
4087 if (!weston_subsurface_create_for_parent(parent)) {
4088 wl_resource_post_no_memory(resource);
4089 return;
4090 }
4091 }
4092
4093 sub = weston_subsurface_create(id, surface, parent);
4094 if (!sub) {
4095 wl_resource_post_no_memory(resource);
4096 return;
4097 }
4098
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004099 surface->committed = subsurface_committed;
4100 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004101 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004102}
4103
4104static void
4105subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4106{
4107 wl_resource_destroy(resource);
4108}
4109
4110static const struct wl_subcompositor_interface subcompositor_interface = {
4111 subcompositor_destroy,
4112 subcompositor_get_subsurface
4113};
4114
4115static void
4116bind_subcompositor(struct wl_client *client,
4117 void *data, uint32_t version, uint32_t id)
4118{
4119 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004120 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004121
Jason Ekstranda85118c2013-06-27 20:17:02 -05004122 resource =
4123 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004124 if (resource == NULL) {
4125 wl_client_post_no_memory(client);
4126 return;
4127 }
4128 wl_resource_set_implementation(resource, &subcompositor_interface,
4129 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004130}
4131
Bryce Harrington0795ece2016-08-30 12:04:26 -07004132/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004133 *
4134 * \param compositor The compositor instance
4135 * \param state The DPMS state the outputs will be set to
4136 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004137static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004138weston_compositor_dpms(struct weston_compositor *compositor,
4139 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004140{
4141 struct weston_output *output;
4142
Bryce Harrington08976ac2016-08-30 12:05:16 -07004143 wl_list_for_each(output, &compositor->output_list, link)
4144 if (output->set_dpms)
4145 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004146}
4147
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004148/** Restores the compositor to active status
4149 *
4150 * \param compositor The compositor instance
4151 *
4152 * If the compositor was in a sleeping mode, all outputs are powered
4153 * back on via DPMS. Otherwise if the compositor was inactive
4154 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4155 * signal will fire.
4156 *
4157 * Restarts the idle timer.
4158 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004159WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004160weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004161{
Neil Roberts8b62e202013-09-30 13:14:47 +01004162 uint32_t old_state = compositor->state;
4163
4164 /* The state needs to be changed before emitting the wake
4165 * signal because that may try to schedule a repaint which
4166 * will not work if the compositor is still sleeping */
4167 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4168
4169 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004170 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004171 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004172 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004173 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004174 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004175 /* fall through */
4176 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004177 wl_event_source_timer_update(compositor->idle_source,
4178 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004179 }
4180}
4181
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004182/** Turns off rendering and frame events for the compositor.
4183 *
4184 * \param compositor The compositor instance
4185 *
4186 * This is used for example to prevent further rendering while the
4187 * compositor is shutting down.
4188 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004189 * Stops the idle timer.
4190 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004191WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004192weston_compositor_offscreen(struct weston_compositor *compositor)
4193{
4194 switch (compositor->state) {
4195 case WESTON_COMPOSITOR_OFFSCREEN:
4196 return;
4197 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004198 default:
4199 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4200 wl_event_source_timer_update(compositor->idle_source, 0);
4201 }
4202}
4203
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004204/** Powers down all attached output devices
4205 *
4206 * \param compositor The compositor instance
4207 *
4208 * Causes rendering to the outputs to cease, and no frame events to be
4209 * sent. Only powers down the outputs if the compositor is not already
4210 * in sleep mode.
4211 *
4212 * Stops the idle timer.
4213 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004214WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004215weston_compositor_sleep(struct weston_compositor *compositor)
4216{
4217 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4218 return;
4219
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004220 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004221 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4222 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4223}
4224
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004225/** Sets compositor to idle mode
4226 *
4227 * \param data The compositor instance
4228 *
4229 * This is called when the idle timer fires. Once the compositor is in
4230 * idle mode it requires a wake action (e.g. via
4231 * weston_compositor_wake()) to restore it. The compositor's
4232 * idle_signal will be triggered when the idle event occurs.
4233 *
4234 * Idleness can be inhibited by setting the compositor's idle_inhibit
4235 * property.
4236 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004237static int
4238idle_handler(void *data)
4239{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004240 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004241
4242 if (compositor->idle_inhibit)
4243 return 1;
4244
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004245 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004246 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004247
4248 return 1;
4249}
4250
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004251WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004252weston_plane_init(struct weston_plane *plane,
4253 struct weston_compositor *ec,
4254 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004255{
4256 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004257 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004258 plane->x = x;
4259 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004260 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004261
4262 /* Init the link so that the call to wl_list_remove() when releasing
4263 * the plane without ever stacking doesn't lead to a crash */
4264 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004265}
4266
4267WL_EXPORT void
4268weston_plane_release(struct weston_plane *plane)
4269{
Xiong Zhang97116532013-10-23 13:58:31 +08004270 struct weston_view *view;
4271
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004272 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004273 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004274
Xiong Zhang97116532013-10-23 13:58:31 +08004275 wl_list_for_each(view, &plane->compositor->view_list, link) {
4276 if (view->plane == plane)
4277 view->plane = NULL;
4278 }
4279
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004280 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004281}
4282
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004283WL_EXPORT void
4284weston_compositor_stack_plane(struct weston_compositor *ec,
4285 struct weston_plane *plane,
4286 struct weston_plane *above)
4287{
4288 if (above)
4289 wl_list_insert(above->link.prev, &plane->link);
4290 else
4291 wl_list_insert(&ec->plane_list, &plane->link);
4292}
4293
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004294static void
4295output_release(struct wl_client *client, struct wl_resource *resource)
4296{
4297 wl_resource_destroy(resource);
4298}
4299
4300static const struct wl_output_interface output_interface = {
4301 output_release,
4302};
4303
4304
Casey Dahlin9074db52012-04-19 22:50:09 -04004305static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004306{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004307 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004308}
4309
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004310static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004311bind_output(struct wl_client *client,
4312 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004313{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004314 struct weston_output *output = data;
4315 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004316 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004317
Jason Ekstranda85118c2013-06-27 20:17:02 -05004318 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004319 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004320 if (resource == NULL) {
4321 wl_client_post_no_memory(client);
4322 return;
4323 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004324
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004325 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004326 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004327
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004328 wl_output_send_geometry(resource,
4329 output->x,
4330 output->y,
4331 output->mm_width,
4332 output->mm_height,
4333 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004334 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004335 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004336 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004337 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004338 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004339
4340 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004341 wl_output_send_mode(resource,
4342 mode->flags,
4343 mode->width,
4344 mode->height,
4345 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004346 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004347
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004348 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004349 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004350}
4351
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004352/** Get the backing object of wl_output
4353 *
4354 * \param resource A wl_output protocol object.
4355 * \return The backing object (user data) of a wl_resource representing a
4356 * wl_output protocol object.
4357 */
4358WL_EXPORT struct weston_output *
4359weston_output_from_resource(struct wl_resource *resource)
4360{
4361 assert(wl_resource_instance_of(resource, &wl_output_interface,
4362 &output_interface));
4363
4364 return wl_resource_get_user_data(resource);
4365}
4366
4367
David Fort0de859e2016-05-27 23:22:57 +02004368/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004369static void
David Fort0de859e2016-05-27 23:22:57 +02004370weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4371 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004372{
4373 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004374 bool start_resizing = false;
4375
4376 if (!delta_width)
4377 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004378
4379 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004380 if (output == resized_output) {
4381 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004382 continue;
4383 }
4384
David Fort0de859e2016-05-27 23:22:57 +02004385 if (start_resizing) {
4386 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004387 output->dirty = 1;
4388 }
4389 }
4390}
4391
Pekka Paalanend72bad22017-03-29 17:01:41 +03004392static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004393weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004394{
Scott Moreau850ca422012-05-21 15:21:25 -06004395 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004396
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004397 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004398 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004399
Scott Moreauccbf29d2012-02-22 14:21:41 -07004400 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004401 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004402 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004403 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004404 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004405 weston_matrix_scale(&output->matrix, magnification,
4406 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004407 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004408
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004409 switch (output->transform) {
4410 case WL_OUTPUT_TRANSFORM_FLIPPED:
4411 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4412 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4413 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4414 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4415 weston_matrix_scale(&output->matrix, -1, 1, 1);
4416 break;
4417 }
4418
4419 switch (output->transform) {
4420 default:
4421 case WL_OUTPUT_TRANSFORM_NORMAL:
4422 case WL_OUTPUT_TRANSFORM_FLIPPED:
4423 break;
4424 case WL_OUTPUT_TRANSFORM_90:
4425 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4426 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4427 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4428 break;
4429 case WL_OUTPUT_TRANSFORM_180:
4430 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4431 weston_matrix_translate(&output->matrix,
4432 -output->width, -output->height, 0);
4433 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4434 break;
4435 case WL_OUTPUT_TRANSFORM_270:
4436 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4437 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4438 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4439 break;
4440 }
4441
4442 if (output->current_scale != 1)
4443 weston_matrix_scale(&output->matrix,
4444 output->current_scale,
4445 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004446
Scott Moreauccbf29d2012-02-22 14:21:41 -07004447 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004448
4449 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004450}
4451
Scott Moreau1bad5db2012-08-18 01:04:05 -06004452static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004453weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004454{
4455 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004456 output->native_scale = scale;
4457 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004458
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004459 convert_size_by_transform_scale(&output->width, &output->height,
4460 output->current_mode->width,
4461 output->current_mode->height,
4462 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004463}
4464
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004465static void
4466weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004467{
4468 output->x = x;
4469 output->y = y;
4470
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004471 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004472 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004473
4474 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004475 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004476 output->width,
4477 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004478}
4479
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004480WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004481weston_output_move(struct weston_output *output, int x, int y)
4482{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004483 struct wl_resource *resource;
4484
4485 output->move_x = x - output->x;
4486 output->move_y = y - output->y;
4487
4488 if (output->move_x == 0 && output->move_y == 0)
4489 return;
4490
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004491 weston_output_init_geometry(output, x, y);
4492
4493 output->dirty = 1;
4494
4495 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004496 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004497
4498 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004499 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004500 wl_output_send_geometry(resource,
4501 output->x,
4502 output->y,
4503 output->mm_width,
4504 output->mm_height,
4505 output->subpixel,
4506 output->make,
4507 output->model,
4508 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004509
FORT David8a120692016-04-26 23:34:06 +02004510 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004511 wl_output_send_done(resource);
4512 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004513}
4514
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004515/** Signal that a pending output is taken into use.
4516 *
4517 * Removes the output from the pending list and adds it to the compositor's
4518 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03004519 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004520 * The output gets an internal ID assigned, and the wl_output global is
4521 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004522 *
Giulio Camuffob1147152015-05-06 21:41:57 +03004523 * \param compositor The compositor instance.
4524 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004525 *
4526 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03004527 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03004528static void
Giulio Camuffob1147152015-05-06 21:41:57 +03004529weston_compositor_add_output(struct weston_compositor *compositor,
4530 struct weston_output *output)
4531{
Armin Krezoviće5403842016-08-05 15:28:29 +02004532 struct weston_view *view, *next;
4533
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004534 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004535
4536 /* Verify we haven't reached the limit of 32 available output IDs */
4537 assert(ffs(~compositor->output_id_pool) > 0);
4538
4539 /* Invert the output id pool and look for the lowest numbered
4540 * switch (the least significant bit). Take that bit's position
4541 * as our ID, and mark it used in the compositor's output_id_pool.
4542 */
4543 output->id = ffs(~compositor->output_id_pool) - 1;
4544 compositor->output_id_pool |= 1u << output->id;
4545
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004546 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03004547 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004548 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004549
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004550 output->global = wl_global_create(compositor->wl_display,
4551 &wl_output_interface, 3,
4552 output, bind_output);
4553
Giulio Camuffob1147152015-05-06 21:41:57 +03004554 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02004555
4556 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4557 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004558}
4559
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004560/** Transform device coordinates into global coordinates
4561 *
4562 * \param device_x[in] X coordinate in device units.
4563 * \param device_y[in] Y coordinate in device units.
4564 * \param x[out] X coordinate in the global space.
4565 * \param y[out] Y coordinate in the global space.
4566 *
4567 * Transforms coordinates from the device coordinate space
4568 * (physical pixel units) to the global coordinate space (logical pixel units).
4569 * This takes into account output transform and scale.
4570 *
4571 * \memberof weston_output
4572 * \internal
4573 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004574WL_EXPORT void
4575weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004576 double device_x, double device_y,
4577 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004578{
Derek Foreman0f679412014-10-02 13:41:17 -05004579 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004580 device_x,
4581 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004582 0.0,
4583 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004584
Derek Foreman67a18b92015-03-24 11:36:14 -05004585 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004586
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004587 *x = p.f[0] / p.f[3];
4588 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004589}
4590
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004591/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004592 *
4593 * \param output The weston_output object that is being removed.
4594 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004595 * The following happens:
4596 *
4597 * - The output assignments of all views in the current scenegraph are
4598 * recomputed.
4599 *
4600 * - Presentation feedback is discarded.
4601 *
4602 * - Compositor is notified that outputs were changed and
4603 * applies the necessary changes to re-layout outputs.
4604 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004605 * - The output is put back in the pending outputs list.
4606 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004607 * - Signal is emitted to notify all users of the weston_output
4608 * object that the output is being destroyed.
4609 *
4610 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004611 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004612 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004613 * - The output's internal ID is released.
4614 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004615 * \memberof weston_output
4616 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004617 */
4618static void
4619weston_compositor_remove_output(struct weston_output *output)
4620{
Pekka Paalanenbccda712017-03-29 16:16:04 +03004621 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004622 struct wl_resource *resource;
4623 struct weston_view *view;
4624
4625 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004626 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004627
Pekka Paalanenbccda712017-03-29 16:16:04 +03004628 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004629 if (view->output_mask & (1u << output->id))
4630 weston_view_assign_output(view);
4631 }
4632
4633 weston_presentation_feedback_discard_list(&output->feedback_list);
4634
Pekka Paalanenbccda712017-03-29 16:16:04 +03004635 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004636
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004637 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004638 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004639 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004640
Pekka Paalanenbccda712017-03-29 16:16:04 +03004641 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004642 wl_signal_emit(&output->destroy_signal, output);
4643
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004644 wl_global_destroy(output->global);
4645 output->global = NULL;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004646 wl_resource_for_each(resource, &output->resource_list) {
4647 wl_resource_set_destructor(resource, NULL);
4648 }
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004649
4650 compositor->output_id_pool &= ~(1u << output->id);
4651 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004652}
4653
4654/** Sets the output scale for a given output.
4655 *
4656 * \param output The weston_output object that the scale is set for.
4657 * \param scale Scale factor for the given output.
4658 *
4659 * It only supports setting scale for an output that
4660 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004661 *
4662 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004663 */
4664WL_EXPORT void
4665weston_output_set_scale(struct weston_output *output,
4666 int32_t scale)
4667{
4668 /* We can only set scale on a disabled output */
4669 assert(!output->enabled);
4670
4671 /* We only want to set scale once */
4672 assert(!output->scale);
4673
4674 output->scale = scale;
4675}
4676
4677/** Sets the output transform for a given output.
4678 *
4679 * \param output The weston_output object that the transform is set for.
4680 * \param transform Transform value for the given output.
4681 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004682 * Refer to wl_output::transform section located at
4683 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
4684 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004685 *
4686 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004687 */
4688WL_EXPORT void
4689weston_output_set_transform(struct weston_output *output,
4690 uint32_t transform)
4691{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00004692 struct weston_pointer_motion_event ev;
4693 struct wl_resource *resource;
4694 struct weston_seat *seat;
4695 pixman_region32_t old_region;
4696 int mid_x, mid_y;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004697
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00004698 if (!output->enabled && output->transform == UINT32_MAX) {
4699 output->transform = transform;
4700 return;
4701 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004702
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00004703 weston_output_transform_scale_init(output, transform, output->scale);
4704
4705 pixman_region32_init(&old_region);
4706 pixman_region32_copy(&old_region, &output->region);
4707
4708 pixman_region32_fini(&output->region);
4709 pixman_region32_fini(&output->previous_damage);
4710
4711 weston_output_init_geometry(output, output->x, output->y);
4712
4713 output->dirty = 1;
4714
4715 /* Notify clients of the change for output transform. */
4716 wl_resource_for_each(resource, &output->resource_list) {
4717 wl_output_send_geometry(resource,
4718 output->x,
4719 output->y,
4720 output->mm_width,
4721 output->mm_height,
4722 output->subpixel,
4723 output->make,
4724 output->model,
4725 output->transform);
4726
4727 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
4728 wl_output_send_done(resource);
4729 }
4730
4731 /* we must ensure that pointers are inside output, otherwise they disappear */
4732 mid_x = output->x + output->width / 2;
4733 mid_y = output->y + output->height / 2;
4734
4735 ev.mask = WESTON_POINTER_MOTION_ABS;
4736 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
4737 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
4738
4739 wl_list_for_each(seat, &output->compositor->seat_list, link) {
4740 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4741
4742 if (pointer && pixman_region32_contains_point(&old_region,
4743 wl_fixed_to_int(pointer->x),
4744 wl_fixed_to_int(pointer->y),
4745 NULL))
4746 weston_pointer_move(pointer, &ev);
4747 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004748}
4749
4750/** Initializes a weston_output object with enough data so
4751 ** an output can be configured.
4752 *
4753 * \param output The weston_output object to initialize
4754 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004755 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004756 *
4757 * Sets initial values for fields that are expected to be
4758 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004759 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004760 * The name is used in logs, and can be used by compositors as a configuration
4761 * identifier.
4762 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004763 * \memberof weston_output
4764 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004765 */
4766WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02004767weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004768 struct weston_compositor *compositor,
4769 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004770{
4771 output->compositor = compositor;
4772 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004773 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004774 wl_list_init(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004775 output->enabled = false;
4776
4777 /* Add some (in)sane defaults which can be used
4778 * for checking if an output was properly configured
4779 */
4780 output->mm_width = 0;
4781 output->mm_height = 0;
4782 output->scale = 0;
4783 /* Can't use -1 on uint32_t and 0 is valid enum value */
4784 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004785
4786 pixman_region32_init(&output->previous_damage);
4787 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03004788 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004789}
4790
4791/** Adds weston_output object to pending output list.
4792 *
4793 * \param output The weston_output object to add
4794 * \param compositor The compositor instance.
4795 *
4796 * Also notifies the compositor that an output is pending for
4797 * configuration.
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004798 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03004799 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004800 *
4801 * \memberof weston_output
4802 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004803 */
4804WL_EXPORT void
4805weston_compositor_add_pending_output(struct weston_output *output,
4806 struct weston_compositor *compositor)
4807{
Pekka Paalanene952a012017-03-29 17:14:00 +03004808 assert(output->disable);
4809 assert(output->enable);
4810
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004811 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004812 wl_list_insert(compositor->pending_output_list.prev, &output->link);
4813 wl_signal_emit(&compositor->output_pending_signal, output);
4814}
4815
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004816/** Constructs a weston_output object that can be used by the compositor.
4817 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03004818 * \param output The weston_output object that needs to be enabled. Must not
4819 * be enabled already.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004820 *
4821 * Output coordinates are calculated and each new output is by default
4822 * assigned to the right of previous one.
4823 *
4824 * Sets up the transformation, zoom, and geometry of the output using
4825 * the properties that need to be configured by the compositor.
4826 *
4827 * Establishes a repaint timer for the output with the relevant display
4828 * object's event loop. See output_repaint_timer_handler().
4829 *
4830 * The output is assigned an ID. Weston can support up to 32 distinct
4831 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4832 * is referred to and used to find the first available ID number, and
4833 * then this ID is marked as used in output_id_pool.
4834 *
4835 * The output is also assigned a Wayland global with the wl_output
4836 * external interface.
4837 *
4838 * Backend specific function is called to set up the output output.
4839 *
4840 * Output is added to the compositor's output list
4841 *
4842 * If the backend specific function fails, the weston_output object
4843 * is returned to a state it was before calling this function and
4844 * is added to the compositor's pending_output_list in case it needs
4845 * to be reconfigured or just so it can be destroyed at shutdown.
4846 *
4847 * 0 is returned on success, -1 on failure.
4848 */
4849WL_EXPORT int
4850weston_output_enable(struct weston_output *output)
4851{
Armin Krezović782f5df2016-09-30 14:11:11 +02004852 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004853 struct weston_output *iterator;
4854 int x = 0, y = 0;
4855
Pekka Paalanencc201e42017-03-30 15:11:25 +03004856 if (output->enabled) {
4857 weston_log("Error: attempt to enable an enabled output '%s'\n",
4858 output->name);
4859 return -1;
4860 }
4861
Armin Krezović782f5df2016-09-30 14:11:11 +02004862 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004863 struct weston_output, link);
4864
Armin Krezović782f5df2016-09-30 14:11:11 +02004865 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004866 x = iterator->x + iterator->width;
4867
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004868 /* Make sure the scale is set up */
4869 assert(output->scale);
4870
4871 /* Make sure we have a transform set */
4872 assert(output->transform != UINT32_MAX);
4873
Armin Krezović782f5df2016-09-30 14:11:11 +02004874 output->x = x;
4875 output->y = y;
4876 output->dirty = 1;
4877 output->original_scale = output->scale;
4878
4879 weston_output_transform_scale_init(output, output->transform, output->scale);
4880 weston_output_init_zoom(output);
4881
4882 weston_output_init_geometry(output, x, y);
4883 weston_output_damage(output);
4884
4885 wl_signal_init(&output->frame_signal);
4886 wl_signal_init(&output->destroy_signal);
4887 wl_list_init(&output->animation_list);
4888 wl_list_init(&output->resource_list);
4889 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02004890
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004891 /* Enable the output (set up the crtc or create a
4892 * window representing the output, set up the
4893 * renderer, etc)
4894 */
4895 if (output->enable(output) < 0) {
4896 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004897 return -1;
4898 }
4899
4900 weston_compositor_add_output(output->compositor, output);
4901
4902 return 0;
4903}
4904
4905/** Converts a weston_output object to a pending output state, so it
4906 ** can be configured again or destroyed.
4907 *
4908 * \param output The weston_output object that needs to be disabled.
4909 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004910 * Calls a backend specific function to disable an output, in case
4911 * such function exists.
4912 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004913 * The backend specific disable function may choose to postpone the disabling
4914 * by returning a negative value, in which case this function returns early.
4915 * In that case the backend will guarantee the output will be disabled soon
4916 * by the backend calling this function again. One must not attempt to re-enable
4917 * the output until that happens.
4918 *
4919 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004920 * from weston's output_list (see weston_compositor_remove_output())
4921 * and is returned to a state it was before weston_output_enable()
4922 * was ran (see weston_output_enable_undo()).
4923 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004924 * See weston_output_init() for more information on the
4925 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03004926 *
4927 * If the output has never been enabled yet, this function can still be
4928 * called to ensure that the output is actually turned off rather than left
4929 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004930 */
4931WL_EXPORT void
4932weston_output_disable(struct weston_output *output)
4933{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004934 /* Should we rename this? */
4935 output->destroying = 1;
4936
Pekka Paalanenc65df642017-03-29 15:45:46 +03004937 /* Disable is called unconditionally also for not-enabled outputs,
4938 * because at compositor start-up, if there is an output that is
4939 * already on but the compositor wants to turn it off, we have to
4940 * forward the turn-off to the backend so it knows to do it.
4941 * The backend cannot initially turn off everything, because it
4942 * would cause unnecessary mode-sets for all outputs the compositor
4943 * wants to be on.
4944 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004945 if (output->disable(output) < 0)
4946 return;
4947
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004948 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004949 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004950
4951 output->destroying = 0;
4952}
4953
4954/** Emits a signal to indicate that there are outputs waiting to be configured.
4955 *
4956 * \param compositor The compositor instance
4957 */
4958WL_EXPORT void
4959weston_pending_output_coldplug(struct weston_compositor *compositor)
4960{
4961 struct weston_output *output, *next;
4962
4963 wl_list_for_each_safe(output, next, &compositor->pending_output_list, link)
4964 wl_signal_emit(&compositor->output_pending_signal, output);
4965}
4966
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004967/** Uninitialize an output
4968 *
4969 * Removes the output from the list of enabled outputs if necessary, but
4970 * does not call the backend's output disable function. The output will no
4971 * longer be in the list of pending outputs either.
4972 *
4973 * All fields of weston_output become uninitialized, i.e. should not be used
4974 * anymore. The caller can free the memory after this.
4975 *
4976 * \memberof weston_output
4977 * \internal
4978 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004979WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03004980weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004981{
4982 output->destroying = 1;
4983
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004984 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004985 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004986
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004987 pixman_region32_fini(&output->region);
4988 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004989 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004990 free(output->name);
4991}
4992
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004993static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004994destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004995{
Jonny Lamb74130762013-11-26 18:19:46 +01004996 struct weston_surface *surface =
4997 wl_resource_get_user_data(resource);
4998
Pekka Paalanen4826f872016-04-22 14:14:38 +03004999 if (!surface)
5000 return;
5001
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005002 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02005003 surface->pending.buffer_viewport.buffer.src_width =
5004 wl_fixed_from_int(-1);
5005 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005006 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005007}
5008
5009static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005010viewport_destroy(struct wl_client *client,
5011 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005012{
5013 wl_resource_destroy(resource);
5014}
5015
5016static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005017viewport_set_source(struct wl_client *client,
5018 struct wl_resource *resource,
5019 wl_fixed_t src_x,
5020 wl_fixed_t src_y,
5021 wl_fixed_t src_width,
5022 wl_fixed_t src_height)
5023{
5024 struct weston_surface *surface =
5025 wl_resource_get_user_data(resource);
5026
Pekka Paalanen4826f872016-04-22 14:14:38 +03005027 if (!surface) {
5028 wl_resource_post_error(resource,
5029 WP_VIEWPORT_ERROR_NO_SURFACE,
5030 "wl_surface for this viewport is no longer exists");
5031 return;
5032 }
5033
5034 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03005035 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005036
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005037 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03005038 src_height == wl_fixed_from_int(-1) &&
5039 src_x == wl_fixed_from_int(-1) &&
5040 src_y == wl_fixed_from_int(-1)) {
5041 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005042 surface->pending.buffer_viewport.buffer.src_width =
5043 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005044 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005045 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005046 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005047
Pekka Paalanen201769a2016-04-26 14:42:11 +03005048 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005049 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005050 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03005051 "wl_surface@%d viewport source "
5052 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
5053 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005054 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03005055 wl_fixed_to_double(src_height),
5056 wl_fixed_to_double(src_x),
5057 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005058 return;
5059 }
5060
5061 surface->pending.buffer_viewport.buffer.src_x = src_x;
5062 surface->pending.buffer_viewport.buffer.src_y = src_y;
5063 surface->pending.buffer_viewport.buffer.src_width = src_width;
5064 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005065 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005066}
5067
5068static void
5069viewport_set_destination(struct wl_client *client,
5070 struct wl_resource *resource,
5071 int32_t dst_width,
5072 int32_t dst_height)
5073{
5074 struct weston_surface *surface =
5075 wl_resource_get_user_data(resource);
5076
Pekka Paalanen4826f872016-04-22 14:14:38 +03005077 if (!surface) {
5078 wl_resource_post_error(resource,
5079 WP_VIEWPORT_ERROR_NO_SURFACE,
5080 "wl_surface for this viewport no longer exists");
5081 return;
5082 }
5083
5084 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005085
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005086 if (dst_width == -1 && dst_height == -1) {
5087 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005088 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005089 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005090 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005091 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005092
5093 if (dst_width <= 0 || dst_height <= 0) {
5094 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005095 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005096 "destination size must be positive (%dx%d)",
5097 dst_width, dst_height);
5098 return;
5099 }
5100
5101 surface->pending.buffer_viewport.surface.width = dst_width;
5102 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005103 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005104}
5105
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005106static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005107 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005108 viewport_set_source,
5109 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01005110};
5111
5112static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005113viewporter_destroy(struct wl_client *client,
5114 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005115{
5116 wl_resource_destroy(resource);
5117}
5118
5119static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005120viewporter_get_viewport(struct wl_client *client,
5121 struct wl_resource *viewporter,
5122 uint32_t id,
5123 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005124{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005125 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005126 struct weston_surface *surface =
5127 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005128 struct wl_resource *resource;
5129
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005130 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005131 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005132 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005133 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01005134 return;
5135 }
5136
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005137 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005138 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005139 if (resource == NULL) {
5140 wl_client_post_no_memory(client);
5141 return;
5142 }
5143
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005144 wl_resource_set_implementation(resource, &viewport_interface,
5145 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01005146
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005147 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005148}
5149
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005150static const struct wp_viewporter_interface viewporter_interface = {
5151 viewporter_destroy,
5152 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01005153};
5154
5155static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005156bind_viewporter(struct wl_client *client,
5157 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005158{
5159 struct wl_resource *resource;
5160
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005161 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005162 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005163 if (resource == NULL) {
5164 wl_client_post_no_memory(client);
5165 return;
5166 }
5167
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005168 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01005169 NULL, NULL);
5170}
5171
5172static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04005173destroy_presentation_feedback(struct wl_resource *feedback_resource)
5174{
5175 struct weston_presentation_feedback *feedback;
5176
5177 feedback = wl_resource_get_user_data(feedback_resource);
5178
5179 wl_list_remove(&feedback->link);
5180 free(feedback);
5181}
5182
5183static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005184presentation_destroy(struct wl_client *client, struct wl_resource *resource)
5185{
5186 wl_resource_destroy(resource);
5187}
5188
5189static void
5190presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005191 struct wl_resource *presentation_resource,
5192 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005193 uint32_t callback)
5194{
Pekka Paalanen133e4392014-09-23 22:08:46 -04005195 struct weston_surface *surface;
5196 struct weston_presentation_feedback *feedback;
5197
5198 surface = wl_resource_get_user_data(surface_resource);
5199
Bryce Harringtonde16d892014-11-20 22:21:57 -08005200 feedback = zalloc(sizeof *feedback);
5201 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04005202 goto err_calloc;
5203
5204 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005205 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005206 1, callback);
5207 if (!feedback->resource)
5208 goto err_create;
5209
5210 wl_resource_set_implementation(feedback->resource, NULL, feedback,
5211 destroy_presentation_feedback);
5212
5213 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
5214
5215 return;
5216
5217err_create:
5218 free(feedback);
5219
5220err_calloc:
5221 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005222}
5223
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005224static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005225 presentation_destroy,
5226 presentation_feedback
5227};
5228
5229static void
5230bind_presentation(struct wl_client *client,
5231 void *data, uint32_t version, uint32_t id)
5232{
5233 struct weston_compositor *compositor = data;
5234 struct wl_resource *resource;
5235
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005236 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005237 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005238 if (resource == NULL) {
5239 wl_client_post_no_memory(client);
5240 return;
5241 }
5242
5243 wl_resource_set_implementation(resource, &presentation_implementation,
5244 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005245 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005246}
5247
5248static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05005249compositor_bind(struct wl_client *client,
5250 void *data, uint32_t version, uint32_t id)
5251{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005252 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005253 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05005254
Jason Ekstranda85118c2013-06-27 20:17:02 -05005255 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005256 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005257 if (resource == NULL) {
5258 wl_client_post_no_memory(client);
5259 return;
5260 }
5261
5262 wl_resource_set_implementation(resource, &compositor_interface,
5263 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05005264}
5265
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005266WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005267weston_environment_get_fd(const char *env)
5268{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005269 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005270 int fd, flags;
5271
5272 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005273 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005274 return -1;
5275
5276 flags = fcntl(fd, F_GETFD);
5277 if (flags == -1)
5278 return -1;
5279
5280 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
5281 unsetenv(env);
5282
5283 return fd;
5284}
5285
Pekka Paalanenb5026542014-11-12 15:09:24 +02005286static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005287timeline_key_binding_handler(struct weston_keyboard *keyboard,
5288 const struct timespec *time, uint32_t key,
5289 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02005290{
5291 struct weston_compositor *compositor = data;
5292
5293 if (weston_timeline_enabled_)
5294 weston_timeline_close();
5295 else
5296 weston_timeline_open(compositor);
5297}
5298
Giulio Camuffo459137b2014-10-11 23:56:24 +03005299/** Create the compositor.
5300 *
5301 * This functions creates and initializes a compositor instance.
5302 *
5303 * \param display The Wayland display to be used.
5304 * \param user_data A pointer to an object that can later be retrieved
5305 * using the \ref weston_compositor_get_user_data function.
5306 * \return The compositor instance on success or NULL on failure.
5307 */
5308WL_EXPORT struct weston_compositor *
5309weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005310{
Giulio Camuffo459137b2014-10-11 23:56:24 +03005311 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05005312 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07005313
Giulio Camuffo459137b2014-10-11 23:56:24 +03005314 ec = zalloc(sizeof *ec);
5315 if (!ec)
5316 return NULL;
5317
5318 ec->wl_display = display;
5319 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005320 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07005321 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005322 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005323 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005324 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005325 wl_signal_init(&ec->idle_signal);
5326 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005327 wl_signal_init(&ec->show_input_panel_signal);
5328 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005329 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01005330 wl_signal_init(&ec->seat_created_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005331 wl_signal_init(&ec->output_pending_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01005332 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02005333 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005334 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02005335 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005336 wl_signal_init(&ec->session_signal);
5337 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005338
Casey Dahlin58ba1372012-04-19 22:50:08 -04005339 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03005340 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04005341
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02005342 ec->activate_serial = 1;
5343
Derek Foreman152254b2015-11-26 14:17:48 -06005344 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005345 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005346 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05005347
Giulio Camuffo954f1832014-10-11 18:27:30 +03005348 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005349 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005350 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03005351
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005352 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005353 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005354 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005355
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005356 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005357 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005358 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005359
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005360 if (weston_input_init(ec) != 0)
5361 goto fail;
5362
Jason Ekstranda7af7042013-10-12 22:38:11 -05005363 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005364 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005365 wl_list_init(&ec->layer_list);
5366 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005367 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005368 wl_list_init(&ec->output_list);
5369 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01005370 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005371 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005372 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005373 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005374 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005375
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005376 wl_list_init(&ec->plugin_api_list);
5377
Xiong Zhang97116532013-10-23 13:58:31 +08005378 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005379 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005380
Giulio Camuffo459137b2014-10-11 23:56:24 +03005381 wl_data_device_manager_init(ec->wl_display);
5382
5383 wl_display_init_shm(ec->wl_display);
5384
5385 loop = wl_display_get_event_loop(ec->wl_display);
5386 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00005387 ec->repaint_timer =
5388 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
5389 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005390
Quentin Glidic82681572016-12-17 13:40:51 +01005391 weston_layer_init(&ec->fade_layer, ec);
5392 weston_layer_init(&ec->cursor_layer, ec);
5393
5394 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
5395 weston_layer_set_position(&ec->cursor_layer,
5396 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005397
5398 weston_compositor_add_debug_binding(ec, KEY_T,
5399 timeline_key_binding_handler, ec);
5400
Giulio Camuffo459137b2014-10-11 23:56:24 +03005401 return ec;
5402
5403fail:
5404 free(ec);
5405 return NULL;
5406}
5407
Benjamin Franzkeb8263022011-08-30 11:32:47 +02005408WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005409weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07005410{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005411 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07005412
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005413 wl_event_source_remove(ec->idle_source);
5414
Matt Roper361d2ad2011-08-29 13:52:23 -07005415 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02005416 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07005417 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02005418
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005419 /* Destroy all pending outputs associated with this compositor */
5420 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
5421 output->destroy(output);
5422
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02005423 if (ec->renderer)
5424 ec->renderer->destroy(ec);
5425
Daniel Stone325fc2d2012-05-30 16:31:58 +01005426 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09005427 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005428 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005429 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005430 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005431 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005432
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005433 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07005434}
5435
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005436WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04005437weston_compositor_exit_with_code(struct weston_compositor *compositor,
5438 int exit_code)
5439{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02005440 if (compositor->exit_code == EXIT_SUCCESS)
5441 compositor->exit_code = exit_code;
5442
Giulio Camuffo459137b2014-10-11 23:56:24 +03005443 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04005444}
5445
5446WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005447weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
5448 const struct weston_pointer_grab_interface *interface)
5449{
5450 struct weston_seat *seat;
5451
5452 ec->default_pointer_grab = interface;
5453 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005454 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5455
5456 if (pointer)
5457 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005458 }
5459}
5460
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04005461WL_EXPORT int
5462weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
5463 clockid_t clk_id)
5464{
5465 struct timespec ts;
5466
5467 if (clock_gettime(clk_id, &ts) < 0)
5468 return -1;
5469
5470 compositor->presentation_clock = clk_id;
5471
5472 return 0;
5473}
5474
5475/*
5476 * For choosing the software clock, when the display hardware or API
5477 * does not expose a compatible presentation timestamp.
5478 */
5479WL_EXPORT int
5480weston_compositor_set_presentation_clock_software(
5481 struct weston_compositor *compositor)
5482{
5483 /* In order of preference */
5484 static const clockid_t clocks[] = {
5485 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
5486 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
5487 CLOCK_MONOTONIC, /* no jumps, may crawl */
5488 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
5489 CLOCK_REALTIME /* may jump and crawl */
5490 };
5491 unsigned i;
5492
5493 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
5494 if (weston_compositor_set_presentation_clock(compositor,
5495 clocks[i]) == 0)
5496 return 0;
5497
5498 weston_log("Error: no suitable presentation clock available.\n");
5499
5500 return -1;
5501}
5502
Pekka Paalanen662f3842015-03-18 12:17:26 +02005503/** Read the current time from the Presentation clock
5504 *
5505 * \param compositor
5506 * \param ts[out] The current time.
5507 *
5508 * \note Reading the current time in user space is always imprecise to some
5509 * degree.
5510 *
5511 * This function is never meant to fail. If reading the clock does fail,
5512 * an error message is logged and a zero time is returned. Callers are not
5513 * supposed to detect or react to failures.
5514 */
5515WL_EXPORT void
5516weston_compositor_read_presentation_clock(
5517 const struct weston_compositor *compositor,
5518 struct timespec *ts)
5519{
5520 static bool warned;
5521 int ret;
5522
5523 ret = clock_gettime(compositor->presentation_clock, ts);
5524 if (ret < 0) {
5525 ts->tv_sec = 0;
5526 ts->tv_nsec = 0;
5527
5528 if (!warned)
5529 weston_log("Error: failure to read "
5530 "the presentation clock %#x: '%m' (%d)\n",
5531 compositor->presentation_clock, errno);
5532 warned = true;
5533 }
5534}
5535
Pekka Paalanen230f3b12014-09-29 14:18:40 -04005536/** Import dmabuf buffer into current renderer
5537 *
5538 * \param compositor
5539 * \param buffer the dmabuf buffer to import
5540 * \return true on usable buffers, false otherwise
5541 *
5542 * This function tests that the linux_dmabuf_buffer is usable
5543 * for the current renderer. Returns false on unusable buffers. Usually
5544 * usability is tested by importing the dmabufs for composition.
5545 *
5546 * This hook is also used for detecting if the renderer supports
5547 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
5548 * supported.
5549 * */
5550WL_EXPORT bool
5551weston_compositor_import_dmabuf(struct weston_compositor *compositor,
5552 struct linux_dmabuf_buffer *buffer)
5553{
5554 struct weston_renderer *renderer;
5555
5556 renderer = compositor->renderer;
5557
5558 if (renderer->import_dmabuf == NULL)
5559 return false;
5560
5561 return renderer->import_dmabuf(compositor, buffer);
5562}
5563
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005564WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005565weston_version(int *major, int *minor, int *micro)
5566{
5567 *major = WESTON_VERSION_MAJOR;
5568 *minor = WESTON_VERSION_MINOR;
5569 *micro = WESTON_VERSION_MICRO;
5570}
5571
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005572WL_EXPORT void *
5573weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005574{
Derek Foreman3f86e502015-06-08 11:46:54 -05005575 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005576 char path[PATH_MAX];
5577 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00005578 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005579
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005580 if (name == NULL)
5581 return NULL;
5582
Derek Foreman3f86e502015-06-08 11:46:54 -05005583 if (name[0] != '/') {
5584 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00005585 len = snprintf(path, sizeof path, "%s/.libs/%s",
5586 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005587 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00005588 len = snprintf(path, sizeof path, "%s/%s",
5589 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005590 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00005591 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005592 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005593
Daniel Stonebeb97e52016-11-28 12:13:54 +00005594 /* snprintf returns the length of the string it would've written,
5595 * _excluding_ the NUL byte. So even being equal to the size of
5596 * our buffer is an error here. */
5597 if (len >= sizeof path)
5598 return NULL;
5599
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005600 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
5601 if (module) {
5602 weston_log("Module '%s' already loaded\n", path);
5603 dlclose(module);
5604 return NULL;
5605 }
5606
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005607 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04005608 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005609 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005610 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005611 return NULL;
5612 }
5613
5614 init = dlsym(module, entrypoint);
5615 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005616 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00005617 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005618 return NULL;
5619 }
5620
5621 return init;
5622}
5623
Giulio Camuffo459137b2014-10-11 23:56:24 +03005624
5625/** Destroys the compositor.
5626 *
5627 * This function cleans up the compositor state and destroys it.
5628 *
5629 * \param compositor The compositor to be destroyed.
5630 */
5631WL_EXPORT void
5632weston_compositor_destroy(struct weston_compositor *compositor)
5633{
5634 /* prevent further rendering while shutting down */
5635 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5636
5637 wl_signal_emit(&compositor->destroy_signal, compositor);
5638
5639 weston_compositor_xkb_destroy(compositor);
5640
Giulio Camuffo2d24e642015-10-03 16:25:15 +03005641 if (compositor->backend)
5642 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005643
5644 weston_plugin_api_destroy_list(compositor);
5645
Giulio Camuffo459137b2014-10-11 23:56:24 +03005646 free(compositor);
5647}
5648
5649/** Instruct the compositor to exit.
5650 *
5651 * This functions does not directly destroy the compositor object, it merely
5652 * command it to start the tear down process. It is not guaranteed that the
5653 * tear down will happen immediately.
5654 *
5655 * \param compositor The compositor to tear down.
5656 */
5657WL_EXPORT void
5658weston_compositor_exit(struct weston_compositor *compositor)
5659{
5660 compositor->exit(compositor);
5661}
5662
5663/** Return the user data stored in the compositor.
5664 *
5665 * This function returns the user data pointer set with user_data parameter
5666 * to the \ref weston_compositor_create function.
5667 */
5668WL_EXPORT void *
5669weston_compositor_get_user_data(struct weston_compositor *compositor)
5670{
5671 return compositor->user_data;
5672}
Pekka Paalanendd186732016-06-03 14:49:54 +03005673
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005674static const char * const backend_map[] = {
5675 [WESTON_BACKEND_DRM] = "drm-backend.so",
5676 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5677 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5678 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5679 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5680 [WESTON_BACKEND_X11] = "x11-backend.so",
5681};
5682
Pekka Paalanendd186732016-06-03 14:49:54 +03005683/** Load a backend into a weston_compositor
5684 *
5685 * A backend must be loaded to make a weston_compositor work. A backend
5686 * provides input and output capabilities, and determines the renderer to use.
5687 *
5688 * \param compositor A compositor that has not had a backend loaded yet.
5689 * \param backend Name of the backend file.
5690 * \param config_base A pointer to a backend-specific configuration
5691 * structure's 'base' member.
5692 *
5693 * \return 0 on success, or -1 on error.
5694 */
5695WL_EXPORT int
5696weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005697 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005698 struct weston_backend_config *config_base)
5699{
5700 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005701 struct weston_backend_config *config_base);
5702
Pekka Paalanend7e35112017-08-29 17:04:12 +03005703 if (compositor->backend) {
5704 weston_log("Error: attempt to load a backend when one is already loaded\n");
5705 return -1;
5706 }
5707
Quentin Glidic887c0182016-07-10 11:00:53 +02005708 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005709 return -1;
5710
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01005711 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005712 if (!backend_init)
5713 return -1;
5714
Pekka Paalanend7e35112017-08-29 17:04:12 +03005715 if (backend_init(compositor, config_base) < 0) {
5716 compositor->backend = NULL;
5717 return -1;
5718 }
5719
5720 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03005721}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005722
5723WL_EXPORT int
5724weston_compositor_load_xwayland(struct weston_compositor *compositor)
5725{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005726 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005727
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005728 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005729 if (!module_init)
5730 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005731 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005732 return -1;
5733 return 0;
5734}