blob: 165ff013208d92c257d33d06479290586c137dbf [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;
344 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400345
346 wl_resource_for_each(o, &output->resource_list) {
347 if (wl_resource_get_client(o) != client)
348 continue;
349
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200350 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400351 }
352
353 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200354 wp_presentation_feedback_send_presented(feedback->resource,
355 secs >> 32, secs & 0xffffffff,
356 ts->tv_nsec,
357 refresh_nsec,
358 seq >> 32, seq & 0xffffffff,
359 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400360 wl_resource_destroy(feedback->resource);
361}
362
363static void
364weston_presentation_feedback_present_list(struct wl_list *list,
365 struct weston_output *output,
366 uint32_t refresh_nsec,
367 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200368 uint64_t seq,
369 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400370{
371 struct weston_presentation_feedback *feedback, *tmp;
372
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200373 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200374 wl_list_empty(list));
375
Pekka Paalanen133e4392014-09-23 22:08:46 -0400376 wl_list_for_each_safe(feedback, tmp, list, link)
377 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200378 refresh_nsec, ts, seq,
379 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400380}
381
Jason Ekstrand7b982072014-05-20 14:33:03 -0500382static void
383surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
384{
385 struct weston_surface_state *state =
386 container_of(listener, struct weston_surface_state,
387 buffer_destroy_listener);
388
389 state->buffer = NULL;
390}
391
392static void
393weston_surface_state_init(struct weston_surface_state *state)
394{
395 state->newly_attached = 0;
396 state->buffer = NULL;
397 state->buffer_destroy_listener.notify =
398 surface_state_handle_buffer_destroy;
399 state->sx = 0;
400 state->sy = 0;
401
Derek Foreman152254b2015-11-26 14:17:48 -0600402 pixman_region32_init(&state->damage_surface);
403 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500404 pixman_region32_init(&state->opaque);
405 region_init_infinite(&state->input);
406
407 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400408 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500409
410 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
411 state->buffer_viewport.buffer.scale = 1;
412 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
413 state->buffer_viewport.surface.width = -1;
414 state->buffer_viewport.changed = 0;
415}
416
417static void
418weston_surface_state_fini(struct weston_surface_state *state)
419{
420 struct weston_frame_callback *cb, *next;
421
422 wl_list_for_each_safe(cb, next,
423 &state->frame_callback_list, link)
424 wl_resource_destroy(cb->resource);
425
Pekka Paalanen133e4392014-09-23 22:08:46 -0400426 weston_presentation_feedback_discard_list(&state->feedback_list);
427
Jason Ekstrand7b982072014-05-20 14:33:03 -0500428 pixman_region32_fini(&state->input);
429 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600430 pixman_region32_fini(&state->damage_surface);
431 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500432
433 if (state->buffer)
434 wl_list_remove(&state->buffer_destroy_listener.link);
435 state->buffer = NULL;
436}
437
438static void
439weston_surface_state_set_buffer(struct weston_surface_state *state,
440 struct weston_buffer *buffer)
441{
442 if (state->buffer == buffer)
443 return;
444
445 if (state->buffer)
446 wl_list_remove(&state->buffer_destroy_listener.link);
447 state->buffer = buffer;
448 if (state->buffer)
449 wl_signal_add(&state->buffer->destroy_signal,
450 &state->buffer_destroy_listener);
451}
452
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500453WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500454weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500455{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500456 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400457
Bryce Harringtonde16d892014-11-20 22:21:57 -0800458 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400459 if (surface == NULL)
460 return NULL;
461
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500462 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800463 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500464
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500465 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200466 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400467
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200468 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
469 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200470 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
471 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500472
473 weston_surface_state_init(&surface->pending);
474
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400475 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500476 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300477 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400478
Jason Ekstranda7af7042013-10-12 22:38:11 -0500479 wl_list_init(&surface->views);
480
481 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400482 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500483
Pekka Paalanene67858b2013-04-25 13:57:42 +0300484 wl_list_init(&surface->subsurface_list);
485 wl_list_init(&surface->subsurface_list_pending);
486
Jason Ekstrand1e059042014-10-16 10:55:19 -0500487 weston_matrix_init(&surface->buffer_to_surface_matrix);
488 weston_matrix_init(&surface->surface_to_buffer_matrix);
489
Jonas Ådahld3414f22016-07-22 17:56:31 +0800490 wl_list_init(&surface->pointer_constraints);
491
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400492 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500493}
494
Alex Wu8811bf92012-02-28 18:07:54 +0800495WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500496weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200497 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500498{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100499 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500500}
501
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400502WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500503weston_view_to_global_float(struct weston_view *view,
504 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200505{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500506 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200507 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
508
Jason Ekstranda7af7042013-10-12 22:38:11 -0500509 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200510
511 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200512 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700513 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200514 v.f[3]);
515 *x = 0;
516 *y = 0;
517 return;
518 }
519
520 *x = v.f[0] / v.f[3];
521 *y = v.f[1] / v.f[3];
522 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500523 *x = sx + view->geometry.x;
524 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200525 }
526}
527
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500528WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200529weston_transformed_coord(int width, int height,
530 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200531 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200532 float sx, float sy, float *bx, float *by)
533{
534 switch (transform) {
535 case WL_OUTPUT_TRANSFORM_NORMAL:
536 default:
537 *bx = sx;
538 *by = sy;
539 break;
540 case WL_OUTPUT_TRANSFORM_FLIPPED:
541 *bx = width - sx;
542 *by = sy;
543 break;
544 case WL_OUTPUT_TRANSFORM_90:
545 *bx = height - sy;
546 *by = sx;
547 break;
548 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
549 *bx = height - sy;
550 *by = width - sx;
551 break;
552 case WL_OUTPUT_TRANSFORM_180:
553 *bx = width - sx;
554 *by = height - sy;
555 break;
556 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
557 *bx = sx;
558 *by = height - sy;
559 break;
560 case WL_OUTPUT_TRANSFORM_270:
561 *bx = sy;
562 *by = width - sx;
563 break;
564 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
565 *bx = sy;
566 *by = sx;
567 break;
568 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200569
570 *bx *= scale;
571 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200572}
573
574WL_EXPORT pixman_box32_t
575weston_transformed_rect(int width, int height,
576 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200577 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200578 pixman_box32_t rect)
579{
580 float x1, x2, y1, y2;
581
582 pixman_box32_t ret;
583
Alexander Larsson4ea95522013-05-22 14:41:37 +0200584 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200585 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200586 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200587 rect.x2, rect.y2, &x2, &y2);
588
589 if (x1 <= x2) {
590 ret.x1 = x1;
591 ret.x2 = x2;
592 } else {
593 ret.x1 = x2;
594 ret.x2 = x1;
595 }
596
597 if (y1 <= y2) {
598 ret.y1 = y1;
599 ret.y2 = y2;
600 } else {
601 ret.y1 = y2;
602 ret.y2 = y1;
603 }
604
605 return ret;
606}
607
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600608/** Transform a region by a matrix, restricted to axis-aligned transformations
609 *
610 * Warning: This function does not work for projective, affine, or matrices
611 * that encode arbitrary rotations. Only 90-degree step rotations are
612 * supported.
613 */
614WL_EXPORT void
615weston_matrix_transform_region(pixman_region32_t *dest,
616 struct weston_matrix *matrix,
617 pixman_region32_t *src)
618{
619 pixman_box32_t *src_rects, *dest_rects;
620 int nrects, i;
621
622 src_rects = pixman_region32_rectangles(src, &nrects);
623 dest_rects = malloc(nrects * sizeof(*dest_rects));
624 if (!dest_rects)
625 return;
626
627 for (i = 0; i < nrects; i++) {
628 struct weston_vector vec1 = {{
629 src_rects[i].x1, src_rects[i].y1, 0, 1
630 }};
631 weston_matrix_transform(matrix, &vec1);
632 vec1.f[0] /= vec1.f[3];
633 vec1.f[1] /= vec1.f[3];
634
635 struct weston_vector vec2 = {{
636 src_rects[i].x2, src_rects[i].y2, 0, 1
637 }};
638 weston_matrix_transform(matrix, &vec2);
639 vec2.f[0] /= vec2.f[3];
640 vec2.f[1] /= vec2.f[3];
641
642 if (vec1.f[0] < vec2.f[0]) {
643 dest_rects[i].x1 = floor(vec1.f[0]);
644 dest_rects[i].x2 = ceil(vec2.f[0]);
645 } else {
646 dest_rects[i].x1 = floor(vec2.f[0]);
647 dest_rects[i].x2 = ceil(vec1.f[0]);
648 }
649
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600650 if (vec1.f[1] < vec2.f[1]) {
651 dest_rects[i].y1 = floor(vec1.f[1]);
652 dest_rects[i].y2 = ceil(vec2.f[1]);
653 } else {
654 dest_rects[i].y1 = floor(vec2.f[1]);
655 dest_rects[i].y2 = ceil(vec1.f[1]);
656 }
657 }
658
659 pixman_region32_clear(dest);
660 pixman_region32_init_rects(dest, dest_rects, nrects);
661 free(dest_rects);
662}
663
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200664WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500665weston_transformed_region(int width, int height,
666 enum wl_output_transform transform,
667 int32_t scale,
668 pixman_region32_t *src, pixman_region32_t *dest)
669{
670 pixman_box32_t *src_rects, *dest_rects;
671 int nrects, i;
672
673 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
674 if (src != dest)
675 pixman_region32_copy(dest, src);
676 return;
677 }
678
679 src_rects = pixman_region32_rectangles(src, &nrects);
680 dest_rects = malloc(nrects * sizeof(*dest_rects));
681 if (!dest_rects)
682 return;
683
684 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
685 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
686 } else {
687 for (i = 0; i < nrects; i++) {
688 switch (transform) {
689 default:
690 case WL_OUTPUT_TRANSFORM_NORMAL:
691 dest_rects[i].x1 = src_rects[i].x1;
692 dest_rects[i].y1 = src_rects[i].y1;
693 dest_rects[i].x2 = src_rects[i].x2;
694 dest_rects[i].y2 = src_rects[i].y2;
695 break;
696 case WL_OUTPUT_TRANSFORM_90:
697 dest_rects[i].x1 = height - src_rects[i].y2;
698 dest_rects[i].y1 = src_rects[i].x1;
699 dest_rects[i].x2 = height - src_rects[i].y1;
700 dest_rects[i].y2 = src_rects[i].x2;
701 break;
702 case WL_OUTPUT_TRANSFORM_180:
703 dest_rects[i].x1 = width - src_rects[i].x2;
704 dest_rects[i].y1 = height - src_rects[i].y2;
705 dest_rects[i].x2 = width - src_rects[i].x1;
706 dest_rects[i].y2 = height - src_rects[i].y1;
707 break;
708 case WL_OUTPUT_TRANSFORM_270:
709 dest_rects[i].x1 = src_rects[i].y1;
710 dest_rects[i].y1 = width - src_rects[i].x2;
711 dest_rects[i].x2 = src_rects[i].y2;
712 dest_rects[i].y2 = width - src_rects[i].x1;
713 break;
714 case WL_OUTPUT_TRANSFORM_FLIPPED:
715 dest_rects[i].x1 = width - src_rects[i].x2;
716 dest_rects[i].y1 = src_rects[i].y1;
717 dest_rects[i].x2 = width - src_rects[i].x1;
718 dest_rects[i].y2 = src_rects[i].y2;
719 break;
720 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
721 dest_rects[i].x1 = height - src_rects[i].y2;
722 dest_rects[i].y1 = width - src_rects[i].x2;
723 dest_rects[i].x2 = height - src_rects[i].y1;
724 dest_rects[i].y2 = width - src_rects[i].x1;
725 break;
726 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
727 dest_rects[i].x1 = src_rects[i].x1;
728 dest_rects[i].y1 = height - src_rects[i].y2;
729 dest_rects[i].x2 = src_rects[i].x2;
730 dest_rects[i].y2 = height - src_rects[i].y1;
731 break;
732 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
733 dest_rects[i].x1 = src_rects[i].y1;
734 dest_rects[i].y1 = src_rects[i].x1;
735 dest_rects[i].x2 = src_rects[i].y2;
736 dest_rects[i].y2 = src_rects[i].x2;
737 break;
738 }
739 }
740 }
741
742 if (scale != 1) {
743 for (i = 0; i < nrects; i++) {
744 dest_rects[i].x1 *= scale;
745 dest_rects[i].x2 *= scale;
746 dest_rects[i].y1 *= scale;
747 dest_rects[i].y2 *= scale;
748 }
749 }
750
751 pixman_region32_clear(dest);
752 pixman_region32_init_rects(dest, dest_rects, nrects);
753 free(dest_rects);
754}
755
Jonny Lamb74130762013-11-26 18:19:46 +0100756static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300757viewport_surface_to_buffer(struct weston_surface *surface,
758 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100759{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200760 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200761 double src_width, src_height;
762 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200763
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200764 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
765 if (vp->surface.width == -1) {
766 *bx = sx;
767 *by = sy;
768 return;
769 }
Jonny Lamb74130762013-11-26 18:19:46 +0100770
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200771 src_x = 0.0;
772 src_y = 0.0;
773 src_width = surface->width_from_buffer;
774 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100775 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200776 src_x = wl_fixed_to_double(vp->buffer.src_x);
777 src_y = wl_fixed_to_double(vp->buffer.src_y);
778 src_width = wl_fixed_to_double(vp->buffer.src_width);
779 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100780 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200781
782 *bx = sx * src_width / surface->width + src_x;
783 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100784}
785
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500786WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200787weston_surface_to_buffer_float(struct weston_surface *surface,
788 float sx, float sy, float *bx, float *by)
789{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200790 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
791
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300792 /* first transform coordinates if the viewport is set */
793 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100794
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500795 weston_transformed_coord(surface->width_from_buffer,
796 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200797 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100798 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200799}
800
Derek Foreman99739672015-12-03 16:38:11 -0600801/** Transform a rectangle from surface coordinates to buffer coordinates
802 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300803 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600804 * from.
805 * \param rect The rectangle to transform.
806 * \return The transformed rectangle.
807 *
808 * Viewport and buffer transformations can only do translation, scaling,
809 * and rotations in 90-degree steps. Therefore the only loss in the
810 * conversion is coordinate rounding.
811 *
812 * However, some coordinate rounding takes place as an intermediate
813 * step before the buffer scale factor is applied, so the rectangle
814 * boundary may not be exactly as expected.
815 *
816 * This is OK for damage tracking since a little extra coverage is
817 * not a problem.
818 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200819WL_EXPORT pixman_box32_t
820weston_surface_to_buffer_rect(struct weston_surface *surface,
821 pixman_box32_t rect)
822{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200823 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100824 float xf, yf;
825
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300826 /* first transform box coordinates if the viewport is set */
827 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100828 rect.x1 = floorf(xf);
829 rect.y1 = floorf(yf);
830
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300831 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600832 rect.x2 = ceilf(xf);
833 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100834
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500835 return weston_transformed_rect(surface->width_from_buffer,
836 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200837 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200838 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200839}
840
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200841/** Transform a region from surface coordinates to buffer coordinates
842 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300843 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200844 * from.
845 * \param surface_region[in] The region in surface coordinates.
846 * \param buffer_region[out] The region converted to buffer coordinates.
847 *
848 * Buffer_region must be init'd, but will be completely overwritten.
849 *
850 * Viewport and buffer transformations can only do translation, scaling,
851 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600852 * conversion is from the coordinate rounding that takes place in
853 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200854 */
855WL_EXPORT void
856weston_surface_to_buffer_region(struct weston_surface *surface,
857 pixman_region32_t *surface_region,
858 pixman_region32_t *buffer_region)
859{
860 pixman_box32_t *src_rects, *dest_rects;
861 int nrects, i;
862
863 src_rects = pixman_region32_rectangles(surface_region, &nrects);
864 dest_rects = malloc(nrects * sizeof(*dest_rects));
865 if (!dest_rects)
866 return;
867
868 for (i = 0; i < nrects; i++) {
869 dest_rects[i] = weston_surface_to_buffer_rect(surface,
870 src_rects[i]);
871 }
872
873 pixman_region32_fini(buffer_region);
874 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
875 free(dest_rects);
876}
877
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200878WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400880 struct weston_plane *plane)
881{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500882 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400883 return;
884
Jason Ekstranda7af7042013-10-12 22:38:11 -0500885 weston_view_damage_below(view);
886 view->plane = plane;
887 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400888}
889
Pekka Paalanen51723d52015-02-17 13:10:01 +0200890/** Inflict damage on the plane where the view is visible.
891 *
892 * \param view The view that causes the damage.
893 *
894 * If the view is currently on a plane (including the primary plane),
895 * take the view's boundingbox, subtract all the opaque views that cover it,
896 * and add the remaining region as damage to the plane. This corresponds
897 * to the damage inflicted to the plane if this view disappeared.
898 *
899 * A repaint is scheduled for this view.
900 *
901 * The region of all opaque views covering this view is stored in
902 * weston_view::clip and updated by view_accumulate_damage() during
903 * weston_output_repaint(). Specifically, that region matches the
904 * scenegraph as it was last painted.
905 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400906WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500907weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200908{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500909 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200910
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500911 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200912 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500913 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800914 if (view->plane)
915 pixman_region32_union(&view->plane->damage,
916 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500917 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800918 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200919}
920
Bryce Harrington3f650b82015-12-23 11:01:58 -0800921/**
922 * \param es The surface
923 * \param mask The new set of outputs for the surface
924 *
925 * Sets the surface's set of outputs to the ones specified by
926 * the new output mask provided. Identifies the outputs that
927 * have changed, the posts enter and leave events for these
928 * outputs as appropriate.
929 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400930static void
931weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
932{
933 uint32_t different = es->output_mask ^ mask;
934 uint32_t entered = mask & different;
935 uint32_t left = es->output_mask & different;
936 struct weston_output *output;
937 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500938 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400939
940 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500941 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400942 return;
943 if (different == 0)
944 return;
945
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500946 client = wl_resource_get_client(es->resource);
947
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400948 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800949 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400950 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500951 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400952 client);
953 if (resource == NULL)
954 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800955 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500956 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800957 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500958 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400959 }
960}
961
Bryce Harrington3f650b82015-12-23 11:01:58 -0800962/** Recalculate which output(s) the surface has views displayed on
963 *
964 * \param es The surface to remap to outputs
965 *
966 * Finds the output that is showing the largest amount of one
967 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300968 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800969 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300970 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800971 * in the output_mask for the surface.
972 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400973static void
974weston_surface_assign_output(struct weston_surface *es)
975{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 struct weston_output *new_output;
977 struct weston_view *view;
978 pixman_region32_t region;
979 uint32_t max, area, mask;
980 pixman_box32_t *e;
981
982 new_output = NULL;
983 max = 0;
984 mask = 0;
985 pixman_region32_init(&region);
986 wl_list_for_each(view, &es->views, surface_link) {
987 if (!view->output)
988 continue;
989
990 pixman_region32_intersect(&region, &view->transform.boundingbox,
991 &view->output->region);
992
993 e = pixman_region32_extents(&region);
994 area = (e->x2 - e->x1) * (e->y2 - e->y1);
995
996 mask |= view->output_mask;
997
998 if (area >= max) {
999 new_output = view->output;
1000 max = area;
1001 }
1002 }
1003 pixman_region32_fini(&region);
1004
1005 es->output = new_output;
1006 weston_surface_update_output_mask(es, mask);
1007}
1008
Bryce Harrington3f650b82015-12-23 11:01:58 -08001009/** Recalculate which output(s) the view is displayed on
1010 *
1011 * \param ev The view to remap to outputs
1012 *
1013 * Identifies the set of outputs that the view is visible on,
1014 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001015 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001016 *
1017 * Also does the same for the view's surface. See
1018 * weston_surface_assign_output().
1019 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020static void
1021weston_view_assign_output(struct weston_view *ev)
1022{
1023 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001024 struct weston_output *output, *new_output;
1025 pixman_region32_t region;
1026 uint32_t max, area, mask;
1027 pixman_box32_t *e;
1028
1029 new_output = NULL;
1030 max = 0;
1031 mask = 0;
1032 pixman_region32_init(&region);
1033 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001034 if (output->destroying)
1035 continue;
1036
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001038 &output->region);
1039
1040 e = pixman_region32_extents(&region);
1041 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1042
1043 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001044 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001045
1046 if (area >= max) {
1047 new_output = output;
1048 max = area;
1049 }
1050 }
1051 pixman_region32_fini(&region);
1052
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 ev->output = new_output;
1054 ev->output_mask = mask;
1055
1056 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001057}
1058
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001059static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001060weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1061 int from_x, int from_y, int *to_x, int *to_y)
1062{
1063 float x, y;
1064
1065 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1066 weston_view_from_global_float(to, x, y, &x, &y);
1067
1068 *to_x = round(x);
1069 *to_y = round(y);
1070}
1071
1072static void
1073weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1074{
1075 pixman_box32_t *a;
1076 pixman_box32_t b;
1077
1078 a = pixman_region32_extents(&from->geometry.scissor);
1079
1080 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1081 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1082
1083 pixman_region32_fini(&to->geometry.scissor);
1084 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1085}
1086
1087static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001088view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001090{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001091 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1092 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001093 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001094 { inbox->x1, inbox->y1 },
1095 { inbox->x1, inbox->y2 },
1096 { inbox->x2, inbox->y1 },
1097 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001098 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001099 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001100 int i;
1101
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001102 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001103 /* avoid rounding empty bbox to 1x1 */
1104 pixman_region32_init(bbox);
1105 return;
1106 }
1107
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001108 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001109 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001110 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001111 if (x < min_x)
1112 min_x = x;
1113 if (x > max_x)
1114 max_x = x;
1115 if (y < min_y)
1116 min_y = y;
1117 if (y > max_y)
1118 max_y = y;
1119 }
1120
Pekka Paalanen219b9822012-02-08 15:38:37 +02001121 int_x = floorf(min_x);
1122 int_y = floorf(min_y);
1123 pixman_region32_init_rect(bbox, int_x, int_y,
1124 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001125}
1126
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001127static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001129{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001131
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001132 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 view->geometry.x = roundf(view->geometry.x);
1134 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001135
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001136 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1138 view->transform.position.matrix.d[12] = view->geometry.x;
1139 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001140
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001142
Jason Ekstranda7af7042013-10-12 22:38:11 -05001143 view->transform.inverse = view->transform.position.matrix;
1144 view->transform.inverse.d[12] = -view->geometry.x;
1145 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001146
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001148 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001149 view->surface->width,
1150 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001151 if (view->geometry.scissor_enabled)
1152 pixman_region32_intersect(&view->transform.boundingbox,
1153 &view->transform.boundingbox,
1154 &view->geometry.scissor);
1155
1156 pixman_region32_translate(&view->transform.boundingbox,
1157 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001158
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 if (view->alpha == 1.0) {
1160 pixman_region32_copy(&view->transform.opaque,
1161 &view->surface->opaque);
1162 pixman_region32_translate(&view->transform.opaque,
1163 view->geometry.x,
1164 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001165 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001166}
1167
1168static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001170{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171 struct weston_view *parent = view->geometry.parent;
1172 struct weston_matrix *matrix = &view->transform.matrix;
1173 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001174 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001175 pixman_region32_t surfregion;
1176 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001177
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001179
1180 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1182 view->transform.position.matrix.d[12] = view->geometry.x;
1183 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001184
1185 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001187 weston_matrix_multiply(matrix, &tform->matrix);
1188
Pekka Paalanen483243f2013-03-08 14:56:50 +02001189 if (parent)
1190 weston_matrix_multiply(matrix, &parent->transform.matrix);
1191
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001192 if (weston_matrix_invert(inverse, matrix) < 0) {
1193 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 weston_log("error: weston_view %p"
1195 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001196 return -1;
1197 }
1198
Giulio Camuffo148c1992016-09-04 18:50:46 +03001199 if (view->alpha == 1.0 &&
1200 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1201 pixman_region32_copy(&view->transform.opaque,
1202 &view->surface->opaque);
1203 pixman_region32_translate(&view->transform.opaque,
1204 matrix->d[12],
1205 matrix->d[13]);
1206 }
1207
Pekka Paalanen380adf52015-02-16 14:39:11 +02001208 pixman_region32_init_rect(&surfregion, 0, 0,
1209 view->surface->width, view->surface->height);
1210 if (view->geometry.scissor_enabled)
1211 pixman_region32_intersect(&surfregion, &surfregion,
1212 &view->geometry.scissor);
1213 surfbox = pixman_region32_extents(&surfregion);
1214
1215 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1216 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001217
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001218 return 0;
1219}
1220
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001221static struct weston_layer *
1222get_view_layer(struct weston_view *view)
1223{
1224 if (view->parent_view)
1225 return get_view_layer(view->parent_view);
1226 return view->layer_link.layer;
1227}
1228
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001229WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001231{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001233 struct weston_layer *layer;
1234 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001235
Jason Ekstranda7af7042013-10-12 22:38:11 -05001236 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001237 return;
1238
Pekka Paalanen483243f2013-03-08 14:56:50 +02001239 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001241
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001243
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001245
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 pixman_region32_fini(&view->transform.boundingbox);
1247 pixman_region32_fini(&view->transform.opaque);
1248 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001249
Pekka Paalanencd403622012-01-25 13:37:39 +02001250 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001251 if (view->geometry.transformation_list.next ==
1252 &view->transform.position.link &&
1253 view->geometry.transformation_list.prev ==
1254 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001255 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001257 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 if (weston_view_update_transform_enable(view) < 0)
1259 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001260 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001261
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001262 layer = get_view_layer(view);
1263 if (layer) {
1264 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001265 pixman_region32_intersect(&view->transform.boundingbox,
1266 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001267 pixman_region32_intersect(&view->transform.opaque,
1268 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001269 pixman_region32_fini(&mask);
1270 }
1271
Pekka Paalanen380adf52015-02-16 14:39:11 +02001272 if (parent) {
1273 if (parent->geometry.scissor_enabled) {
1274 view->geometry.scissor_enabled = true;
1275 weston_view_transfer_scissor(parent, view);
1276 } else {
1277 view->geometry.scissor_enabled = false;
1278 }
1279 }
1280
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001282
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001284
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 wl_signal_emit(&view->surface->compositor->transform_signal,
1286 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001287}
1288
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001289WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001291{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001293
1294 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 * The invariant: if view->geometry.dirty, then all views
1296 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001297 * Corollary: if not parent->geometry.dirty, then all ancestors
1298 * are not dirty.
1299 */
1300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001302 return;
1303
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001305
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001307 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001309}
1310
1311WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312weston_view_to_global_fixed(struct weston_view *view,
1313 wl_fixed_t vx, wl_fixed_t vy,
1314 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001315{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001316 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001317
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 weston_view_to_global_float(view,
1319 wl_fixed_to_double(vx),
1320 wl_fixed_to_double(vy),
1321 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001322 *x = wl_fixed_from_double(xf);
1323 *y = wl_fixed_from_double(yf);
1324}
1325
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001326WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327weston_view_from_global_float(struct weston_view *view,
1328 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001329{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001331 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1332
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001334
1335 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001336 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001338 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339 *vx = 0;
1340 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001341 return;
1342 }
1343
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 *vx = v.f[0] / v.f[3];
1345 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001346 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 *vx = x - view->geometry.x;
1348 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001349 }
1350}
1351
1352WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353weston_view_from_global_fixed(struct weston_view *view,
1354 wl_fixed_t x, wl_fixed_t y,
1355 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001356{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 weston_view_from_global_float(view,
1360 wl_fixed_to_double(x),
1361 wl_fixed_to_double(y),
1362 &vxf, &vyf);
1363 *vx = wl_fixed_from_double(vxf);
1364 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001365}
1366
1367WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368weston_view_from_global(struct weston_view *view,
1369 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001370{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001372
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1374 *vx = floorf(vxf);
1375 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001376}
1377
Bryce Harrington3f650b82015-12-23 11:01:58 -08001378/**
1379 * \param surface The surface to be repainted
1380 *
1381 * Marks the output(s) that the surface is shown on as needing to be
1382 * repainted. See weston_output_schedule_repaint().
1383 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001384WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001385weston_surface_schedule_repaint(struct weston_surface *surface)
1386{
1387 struct weston_output *output;
1388
1389 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001390 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001391 weston_output_schedule_repaint(output);
1392}
1393
Bryce Harrington3f650b82015-12-23 11:01:58 -08001394/**
1395 * \param view The view to be repainted
1396 *
1397 * Marks the output(s) that the view is shown on as needing to be
1398 * repainted. See weston_output_schedule_repaint().
1399 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001400WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401weston_view_schedule_repaint(struct weston_view *view)
1402{
1403 struct weston_output *output;
1404
1405 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001406 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 weston_output_schedule_repaint(output);
1408}
1409
Pekka Paalanene508ce62015-02-19 13:59:55 +02001410/**
1411 * XXX: This function does it the wrong way.
1412 * surface->damage is the damage from the client, and causes
1413 * surface_flush_damage() to copy pixels. No window management action can
1414 * cause damage to the client-provided content, warranting re-upload!
1415 *
1416 * Instead of surface->damage, this function should record the damage
1417 * with all the views for this surface to avoid extraneous texture
1418 * uploads.
1419 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001421weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001422{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001423 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 0, 0, surface->width,
1425 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001426
Kristian Høgsberg98238702012-08-03 16:29:12 -04001427 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001428}
1429
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001430WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001432{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001433 if (view->geometry.x == x && view->geometry.y == y)
1434 return;
1435
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 view->geometry.x = x;
1437 view->geometry.y = y;
1438 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001439}
1440
Pekka Paalanen483243f2013-03-08 14:56:50 +02001441static void
1442transform_parent_handle_parent_destroy(struct wl_listener *listener,
1443 void *data)
1444{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 struct weston_view *view =
1446 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001447 geometry.parent_destroy_listener);
1448
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001450}
1451
1452WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001454 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001455{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 if (view->geometry.parent) {
1457 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1458 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001459
1460 if (!parent)
1461 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001462 }
1463
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001465
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001467 transform_parent_handle_parent_destroy;
1468 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001469 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001471 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001473 }
1474
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 weston_view_geometry_dirty(view);
1476}
1477
Pekka Paalanen380adf52015-02-16 14:39:11 +02001478/** Set a clip mask rectangle on a view
1479 *
1480 * \param view The view to set the clip mask on.
1481 * \param x Top-left corner X coordinate of the clip rectangle.
1482 * \param y Top-left corner Y coordinate of the clip rectangle.
1483 * \param width Width of the clip rectangle, non-negative.
1484 * \param height Height of the clip rectangle, non-negative.
1485 *
1486 * A shell may set a clip mask rectangle on a view. Everything outside
1487 * the rectangle is cut away for input and output purposes: it is
1488 * not drawn and cannot be hit by hit-test based input like pointer
1489 * motion or touch-downs. Everything inside the rectangle will behave
1490 * normally. Clients are unaware of clipping.
1491 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001492 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001493 * mask rectangle does not affect the view position, the view is positioned
1494 * as it would be without a clip. The clip also does not change
1495 * weston_surface::width,height.
1496 *
1497 * The clip mask rectangle is part of transformation inheritance
1498 * (weston_view_set_transform_parent()). A clip set in the root of the
1499 * transformation inheritance tree will affect all views in the tree.
1500 * A clip can be set only on the root view. Attempting to set a clip
1501 * on view that has a transformation parent will fail. Assigning a parent
1502 * to a view that has a clip set will cause the clip to be forgotten.
1503 *
1504 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1505 * on the additional transformations in the child views. These transformations
1506 * may not rotate the coordinate axes, i.e., only translation and scaling
1507 * are allowed. Violating this restriction causes the clipping to malfunction.
1508 * Furthermore, using scaling may cause rounding errors in child clipping.
1509 *
1510 * The clip mask rectangle is not automatically adjusted based on
1511 * wl_surface.attach dx and dy arguments.
1512 *
1513 * A clip mask rectangle can be set only if the compositor capability
1514 * WESTON_CAP_VIEW_CLIP_MASK is present.
1515 *
1516 * This function sets the clip mask rectangle and schedules a repaint for
1517 * the view.
1518 */
1519WL_EXPORT void
1520weston_view_set_mask(struct weston_view *view,
1521 int x, int y, int width, int height)
1522{
1523 struct weston_compositor *compositor = view->surface->compositor;
1524
1525 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1526 weston_log("%s not allowed without capability!\n", __func__);
1527 return;
1528 }
1529
1530 if (view->geometry.parent) {
1531 weston_log("view %p has a parent, clip forbidden!\n", view);
1532 return;
1533 }
1534
1535 if (width < 0 || height < 0) {
1536 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1537 x, y, width, height);
1538 return;
1539 }
1540
1541 pixman_region32_fini(&view->geometry.scissor);
1542 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1543 view->geometry.scissor_enabled = true;
1544 weston_view_geometry_dirty(view);
1545 weston_view_schedule_repaint(view);
1546}
1547
1548/** Remove the clip mask from a view
1549 *
1550 * \param view The view to remove the clip mask from.
1551 *
1552 * Removed the clip mask rectangle and schedules a repaint.
1553 *
1554 * \sa weston_view_set_mask
1555 */
1556WL_EXPORT void
1557weston_view_set_mask_infinite(struct weston_view *view)
1558{
1559 view->geometry.scissor_enabled = false;
1560 weston_view_geometry_dirty(view);
1561 weston_view_schedule_repaint(view);
1562}
1563
Armin Krezović0da12b82016-06-30 06:04:33 +02001564/* Check if view should be displayed
1565 *
1566 * The indicator is set manually when assigning
1567 * a view to a surface.
1568 *
1569 * This needs reworking. See the thread starting at:
1570 *
1571 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1572 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001573WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574weston_view_is_mapped(struct weston_view *view)
1575{
Armin Krezović0da12b82016-06-30 06:04:33 +02001576 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001577}
1578
Armin Krezović0da12b82016-06-30 06:04:33 +02001579/* Check if a surface has a view assigned to it
1580 *
1581 * The indicator is set manually when mapping
1582 * a surface and creating a view for it.
1583 *
1584 * This needs to go. See the thread starting at:
1585 *
1586 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1587 *
1588 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001589WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001590weston_surface_is_mapped(struct weston_surface *surface)
1591{
Armin Krezović0da12b82016-06-30 06:04:33 +02001592 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001593}
1594
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001595static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001596surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001597{
1598 struct weston_view *view;
1599
1600 if (surface->width == width && surface->height == height)
1601 return;
1602
1603 surface->width = width;
1604 surface->height = height;
1605
1606 wl_list_for_each(view, &surface->views, surface_link)
1607 weston_view_geometry_dirty(view);
1608}
1609
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001610WL_EXPORT void
1611weston_surface_set_size(struct weston_surface *surface,
1612 int32_t width, int32_t height)
1613{
1614 assert(!surface->resource);
1615 surface_set_size(surface, width, height);
1616}
1617
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001618static int
1619fixed_round_up_to_int(wl_fixed_t f)
1620{
1621 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1622}
1623
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001624static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001625convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1626 int32_t width, int32_t height,
1627 uint32_t transform,
1628 int32_t scale)
1629{
1630 assert(scale > 0);
1631
1632 switch (transform) {
1633 case WL_OUTPUT_TRANSFORM_NORMAL:
1634 case WL_OUTPUT_TRANSFORM_180:
1635 case WL_OUTPUT_TRANSFORM_FLIPPED:
1636 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1637 *width_out = width / scale;
1638 *height_out = height / scale;
1639 break;
1640 case WL_OUTPUT_TRANSFORM_90:
1641 case WL_OUTPUT_TRANSFORM_270:
1642 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1643 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1644 *width_out = height / scale;
1645 *height_out = width / scale;
1646 break;
1647 default:
1648 assert(0 && "invalid transform");
1649 }
1650}
1651
1652static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001653weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001654{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001655 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001656
1657 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001658 surface->width_from_buffer = 0;
1659 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001660 return;
1661 }
1662
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001663 convert_size_by_transform_scale(&surface->width_from_buffer,
1664 &surface->height_from_buffer,
1665 surface->buffer_ref.buffer->width,
1666 surface->buffer_ref.buffer->height,
1667 vp->buffer.transform,
1668 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001669}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001670
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001671static void
1672weston_surface_update_size(struct weston_surface *surface)
1673{
1674 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1675 int32_t width, height;
1676
1677 width = surface->width_from_buffer;
1678 height = surface->height_from_buffer;
1679
1680 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001681 surface_set_size(surface,
1682 vp->surface.width, vp->surface.height);
1683 return;
1684 }
1685
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001686 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001687 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1688 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1689
1690 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001691 return;
1692 }
1693
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001694 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001695}
1696
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001697WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001698weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001699{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001700 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001701
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001702 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001703
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001704 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001705}
1706
Jason Ekstranda7af7042013-10-12 22:38:11 -05001707WL_EXPORT struct weston_view *
1708weston_compositor_pick_view(struct weston_compositor *compositor,
1709 wl_fixed_t x, wl_fixed_t y,
1710 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001711{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001712 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001713 wl_fixed_t view_x, view_y;
1714 int view_ix, view_iy;
1715 int ix = wl_fixed_to_int(x);
1716 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001717
Jason Ekstranda7af7042013-10-12 22:38:11 -05001718 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001719 if (!pixman_region32_contains_point(
1720 &view->transform.boundingbox, ix, iy, NULL))
1721 continue;
1722
1723 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1724 view_ix = wl_fixed_to_int(view_x);
1725 view_iy = wl_fixed_to_int(view_y);
1726
1727 if (!pixman_region32_contains_point(&view->surface->input,
1728 view_ix, view_iy, NULL))
1729 continue;
1730
Pekka Paalanen380adf52015-02-16 14:39:11 +02001731 if (view->geometry.scissor_enabled &&
1732 !pixman_region32_contains_point(&view->geometry.scissor,
1733 view_ix, view_iy, NULL))
1734 continue;
1735
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001736 *vx = view_x;
1737 *vy = view_y;
1738 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001739 }
1740
Derek Foremanf9318d12015-05-11 15:40:11 -05001741 *vx = wl_fixed_from_int(-1000000);
1742 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001743 return NULL;
1744}
1745
1746static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001747weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001748{
Daniel Stone37816df2012-05-16 18:45:18 +01001749 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001750
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001751 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001752 return;
1753
Daniel Stone37816df2012-05-16 18:45:18 +01001754 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001755 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001756}
1757
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001758WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001759weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001760{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001761 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001762
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763 if (!weston_view_is_mapped(view))
1764 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001765
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766 weston_view_damage_below(view);
1767 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001768 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001769 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001770 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001771 wl_list_remove(&view->link);
1772 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001773 view->output_mask = 0;
1774 weston_surface_assign_output(view->surface);
1775
1776 if (weston_surface_is_mapped(view->surface))
1777 return;
1778
1779 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001780 struct weston_touch *touch = weston_seat_get_touch(seat);
1781 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1782 struct weston_keyboard *keyboard =
1783 weston_seat_get_keyboard(seat);
1784
1785 if (keyboard && keyboard->focus == view->surface)
1786 weston_keyboard_set_focus(keyboard, NULL);
1787 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001788 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001789 if (touch && touch->focus == view)
1790 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001791 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001792}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001793
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794WL_EXPORT void
1795weston_surface_unmap(struct weston_surface *surface)
1796{
1797 struct weston_view *view;
1798
Armin Krezovićf8486c32016-06-30 06:04:28 +02001799 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001800 wl_list_for_each(view, &surface->views, surface_link)
1801 weston_view_unmap(view);
1802 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001803}
1804
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001805static void
1806weston_surface_reset_pending_buffer(struct weston_surface *surface)
1807{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001808 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001809 surface->pending.sx = 0;
1810 surface->pending.sy = 0;
1811 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001812 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001813}
1814
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815WL_EXPORT void
1816weston_view_destroy(struct weston_view *view)
1817{
1818 wl_signal_emit(&view->destroy_signal, view);
1819
1820 assert(wl_list_empty(&view->geometry.child_list));
1821
1822 if (weston_view_is_mapped(view)) {
1823 weston_view_unmap(view);
1824 weston_compositor_build_view_list(view->surface->compositor);
1825 }
1826
1827 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001828 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829
1830 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001831 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001832 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001833 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834
1835 weston_view_set_transform_parent(view, NULL);
1836
Jason Ekstranda7af7042013-10-12 22:38:11 -05001837 wl_list_remove(&view->surface_link);
1838
1839 free(view);
1840}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001841
1842WL_EXPORT void
1843weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001844{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001845 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001846 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001847 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001848
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001849 if (--surface->ref_count > 0)
1850 return;
1851
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001852 assert(surface->resource == NULL);
1853
Pekka Paalanenca790762015-04-17 14:23:38 +03001854 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001855
Pekka Paalanene67858b2013-04-25 13:57:42 +03001856 assert(wl_list_empty(&surface->subsurface_list_pending));
1857 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001858
Jason Ekstranda7af7042013-10-12 22:38:11 -05001859 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1860 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001861
Jason Ekstrand7b982072014-05-20 14:33:03 -05001862 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001863
Pekka Paalanende685b82012-12-04 15:58:12 +02001864 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001865
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001866 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001867 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001868 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001869
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001870 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001871 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001872
Pekka Paalanen133e4392014-09-23 22:08:46 -04001873 weston_presentation_feedback_discard_list(&surface->feedback_list);
1874
Jonas Ådahld3414f22016-07-22 17:56:31 +08001875 wl_list_for_each_safe(constraint, next_constraint,
1876 &surface->pointer_constraints,
1877 link)
1878 weston_pointer_constraint_destroy(constraint);
1879
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001880 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001881}
1882
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001883static void
1884destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001885{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001886 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001887
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001888 assert(surface);
1889
Giulio Camuffo0d379742013-11-15 22:06:15 +01001890 /* Set the resource to NULL, since we don't want to leave a
1891 * dangling pointer if the surface was refcounted and survives
1892 * the weston_surface_destroy() call. */
1893 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001894
1895 if (surface->viewport_resource)
1896 wl_resource_set_user_data(surface->viewport_resource, NULL);
1897
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001898 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001899}
1900
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001901static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001902weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1903{
1904 struct weston_buffer *buffer =
1905 container_of(listener, struct weston_buffer, destroy_listener);
1906
1907 wl_signal_emit(&buffer->destroy_signal, buffer);
1908 free(buffer);
1909}
1910
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001911WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001912weston_buffer_from_resource(struct wl_resource *resource)
1913{
1914 struct weston_buffer *buffer;
1915 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001916
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001917 listener = wl_resource_get_destroy_listener(resource,
1918 weston_buffer_destroy_handler);
1919
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001920 if (listener)
1921 return container_of(listener, struct weston_buffer,
1922 destroy_listener);
1923
1924 buffer = zalloc(sizeof *buffer);
1925 if (buffer == NULL)
1926 return NULL;
1927
1928 buffer->resource = resource;
1929 wl_signal_init(&buffer->destroy_signal);
1930 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001931 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001932 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001933
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001934 return buffer;
1935}
1936
1937static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001938weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1939 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001940{
Pekka Paalanende685b82012-12-04 15:58:12 +02001941 struct weston_buffer_reference *ref =
1942 container_of(listener, struct weston_buffer_reference,
1943 destroy_listener);
1944
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001945 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001946 ref->buffer = NULL;
1947}
1948
1949WL_EXPORT void
1950weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001951 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001952{
1953 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001954 ref->buffer->busy_count--;
1955 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001956 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05001957 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05001958 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001959 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001960 }
1961
Pekka Paalanende685b82012-12-04 15:58:12 +02001962 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001963 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001964 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001965 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001966 }
1967
Pekka Paalanende685b82012-12-04 15:58:12 +02001968 ref->buffer = buffer;
1969 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1970}
1971
1972static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001973weston_surface_attach(struct weston_surface *surface,
1974 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001975{
1976 weston_buffer_reference(&surface->buffer_ref, buffer);
1977
Pekka Paalanena6421c42012-12-04 15:58:10 +02001978 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001979 if (weston_surface_is_mapped(surface))
1980 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001981 }
1982
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001983 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001984
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001985 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001986 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001987}
1988
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001989WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001990weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001991{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001992 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001993
1994 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001995 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001996}
1997
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001998WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001999weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002000{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002001 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002002
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002003 pixman_region32_union(&compositor->primary_plane.damage,
2004 &compositor->primary_plane.damage,
2005 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002006 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002007}
2008
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002009static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002010surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002011{
Pekka Paalanende685b82012-12-04 15:58:12 +02002012 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002013 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002014 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002015
Pekka Paalanenb5026542014-11-12 15:09:24 +02002016 if (weston_timeline_enabled_ &&
2017 pixman_region32_not_empty(&surface->damage))
2018 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2019 TLP_OUTPUT(surface->output), TLP_END);
2020
Jason Ekstrandef540082014-06-26 10:37:36 -07002021 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002022}
2023
2024static void
2025view_accumulate_damage(struct weston_view *view,
2026 pixman_region32_t *opaque)
2027{
2028 pixman_region32_t damage;
2029
2030 pixman_region32_init(&damage);
2031 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002032 pixman_box32_t *extents;
2033
Jason Ekstranda7af7042013-10-12 22:38:11 -05002034 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002035 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002036 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002037 pixman_region32_copy(&damage, &view->surface->damage);
2038 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002039 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002040 }
2041
Pekka Paalanen380adf52015-02-16 14:39:11 +02002042 pixman_region32_intersect(&damage, &damage,
2043 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002044 pixman_region32_subtract(&damage, &damage, opaque);
2045 pixman_region32_union(&view->plane->damage,
2046 &view->plane->damage, &damage);
2047 pixman_region32_fini(&damage);
2048 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002049 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002050}
2051
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002052static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002053compositor_accumulate_damage(struct weston_compositor *ec)
2054{
2055 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002056 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002057 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002058
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002059 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002060
2061 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002062 pixman_region32_copy(&plane->clip, &clip);
2063
2064 pixman_region32_init(&opaque);
2065
Jason Ekstranda7af7042013-10-12 22:38:11 -05002066 wl_list_for_each(ev, &ec->view_list, link) {
2067 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002068 continue;
2069
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002071 }
2072
2073 pixman_region32_union(&clip, &clip, &opaque);
2074 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002075 }
2076
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002077 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002078
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002080 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002081
2082 wl_list_for_each(ev, &ec->view_list, link) {
2083 if (ev->surface->touched)
2084 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002085 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002086
2087 surface_flush_damage(ev->surface);
2088
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002089 /* Both the renderer and the backend have seen the buffer
2090 * by now. If renderer needs the buffer, it has its own
2091 * reference set. If the backend wants to keep the buffer
2092 * around for migrating the surface into a non-primary plane
2093 * later, keep_buffer is true. Otherwise, drop the core
2094 * reference now, and allow early buffer release. This enables
2095 * clients to use single-buffering.
2096 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002097 if (!ev->surface->keep_buffer)
2098 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002099 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002100}
2101
2102static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002103surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002104{
2105 struct weston_subsurface *sub;
2106
Pekka Paalanene67858b2013-04-25 13:57:42 +03002107 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002108 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002109 continue;
2110
Jason Ekstranda7af7042013-10-12 22:38:11 -05002111 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2112 wl_list_init(&sub->surface->views);
2113
2114 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002115 }
2116}
2117
2118static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002119surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002120{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 struct weston_subsurface *sub;
2122 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002123
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2125 if (sub->surface == surface)
2126 continue;
2127
George Kiagiadakised04d382014-06-13 18:10:26 +02002128 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2129 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002130 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002131 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002132
2133 surface_free_unused_subsurface_views(sub->surface);
2134 }
2135}
2136
2137static void
2138view_list_add_subsurface_view(struct weston_compositor *compositor,
2139 struct weston_subsurface *sub,
2140 struct weston_view *parent)
2141{
2142 struct weston_subsurface *child;
2143 struct weston_view *view = NULL, *iv;
2144
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002145 if (!weston_surface_is_mapped(sub->surface))
2146 return;
2147
Jason Ekstranda7af7042013-10-12 22:38:11 -05002148 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2149 if (iv->geometry.parent == parent) {
2150 view = iv;
2151 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002152 }
2153 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002154
2155 if (view) {
2156 /* Put it back in the surface's list of views */
2157 wl_list_remove(&view->surface_link);
2158 wl_list_insert(&sub->surface->views, &view->surface_link);
2159 } else {
2160 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002161 weston_view_set_position(view,
2162 sub->position.x,
2163 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002164 weston_view_set_transform_parent(view, parent);
2165 }
2166
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002167 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002168 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002169 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002170
Pekka Paalanenb188e912013-11-19 14:03:35 +02002171 if (wl_list_empty(&sub->surface->subsurface_list)) {
2172 wl_list_insert(compositor->view_list.prev, &view->link);
2173 return;
2174 }
2175
2176 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2177 if (child->surface == sub->surface)
2178 wl_list_insert(compositor->view_list.prev, &view->link);
2179 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002181 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002182}
2183
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002184/* This recursively adds the sub-surfaces for a view, relying on the
2185 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2186 * change first happens to the sub-surface list, and then automatically
2187 * propagates here. See weston_surface_damage_subsurfaces() for how the
2188 * sub-surfaces receive damage when the client changes the state.
2189 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002190static void
2191view_list_add(struct weston_compositor *compositor,
2192 struct weston_view *view)
2193{
2194 struct weston_subsurface *sub;
2195
2196 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002197
Pekka Paalanenb188e912013-11-19 14:03:35 +02002198 if (wl_list_empty(&view->surface->subsurface_list)) {
2199 wl_list_insert(compositor->view_list.prev, &view->link);
2200 return;
2201 }
2202
2203 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2204 if (sub->surface == view->surface)
2205 wl_list_insert(compositor->view_list.prev, &view->link);
2206 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002207 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002208 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002209}
2210
2211static void
2212weston_compositor_build_view_list(struct weston_compositor *compositor)
2213{
2214 struct weston_view *view;
2215 struct weston_layer *layer;
2216
2217 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002218 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002219 surface_stash_subsurface_views(view->surface);
2220
2221 wl_list_init(&compositor->view_list);
2222 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002223 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002224 view_list_add(compositor, view);
2225 }
2226 }
2227
2228 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002229 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002231}
2232
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002233static void
2234weston_output_take_feedback_list(struct weston_output *output,
2235 struct weston_surface *surface)
2236{
2237 struct weston_view *view;
2238 struct weston_presentation_feedback *feedback;
2239 uint32_t flags = 0xffffffff;
2240
2241 if (wl_list_empty(&surface->feedback_list))
2242 return;
2243
2244 /* All views must have the flag for the flag to survive. */
2245 wl_list_for_each(view, &surface->views, surface_link) {
2246 /* ignore views that are not on this output at all */
2247 if (view->output_mask & (1u << output->id))
2248 flags &= view->psf_flags;
2249 }
2250
2251 wl_list_for_each(feedback, &surface->feedback_list, link)
2252 feedback->psf_flags = flags;
2253
2254 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2255 wl_list_init(&surface->feedback_list);
2256}
2257
David Herrmann1edf44c2013-10-22 17:11:26 +02002258static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002259weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002260{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002261 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002262 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002263 struct weston_animation *animation, *next;
2264 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002265 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002266 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002267 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002268
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002269 if (output->destroying)
2270 return 0;
2271
Pekka Paalanenb5026542014-11-12 15:09:24 +02002272 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2273
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002274 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002275 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002276
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002277 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002278 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002279 } else {
2280 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002281 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002282 ev->psf_flags = 0;
2283 }
2284 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002285
Pekka Paalanene67858b2013-04-25 13:57:42 +03002286 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002287 wl_list_for_each(ev, &ec->view_list, link) {
2288 /* Note: This operation is safe to do multiple times on the
2289 * same surface.
2290 */
2291 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002292 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293 &ev->surface->frame_callback_list);
2294 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002295
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002296 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002297 }
2298 }
2299
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002300 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002301
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002302 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002303 pixman_region32_intersect(&output_damage,
2304 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002305 pixman_region32_subtract(&output_damage,
2306 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002307
Scott Moreauccbf29d2012-02-22 14:21:41 -07002308 if (output->dirty)
2309 weston_output_update_matrix(output);
2310
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002311 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002312
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002313 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002314
Daniel Stone09a97e22017-03-01 11:34:06 +00002315 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002316 if (r == 0)
2317 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002318
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002319 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002320
Jonas Ådahldb773762012-06-13 00:01:21 +02002321 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002322 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002323 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002324 }
2325
Scott Moreaud64cf212012-06-08 19:40:54 -06002326 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002327 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002328 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002329 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002330
Pekka Paalanenb5026542014-11-12 15:09:24 +02002331 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2332
David Herrmann1edf44c2013-10-22 17:11:26 +02002333 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002334}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002335
Pekka Paalanen82919792014-05-21 13:51:49 +03002336static void
2337weston_output_schedule_repaint_reset(struct weston_output *output)
2338{
Daniel Stone05df8c12017-03-03 16:59:42 +00002339 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002340 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002341}
2342
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002343static int
2344weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2345 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002346{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002347 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002348 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002349 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002350
Daniel Stone6847b852017-03-01 11:34:08 +00002351 /* We're not ready yet; come back to make a decision later. */
2352 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002353 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002354
2355 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2356 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002357 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002358
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002359 /* If we're sleeping, drop the repaint machinery entirely; we will
2360 * explicitly repaint all outputs when we come back. */
2361 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2362 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2363 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002364
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002365 /* We don't actually need to repaint this output; drop it from
2366 * repaint until something causes damage. */
2367 if (!output->repaint_needed)
2368 goto err;
2369
2370 /* If repaint fails, we aren't going to get weston_output_finish_frame
2371 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002372 * something schedules a successful repaint later. As repainting may
2373 * take some time, re-read our clock as a courtesy to the next
2374 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002375 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002376 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002377 if (ret != 0)
2378 goto err;
2379
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002380 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002381
2382err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002383 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002384 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002385}
2386
2387static void
2388output_repaint_timer_arm(struct weston_compositor *compositor)
2389{
2390 struct weston_output *output;
2391 bool any_should_repaint = false;
2392 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002393 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002394
2395 weston_compositor_read_presentation_clock(compositor, &now);
2396
2397 wl_list_for_each(output, &compositor->output_list, link) {
2398 int64_t msec_to_this;
2399
2400 if (output->repaint_status != REPAINT_SCHEDULED)
2401 continue;
2402
2403 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2404 &now);
2405 if (!any_should_repaint || msec_to_this < msec_to_next)
2406 msec_to_next = msec_to_this;
2407
2408 any_should_repaint = true;
2409 }
2410
2411 if (!any_should_repaint)
2412 return;
2413
2414 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2415 * This is a workaround to allow coalescing multiple output repaints
2416 * particularly from weston_output_finish_frame()
2417 * into the same call, which would not happen if we called
2418 * output_repaint_timer_handler() directly.
2419 */
2420 if (msec_to_next < 1)
2421 msec_to_next = 1;
2422
2423 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2424}
2425
2426static int
2427output_repaint_timer_handler(void *data)
2428{
2429 struct weston_compositor *compositor = data;
2430 struct weston_output *output;
2431 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002432 void *repaint_data = NULL;
2433 int ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002434
2435 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002436
2437 if (compositor->backend->repaint_begin)
2438 repaint_data = compositor->backend->repaint_begin(compositor);
2439
2440 wl_list_for_each(output, &compositor->output_list, link) {
2441 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2442 if (ret)
2443 break;
2444 }
2445
2446 if (ret == 0) {
2447 if (compositor->backend->repaint_flush)
2448 compositor->backend->repaint_flush(compositor,
2449 repaint_data);
2450 } else {
2451 if (compositor->backend->repaint_cancel)
2452 compositor->backend->repaint_cancel(compositor,
2453 repaint_data);
2454 }
Daniel Stone6847b852017-03-01 11:34:08 +00002455
2456 output_repaint_timer_arm(compositor);
2457
Pekka Paalanen0513a952014-05-21 16:17:27 +03002458 return 0;
2459}
2460
Kristian Høgsbergef044142011-06-21 15:02:12 -04002461WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002462weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002463 const struct timespec *stamp,
2464 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002465{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002466 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002467 int32_t refresh_nsec;
2468 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002469 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002470
Pekka Paalanenb5026542014-11-12 15:09:24 +02002471 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2472 TLP_VBLANK(stamp), TLP_END);
2473
Daniel Stone05df8c12017-03-03 16:59:42 +00002474 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002475 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2476
Daniel Stone6847b852017-03-01 11:34:08 +00002477 weston_compositor_read_presentation_clock(compositor, &now);
2478
Daniel Stone3615ce12017-03-01 11:34:05 +00002479 /* If we haven't been supplied any timestamp at all, we don't have a
2480 * timebase to work against, so any delay just wastes time. Push a
2481 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002482 if (!stamp) {
2483 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002484 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002485 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002486
Pekka Paalanend7894d02015-07-03 15:08:53 +03002487 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002488 weston_presentation_feedback_present_list(&output->feedback_list,
2489 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002490 output->msc,
2491 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002492
Daniel Stone37ad7e32017-03-01 11:34:02 +00002493 output->frame_time = timespec_to_msec(stamp);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002494
Daniel Stone6847b852017-03-01 11:34:08 +00002495 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2496 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2497 -compositor->repaint_msec);
2498 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002499
2500 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002501 static bool warned;
2502
2503 if (!warned)
2504 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002505 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002506 warned = true;
2507
Daniel Stone6847b852017-03-01 11:34:08 +00002508 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002509 }
2510
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002511 /* Called from restart_repaint_loop and restart happens already after
2512 * the deadline given by repaint_msec? In that case we delay until
2513 * the deadline of the next frame, to give clients a more predictable
2514 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002515 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2516 msec_rel < 0) {
2517 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2518 timespec_add_nsec(&output->next_repaint,
2519 &output->next_repaint,
2520 refresh_nsec);
2521 }
2522 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002523
Daniel Stone3615ce12017-03-01 11:34:05 +00002524out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002525 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002526 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002527}
2528
2529static void
2530idle_repaint(void *data)
2531{
2532 struct weston_output *output = data;
2533
Daniel Stone05df8c12017-03-03 16:59:42 +00002534 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2535 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002536 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002537}
2538
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002539WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002540weston_layer_entry_insert(struct weston_layer_entry *list,
2541 struct weston_layer_entry *entry)
2542{
2543 wl_list_insert(&list->link, &entry->link);
2544 entry->layer = list->layer;
2545}
2546
2547WL_EXPORT void
2548weston_layer_entry_remove(struct weston_layer_entry *entry)
2549{
2550 wl_list_remove(&entry->link);
2551 wl_list_init(&entry->link);
2552 entry->layer = NULL;
2553}
2554
Quentin Glidic82681572016-12-17 13:40:51 +01002555
2556/** Initialize the weston_layer struct.
2557 *
2558 * \param compositor The compositor instance
2559 * \param layer The layer to initialize
2560 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002561WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002562weston_layer_init(struct weston_layer *layer,
2563 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002564{
Quentin Glidic82681572016-12-17 13:40:51 +01002565 layer->compositor = compositor;
2566 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002567 wl_list_init(&layer->view_list.link);
2568 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002569 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002570}
2571
2572/** Sets the position of the layer in the layer list. The layer will be placed
2573 * below any layer with the same position value, if any.
2574 * This function is safe to call if the layer is already on the list, but the
2575 * layer may be moved below other layers at the same position, if any.
2576 *
2577 * \param layer The layer to modify
2578 * \param position The position the layer will be placed at
2579 */
2580WL_EXPORT void
2581weston_layer_set_position(struct weston_layer *layer,
2582 enum weston_layer_position position)
2583{
2584 struct weston_layer *below;
2585
2586 wl_list_remove(&layer->link);
2587
2588 /* layer_list is ordered from top to bottom, the last layer being the
2589 * background with the smallest position value */
2590
2591 layer->position = position;
2592 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2593 if (below->position >= layer->position) {
2594 wl_list_insert(&below->link, &layer->link);
2595 return;
2596 }
2597 }
2598 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2599}
2600
2601/** Hide a layer by taking it off the layer list.
2602 * This function is safe to call if the layer is not on the list.
2603 *
2604 * \param layer The layer to hide
2605 */
2606WL_EXPORT void
2607weston_layer_unset_position(struct weston_layer *layer)
2608{
2609 wl_list_remove(&layer->link);
2610 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002611}
2612
2613WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002614weston_layer_set_mask(struct weston_layer *layer,
2615 int x, int y, int width, int height)
2616{
2617 struct weston_view *view;
2618
2619 layer->mask.x1 = x;
2620 layer->mask.x2 = x + width;
2621 layer->mask.y1 = y;
2622 layer->mask.y2 = y + height;
2623
2624 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2625 weston_view_geometry_dirty(view);
2626 }
2627}
2628
2629WL_EXPORT void
2630weston_layer_set_mask_infinite(struct weston_layer *layer)
2631{
2632 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2633 UINT32_MAX, UINT32_MAX);
2634}
2635
2636WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002637weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002638{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002639 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002640 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002641
Bryce Harrington08976ac2016-08-30 12:05:16 -07002642 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2643 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002644 return;
2645
Pekka Paalanenb5026542014-11-12 15:09:24 +02002646 if (!output->repaint_needed)
2647 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2648
Kristian Høgsbergef044142011-06-21 15:02:12 -04002649 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002650 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002651
2652 /* If we already have a repaint scheduled for our idle handler,
2653 * no need to set it again. If the repaint has been called but
2654 * not finished, then weston_output_finish_frame() will notice
2655 * that a repaint is needed and schedule one. */
2656 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002657 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002658
Daniel Stone05df8c12017-03-03 16:59:42 +00002659 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002660 wl_event_loop_add_idle(loop, idle_repaint, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002661 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002662}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002663
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002664WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002665weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2666{
2667 struct weston_output *output;
2668
2669 wl_list_for_each(output, &compositor->output_list, link)
2670 weston_output_schedule_repaint(output);
2671}
2672
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002673static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002674surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002675{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002676 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002677}
2678
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002679static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002680surface_attach(struct wl_client *client,
2681 struct wl_resource *resource,
2682 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2683{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002684 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002685 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002686
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002687 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002688 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002689 if (buffer == NULL) {
2690 wl_client_post_no_memory(client);
2691 return;
2692 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002693 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002694
Pekka Paalanende685b82012-12-04 15:58:12 +02002695 /* Attach, attach, without commit in between does not send
2696 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002697 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002698
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002699 surface->pending.sx = sx;
2700 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002701 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002702}
2703
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002704static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002705surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002706 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002707 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002708{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002709 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002710
Derek Foreman57e92ed2015-11-17 14:11:35 -06002711 if (width <= 0 || height <= 0)
2712 return;
2713
Derek Foreman152254b2015-11-26 14:17:48 -06002714 pixman_region32_union_rect(&surface->pending.damage_surface,
2715 &surface->pending.damage_surface,
2716 x, y, width, height);
2717}
2718
2719static void
2720surface_damage_buffer(struct wl_client *client,
2721 struct wl_resource *resource,
2722 int32_t x, int32_t y, int32_t width, int32_t height)
2723{
2724 struct weston_surface *surface = wl_resource_get_user_data(resource);
2725
2726 if (width <= 0 || height <= 0)
2727 return;
2728
2729 pixman_region32_union_rect(&surface->pending.damage_buffer,
2730 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002731 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002732}
2733
Kristian Høgsberg33418202011-08-16 23:01:28 -04002734static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002735destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002736{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002737 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002738
2739 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002740 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002741}
2742
2743static void
2744surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002745 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002746{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002747 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002748 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002749
2750 cb = malloc(sizeof *cb);
2751 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002752 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002753 return;
2754 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002755
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002756 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2757 callback);
2758 if (cb->resource == NULL) {
2759 free(cb);
2760 wl_resource_post_no_memory(resource);
2761 return;
2762 }
2763
Jason Ekstranda85118c2013-06-27 20:17:02 -05002764 wl_resource_set_implementation(cb->resource, NULL, cb,
2765 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002766
Pekka Paalanenbc106382012-10-10 12:49:31 +03002767 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002768}
2769
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002770static void
2771surface_set_opaque_region(struct wl_client *client,
2772 struct wl_resource *resource,
2773 struct wl_resource *region_resource)
2774{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002775 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002776 struct weston_region *region;
2777
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002778 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002779 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002780 pixman_region32_copy(&surface->pending.opaque,
2781 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002782 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002783 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002784 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002785}
2786
2787static void
2788surface_set_input_region(struct wl_client *client,
2789 struct wl_resource *resource,
2790 struct wl_resource *region_resource)
2791{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002792 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002793 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002794
2795 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002796 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002797 pixman_region32_copy(&surface->pending.input,
2798 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002799 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002800 pixman_region32_fini(&surface->pending.input);
2801 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002802 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002803}
2804
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002805/* Cause damage to this sub-surface and all its children.
2806 *
2807 * This is useful when there are state changes that need an implicit
2808 * damage, e.g. a z-order change.
2809 */
2810static void
2811weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2812{
2813 struct weston_subsurface *child;
2814
2815 weston_surface_damage(sub->surface);
2816 sub->reordered = false;
2817
2818 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2819 if (child != sub)
2820 weston_surface_damage_subsurfaces(child);
2821}
2822
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002823static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002824weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002825{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002826 struct weston_subsurface *sub;
2827
2828 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2829 parent_link_pending) {
2830 wl_list_remove(&sub->parent_link);
2831 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002832
2833 if (sub->reordered)
2834 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002835 }
2836}
2837
2838static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002839weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002840 struct weston_matrix *matrix)
2841{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002842 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002843 double src_width, src_height, dest_width, dest_height;
2844
2845 weston_matrix_init(matrix);
2846
2847 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2848 src_width = surface->width_from_buffer;
2849 src_height = surface->height_from_buffer;
2850 } else {
2851 src_width = wl_fixed_to_double(vp->buffer.src_width);
2852 src_height = wl_fixed_to_double(vp->buffer.src_height);
2853 }
2854
2855 if (vp->surface.width == -1) {
2856 dest_width = src_width;
2857 dest_height = src_height;
2858 } else {
2859 dest_width = vp->surface.width;
2860 dest_height = vp->surface.height;
2861 }
2862
2863 if (src_width != dest_width || src_height != dest_height)
2864 weston_matrix_scale(matrix,
2865 src_width / dest_width,
2866 src_height / dest_height, 1);
2867
2868 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2869 weston_matrix_translate(matrix,
2870 wl_fixed_to_double(vp->buffer.src_x),
2871 wl_fixed_to_double(vp->buffer.src_y),
2872 0);
2873
2874 switch (vp->buffer.transform) {
2875 case WL_OUTPUT_TRANSFORM_FLIPPED:
2876 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2877 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2878 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2879 weston_matrix_scale(matrix, -1, 1, 1);
2880 weston_matrix_translate(matrix,
2881 surface->width_from_buffer, 0, 0);
2882 break;
2883 }
2884
2885 switch (vp->buffer.transform) {
2886 default:
2887 case WL_OUTPUT_TRANSFORM_NORMAL:
2888 case WL_OUTPUT_TRANSFORM_FLIPPED:
2889 break;
2890 case WL_OUTPUT_TRANSFORM_90:
2891 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2892 weston_matrix_rotate_xy(matrix, 0, 1);
2893 weston_matrix_translate(matrix,
2894 surface->height_from_buffer, 0, 0);
2895 break;
2896 case WL_OUTPUT_TRANSFORM_180:
2897 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2898 weston_matrix_rotate_xy(matrix, -1, 0);
2899 weston_matrix_translate(matrix,
2900 surface->width_from_buffer,
2901 surface->height_from_buffer, 0);
2902 break;
2903 case WL_OUTPUT_TRANSFORM_270:
2904 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2905 weston_matrix_rotate_xy(matrix, 0, -1);
2906 weston_matrix_translate(matrix,
2907 0, surface->width_from_buffer, 0);
2908 break;
2909 }
2910
2911 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2912}
2913
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002914/**
2915 * Compute a + b > c while being safe to overflows.
2916 */
2917static bool
2918fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2919{
2920 return (int64_t)a + (int64_t)b > (int64_t)c;
2921}
2922
2923static bool
2924weston_surface_is_pending_viewport_source_valid(
2925 const struct weston_surface *surface)
2926{
2927 const struct weston_surface_state *pend = &surface->pending;
2928 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2929 int width_from_buffer = 0;
2930 int height_from_buffer = 0;
2931 wl_fixed_t w;
2932 wl_fixed_t h;
2933
2934 /* If viewport source rect is not set, it is always ok. */
2935 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2936 return true;
2937
2938 if (pend->newly_attached) {
2939 if (pend->buffer) {
2940 convert_size_by_transform_scale(&width_from_buffer,
2941 &height_from_buffer,
2942 pend->buffer->width,
2943 pend->buffer->height,
2944 vp->buffer.transform,
2945 vp->buffer.scale);
2946 } else {
2947 /* No buffer: viewport is irrelevant. */
2948 return true;
2949 }
2950 } else {
2951 width_from_buffer = surface->width_from_buffer;
2952 height_from_buffer = surface->height_from_buffer;
2953 }
2954
2955 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2956 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2957
2958 /* No buffer: viewport is irrelevant. */
2959 if (width_from_buffer == 0 || height_from_buffer == 0)
2960 return true;
2961
2962 /* overflow checks for wl_fixed_from_int() */
2963 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2964 return false;
2965 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2966 return false;
2967
2968 w = wl_fixed_from_int(width_from_buffer);
2969 h = wl_fixed_from_int(height_from_buffer);
2970
2971 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2972 return false;
2973 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2974 return false;
2975
2976 return true;
2977}
2978
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002979static bool
2980fixed_is_integer(wl_fixed_t v)
2981{
2982 return (v & 0xff) == 0;
2983}
2984
2985static bool
2986weston_surface_is_pending_viewport_dst_size_int(
2987 const struct weston_surface *surface)
2988{
2989 const struct weston_buffer_viewport *vp =
2990 &surface->pending.buffer_viewport;
2991
2992 if (vp->surface.width != -1) {
2993 assert(vp->surface.width > 0 && vp->surface.height > 0);
2994 return true;
2995 }
2996
2997 return fixed_is_integer(vp->buffer.src_width) &&
2998 fixed_is_integer(vp->buffer.src_height);
2999}
3000
Derek Foreman152254b2015-11-26 14:17:48 -06003001/* Translate pending damage in buffer co-ordinates to surface
3002 * co-ordinates and union it with a pixman_region32_t.
3003 * This should only be called after the buffer is attached.
3004 */
3005static void
3006apply_damage_buffer(pixman_region32_t *dest,
3007 struct weston_surface *surface,
3008 struct weston_surface_state *state)
3009{
3010 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3011
3012 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3013 * translated into surface co-ordinates and unioned with
3014 * any other surface damage.
3015 * None of this makes sense if there is no buffer though.
3016 */
3017 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3018 pixman_region32_t buffer_damage;
3019
3020 pixman_region32_intersect_rect(&state->damage_buffer,
3021 &state->damage_buffer,
3022 0, 0, buffer->width,
3023 buffer->height);
3024 pixman_region32_init(&buffer_damage);
3025 weston_matrix_transform_region(&buffer_damage,
3026 &surface->buffer_to_surface_matrix,
3027 &state->damage_buffer);
3028 pixman_region32_union(dest, dest, &buffer_damage);
3029 pixman_region32_fini(&buffer_damage);
3030 }
3031 /* We should clear this on commit even if there was no buffer */
3032 pixman_region32_clear(&state->damage_buffer);
3033}
3034
Jason Ekstrand1e059042014-10-16 10:55:19 -05003035static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003036weston_surface_commit_state(struct weston_surface *surface,
3037 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003038{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003039 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003040 pixman_region32_t opaque;
3041
Alexander Larsson4ea95522013-05-22 14:41:37 +02003042 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003043 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003044 /* wp_viewport.set_source */
3045 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003046 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003047
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003048 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003049 if (state->newly_attached)
3050 weston_surface_attach(surface, state->buffer);
3051 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003052
Jason Ekstrand1e059042014-10-16 10:55:19 -05003053 weston_surface_build_buffer_matrix(surface,
3054 &surface->surface_to_buffer_matrix);
3055 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3056 &surface->surface_to_buffer_matrix);
3057
Jason Ekstrand7b982072014-05-20 14:33:03 -05003058 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003059 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003060 if (surface->committed)
3061 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003062 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003063
Jason Ekstrand7b982072014-05-20 14:33:03 -05003064 state->sx = 0;
3065 state->sy = 0;
3066 state->newly_attached = 0;
3067 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003068
Derek Foreman152254b2015-11-26 14:17:48 -06003069 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003070 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003071 (pixman_region32_not_empty(&state->damage_surface) ||
3072 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003073 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003074
Pekka Paalanen8e159182012-10-10 12:49:25 +03003075 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003076 &state->damage_surface);
3077
3078 apply_damage_buffer(&surface->damage, surface, state);
3079
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003080 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003081 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003082 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003083
3084 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003085 pixman_region32_init(&opaque);
3086 pixman_region32_intersect_rect(&opaque, &state->opaque,
3087 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003088
3089 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3090 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003091 wl_list_for_each(view, &surface->views, surface_link)
3092 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003093 }
3094
3095 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003096
3097 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003098 pixman_region32_intersect_rect(&surface->input, &state->input,
3099 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003100
Pekka Paalanenbc106382012-10-10 12:49:31 +03003101 /* wl_surface.frame */
3102 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003103 &state->frame_callback_list);
3104 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003105
3106 /* XXX:
3107 * What should happen with a feedback request, if there
3108 * is no wl_buffer attached for this commit?
3109 */
3110
3111 /* presentation.feedback */
3112 wl_list_insert_list(&surface->feedback_list,
3113 &state->feedback_list);
3114 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003115
3116 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003117}
3118
3119static void
3120weston_surface_commit(struct weston_surface *surface)
3121{
3122 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003123
Pekka Paalanene67858b2013-04-25 13:57:42 +03003124 weston_surface_commit_subsurface_order(surface);
3125
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003126 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003127}
3128
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003129static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003130weston_subsurface_commit(struct weston_subsurface *sub);
3131
3132static void
3133weston_subsurface_parent_commit(struct weston_subsurface *sub,
3134 int parent_is_synchronized);
3135
3136static void
3137surface_commit(struct wl_client *client, struct wl_resource *resource)
3138{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003139 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003140 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3141
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003142 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3143 assert(surface->viewport_resource);
3144
3145 wl_resource_post_error(surface->viewport_resource,
3146 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3147 "wl_surface@%d has viewport source outside buffer",
3148 wl_resource_get_id(resource));
3149 return;
3150 }
3151
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003152 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3153 assert(surface->viewport_resource);
3154
3155 wl_resource_post_error(surface->viewport_resource,
3156 WP_VIEWPORT_ERROR_BAD_SIZE,
3157 "wl_surface@%d viewport dst size not integer",
3158 wl_resource_get_id(resource));
3159 return;
3160 }
3161
Pekka Paalanene67858b2013-04-25 13:57:42 +03003162 if (sub) {
3163 weston_subsurface_commit(sub);
3164 return;
3165 }
3166
3167 weston_surface_commit(surface);
3168
3169 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3170 if (sub->surface != surface)
3171 weston_subsurface_parent_commit(sub, 0);
3172 }
3173}
3174
3175static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003176surface_set_buffer_transform(struct wl_client *client,
3177 struct wl_resource *resource, int transform)
3178{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003179 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003180
Jonny Lamba55f1392014-05-30 12:07:15 +02003181 /* if wl_output.transform grows more members this will need to be updated. */
3182 if (transform < 0 ||
3183 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3184 wl_resource_post_error(resource,
3185 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3186 "buffer transform must be a valid transform "
3187 "('%d' specified)", transform);
3188 return;
3189 }
3190
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003191 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003192 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003193}
3194
Alexander Larsson4ea95522013-05-22 14:41:37 +02003195static void
3196surface_set_buffer_scale(struct wl_client *client,
3197 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003198 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003199{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003200 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003201
Jonny Lamba55f1392014-05-30 12:07:15 +02003202 if (scale < 1) {
3203 wl_resource_post_error(resource,
3204 WL_SURFACE_ERROR_INVALID_SCALE,
3205 "buffer scale must be at least one "
3206 "('%d' specified)", scale);
3207 return;
3208 }
3209
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003210 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003211 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003212}
3213
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003214static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003215 surface_destroy,
3216 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003217 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003218 surface_frame,
3219 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003220 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003221 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003222 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003223 surface_set_buffer_scale,
3224 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003225};
3226
3227static void
3228compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003229 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003230{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003231 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003232 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003233
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003234 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003235 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003236 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003237 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003238 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003239
Jason Ekstranda85118c2013-06-27 20:17:02 -05003240 surface->resource =
3241 wl_resource_create(client, &wl_surface_interface,
3242 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003243 if (surface->resource == NULL) {
3244 weston_surface_destroy(surface);
3245 wl_resource_post_no_memory(resource);
3246 return;
3247 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003248 wl_resource_set_implementation(surface->resource, &surface_interface,
3249 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003250
3251 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003252}
3253
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003254static void
3255destroy_region(struct wl_resource *resource)
3256{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003257 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003258
3259 pixman_region32_fini(&region->region);
3260 free(region);
3261}
3262
3263static void
3264region_destroy(struct wl_client *client, struct wl_resource *resource)
3265{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003266 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003267}
3268
3269static void
3270region_add(struct wl_client *client, struct wl_resource *resource,
3271 int32_t x, int32_t y, int32_t width, int32_t height)
3272{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003273 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003274
3275 pixman_region32_union_rect(&region->region, &region->region,
3276 x, y, width, height);
3277}
3278
3279static void
3280region_subtract(struct wl_client *client, struct wl_resource *resource,
3281 int32_t x, int32_t y, int32_t width, int32_t height)
3282{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003283 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003284 pixman_region32_t rect;
3285
3286 pixman_region32_init_rect(&rect, x, y, width, height);
3287 pixman_region32_subtract(&region->region, &region->region, &rect);
3288 pixman_region32_fini(&rect);
3289}
3290
3291static const struct wl_region_interface region_interface = {
3292 region_destroy,
3293 region_add,
3294 region_subtract
3295};
3296
3297static void
3298compositor_create_region(struct wl_client *client,
3299 struct wl_resource *resource, uint32_t id)
3300{
3301 struct weston_region *region;
3302
3303 region = malloc(sizeof *region);
3304 if (region == NULL) {
3305 wl_resource_post_no_memory(resource);
3306 return;
3307 }
3308
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003309 pixman_region32_init(&region->region);
3310
Jason Ekstranda85118c2013-06-27 20:17:02 -05003311 region->resource =
3312 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003313 if (region->resource == NULL) {
3314 free(region);
3315 wl_resource_post_no_memory(resource);
3316 return;
3317 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003318 wl_resource_set_implementation(region->resource, &region_interface,
3319 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003320}
3321
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003322static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003323 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003324 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003325};
3326
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003327static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003328weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3329{
3330 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003331
Jason Ekstrand7b982072014-05-20 14:33:03 -05003332 weston_surface_commit_state(surface, &sub->cached);
3333 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003334
3335 weston_surface_commit_subsurface_order(surface);
3336
3337 weston_surface_schedule_repaint(surface);
3338
Jason Ekstrand7b982072014-05-20 14:33:03 -05003339 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003340}
3341
3342static void
3343weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3344{
3345 struct weston_surface *surface = sub->surface;
3346
3347 /*
3348 * If this commit would cause the surface to move by the
3349 * attach(dx, dy) parameters, the old damage region must be
3350 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003351 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003352 */
Derek Foreman152254b2015-11-26 14:17:48 -06003353 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003354 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003355 pixman_region32_union(&sub->cached.damage_surface,
3356 &sub->cached.damage_surface,
3357 &surface->pending.damage_surface);
3358 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003359
3360 if (surface->pending.newly_attached) {
3361 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003362 weston_surface_state_set_buffer(&sub->cached,
3363 surface->pending.buffer);
3364 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003365 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003366 weston_presentation_feedback_discard_list(
3367 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003368 }
3369 sub->cached.sx += surface->pending.sx;
3370 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003371
Derek Foreman152254b2015-11-26 14:17:48 -06003372 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3373
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003374 sub->cached.buffer_viewport.changed |=
3375 surface->pending.buffer_viewport.changed;
3376 sub->cached.buffer_viewport.buffer =
3377 surface->pending.buffer_viewport.buffer;
3378 sub->cached.buffer_viewport.surface =
3379 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003380
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003381 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003382
3383 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3384
3385 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3386
3387 wl_list_insert_list(&sub->cached.frame_callback_list,
3388 &surface->pending.frame_callback_list);
3389 wl_list_init(&surface->pending.frame_callback_list);
3390
Pekka Paalanen133e4392014-09-23 22:08:46 -04003391 wl_list_insert_list(&sub->cached.feedback_list,
3392 &surface->pending.feedback_list);
3393 wl_list_init(&surface->pending.feedback_list);
3394
Jason Ekstrand7b982072014-05-20 14:33:03 -05003395 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003396}
3397
Derek Foreman280e7dd2014-10-03 13:13:42 -05003398static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003399weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3400{
3401 while (sub) {
3402 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003403 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003404
3405 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003406 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003407
3408 sub = weston_surface_to_subsurface(sub->parent);
3409 }
3410
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003411 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003412}
3413
3414static void
3415weston_subsurface_commit(struct weston_subsurface *sub)
3416{
3417 struct weston_surface *surface = sub->surface;
3418 struct weston_subsurface *tmp;
3419
3420 /* Recursive check for effectively synchronized. */
3421 if (weston_subsurface_is_synchronized(sub)) {
3422 weston_subsurface_commit_to_cache(sub);
3423 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003424 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003425 /* flush accumulated state from cache */
3426 weston_subsurface_commit_to_cache(sub);
3427 weston_subsurface_commit_from_cache(sub);
3428 } else {
3429 weston_surface_commit(surface);
3430 }
3431
3432 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3433 if (tmp->surface != surface)
3434 weston_subsurface_parent_commit(tmp, 0);
3435 }
3436 }
3437}
3438
3439static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003440weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003441{
3442 struct weston_surface *surface = sub->surface;
3443 struct weston_subsurface *tmp;
3444
Pekka Paalanene67858b2013-04-25 13:57:42 +03003445 /* From now on, commit_from_cache the whole sub-tree, regardless of
3446 * the synchronized mode of each child. This sub-surface or some
3447 * of its ancestors were synchronized, so we are synchronized
3448 * all the way down.
3449 */
3450
Jason Ekstrand7b982072014-05-20 14:33:03 -05003451 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003452 weston_subsurface_commit_from_cache(sub);
3453
3454 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3455 if (tmp->surface != surface)
3456 weston_subsurface_parent_commit(tmp, 1);
3457 }
3458}
3459
3460static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003461weston_subsurface_parent_commit(struct weston_subsurface *sub,
3462 int parent_is_synchronized)
3463{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003464 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003465 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003466 wl_list_for_each(view, &sub->surface->views, surface_link)
3467 weston_view_set_position(view,
3468 sub->position.x,
3469 sub->position.y);
3470
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003471 sub->position.set = 0;
3472 }
3473
3474 if (parent_is_synchronized || sub->synchronized)
3475 weston_subsurface_synchronized_commit(sub);
3476}
3477
Pekka Paalanen8274d902014-08-06 19:36:51 +03003478static int
3479subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3480{
3481 return snprintf(buf, len, "sub-surface");
3482}
3483
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003484static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003485subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003486{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003487 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003488
Jason Ekstranda7af7042013-10-12 22:38:11 -05003489 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003490 weston_view_set_position(view,
3491 view->geometry.x + dx,
3492 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003493
3494 /* No need to check parent mappedness, because if parent is not
3495 * mapped, parent is not in a visible layer, so this sub-surface
3496 * will not be drawn either.
3497 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003498
Pekka Paalanene67858b2013-04-25 13:57:42 +03003499 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003500 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003501
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003502 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003503 * because that would call it also for the parent surface,
3504 * which might not be mapped yet. That would lead to
3505 * inconsistent state, where the window could never be
3506 * mapped.
3507 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003508 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003509 * weston_surface_is_mapped() return true, so that when the
3510 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003511 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003512 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003513 }
3514}
3515
3516static struct weston_subsurface *
3517weston_surface_to_subsurface(struct weston_surface *surface)
3518{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003519 if (surface->committed == subsurface_committed)
3520 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003521
3522 return NULL;
3523}
3524
Pekka Paalanen01388e22013-04-25 13:57:44 +03003525WL_EXPORT struct weston_surface *
3526weston_surface_get_main_surface(struct weston_surface *surface)
3527{
3528 struct weston_subsurface *sub;
3529
3530 while (surface && (sub = weston_surface_to_subsurface(surface)))
3531 surface = sub->parent;
3532
3533 return surface;
3534}
3535
Pekka Paalanen50b67472014-10-01 15:02:41 +03003536WL_EXPORT int
3537weston_surface_set_role(struct weston_surface *surface,
3538 const char *role_name,
3539 struct wl_resource *error_resource,
3540 uint32_t error_code)
3541{
3542 assert(role_name);
3543
3544 if (surface->role_name == NULL ||
3545 surface->role_name == role_name ||
3546 strcmp(surface->role_name, role_name) == 0) {
3547 surface->role_name = role_name;
3548
3549 return 0;
3550 }
3551
3552 wl_resource_post_error(error_resource, error_code,
3553 "Cannot assign role %s to wl_surface@%d,"
3554 " already has role %s\n",
3555 role_name,
3556 wl_resource_get_id(surface->resource),
3557 surface->role_name);
3558 return -1;
3559}
3560
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003561WL_EXPORT const char *
3562weston_surface_get_role(struct weston_surface *surface)
3563{
3564 return surface->role_name;
3565}
3566
Pekka Paalanen8274d902014-08-06 19:36:51 +03003567WL_EXPORT void
3568weston_surface_set_label_func(struct weston_surface *surface,
3569 int (*desc)(struct weston_surface *,
3570 char *, size_t))
3571{
3572 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003573 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003574}
3575
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003576/** Get the size of surface contents
3577 *
3578 * \param surface The surface to query.
3579 * \param width Returns the width of raw contents.
3580 * \param height Returns the height of raw contents.
3581 *
3582 * Retrieves the raw surface content size in pixels for the given surface.
3583 * This is the whole content size in buffer pixels. If the surface
3584 * has no content or the renderer does not implement this feature,
3585 * zeroes are returned.
3586 *
3587 * This function is used to determine the buffer size needed for
3588 * a weston_surface_copy_content() call.
3589 */
3590WL_EXPORT void
3591weston_surface_get_content_size(struct weston_surface *surface,
3592 int *width, int *height)
3593{
3594 struct weston_renderer *rer = surface->compositor->renderer;
3595
3596 if (!rer->surface_get_content_size) {
3597 *width = 0;
3598 *height = 0;
3599 return;
3600 }
3601
3602 rer->surface_get_content_size(surface, width, height);
3603}
3604
Quentin Glidic248dd102016-08-12 10:41:34 +02003605/** Get the bounding box of a surface and its subsurfaces
3606 *
3607 * \param surface The surface to query.
3608 * \return The bounding box relative to the surface origin.
3609 *
3610 */
3611WL_EXPORT struct weston_geometry
3612weston_surface_get_bounding_box(struct weston_surface *surface)
3613{
3614 pixman_region32_t region;
3615 pixman_box32_t *box;
3616 struct weston_subsurface *subsurface;
3617
3618 pixman_region32_init_rect(&region,
3619 0, 0,
3620 surface->width, surface->height);
3621
3622 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3623 pixman_region32_union_rect(&region, &region,
3624 subsurface->position.x,
3625 subsurface->position.y,
3626 subsurface->surface->width,
3627 subsurface->surface->height);
3628
3629 box = pixman_region32_extents(&region);
3630 struct weston_geometry geometry = {
3631 .x = box->x1,
3632 .y = box->y1,
3633 .width = box->x2 - box->x1,
3634 .height = box->y2 - box->y1,
3635 };
3636
3637 pixman_region32_fini(&region);
3638
3639 return geometry;
3640}
3641
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003642/** Copy surface contents to system memory.
3643 *
3644 * \param surface The surface to copy from.
3645 * \param target Pointer to the target memory buffer.
3646 * \param size Size of the target buffer in bytes.
3647 * \param src_x X location on contents to copy from.
3648 * \param src_y Y location on contents to copy from.
3649 * \param width Width in pixels of the area to copy.
3650 * \param height Height in pixels of the area to copy.
3651 * \return 0 for success, -1 for failure.
3652 *
3653 * Surface contents are maintained by the renderer. They can be in a
3654 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3655 * else.
3656 *
3657 * Surface contents are copied into memory pointed to by target,
3658 * which has size bytes of space available. The target memory
3659 * may be larger than needed, but being smaller returns an error.
3660 * The extra bytes in target may or may not be written; their content is
3661 * unspecified. Size must be large enough to hold the image.
3662 *
3663 * The image in the target memory will be arranged in rows from
3664 * top to bottom, and pixels on a row from left to right. The pixel
3665 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3666 * width * 4.
3667 *
3668 * Parameters src_x and src_y define the upper-left corner in buffer
3669 * coordinates (pixels) to copy from. Parameters width and height
3670 * define the size of the area to copy in pixels.
3671 *
3672 * The rectangle defined by src_x, src_y, width, height must fit in
3673 * the surface contents. Otherwise an error is returned.
3674 *
3675 * Use surface_get_data_size to determine the content size; the
3676 * needed target buffer size and rectangle limits.
3677 *
3678 * CURRENT IMPLEMENTATION RESTRICTIONS:
3679 * - the machine must be little-endian due to Pixman formats.
3680 *
3681 * NOTE: Pixman formats are premultiplied.
3682 */
3683WL_EXPORT int
3684weston_surface_copy_content(struct weston_surface *surface,
3685 void *target, size_t size,
3686 int src_x, int src_y,
3687 int width, int height)
3688{
3689 struct weston_renderer *rer = surface->compositor->renderer;
3690 int cw, ch;
3691 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3692
3693 if (!rer->surface_copy_content)
3694 return -1;
3695
3696 weston_surface_get_content_size(surface, &cw, &ch);
3697
3698 if (src_x < 0 || src_y < 0)
3699 return -1;
3700
3701 if (width <= 0 || height <= 0)
3702 return -1;
3703
3704 if (src_x + width > cw || src_y + height > ch)
3705 return -1;
3706
3707 if (width * bytespp * height > size)
3708 return -1;
3709
3710 return rer->surface_copy_content(surface, target, size,
3711 src_x, src_y, width, height);
3712}
3713
Pekka Paalanene67858b2013-04-25 13:57:42 +03003714static void
3715subsurface_set_position(struct wl_client *client,
3716 struct wl_resource *resource, int32_t x, int32_t y)
3717{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003718 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003719
3720 if (!sub)
3721 return;
3722
3723 sub->position.x = x;
3724 sub->position.y = y;
3725 sub->position.set = 1;
3726}
3727
3728static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003729subsurface_find_sibling(struct weston_subsurface *sub,
3730 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003731{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003732 struct weston_surface *parent = sub->parent;
3733 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003734
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003735 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3736 if (sibling->surface == surface && sibling != sub)
3737 return sibling;
3738 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003739
3740 return NULL;
3741}
3742
3743static struct weston_subsurface *
3744subsurface_sibling_check(struct weston_subsurface *sub,
3745 struct weston_surface *surface,
3746 const char *request)
3747{
3748 struct weston_subsurface *sibling;
3749
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003750 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003751 if (!sibling) {
3752 wl_resource_post_error(sub->resource,
3753 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3754 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003755 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003756 return NULL;
3757 }
3758
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003759 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003760
3761 return sibling;
3762}
3763
3764static void
3765subsurface_place_above(struct wl_client *client,
3766 struct wl_resource *resource,
3767 struct wl_resource *sibling_resource)
3768{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003769 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003770 struct weston_surface *surface =
3771 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003772 struct weston_subsurface *sibling;
3773
3774 if (!sub)
3775 return;
3776
3777 sibling = subsurface_sibling_check(sub, surface, "place_above");
3778 if (!sibling)
3779 return;
3780
3781 wl_list_remove(&sub->parent_link_pending);
3782 wl_list_insert(sibling->parent_link_pending.prev,
3783 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003784
3785 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003786}
3787
3788static void
3789subsurface_place_below(struct wl_client *client,
3790 struct wl_resource *resource,
3791 struct wl_resource *sibling_resource)
3792{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003793 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003794 struct weston_surface *surface =
3795 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003796 struct weston_subsurface *sibling;
3797
3798 if (!sub)
3799 return;
3800
3801 sibling = subsurface_sibling_check(sub, surface, "place_below");
3802 if (!sibling)
3803 return;
3804
3805 wl_list_remove(&sub->parent_link_pending);
3806 wl_list_insert(&sibling->parent_link_pending,
3807 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003808
3809 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003810}
3811
3812static void
3813subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3814{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003815 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003816
3817 if (sub)
3818 sub->synchronized = 1;
3819}
3820
3821static void
3822subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3823{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003824 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003825
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003826 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003827 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003828
3829 /* If sub became effectively desynchronized, flush. */
3830 if (!weston_subsurface_is_synchronized(sub))
3831 weston_subsurface_synchronized_commit(sub);
3832 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003833}
3834
3835static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003836weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3837{
3838 wl_list_remove(&sub->parent_link);
3839 wl_list_remove(&sub->parent_link_pending);
3840 wl_list_remove(&sub->parent_destroy_listener.link);
3841 sub->parent = NULL;
3842}
3843
3844static void
3845weston_subsurface_destroy(struct weston_subsurface *sub);
3846
3847static void
3848subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3849{
3850 struct weston_subsurface *sub =
3851 container_of(listener, struct weston_subsurface,
3852 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003853 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003854
3855 /* The protocol object (wl_resource) is left inert. */
3856 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003857 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003858
3859 weston_subsurface_destroy(sub);
3860}
3861
3862static void
3863subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3864{
3865 struct weston_subsurface *sub =
3866 container_of(listener, struct weston_subsurface,
3867 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003868 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003869 assert(sub->surface != sub->parent);
3870
3871 if (weston_surface_is_mapped(sub->surface))
3872 weston_surface_unmap(sub->surface);
3873
3874 weston_subsurface_unlink_parent(sub);
3875}
3876
3877static void
3878subsurface_resource_destroy(struct wl_resource *resource)
3879{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003880 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003881
3882 if (sub)
3883 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003884}
3885
3886static void
3887subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3888{
3889 wl_resource_destroy(resource);
3890}
3891
3892static void
3893weston_subsurface_link_parent(struct weston_subsurface *sub,
3894 struct weston_surface *parent)
3895{
3896 sub->parent = parent;
3897 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003898 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003899 &sub->parent_destroy_listener);
3900
3901 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3902 wl_list_insert(&parent->subsurface_list_pending,
3903 &sub->parent_link_pending);
3904}
3905
3906static void
3907weston_subsurface_link_surface(struct weston_subsurface *sub,
3908 struct weston_surface *surface)
3909{
3910 sub->surface = surface;
3911 sub->surface_destroy_listener.notify =
3912 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003913 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003914 &sub->surface_destroy_listener);
3915}
3916
3917static void
3918weston_subsurface_destroy(struct weston_subsurface *sub)
3919{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003920 struct weston_view *view, *next;
3921
Pekka Paalanene67858b2013-04-25 13:57:42 +03003922 assert(sub->surface);
3923
3924 if (sub->resource) {
3925 assert(weston_surface_to_subsurface(sub->surface) == sub);
3926 assert(sub->parent_destroy_listener.notify ==
3927 subsurface_handle_parent_destroy);
3928
George Kiagiadakised04d382014-06-13 18:10:26 +02003929 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3930 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003931 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003932 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003933
Pekka Paalanene67858b2013-04-25 13:57:42 +03003934 if (sub->parent)
3935 weston_subsurface_unlink_parent(sub);
3936
Jason Ekstrand7b982072014-05-20 14:33:03 -05003937 weston_surface_state_fini(&sub->cached);
3938 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003939
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003940 sub->surface->committed = NULL;
3941 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003942 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003943 } else {
3944 /* the dummy weston_subsurface for the parent itself */
3945 assert(sub->parent_destroy_listener.notify == NULL);
3946 wl_list_remove(&sub->parent_link);
3947 wl_list_remove(&sub->parent_link_pending);
3948 }
3949
3950 wl_list_remove(&sub->surface_destroy_listener.link);
3951 free(sub);
3952}
3953
3954static const struct wl_subsurface_interface subsurface_implementation = {
3955 subsurface_destroy,
3956 subsurface_set_position,
3957 subsurface_place_above,
3958 subsurface_place_below,
3959 subsurface_set_sync,
3960 subsurface_set_desync
3961};
3962
3963static struct weston_subsurface *
3964weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3965 struct weston_surface *parent)
3966{
3967 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003968 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003969
Bryce Harringtonde16d892014-11-20 22:21:57 -08003970 sub = zalloc(sizeof *sub);
3971 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003972 return NULL;
3973
Jason Ekstranda7af7042013-10-12 22:38:11 -05003974 wl_list_init(&sub->unused_views);
3975
Jason Ekstranda85118c2013-06-27 20:17:02 -05003976 sub->resource =
3977 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003978 if (!sub->resource) {
3979 free(sub);
3980 return NULL;
3981 }
3982
Jason Ekstranda85118c2013-06-27 20:17:02 -05003983 wl_resource_set_implementation(sub->resource,
3984 &subsurface_implementation,
3985 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003986 weston_subsurface_link_surface(sub, surface);
3987 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003988 weston_surface_state_init(&sub->cached);
3989 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003990 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003991
3992 return sub;
3993}
3994
3995/* Create a dummy subsurface for having the parent itself in its
3996 * sub-surface lists. Makes stacking order manipulation easy.
3997 */
3998static struct weston_subsurface *
3999weston_subsurface_create_for_parent(struct weston_surface *parent)
4000{
4001 struct weston_subsurface *sub;
4002
Bryce Harringtonde16d892014-11-20 22:21:57 -08004003 sub = zalloc(sizeof *sub);
4004 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004005 return NULL;
4006
4007 weston_subsurface_link_surface(sub, parent);
4008 sub->parent = parent;
4009 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4010 wl_list_insert(&parent->subsurface_list_pending,
4011 &sub->parent_link_pending);
4012
4013 return sub;
4014}
4015
4016static void
4017subcompositor_get_subsurface(struct wl_client *client,
4018 struct wl_resource *resource,
4019 uint32_t id,
4020 struct wl_resource *surface_resource,
4021 struct wl_resource *parent_resource)
4022{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004023 struct weston_surface *surface =
4024 wl_resource_get_user_data(surface_resource);
4025 struct weston_surface *parent =
4026 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004027 struct weston_subsurface *sub;
4028 static const char where[] = "get_subsurface: wl_subsurface@";
4029
4030 if (surface == parent) {
4031 wl_resource_post_error(resource,
4032 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4033 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004034 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004035 return;
4036 }
4037
4038 if (weston_surface_to_subsurface(surface)) {
4039 wl_resource_post_error(resource,
4040 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4041 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004042 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004043 return;
4044 }
4045
Pekka Paalanen50b67472014-10-01 15:02:41 +03004046 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4047 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004048 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004049
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004050 if (weston_surface_get_main_surface(parent) == surface) {
4051 wl_resource_post_error(resource,
4052 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4053 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004054 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004055 return;
4056 }
4057
Pekka Paalanene67858b2013-04-25 13:57:42 +03004058 /* make sure the parent is in its own list */
4059 if (wl_list_empty(&parent->subsurface_list)) {
4060 if (!weston_subsurface_create_for_parent(parent)) {
4061 wl_resource_post_no_memory(resource);
4062 return;
4063 }
4064 }
4065
4066 sub = weston_subsurface_create(id, surface, parent);
4067 if (!sub) {
4068 wl_resource_post_no_memory(resource);
4069 return;
4070 }
4071
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004072 surface->committed = subsurface_committed;
4073 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004074 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004075}
4076
4077static void
4078subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4079{
4080 wl_resource_destroy(resource);
4081}
4082
4083static const struct wl_subcompositor_interface subcompositor_interface = {
4084 subcompositor_destroy,
4085 subcompositor_get_subsurface
4086};
4087
4088static void
4089bind_subcompositor(struct wl_client *client,
4090 void *data, uint32_t version, uint32_t id)
4091{
4092 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004093 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004094
Jason Ekstranda85118c2013-06-27 20:17:02 -05004095 resource =
4096 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004097 if (resource == NULL) {
4098 wl_client_post_no_memory(client);
4099 return;
4100 }
4101 wl_resource_set_implementation(resource, &subcompositor_interface,
4102 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004103}
4104
Bryce Harrington0795ece2016-08-30 12:04:26 -07004105/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004106 *
4107 * \param compositor The compositor instance
4108 * \param state The DPMS state the outputs will be set to
4109 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004110static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004111weston_compositor_dpms(struct weston_compositor *compositor,
4112 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004113{
4114 struct weston_output *output;
4115
Bryce Harrington08976ac2016-08-30 12:05:16 -07004116 wl_list_for_each(output, &compositor->output_list, link)
4117 if (output->set_dpms)
4118 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004119}
4120
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004121/** Restores the compositor to active status
4122 *
4123 * \param compositor The compositor instance
4124 *
4125 * If the compositor was in a sleeping mode, all outputs are powered
4126 * back on via DPMS. Otherwise if the compositor was inactive
4127 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4128 * signal will fire.
4129 *
4130 * Restarts the idle timer.
4131 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004132WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004133weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004134{
Neil Roberts8b62e202013-09-30 13:14:47 +01004135 uint32_t old_state = compositor->state;
4136
4137 /* The state needs to be changed before emitting the wake
4138 * signal because that may try to schedule a repaint which
4139 * will not work if the compositor is still sleeping */
4140 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4141
4142 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004143 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004144 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004145 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004146 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004147 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004148 /* fall through */
4149 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004150 wl_event_source_timer_update(compositor->idle_source,
4151 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004152 }
4153}
4154
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004155/** Turns off rendering and frame events for the compositor.
4156 *
4157 * \param compositor The compositor instance
4158 *
4159 * This is used for example to prevent further rendering while the
4160 * compositor is shutting down.
4161 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004162 * Stops the idle timer.
4163 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004164WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004165weston_compositor_offscreen(struct weston_compositor *compositor)
4166{
4167 switch (compositor->state) {
4168 case WESTON_COMPOSITOR_OFFSCREEN:
4169 return;
4170 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004171 default:
4172 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4173 wl_event_source_timer_update(compositor->idle_source, 0);
4174 }
4175}
4176
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004177/** Powers down all attached output devices
4178 *
4179 * \param compositor The compositor instance
4180 *
4181 * Causes rendering to the outputs to cease, and no frame events to be
4182 * sent. Only powers down the outputs if the compositor is not already
4183 * in sleep mode.
4184 *
4185 * Stops the idle timer.
4186 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004187WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004188weston_compositor_sleep(struct weston_compositor *compositor)
4189{
4190 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4191 return;
4192
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004193 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004194 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4195 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4196}
4197
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004198/** Sets compositor to idle mode
4199 *
4200 * \param data The compositor instance
4201 *
4202 * This is called when the idle timer fires. Once the compositor is in
4203 * idle mode it requires a wake action (e.g. via
4204 * weston_compositor_wake()) to restore it. The compositor's
4205 * idle_signal will be triggered when the idle event occurs.
4206 *
4207 * Idleness can be inhibited by setting the compositor's idle_inhibit
4208 * property.
4209 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004210static int
4211idle_handler(void *data)
4212{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004213 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004214
4215 if (compositor->idle_inhibit)
4216 return 1;
4217
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004218 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004219 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004220
4221 return 1;
4222}
4223
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004224WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004225weston_plane_init(struct weston_plane *plane,
4226 struct weston_compositor *ec,
4227 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004228{
4229 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004230 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004231 plane->x = x;
4232 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004233 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004234
4235 /* Init the link so that the call to wl_list_remove() when releasing
4236 * the plane without ever stacking doesn't lead to a crash */
4237 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004238}
4239
4240WL_EXPORT void
4241weston_plane_release(struct weston_plane *plane)
4242{
Xiong Zhang97116532013-10-23 13:58:31 +08004243 struct weston_view *view;
4244
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004245 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004246 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004247
Xiong Zhang97116532013-10-23 13:58:31 +08004248 wl_list_for_each(view, &plane->compositor->view_list, link) {
4249 if (view->plane == plane)
4250 view->plane = NULL;
4251 }
4252
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004253 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004254}
4255
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004256WL_EXPORT void
4257weston_compositor_stack_plane(struct weston_compositor *ec,
4258 struct weston_plane *plane,
4259 struct weston_plane *above)
4260{
4261 if (above)
4262 wl_list_insert(above->link.prev, &plane->link);
4263 else
4264 wl_list_insert(&ec->plane_list, &plane->link);
4265}
4266
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004267static void
4268output_release(struct wl_client *client, struct wl_resource *resource)
4269{
4270 wl_resource_destroy(resource);
4271}
4272
4273static const struct wl_output_interface output_interface = {
4274 output_release,
4275};
4276
4277
Casey Dahlin9074db52012-04-19 22:50:09 -04004278static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004279{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004280 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004281}
4282
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004283static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004284bind_output(struct wl_client *client,
4285 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004286{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004287 struct weston_output *output = data;
4288 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004289 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004290
Jason Ekstranda85118c2013-06-27 20:17:02 -05004291 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004292 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004293 if (resource == NULL) {
4294 wl_client_post_no_memory(client);
4295 return;
4296 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004297
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004298 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004299 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004300
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004301 wl_output_send_geometry(resource,
4302 output->x,
4303 output->y,
4304 output->mm_width,
4305 output->mm_height,
4306 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004307 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004308 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004309 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004310 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004311 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004312
4313 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004314 wl_output_send_mode(resource,
4315 mode->flags,
4316 mode->width,
4317 mode->height,
4318 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004319 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004320
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004321 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004322 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004323}
4324
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004325/** Get the backing object of wl_output
4326 *
4327 * \param resource A wl_output protocol object.
4328 * \return The backing object (user data) of a wl_resource representing a
4329 * wl_output protocol object.
4330 */
4331WL_EXPORT struct weston_output *
4332weston_output_from_resource(struct wl_resource *resource)
4333{
4334 assert(wl_resource_instance_of(resource, &wl_output_interface,
4335 &output_interface));
4336
4337 return wl_resource_get_user_data(resource);
4338}
4339
4340
David Fort0de859e2016-05-27 23:22:57 +02004341/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004342static void
David Fort0de859e2016-05-27 23:22:57 +02004343weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4344 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004345{
4346 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004347 bool start_resizing = false;
4348
4349 if (!delta_width)
4350 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004351
4352 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004353 if (output == resized_output) {
4354 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004355 continue;
4356 }
4357
David Fort0de859e2016-05-27 23:22:57 +02004358 if (start_resizing) {
4359 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004360 output->dirty = 1;
4361 }
4362 }
4363}
4364
Pekka Paalanend72bad22017-03-29 17:01:41 +03004365static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004366weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004367{
Scott Moreau850ca422012-05-21 15:21:25 -06004368 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004369
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004370 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004371 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004372
Scott Moreauccbf29d2012-02-22 14:21:41 -07004373 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004374 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004375 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004376 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004377 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004378 weston_matrix_scale(&output->matrix, magnification,
4379 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004380 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004381
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004382 switch (output->transform) {
4383 case WL_OUTPUT_TRANSFORM_FLIPPED:
4384 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4385 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4386 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4387 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4388 weston_matrix_scale(&output->matrix, -1, 1, 1);
4389 break;
4390 }
4391
4392 switch (output->transform) {
4393 default:
4394 case WL_OUTPUT_TRANSFORM_NORMAL:
4395 case WL_OUTPUT_TRANSFORM_FLIPPED:
4396 break;
4397 case WL_OUTPUT_TRANSFORM_90:
4398 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4399 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4400 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4401 break;
4402 case WL_OUTPUT_TRANSFORM_180:
4403 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4404 weston_matrix_translate(&output->matrix,
4405 -output->width, -output->height, 0);
4406 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4407 break;
4408 case WL_OUTPUT_TRANSFORM_270:
4409 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4410 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4411 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4412 break;
4413 }
4414
4415 if (output->current_scale != 1)
4416 weston_matrix_scale(&output->matrix,
4417 output->current_scale,
4418 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004419
Scott Moreauccbf29d2012-02-22 14:21:41 -07004420 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004421
4422 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004423}
4424
Scott Moreau1bad5db2012-08-18 01:04:05 -06004425static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004426weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004427{
4428 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004429 output->native_scale = scale;
4430 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004431
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004432 convert_size_by_transform_scale(&output->width, &output->height,
4433 output->current_mode->width,
4434 output->current_mode->height,
4435 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004436}
4437
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004438static void
4439weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004440{
4441 output->x = x;
4442 output->y = y;
4443
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004444 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004445 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004446
4447 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004448 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004449 output->width,
4450 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004451}
4452
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004453WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004454weston_output_move(struct weston_output *output, int x, int y)
4455{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004456 struct wl_resource *resource;
4457
4458 output->move_x = x - output->x;
4459 output->move_y = y - output->y;
4460
4461 if (output->move_x == 0 && output->move_y == 0)
4462 return;
4463
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004464 weston_output_init_geometry(output, x, y);
4465
4466 output->dirty = 1;
4467
4468 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004469 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004470
4471 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004472 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004473 wl_output_send_geometry(resource,
4474 output->x,
4475 output->y,
4476 output->mm_width,
4477 output->mm_height,
4478 output->subpixel,
4479 output->make,
4480 output->model,
4481 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004482
FORT David8a120692016-04-26 23:34:06 +02004483 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004484 wl_output_send_done(resource);
4485 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004486}
4487
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004488/** Signal that a pending output is taken into use.
4489 *
4490 * Removes the output from the pending list and adds it to the compositor's
4491 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03004492 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004493 * The output gets an internal ID assigned, and the wl_output global is
4494 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004495 *
Giulio Camuffob1147152015-05-06 21:41:57 +03004496 * \param compositor The compositor instance.
4497 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004498 *
4499 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03004500 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03004501static void
Giulio Camuffob1147152015-05-06 21:41:57 +03004502weston_compositor_add_output(struct weston_compositor *compositor,
4503 struct weston_output *output)
4504{
Armin Krezoviće5403842016-08-05 15:28:29 +02004505 struct weston_view *view, *next;
4506
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004507 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004508
4509 /* Verify we haven't reached the limit of 32 available output IDs */
4510 assert(ffs(~compositor->output_id_pool) > 0);
4511
4512 /* Invert the output id pool and look for the lowest numbered
4513 * switch (the least significant bit). Take that bit's position
4514 * as our ID, and mark it used in the compositor's output_id_pool.
4515 */
4516 output->id = ffs(~compositor->output_id_pool) - 1;
4517 compositor->output_id_pool |= 1u << output->id;
4518
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004519 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03004520 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004521 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004522
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004523 output->global = wl_global_create(compositor->wl_display,
4524 &wl_output_interface, 3,
4525 output, bind_output);
4526
Giulio Camuffob1147152015-05-06 21:41:57 +03004527 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02004528
4529 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4530 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004531}
4532
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004533/** Transform device coordinates into global coordinates
4534 *
4535 * \param device_x[in] X coordinate in device units.
4536 * \param device_y[in] Y coordinate in device units.
4537 * \param x[out] X coordinate in the global space.
4538 * \param y[out] Y coordinate in the global space.
4539 *
4540 * Transforms coordinates from the device coordinate space
4541 * (physical pixel units) to the global coordinate space (logical pixel units).
4542 * This takes into account output transform and scale.
4543 *
4544 * \memberof weston_output
4545 * \internal
4546 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004547WL_EXPORT void
4548weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004549 double device_x, double device_y,
4550 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004551{
Derek Foreman0f679412014-10-02 13:41:17 -05004552 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004553 device_x,
4554 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004555 0.0,
4556 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004557
Derek Foreman67a18b92015-03-24 11:36:14 -05004558 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004559
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004560 *x = p.f[0] / p.f[3];
4561 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004562}
4563
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004564/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004565 *
4566 * \param output The weston_output object that is being removed.
4567 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004568 * The following happens:
4569 *
4570 * - The output assignments of all views in the current scenegraph are
4571 * recomputed.
4572 *
4573 * - Presentation feedback is discarded.
4574 *
4575 * - Compositor is notified that outputs were changed and
4576 * applies the necessary changes to re-layout outputs.
4577 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004578 * - The output is put back in the pending outputs list.
4579 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004580 * - Signal is emitted to notify all users of the weston_output
4581 * object that the output is being destroyed.
4582 *
4583 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004584 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004585 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004586 * - The output's internal ID is released.
4587 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004588 * \memberof weston_output
4589 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004590 */
4591static void
4592weston_compositor_remove_output(struct weston_output *output)
4593{
Pekka Paalanenbccda712017-03-29 16:16:04 +03004594 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004595 struct wl_resource *resource;
4596 struct weston_view *view;
4597
4598 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004599 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004600
Pekka Paalanenbccda712017-03-29 16:16:04 +03004601 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004602 if (view->output_mask & (1u << output->id))
4603 weston_view_assign_output(view);
4604 }
4605
4606 weston_presentation_feedback_discard_list(&output->feedback_list);
4607
Pekka Paalanenbccda712017-03-29 16:16:04 +03004608 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004609
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004610 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004611 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004612 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004613
Pekka Paalanenbccda712017-03-29 16:16:04 +03004614 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004615 wl_signal_emit(&output->destroy_signal, output);
4616
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004617 wl_global_destroy(output->global);
4618 output->global = NULL;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004619 wl_resource_for_each(resource, &output->resource_list) {
4620 wl_resource_set_destructor(resource, NULL);
4621 }
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004622
4623 compositor->output_id_pool &= ~(1u << output->id);
4624 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004625}
4626
4627/** Sets the output scale for a given output.
4628 *
4629 * \param output The weston_output object that the scale is set for.
4630 * \param scale Scale factor for the given output.
4631 *
4632 * It only supports setting scale for an output that
4633 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004634 *
4635 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004636 */
4637WL_EXPORT void
4638weston_output_set_scale(struct weston_output *output,
4639 int32_t scale)
4640{
4641 /* We can only set scale on a disabled output */
4642 assert(!output->enabled);
4643
4644 /* We only want to set scale once */
4645 assert(!output->scale);
4646
4647 output->scale = scale;
4648}
4649
4650/** Sets the output transform for a given output.
4651 *
4652 * \param output The weston_output object that the transform is set for.
4653 * \param transform Transform value for the given output.
4654 *
4655 * It only supports setting transform for an output that is
4656 * not enabled and it can only be ran once.
4657 *
4658 * Refer to wl_output::transform section located at
4659 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
4660 * for list of values that can be passed to this function.
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_transform(struct weston_output *output,
4666 uint32_t transform)
4667{
4668 /* We can only set transform on a disabled output */
4669 assert(!output->enabled);
4670
4671 /* We only want to set transform once */
4672 assert(output->transform == UINT32_MAX);
4673
4674 output->transform = transform;
4675}
4676
4677/** Initializes a weston_output object with enough data so
4678 ** an output can be configured.
4679 *
4680 * \param output The weston_output object to initialize
4681 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004682 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004683 *
4684 * Sets initial values for fields that are expected to be
4685 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004686 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004687 * The name is used in logs, and can be used by compositors as a configuration
4688 * identifier.
4689 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004690 * \memberof weston_output
4691 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004692 */
4693WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02004694weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004695 struct weston_compositor *compositor,
4696 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004697{
4698 output->compositor = compositor;
4699 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004700 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004701 wl_list_init(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004702 output->enabled = false;
4703
4704 /* Add some (in)sane defaults which can be used
4705 * for checking if an output was properly configured
4706 */
4707 output->mm_width = 0;
4708 output->mm_height = 0;
4709 output->scale = 0;
4710 /* Can't use -1 on uint32_t and 0 is valid enum value */
4711 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004712
4713 pixman_region32_init(&output->previous_damage);
4714 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03004715 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004716}
4717
4718/** Adds weston_output object to pending output list.
4719 *
4720 * \param output The weston_output object to add
4721 * \param compositor The compositor instance.
4722 *
4723 * Also notifies the compositor that an output is pending for
4724 * configuration.
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004725 *
4726 * The opposite of this operation is built into weston_output_destroy().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004727 *
4728 * \memberof weston_output
4729 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004730 */
4731WL_EXPORT void
4732weston_compositor_add_pending_output(struct weston_output *output,
4733 struct weston_compositor *compositor)
4734{
Pekka Paalanene952a012017-03-29 17:14:00 +03004735 assert(output->disable);
4736 assert(output->enable);
4737
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004738 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004739 wl_list_insert(compositor->pending_output_list.prev, &output->link);
4740 wl_signal_emit(&compositor->output_pending_signal, output);
4741}
4742
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004743/** Constructs a weston_output object that can be used by the compositor.
4744 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03004745 * \param output The weston_output object that needs to be enabled. Must not
4746 * be enabled already.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004747 *
4748 * Output coordinates are calculated and each new output is by default
4749 * assigned to the right of previous one.
4750 *
4751 * Sets up the transformation, zoom, and geometry of the output using
4752 * the properties that need to be configured by the compositor.
4753 *
4754 * Establishes a repaint timer for the output with the relevant display
4755 * object's event loop. See output_repaint_timer_handler().
4756 *
4757 * The output is assigned an ID. Weston can support up to 32 distinct
4758 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4759 * is referred to and used to find the first available ID number, and
4760 * then this ID is marked as used in output_id_pool.
4761 *
4762 * The output is also assigned a Wayland global with the wl_output
4763 * external interface.
4764 *
4765 * Backend specific function is called to set up the output output.
4766 *
4767 * Output is added to the compositor's output list
4768 *
4769 * If the backend specific function fails, the weston_output object
4770 * is returned to a state it was before calling this function and
4771 * is added to the compositor's pending_output_list in case it needs
4772 * to be reconfigured or just so it can be destroyed at shutdown.
4773 *
4774 * 0 is returned on success, -1 on failure.
4775 */
4776WL_EXPORT int
4777weston_output_enable(struct weston_output *output)
4778{
Armin Krezović782f5df2016-09-30 14:11:11 +02004779 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004780 struct weston_output *iterator;
4781 int x = 0, y = 0;
4782
Pekka Paalanencc201e42017-03-30 15:11:25 +03004783 if (output->enabled) {
4784 weston_log("Error: attempt to enable an enabled output '%s'\n",
4785 output->name);
4786 return -1;
4787 }
4788
Armin Krezović782f5df2016-09-30 14:11:11 +02004789 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004790 struct weston_output, link);
4791
Armin Krezović782f5df2016-09-30 14:11:11 +02004792 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004793 x = iterator->x + iterator->width;
4794
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004795 /* Make sure the scale is set up */
4796 assert(output->scale);
4797
4798 /* Make sure we have a transform set */
4799 assert(output->transform != UINT32_MAX);
4800
Armin Krezović782f5df2016-09-30 14:11:11 +02004801 output->x = x;
4802 output->y = y;
4803 output->dirty = 1;
4804 output->original_scale = output->scale;
4805
4806 weston_output_transform_scale_init(output, output->transform, output->scale);
4807 weston_output_init_zoom(output);
4808
4809 weston_output_init_geometry(output, x, y);
4810 weston_output_damage(output);
4811
4812 wl_signal_init(&output->frame_signal);
4813 wl_signal_init(&output->destroy_signal);
4814 wl_list_init(&output->animation_list);
4815 wl_list_init(&output->resource_list);
4816 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02004817
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004818 /* Enable the output (set up the crtc or create a
4819 * window representing the output, set up the
4820 * renderer, etc)
4821 */
4822 if (output->enable(output) < 0) {
4823 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004824 return -1;
4825 }
4826
4827 weston_compositor_add_output(output->compositor, output);
4828
4829 return 0;
4830}
4831
4832/** Converts a weston_output object to a pending output state, so it
4833 ** can be configured again or destroyed.
4834 *
4835 * \param output The weston_output object that needs to be disabled.
4836 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004837 * Calls a backend specific function to disable an output, in case
4838 * such function exists.
4839 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004840 * The backend specific disable function may choose to postpone the disabling
4841 * by returning a negative value, in which case this function returns early.
4842 * In that case the backend will guarantee the output will be disabled soon
4843 * by the backend calling this function again. One must not attempt to re-enable
4844 * the output until that happens.
4845 *
4846 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004847 * from weston's output_list (see weston_compositor_remove_output())
4848 * and is returned to a state it was before weston_output_enable()
4849 * was ran (see weston_output_enable_undo()).
4850 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004851 * See weston_output_init() for more information on the
4852 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03004853 *
4854 * If the output has never been enabled yet, this function can still be
4855 * called to ensure that the output is actually turned off rather than left
4856 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004857 */
4858WL_EXPORT void
4859weston_output_disable(struct weston_output *output)
4860{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004861 /* Should we rename this? */
4862 output->destroying = 1;
4863
Pekka Paalanenc65df642017-03-29 15:45:46 +03004864 /* Disable is called unconditionally also for not-enabled outputs,
4865 * because at compositor start-up, if there is an output that is
4866 * already on but the compositor wants to turn it off, we have to
4867 * forward the turn-off to the backend so it knows to do it.
4868 * The backend cannot initially turn off everything, because it
4869 * would cause unnecessary mode-sets for all outputs the compositor
4870 * wants to be on.
4871 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004872 if (output->disable(output) < 0)
4873 return;
4874
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004875 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004876 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004877
4878 output->destroying = 0;
4879}
4880
4881/** Emits a signal to indicate that there are outputs waiting to be configured.
4882 *
4883 * \param compositor The compositor instance
4884 */
4885WL_EXPORT void
4886weston_pending_output_coldplug(struct weston_compositor *compositor)
4887{
4888 struct weston_output *output, *next;
4889
4890 wl_list_for_each_safe(output, next, &compositor->pending_output_list, link)
4891 wl_signal_emit(&compositor->output_pending_signal, output);
4892}
4893
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004894/** Uninitialize an output
4895 *
4896 * Removes the output from the list of enabled outputs if necessary, but
4897 * does not call the backend's output disable function. The output will no
4898 * longer be in the list of pending outputs either.
4899 *
4900 * All fields of weston_output become uninitialized, i.e. should not be used
4901 * anymore. The caller can free the memory after this.
4902 *
4903 * \memberof weston_output
4904 * \internal
4905 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004906WL_EXPORT void
4907weston_output_destroy(struct weston_output *output)
4908{
4909 output->destroying = 1;
4910
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004911 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004912 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004913
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004914 pixman_region32_fini(&output->region);
4915 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004916 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004917 free(output->name);
4918}
4919
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004920static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004921destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004922{
Jonny Lamb74130762013-11-26 18:19:46 +01004923 struct weston_surface *surface =
4924 wl_resource_get_user_data(resource);
4925
Pekka Paalanen4826f872016-04-22 14:14:38 +03004926 if (!surface)
4927 return;
4928
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004929 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004930 surface->pending.buffer_viewport.buffer.src_width =
4931 wl_fixed_from_int(-1);
4932 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004933 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004934}
4935
4936static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004937viewport_destroy(struct wl_client *client,
4938 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004939{
4940 wl_resource_destroy(resource);
4941}
4942
4943static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004944viewport_set_source(struct wl_client *client,
4945 struct wl_resource *resource,
4946 wl_fixed_t src_x,
4947 wl_fixed_t src_y,
4948 wl_fixed_t src_width,
4949 wl_fixed_t src_height)
4950{
4951 struct weston_surface *surface =
4952 wl_resource_get_user_data(resource);
4953
Pekka Paalanen4826f872016-04-22 14:14:38 +03004954 if (!surface) {
4955 wl_resource_post_error(resource,
4956 WP_VIEWPORT_ERROR_NO_SURFACE,
4957 "wl_surface for this viewport is no longer exists");
4958 return;
4959 }
4960
4961 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004962 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004963
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004964 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004965 src_height == wl_fixed_from_int(-1) &&
4966 src_x == wl_fixed_from_int(-1) &&
4967 src_y == wl_fixed_from_int(-1)) {
4968 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004969 surface->pending.buffer_viewport.buffer.src_width =
4970 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004971 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004972 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004973 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004974
Pekka Paalanen201769a2016-04-26 14:42:11 +03004975 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004976 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004977 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03004978 "wl_surface@%d viewport source "
4979 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4980 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004981 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03004982 wl_fixed_to_double(src_height),
4983 wl_fixed_to_double(src_x),
4984 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004985 return;
4986 }
4987
4988 surface->pending.buffer_viewport.buffer.src_x = src_x;
4989 surface->pending.buffer_viewport.buffer.src_y = src_y;
4990 surface->pending.buffer_viewport.buffer.src_width = src_width;
4991 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004992 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004993}
4994
4995static void
4996viewport_set_destination(struct wl_client *client,
4997 struct wl_resource *resource,
4998 int32_t dst_width,
4999 int32_t dst_height)
5000{
5001 struct weston_surface *surface =
5002 wl_resource_get_user_data(resource);
5003
Pekka Paalanen4826f872016-04-22 14:14:38 +03005004 if (!surface) {
5005 wl_resource_post_error(resource,
5006 WP_VIEWPORT_ERROR_NO_SURFACE,
5007 "wl_surface for this viewport no longer exists");
5008 return;
5009 }
5010
5011 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005012
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005013 if (dst_width == -1 && dst_height == -1) {
5014 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005015 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005016 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005017 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005018 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005019
5020 if (dst_width <= 0 || dst_height <= 0) {
5021 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005022 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005023 "destination size must be positive (%dx%d)",
5024 dst_width, dst_height);
5025 return;
5026 }
5027
5028 surface->pending.buffer_viewport.surface.width = dst_width;
5029 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005030 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005031}
5032
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005033static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005034 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005035 viewport_set_source,
5036 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01005037};
5038
5039static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005040viewporter_destroy(struct wl_client *client,
5041 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005042{
5043 wl_resource_destroy(resource);
5044}
5045
5046static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005047viewporter_get_viewport(struct wl_client *client,
5048 struct wl_resource *viewporter,
5049 uint32_t id,
5050 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005051{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005052 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005053 struct weston_surface *surface =
5054 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005055 struct wl_resource *resource;
5056
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005057 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005058 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005059 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005060 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01005061 return;
5062 }
5063
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005064 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005065 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005066 if (resource == NULL) {
5067 wl_client_post_no_memory(client);
5068 return;
5069 }
5070
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005071 wl_resource_set_implementation(resource, &viewport_interface,
5072 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01005073
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005074 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005075}
5076
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005077static const struct wp_viewporter_interface viewporter_interface = {
5078 viewporter_destroy,
5079 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01005080};
5081
5082static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005083bind_viewporter(struct wl_client *client,
5084 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005085{
5086 struct wl_resource *resource;
5087
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005088 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005089 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005090 if (resource == NULL) {
5091 wl_client_post_no_memory(client);
5092 return;
5093 }
5094
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005095 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01005096 NULL, NULL);
5097}
5098
5099static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04005100destroy_presentation_feedback(struct wl_resource *feedback_resource)
5101{
5102 struct weston_presentation_feedback *feedback;
5103
5104 feedback = wl_resource_get_user_data(feedback_resource);
5105
5106 wl_list_remove(&feedback->link);
5107 free(feedback);
5108}
5109
5110static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005111presentation_destroy(struct wl_client *client, struct wl_resource *resource)
5112{
5113 wl_resource_destroy(resource);
5114}
5115
5116static void
5117presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005118 struct wl_resource *presentation_resource,
5119 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005120 uint32_t callback)
5121{
Pekka Paalanen133e4392014-09-23 22:08:46 -04005122 struct weston_surface *surface;
5123 struct weston_presentation_feedback *feedback;
5124
5125 surface = wl_resource_get_user_data(surface_resource);
5126
Bryce Harringtonde16d892014-11-20 22:21:57 -08005127 feedback = zalloc(sizeof *feedback);
5128 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04005129 goto err_calloc;
5130
5131 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005132 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005133 1, callback);
5134 if (!feedback->resource)
5135 goto err_create;
5136
5137 wl_resource_set_implementation(feedback->resource, NULL, feedback,
5138 destroy_presentation_feedback);
5139
5140 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
5141
5142 return;
5143
5144err_create:
5145 free(feedback);
5146
5147err_calloc:
5148 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005149}
5150
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005151static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005152 presentation_destroy,
5153 presentation_feedback
5154};
5155
5156static void
5157bind_presentation(struct wl_client *client,
5158 void *data, uint32_t version, uint32_t id)
5159{
5160 struct weston_compositor *compositor = data;
5161 struct wl_resource *resource;
5162
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005163 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005164 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005165 if (resource == NULL) {
5166 wl_client_post_no_memory(client);
5167 return;
5168 }
5169
5170 wl_resource_set_implementation(resource, &presentation_implementation,
5171 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005172 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005173}
5174
5175static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05005176compositor_bind(struct wl_client *client,
5177 void *data, uint32_t version, uint32_t id)
5178{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005179 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005180 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05005181
Jason Ekstranda85118c2013-06-27 20:17:02 -05005182 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005183 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005184 if (resource == NULL) {
5185 wl_client_post_no_memory(client);
5186 return;
5187 }
5188
5189 wl_resource_set_implementation(resource, &compositor_interface,
5190 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05005191}
5192
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005193WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005194weston_environment_get_fd(const char *env)
5195{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005196 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005197 int fd, flags;
5198
5199 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005200 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005201 return -1;
5202
5203 flags = fcntl(fd, F_GETFD);
5204 if (flags == -1)
5205 return -1;
5206
5207 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
5208 unsetenv(env);
5209
5210 return fd;
5211}
5212
Pekka Paalanenb5026542014-11-12 15:09:24 +02005213static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005214timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02005215 uint32_t key, void *data)
5216{
5217 struct weston_compositor *compositor = data;
5218
5219 if (weston_timeline_enabled_)
5220 weston_timeline_close();
5221 else
5222 weston_timeline_open(compositor);
5223}
5224
Giulio Camuffo459137b2014-10-11 23:56:24 +03005225/** Create the compositor.
5226 *
5227 * This functions creates and initializes a compositor instance.
5228 *
5229 * \param display The Wayland display to be used.
5230 * \param user_data A pointer to an object that can later be retrieved
5231 * using the \ref weston_compositor_get_user_data function.
5232 * \return The compositor instance on success or NULL on failure.
5233 */
5234WL_EXPORT struct weston_compositor *
5235weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005236{
Giulio Camuffo459137b2014-10-11 23:56:24 +03005237 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05005238 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07005239
Giulio Camuffo459137b2014-10-11 23:56:24 +03005240 ec = zalloc(sizeof *ec);
5241 if (!ec)
5242 return NULL;
5243
5244 ec->wl_display = display;
5245 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005246 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07005247 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005248 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005249 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005250 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005251 wl_signal_init(&ec->idle_signal);
5252 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005253 wl_signal_init(&ec->show_input_panel_signal);
5254 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005255 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01005256 wl_signal_init(&ec->seat_created_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005257 wl_signal_init(&ec->output_pending_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01005258 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02005259 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005260 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02005261 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005262 wl_signal_init(&ec->session_signal);
5263 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005264
Casey Dahlin58ba1372012-04-19 22:50:08 -04005265 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03005266 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04005267
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02005268 ec->activate_serial = 1;
5269
Derek Foreman152254b2015-11-26 14:17:48 -06005270 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005271 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005272 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05005273
Giulio Camuffo954f1832014-10-11 18:27:30 +03005274 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005275 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005276 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03005277
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005278 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005279 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005280 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005281
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005282 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005283 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005284 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005285
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005286 if (weston_input_init(ec) != 0)
5287 goto fail;
5288
Jason Ekstranda7af7042013-10-12 22:38:11 -05005289 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005290 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005291 wl_list_init(&ec->layer_list);
5292 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005293 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005294 wl_list_init(&ec->output_list);
5295 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01005296 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005297 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005298 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005299 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005300 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005301
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005302 wl_list_init(&ec->plugin_api_list);
5303
Xiong Zhang97116532013-10-23 13:58:31 +08005304 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005305 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005306
Giulio Camuffo459137b2014-10-11 23:56:24 +03005307 wl_data_device_manager_init(ec->wl_display);
5308
5309 wl_display_init_shm(ec->wl_display);
5310
5311 loop = wl_display_get_event_loop(ec->wl_display);
5312 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00005313 ec->repaint_timer =
5314 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
5315 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005316
Quentin Glidic82681572016-12-17 13:40:51 +01005317 weston_layer_init(&ec->fade_layer, ec);
5318 weston_layer_init(&ec->cursor_layer, ec);
5319
5320 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
5321 weston_layer_set_position(&ec->cursor_layer,
5322 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005323
5324 weston_compositor_add_debug_binding(ec, KEY_T,
5325 timeline_key_binding_handler, ec);
5326
Giulio Camuffo459137b2014-10-11 23:56:24 +03005327 return ec;
5328
5329fail:
5330 free(ec);
5331 return NULL;
5332}
5333
Benjamin Franzkeb8263022011-08-30 11:32:47 +02005334WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005335weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07005336{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005337 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07005338
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005339 wl_event_source_remove(ec->idle_source);
5340
Matt Roper361d2ad2011-08-29 13:52:23 -07005341 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02005342 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07005343 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02005344
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005345 /* Destroy all pending outputs associated with this compositor */
5346 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
5347 output->destroy(output);
5348
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02005349 if (ec->renderer)
5350 ec->renderer->destroy(ec);
5351
Daniel Stone325fc2d2012-05-30 16:31:58 +01005352 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09005353 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005354 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005355 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005356 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005357 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005358
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005359 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07005360}
5361
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005362WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04005363weston_compositor_exit_with_code(struct weston_compositor *compositor,
5364 int exit_code)
5365{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02005366 if (compositor->exit_code == EXIT_SUCCESS)
5367 compositor->exit_code = exit_code;
5368
Giulio Camuffo459137b2014-10-11 23:56:24 +03005369 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04005370}
5371
5372WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005373weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
5374 const struct weston_pointer_grab_interface *interface)
5375{
5376 struct weston_seat *seat;
5377
5378 ec->default_pointer_grab = interface;
5379 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005380 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5381
5382 if (pointer)
5383 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005384 }
5385}
5386
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04005387WL_EXPORT int
5388weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
5389 clockid_t clk_id)
5390{
5391 struct timespec ts;
5392
5393 if (clock_gettime(clk_id, &ts) < 0)
5394 return -1;
5395
5396 compositor->presentation_clock = clk_id;
5397
5398 return 0;
5399}
5400
5401/*
5402 * For choosing the software clock, when the display hardware or API
5403 * does not expose a compatible presentation timestamp.
5404 */
5405WL_EXPORT int
5406weston_compositor_set_presentation_clock_software(
5407 struct weston_compositor *compositor)
5408{
5409 /* In order of preference */
5410 static const clockid_t clocks[] = {
5411 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
5412 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
5413 CLOCK_MONOTONIC, /* no jumps, may crawl */
5414 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
5415 CLOCK_REALTIME /* may jump and crawl */
5416 };
5417 unsigned i;
5418
5419 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
5420 if (weston_compositor_set_presentation_clock(compositor,
5421 clocks[i]) == 0)
5422 return 0;
5423
5424 weston_log("Error: no suitable presentation clock available.\n");
5425
5426 return -1;
5427}
5428
Pekka Paalanen662f3842015-03-18 12:17:26 +02005429/** Read the current time from the Presentation clock
5430 *
5431 * \param compositor
5432 * \param ts[out] The current time.
5433 *
5434 * \note Reading the current time in user space is always imprecise to some
5435 * degree.
5436 *
5437 * This function is never meant to fail. If reading the clock does fail,
5438 * an error message is logged and a zero time is returned. Callers are not
5439 * supposed to detect or react to failures.
5440 */
5441WL_EXPORT void
5442weston_compositor_read_presentation_clock(
5443 const struct weston_compositor *compositor,
5444 struct timespec *ts)
5445{
5446 static bool warned;
5447 int ret;
5448
5449 ret = clock_gettime(compositor->presentation_clock, ts);
5450 if (ret < 0) {
5451 ts->tv_sec = 0;
5452 ts->tv_nsec = 0;
5453
5454 if (!warned)
5455 weston_log("Error: failure to read "
5456 "the presentation clock %#x: '%m' (%d)\n",
5457 compositor->presentation_clock, errno);
5458 warned = true;
5459 }
5460}
5461
Pekka Paalanen230f3b12014-09-29 14:18:40 -04005462/** Import dmabuf buffer into current renderer
5463 *
5464 * \param compositor
5465 * \param buffer the dmabuf buffer to import
5466 * \return true on usable buffers, false otherwise
5467 *
5468 * This function tests that the linux_dmabuf_buffer is usable
5469 * for the current renderer. Returns false on unusable buffers. Usually
5470 * usability is tested by importing the dmabufs for composition.
5471 *
5472 * This hook is also used for detecting if the renderer supports
5473 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
5474 * supported.
5475 * */
5476WL_EXPORT bool
5477weston_compositor_import_dmabuf(struct weston_compositor *compositor,
5478 struct linux_dmabuf_buffer *buffer)
5479{
5480 struct weston_renderer *renderer;
5481
5482 renderer = compositor->renderer;
5483
5484 if (renderer->import_dmabuf == NULL)
5485 return false;
5486
5487 return renderer->import_dmabuf(compositor, buffer);
5488}
5489
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005490WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005491weston_version(int *major, int *minor, int *micro)
5492{
5493 *major = WESTON_VERSION_MAJOR;
5494 *minor = WESTON_VERSION_MINOR;
5495 *micro = WESTON_VERSION_MICRO;
5496}
5497
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005498WL_EXPORT void *
5499weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005500{
Derek Foreman3f86e502015-06-08 11:46:54 -05005501 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005502 char path[PATH_MAX];
5503 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00005504 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005505
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005506 if (name == NULL)
5507 return NULL;
5508
Derek Foreman3f86e502015-06-08 11:46:54 -05005509 if (name[0] != '/') {
5510 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00005511 len = snprintf(path, sizeof path, "%s/.libs/%s",
5512 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005513 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00005514 len = snprintf(path, sizeof path, "%s/%s",
5515 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005516 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00005517 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005518 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005519
Daniel Stonebeb97e52016-11-28 12:13:54 +00005520 /* snprintf returns the length of the string it would've written,
5521 * _excluding_ the NUL byte. So even being equal to the size of
5522 * our buffer is an error here. */
5523 if (len >= sizeof path)
5524 return NULL;
5525
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005526 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
5527 if (module) {
5528 weston_log("Module '%s' already loaded\n", path);
5529 dlclose(module);
5530 return NULL;
5531 }
5532
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005533 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04005534 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005535 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005536 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005537 return NULL;
5538 }
5539
5540 init = dlsym(module, entrypoint);
5541 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005542 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00005543 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005544 return NULL;
5545 }
5546
5547 return init;
5548}
5549
Giulio Camuffo459137b2014-10-11 23:56:24 +03005550
5551/** Destroys the compositor.
5552 *
5553 * This function cleans up the compositor state and destroys it.
5554 *
5555 * \param compositor The compositor to be destroyed.
5556 */
5557WL_EXPORT void
5558weston_compositor_destroy(struct weston_compositor *compositor)
5559{
5560 /* prevent further rendering while shutting down */
5561 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5562
5563 wl_signal_emit(&compositor->destroy_signal, compositor);
5564
5565 weston_compositor_xkb_destroy(compositor);
5566
Giulio Camuffo2d24e642015-10-03 16:25:15 +03005567 if (compositor->backend)
5568 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005569
5570 weston_plugin_api_destroy_list(compositor);
5571
Giulio Camuffo459137b2014-10-11 23:56:24 +03005572 free(compositor);
5573}
5574
5575/** Instruct the compositor to exit.
5576 *
5577 * This functions does not directly destroy the compositor object, it merely
5578 * command it to start the tear down process. It is not guaranteed that the
5579 * tear down will happen immediately.
5580 *
5581 * \param compositor The compositor to tear down.
5582 */
5583WL_EXPORT void
5584weston_compositor_exit(struct weston_compositor *compositor)
5585{
5586 compositor->exit(compositor);
5587}
5588
5589/** Return the user data stored in the compositor.
5590 *
5591 * This function returns the user data pointer set with user_data parameter
5592 * to the \ref weston_compositor_create function.
5593 */
5594WL_EXPORT void *
5595weston_compositor_get_user_data(struct weston_compositor *compositor)
5596{
5597 return compositor->user_data;
5598}
Pekka Paalanendd186732016-06-03 14:49:54 +03005599
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005600static const char * const backend_map[] = {
5601 [WESTON_BACKEND_DRM] = "drm-backend.so",
5602 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5603 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5604 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5605 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5606 [WESTON_BACKEND_X11] = "x11-backend.so",
5607};
5608
Pekka Paalanendd186732016-06-03 14:49:54 +03005609/** Load a backend into a weston_compositor
5610 *
5611 * A backend must be loaded to make a weston_compositor work. A backend
5612 * provides input and output capabilities, and determines the renderer to use.
5613 *
5614 * \param compositor A compositor that has not had a backend loaded yet.
5615 * \param backend Name of the backend file.
5616 * \param config_base A pointer to a backend-specific configuration
5617 * structure's 'base' member.
5618 *
5619 * \return 0 on success, or -1 on error.
5620 */
5621WL_EXPORT int
5622weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005623 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005624 struct weston_backend_config *config_base)
5625{
5626 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005627 struct weston_backend_config *config_base);
5628
Quentin Glidic887c0182016-07-10 11:00:53 +02005629 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005630 return -1;
5631
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01005632 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005633 if (!backend_init)
5634 return -1;
5635
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005636 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005637}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005638
5639WL_EXPORT int
5640weston_compositor_load_xwayland(struct weston_compositor *compositor)
5641{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005642 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005643
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005644 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005645 if (!module_init)
5646 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005647 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005648 return -1;
5649 return 0;
5650}