blob: 7d7a17edfec365a24d75146b6843249b19a86ab1 [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
Pekka Paalanen01e00682017-03-24 16:21:06 +0200921/** Send wl_surface.enter/leave events
922 *
923 * \param surface The surface.
924 * \param output The entered/left output.
925 * \param enter True if entered.
926 * \param left True if left.
927 *
928 * Send the enter/leave events for all protocol objects bound to the given
929 * output by the client owning the surface.
930 */
931static void
932weston_surface_send_enter_leave(struct weston_surface *surface,
933 struct weston_output *output,
934 bool enter,
935 bool leave)
936{
937 struct wl_resource *wloutput;
938 struct wl_client *client;
939
940 assert(enter != leave);
941
942 client = wl_resource_get_client(surface->resource);
943 wl_resource_for_each(wloutput, &output->resource_list) {
944 if (wl_resource_get_client(wloutput) != client)
945 continue;
946
947 if (enter)
948 wl_surface_send_enter(surface->resource, wloutput);
949 if (leave)
950 wl_surface_send_leave(surface->resource, wloutput);
951 }
952}
953
Bryce Harrington3f650b82015-12-23 11:01:58 -0800954/**
955 * \param es The surface
956 * \param mask The new set of outputs for the surface
957 *
958 * Sets the surface's set of outputs to the ones specified by
959 * the new output mask provided. Identifies the outputs that
960 * have changed, the posts enter and leave events for these
961 * outputs as appropriate.
962 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400963static void
964weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
965{
966 uint32_t different = es->output_mask ^ mask;
967 uint32_t entered = mask & different;
968 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +0200969 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400970 struct weston_output *output;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400971
972 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500973 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400974 return;
975 if (different == 0)
976 return;
977
978 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +0200979 output_bit = 1u << output->id;
980 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400981 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +0200982
983 weston_surface_send_enter_leave(es, output,
984 output_bit & entered,
985 output_bit & left);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400986 }
987}
988
Bryce Harrington3f650b82015-12-23 11:01:58 -0800989/** Recalculate which output(s) the surface has views displayed on
990 *
991 * \param es The surface to remap to outputs
992 *
993 * Finds the output that is showing the largest amount of one
994 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300995 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800996 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300997 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800998 * in the output_mask for the surface.
999 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001000static void
1001weston_surface_assign_output(struct weston_surface *es)
1002{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 struct weston_output *new_output;
1004 struct weston_view *view;
1005 pixman_region32_t region;
1006 uint32_t max, area, mask;
1007 pixman_box32_t *e;
1008
1009 new_output = NULL;
1010 max = 0;
1011 mask = 0;
1012 pixman_region32_init(&region);
1013 wl_list_for_each(view, &es->views, surface_link) {
1014 if (!view->output)
1015 continue;
1016
1017 pixman_region32_intersect(&region, &view->transform.boundingbox,
1018 &view->output->region);
1019
1020 e = pixman_region32_extents(&region);
1021 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1022
1023 mask |= view->output_mask;
1024
1025 if (area >= max) {
1026 new_output = view->output;
1027 max = area;
1028 }
1029 }
1030 pixman_region32_fini(&region);
1031
1032 es->output = new_output;
1033 weston_surface_update_output_mask(es, mask);
1034}
1035
Bryce Harrington3f650b82015-12-23 11:01:58 -08001036/** Recalculate which output(s) the view is displayed on
1037 *
1038 * \param ev The view to remap to outputs
1039 *
1040 * Identifies the set of outputs that the view is visible on,
1041 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001042 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001043 *
1044 * Also does the same for the view's surface. See
1045 * weston_surface_assign_output().
1046 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001047static void
1048weston_view_assign_output(struct weston_view *ev)
1049{
1050 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001051 struct weston_output *output, *new_output;
1052 pixman_region32_t region;
1053 uint32_t max, area, mask;
1054 pixman_box32_t *e;
1055
1056 new_output = NULL;
1057 max = 0;
1058 mask = 0;
1059 pixman_region32_init(&region);
1060 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001061 if (output->destroying)
1062 continue;
1063
Jason Ekstranda7af7042013-10-12 22:38:11 -05001064 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001065 &output->region);
1066
1067 e = pixman_region32_extents(&region);
1068 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1069
1070 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001071 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001072
1073 if (area >= max) {
1074 new_output = output;
1075 max = area;
1076 }
1077 }
1078 pixman_region32_fini(&region);
1079
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 ev->output = new_output;
1081 ev->output_mask = mask;
1082
1083 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001084}
1085
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001086static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001087weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1088 int from_x, int from_y, int *to_x, int *to_y)
1089{
1090 float x, y;
1091
1092 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1093 weston_view_from_global_float(to, x, y, &x, &y);
1094
1095 *to_x = round(x);
1096 *to_y = round(y);
1097}
1098
1099static void
1100weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1101{
1102 pixman_box32_t *a;
1103 pixman_box32_t b;
1104
1105 a = pixman_region32_extents(&from->geometry.scissor);
1106
1107 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1108 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1109
1110 pixman_region32_fini(&to->geometry.scissor);
1111 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1112}
1113
1114static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001115view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001117{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001118 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1119 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001120 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001121 { inbox->x1, inbox->y1 },
1122 { inbox->x1, inbox->y2 },
1123 { inbox->x2, inbox->y1 },
1124 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001125 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001126 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001127 int i;
1128
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001129 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001130 /* avoid rounding empty bbox to 1x1 */
1131 pixman_region32_init(bbox);
1132 return;
1133 }
1134
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001135 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001136 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001138 if (x < min_x)
1139 min_x = x;
1140 if (x > max_x)
1141 max_x = x;
1142 if (y < min_y)
1143 min_y = y;
1144 if (y > max_y)
1145 max_y = y;
1146 }
1147
Pekka Paalanen219b9822012-02-08 15:38:37 +02001148 int_x = floorf(min_x);
1149 int_y = floorf(min_y);
1150 pixman_region32_init_rect(bbox, int_x, int_y,
1151 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001152}
1153
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001154static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001156{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001158
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001159 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160 view->geometry.x = roundf(view->geometry.x);
1161 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001162
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001163 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1165 view->transform.position.matrix.d[12] = view->geometry.x;
1166 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001167
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 view->transform.inverse = view->transform.position.matrix;
1171 view->transform.inverse.d[12] = -view->geometry.x;
1172 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001173
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001175 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001176 view->surface->width,
1177 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001178 if (view->geometry.scissor_enabled)
1179 pixman_region32_intersect(&view->transform.boundingbox,
1180 &view->transform.boundingbox,
1181 &view->geometry.scissor);
1182
1183 pixman_region32_translate(&view->transform.boundingbox,
1184 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001185
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 if (view->alpha == 1.0) {
1187 pixman_region32_copy(&view->transform.opaque,
1188 &view->surface->opaque);
1189 pixman_region32_translate(&view->transform.opaque,
1190 view->geometry.x,
1191 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001192 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001193}
1194
1195static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001197{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198 struct weston_view *parent = view->geometry.parent;
1199 struct weston_matrix *matrix = &view->transform.matrix;
1200 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001201 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001202 pixman_region32_t surfregion;
1203 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001204
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001206
1207 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1209 view->transform.position.matrix.d[12] = view->geometry.x;
1210 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001211
1212 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001214 weston_matrix_multiply(matrix, &tform->matrix);
1215
Pekka Paalanen483243f2013-03-08 14:56:50 +02001216 if (parent)
1217 weston_matrix_multiply(matrix, &parent->transform.matrix);
1218
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001219 if (weston_matrix_invert(inverse, matrix) < 0) {
1220 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 weston_log("error: weston_view %p"
1222 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001223 return -1;
1224 }
1225
Giulio Camuffo148c1992016-09-04 18:50:46 +03001226 if (view->alpha == 1.0 &&
1227 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1228 pixman_region32_copy(&view->transform.opaque,
1229 &view->surface->opaque);
1230 pixman_region32_translate(&view->transform.opaque,
1231 matrix->d[12],
1232 matrix->d[13]);
1233 }
1234
Pekka Paalanen380adf52015-02-16 14:39:11 +02001235 pixman_region32_init_rect(&surfregion, 0, 0,
1236 view->surface->width, view->surface->height);
1237 if (view->geometry.scissor_enabled)
1238 pixman_region32_intersect(&surfregion, &surfregion,
1239 &view->geometry.scissor);
1240 surfbox = pixman_region32_extents(&surfregion);
1241
1242 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1243 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001244
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001245 return 0;
1246}
1247
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001248static struct weston_layer *
1249get_view_layer(struct weston_view *view)
1250{
1251 if (view->parent_view)
1252 return get_view_layer(view->parent_view);
1253 return view->layer_link.layer;
1254}
1255
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001256WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001258{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001260 struct weston_layer *layer;
1261 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001262
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001264 return;
1265
Pekka Paalanen483243f2013-03-08 14:56:50 +02001266 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001268
Jason Ekstranda7af7042013-10-12 22:38:11 -05001269 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001270
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001272
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 pixman_region32_fini(&view->transform.boundingbox);
1274 pixman_region32_fini(&view->transform.opaque);
1275 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001276
Pekka Paalanencd403622012-01-25 13:37:39 +02001277 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 if (view->geometry.transformation_list.next ==
1279 &view->transform.position.link &&
1280 view->geometry.transformation_list.prev ==
1281 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001282 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001284 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 if (weston_view_update_transform_enable(view) < 0)
1286 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001287 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001288
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001289 layer = get_view_layer(view);
1290 if (layer) {
1291 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001292 pixman_region32_intersect(&view->transform.boundingbox,
1293 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001294 pixman_region32_intersect(&view->transform.opaque,
1295 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001296 pixman_region32_fini(&mask);
1297 }
1298
Pekka Paalanen380adf52015-02-16 14:39:11 +02001299 if (parent) {
1300 if (parent->geometry.scissor_enabled) {
1301 view->geometry.scissor_enabled = true;
1302 weston_view_transfer_scissor(parent, view);
1303 } else {
1304 view->geometry.scissor_enabled = false;
1305 }
1306 }
1307
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001309
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001311
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 wl_signal_emit(&view->surface->compositor->transform_signal,
1313 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001314}
1315
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001316WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001318{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001320
1321 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 * The invariant: if view->geometry.dirty, then all views
1323 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001324 * Corollary: if not parent->geometry.dirty, then all ancestors
1325 * are not dirty.
1326 */
1327
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001329 return;
1330
Jason Ekstranda7af7042013-10-12 22:38:11 -05001331 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001332
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001334 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001336}
1337
1338WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339weston_view_to_global_fixed(struct weston_view *view,
1340 wl_fixed_t vx, wl_fixed_t vy,
1341 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001342{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001343 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001344
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 weston_view_to_global_float(view,
1346 wl_fixed_to_double(vx),
1347 wl_fixed_to_double(vy),
1348 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001349 *x = wl_fixed_from_double(xf);
1350 *y = wl_fixed_from_double(yf);
1351}
1352
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001353WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354weston_view_from_global_float(struct weston_view *view,
1355 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001356{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001358 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001361
1362 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001363 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001365 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 *vx = 0;
1367 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001368 return;
1369 }
1370
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371 *vx = v.f[0] / v.f[3];
1372 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001373 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374 *vx = x - view->geometry.x;
1375 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001376 }
1377}
1378
1379WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380weston_view_from_global_fixed(struct weston_view *view,
1381 wl_fixed_t x, wl_fixed_t y,
1382 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001383{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001385
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 weston_view_from_global_float(view,
1387 wl_fixed_to_double(x),
1388 wl_fixed_to_double(y),
1389 &vxf, &vyf);
1390 *vx = wl_fixed_from_double(vxf);
1391 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001392}
1393
1394WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395weston_view_from_global(struct weston_view *view,
1396 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001397{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001399
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1401 *vx = floorf(vxf);
1402 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001403}
1404
Bryce Harrington3f650b82015-12-23 11:01:58 -08001405/**
1406 * \param surface The surface to be repainted
1407 *
1408 * Marks the output(s) that the surface is shown on as needing to be
1409 * repainted. See weston_output_schedule_repaint().
1410 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001411WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001412weston_surface_schedule_repaint(struct weston_surface *surface)
1413{
1414 struct weston_output *output;
1415
1416 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001417 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001418 weston_output_schedule_repaint(output);
1419}
1420
Bryce Harrington3f650b82015-12-23 11:01:58 -08001421/**
1422 * \param view The view to be repainted
1423 *
1424 * Marks the output(s) that the view is shown on as needing to be
1425 * repainted. See weston_output_schedule_repaint().
1426 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001427WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428weston_view_schedule_repaint(struct weston_view *view)
1429{
1430 struct weston_output *output;
1431
1432 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001433 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 weston_output_schedule_repaint(output);
1435}
1436
Pekka Paalanene508ce62015-02-19 13:59:55 +02001437/**
1438 * XXX: This function does it the wrong way.
1439 * surface->damage is the damage from the client, and causes
1440 * surface_flush_damage() to copy pixels. No window management action can
1441 * cause damage to the client-provided content, warranting re-upload!
1442 *
1443 * Instead of surface->damage, this function should record the damage
1444 * with all the views for this surface to avoid extraneous texture
1445 * uploads.
1446 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001448weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001449{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001450 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 0, 0, surface->width,
1452 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001453
Kristian Høgsberg98238702012-08-03 16:29:12 -04001454 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001455}
1456
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001457WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001459{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001460 if (view->geometry.x == x && view->geometry.y == y)
1461 return;
1462
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 view->geometry.x = x;
1464 view->geometry.y = y;
1465 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001466}
1467
Pekka Paalanen483243f2013-03-08 14:56:50 +02001468static void
1469transform_parent_handle_parent_destroy(struct wl_listener *listener,
1470 void *data)
1471{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 struct weston_view *view =
1473 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001474 geometry.parent_destroy_listener);
1475
Jason Ekstranda7af7042013-10-12 22:38:11 -05001476 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001477}
1478
1479WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001481 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001482{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 if (view->geometry.parent) {
1484 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1485 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001486
1487 if (!parent)
1488 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001489 }
1490
Jason Ekstranda7af7042013-10-12 22:38:11 -05001491 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001492
Jason Ekstranda7af7042013-10-12 22:38:11 -05001493 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001494 transform_parent_handle_parent_destroy;
1495 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001496 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001498 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001499 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001500 }
1501
Jason Ekstranda7af7042013-10-12 22:38:11 -05001502 weston_view_geometry_dirty(view);
1503}
1504
Pekka Paalanen380adf52015-02-16 14:39:11 +02001505/** Set a clip mask rectangle on a view
1506 *
1507 * \param view The view to set the clip mask on.
1508 * \param x Top-left corner X coordinate of the clip rectangle.
1509 * \param y Top-left corner Y coordinate of the clip rectangle.
1510 * \param width Width of the clip rectangle, non-negative.
1511 * \param height Height of the clip rectangle, non-negative.
1512 *
1513 * A shell may set a clip mask rectangle on a view. Everything outside
1514 * the rectangle is cut away for input and output purposes: it is
1515 * not drawn and cannot be hit by hit-test based input like pointer
1516 * motion or touch-downs. Everything inside the rectangle will behave
1517 * normally. Clients are unaware of clipping.
1518 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001519 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001520 * mask rectangle does not affect the view position, the view is positioned
1521 * as it would be without a clip. The clip also does not change
1522 * weston_surface::width,height.
1523 *
1524 * The clip mask rectangle is part of transformation inheritance
1525 * (weston_view_set_transform_parent()). A clip set in the root of the
1526 * transformation inheritance tree will affect all views in the tree.
1527 * A clip can be set only on the root view. Attempting to set a clip
1528 * on view that has a transformation parent will fail. Assigning a parent
1529 * to a view that has a clip set will cause the clip to be forgotten.
1530 *
1531 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1532 * on the additional transformations in the child views. These transformations
1533 * may not rotate the coordinate axes, i.e., only translation and scaling
1534 * are allowed. Violating this restriction causes the clipping to malfunction.
1535 * Furthermore, using scaling may cause rounding errors in child clipping.
1536 *
1537 * The clip mask rectangle is not automatically adjusted based on
1538 * wl_surface.attach dx and dy arguments.
1539 *
1540 * A clip mask rectangle can be set only if the compositor capability
1541 * WESTON_CAP_VIEW_CLIP_MASK is present.
1542 *
1543 * This function sets the clip mask rectangle and schedules a repaint for
1544 * the view.
1545 */
1546WL_EXPORT void
1547weston_view_set_mask(struct weston_view *view,
1548 int x, int y, int width, int height)
1549{
1550 struct weston_compositor *compositor = view->surface->compositor;
1551
1552 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1553 weston_log("%s not allowed without capability!\n", __func__);
1554 return;
1555 }
1556
1557 if (view->geometry.parent) {
1558 weston_log("view %p has a parent, clip forbidden!\n", view);
1559 return;
1560 }
1561
1562 if (width < 0 || height < 0) {
1563 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1564 x, y, width, height);
1565 return;
1566 }
1567
1568 pixman_region32_fini(&view->geometry.scissor);
1569 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1570 view->geometry.scissor_enabled = true;
1571 weston_view_geometry_dirty(view);
1572 weston_view_schedule_repaint(view);
1573}
1574
1575/** Remove the clip mask from a view
1576 *
1577 * \param view The view to remove the clip mask from.
1578 *
1579 * Removed the clip mask rectangle and schedules a repaint.
1580 *
1581 * \sa weston_view_set_mask
1582 */
1583WL_EXPORT void
1584weston_view_set_mask_infinite(struct weston_view *view)
1585{
1586 view->geometry.scissor_enabled = false;
1587 weston_view_geometry_dirty(view);
1588 weston_view_schedule_repaint(view);
1589}
1590
Armin Krezović0da12b82016-06-30 06:04:33 +02001591/* Check if view should be displayed
1592 *
1593 * The indicator is set manually when assigning
1594 * a view to a surface.
1595 *
1596 * This needs reworking. See the thread starting at:
1597 *
1598 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1599 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001600WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001601weston_view_is_mapped(struct weston_view *view)
1602{
Armin Krezović0da12b82016-06-30 06:04:33 +02001603 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001604}
1605
Armin Krezović0da12b82016-06-30 06:04:33 +02001606/* Check if a surface has a view assigned to it
1607 *
1608 * The indicator is set manually when mapping
1609 * a surface and creating a view for it.
1610 *
1611 * This needs to go. See the thread starting at:
1612 *
1613 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1614 *
1615 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001616WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001617weston_surface_is_mapped(struct weston_surface *surface)
1618{
Armin Krezović0da12b82016-06-30 06:04:33 +02001619 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001620}
1621
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001622static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001623surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001624{
1625 struct weston_view *view;
1626
1627 if (surface->width == width && surface->height == height)
1628 return;
1629
1630 surface->width = width;
1631 surface->height = height;
1632
1633 wl_list_for_each(view, &surface->views, surface_link)
1634 weston_view_geometry_dirty(view);
1635}
1636
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001637WL_EXPORT void
1638weston_surface_set_size(struct weston_surface *surface,
1639 int32_t width, int32_t height)
1640{
1641 assert(!surface->resource);
1642 surface_set_size(surface, width, height);
1643}
1644
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001645static int
1646fixed_round_up_to_int(wl_fixed_t f)
1647{
1648 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1649}
1650
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001651static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001652convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1653 int32_t width, int32_t height,
1654 uint32_t transform,
1655 int32_t scale)
1656{
1657 assert(scale > 0);
1658
1659 switch (transform) {
1660 case WL_OUTPUT_TRANSFORM_NORMAL:
1661 case WL_OUTPUT_TRANSFORM_180:
1662 case WL_OUTPUT_TRANSFORM_FLIPPED:
1663 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1664 *width_out = width / scale;
1665 *height_out = height / scale;
1666 break;
1667 case WL_OUTPUT_TRANSFORM_90:
1668 case WL_OUTPUT_TRANSFORM_270:
1669 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1670 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1671 *width_out = height / scale;
1672 *height_out = width / scale;
1673 break;
1674 default:
1675 assert(0 && "invalid transform");
1676 }
1677}
1678
1679static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001680weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001681{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001682 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001683
1684 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001685 surface->width_from_buffer = 0;
1686 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001687 return;
1688 }
1689
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001690 convert_size_by_transform_scale(&surface->width_from_buffer,
1691 &surface->height_from_buffer,
1692 surface->buffer_ref.buffer->width,
1693 surface->buffer_ref.buffer->height,
1694 vp->buffer.transform,
1695 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001696}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001697
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001698static void
1699weston_surface_update_size(struct weston_surface *surface)
1700{
1701 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1702 int32_t width, height;
1703
1704 width = surface->width_from_buffer;
1705 height = surface->height_from_buffer;
1706
1707 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001708 surface_set_size(surface,
1709 vp->surface.width, vp->surface.height);
1710 return;
1711 }
1712
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001713 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001714 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1715 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1716
1717 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001718 return;
1719 }
1720
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001721 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001722}
1723
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001724WL_EXPORT void
1725weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001726{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001727 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001728}
1729
Jason Ekstranda7af7042013-10-12 22:38:11 -05001730WL_EXPORT struct weston_view *
1731weston_compositor_pick_view(struct weston_compositor *compositor,
1732 wl_fixed_t x, wl_fixed_t y,
1733 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001734{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001736 wl_fixed_t view_x, view_y;
1737 int view_ix, view_iy;
1738 int ix = wl_fixed_to_int(x);
1739 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001740
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001742 if (!pixman_region32_contains_point(
1743 &view->transform.boundingbox, ix, iy, NULL))
1744 continue;
1745
1746 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1747 view_ix = wl_fixed_to_int(view_x);
1748 view_iy = wl_fixed_to_int(view_y);
1749
1750 if (!pixman_region32_contains_point(&view->surface->input,
1751 view_ix, view_iy, NULL))
1752 continue;
1753
Pekka Paalanen380adf52015-02-16 14:39:11 +02001754 if (view->geometry.scissor_enabled &&
1755 !pixman_region32_contains_point(&view->geometry.scissor,
1756 view_ix, view_iy, NULL))
1757 continue;
1758
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001759 *vx = view_x;
1760 *vy = view_y;
1761 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001762 }
1763
Derek Foremanf9318d12015-05-11 15:40:11 -05001764 *vx = wl_fixed_from_int(-1000000);
1765 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001766 return NULL;
1767}
1768
1769static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001770weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001771{
Daniel Stone37816df2012-05-16 18:45:18 +01001772 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001773
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001774 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001775 return;
1776
Daniel Stone37816df2012-05-16 18:45:18 +01001777 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001778 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001779}
1780
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001781WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001782weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001783{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001784 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001785
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786 if (!weston_view_is_mapped(view))
1787 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001788
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789 weston_view_damage_below(view);
1790 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001791 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001792 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001793 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794 wl_list_remove(&view->link);
1795 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796 view->output_mask = 0;
1797 weston_surface_assign_output(view->surface);
1798
1799 if (weston_surface_is_mapped(view->surface))
1800 return;
1801
1802 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001803 struct weston_touch *touch = weston_seat_get_touch(seat);
1804 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1805 struct weston_keyboard *keyboard =
1806 weston_seat_get_keyboard(seat);
1807
1808 if (keyboard && keyboard->focus == view->surface)
1809 weston_keyboard_set_focus(keyboard, NULL);
1810 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001811 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001812 if (touch && touch->focus == view)
1813 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001814 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001816
Jason Ekstranda7af7042013-10-12 22:38:11 -05001817WL_EXPORT void
1818weston_surface_unmap(struct weston_surface *surface)
1819{
1820 struct weston_view *view;
1821
Armin Krezovićf8486c32016-06-30 06:04:28 +02001822 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001823 wl_list_for_each(view, &surface->views, surface_link)
1824 weston_view_unmap(view);
1825 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001826}
1827
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001828static void
1829weston_surface_reset_pending_buffer(struct weston_surface *surface)
1830{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001831 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001832 surface->pending.sx = 0;
1833 surface->pending.sy = 0;
1834 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001835 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001836}
1837
Jason Ekstranda7af7042013-10-12 22:38:11 -05001838WL_EXPORT void
1839weston_view_destroy(struct weston_view *view)
1840{
1841 wl_signal_emit(&view->destroy_signal, view);
1842
1843 assert(wl_list_empty(&view->geometry.child_list));
1844
1845 if (weston_view_is_mapped(view)) {
1846 weston_view_unmap(view);
1847 weston_compositor_build_view_list(view->surface->compositor);
1848 }
1849
1850 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001851 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001852
1853 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001854 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001855 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001856 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001857
1858 weston_view_set_transform_parent(view, NULL);
1859
Jason Ekstranda7af7042013-10-12 22:38:11 -05001860 wl_list_remove(&view->surface_link);
1861
1862 free(view);
1863}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001864
1865WL_EXPORT void
1866weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001867{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001868 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001870 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001871
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001872 if (--surface->ref_count > 0)
1873 return;
1874
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001875 assert(surface->resource == NULL);
1876
Pekka Paalanenca790762015-04-17 14:23:38 +03001877 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001878
Pekka Paalanene67858b2013-04-25 13:57:42 +03001879 assert(wl_list_empty(&surface->subsurface_list_pending));
1880 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001881
Jason Ekstranda7af7042013-10-12 22:38:11 -05001882 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1883 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001884
Jason Ekstrand7b982072014-05-20 14:33:03 -05001885 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001886
Pekka Paalanende685b82012-12-04 15:58:12 +02001887 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001888
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001889 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001890 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001891 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001892
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001893 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001894 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001895
Pekka Paalanen133e4392014-09-23 22:08:46 -04001896 weston_presentation_feedback_discard_list(&surface->feedback_list);
1897
Jonas Ådahld3414f22016-07-22 17:56:31 +08001898 wl_list_for_each_safe(constraint, next_constraint,
1899 &surface->pointer_constraints,
1900 link)
1901 weston_pointer_constraint_destroy(constraint);
1902
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001903 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001904}
1905
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001906static void
1907destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001908{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001909 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001910
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001911 assert(surface);
1912
Giulio Camuffo0d379742013-11-15 22:06:15 +01001913 /* Set the resource to NULL, since we don't want to leave a
1914 * dangling pointer if the surface was refcounted and survives
1915 * the weston_surface_destroy() call. */
1916 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001917
1918 if (surface->viewport_resource)
1919 wl_resource_set_user_data(surface->viewport_resource, NULL);
1920
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001921 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001922}
1923
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001924static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001925weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1926{
1927 struct weston_buffer *buffer =
1928 container_of(listener, struct weston_buffer, destroy_listener);
1929
1930 wl_signal_emit(&buffer->destroy_signal, buffer);
1931 free(buffer);
1932}
1933
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001934WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001935weston_buffer_from_resource(struct wl_resource *resource)
1936{
1937 struct weston_buffer *buffer;
1938 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001939
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001940 listener = wl_resource_get_destroy_listener(resource,
1941 weston_buffer_destroy_handler);
1942
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001943 if (listener)
1944 return container_of(listener, struct weston_buffer,
1945 destroy_listener);
1946
1947 buffer = zalloc(sizeof *buffer);
1948 if (buffer == NULL)
1949 return NULL;
1950
1951 buffer->resource = resource;
1952 wl_signal_init(&buffer->destroy_signal);
1953 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001954 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001955 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001956
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001957 return buffer;
1958}
1959
1960static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001961weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1962 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001963{
Pekka Paalanende685b82012-12-04 15:58:12 +02001964 struct weston_buffer_reference *ref =
1965 container_of(listener, struct weston_buffer_reference,
1966 destroy_listener);
1967
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001968 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001969 ref->buffer = NULL;
1970}
1971
1972WL_EXPORT void
1973weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001974 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001975{
1976 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001977 ref->buffer->busy_count--;
1978 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001979 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05001980 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05001981 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001982 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001983 }
1984
Pekka Paalanende685b82012-12-04 15:58:12 +02001985 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001986 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001987 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001988 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001989 }
1990
Pekka Paalanende685b82012-12-04 15:58:12 +02001991 ref->buffer = buffer;
1992 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1993}
1994
1995static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001996weston_surface_attach(struct weston_surface *surface,
1997 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001998{
1999 weston_buffer_reference(&surface->buffer_ref, buffer);
2000
Pekka Paalanena6421c42012-12-04 15:58:10 +02002001 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002002 if (weston_surface_is_mapped(surface))
2003 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002004 }
2005
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002006 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002007
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002008 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002009 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002010}
2011
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002012WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002013weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002014{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002015 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002016
2017 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002018 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002019}
2020
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002021WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002022weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002023{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002024 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002025
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002026 pixman_region32_union(&compositor->primary_plane.damage,
2027 &compositor->primary_plane.damage,
2028 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002029 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002030}
2031
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002032static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002033surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002034{
Pekka Paalanende685b82012-12-04 15:58:12 +02002035 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002036 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002037 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002038
Pekka Paalanenb5026542014-11-12 15:09:24 +02002039 if (weston_timeline_enabled_ &&
2040 pixman_region32_not_empty(&surface->damage))
2041 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2042 TLP_OUTPUT(surface->output), TLP_END);
2043
Jason Ekstrandef540082014-06-26 10:37:36 -07002044 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002045}
2046
2047static void
2048view_accumulate_damage(struct weston_view *view,
2049 pixman_region32_t *opaque)
2050{
2051 pixman_region32_t damage;
2052
2053 pixman_region32_init(&damage);
2054 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002055 pixman_box32_t *extents;
2056
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002058 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002059 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060 pixman_region32_copy(&damage, &view->surface->damage);
2061 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002062 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002063 }
2064
Pekka Paalanen380adf52015-02-16 14:39:11 +02002065 pixman_region32_intersect(&damage, &damage,
2066 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002067 pixman_region32_subtract(&damage, &damage, opaque);
2068 pixman_region32_union(&view->plane->damage,
2069 &view->plane->damage, &damage);
2070 pixman_region32_fini(&damage);
2071 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002072 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002073}
2074
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002075static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002076compositor_accumulate_damage(struct weston_compositor *ec)
2077{
2078 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002080 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002081
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002082 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002083
2084 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002085 pixman_region32_copy(&plane->clip, &clip);
2086
2087 pixman_region32_init(&opaque);
2088
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089 wl_list_for_each(ev, &ec->view_list, link) {
2090 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002091 continue;
2092
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002094 }
2095
2096 pixman_region32_union(&clip, &clip, &opaque);
2097 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002098 }
2099
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002100 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002101
Jason Ekstranda7af7042013-10-12 22:38:11 -05002102 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002103 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002104
2105 wl_list_for_each(ev, &ec->view_list, link) {
2106 if (ev->surface->touched)
2107 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002108 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109
2110 surface_flush_damage(ev->surface);
2111
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002112 /* Both the renderer and the backend have seen the buffer
2113 * by now. If renderer needs the buffer, it has its own
2114 * reference set. If the backend wants to keep the buffer
2115 * around for migrating the surface into a non-primary plane
2116 * later, keep_buffer is true. Otherwise, drop the core
2117 * reference now, and allow early buffer release. This enables
2118 * clients to use single-buffering.
2119 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002120 if (!ev->surface->keep_buffer)
2121 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002122 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002123}
2124
2125static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002126surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002127{
2128 struct weston_subsurface *sub;
2129
Pekka Paalanene67858b2013-04-25 13:57:42 +03002130 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002132 continue;
2133
Jason Ekstranda7af7042013-10-12 22:38:11 -05002134 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2135 wl_list_init(&sub->surface->views);
2136
2137 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002138 }
2139}
2140
2141static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002142surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002143{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144 struct weston_subsurface *sub;
2145 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002146
Jason Ekstranda7af7042013-10-12 22:38:11 -05002147 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2148 if (sub->surface == surface)
2149 continue;
2150
George Kiagiadakised04d382014-06-13 18:10:26 +02002151 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2152 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002153 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002154 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002155
2156 surface_free_unused_subsurface_views(sub->surface);
2157 }
2158}
2159
2160static void
2161view_list_add_subsurface_view(struct weston_compositor *compositor,
2162 struct weston_subsurface *sub,
2163 struct weston_view *parent)
2164{
2165 struct weston_subsurface *child;
2166 struct weston_view *view = NULL, *iv;
2167
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002168 if (!weston_surface_is_mapped(sub->surface))
2169 return;
2170
Jason Ekstranda7af7042013-10-12 22:38:11 -05002171 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2172 if (iv->geometry.parent == parent) {
2173 view = iv;
2174 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002175 }
2176 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177
2178 if (view) {
2179 /* Put it back in the surface's list of views */
2180 wl_list_remove(&view->surface_link);
2181 wl_list_insert(&sub->surface->views, &view->surface_link);
2182 } else {
2183 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002184 weston_view_set_position(view,
2185 sub->position.x,
2186 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002187 weston_view_set_transform_parent(view, parent);
2188 }
2189
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002190 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002192 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002193
Pekka Paalanenb188e912013-11-19 14:03:35 +02002194 if (wl_list_empty(&sub->surface->subsurface_list)) {
2195 wl_list_insert(compositor->view_list.prev, &view->link);
2196 return;
2197 }
2198
2199 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2200 if (child->surface == sub->surface)
2201 wl_list_insert(compositor->view_list.prev, &view->link);
2202 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002204 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002205}
2206
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002207/* This recursively adds the sub-surfaces for a view, relying on the
2208 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2209 * change first happens to the sub-surface list, and then automatically
2210 * propagates here. See weston_surface_damage_subsurfaces() for how the
2211 * sub-surfaces receive damage when the client changes the state.
2212 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002213static void
2214view_list_add(struct weston_compositor *compositor,
2215 struct weston_view *view)
2216{
2217 struct weston_subsurface *sub;
2218
2219 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002220
Pekka Paalanenb188e912013-11-19 14:03:35 +02002221 if (wl_list_empty(&view->surface->subsurface_list)) {
2222 wl_list_insert(compositor->view_list.prev, &view->link);
2223 return;
2224 }
2225
2226 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2227 if (sub->surface == view->surface)
2228 wl_list_insert(compositor->view_list.prev, &view->link);
2229 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002231 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002232}
2233
2234static void
2235weston_compositor_build_view_list(struct weston_compositor *compositor)
2236{
2237 struct weston_view *view;
2238 struct weston_layer *layer;
2239
2240 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002241 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002242 surface_stash_subsurface_views(view->surface);
2243
2244 wl_list_init(&compositor->view_list);
2245 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002246 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002247 view_list_add(compositor, view);
2248 }
2249 }
2250
2251 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002252 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002253 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002254}
2255
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002256static void
2257weston_output_take_feedback_list(struct weston_output *output,
2258 struct weston_surface *surface)
2259{
2260 struct weston_view *view;
2261 struct weston_presentation_feedback *feedback;
2262 uint32_t flags = 0xffffffff;
2263
2264 if (wl_list_empty(&surface->feedback_list))
2265 return;
2266
2267 /* All views must have the flag for the flag to survive. */
2268 wl_list_for_each(view, &surface->views, surface_link) {
2269 /* ignore views that are not on this output at all */
2270 if (view->output_mask & (1u << output->id))
2271 flags &= view->psf_flags;
2272 }
2273
2274 wl_list_for_each(feedback, &surface->feedback_list, link)
2275 feedback->psf_flags = flags;
2276
2277 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2278 wl_list_init(&surface->feedback_list);
2279}
2280
David Herrmann1edf44c2013-10-22 17:11:26 +02002281static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002282weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002283{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002284 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002285 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002286 struct weston_animation *animation, *next;
2287 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002288 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002289 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002290 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002291 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002292
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002293 if (output->destroying)
2294 return 0;
2295
Pekka Paalanenb5026542014-11-12 15:09:24 +02002296 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2297
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002298 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002299 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002300
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002301 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002302 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002303 } else {
2304 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002306 ev->psf_flags = 0;
2307 }
2308 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002309
Pekka Paalanene67858b2013-04-25 13:57:42 +03002310 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002311 wl_list_for_each(ev, &ec->view_list, link) {
2312 /* Note: This operation is safe to do multiple times on the
2313 * same surface.
2314 */
2315 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002316 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002317 &ev->surface->frame_callback_list);
2318 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002319
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002320 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002321 }
2322 }
2323
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002324 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002325
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002326 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002327 pixman_region32_intersect(&output_damage,
2328 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002329 pixman_region32_subtract(&output_damage,
2330 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002331
Scott Moreauccbf29d2012-02-22 14:21:41 -07002332 if (output->dirty)
2333 weston_output_update_matrix(output);
2334
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002335 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002336
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002337 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002338
Daniel Stone09a97e22017-03-01 11:34:06 +00002339 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002340 if (r == 0)
2341 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002342
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002343 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002344
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002345 frame_time_msec = timespec_to_msec(&output->frame_time);
2346
Jonas Ådahldb773762012-06-13 00:01:21 +02002347 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002348 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002349 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002350 }
2351
Scott Moreaud64cf212012-06-08 19:40:54 -06002352 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002353 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002354 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002355 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002356
Pekka Paalanenb5026542014-11-12 15:09:24 +02002357 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2358
David Herrmann1edf44c2013-10-22 17:11:26 +02002359 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002360}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002361
Pekka Paalanen82919792014-05-21 13:51:49 +03002362static void
2363weston_output_schedule_repaint_reset(struct weston_output *output)
2364{
Daniel Stone05df8c12017-03-03 16:59:42 +00002365 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002366 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002367}
2368
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002369static int
2370weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2371 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002372{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002373 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002374 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002375 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002376
Daniel Stone6847b852017-03-01 11:34:08 +00002377 /* We're not ready yet; come back to make a decision later. */
2378 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002379 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002380
2381 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2382 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002383 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002384
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002385 /* If we're sleeping, drop the repaint machinery entirely; we will
2386 * explicitly repaint all outputs when we come back. */
2387 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2388 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2389 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002390
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002391 /* We don't actually need to repaint this output; drop it from
2392 * repaint until something causes damage. */
2393 if (!output->repaint_needed)
2394 goto err;
2395
2396 /* If repaint fails, we aren't going to get weston_output_finish_frame
2397 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002398 * something schedules a successful repaint later. As repainting may
2399 * take some time, re-read our clock as a courtesy to the next
2400 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002401 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002402 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002403 if (ret != 0)
2404 goto err;
2405
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002406 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002407
2408err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002409 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002410 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002411}
2412
2413static void
2414output_repaint_timer_arm(struct weston_compositor *compositor)
2415{
2416 struct weston_output *output;
2417 bool any_should_repaint = false;
2418 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002419 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002420
2421 weston_compositor_read_presentation_clock(compositor, &now);
2422
2423 wl_list_for_each(output, &compositor->output_list, link) {
2424 int64_t msec_to_this;
2425
2426 if (output->repaint_status != REPAINT_SCHEDULED)
2427 continue;
2428
2429 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2430 &now);
2431 if (!any_should_repaint || msec_to_this < msec_to_next)
2432 msec_to_next = msec_to_this;
2433
2434 any_should_repaint = true;
2435 }
2436
2437 if (!any_should_repaint)
2438 return;
2439
2440 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2441 * This is a workaround to allow coalescing multiple output repaints
2442 * particularly from weston_output_finish_frame()
2443 * into the same call, which would not happen if we called
2444 * output_repaint_timer_handler() directly.
2445 */
2446 if (msec_to_next < 1)
2447 msec_to_next = 1;
2448
2449 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2450}
2451
2452static int
2453output_repaint_timer_handler(void *data)
2454{
2455 struct weston_compositor *compositor = data;
2456 struct weston_output *output;
2457 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002458 void *repaint_data = NULL;
2459 int ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002460
2461 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002462
2463 if (compositor->backend->repaint_begin)
2464 repaint_data = compositor->backend->repaint_begin(compositor);
2465
2466 wl_list_for_each(output, &compositor->output_list, link) {
2467 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2468 if (ret)
2469 break;
2470 }
2471
2472 if (ret == 0) {
2473 if (compositor->backend->repaint_flush)
2474 compositor->backend->repaint_flush(compositor,
2475 repaint_data);
2476 } else {
2477 if (compositor->backend->repaint_cancel)
2478 compositor->backend->repaint_cancel(compositor,
2479 repaint_data);
2480 }
Daniel Stone6847b852017-03-01 11:34:08 +00002481
2482 output_repaint_timer_arm(compositor);
2483
Pekka Paalanen0513a952014-05-21 16:17:27 +03002484 return 0;
2485}
2486
Kristian Høgsbergef044142011-06-21 15:02:12 -04002487WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002488weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002489 const struct timespec *stamp,
2490 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002491{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002492 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002493 int32_t refresh_nsec;
2494 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002495 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002496
Pekka Paalanenb5026542014-11-12 15:09:24 +02002497 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2498 TLP_VBLANK(stamp), TLP_END);
2499
Daniel Stone05df8c12017-03-03 16:59:42 +00002500 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002501 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2502
Daniel Stone6847b852017-03-01 11:34:08 +00002503 weston_compositor_read_presentation_clock(compositor, &now);
2504
Daniel Stone3615ce12017-03-01 11:34:05 +00002505 /* If we haven't been supplied any timestamp at all, we don't have a
2506 * timebase to work against, so any delay just wastes time. Push a
2507 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002508 if (!stamp) {
2509 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002510 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002511 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002512
Pekka Paalanend7894d02015-07-03 15:08:53 +03002513 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002514 weston_presentation_feedback_present_list(&output->feedback_list,
2515 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002516 output->msc,
2517 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002518
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002519 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002520
Daniel Stone6847b852017-03-01 11:34:08 +00002521 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2522 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2523 -compositor->repaint_msec);
2524 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002525
2526 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002527 static bool warned;
2528
2529 if (!warned)
2530 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002531 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002532 warned = true;
2533
Daniel Stone6847b852017-03-01 11:34:08 +00002534 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002535 }
2536
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002537 /* Called from restart_repaint_loop and restart happens already after
2538 * the deadline given by repaint_msec? In that case we delay until
2539 * the deadline of the next frame, to give clients a more predictable
2540 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002541 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2542 msec_rel < 0) {
2543 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2544 timespec_add_nsec(&output->next_repaint,
2545 &output->next_repaint,
2546 refresh_nsec);
2547 }
2548 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002549
Daniel Stone3615ce12017-03-01 11:34:05 +00002550out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002551 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002552 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002553}
2554
2555static void
2556idle_repaint(void *data)
2557{
2558 struct weston_output *output = data;
2559
Daniel Stone05df8c12017-03-03 16:59:42 +00002560 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2561 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002562 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002563}
2564
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002565WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002566weston_layer_entry_insert(struct weston_layer_entry *list,
2567 struct weston_layer_entry *entry)
2568{
2569 wl_list_insert(&list->link, &entry->link);
2570 entry->layer = list->layer;
2571}
2572
2573WL_EXPORT void
2574weston_layer_entry_remove(struct weston_layer_entry *entry)
2575{
2576 wl_list_remove(&entry->link);
2577 wl_list_init(&entry->link);
2578 entry->layer = NULL;
2579}
2580
Quentin Glidic82681572016-12-17 13:40:51 +01002581
2582/** Initialize the weston_layer struct.
2583 *
2584 * \param compositor The compositor instance
2585 * \param layer The layer to initialize
2586 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002587WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002588weston_layer_init(struct weston_layer *layer,
2589 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002590{
Quentin Glidic82681572016-12-17 13:40:51 +01002591 layer->compositor = compositor;
2592 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002593 wl_list_init(&layer->view_list.link);
2594 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002595 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002596}
2597
2598/** Sets the position of the layer in the layer list. The layer will be placed
2599 * below any layer with the same position value, if any.
2600 * This function is safe to call if the layer is already on the list, but the
2601 * layer may be moved below other layers at the same position, if any.
2602 *
2603 * \param layer The layer to modify
2604 * \param position The position the layer will be placed at
2605 */
2606WL_EXPORT void
2607weston_layer_set_position(struct weston_layer *layer,
2608 enum weston_layer_position position)
2609{
2610 struct weston_layer *below;
2611
2612 wl_list_remove(&layer->link);
2613
2614 /* layer_list is ordered from top to bottom, the last layer being the
2615 * background with the smallest position value */
2616
2617 layer->position = position;
2618 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2619 if (below->position >= layer->position) {
2620 wl_list_insert(&below->link, &layer->link);
2621 return;
2622 }
2623 }
2624 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2625}
2626
2627/** Hide a layer by taking it off the layer list.
2628 * This function is safe to call if the layer is not on the list.
2629 *
2630 * \param layer The layer to hide
2631 */
2632WL_EXPORT void
2633weston_layer_unset_position(struct weston_layer *layer)
2634{
2635 wl_list_remove(&layer->link);
2636 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002637}
2638
2639WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002640weston_layer_set_mask(struct weston_layer *layer,
2641 int x, int y, int width, int height)
2642{
2643 struct weston_view *view;
2644
2645 layer->mask.x1 = x;
2646 layer->mask.x2 = x + width;
2647 layer->mask.y1 = y;
2648 layer->mask.y2 = y + height;
2649
2650 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2651 weston_view_geometry_dirty(view);
2652 }
2653}
2654
2655WL_EXPORT void
2656weston_layer_set_mask_infinite(struct weston_layer *layer)
2657{
2658 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2659 UINT32_MAX, UINT32_MAX);
2660}
2661
2662WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002663weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002664{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002665 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002666 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002667
Bryce Harrington08976ac2016-08-30 12:05:16 -07002668 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2669 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002670 return;
2671
Pekka Paalanenb5026542014-11-12 15:09:24 +02002672 if (!output->repaint_needed)
2673 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2674
Kristian Høgsbergef044142011-06-21 15:02:12 -04002675 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002676 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002677
2678 /* If we already have a repaint scheduled for our idle handler,
2679 * no need to set it again. If the repaint has been called but
2680 * not finished, then weston_output_finish_frame() will notice
2681 * that a repaint is needed and schedule one. */
2682 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002683 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002684
Daniel Stone05df8c12017-03-03 16:59:42 +00002685 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002686 wl_event_loop_add_idle(loop, idle_repaint, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002687 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002688}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002689
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002690WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002691weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2692{
2693 struct weston_output *output;
2694
2695 wl_list_for_each(output, &compositor->output_list, link)
2696 weston_output_schedule_repaint(output);
2697}
2698
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002699static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002700surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002701{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002702 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002703}
2704
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002705static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002706surface_attach(struct wl_client *client,
2707 struct wl_resource *resource,
2708 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2709{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002710 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002711 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002712
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002713 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002714 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002715 if (buffer == NULL) {
2716 wl_client_post_no_memory(client);
2717 return;
2718 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002719 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002720
Pekka Paalanende685b82012-12-04 15:58:12 +02002721 /* Attach, attach, without commit in between does not send
2722 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002723 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002724
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002725 surface->pending.sx = sx;
2726 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002727 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002728}
2729
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002730static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002731surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002732 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002733 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002734{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002735 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002736
Derek Foreman57e92ed2015-11-17 14:11:35 -06002737 if (width <= 0 || height <= 0)
2738 return;
2739
Derek Foreman152254b2015-11-26 14:17:48 -06002740 pixman_region32_union_rect(&surface->pending.damage_surface,
2741 &surface->pending.damage_surface,
2742 x, y, width, height);
2743}
2744
2745static void
2746surface_damage_buffer(struct wl_client *client,
2747 struct wl_resource *resource,
2748 int32_t x, int32_t y, int32_t width, int32_t height)
2749{
2750 struct weston_surface *surface = wl_resource_get_user_data(resource);
2751
2752 if (width <= 0 || height <= 0)
2753 return;
2754
2755 pixman_region32_union_rect(&surface->pending.damage_buffer,
2756 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002757 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002758}
2759
Kristian Høgsberg33418202011-08-16 23:01:28 -04002760static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002761destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002762{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002763 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002764
2765 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002766 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002767}
2768
2769static void
2770surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002771 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002772{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002773 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002774 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002775
2776 cb = malloc(sizeof *cb);
2777 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002778 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002779 return;
2780 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002781
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002782 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2783 callback);
2784 if (cb->resource == NULL) {
2785 free(cb);
2786 wl_resource_post_no_memory(resource);
2787 return;
2788 }
2789
Jason Ekstranda85118c2013-06-27 20:17:02 -05002790 wl_resource_set_implementation(cb->resource, NULL, cb,
2791 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002792
Pekka Paalanenbc106382012-10-10 12:49:31 +03002793 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002794}
2795
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002796static void
2797surface_set_opaque_region(struct wl_client *client,
2798 struct wl_resource *resource,
2799 struct wl_resource *region_resource)
2800{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002801 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002802 struct weston_region *region;
2803
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002804 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002805 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002806 pixman_region32_copy(&surface->pending.opaque,
2807 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002808 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002809 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002810 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002811}
2812
2813static void
2814surface_set_input_region(struct wl_client *client,
2815 struct wl_resource *resource,
2816 struct wl_resource *region_resource)
2817{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002818 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002819 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002820
2821 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002822 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002823 pixman_region32_copy(&surface->pending.input,
2824 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002825 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002826 pixman_region32_fini(&surface->pending.input);
2827 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002828 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002829}
2830
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002831/* Cause damage to this sub-surface and all its children.
2832 *
2833 * This is useful when there are state changes that need an implicit
2834 * damage, e.g. a z-order change.
2835 */
2836static void
2837weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2838{
2839 struct weston_subsurface *child;
2840
2841 weston_surface_damage(sub->surface);
2842 sub->reordered = false;
2843
2844 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2845 if (child != sub)
2846 weston_surface_damage_subsurfaces(child);
2847}
2848
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002849static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002850weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002851{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002852 struct weston_subsurface *sub;
2853
2854 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2855 parent_link_pending) {
2856 wl_list_remove(&sub->parent_link);
2857 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002858
2859 if (sub->reordered)
2860 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002861 }
2862}
2863
2864static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002865weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002866 struct weston_matrix *matrix)
2867{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002868 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002869 double src_width, src_height, dest_width, dest_height;
2870
2871 weston_matrix_init(matrix);
2872
2873 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2874 src_width = surface->width_from_buffer;
2875 src_height = surface->height_from_buffer;
2876 } else {
2877 src_width = wl_fixed_to_double(vp->buffer.src_width);
2878 src_height = wl_fixed_to_double(vp->buffer.src_height);
2879 }
2880
2881 if (vp->surface.width == -1) {
2882 dest_width = src_width;
2883 dest_height = src_height;
2884 } else {
2885 dest_width = vp->surface.width;
2886 dest_height = vp->surface.height;
2887 }
2888
2889 if (src_width != dest_width || src_height != dest_height)
2890 weston_matrix_scale(matrix,
2891 src_width / dest_width,
2892 src_height / dest_height, 1);
2893
2894 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2895 weston_matrix_translate(matrix,
2896 wl_fixed_to_double(vp->buffer.src_x),
2897 wl_fixed_to_double(vp->buffer.src_y),
2898 0);
2899
2900 switch (vp->buffer.transform) {
2901 case WL_OUTPUT_TRANSFORM_FLIPPED:
2902 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2903 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2904 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2905 weston_matrix_scale(matrix, -1, 1, 1);
2906 weston_matrix_translate(matrix,
2907 surface->width_from_buffer, 0, 0);
2908 break;
2909 }
2910
2911 switch (vp->buffer.transform) {
2912 default:
2913 case WL_OUTPUT_TRANSFORM_NORMAL:
2914 case WL_OUTPUT_TRANSFORM_FLIPPED:
2915 break;
2916 case WL_OUTPUT_TRANSFORM_90:
2917 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2918 weston_matrix_rotate_xy(matrix, 0, 1);
2919 weston_matrix_translate(matrix,
2920 surface->height_from_buffer, 0, 0);
2921 break;
2922 case WL_OUTPUT_TRANSFORM_180:
2923 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2924 weston_matrix_rotate_xy(matrix, -1, 0);
2925 weston_matrix_translate(matrix,
2926 surface->width_from_buffer,
2927 surface->height_from_buffer, 0);
2928 break;
2929 case WL_OUTPUT_TRANSFORM_270:
2930 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2931 weston_matrix_rotate_xy(matrix, 0, -1);
2932 weston_matrix_translate(matrix,
2933 0, surface->width_from_buffer, 0);
2934 break;
2935 }
2936
2937 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2938}
2939
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002940/**
2941 * Compute a + b > c while being safe to overflows.
2942 */
2943static bool
2944fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2945{
2946 return (int64_t)a + (int64_t)b > (int64_t)c;
2947}
2948
2949static bool
2950weston_surface_is_pending_viewport_source_valid(
2951 const struct weston_surface *surface)
2952{
2953 const struct weston_surface_state *pend = &surface->pending;
2954 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2955 int width_from_buffer = 0;
2956 int height_from_buffer = 0;
2957 wl_fixed_t w;
2958 wl_fixed_t h;
2959
2960 /* If viewport source rect is not set, it is always ok. */
2961 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2962 return true;
2963
2964 if (pend->newly_attached) {
2965 if (pend->buffer) {
2966 convert_size_by_transform_scale(&width_from_buffer,
2967 &height_from_buffer,
2968 pend->buffer->width,
2969 pend->buffer->height,
2970 vp->buffer.transform,
2971 vp->buffer.scale);
2972 } else {
2973 /* No buffer: viewport is irrelevant. */
2974 return true;
2975 }
2976 } else {
2977 width_from_buffer = surface->width_from_buffer;
2978 height_from_buffer = surface->height_from_buffer;
2979 }
2980
2981 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2982 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2983
2984 /* No buffer: viewport is irrelevant. */
2985 if (width_from_buffer == 0 || height_from_buffer == 0)
2986 return true;
2987
2988 /* overflow checks for wl_fixed_from_int() */
2989 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2990 return false;
2991 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2992 return false;
2993
2994 w = wl_fixed_from_int(width_from_buffer);
2995 h = wl_fixed_from_int(height_from_buffer);
2996
2997 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2998 return false;
2999 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3000 return false;
3001
3002 return true;
3003}
3004
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003005static bool
3006fixed_is_integer(wl_fixed_t v)
3007{
3008 return (v & 0xff) == 0;
3009}
3010
3011static bool
3012weston_surface_is_pending_viewport_dst_size_int(
3013 const struct weston_surface *surface)
3014{
3015 const struct weston_buffer_viewport *vp =
3016 &surface->pending.buffer_viewport;
3017
3018 if (vp->surface.width != -1) {
3019 assert(vp->surface.width > 0 && vp->surface.height > 0);
3020 return true;
3021 }
3022
3023 return fixed_is_integer(vp->buffer.src_width) &&
3024 fixed_is_integer(vp->buffer.src_height);
3025}
3026
Derek Foreman152254b2015-11-26 14:17:48 -06003027/* Translate pending damage in buffer co-ordinates to surface
3028 * co-ordinates and union it with a pixman_region32_t.
3029 * This should only be called after the buffer is attached.
3030 */
3031static void
3032apply_damage_buffer(pixman_region32_t *dest,
3033 struct weston_surface *surface,
3034 struct weston_surface_state *state)
3035{
3036 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3037
3038 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3039 * translated into surface co-ordinates and unioned with
3040 * any other surface damage.
3041 * None of this makes sense if there is no buffer though.
3042 */
3043 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3044 pixman_region32_t buffer_damage;
3045
3046 pixman_region32_intersect_rect(&state->damage_buffer,
3047 &state->damage_buffer,
3048 0, 0, buffer->width,
3049 buffer->height);
3050 pixman_region32_init(&buffer_damage);
3051 weston_matrix_transform_region(&buffer_damage,
3052 &surface->buffer_to_surface_matrix,
3053 &state->damage_buffer);
3054 pixman_region32_union(dest, dest, &buffer_damage);
3055 pixman_region32_fini(&buffer_damage);
3056 }
3057 /* We should clear this on commit even if there was no buffer */
3058 pixman_region32_clear(&state->damage_buffer);
3059}
3060
Jason Ekstrand1e059042014-10-16 10:55:19 -05003061static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003062weston_surface_commit_state(struct weston_surface *surface,
3063 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003064{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003065 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003066 pixman_region32_t opaque;
3067
Alexander Larsson4ea95522013-05-22 14:41:37 +02003068 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003069 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003070 /* wp_viewport.set_source */
3071 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003072 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003073
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003074 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003075 if (state->newly_attached)
3076 weston_surface_attach(surface, state->buffer);
3077 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003078
Jason Ekstrand1e059042014-10-16 10:55:19 -05003079 weston_surface_build_buffer_matrix(surface,
3080 &surface->surface_to_buffer_matrix);
3081 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3082 &surface->surface_to_buffer_matrix);
3083
Jason Ekstrand7b982072014-05-20 14:33:03 -05003084 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003085 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003086 if (surface->committed)
3087 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003088 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003089
Jason Ekstrand7b982072014-05-20 14:33:03 -05003090 state->sx = 0;
3091 state->sy = 0;
3092 state->newly_attached = 0;
3093 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003094
Derek Foreman152254b2015-11-26 14:17:48 -06003095 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003096 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003097 (pixman_region32_not_empty(&state->damage_surface) ||
3098 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003099 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003100
Pekka Paalanen8e159182012-10-10 12:49:25 +03003101 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003102 &state->damage_surface);
3103
3104 apply_damage_buffer(&surface->damage, surface, state);
3105
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003106 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003107 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003108 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003109
3110 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003111 pixman_region32_init(&opaque);
3112 pixman_region32_intersect_rect(&opaque, &state->opaque,
3113 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003114
3115 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3116 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003117 wl_list_for_each(view, &surface->views, surface_link)
3118 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003119 }
3120
3121 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003122
3123 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003124 pixman_region32_intersect_rect(&surface->input, &state->input,
3125 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003126
Pekka Paalanenbc106382012-10-10 12:49:31 +03003127 /* wl_surface.frame */
3128 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003129 &state->frame_callback_list);
3130 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003131
3132 /* XXX:
3133 * What should happen with a feedback request, if there
3134 * is no wl_buffer attached for this commit?
3135 */
3136
3137 /* presentation.feedback */
3138 wl_list_insert_list(&surface->feedback_list,
3139 &state->feedback_list);
3140 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003141
3142 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003143}
3144
3145static void
3146weston_surface_commit(struct weston_surface *surface)
3147{
3148 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003149
Pekka Paalanene67858b2013-04-25 13:57:42 +03003150 weston_surface_commit_subsurface_order(surface);
3151
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003152 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003153}
3154
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003155static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003156weston_subsurface_commit(struct weston_subsurface *sub);
3157
3158static void
3159weston_subsurface_parent_commit(struct weston_subsurface *sub,
3160 int parent_is_synchronized);
3161
3162static void
3163surface_commit(struct wl_client *client, struct wl_resource *resource)
3164{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003165 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003166 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3167
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003168 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3169 assert(surface->viewport_resource);
3170
3171 wl_resource_post_error(surface->viewport_resource,
3172 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3173 "wl_surface@%d has viewport source outside buffer",
3174 wl_resource_get_id(resource));
3175 return;
3176 }
3177
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003178 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3179 assert(surface->viewport_resource);
3180
3181 wl_resource_post_error(surface->viewport_resource,
3182 WP_VIEWPORT_ERROR_BAD_SIZE,
3183 "wl_surface@%d viewport dst size not integer",
3184 wl_resource_get_id(resource));
3185 return;
3186 }
3187
Pekka Paalanene67858b2013-04-25 13:57:42 +03003188 if (sub) {
3189 weston_subsurface_commit(sub);
3190 return;
3191 }
3192
3193 weston_surface_commit(surface);
3194
3195 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3196 if (sub->surface != surface)
3197 weston_subsurface_parent_commit(sub, 0);
3198 }
3199}
3200
3201static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003202surface_set_buffer_transform(struct wl_client *client,
3203 struct wl_resource *resource, int transform)
3204{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003205 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003206
Jonny Lamba55f1392014-05-30 12:07:15 +02003207 /* if wl_output.transform grows more members this will need to be updated. */
3208 if (transform < 0 ||
3209 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3210 wl_resource_post_error(resource,
3211 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3212 "buffer transform must be a valid transform "
3213 "('%d' specified)", transform);
3214 return;
3215 }
3216
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003217 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003218 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003219}
3220
Alexander Larsson4ea95522013-05-22 14:41:37 +02003221static void
3222surface_set_buffer_scale(struct wl_client *client,
3223 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003224 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003225{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003226 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003227
Jonny Lamba55f1392014-05-30 12:07:15 +02003228 if (scale < 1) {
3229 wl_resource_post_error(resource,
3230 WL_SURFACE_ERROR_INVALID_SCALE,
3231 "buffer scale must be at least one "
3232 "('%d' specified)", scale);
3233 return;
3234 }
3235
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003236 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003237 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003238}
3239
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003240static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003241 surface_destroy,
3242 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003243 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003244 surface_frame,
3245 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003246 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003247 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003248 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003249 surface_set_buffer_scale,
3250 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003251};
3252
3253static void
3254compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003255 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003256{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003257 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003258 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003259
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003260 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003261 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003262 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003263 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003264 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003265
Jason Ekstranda85118c2013-06-27 20:17:02 -05003266 surface->resource =
3267 wl_resource_create(client, &wl_surface_interface,
3268 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003269 if (surface->resource == NULL) {
3270 weston_surface_destroy(surface);
3271 wl_resource_post_no_memory(resource);
3272 return;
3273 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003274 wl_resource_set_implementation(surface->resource, &surface_interface,
3275 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003276
3277 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003278}
3279
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003280static void
3281destroy_region(struct wl_resource *resource)
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
3285 pixman_region32_fini(&region->region);
3286 free(region);
3287}
3288
3289static void
3290region_destroy(struct wl_client *client, struct wl_resource *resource)
3291{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003292 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003293}
3294
3295static void
3296region_add(struct wl_client *client, struct wl_resource *resource,
3297 int32_t x, int32_t y, int32_t width, int32_t height)
3298{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003299 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003300
3301 pixman_region32_union_rect(&region->region, &region->region,
3302 x, y, width, height);
3303}
3304
3305static void
3306region_subtract(struct wl_client *client, struct wl_resource *resource,
3307 int32_t x, int32_t y, int32_t width, int32_t height)
3308{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003309 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003310 pixman_region32_t rect;
3311
3312 pixman_region32_init_rect(&rect, x, y, width, height);
3313 pixman_region32_subtract(&region->region, &region->region, &rect);
3314 pixman_region32_fini(&rect);
3315}
3316
3317static const struct wl_region_interface region_interface = {
3318 region_destroy,
3319 region_add,
3320 region_subtract
3321};
3322
3323static void
3324compositor_create_region(struct wl_client *client,
3325 struct wl_resource *resource, uint32_t id)
3326{
3327 struct weston_region *region;
3328
3329 region = malloc(sizeof *region);
3330 if (region == NULL) {
3331 wl_resource_post_no_memory(resource);
3332 return;
3333 }
3334
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003335 pixman_region32_init(&region->region);
3336
Jason Ekstranda85118c2013-06-27 20:17:02 -05003337 region->resource =
3338 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003339 if (region->resource == NULL) {
3340 free(region);
3341 wl_resource_post_no_memory(resource);
3342 return;
3343 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003344 wl_resource_set_implementation(region->resource, &region_interface,
3345 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003346}
3347
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003348static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003349 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003350 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003351};
3352
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003353static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003354weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3355{
3356 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003357
Jason Ekstrand7b982072014-05-20 14:33:03 -05003358 weston_surface_commit_state(surface, &sub->cached);
3359 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003360
3361 weston_surface_commit_subsurface_order(surface);
3362
3363 weston_surface_schedule_repaint(surface);
3364
Jason Ekstrand7b982072014-05-20 14:33:03 -05003365 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003366}
3367
3368static void
3369weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3370{
3371 struct weston_surface *surface = sub->surface;
3372
3373 /*
3374 * If this commit would cause the surface to move by the
3375 * attach(dx, dy) parameters, the old damage region must be
3376 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003377 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003378 */
Derek Foreman152254b2015-11-26 14:17:48 -06003379 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003380 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003381 pixman_region32_union(&sub->cached.damage_surface,
3382 &sub->cached.damage_surface,
3383 &surface->pending.damage_surface);
3384 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003385
3386 if (surface->pending.newly_attached) {
3387 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003388 weston_surface_state_set_buffer(&sub->cached,
3389 surface->pending.buffer);
3390 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003391 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003392 weston_presentation_feedback_discard_list(
3393 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003394 }
3395 sub->cached.sx += surface->pending.sx;
3396 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003397
Derek Foreman152254b2015-11-26 14:17:48 -06003398 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3399
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003400 sub->cached.buffer_viewport.changed |=
3401 surface->pending.buffer_viewport.changed;
3402 sub->cached.buffer_viewport.buffer =
3403 surface->pending.buffer_viewport.buffer;
3404 sub->cached.buffer_viewport.surface =
3405 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003406
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003407 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003408
3409 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3410
3411 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3412
3413 wl_list_insert_list(&sub->cached.frame_callback_list,
3414 &surface->pending.frame_callback_list);
3415 wl_list_init(&surface->pending.frame_callback_list);
3416
Pekka Paalanen133e4392014-09-23 22:08:46 -04003417 wl_list_insert_list(&sub->cached.feedback_list,
3418 &surface->pending.feedback_list);
3419 wl_list_init(&surface->pending.feedback_list);
3420
Jason Ekstrand7b982072014-05-20 14:33:03 -05003421 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003422}
3423
Derek Foreman280e7dd2014-10-03 13:13:42 -05003424static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003425weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3426{
3427 while (sub) {
3428 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003429 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003430
3431 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003432 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003433
3434 sub = weston_surface_to_subsurface(sub->parent);
3435 }
3436
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003437 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003438}
3439
3440static void
3441weston_subsurface_commit(struct weston_subsurface *sub)
3442{
3443 struct weston_surface *surface = sub->surface;
3444 struct weston_subsurface *tmp;
3445
3446 /* Recursive check for effectively synchronized. */
3447 if (weston_subsurface_is_synchronized(sub)) {
3448 weston_subsurface_commit_to_cache(sub);
3449 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003450 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003451 /* flush accumulated state from cache */
3452 weston_subsurface_commit_to_cache(sub);
3453 weston_subsurface_commit_from_cache(sub);
3454 } else {
3455 weston_surface_commit(surface);
3456 }
3457
3458 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3459 if (tmp->surface != surface)
3460 weston_subsurface_parent_commit(tmp, 0);
3461 }
3462 }
3463}
3464
3465static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003466weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003467{
3468 struct weston_surface *surface = sub->surface;
3469 struct weston_subsurface *tmp;
3470
Pekka Paalanene67858b2013-04-25 13:57:42 +03003471 /* From now on, commit_from_cache the whole sub-tree, regardless of
3472 * the synchronized mode of each child. This sub-surface or some
3473 * of its ancestors were synchronized, so we are synchronized
3474 * all the way down.
3475 */
3476
Jason Ekstrand7b982072014-05-20 14:33:03 -05003477 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003478 weston_subsurface_commit_from_cache(sub);
3479
3480 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3481 if (tmp->surface != surface)
3482 weston_subsurface_parent_commit(tmp, 1);
3483 }
3484}
3485
3486static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003487weston_subsurface_parent_commit(struct weston_subsurface *sub,
3488 int parent_is_synchronized)
3489{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003490 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003491 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003492 wl_list_for_each(view, &sub->surface->views, surface_link)
3493 weston_view_set_position(view,
3494 sub->position.x,
3495 sub->position.y);
3496
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003497 sub->position.set = 0;
3498 }
3499
3500 if (parent_is_synchronized || sub->synchronized)
3501 weston_subsurface_synchronized_commit(sub);
3502}
3503
Pekka Paalanen8274d902014-08-06 19:36:51 +03003504static int
3505subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3506{
3507 return snprintf(buf, len, "sub-surface");
3508}
3509
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003510static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003511subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003512{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003513 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003514
Jason Ekstranda7af7042013-10-12 22:38:11 -05003515 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003516 weston_view_set_position(view,
3517 view->geometry.x + dx,
3518 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003519
3520 /* No need to check parent mappedness, because if parent is not
3521 * mapped, parent is not in a visible layer, so this sub-surface
3522 * will not be drawn either.
3523 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003524
Pekka Paalanene67858b2013-04-25 13:57:42 +03003525 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003526 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003527
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003528 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003529 * because that would call it also for the parent surface,
3530 * which might not be mapped yet. That would lead to
3531 * inconsistent state, where the window could never be
3532 * mapped.
3533 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003534 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003535 * weston_surface_is_mapped() return true, so that when the
3536 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003537 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003538 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003539 }
3540}
3541
3542static struct weston_subsurface *
3543weston_surface_to_subsurface(struct weston_surface *surface)
3544{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003545 if (surface->committed == subsurface_committed)
3546 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003547
3548 return NULL;
3549}
3550
Pekka Paalanen01388e22013-04-25 13:57:44 +03003551WL_EXPORT struct weston_surface *
3552weston_surface_get_main_surface(struct weston_surface *surface)
3553{
3554 struct weston_subsurface *sub;
3555
3556 while (surface && (sub = weston_surface_to_subsurface(surface)))
3557 surface = sub->parent;
3558
3559 return surface;
3560}
3561
Pekka Paalanen50b67472014-10-01 15:02:41 +03003562WL_EXPORT int
3563weston_surface_set_role(struct weston_surface *surface,
3564 const char *role_name,
3565 struct wl_resource *error_resource,
3566 uint32_t error_code)
3567{
3568 assert(role_name);
3569
3570 if (surface->role_name == NULL ||
3571 surface->role_name == role_name ||
3572 strcmp(surface->role_name, role_name) == 0) {
3573 surface->role_name = role_name;
3574
3575 return 0;
3576 }
3577
3578 wl_resource_post_error(error_resource, error_code,
3579 "Cannot assign role %s to wl_surface@%d,"
3580 " already has role %s\n",
3581 role_name,
3582 wl_resource_get_id(surface->resource),
3583 surface->role_name);
3584 return -1;
3585}
3586
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003587WL_EXPORT const char *
3588weston_surface_get_role(struct weston_surface *surface)
3589{
3590 return surface->role_name;
3591}
3592
Pekka Paalanen8274d902014-08-06 19:36:51 +03003593WL_EXPORT void
3594weston_surface_set_label_func(struct weston_surface *surface,
3595 int (*desc)(struct weston_surface *,
3596 char *, size_t))
3597{
3598 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003599 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003600}
3601
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003602/** Get the size of surface contents
3603 *
3604 * \param surface The surface to query.
3605 * \param width Returns the width of raw contents.
3606 * \param height Returns the height of raw contents.
3607 *
3608 * Retrieves the raw surface content size in pixels for the given surface.
3609 * This is the whole content size in buffer pixels. If the surface
3610 * has no content or the renderer does not implement this feature,
3611 * zeroes are returned.
3612 *
3613 * This function is used to determine the buffer size needed for
3614 * a weston_surface_copy_content() call.
3615 */
3616WL_EXPORT void
3617weston_surface_get_content_size(struct weston_surface *surface,
3618 int *width, int *height)
3619{
3620 struct weston_renderer *rer = surface->compositor->renderer;
3621
3622 if (!rer->surface_get_content_size) {
3623 *width = 0;
3624 *height = 0;
3625 return;
3626 }
3627
3628 rer->surface_get_content_size(surface, width, height);
3629}
3630
Quentin Glidic248dd102016-08-12 10:41:34 +02003631/** Get the bounding box of a surface and its subsurfaces
3632 *
3633 * \param surface The surface to query.
3634 * \return The bounding box relative to the surface origin.
3635 *
3636 */
3637WL_EXPORT struct weston_geometry
3638weston_surface_get_bounding_box(struct weston_surface *surface)
3639{
3640 pixman_region32_t region;
3641 pixman_box32_t *box;
3642 struct weston_subsurface *subsurface;
3643
3644 pixman_region32_init_rect(&region,
3645 0, 0,
3646 surface->width, surface->height);
3647
3648 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3649 pixman_region32_union_rect(&region, &region,
3650 subsurface->position.x,
3651 subsurface->position.y,
3652 subsurface->surface->width,
3653 subsurface->surface->height);
3654
3655 box = pixman_region32_extents(&region);
3656 struct weston_geometry geometry = {
3657 .x = box->x1,
3658 .y = box->y1,
3659 .width = box->x2 - box->x1,
3660 .height = box->y2 - box->y1,
3661 };
3662
3663 pixman_region32_fini(&region);
3664
3665 return geometry;
3666}
3667
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003668/** Copy surface contents to system memory.
3669 *
3670 * \param surface The surface to copy from.
3671 * \param target Pointer to the target memory buffer.
3672 * \param size Size of the target buffer in bytes.
3673 * \param src_x X location on contents to copy from.
3674 * \param src_y Y location on contents to copy from.
3675 * \param width Width in pixels of the area to copy.
3676 * \param height Height in pixels of the area to copy.
3677 * \return 0 for success, -1 for failure.
3678 *
3679 * Surface contents are maintained by the renderer. They can be in a
3680 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3681 * else.
3682 *
3683 * Surface contents are copied into memory pointed to by target,
3684 * which has size bytes of space available. The target memory
3685 * may be larger than needed, but being smaller returns an error.
3686 * The extra bytes in target may or may not be written; their content is
3687 * unspecified. Size must be large enough to hold the image.
3688 *
3689 * The image in the target memory will be arranged in rows from
3690 * top to bottom, and pixels on a row from left to right. The pixel
3691 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3692 * width * 4.
3693 *
3694 * Parameters src_x and src_y define the upper-left corner in buffer
3695 * coordinates (pixels) to copy from. Parameters width and height
3696 * define the size of the area to copy in pixels.
3697 *
3698 * The rectangle defined by src_x, src_y, width, height must fit in
3699 * the surface contents. Otherwise an error is returned.
3700 *
3701 * Use surface_get_data_size to determine the content size; the
3702 * needed target buffer size and rectangle limits.
3703 *
3704 * CURRENT IMPLEMENTATION RESTRICTIONS:
3705 * - the machine must be little-endian due to Pixman formats.
3706 *
3707 * NOTE: Pixman formats are premultiplied.
3708 */
3709WL_EXPORT int
3710weston_surface_copy_content(struct weston_surface *surface,
3711 void *target, size_t size,
3712 int src_x, int src_y,
3713 int width, int height)
3714{
3715 struct weston_renderer *rer = surface->compositor->renderer;
3716 int cw, ch;
3717 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3718
3719 if (!rer->surface_copy_content)
3720 return -1;
3721
3722 weston_surface_get_content_size(surface, &cw, &ch);
3723
3724 if (src_x < 0 || src_y < 0)
3725 return -1;
3726
3727 if (width <= 0 || height <= 0)
3728 return -1;
3729
3730 if (src_x + width > cw || src_y + height > ch)
3731 return -1;
3732
3733 if (width * bytespp * height > size)
3734 return -1;
3735
3736 return rer->surface_copy_content(surface, target, size,
3737 src_x, src_y, width, height);
3738}
3739
Pekka Paalanene67858b2013-04-25 13:57:42 +03003740static void
3741subsurface_set_position(struct wl_client *client,
3742 struct wl_resource *resource, int32_t x, int32_t y)
3743{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003744 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003745
3746 if (!sub)
3747 return;
3748
3749 sub->position.x = x;
3750 sub->position.y = y;
3751 sub->position.set = 1;
3752}
3753
3754static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003755subsurface_find_sibling(struct weston_subsurface *sub,
3756 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003757{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003758 struct weston_surface *parent = sub->parent;
3759 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003760
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003761 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3762 if (sibling->surface == surface && sibling != sub)
3763 return sibling;
3764 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003765
3766 return NULL;
3767}
3768
3769static struct weston_subsurface *
3770subsurface_sibling_check(struct weston_subsurface *sub,
3771 struct weston_surface *surface,
3772 const char *request)
3773{
3774 struct weston_subsurface *sibling;
3775
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003776 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003777 if (!sibling) {
3778 wl_resource_post_error(sub->resource,
3779 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3780 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003781 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003782 return NULL;
3783 }
3784
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003785 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003786
3787 return sibling;
3788}
3789
3790static void
3791subsurface_place_above(struct wl_client *client,
3792 struct wl_resource *resource,
3793 struct wl_resource *sibling_resource)
3794{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003795 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003796 struct weston_surface *surface =
3797 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003798 struct weston_subsurface *sibling;
3799
3800 if (!sub)
3801 return;
3802
3803 sibling = subsurface_sibling_check(sub, surface, "place_above");
3804 if (!sibling)
3805 return;
3806
3807 wl_list_remove(&sub->parent_link_pending);
3808 wl_list_insert(sibling->parent_link_pending.prev,
3809 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003810
3811 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003812}
3813
3814static void
3815subsurface_place_below(struct wl_client *client,
3816 struct wl_resource *resource,
3817 struct wl_resource *sibling_resource)
3818{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003819 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003820 struct weston_surface *surface =
3821 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003822 struct weston_subsurface *sibling;
3823
3824 if (!sub)
3825 return;
3826
3827 sibling = subsurface_sibling_check(sub, surface, "place_below");
3828 if (!sibling)
3829 return;
3830
3831 wl_list_remove(&sub->parent_link_pending);
3832 wl_list_insert(&sibling->parent_link_pending,
3833 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003834
3835 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003836}
3837
3838static void
3839subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3840{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003841 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003842
3843 if (sub)
3844 sub->synchronized = 1;
3845}
3846
3847static void
3848subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3849{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003850 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003851
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003852 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003853 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003854
3855 /* If sub became effectively desynchronized, flush. */
3856 if (!weston_subsurface_is_synchronized(sub))
3857 weston_subsurface_synchronized_commit(sub);
3858 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003859}
3860
3861static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003862weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3863{
3864 wl_list_remove(&sub->parent_link);
3865 wl_list_remove(&sub->parent_link_pending);
3866 wl_list_remove(&sub->parent_destroy_listener.link);
3867 sub->parent = NULL;
3868}
3869
3870static void
3871weston_subsurface_destroy(struct weston_subsurface *sub);
3872
3873static void
3874subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3875{
3876 struct weston_subsurface *sub =
3877 container_of(listener, struct weston_subsurface,
3878 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003879 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003880
3881 /* The protocol object (wl_resource) is left inert. */
3882 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003883 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003884
3885 weston_subsurface_destroy(sub);
3886}
3887
3888static void
3889subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3890{
3891 struct weston_subsurface *sub =
3892 container_of(listener, struct weston_subsurface,
3893 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003894 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003895 assert(sub->surface != sub->parent);
3896
3897 if (weston_surface_is_mapped(sub->surface))
3898 weston_surface_unmap(sub->surface);
3899
3900 weston_subsurface_unlink_parent(sub);
3901}
3902
3903static void
3904subsurface_resource_destroy(struct wl_resource *resource)
3905{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003906 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003907
3908 if (sub)
3909 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003910}
3911
3912static void
3913subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3914{
3915 wl_resource_destroy(resource);
3916}
3917
3918static void
3919weston_subsurface_link_parent(struct weston_subsurface *sub,
3920 struct weston_surface *parent)
3921{
3922 sub->parent = parent;
3923 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003924 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003925 &sub->parent_destroy_listener);
3926
3927 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3928 wl_list_insert(&parent->subsurface_list_pending,
3929 &sub->parent_link_pending);
3930}
3931
3932static void
3933weston_subsurface_link_surface(struct weston_subsurface *sub,
3934 struct weston_surface *surface)
3935{
3936 sub->surface = surface;
3937 sub->surface_destroy_listener.notify =
3938 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003939 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003940 &sub->surface_destroy_listener);
3941}
3942
3943static void
3944weston_subsurface_destroy(struct weston_subsurface *sub)
3945{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003946 struct weston_view *view, *next;
3947
Pekka Paalanene67858b2013-04-25 13:57:42 +03003948 assert(sub->surface);
3949
3950 if (sub->resource) {
3951 assert(weston_surface_to_subsurface(sub->surface) == sub);
3952 assert(sub->parent_destroy_listener.notify ==
3953 subsurface_handle_parent_destroy);
3954
George Kiagiadakised04d382014-06-13 18:10:26 +02003955 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3956 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003957 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003958 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003959
Pekka Paalanene67858b2013-04-25 13:57:42 +03003960 if (sub->parent)
3961 weston_subsurface_unlink_parent(sub);
3962
Jason Ekstrand7b982072014-05-20 14:33:03 -05003963 weston_surface_state_fini(&sub->cached);
3964 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003965
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003966 sub->surface->committed = NULL;
3967 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003968 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003969 } else {
3970 /* the dummy weston_subsurface for the parent itself */
3971 assert(sub->parent_destroy_listener.notify == NULL);
3972 wl_list_remove(&sub->parent_link);
3973 wl_list_remove(&sub->parent_link_pending);
3974 }
3975
3976 wl_list_remove(&sub->surface_destroy_listener.link);
3977 free(sub);
3978}
3979
3980static const struct wl_subsurface_interface subsurface_implementation = {
3981 subsurface_destroy,
3982 subsurface_set_position,
3983 subsurface_place_above,
3984 subsurface_place_below,
3985 subsurface_set_sync,
3986 subsurface_set_desync
3987};
3988
3989static struct weston_subsurface *
3990weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3991 struct weston_surface *parent)
3992{
3993 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003994 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003995
Bryce Harringtonde16d892014-11-20 22:21:57 -08003996 sub = zalloc(sizeof *sub);
3997 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003998 return NULL;
3999
Jason Ekstranda7af7042013-10-12 22:38:11 -05004000 wl_list_init(&sub->unused_views);
4001
Jason Ekstranda85118c2013-06-27 20:17:02 -05004002 sub->resource =
4003 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004004 if (!sub->resource) {
4005 free(sub);
4006 return NULL;
4007 }
4008
Jason Ekstranda85118c2013-06-27 20:17:02 -05004009 wl_resource_set_implementation(sub->resource,
4010 &subsurface_implementation,
4011 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004012 weston_subsurface_link_surface(sub, surface);
4013 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004014 weston_surface_state_init(&sub->cached);
4015 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004016 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004017
4018 return sub;
4019}
4020
4021/* Create a dummy subsurface for having the parent itself in its
4022 * sub-surface lists. Makes stacking order manipulation easy.
4023 */
4024static struct weston_subsurface *
4025weston_subsurface_create_for_parent(struct weston_surface *parent)
4026{
4027 struct weston_subsurface *sub;
4028
Bryce Harringtonde16d892014-11-20 22:21:57 -08004029 sub = zalloc(sizeof *sub);
4030 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004031 return NULL;
4032
4033 weston_subsurface_link_surface(sub, parent);
4034 sub->parent = parent;
4035 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4036 wl_list_insert(&parent->subsurface_list_pending,
4037 &sub->parent_link_pending);
4038
4039 return sub;
4040}
4041
4042static void
4043subcompositor_get_subsurface(struct wl_client *client,
4044 struct wl_resource *resource,
4045 uint32_t id,
4046 struct wl_resource *surface_resource,
4047 struct wl_resource *parent_resource)
4048{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004049 struct weston_surface *surface =
4050 wl_resource_get_user_data(surface_resource);
4051 struct weston_surface *parent =
4052 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004053 struct weston_subsurface *sub;
4054 static const char where[] = "get_subsurface: wl_subsurface@";
4055
4056 if (surface == parent) {
4057 wl_resource_post_error(resource,
4058 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4059 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004060 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004061 return;
4062 }
4063
4064 if (weston_surface_to_subsurface(surface)) {
4065 wl_resource_post_error(resource,
4066 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4067 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004068 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004069 return;
4070 }
4071
Pekka Paalanen50b67472014-10-01 15:02:41 +03004072 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4073 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004074 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004075
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004076 if (weston_surface_get_main_surface(parent) == surface) {
4077 wl_resource_post_error(resource,
4078 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4079 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004080 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004081 return;
4082 }
4083
Pekka Paalanene67858b2013-04-25 13:57:42 +03004084 /* make sure the parent is in its own list */
4085 if (wl_list_empty(&parent->subsurface_list)) {
4086 if (!weston_subsurface_create_for_parent(parent)) {
4087 wl_resource_post_no_memory(resource);
4088 return;
4089 }
4090 }
4091
4092 sub = weston_subsurface_create(id, surface, parent);
4093 if (!sub) {
4094 wl_resource_post_no_memory(resource);
4095 return;
4096 }
4097
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004098 surface->committed = subsurface_committed;
4099 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004100 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004101}
4102
4103static void
4104subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4105{
4106 wl_resource_destroy(resource);
4107}
4108
4109static const struct wl_subcompositor_interface subcompositor_interface = {
4110 subcompositor_destroy,
4111 subcompositor_get_subsurface
4112};
4113
4114static void
4115bind_subcompositor(struct wl_client *client,
4116 void *data, uint32_t version, uint32_t id)
4117{
4118 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004119 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004120
Jason Ekstranda85118c2013-06-27 20:17:02 -05004121 resource =
4122 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004123 if (resource == NULL) {
4124 wl_client_post_no_memory(client);
4125 return;
4126 }
4127 wl_resource_set_implementation(resource, &subcompositor_interface,
4128 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004129}
4130
Bryce Harrington0795ece2016-08-30 12:04:26 -07004131/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004132 *
4133 * \param compositor The compositor instance
4134 * \param state The DPMS state the outputs will be set to
4135 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004136static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004137weston_compositor_dpms(struct weston_compositor *compositor,
4138 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004139{
4140 struct weston_output *output;
4141
Bryce Harrington08976ac2016-08-30 12:05:16 -07004142 wl_list_for_each(output, &compositor->output_list, link)
4143 if (output->set_dpms)
4144 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004145}
4146
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004147/** Restores the compositor to active status
4148 *
4149 * \param compositor The compositor instance
4150 *
4151 * If the compositor was in a sleeping mode, all outputs are powered
4152 * back on via DPMS. Otherwise if the compositor was inactive
4153 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4154 * signal will fire.
4155 *
4156 * Restarts the idle timer.
4157 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004158WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004159weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004160{
Neil Roberts8b62e202013-09-30 13:14:47 +01004161 uint32_t old_state = compositor->state;
4162
4163 /* The state needs to be changed before emitting the wake
4164 * signal because that may try to schedule a repaint which
4165 * will not work if the compositor is still sleeping */
4166 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4167
4168 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004169 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004170 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004171 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004172 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004173 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004174 /* fall through */
4175 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004176 wl_event_source_timer_update(compositor->idle_source,
4177 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004178 }
4179}
4180
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004181/** Turns off rendering and frame events for the compositor.
4182 *
4183 * \param compositor The compositor instance
4184 *
4185 * This is used for example to prevent further rendering while the
4186 * compositor is shutting down.
4187 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004188 * Stops the idle timer.
4189 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004190WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004191weston_compositor_offscreen(struct weston_compositor *compositor)
4192{
4193 switch (compositor->state) {
4194 case WESTON_COMPOSITOR_OFFSCREEN:
4195 return;
4196 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004197 default:
4198 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4199 wl_event_source_timer_update(compositor->idle_source, 0);
4200 }
4201}
4202
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004203/** Powers down all attached output devices
4204 *
4205 * \param compositor The compositor instance
4206 *
4207 * Causes rendering to the outputs to cease, and no frame events to be
4208 * sent. Only powers down the outputs if the compositor is not already
4209 * in sleep mode.
4210 *
4211 * Stops the idle timer.
4212 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004213WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004214weston_compositor_sleep(struct weston_compositor *compositor)
4215{
4216 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4217 return;
4218
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004219 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004220 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4221 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4222}
4223
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004224/** Sets compositor to idle mode
4225 *
4226 * \param data The compositor instance
4227 *
4228 * This is called when the idle timer fires. Once the compositor is in
4229 * idle mode it requires a wake action (e.g. via
4230 * weston_compositor_wake()) to restore it. The compositor's
4231 * idle_signal will be triggered when the idle event occurs.
4232 *
4233 * Idleness can be inhibited by setting the compositor's idle_inhibit
4234 * property.
4235 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004236static int
4237idle_handler(void *data)
4238{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004239 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004240
4241 if (compositor->idle_inhibit)
4242 return 1;
4243
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004244 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004245 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004246
4247 return 1;
4248}
4249
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004250WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004251weston_plane_init(struct weston_plane *plane,
4252 struct weston_compositor *ec,
4253 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004254{
4255 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004256 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004257 plane->x = x;
4258 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004259 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004260
4261 /* Init the link so that the call to wl_list_remove() when releasing
4262 * the plane without ever stacking doesn't lead to a crash */
4263 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004264}
4265
4266WL_EXPORT void
4267weston_plane_release(struct weston_plane *plane)
4268{
Xiong Zhang97116532013-10-23 13:58:31 +08004269 struct weston_view *view;
4270
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004271 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004272 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004273
Xiong Zhang97116532013-10-23 13:58:31 +08004274 wl_list_for_each(view, &plane->compositor->view_list, link) {
4275 if (view->plane == plane)
4276 view->plane = NULL;
4277 }
4278
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004279 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004280}
4281
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004282WL_EXPORT void
4283weston_compositor_stack_plane(struct weston_compositor *ec,
4284 struct weston_plane *plane,
4285 struct weston_plane *above)
4286{
4287 if (above)
4288 wl_list_insert(above->link.prev, &plane->link);
4289 else
4290 wl_list_insert(&ec->plane_list, &plane->link);
4291}
4292
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004293static void
4294output_release(struct wl_client *client, struct wl_resource *resource)
4295{
4296 wl_resource_destroy(resource);
4297}
4298
4299static const struct wl_output_interface output_interface = {
4300 output_release,
4301};
4302
4303
Casey Dahlin9074db52012-04-19 22:50:09 -04004304static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004305{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004306 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004307}
4308
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004309static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004310bind_output(struct wl_client *client,
4311 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004312{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004313 struct weston_output *output = data;
4314 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004315 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004316
Jason Ekstranda85118c2013-06-27 20:17:02 -05004317 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004318 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004319 if (resource == NULL) {
4320 wl_client_post_no_memory(client);
4321 return;
4322 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004323
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004324 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004325 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004326
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004327 wl_output_send_geometry(resource,
4328 output->x,
4329 output->y,
4330 output->mm_width,
4331 output->mm_height,
4332 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004333 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004334 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004335 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004336 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004337 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004338
4339 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004340 wl_output_send_mode(resource,
4341 mode->flags,
4342 mode->width,
4343 mode->height,
4344 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004345 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004346
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004347 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004348 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004349}
4350
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004351/** Get the backing object of wl_output
4352 *
4353 * \param resource A wl_output protocol object.
4354 * \return The backing object (user data) of a wl_resource representing a
4355 * wl_output protocol object.
4356 */
4357WL_EXPORT struct weston_output *
4358weston_output_from_resource(struct wl_resource *resource)
4359{
4360 assert(wl_resource_instance_of(resource, &wl_output_interface,
4361 &output_interface));
4362
4363 return wl_resource_get_user_data(resource);
4364}
4365
4366
David Fort0de859e2016-05-27 23:22:57 +02004367/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004368static void
David Fort0de859e2016-05-27 23:22:57 +02004369weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4370 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004371{
4372 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004373 bool start_resizing = false;
4374
4375 if (!delta_width)
4376 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004377
4378 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004379 if (output == resized_output) {
4380 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004381 continue;
4382 }
4383
David Fort0de859e2016-05-27 23:22:57 +02004384 if (start_resizing) {
4385 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004386 output->dirty = 1;
4387 }
4388 }
4389}
4390
Pekka Paalanend72bad22017-03-29 17:01:41 +03004391static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004392weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004393{
Scott Moreau850ca422012-05-21 15:21:25 -06004394 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004395
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004396 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004397 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004398
Scott Moreauccbf29d2012-02-22 14:21:41 -07004399 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004400 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004401 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004402 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004403 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004404 weston_matrix_scale(&output->matrix, magnification,
4405 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004406 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004407
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004408 switch (output->transform) {
4409 case WL_OUTPUT_TRANSFORM_FLIPPED:
4410 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4411 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4412 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4413 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4414 weston_matrix_scale(&output->matrix, -1, 1, 1);
4415 break;
4416 }
4417
4418 switch (output->transform) {
4419 default:
4420 case WL_OUTPUT_TRANSFORM_NORMAL:
4421 case WL_OUTPUT_TRANSFORM_FLIPPED:
4422 break;
4423 case WL_OUTPUT_TRANSFORM_90:
4424 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4425 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4426 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4427 break;
4428 case WL_OUTPUT_TRANSFORM_180:
4429 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4430 weston_matrix_translate(&output->matrix,
4431 -output->width, -output->height, 0);
4432 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4433 break;
4434 case WL_OUTPUT_TRANSFORM_270:
4435 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4436 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4437 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4438 break;
4439 }
4440
4441 if (output->current_scale != 1)
4442 weston_matrix_scale(&output->matrix,
4443 output->current_scale,
4444 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004445
Scott Moreauccbf29d2012-02-22 14:21:41 -07004446 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004447
4448 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004449}
4450
Scott Moreau1bad5db2012-08-18 01:04:05 -06004451static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004452weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004453{
4454 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004455 output->native_scale = scale;
4456 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004457
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004458 convert_size_by_transform_scale(&output->width, &output->height,
4459 output->current_mode->width,
4460 output->current_mode->height,
4461 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004462}
4463
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004464static void
4465weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004466{
4467 output->x = x;
4468 output->y = y;
4469
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004470 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004471 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004472
4473 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004474 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004475 output->width,
4476 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004477}
4478
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004479WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004480weston_output_move(struct weston_output *output, int x, int y)
4481{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004482 struct wl_resource *resource;
4483
4484 output->move_x = x - output->x;
4485 output->move_y = y - output->y;
4486
4487 if (output->move_x == 0 && output->move_y == 0)
4488 return;
4489
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004490 weston_output_init_geometry(output, x, y);
4491
4492 output->dirty = 1;
4493
4494 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004495 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004496
4497 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004498 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004499 wl_output_send_geometry(resource,
4500 output->x,
4501 output->y,
4502 output->mm_width,
4503 output->mm_height,
4504 output->subpixel,
4505 output->make,
4506 output->model,
4507 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004508
FORT David8a120692016-04-26 23:34:06 +02004509 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004510 wl_output_send_done(resource);
4511 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004512}
4513
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004514/** Signal that a pending output is taken into use.
4515 *
4516 * Removes the output from the pending list and adds it to the compositor's
4517 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03004518 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004519 * The output gets an internal ID assigned, and the wl_output global is
4520 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004521 *
Giulio Camuffob1147152015-05-06 21:41:57 +03004522 * \param compositor The compositor instance.
4523 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004524 *
4525 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03004526 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03004527static void
Giulio Camuffob1147152015-05-06 21:41:57 +03004528weston_compositor_add_output(struct weston_compositor *compositor,
4529 struct weston_output *output)
4530{
Armin Krezoviće5403842016-08-05 15:28:29 +02004531 struct weston_view *view, *next;
4532
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004533 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004534
4535 /* Verify we haven't reached the limit of 32 available output IDs */
4536 assert(ffs(~compositor->output_id_pool) > 0);
4537
4538 /* Invert the output id pool and look for the lowest numbered
4539 * switch (the least significant bit). Take that bit's position
4540 * as our ID, and mark it used in the compositor's output_id_pool.
4541 */
4542 output->id = ffs(~compositor->output_id_pool) - 1;
4543 compositor->output_id_pool |= 1u << output->id;
4544
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004545 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03004546 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004547 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004548
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004549 output->global = wl_global_create(compositor->wl_display,
4550 &wl_output_interface, 3,
4551 output, bind_output);
4552
Giulio Camuffob1147152015-05-06 21:41:57 +03004553 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02004554
4555 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4556 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004557}
4558
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004559/** Transform device coordinates into global coordinates
4560 *
4561 * \param device_x[in] X coordinate in device units.
4562 * \param device_y[in] Y coordinate in device units.
4563 * \param x[out] X coordinate in the global space.
4564 * \param y[out] Y coordinate in the global space.
4565 *
4566 * Transforms coordinates from the device coordinate space
4567 * (physical pixel units) to the global coordinate space (logical pixel units).
4568 * This takes into account output transform and scale.
4569 *
4570 * \memberof weston_output
4571 * \internal
4572 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004573WL_EXPORT void
4574weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004575 double device_x, double device_y,
4576 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004577{
Derek Foreman0f679412014-10-02 13:41:17 -05004578 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004579 device_x,
4580 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004581 0.0,
4582 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004583
Derek Foreman67a18b92015-03-24 11:36:14 -05004584 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004585
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004586 *x = p.f[0] / p.f[3];
4587 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004588}
4589
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004590/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004591 *
4592 * \param output The weston_output object that is being removed.
4593 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004594 * The following happens:
4595 *
4596 * - The output assignments of all views in the current scenegraph are
4597 * recomputed.
4598 *
4599 * - Presentation feedback is discarded.
4600 *
4601 * - Compositor is notified that outputs were changed and
4602 * applies the necessary changes to re-layout outputs.
4603 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004604 * - The output is put back in the pending outputs list.
4605 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004606 * - Signal is emitted to notify all users of the weston_output
4607 * object that the output is being destroyed.
4608 *
4609 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004610 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004611 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004612 * - The output's internal ID is released.
4613 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004614 * \memberof weston_output
4615 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004616 */
4617static void
4618weston_compositor_remove_output(struct weston_output *output)
4619{
Pekka Paalanenbccda712017-03-29 16:16:04 +03004620 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004621 struct wl_resource *resource;
4622 struct weston_view *view;
4623
4624 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004625 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004626
Pekka Paalanenbccda712017-03-29 16:16:04 +03004627 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004628 if (view->output_mask & (1u << output->id))
4629 weston_view_assign_output(view);
4630 }
4631
4632 weston_presentation_feedback_discard_list(&output->feedback_list);
4633
Pekka Paalanenbccda712017-03-29 16:16:04 +03004634 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004635
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004636 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004637 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004638 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004639
Pekka Paalanenbccda712017-03-29 16:16:04 +03004640 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004641 wl_signal_emit(&output->destroy_signal, output);
4642
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004643 wl_global_destroy(output->global);
4644 output->global = NULL;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004645 wl_resource_for_each(resource, &output->resource_list) {
4646 wl_resource_set_destructor(resource, NULL);
4647 }
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004648
4649 compositor->output_id_pool &= ~(1u << output->id);
4650 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004651}
4652
4653/** Sets the output scale for a given output.
4654 *
4655 * \param output The weston_output object that the scale is set for.
4656 * \param scale Scale factor for the given output.
4657 *
4658 * It only supports setting scale for an output that
4659 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004660 *
4661 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004662 */
4663WL_EXPORT void
4664weston_output_set_scale(struct weston_output *output,
4665 int32_t scale)
4666{
4667 /* We can only set scale on a disabled output */
4668 assert(!output->enabled);
4669
4670 /* We only want to set scale once */
4671 assert(!output->scale);
4672
4673 output->scale = scale;
4674}
4675
4676/** Sets the output transform for a given output.
4677 *
4678 * \param output The weston_output object that the transform is set for.
4679 * \param transform Transform value for the given output.
4680 *
4681 * It only supports setting transform for an output that is
4682 * not enabled and it can only be ran once.
4683 *
4684 * Refer to wl_output::transform section located at
4685 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
4686 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004687 *
4688 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004689 */
4690WL_EXPORT void
4691weston_output_set_transform(struct weston_output *output,
4692 uint32_t transform)
4693{
4694 /* We can only set transform on a disabled output */
4695 assert(!output->enabled);
4696
4697 /* We only want to set transform once */
4698 assert(output->transform == UINT32_MAX);
4699
4700 output->transform = transform;
4701}
4702
4703/** Initializes a weston_output object with enough data so
4704 ** an output can be configured.
4705 *
4706 * \param output The weston_output object to initialize
4707 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004708 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004709 *
4710 * Sets initial values for fields that are expected to be
4711 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004712 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004713 * The name is used in logs, and can be used by compositors as a configuration
4714 * identifier.
4715 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004716 * \memberof weston_output
4717 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004718 */
4719WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02004720weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004721 struct weston_compositor *compositor,
4722 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004723{
4724 output->compositor = compositor;
4725 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004726 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004727 wl_list_init(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004728 output->enabled = false;
4729
4730 /* Add some (in)sane defaults which can be used
4731 * for checking if an output was properly configured
4732 */
4733 output->mm_width = 0;
4734 output->mm_height = 0;
4735 output->scale = 0;
4736 /* Can't use -1 on uint32_t and 0 is valid enum value */
4737 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004738
4739 pixman_region32_init(&output->previous_damage);
4740 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03004741 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004742}
4743
4744/** Adds weston_output object to pending output list.
4745 *
4746 * \param output The weston_output object to add
4747 * \param compositor The compositor instance.
4748 *
4749 * Also notifies the compositor that an output is pending for
4750 * configuration.
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004751 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03004752 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004753 *
4754 * \memberof weston_output
4755 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004756 */
4757WL_EXPORT void
4758weston_compositor_add_pending_output(struct weston_output *output,
4759 struct weston_compositor *compositor)
4760{
Pekka Paalanene952a012017-03-29 17:14:00 +03004761 assert(output->disable);
4762 assert(output->enable);
4763
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004764 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004765 wl_list_insert(compositor->pending_output_list.prev, &output->link);
4766 wl_signal_emit(&compositor->output_pending_signal, output);
4767}
4768
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004769/** Constructs a weston_output object that can be used by the compositor.
4770 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03004771 * \param output The weston_output object that needs to be enabled. Must not
4772 * be enabled already.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004773 *
4774 * Output coordinates are calculated and each new output is by default
4775 * assigned to the right of previous one.
4776 *
4777 * Sets up the transformation, zoom, and geometry of the output using
4778 * the properties that need to be configured by the compositor.
4779 *
4780 * Establishes a repaint timer for the output with the relevant display
4781 * object's event loop. See output_repaint_timer_handler().
4782 *
4783 * The output is assigned an ID. Weston can support up to 32 distinct
4784 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4785 * is referred to and used to find the first available ID number, and
4786 * then this ID is marked as used in output_id_pool.
4787 *
4788 * The output is also assigned a Wayland global with the wl_output
4789 * external interface.
4790 *
4791 * Backend specific function is called to set up the output output.
4792 *
4793 * Output is added to the compositor's output list
4794 *
4795 * If the backend specific function fails, the weston_output object
4796 * is returned to a state it was before calling this function and
4797 * is added to the compositor's pending_output_list in case it needs
4798 * to be reconfigured or just so it can be destroyed at shutdown.
4799 *
4800 * 0 is returned on success, -1 on failure.
4801 */
4802WL_EXPORT int
4803weston_output_enable(struct weston_output *output)
4804{
Armin Krezović782f5df2016-09-30 14:11:11 +02004805 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004806 struct weston_output *iterator;
4807 int x = 0, y = 0;
4808
Pekka Paalanencc201e42017-03-30 15:11:25 +03004809 if (output->enabled) {
4810 weston_log("Error: attempt to enable an enabled output '%s'\n",
4811 output->name);
4812 return -1;
4813 }
4814
Armin Krezović782f5df2016-09-30 14:11:11 +02004815 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004816 struct weston_output, link);
4817
Armin Krezović782f5df2016-09-30 14:11:11 +02004818 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004819 x = iterator->x + iterator->width;
4820
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004821 /* Make sure the scale is set up */
4822 assert(output->scale);
4823
4824 /* Make sure we have a transform set */
4825 assert(output->transform != UINT32_MAX);
4826
Armin Krezović782f5df2016-09-30 14:11:11 +02004827 output->x = x;
4828 output->y = y;
4829 output->dirty = 1;
4830 output->original_scale = output->scale;
4831
4832 weston_output_transform_scale_init(output, output->transform, output->scale);
4833 weston_output_init_zoom(output);
4834
4835 weston_output_init_geometry(output, x, y);
4836 weston_output_damage(output);
4837
4838 wl_signal_init(&output->frame_signal);
4839 wl_signal_init(&output->destroy_signal);
4840 wl_list_init(&output->animation_list);
4841 wl_list_init(&output->resource_list);
4842 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02004843
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004844 /* Enable the output (set up the crtc or create a
4845 * window representing the output, set up the
4846 * renderer, etc)
4847 */
4848 if (output->enable(output) < 0) {
4849 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004850 return -1;
4851 }
4852
4853 weston_compositor_add_output(output->compositor, output);
4854
4855 return 0;
4856}
4857
4858/** Converts a weston_output object to a pending output state, so it
4859 ** can be configured again or destroyed.
4860 *
4861 * \param output The weston_output object that needs to be disabled.
4862 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004863 * Calls a backend specific function to disable an output, in case
4864 * such function exists.
4865 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004866 * The backend specific disable function may choose to postpone the disabling
4867 * by returning a negative value, in which case this function returns early.
4868 * In that case the backend will guarantee the output will be disabled soon
4869 * by the backend calling this function again. One must not attempt to re-enable
4870 * the output until that happens.
4871 *
4872 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004873 * from weston's output_list (see weston_compositor_remove_output())
4874 * and is returned to a state it was before weston_output_enable()
4875 * was ran (see weston_output_enable_undo()).
4876 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004877 * See weston_output_init() for more information on the
4878 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03004879 *
4880 * If the output has never been enabled yet, this function can still be
4881 * called to ensure that the output is actually turned off rather than left
4882 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004883 */
4884WL_EXPORT void
4885weston_output_disable(struct weston_output *output)
4886{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004887 /* Should we rename this? */
4888 output->destroying = 1;
4889
Pekka Paalanenc65df642017-03-29 15:45:46 +03004890 /* Disable is called unconditionally also for not-enabled outputs,
4891 * because at compositor start-up, if there is an output that is
4892 * already on but the compositor wants to turn it off, we have to
4893 * forward the turn-off to the backend so it knows to do it.
4894 * The backend cannot initially turn off everything, because it
4895 * would cause unnecessary mode-sets for all outputs the compositor
4896 * wants to be on.
4897 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004898 if (output->disable(output) < 0)
4899 return;
4900
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004901 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004902 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004903
4904 output->destroying = 0;
4905}
4906
4907/** Emits a signal to indicate that there are outputs waiting to be configured.
4908 *
4909 * \param compositor The compositor instance
4910 */
4911WL_EXPORT void
4912weston_pending_output_coldplug(struct weston_compositor *compositor)
4913{
4914 struct weston_output *output, *next;
4915
4916 wl_list_for_each_safe(output, next, &compositor->pending_output_list, link)
4917 wl_signal_emit(&compositor->output_pending_signal, output);
4918}
4919
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004920/** Uninitialize an output
4921 *
4922 * Removes the output from the list of enabled outputs if necessary, but
4923 * does not call the backend's output disable function. The output will no
4924 * longer be in the list of pending outputs either.
4925 *
4926 * All fields of weston_output become uninitialized, i.e. should not be used
4927 * anymore. The caller can free the memory after this.
4928 *
4929 * \memberof weston_output
4930 * \internal
4931 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004932WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03004933weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004934{
4935 output->destroying = 1;
4936
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004937 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004938 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004939
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004940 pixman_region32_fini(&output->region);
4941 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004942 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004943 free(output->name);
4944}
4945
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004946static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004947destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004948{
Jonny Lamb74130762013-11-26 18:19:46 +01004949 struct weston_surface *surface =
4950 wl_resource_get_user_data(resource);
4951
Pekka Paalanen4826f872016-04-22 14:14:38 +03004952 if (!surface)
4953 return;
4954
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004955 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004956 surface->pending.buffer_viewport.buffer.src_width =
4957 wl_fixed_from_int(-1);
4958 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004959 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004960}
4961
4962static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004963viewport_destroy(struct wl_client *client,
4964 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004965{
4966 wl_resource_destroy(resource);
4967}
4968
4969static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004970viewport_set_source(struct wl_client *client,
4971 struct wl_resource *resource,
4972 wl_fixed_t src_x,
4973 wl_fixed_t src_y,
4974 wl_fixed_t src_width,
4975 wl_fixed_t src_height)
4976{
4977 struct weston_surface *surface =
4978 wl_resource_get_user_data(resource);
4979
Pekka Paalanen4826f872016-04-22 14:14:38 +03004980 if (!surface) {
4981 wl_resource_post_error(resource,
4982 WP_VIEWPORT_ERROR_NO_SURFACE,
4983 "wl_surface for this viewport is no longer exists");
4984 return;
4985 }
4986
4987 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004988 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004989
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004990 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004991 src_height == wl_fixed_from_int(-1) &&
4992 src_x == wl_fixed_from_int(-1) &&
4993 src_y == wl_fixed_from_int(-1)) {
4994 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004995 surface->pending.buffer_viewport.buffer.src_width =
4996 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004997 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004998 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004999 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005000
Pekka Paalanen201769a2016-04-26 14:42:11 +03005001 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005002 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005003 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03005004 "wl_surface@%d viewport source "
5005 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
5006 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005007 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03005008 wl_fixed_to_double(src_height),
5009 wl_fixed_to_double(src_x),
5010 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005011 return;
5012 }
5013
5014 surface->pending.buffer_viewport.buffer.src_x = src_x;
5015 surface->pending.buffer_viewport.buffer.src_y = src_y;
5016 surface->pending.buffer_viewport.buffer.src_width = src_width;
5017 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005018 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005019}
5020
5021static void
5022viewport_set_destination(struct wl_client *client,
5023 struct wl_resource *resource,
5024 int32_t dst_width,
5025 int32_t dst_height)
5026{
5027 struct weston_surface *surface =
5028 wl_resource_get_user_data(resource);
5029
Pekka Paalanen4826f872016-04-22 14:14:38 +03005030 if (!surface) {
5031 wl_resource_post_error(resource,
5032 WP_VIEWPORT_ERROR_NO_SURFACE,
5033 "wl_surface for this viewport no longer exists");
5034 return;
5035 }
5036
5037 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005038
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005039 if (dst_width == -1 && dst_height == -1) {
5040 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005041 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005042 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005043 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005044 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005045
5046 if (dst_width <= 0 || dst_height <= 0) {
5047 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005048 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005049 "destination size must be positive (%dx%d)",
5050 dst_width, dst_height);
5051 return;
5052 }
5053
5054 surface->pending.buffer_viewport.surface.width = dst_width;
5055 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005056 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005057}
5058
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005059static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005060 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005061 viewport_set_source,
5062 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01005063};
5064
5065static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005066viewporter_destroy(struct wl_client *client,
5067 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005068{
5069 wl_resource_destroy(resource);
5070}
5071
5072static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005073viewporter_get_viewport(struct wl_client *client,
5074 struct wl_resource *viewporter,
5075 uint32_t id,
5076 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005077{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005078 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005079 struct weston_surface *surface =
5080 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005081 struct wl_resource *resource;
5082
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005083 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005084 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005085 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005086 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01005087 return;
5088 }
5089
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005090 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005091 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005092 if (resource == NULL) {
5093 wl_client_post_no_memory(client);
5094 return;
5095 }
5096
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005097 wl_resource_set_implementation(resource, &viewport_interface,
5098 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01005099
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005100 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005101}
5102
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005103static const struct wp_viewporter_interface viewporter_interface = {
5104 viewporter_destroy,
5105 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01005106};
5107
5108static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005109bind_viewporter(struct wl_client *client,
5110 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005111{
5112 struct wl_resource *resource;
5113
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005114 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005115 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005116 if (resource == NULL) {
5117 wl_client_post_no_memory(client);
5118 return;
5119 }
5120
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005121 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01005122 NULL, NULL);
5123}
5124
5125static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04005126destroy_presentation_feedback(struct wl_resource *feedback_resource)
5127{
5128 struct weston_presentation_feedback *feedback;
5129
5130 feedback = wl_resource_get_user_data(feedback_resource);
5131
5132 wl_list_remove(&feedback->link);
5133 free(feedback);
5134}
5135
5136static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005137presentation_destroy(struct wl_client *client, struct wl_resource *resource)
5138{
5139 wl_resource_destroy(resource);
5140}
5141
5142static void
5143presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005144 struct wl_resource *presentation_resource,
5145 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005146 uint32_t callback)
5147{
Pekka Paalanen133e4392014-09-23 22:08:46 -04005148 struct weston_surface *surface;
5149 struct weston_presentation_feedback *feedback;
5150
5151 surface = wl_resource_get_user_data(surface_resource);
5152
Bryce Harringtonde16d892014-11-20 22:21:57 -08005153 feedback = zalloc(sizeof *feedback);
5154 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04005155 goto err_calloc;
5156
5157 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005158 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005159 1, callback);
5160 if (!feedback->resource)
5161 goto err_create;
5162
5163 wl_resource_set_implementation(feedback->resource, NULL, feedback,
5164 destroy_presentation_feedback);
5165
5166 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
5167
5168 return;
5169
5170err_create:
5171 free(feedback);
5172
5173err_calloc:
5174 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005175}
5176
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005177static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005178 presentation_destroy,
5179 presentation_feedback
5180};
5181
5182static void
5183bind_presentation(struct wl_client *client,
5184 void *data, uint32_t version, uint32_t id)
5185{
5186 struct weston_compositor *compositor = data;
5187 struct wl_resource *resource;
5188
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005189 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005190 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005191 if (resource == NULL) {
5192 wl_client_post_no_memory(client);
5193 return;
5194 }
5195
5196 wl_resource_set_implementation(resource, &presentation_implementation,
5197 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005198 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005199}
5200
5201static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05005202compositor_bind(struct wl_client *client,
5203 void *data, uint32_t version, uint32_t id)
5204{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005205 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005206 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05005207
Jason Ekstranda85118c2013-06-27 20:17:02 -05005208 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005209 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005210 if (resource == NULL) {
5211 wl_client_post_no_memory(client);
5212 return;
5213 }
5214
5215 wl_resource_set_implementation(resource, &compositor_interface,
5216 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05005217}
5218
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005219WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005220weston_environment_get_fd(const char *env)
5221{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005222 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005223 int fd, flags;
5224
5225 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005226 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005227 return -1;
5228
5229 flags = fcntl(fd, F_GETFD);
5230 if (flags == -1)
5231 return -1;
5232
5233 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
5234 unsetenv(env);
5235
5236 return fd;
5237}
5238
Pekka Paalanenb5026542014-11-12 15:09:24 +02005239static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02005240timeline_key_binding_handler(struct weston_keyboard *keyboard,
5241 const struct timespec *time, uint32_t key,
5242 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02005243{
5244 struct weston_compositor *compositor = data;
5245
5246 if (weston_timeline_enabled_)
5247 weston_timeline_close();
5248 else
5249 weston_timeline_open(compositor);
5250}
5251
Giulio Camuffo459137b2014-10-11 23:56:24 +03005252/** Create the compositor.
5253 *
5254 * This functions creates and initializes a compositor instance.
5255 *
5256 * \param display The Wayland display to be used.
5257 * \param user_data A pointer to an object that can later be retrieved
5258 * using the \ref weston_compositor_get_user_data function.
5259 * \return The compositor instance on success or NULL on failure.
5260 */
5261WL_EXPORT struct weston_compositor *
5262weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005263{
Giulio Camuffo459137b2014-10-11 23:56:24 +03005264 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05005265 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07005266
Giulio Camuffo459137b2014-10-11 23:56:24 +03005267 ec = zalloc(sizeof *ec);
5268 if (!ec)
5269 return NULL;
5270
5271 ec->wl_display = display;
5272 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005273 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07005274 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005275 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005276 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005277 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005278 wl_signal_init(&ec->idle_signal);
5279 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005280 wl_signal_init(&ec->show_input_panel_signal);
5281 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005282 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01005283 wl_signal_init(&ec->seat_created_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005284 wl_signal_init(&ec->output_pending_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01005285 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02005286 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005287 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02005288 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005289 wl_signal_init(&ec->session_signal);
5290 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005291
Casey Dahlin58ba1372012-04-19 22:50:08 -04005292 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03005293 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04005294
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02005295 ec->activate_serial = 1;
5296
Derek Foreman152254b2015-11-26 14:17:48 -06005297 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005298 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005299 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05005300
Giulio Camuffo954f1832014-10-11 18:27:30 +03005301 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005302 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005303 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03005304
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005305 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005306 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005307 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005308
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005309 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005310 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005311 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005312
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005313 if (weston_input_init(ec) != 0)
5314 goto fail;
5315
Jason Ekstranda7af7042013-10-12 22:38:11 -05005316 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005317 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005318 wl_list_init(&ec->layer_list);
5319 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005320 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005321 wl_list_init(&ec->output_list);
5322 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01005323 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005324 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005325 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005326 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005327 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005328
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005329 wl_list_init(&ec->plugin_api_list);
5330
Xiong Zhang97116532013-10-23 13:58:31 +08005331 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005332 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005333
Giulio Camuffo459137b2014-10-11 23:56:24 +03005334 wl_data_device_manager_init(ec->wl_display);
5335
5336 wl_display_init_shm(ec->wl_display);
5337
5338 loop = wl_display_get_event_loop(ec->wl_display);
5339 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00005340 ec->repaint_timer =
5341 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
5342 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005343
Quentin Glidic82681572016-12-17 13:40:51 +01005344 weston_layer_init(&ec->fade_layer, ec);
5345 weston_layer_init(&ec->cursor_layer, ec);
5346
5347 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
5348 weston_layer_set_position(&ec->cursor_layer,
5349 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005350
5351 weston_compositor_add_debug_binding(ec, KEY_T,
5352 timeline_key_binding_handler, ec);
5353
Giulio Camuffo459137b2014-10-11 23:56:24 +03005354 return ec;
5355
5356fail:
5357 free(ec);
5358 return NULL;
5359}
5360
Benjamin Franzkeb8263022011-08-30 11:32:47 +02005361WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005362weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07005363{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005364 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07005365
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005366 wl_event_source_remove(ec->idle_source);
5367
Matt Roper361d2ad2011-08-29 13:52:23 -07005368 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02005369 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07005370 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02005371
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005372 /* Destroy all pending outputs associated with this compositor */
5373 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
5374 output->destroy(output);
5375
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02005376 if (ec->renderer)
5377 ec->renderer->destroy(ec);
5378
Daniel Stone325fc2d2012-05-30 16:31:58 +01005379 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09005380 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005381 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005382 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005383 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005384 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005385
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005386 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07005387}
5388
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005389WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04005390weston_compositor_exit_with_code(struct weston_compositor *compositor,
5391 int exit_code)
5392{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02005393 if (compositor->exit_code == EXIT_SUCCESS)
5394 compositor->exit_code = exit_code;
5395
Giulio Camuffo459137b2014-10-11 23:56:24 +03005396 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04005397}
5398
5399WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005400weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
5401 const struct weston_pointer_grab_interface *interface)
5402{
5403 struct weston_seat *seat;
5404
5405 ec->default_pointer_grab = interface;
5406 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005407 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5408
5409 if (pointer)
5410 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005411 }
5412}
5413
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04005414WL_EXPORT int
5415weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
5416 clockid_t clk_id)
5417{
5418 struct timespec ts;
5419
5420 if (clock_gettime(clk_id, &ts) < 0)
5421 return -1;
5422
5423 compositor->presentation_clock = clk_id;
5424
5425 return 0;
5426}
5427
5428/*
5429 * For choosing the software clock, when the display hardware or API
5430 * does not expose a compatible presentation timestamp.
5431 */
5432WL_EXPORT int
5433weston_compositor_set_presentation_clock_software(
5434 struct weston_compositor *compositor)
5435{
5436 /* In order of preference */
5437 static const clockid_t clocks[] = {
5438 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
5439 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
5440 CLOCK_MONOTONIC, /* no jumps, may crawl */
5441 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
5442 CLOCK_REALTIME /* may jump and crawl */
5443 };
5444 unsigned i;
5445
5446 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
5447 if (weston_compositor_set_presentation_clock(compositor,
5448 clocks[i]) == 0)
5449 return 0;
5450
5451 weston_log("Error: no suitable presentation clock available.\n");
5452
5453 return -1;
5454}
5455
Pekka Paalanen662f3842015-03-18 12:17:26 +02005456/** Read the current time from the Presentation clock
5457 *
5458 * \param compositor
5459 * \param ts[out] The current time.
5460 *
5461 * \note Reading the current time in user space is always imprecise to some
5462 * degree.
5463 *
5464 * This function is never meant to fail. If reading the clock does fail,
5465 * an error message is logged and a zero time is returned. Callers are not
5466 * supposed to detect or react to failures.
5467 */
5468WL_EXPORT void
5469weston_compositor_read_presentation_clock(
5470 const struct weston_compositor *compositor,
5471 struct timespec *ts)
5472{
5473 static bool warned;
5474 int ret;
5475
5476 ret = clock_gettime(compositor->presentation_clock, ts);
5477 if (ret < 0) {
5478 ts->tv_sec = 0;
5479 ts->tv_nsec = 0;
5480
5481 if (!warned)
5482 weston_log("Error: failure to read "
5483 "the presentation clock %#x: '%m' (%d)\n",
5484 compositor->presentation_clock, errno);
5485 warned = true;
5486 }
5487}
5488
Pekka Paalanen230f3b12014-09-29 14:18:40 -04005489/** Import dmabuf buffer into current renderer
5490 *
5491 * \param compositor
5492 * \param buffer the dmabuf buffer to import
5493 * \return true on usable buffers, false otherwise
5494 *
5495 * This function tests that the linux_dmabuf_buffer is usable
5496 * for the current renderer. Returns false on unusable buffers. Usually
5497 * usability is tested by importing the dmabufs for composition.
5498 *
5499 * This hook is also used for detecting if the renderer supports
5500 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
5501 * supported.
5502 * */
5503WL_EXPORT bool
5504weston_compositor_import_dmabuf(struct weston_compositor *compositor,
5505 struct linux_dmabuf_buffer *buffer)
5506{
5507 struct weston_renderer *renderer;
5508
5509 renderer = compositor->renderer;
5510
5511 if (renderer->import_dmabuf == NULL)
5512 return false;
5513
5514 return renderer->import_dmabuf(compositor, buffer);
5515}
5516
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005517WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005518weston_version(int *major, int *minor, int *micro)
5519{
5520 *major = WESTON_VERSION_MAJOR;
5521 *minor = WESTON_VERSION_MINOR;
5522 *micro = WESTON_VERSION_MICRO;
5523}
5524
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005525WL_EXPORT void *
5526weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005527{
Derek Foreman3f86e502015-06-08 11:46:54 -05005528 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005529 char path[PATH_MAX];
5530 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00005531 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005532
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005533 if (name == NULL)
5534 return NULL;
5535
Derek Foreman3f86e502015-06-08 11:46:54 -05005536 if (name[0] != '/') {
5537 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00005538 len = snprintf(path, sizeof path, "%s/.libs/%s",
5539 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005540 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00005541 len = snprintf(path, sizeof path, "%s/%s",
5542 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005543 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00005544 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005545 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005546
Daniel Stonebeb97e52016-11-28 12:13:54 +00005547 /* snprintf returns the length of the string it would've written,
5548 * _excluding_ the NUL byte. So even being equal to the size of
5549 * our buffer is an error here. */
5550 if (len >= sizeof path)
5551 return NULL;
5552
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005553 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
5554 if (module) {
5555 weston_log("Module '%s' already loaded\n", path);
5556 dlclose(module);
5557 return NULL;
5558 }
5559
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005560 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04005561 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005562 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005563 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005564 return NULL;
5565 }
5566
5567 init = dlsym(module, entrypoint);
5568 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005569 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00005570 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005571 return NULL;
5572 }
5573
5574 return init;
5575}
5576
Giulio Camuffo459137b2014-10-11 23:56:24 +03005577
5578/** Destroys the compositor.
5579 *
5580 * This function cleans up the compositor state and destroys it.
5581 *
5582 * \param compositor The compositor to be destroyed.
5583 */
5584WL_EXPORT void
5585weston_compositor_destroy(struct weston_compositor *compositor)
5586{
5587 /* prevent further rendering while shutting down */
5588 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5589
5590 wl_signal_emit(&compositor->destroy_signal, compositor);
5591
5592 weston_compositor_xkb_destroy(compositor);
5593
Giulio Camuffo2d24e642015-10-03 16:25:15 +03005594 if (compositor->backend)
5595 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005596
5597 weston_plugin_api_destroy_list(compositor);
5598
Giulio Camuffo459137b2014-10-11 23:56:24 +03005599 free(compositor);
5600}
5601
5602/** Instruct the compositor to exit.
5603 *
5604 * This functions does not directly destroy the compositor object, it merely
5605 * command it to start the tear down process. It is not guaranteed that the
5606 * tear down will happen immediately.
5607 *
5608 * \param compositor The compositor to tear down.
5609 */
5610WL_EXPORT void
5611weston_compositor_exit(struct weston_compositor *compositor)
5612{
5613 compositor->exit(compositor);
5614}
5615
5616/** Return the user data stored in the compositor.
5617 *
5618 * This function returns the user data pointer set with user_data parameter
5619 * to the \ref weston_compositor_create function.
5620 */
5621WL_EXPORT void *
5622weston_compositor_get_user_data(struct weston_compositor *compositor)
5623{
5624 return compositor->user_data;
5625}
Pekka Paalanendd186732016-06-03 14:49:54 +03005626
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005627static const char * const backend_map[] = {
5628 [WESTON_BACKEND_DRM] = "drm-backend.so",
5629 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5630 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5631 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5632 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5633 [WESTON_BACKEND_X11] = "x11-backend.so",
5634};
5635
Pekka Paalanendd186732016-06-03 14:49:54 +03005636/** Load a backend into a weston_compositor
5637 *
5638 * A backend must be loaded to make a weston_compositor work. A backend
5639 * provides input and output capabilities, and determines the renderer to use.
5640 *
5641 * \param compositor A compositor that has not had a backend loaded yet.
5642 * \param backend Name of the backend file.
5643 * \param config_base A pointer to a backend-specific configuration
5644 * structure's 'base' member.
5645 *
5646 * \return 0 on success, or -1 on error.
5647 */
5648WL_EXPORT int
5649weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005650 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005651 struct weston_backend_config *config_base)
5652{
5653 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005654 struct weston_backend_config *config_base);
5655
Pekka Paalanend7e35112017-08-29 17:04:12 +03005656 if (compositor->backend) {
5657 weston_log("Error: attempt to load a backend when one is already loaded\n");
5658 return -1;
5659 }
5660
Quentin Glidic887c0182016-07-10 11:00:53 +02005661 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005662 return -1;
5663
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01005664 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005665 if (!backend_init)
5666 return -1;
5667
Pekka Paalanend7e35112017-08-29 17:04:12 +03005668 if (backend_init(compositor, config_base) < 0) {
5669 compositor->backend = NULL;
5670 return -1;
5671 }
5672
5673 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03005674}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005675
5676WL_EXPORT int
5677weston_compositor_load_xwayland(struct weston_compositor *compositor)
5678{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005679 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005680
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005681 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005682 if (!module_init)
5683 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005684 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005685 return -1;
5686 return 0;
5687}