blob: 6f8d93918a3b9b8d36b4eb6c3e9dc1e4ee609c37 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050026 */
27
Kristian Høgsberga9410222011-01-14 17:22:35 -050028#include "config.h"
29
Daniel Stoneb7452fe2012-06-01 12:14:06 +010030#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010035#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020037#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010039#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040040#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020041#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020042#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020043#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050045#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040046#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040047#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040048#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050049#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040050#include <sys/time.h>
51#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030052#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050053
Pekka Paalanenb5026542014-11-12 15:09:24 +020054#include "timeline.h"
55
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030057#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020058#include "presentation-time-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070059#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070060#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070061#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030062#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040063#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050064#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020065#include "plugin-registry.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066
Pekka Paalanen0513a952014-05-21 16:17:27 +030067#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
68
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020069static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030070weston_output_update_matrix(struct weston_output *output);
71
72static void
Alexander Larsson0b135062013-05-28 16:23:36 +020073weston_output_transform_scale_init(struct weston_output *output,
74 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020075
Rob Bradford27b17932013-06-26 18:08:46 +010076static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050077weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010078
Derek Foreman6ae7bc92014-11-04 10:47:33 -060079static void weston_mode_switch_finish(struct weston_output *output,
80 int mode_changed,
81 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080082{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020083 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020084 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020085 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060086 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020087
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020088 pixman_region32_init(&old_output_region);
89 pixman_region32_copy(&old_output_region, &output->region);
90
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020092 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020093
94 pixman_region32_init(&output->previous_damage);
95 pixman_region32_init_rect(&output->region, output->x, output->y,
96 output->width, output->height);
97
98 weston_output_update_matrix(output);
99
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200100 /* If a pointer falls outside the outputs new geometry, move it to its
101 * lower-right corner */
102 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500103 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200104 int32_t x, y;
105
106 if (!pointer)
107 continue;
108
109 x = wl_fixed_to_int(pointer->x);
110 y = wl_fixed_to_int(pointer->y);
111
112 if (!pixman_region32_contains_point(&old_output_region,
113 x, y, NULL) ||
114 pixman_region32_contains_point(&output->region,
115 x, y, NULL))
116 continue;
117
118 if (x >= output->x + output->width)
119 x = output->x + output->width - 1;
120 if (y >= output->y + output->height)
121 y = output->y + output->height - 1;
122
123 pointer->x = wl_fixed_from_int(x);
124 pointer->y = wl_fixed_from_int(y);
125 }
126
127 pixman_region32_fini(&old_output_region);
128
Derek Foremandd4cd332014-11-10 10:29:59 -0600129 if (!mode_changed && !scale_changed)
130 return;
131
Hardening57388e42013-09-18 23:56:36 +0200132 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600133 wl_resource_for_each(resource, &output->resource_list) {
134 if (mode_changed) {
135 wl_output_send_mode(resource,
136 output->current_mode->flags,
137 output->current_mode->width,
138 output->current_mode->height,
139 output->current_mode->refresh);
140 }
Hardening57388e42013-09-18 23:56:36 +0200141
Derek Foreman41bdc272014-11-05 13:26:57 -0600142 version = wl_resource_get_version(resource);
143 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600144 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200145
Derek Foreman41bdc272014-11-05 13:26:57 -0600146 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
147 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600148 }
149}
150
David Fort0de859e2016-05-27 23:22:57 +0200151
152static void
153weston_compositor_reflow_outputs(struct weston_compositor *compositor,
154 struct weston_output *resized_output, int delta_width);
155
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600156WL_EXPORT int
157weston_output_mode_set_native(struct weston_output *output,
158 struct weston_mode *mode,
159 int32_t scale)
160{
161 int ret;
162 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200163 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600164
165 if (!output->switch_mode)
166 return -1;
167
168 if (!output->original_mode) {
169 mode_changed = 1;
170 ret = output->switch_mode(output, mode);
171 if (ret < 0)
172 return ret;
173 if (output->current_scale != scale) {
174 scale_changed = 1;
175 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200176 }
177 }
178
David Fort0de859e2016-05-27 23:22:57 +0200179 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600180 output->native_mode = mode;
181 output->native_scale = scale;
182
183 weston_mode_switch_finish(output, mode_changed, scale_changed);
184
David Fort0de859e2016-05-27 23:22:57 +0200185 if (mode_changed || scale_changed) {
186 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
187
188 wl_signal_emit(&output->compositor->output_resized_signal, output);
189 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600190 return 0;
191}
192
193WL_EXPORT int
194weston_output_mode_switch_to_native(struct weston_output *output)
195{
196 int ret;
197 int mode_changed = 0, scale_changed = 0;
198
199 if (!output->switch_mode)
200 return -1;
201
202 if (!output->original_mode) {
203 weston_log("already in the native mode\n");
204 return -1;
205 }
206 /* the non fullscreen clients haven't seen a mode set since we
207 * switched into a temporary, so we need to notify them if the
208 * mode at that time is different from the native mode now.
209 */
210 mode_changed = (output->original_mode != output->native_mode);
211 scale_changed = (output->original_scale != output->native_scale);
212
213 ret = output->switch_mode(output, output->native_mode);
214 if (ret < 0)
215 return ret;
216
217 output->current_scale = output->native_scale;
218
219 output->original_mode = NULL;
220 output->original_scale = 0;
221
222 weston_mode_switch_finish(output, mode_changed, scale_changed);
223
224 return 0;
225}
226
227WL_EXPORT int
228weston_output_mode_switch_to_temporary(struct weston_output *output,
229 struct weston_mode *mode,
230 int32_t scale)
231{
232 int ret;
233
234 if (!output->switch_mode)
235 return -1;
236
237 /* original_mode is the last mode non full screen clients have seen,
238 * so we shouldn't change it if we already have one set.
239 */
240 if (!output->original_mode) {
241 output->original_mode = output->native_mode;
242 output->original_scale = output->native_scale;
243 }
244 ret = output->switch_mode(output, mode);
245 if (ret < 0)
246 return ret;
247
248 output->current_scale = scale;
249
250 weston_mode_switch_finish(output, 0, 0);
251
252 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800253}
254
Benjamin Franzke06286262011-05-06 19:12:33 +0200255static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300256region_init_infinite(pixman_region32_t *region)
257{
258 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
259 UINT32_MAX, UINT32_MAX);
260}
261
Pekka Paalanene67858b2013-04-25 13:57:42 +0300262static struct weston_subsurface *
263weston_surface_to_subsurface(struct weston_surface *surface);
264
Jason Ekstranda7af7042013-10-12 22:38:11 -0500265WL_EXPORT struct weston_view *
266weston_view_create(struct weston_surface *surface)
267{
268 struct weston_view *view;
269
Bryce Harringtonde16d892014-11-20 22:21:57 -0800270 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 if (view == NULL)
272 return NULL;
273
274 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000275 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500276
Jason Ekstranda7af7042013-10-12 22:38:11 -0500277 /* Assign to surface */
278 wl_list_insert(&surface->views, &view->surface_link);
279
280 wl_signal_init(&view->destroy_signal);
281 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300282 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500283
Jason Ekstranda7af7042013-10-12 22:38:11 -0500284 pixman_region32_init(&view->clip);
285
286 view->alpha = 1.0;
287 pixman_region32_init(&view->transform.opaque);
288
289 wl_list_init(&view->geometry.transformation_list);
290 wl_list_insert(&view->geometry.transformation_list,
291 &view->transform.position.link);
292 weston_matrix_init(&view->transform.position.matrix);
293 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200294 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500295 pixman_region32_init(&view->transform.boundingbox);
296 view->transform.dirty = 1;
297
Jason Ekstranda7af7042013-10-12 22:38:11 -0500298 return view;
299}
300
Jason Ekstrand108865d2014-06-26 10:04:49 -0700301struct weston_frame_callback {
302 struct wl_resource *resource;
303 struct wl_list link;
304};
305
Pekka Paalanen133e4392014-09-23 22:08:46 -0400306struct weston_presentation_feedback {
307 struct wl_resource *resource;
308
309 /* XXX: could use just wl_resource_get_link() instead */
310 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200311
312 /* The per-surface feedback flags */
313 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400314};
315
316static void
317weston_presentation_feedback_discard(
318 struct weston_presentation_feedback *feedback)
319{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200320 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400321 wl_resource_destroy(feedback->resource);
322}
323
324static void
325weston_presentation_feedback_discard_list(struct wl_list *list)
326{
327 struct weston_presentation_feedback *feedback, *tmp;
328
329 wl_list_for_each_safe(feedback, tmp, list, link)
330 weston_presentation_feedback_discard(feedback);
331}
332
333static void
334weston_presentation_feedback_present(
335 struct weston_presentation_feedback *feedback,
336 struct weston_output *output,
337 uint32_t refresh_nsec,
338 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200339 uint64_t seq,
340 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400341{
342 struct wl_client *client = wl_resource_get_client(feedback->resource);
343 struct wl_resource *o;
344 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400345
346 wl_resource_for_each(o, &output->resource_list) {
347 if (wl_resource_get_client(o) != client)
348 continue;
349
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200350 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400351 }
352
353 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200354 wp_presentation_feedback_send_presented(feedback->resource,
355 secs >> 32, secs & 0xffffffff,
356 ts->tv_nsec,
357 refresh_nsec,
358 seq >> 32, seq & 0xffffffff,
359 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400360 wl_resource_destroy(feedback->resource);
361}
362
363static void
364weston_presentation_feedback_present_list(struct wl_list *list,
365 struct weston_output *output,
366 uint32_t refresh_nsec,
367 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200368 uint64_t seq,
369 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400370{
371 struct weston_presentation_feedback *feedback, *tmp;
372
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200373 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200374 wl_list_empty(list));
375
Pekka Paalanen133e4392014-09-23 22:08:46 -0400376 wl_list_for_each_safe(feedback, tmp, list, link)
377 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200378 refresh_nsec, ts, seq,
379 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400380}
381
Jason Ekstrand7b982072014-05-20 14:33:03 -0500382static void
383surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
384{
385 struct weston_surface_state *state =
386 container_of(listener, struct weston_surface_state,
387 buffer_destroy_listener);
388
389 state->buffer = NULL;
390}
391
392static void
393weston_surface_state_init(struct weston_surface_state *state)
394{
395 state->newly_attached = 0;
396 state->buffer = NULL;
397 state->buffer_destroy_listener.notify =
398 surface_state_handle_buffer_destroy;
399 state->sx = 0;
400 state->sy = 0;
401
Derek Foreman152254b2015-11-26 14:17:48 -0600402 pixman_region32_init(&state->damage_surface);
403 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500404 pixman_region32_init(&state->opaque);
405 region_init_infinite(&state->input);
406
407 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400408 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500409
410 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
411 state->buffer_viewport.buffer.scale = 1;
412 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
413 state->buffer_viewport.surface.width = -1;
414 state->buffer_viewport.changed = 0;
415}
416
417static void
418weston_surface_state_fini(struct weston_surface_state *state)
419{
420 struct weston_frame_callback *cb, *next;
421
422 wl_list_for_each_safe(cb, next,
423 &state->frame_callback_list, link)
424 wl_resource_destroy(cb->resource);
425
Pekka Paalanen133e4392014-09-23 22:08:46 -0400426 weston_presentation_feedback_discard_list(&state->feedback_list);
427
Jason Ekstrand7b982072014-05-20 14:33:03 -0500428 pixman_region32_fini(&state->input);
429 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600430 pixman_region32_fini(&state->damage_surface);
431 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500432
433 if (state->buffer)
434 wl_list_remove(&state->buffer_destroy_listener.link);
435 state->buffer = NULL;
436}
437
438static void
439weston_surface_state_set_buffer(struct weston_surface_state *state,
440 struct weston_buffer *buffer)
441{
442 if (state->buffer == buffer)
443 return;
444
445 if (state->buffer)
446 wl_list_remove(&state->buffer_destroy_listener.link);
447 state->buffer = buffer;
448 if (state->buffer)
449 wl_signal_add(&state->buffer->destroy_signal,
450 &state->buffer_destroy_listener);
451}
452
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500453WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500454weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500455{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500456 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400457
Bryce Harringtonde16d892014-11-20 22:21:57 -0800458 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400459 if (surface == NULL)
460 return NULL;
461
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500462 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800463 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500464
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500465 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200466 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400467
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200468 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
469 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200470 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
471 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500472
473 weston_surface_state_init(&surface->pending);
474
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400475 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500476 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300477 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400478
Jason Ekstranda7af7042013-10-12 22:38:11 -0500479 wl_list_init(&surface->views);
480
481 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400482 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500483
Pekka Paalanene67858b2013-04-25 13:57:42 +0300484 wl_list_init(&surface->subsurface_list);
485 wl_list_init(&surface->subsurface_list_pending);
486
Jason Ekstrand1e059042014-10-16 10:55:19 -0500487 weston_matrix_init(&surface->buffer_to_surface_matrix);
488 weston_matrix_init(&surface->surface_to_buffer_matrix);
489
Jonas Ådahld3414f22016-07-22 17:56:31 +0800490 wl_list_init(&surface->pointer_constraints);
491
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400492 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500493}
494
Alex Wu8811bf92012-02-28 18:07:54 +0800495WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500496weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200497 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500498{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100499 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500500}
501
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400502WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500503weston_view_to_global_float(struct weston_view *view,
504 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200505{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500506 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200507 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
508
Jason Ekstranda7af7042013-10-12 22:38:11 -0500509 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200510
511 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200512 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700513 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200514 v.f[3]);
515 *x = 0;
516 *y = 0;
517 return;
518 }
519
520 *x = v.f[0] / v.f[3];
521 *y = v.f[1] / v.f[3];
522 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500523 *x = sx + view->geometry.x;
524 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200525 }
526}
527
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500528WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200529weston_transformed_coord(int width, int height,
530 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200531 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200532 float sx, float sy, float *bx, float *by)
533{
534 switch (transform) {
535 case WL_OUTPUT_TRANSFORM_NORMAL:
536 default:
537 *bx = sx;
538 *by = sy;
539 break;
540 case WL_OUTPUT_TRANSFORM_FLIPPED:
541 *bx = width - sx;
542 *by = sy;
543 break;
544 case WL_OUTPUT_TRANSFORM_90:
545 *bx = height - sy;
546 *by = sx;
547 break;
548 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
549 *bx = height - sy;
550 *by = width - sx;
551 break;
552 case WL_OUTPUT_TRANSFORM_180:
553 *bx = width - sx;
554 *by = height - sy;
555 break;
556 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
557 *bx = sx;
558 *by = height - sy;
559 break;
560 case WL_OUTPUT_TRANSFORM_270:
561 *bx = sy;
562 *by = width - sx;
563 break;
564 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
565 *bx = sy;
566 *by = sx;
567 break;
568 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200569
570 *bx *= scale;
571 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200572}
573
574WL_EXPORT pixman_box32_t
575weston_transformed_rect(int width, int height,
576 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200577 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200578 pixman_box32_t rect)
579{
580 float x1, x2, y1, y2;
581
582 pixman_box32_t ret;
583
Alexander Larsson4ea95522013-05-22 14:41:37 +0200584 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200585 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200586 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200587 rect.x2, rect.y2, &x2, &y2);
588
589 if (x1 <= x2) {
590 ret.x1 = x1;
591 ret.x2 = x2;
592 } else {
593 ret.x1 = x2;
594 ret.x2 = x1;
595 }
596
597 if (y1 <= y2) {
598 ret.y1 = y1;
599 ret.y2 = y2;
600 } else {
601 ret.y1 = y2;
602 ret.y2 = y1;
603 }
604
605 return ret;
606}
607
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600608/** Transform a region by a matrix, restricted to axis-aligned transformations
609 *
610 * Warning: This function does not work for projective, affine, or matrices
611 * that encode arbitrary rotations. Only 90-degree step rotations are
612 * supported.
613 */
614WL_EXPORT void
615weston_matrix_transform_region(pixman_region32_t *dest,
616 struct weston_matrix *matrix,
617 pixman_region32_t *src)
618{
619 pixman_box32_t *src_rects, *dest_rects;
620 int nrects, i;
621
622 src_rects = pixman_region32_rectangles(src, &nrects);
623 dest_rects = malloc(nrects * sizeof(*dest_rects));
624 if (!dest_rects)
625 return;
626
627 for (i = 0; i < nrects; i++) {
628 struct weston_vector vec1 = {{
629 src_rects[i].x1, src_rects[i].y1, 0, 1
630 }};
631 weston_matrix_transform(matrix, &vec1);
632 vec1.f[0] /= vec1.f[3];
633 vec1.f[1] /= vec1.f[3];
634
635 struct weston_vector vec2 = {{
636 src_rects[i].x2, src_rects[i].y2, 0, 1
637 }};
638 weston_matrix_transform(matrix, &vec2);
639 vec2.f[0] /= vec2.f[3];
640 vec2.f[1] /= vec2.f[3];
641
642 if (vec1.f[0] < vec2.f[0]) {
643 dest_rects[i].x1 = floor(vec1.f[0]);
644 dest_rects[i].x2 = ceil(vec2.f[0]);
645 } else {
646 dest_rects[i].x1 = floor(vec2.f[0]);
647 dest_rects[i].x2 = ceil(vec1.f[0]);
648 }
649
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600650 if (vec1.f[1] < vec2.f[1]) {
651 dest_rects[i].y1 = floor(vec1.f[1]);
652 dest_rects[i].y2 = ceil(vec2.f[1]);
653 } else {
654 dest_rects[i].y1 = floor(vec2.f[1]);
655 dest_rects[i].y2 = ceil(vec1.f[1]);
656 }
657 }
658
659 pixman_region32_clear(dest);
660 pixman_region32_init_rects(dest, dest_rects, nrects);
661 free(dest_rects);
662}
663
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200664WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500665weston_transformed_region(int width, int height,
666 enum wl_output_transform transform,
667 int32_t scale,
668 pixman_region32_t *src, pixman_region32_t *dest)
669{
670 pixman_box32_t *src_rects, *dest_rects;
671 int nrects, i;
672
673 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
674 if (src != dest)
675 pixman_region32_copy(dest, src);
676 return;
677 }
678
679 src_rects = pixman_region32_rectangles(src, &nrects);
680 dest_rects = malloc(nrects * sizeof(*dest_rects));
681 if (!dest_rects)
682 return;
683
684 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
685 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
686 } else {
687 for (i = 0; i < nrects; i++) {
688 switch (transform) {
689 default:
690 case WL_OUTPUT_TRANSFORM_NORMAL:
691 dest_rects[i].x1 = src_rects[i].x1;
692 dest_rects[i].y1 = src_rects[i].y1;
693 dest_rects[i].x2 = src_rects[i].x2;
694 dest_rects[i].y2 = src_rects[i].y2;
695 break;
696 case WL_OUTPUT_TRANSFORM_90:
697 dest_rects[i].x1 = height - src_rects[i].y2;
698 dest_rects[i].y1 = src_rects[i].x1;
699 dest_rects[i].x2 = height - src_rects[i].y1;
700 dest_rects[i].y2 = src_rects[i].x2;
701 break;
702 case WL_OUTPUT_TRANSFORM_180:
703 dest_rects[i].x1 = width - src_rects[i].x2;
704 dest_rects[i].y1 = height - src_rects[i].y2;
705 dest_rects[i].x2 = width - src_rects[i].x1;
706 dest_rects[i].y2 = height - src_rects[i].y1;
707 break;
708 case WL_OUTPUT_TRANSFORM_270:
709 dest_rects[i].x1 = src_rects[i].y1;
710 dest_rects[i].y1 = width - src_rects[i].x2;
711 dest_rects[i].x2 = src_rects[i].y2;
712 dest_rects[i].y2 = width - src_rects[i].x1;
713 break;
714 case WL_OUTPUT_TRANSFORM_FLIPPED:
715 dest_rects[i].x1 = width - src_rects[i].x2;
716 dest_rects[i].y1 = src_rects[i].y1;
717 dest_rects[i].x2 = width - src_rects[i].x1;
718 dest_rects[i].y2 = src_rects[i].y2;
719 break;
720 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
721 dest_rects[i].x1 = height - src_rects[i].y2;
722 dest_rects[i].y1 = width - src_rects[i].x2;
723 dest_rects[i].x2 = height - src_rects[i].y1;
724 dest_rects[i].y2 = width - src_rects[i].x1;
725 break;
726 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
727 dest_rects[i].x1 = src_rects[i].x1;
728 dest_rects[i].y1 = height - src_rects[i].y2;
729 dest_rects[i].x2 = src_rects[i].x2;
730 dest_rects[i].y2 = height - src_rects[i].y1;
731 break;
732 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
733 dest_rects[i].x1 = src_rects[i].y1;
734 dest_rects[i].y1 = src_rects[i].x1;
735 dest_rects[i].x2 = src_rects[i].y2;
736 dest_rects[i].y2 = src_rects[i].x2;
737 break;
738 }
739 }
740 }
741
742 if (scale != 1) {
743 for (i = 0; i < nrects; i++) {
744 dest_rects[i].x1 *= scale;
745 dest_rects[i].x2 *= scale;
746 dest_rects[i].y1 *= scale;
747 dest_rects[i].y2 *= scale;
748 }
749 }
750
751 pixman_region32_clear(dest);
752 pixman_region32_init_rects(dest, dest_rects, nrects);
753 free(dest_rects);
754}
755
Jonny Lamb74130762013-11-26 18:19:46 +0100756static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300757viewport_surface_to_buffer(struct weston_surface *surface,
758 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100759{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200760 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200761 double src_width, src_height;
762 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200763
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200764 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
765 if (vp->surface.width == -1) {
766 *bx = sx;
767 *by = sy;
768 return;
769 }
Jonny Lamb74130762013-11-26 18:19:46 +0100770
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200771 src_x = 0.0;
772 src_y = 0.0;
773 src_width = surface->width_from_buffer;
774 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100775 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200776 src_x = wl_fixed_to_double(vp->buffer.src_x);
777 src_y = wl_fixed_to_double(vp->buffer.src_y);
778 src_width = wl_fixed_to_double(vp->buffer.src_width);
779 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100780 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200781
782 *bx = sx * src_width / surface->width + src_x;
783 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100784}
785
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500786WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200787weston_surface_to_buffer_float(struct weston_surface *surface,
788 float sx, float sy, float *bx, float *by)
789{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200790 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
791
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300792 /* first transform coordinates if the viewport is set */
793 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100794
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500795 weston_transformed_coord(surface->width_from_buffer,
796 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200797 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100798 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200799}
800
Derek Foreman99739672015-12-03 16:38:11 -0600801/** Transform a rectangle from surface coordinates to buffer coordinates
802 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300803 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600804 * from.
805 * \param rect The rectangle to transform.
806 * \return The transformed rectangle.
807 *
808 * Viewport and buffer transformations can only do translation, scaling,
809 * and rotations in 90-degree steps. Therefore the only loss in the
810 * conversion is coordinate rounding.
811 *
812 * However, some coordinate rounding takes place as an intermediate
813 * step before the buffer scale factor is applied, so the rectangle
814 * boundary may not be exactly as expected.
815 *
816 * This is OK for damage tracking since a little extra coverage is
817 * not a problem.
818 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200819WL_EXPORT pixman_box32_t
820weston_surface_to_buffer_rect(struct weston_surface *surface,
821 pixman_box32_t rect)
822{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200823 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100824 float xf, yf;
825
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300826 /* first transform box coordinates if the viewport is set */
827 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100828 rect.x1 = floorf(xf);
829 rect.y1 = floorf(yf);
830
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300831 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600832 rect.x2 = ceilf(xf);
833 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100834
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500835 return weston_transformed_rect(surface->width_from_buffer,
836 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200837 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200838 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200839}
840
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200841/** Transform a region from surface coordinates to buffer coordinates
842 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300843 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200844 * from.
845 * \param surface_region[in] The region in surface coordinates.
846 * \param buffer_region[out] The region converted to buffer coordinates.
847 *
848 * Buffer_region must be init'd, but will be completely overwritten.
849 *
850 * Viewport and buffer transformations can only do translation, scaling,
851 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600852 * conversion is from the coordinate rounding that takes place in
853 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200854 */
855WL_EXPORT void
856weston_surface_to_buffer_region(struct weston_surface *surface,
857 pixman_region32_t *surface_region,
858 pixman_region32_t *buffer_region)
859{
860 pixman_box32_t *src_rects, *dest_rects;
861 int nrects, i;
862
863 src_rects = pixman_region32_rectangles(surface_region, &nrects);
864 dest_rects = malloc(nrects * sizeof(*dest_rects));
865 if (!dest_rects)
866 return;
867
868 for (i = 0; i < nrects; i++) {
869 dest_rects[i] = weston_surface_to_buffer_rect(surface,
870 src_rects[i]);
871 }
872
873 pixman_region32_fini(buffer_region);
874 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
875 free(dest_rects);
876}
877
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200878WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400880 struct weston_plane *plane)
881{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500882 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400883 return;
884
Jason Ekstranda7af7042013-10-12 22:38:11 -0500885 weston_view_damage_below(view);
886 view->plane = plane;
887 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400888}
889
Pekka Paalanen51723d52015-02-17 13:10:01 +0200890/** Inflict damage on the plane where the view is visible.
891 *
892 * \param view The view that causes the damage.
893 *
894 * If the view is currently on a plane (including the primary plane),
895 * take the view's boundingbox, subtract all the opaque views that cover it,
896 * and add the remaining region as damage to the plane. This corresponds
897 * to the damage inflicted to the plane if this view disappeared.
898 *
899 * A repaint is scheduled for this view.
900 *
901 * The region of all opaque views covering this view is stored in
902 * weston_view::clip and updated by view_accumulate_damage() during
903 * weston_output_repaint(). Specifically, that region matches the
904 * scenegraph as it was last painted.
905 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400906WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500907weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200908{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500909 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200910
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500911 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200912 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500913 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800914 if (view->plane)
915 pixman_region32_union(&view->plane->damage,
916 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500917 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800918 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200919}
920
Bryce Harrington3f650b82015-12-23 11:01:58 -0800921/**
922 * \param es The surface
923 * \param mask The new set of outputs for the surface
924 *
925 * Sets the surface's set of outputs to the ones specified by
926 * the new output mask provided. Identifies the outputs that
927 * have changed, the posts enter and leave events for these
928 * outputs as appropriate.
929 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400930static void
931weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
932{
933 uint32_t different = es->output_mask ^ mask;
934 uint32_t entered = mask & different;
935 uint32_t left = es->output_mask & different;
936 struct weston_output *output;
937 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500938 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400939
940 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500941 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400942 return;
943 if (different == 0)
944 return;
945
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500946 client = wl_resource_get_client(es->resource);
947
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400948 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800949 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400950 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500951 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400952 client);
953 if (resource == NULL)
954 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800955 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500956 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800957 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500958 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400959 }
960}
961
Bryce Harrington3f650b82015-12-23 11:01:58 -0800962/** Recalculate which output(s) the surface has views displayed on
963 *
964 * \param es The surface to remap to outputs
965 *
966 * Finds the output that is showing the largest amount of one
967 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300968 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800969 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300970 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800971 * in the output_mask for the surface.
972 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400973static void
974weston_surface_assign_output(struct weston_surface *es)
975{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 struct weston_output *new_output;
977 struct weston_view *view;
978 pixman_region32_t region;
979 uint32_t max, area, mask;
980 pixman_box32_t *e;
981
982 new_output = NULL;
983 max = 0;
984 mask = 0;
985 pixman_region32_init(&region);
986 wl_list_for_each(view, &es->views, surface_link) {
987 if (!view->output)
988 continue;
989
990 pixman_region32_intersect(&region, &view->transform.boundingbox,
991 &view->output->region);
992
993 e = pixman_region32_extents(&region);
994 area = (e->x2 - e->x1) * (e->y2 - e->y1);
995
996 mask |= view->output_mask;
997
998 if (area >= max) {
999 new_output = view->output;
1000 max = area;
1001 }
1002 }
1003 pixman_region32_fini(&region);
1004
1005 es->output = new_output;
1006 weston_surface_update_output_mask(es, mask);
1007}
1008
Bryce Harrington3f650b82015-12-23 11:01:58 -08001009/** Recalculate which output(s) the view is displayed on
1010 *
1011 * \param ev The view to remap to outputs
1012 *
1013 * Identifies the set of outputs that the view is visible on,
1014 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001015 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001016 *
1017 * Also does the same for the view's surface. See
1018 * weston_surface_assign_output().
1019 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020static void
1021weston_view_assign_output(struct weston_view *ev)
1022{
1023 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001024 struct weston_output *output, *new_output;
1025 pixman_region32_t region;
1026 uint32_t max, area, mask;
1027 pixman_box32_t *e;
1028
1029 new_output = NULL;
1030 max = 0;
1031 mask = 0;
1032 pixman_region32_init(&region);
1033 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001034 if (output->destroying)
1035 continue;
1036
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001038 &output->region);
1039
1040 e = pixman_region32_extents(&region);
1041 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1042
1043 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001044 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001045
1046 if (area >= max) {
1047 new_output = output;
1048 max = area;
1049 }
1050 }
1051 pixman_region32_fini(&region);
1052
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 ev->output = new_output;
1054 ev->output_mask = mask;
1055
1056 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001057}
1058
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001059static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001060weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1061 int from_x, int from_y, int *to_x, int *to_y)
1062{
1063 float x, y;
1064
1065 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1066 weston_view_from_global_float(to, x, y, &x, &y);
1067
1068 *to_x = round(x);
1069 *to_y = round(y);
1070}
1071
1072static void
1073weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1074{
1075 pixman_box32_t *a;
1076 pixman_box32_t b;
1077
1078 a = pixman_region32_extents(&from->geometry.scissor);
1079
1080 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1081 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1082
1083 pixman_region32_fini(&to->geometry.scissor);
1084 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1085}
1086
1087static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001088view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001090{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001091 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1092 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001093 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001094 { inbox->x1, inbox->y1 },
1095 { inbox->x1, inbox->y2 },
1096 { inbox->x2, inbox->y1 },
1097 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001098 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001099 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001100 int i;
1101
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001102 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001103 /* avoid rounding empty bbox to 1x1 */
1104 pixman_region32_init(bbox);
1105 return;
1106 }
1107
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001108 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001109 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001110 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001111 if (x < min_x)
1112 min_x = x;
1113 if (x > max_x)
1114 max_x = x;
1115 if (y < min_y)
1116 min_y = y;
1117 if (y > max_y)
1118 max_y = y;
1119 }
1120
Pekka Paalanen219b9822012-02-08 15:38:37 +02001121 int_x = floorf(min_x);
1122 int_y = floorf(min_y);
1123 pixman_region32_init_rect(bbox, int_x, int_y,
1124 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001125}
1126
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001127static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001129{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001131
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001132 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 view->geometry.x = roundf(view->geometry.x);
1134 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001135
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001136 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1138 view->transform.position.matrix.d[12] = view->geometry.x;
1139 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001140
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001142
Jason Ekstranda7af7042013-10-12 22:38:11 -05001143 view->transform.inverse = view->transform.position.matrix;
1144 view->transform.inverse.d[12] = -view->geometry.x;
1145 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001146
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001148 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001149 view->surface->width,
1150 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001151 if (view->geometry.scissor_enabled)
1152 pixman_region32_intersect(&view->transform.boundingbox,
1153 &view->transform.boundingbox,
1154 &view->geometry.scissor);
1155
1156 pixman_region32_translate(&view->transform.boundingbox,
1157 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001158
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 if (view->alpha == 1.0) {
1160 pixman_region32_copy(&view->transform.opaque,
1161 &view->surface->opaque);
1162 pixman_region32_translate(&view->transform.opaque,
1163 view->geometry.x,
1164 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001165 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001166}
1167
1168static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001170{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171 struct weston_view *parent = view->geometry.parent;
1172 struct weston_matrix *matrix = &view->transform.matrix;
1173 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001174 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001175 pixman_region32_t surfregion;
1176 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001177
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001179
1180 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1182 view->transform.position.matrix.d[12] = view->geometry.x;
1183 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001184
1185 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001187 weston_matrix_multiply(matrix, &tform->matrix);
1188
Pekka Paalanen483243f2013-03-08 14:56:50 +02001189 if (parent)
1190 weston_matrix_multiply(matrix, &parent->transform.matrix);
1191
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001192 if (weston_matrix_invert(inverse, matrix) < 0) {
1193 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 weston_log("error: weston_view %p"
1195 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001196 return -1;
1197 }
1198
Giulio Camuffo148c1992016-09-04 18:50:46 +03001199 if (view->alpha == 1.0 &&
1200 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1201 pixman_region32_copy(&view->transform.opaque,
1202 &view->surface->opaque);
1203 pixman_region32_translate(&view->transform.opaque,
1204 matrix->d[12],
1205 matrix->d[13]);
1206 }
1207
Pekka Paalanen380adf52015-02-16 14:39:11 +02001208 pixman_region32_init_rect(&surfregion, 0, 0,
1209 view->surface->width, view->surface->height);
1210 if (view->geometry.scissor_enabled)
1211 pixman_region32_intersect(&surfregion, &surfregion,
1212 &view->geometry.scissor);
1213 surfbox = pixman_region32_extents(&surfregion);
1214
1215 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1216 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001217
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001218 return 0;
1219}
1220
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001221static struct weston_layer *
1222get_view_layer(struct weston_view *view)
1223{
1224 if (view->parent_view)
1225 return get_view_layer(view->parent_view);
1226 return view->layer_link.layer;
1227}
1228
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001229WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001231{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001233 struct weston_layer *layer;
1234 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001235
Jason Ekstranda7af7042013-10-12 22:38:11 -05001236 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001237 return;
1238
Pekka Paalanen483243f2013-03-08 14:56:50 +02001239 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001241
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001243
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001245
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 pixman_region32_fini(&view->transform.boundingbox);
1247 pixman_region32_fini(&view->transform.opaque);
1248 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001249
Pekka Paalanencd403622012-01-25 13:37:39 +02001250 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001251 if (view->geometry.transformation_list.next ==
1252 &view->transform.position.link &&
1253 view->geometry.transformation_list.prev ==
1254 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001255 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001257 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 if (weston_view_update_transform_enable(view) < 0)
1259 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001260 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001261
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001262 layer = get_view_layer(view);
1263 if (layer) {
1264 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001265 pixman_region32_intersect(&view->transform.boundingbox,
1266 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001267 pixman_region32_intersect(&view->transform.opaque,
1268 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001269 pixman_region32_fini(&mask);
1270 }
1271
Pekka Paalanen380adf52015-02-16 14:39:11 +02001272 if (parent) {
1273 if (parent->geometry.scissor_enabled) {
1274 view->geometry.scissor_enabled = true;
1275 weston_view_transfer_scissor(parent, view);
1276 } else {
1277 view->geometry.scissor_enabled = false;
1278 }
1279 }
1280
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001282
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001284
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 wl_signal_emit(&view->surface->compositor->transform_signal,
1286 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001287}
1288
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001289WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001291{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001293
1294 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 * The invariant: if view->geometry.dirty, then all views
1296 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001297 * Corollary: if not parent->geometry.dirty, then all ancestors
1298 * are not dirty.
1299 */
1300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001302 return;
1303
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001305
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001307 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001309}
1310
1311WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312weston_view_to_global_fixed(struct weston_view *view,
1313 wl_fixed_t vx, wl_fixed_t vy,
1314 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001315{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001316 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001317
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 weston_view_to_global_float(view,
1319 wl_fixed_to_double(vx),
1320 wl_fixed_to_double(vy),
1321 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001322 *x = wl_fixed_from_double(xf);
1323 *y = wl_fixed_from_double(yf);
1324}
1325
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001326WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327weston_view_from_global_float(struct weston_view *view,
1328 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001329{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001331 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1332
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001334
1335 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001336 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001338 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339 *vx = 0;
1340 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001341 return;
1342 }
1343
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 *vx = v.f[0] / v.f[3];
1345 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001346 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 *vx = x - view->geometry.x;
1348 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001349 }
1350}
1351
1352WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353weston_view_from_global_fixed(struct weston_view *view,
1354 wl_fixed_t x, wl_fixed_t y,
1355 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001356{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 weston_view_from_global_float(view,
1360 wl_fixed_to_double(x),
1361 wl_fixed_to_double(y),
1362 &vxf, &vyf);
1363 *vx = wl_fixed_from_double(vxf);
1364 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001365}
1366
1367WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368weston_view_from_global(struct weston_view *view,
1369 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001370{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001372
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1374 *vx = floorf(vxf);
1375 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001376}
1377
Bryce Harrington3f650b82015-12-23 11:01:58 -08001378/**
1379 * \param surface The surface to be repainted
1380 *
1381 * Marks the output(s) that the surface is shown on as needing to be
1382 * repainted. See weston_output_schedule_repaint().
1383 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001384WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001385weston_surface_schedule_repaint(struct weston_surface *surface)
1386{
1387 struct weston_output *output;
1388
1389 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001390 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001391 weston_output_schedule_repaint(output);
1392}
1393
Bryce Harrington3f650b82015-12-23 11:01:58 -08001394/**
1395 * \param view The view to be repainted
1396 *
1397 * Marks the output(s) that the view is shown on as needing to be
1398 * repainted. See weston_output_schedule_repaint().
1399 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001400WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401weston_view_schedule_repaint(struct weston_view *view)
1402{
1403 struct weston_output *output;
1404
1405 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001406 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 weston_output_schedule_repaint(output);
1408}
1409
Pekka Paalanene508ce62015-02-19 13:59:55 +02001410/**
1411 * XXX: This function does it the wrong way.
1412 * surface->damage is the damage from the client, and causes
1413 * surface_flush_damage() to copy pixels. No window management action can
1414 * cause damage to the client-provided content, warranting re-upload!
1415 *
1416 * Instead of surface->damage, this function should record the damage
1417 * with all the views for this surface to avoid extraneous texture
1418 * uploads.
1419 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001421weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001422{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001423 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 0, 0, surface->width,
1425 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001426
Kristian Høgsberg98238702012-08-03 16:29:12 -04001427 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001428}
1429
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001430WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001432{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001433 if (view->geometry.x == x && view->geometry.y == y)
1434 return;
1435
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 view->geometry.x = x;
1437 view->geometry.y = y;
1438 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001439}
1440
Pekka Paalanen483243f2013-03-08 14:56:50 +02001441static void
1442transform_parent_handle_parent_destroy(struct wl_listener *listener,
1443 void *data)
1444{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 struct weston_view *view =
1446 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001447 geometry.parent_destroy_listener);
1448
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001450}
1451
1452WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001454 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001455{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 if (view->geometry.parent) {
1457 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1458 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001459
1460 if (!parent)
1461 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001462 }
1463
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001465
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001467 transform_parent_handle_parent_destroy;
1468 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001469 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001471 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001473 }
1474
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 weston_view_geometry_dirty(view);
1476}
1477
Pekka Paalanen380adf52015-02-16 14:39:11 +02001478/** Set a clip mask rectangle on a view
1479 *
1480 * \param view The view to set the clip mask on.
1481 * \param x Top-left corner X coordinate of the clip rectangle.
1482 * \param y Top-left corner Y coordinate of the clip rectangle.
1483 * \param width Width of the clip rectangle, non-negative.
1484 * \param height Height of the clip rectangle, non-negative.
1485 *
1486 * A shell may set a clip mask rectangle on a view. Everything outside
1487 * the rectangle is cut away for input and output purposes: it is
1488 * not drawn and cannot be hit by hit-test based input like pointer
1489 * motion or touch-downs. Everything inside the rectangle will behave
1490 * normally. Clients are unaware of clipping.
1491 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001492 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001493 * mask rectangle does not affect the view position, the view is positioned
1494 * as it would be without a clip. The clip also does not change
1495 * weston_surface::width,height.
1496 *
1497 * The clip mask rectangle is part of transformation inheritance
1498 * (weston_view_set_transform_parent()). A clip set in the root of the
1499 * transformation inheritance tree will affect all views in the tree.
1500 * A clip can be set only on the root view. Attempting to set a clip
1501 * on view that has a transformation parent will fail. Assigning a parent
1502 * to a view that has a clip set will cause the clip to be forgotten.
1503 *
1504 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1505 * on the additional transformations in the child views. These transformations
1506 * may not rotate the coordinate axes, i.e., only translation and scaling
1507 * are allowed. Violating this restriction causes the clipping to malfunction.
1508 * Furthermore, using scaling may cause rounding errors in child clipping.
1509 *
1510 * The clip mask rectangle is not automatically adjusted based on
1511 * wl_surface.attach dx and dy arguments.
1512 *
1513 * A clip mask rectangle can be set only if the compositor capability
1514 * WESTON_CAP_VIEW_CLIP_MASK is present.
1515 *
1516 * This function sets the clip mask rectangle and schedules a repaint for
1517 * the view.
1518 */
1519WL_EXPORT void
1520weston_view_set_mask(struct weston_view *view,
1521 int x, int y, int width, int height)
1522{
1523 struct weston_compositor *compositor = view->surface->compositor;
1524
1525 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1526 weston_log("%s not allowed without capability!\n", __func__);
1527 return;
1528 }
1529
1530 if (view->geometry.parent) {
1531 weston_log("view %p has a parent, clip forbidden!\n", view);
1532 return;
1533 }
1534
1535 if (width < 0 || height < 0) {
1536 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1537 x, y, width, height);
1538 return;
1539 }
1540
1541 pixman_region32_fini(&view->geometry.scissor);
1542 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1543 view->geometry.scissor_enabled = true;
1544 weston_view_geometry_dirty(view);
1545 weston_view_schedule_repaint(view);
1546}
1547
1548/** Remove the clip mask from a view
1549 *
1550 * \param view The view to remove the clip mask from.
1551 *
1552 * Removed the clip mask rectangle and schedules a repaint.
1553 *
1554 * \sa weston_view_set_mask
1555 */
1556WL_EXPORT void
1557weston_view_set_mask_infinite(struct weston_view *view)
1558{
1559 view->geometry.scissor_enabled = false;
1560 weston_view_geometry_dirty(view);
1561 weston_view_schedule_repaint(view);
1562}
1563
Armin Krezović0da12b82016-06-30 06:04:33 +02001564/* Check if view should be displayed
1565 *
1566 * The indicator is set manually when assigning
1567 * a view to a surface.
1568 *
1569 * This needs reworking. See the thread starting at:
1570 *
1571 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1572 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001573WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574weston_view_is_mapped(struct weston_view *view)
1575{
Armin Krezović0da12b82016-06-30 06:04:33 +02001576 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001577}
1578
Armin Krezović0da12b82016-06-30 06:04:33 +02001579/* Check if a surface has a view assigned to it
1580 *
1581 * The indicator is set manually when mapping
1582 * a surface and creating a view for it.
1583 *
1584 * This needs to go. See the thread starting at:
1585 *
1586 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1587 *
1588 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001589WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001590weston_surface_is_mapped(struct weston_surface *surface)
1591{
Armin Krezović0da12b82016-06-30 06:04:33 +02001592 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001593}
1594
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001595static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001596surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001597{
1598 struct weston_view *view;
1599
1600 if (surface->width == width && surface->height == height)
1601 return;
1602
1603 surface->width = width;
1604 surface->height = height;
1605
1606 wl_list_for_each(view, &surface->views, surface_link)
1607 weston_view_geometry_dirty(view);
1608}
1609
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001610WL_EXPORT void
1611weston_surface_set_size(struct weston_surface *surface,
1612 int32_t width, int32_t height)
1613{
1614 assert(!surface->resource);
1615 surface_set_size(surface, width, height);
1616}
1617
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001618static int
1619fixed_round_up_to_int(wl_fixed_t f)
1620{
1621 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1622}
1623
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001624static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001625convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1626 int32_t width, int32_t height,
1627 uint32_t transform,
1628 int32_t scale)
1629{
1630 assert(scale > 0);
1631
1632 switch (transform) {
1633 case WL_OUTPUT_TRANSFORM_NORMAL:
1634 case WL_OUTPUT_TRANSFORM_180:
1635 case WL_OUTPUT_TRANSFORM_FLIPPED:
1636 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1637 *width_out = width / scale;
1638 *height_out = height / scale;
1639 break;
1640 case WL_OUTPUT_TRANSFORM_90:
1641 case WL_OUTPUT_TRANSFORM_270:
1642 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1643 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1644 *width_out = height / scale;
1645 *height_out = width / scale;
1646 break;
1647 default:
1648 assert(0 && "invalid transform");
1649 }
1650}
1651
1652static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001653weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001654{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001655 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001656
1657 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001658 surface->width_from_buffer = 0;
1659 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001660 return;
1661 }
1662
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001663 convert_size_by_transform_scale(&surface->width_from_buffer,
1664 &surface->height_from_buffer,
1665 surface->buffer_ref.buffer->width,
1666 surface->buffer_ref.buffer->height,
1667 vp->buffer.transform,
1668 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001669}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001670
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001671static void
1672weston_surface_update_size(struct weston_surface *surface)
1673{
1674 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1675 int32_t width, height;
1676
1677 width = surface->width_from_buffer;
1678 height = surface->height_from_buffer;
1679
1680 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001681 surface_set_size(surface,
1682 vp->surface.width, vp->surface.height);
1683 return;
1684 }
1685
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001686 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001687 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1688 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1689
1690 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001691 return;
1692 }
1693
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001694 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001695}
1696
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001697WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001698weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001699{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001700 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001701
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001702 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001703
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001704 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001705}
1706
Jason Ekstranda7af7042013-10-12 22:38:11 -05001707WL_EXPORT struct weston_view *
1708weston_compositor_pick_view(struct weston_compositor *compositor,
1709 wl_fixed_t x, wl_fixed_t y,
1710 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001711{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001712 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001713 wl_fixed_t view_x, view_y;
1714 int view_ix, view_iy;
1715 int ix = wl_fixed_to_int(x);
1716 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001717
Jason Ekstranda7af7042013-10-12 22:38:11 -05001718 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001719 if (!pixman_region32_contains_point(
1720 &view->transform.boundingbox, ix, iy, NULL))
1721 continue;
1722
1723 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1724 view_ix = wl_fixed_to_int(view_x);
1725 view_iy = wl_fixed_to_int(view_y);
1726
1727 if (!pixman_region32_contains_point(&view->surface->input,
1728 view_ix, view_iy, NULL))
1729 continue;
1730
Pekka Paalanen380adf52015-02-16 14:39:11 +02001731 if (view->geometry.scissor_enabled &&
1732 !pixman_region32_contains_point(&view->geometry.scissor,
1733 view_ix, view_iy, NULL))
1734 continue;
1735
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001736 *vx = view_x;
1737 *vy = view_y;
1738 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001739 }
1740
Derek Foremanf9318d12015-05-11 15:40:11 -05001741 *vx = wl_fixed_from_int(-1000000);
1742 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001743 return NULL;
1744}
1745
1746static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001747weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001748{
Daniel Stone37816df2012-05-16 18:45:18 +01001749 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001750
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001751 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001752 return;
1753
Daniel Stone37816df2012-05-16 18:45:18 +01001754 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001755 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001756}
1757
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001758WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001759weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001760{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001761 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001762
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763 if (!weston_view_is_mapped(view))
1764 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001765
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766 weston_view_damage_below(view);
1767 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001768 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001769 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001770 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001771 wl_list_remove(&view->link);
1772 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001773 view->output_mask = 0;
1774 weston_surface_assign_output(view->surface);
1775
1776 if (weston_surface_is_mapped(view->surface))
1777 return;
1778
1779 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001780 struct weston_touch *touch = weston_seat_get_touch(seat);
1781 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1782 struct weston_keyboard *keyboard =
1783 weston_seat_get_keyboard(seat);
1784
1785 if (keyboard && keyboard->focus == view->surface)
1786 weston_keyboard_set_focus(keyboard, NULL);
1787 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001788 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001789 if (touch && touch->focus == view)
1790 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001791 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001792}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001793
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794WL_EXPORT void
1795weston_surface_unmap(struct weston_surface *surface)
1796{
1797 struct weston_view *view;
1798
Armin Krezovićf8486c32016-06-30 06:04:28 +02001799 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001800 wl_list_for_each(view, &surface->views, surface_link)
1801 weston_view_unmap(view);
1802 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001803}
1804
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001805static void
1806weston_surface_reset_pending_buffer(struct weston_surface *surface)
1807{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001808 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001809 surface->pending.sx = 0;
1810 surface->pending.sy = 0;
1811 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001812 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001813}
1814
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815WL_EXPORT void
1816weston_view_destroy(struct weston_view *view)
1817{
1818 wl_signal_emit(&view->destroy_signal, view);
1819
1820 assert(wl_list_empty(&view->geometry.child_list));
1821
1822 if (weston_view_is_mapped(view)) {
1823 weston_view_unmap(view);
1824 weston_compositor_build_view_list(view->surface->compositor);
1825 }
1826
1827 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001828 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829
1830 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001831 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001832 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001833 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834
1835 weston_view_set_transform_parent(view, NULL);
1836
Jason Ekstranda7af7042013-10-12 22:38:11 -05001837 wl_list_remove(&view->surface_link);
1838
1839 free(view);
1840}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001841
1842WL_EXPORT void
1843weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001844{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001845 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001846 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001847 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001848
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001849 if (--surface->ref_count > 0)
1850 return;
1851
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001852 assert(surface->resource == NULL);
1853
Pekka Paalanenca790762015-04-17 14:23:38 +03001854 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001855
Pekka Paalanene67858b2013-04-25 13:57:42 +03001856 assert(wl_list_empty(&surface->subsurface_list_pending));
1857 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001858
Jason Ekstranda7af7042013-10-12 22:38:11 -05001859 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1860 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001861
Jason Ekstrand7b982072014-05-20 14:33:03 -05001862 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001863
Pekka Paalanende685b82012-12-04 15:58:12 +02001864 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001865
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001866 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001867 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001868 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001869
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001870 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001871 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001872
Pekka Paalanen133e4392014-09-23 22:08:46 -04001873 weston_presentation_feedback_discard_list(&surface->feedback_list);
1874
Jonas Ådahld3414f22016-07-22 17:56:31 +08001875 wl_list_for_each_safe(constraint, next_constraint,
1876 &surface->pointer_constraints,
1877 link)
1878 weston_pointer_constraint_destroy(constraint);
1879
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001880 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001881}
1882
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001883static void
1884destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001885{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001886 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001887
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001888 assert(surface);
1889
Giulio Camuffo0d379742013-11-15 22:06:15 +01001890 /* Set the resource to NULL, since we don't want to leave a
1891 * dangling pointer if the surface was refcounted and survives
1892 * the weston_surface_destroy() call. */
1893 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001894
1895 if (surface->viewport_resource)
1896 wl_resource_set_user_data(surface->viewport_resource, NULL);
1897
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001898 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001899}
1900
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001901static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001902weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1903{
1904 struct weston_buffer *buffer =
1905 container_of(listener, struct weston_buffer, destroy_listener);
1906
1907 wl_signal_emit(&buffer->destroy_signal, buffer);
1908 free(buffer);
1909}
1910
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001911WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001912weston_buffer_from_resource(struct wl_resource *resource)
1913{
1914 struct weston_buffer *buffer;
1915 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001916
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001917 listener = wl_resource_get_destroy_listener(resource,
1918 weston_buffer_destroy_handler);
1919
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001920 if (listener)
1921 return container_of(listener, struct weston_buffer,
1922 destroy_listener);
1923
1924 buffer = zalloc(sizeof *buffer);
1925 if (buffer == NULL)
1926 return NULL;
1927
1928 buffer->resource = resource;
1929 wl_signal_init(&buffer->destroy_signal);
1930 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001931 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001932 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001933
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001934 return buffer;
1935}
1936
1937static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001938weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1939 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001940{
Pekka Paalanende685b82012-12-04 15:58:12 +02001941 struct weston_buffer_reference *ref =
1942 container_of(listener, struct weston_buffer_reference,
1943 destroy_listener);
1944
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001945 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001946 ref->buffer = NULL;
1947}
1948
1949WL_EXPORT void
1950weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001951 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001952{
1953 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001954 ref->buffer->busy_count--;
1955 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001956 assert(wl_resource_get_client(ref->buffer->resource));
1957 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001958 WL_BUFFER_RELEASE);
1959 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001960 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001961 }
1962
Pekka Paalanende685b82012-12-04 15:58:12 +02001963 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001964 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001965 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001966 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001967 }
1968
Pekka Paalanende685b82012-12-04 15:58:12 +02001969 ref->buffer = buffer;
1970 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1971}
1972
1973static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001974weston_surface_attach(struct weston_surface *surface,
1975 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001976{
1977 weston_buffer_reference(&surface->buffer_ref, buffer);
1978
Pekka Paalanena6421c42012-12-04 15:58:10 +02001979 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001980 if (weston_surface_is_mapped(surface))
1981 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001982 }
1983
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001984 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001985
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001986 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001987 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001988}
1989
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001990WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001991weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001992{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001993 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001994
1995 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001996 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001997}
1998
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001999WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002000weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002001{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002002 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002003
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002004 pixman_region32_union(&compositor->primary_plane.damage,
2005 &compositor->primary_plane.damage,
2006 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002007 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002008}
2009
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002010static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002011surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002012{
Pekka Paalanende685b82012-12-04 15:58:12 +02002013 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002014 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002015 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002016
Pekka Paalanenb5026542014-11-12 15:09:24 +02002017 if (weston_timeline_enabled_ &&
2018 pixman_region32_not_empty(&surface->damage))
2019 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2020 TLP_OUTPUT(surface->output), TLP_END);
2021
Jason Ekstrandef540082014-06-26 10:37:36 -07002022 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023}
2024
2025static void
2026view_accumulate_damage(struct weston_view *view,
2027 pixman_region32_t *opaque)
2028{
2029 pixman_region32_t damage;
2030
2031 pixman_region32_init(&damage);
2032 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002033 pixman_box32_t *extents;
2034
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002036 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002037 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002038 pixman_region32_copy(&damage, &view->surface->damage);
2039 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002040 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002041 }
2042
Pekka Paalanen380adf52015-02-16 14:39:11 +02002043 pixman_region32_intersect(&damage, &damage,
2044 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002045 pixman_region32_subtract(&damage, &damage, opaque);
2046 pixman_region32_union(&view->plane->damage,
2047 &view->plane->damage, &damage);
2048 pixman_region32_fini(&damage);
2049 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002050 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002051}
2052
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002053static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002054compositor_accumulate_damage(struct weston_compositor *ec)
2055{
2056 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002058 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002059
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002060 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002061
2062 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002063 pixman_region32_copy(&plane->clip, &clip);
2064
2065 pixman_region32_init(&opaque);
2066
Jason Ekstranda7af7042013-10-12 22:38:11 -05002067 wl_list_for_each(ev, &ec->view_list, link) {
2068 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002069 continue;
2070
Jason Ekstranda7af7042013-10-12 22:38:11 -05002071 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002072 }
2073
2074 pixman_region32_union(&clip, &clip, &opaque);
2075 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002076 }
2077
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002078 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002079
Jason Ekstranda7af7042013-10-12 22:38:11 -05002080 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002081 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082
2083 wl_list_for_each(ev, &ec->view_list, link) {
2084 if (ev->surface->touched)
2085 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002086 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087
2088 surface_flush_damage(ev->surface);
2089
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002090 /* Both the renderer and the backend have seen the buffer
2091 * by now. If renderer needs the buffer, it has its own
2092 * reference set. If the backend wants to keep the buffer
2093 * around for migrating the surface into a non-primary plane
2094 * later, keep_buffer is true. Otherwise, drop the core
2095 * reference now, and allow early buffer release. This enables
2096 * clients to use single-buffering.
2097 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002098 if (!ev->surface->keep_buffer)
2099 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002100 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002101}
2102
2103static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002104surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002105{
2106 struct weston_subsurface *sub;
2107
Pekka Paalanene67858b2013-04-25 13:57:42 +03002108 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002110 continue;
2111
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2113 wl_list_init(&sub->surface->views);
2114
2115 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002116 }
2117}
2118
2119static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002120surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002121{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002122 struct weston_subsurface *sub;
2123 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002124
Jason Ekstranda7af7042013-10-12 22:38:11 -05002125 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2126 if (sub->surface == surface)
2127 continue;
2128
George Kiagiadakised04d382014-06-13 18:10:26 +02002129 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2130 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002132 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002133
2134 surface_free_unused_subsurface_views(sub->surface);
2135 }
2136}
2137
2138static void
2139view_list_add_subsurface_view(struct weston_compositor *compositor,
2140 struct weston_subsurface *sub,
2141 struct weston_view *parent)
2142{
2143 struct weston_subsurface *child;
2144 struct weston_view *view = NULL, *iv;
2145
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002146 if (!weston_surface_is_mapped(sub->surface))
2147 return;
2148
Jason Ekstranda7af7042013-10-12 22:38:11 -05002149 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2150 if (iv->geometry.parent == parent) {
2151 view = iv;
2152 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002153 }
2154 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002155
2156 if (view) {
2157 /* Put it back in the surface's list of views */
2158 wl_list_remove(&view->surface_link);
2159 wl_list_insert(&sub->surface->views, &view->surface_link);
2160 } else {
2161 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002162 weston_view_set_position(view,
2163 sub->position.x,
2164 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002165 weston_view_set_transform_parent(view, parent);
2166 }
2167
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002168 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002170 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002171
Pekka Paalanenb188e912013-11-19 14:03:35 +02002172 if (wl_list_empty(&sub->surface->subsurface_list)) {
2173 wl_list_insert(compositor->view_list.prev, &view->link);
2174 return;
2175 }
2176
2177 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2178 if (child->surface == sub->surface)
2179 wl_list_insert(compositor->view_list.prev, &view->link);
2180 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002182 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002183}
2184
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002185/* This recursively adds the sub-surfaces for a view, relying on the
2186 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2187 * change first happens to the sub-surface list, and then automatically
2188 * propagates here. See weston_surface_damage_subsurfaces() for how the
2189 * sub-surfaces receive damage when the client changes the state.
2190 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191static void
2192view_list_add(struct weston_compositor *compositor,
2193 struct weston_view *view)
2194{
2195 struct weston_subsurface *sub;
2196
2197 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002198
Pekka Paalanenb188e912013-11-19 14:03:35 +02002199 if (wl_list_empty(&view->surface->subsurface_list)) {
2200 wl_list_insert(compositor->view_list.prev, &view->link);
2201 return;
2202 }
2203
2204 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2205 if (sub->surface == view->surface)
2206 wl_list_insert(compositor->view_list.prev, &view->link);
2207 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002208 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002209 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002210}
2211
2212static void
2213weston_compositor_build_view_list(struct weston_compositor *compositor)
2214{
2215 struct weston_view *view;
2216 struct weston_layer *layer;
2217
2218 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002219 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002220 surface_stash_subsurface_views(view->surface);
2221
2222 wl_list_init(&compositor->view_list);
2223 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002224 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225 view_list_add(compositor, view);
2226 }
2227 }
2228
2229 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002230 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002231 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002232}
2233
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002234static void
2235weston_output_take_feedback_list(struct weston_output *output,
2236 struct weston_surface *surface)
2237{
2238 struct weston_view *view;
2239 struct weston_presentation_feedback *feedback;
2240 uint32_t flags = 0xffffffff;
2241
2242 if (wl_list_empty(&surface->feedback_list))
2243 return;
2244
2245 /* All views must have the flag for the flag to survive. */
2246 wl_list_for_each(view, &surface->views, surface_link) {
2247 /* ignore views that are not on this output at all */
2248 if (view->output_mask & (1u << output->id))
2249 flags &= view->psf_flags;
2250 }
2251
2252 wl_list_for_each(feedback, &surface->feedback_list, link)
2253 feedback->psf_flags = flags;
2254
2255 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2256 wl_list_init(&surface->feedback_list);
2257}
2258
David Herrmann1edf44c2013-10-22 17:11:26 +02002259static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002260weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002261{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002262 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002263 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002264 struct weston_animation *animation, *next;
2265 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002266 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002267 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002268 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002269
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002270 if (output->destroying)
2271 return 0;
2272
Pekka Paalanenb5026542014-11-12 15:09:24 +02002273 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2274
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002275 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002276 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002277
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002278 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002279 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002280 } else {
2281 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002282 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002283 ev->psf_flags = 0;
2284 }
2285 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002286
Pekka Paalanene67858b2013-04-25 13:57:42 +03002287 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 wl_list_for_each(ev, &ec->view_list, link) {
2289 /* Note: This operation is safe to do multiple times on the
2290 * same surface.
2291 */
2292 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002293 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002294 &ev->surface->frame_callback_list);
2295 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002296
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002297 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002298 }
2299 }
2300
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002301 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002302
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002303 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002304 pixman_region32_intersect(&output_damage,
2305 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002306 pixman_region32_subtract(&output_damage,
2307 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002308
Scott Moreauccbf29d2012-02-22 14:21:41 -07002309 if (output->dirty)
2310 weston_output_update_matrix(output);
2311
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002312 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002313
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002314 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002315
Daniel Stone09a97e22017-03-01 11:34:06 +00002316 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002317 if (r == 0)
2318 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002319
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002320 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002321
Jonas Ådahldb773762012-06-13 00:01:21 +02002322 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002323 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002324 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002325 }
2326
Scott Moreaud64cf212012-06-08 19:40:54 -06002327 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002328 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002329 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002330 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002331
Pekka Paalanenb5026542014-11-12 15:09:24 +02002332 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2333
David Herrmann1edf44c2013-10-22 17:11:26 +02002334 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002335}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002336
Pekka Paalanen82919792014-05-21 13:51:49 +03002337static void
2338weston_output_schedule_repaint_reset(struct weston_output *output)
2339{
Daniel Stone05df8c12017-03-03 16:59:42 +00002340 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002341 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002342}
2343
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002344static int
2345weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2346 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002347{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002348 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002349 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002350 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002351
Daniel Stone6847b852017-03-01 11:34:08 +00002352 /* We're not ready yet; come back to make a decision later. */
2353 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002354 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002355
2356 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2357 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002358 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002359
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002360 /* If we're sleeping, drop the repaint machinery entirely; we will
2361 * explicitly repaint all outputs when we come back. */
2362 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2363 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2364 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002365
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002366 /* We don't actually need to repaint this output; drop it from
2367 * repaint until something causes damage. */
2368 if (!output->repaint_needed)
2369 goto err;
2370
2371 /* If repaint fails, we aren't going to get weston_output_finish_frame
2372 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002373 * something schedules a successful repaint later. As repainting may
2374 * take some time, re-read our clock as a courtesy to the next
2375 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002376 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002377 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002378 if (ret != 0)
2379 goto err;
2380
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002381 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002382
2383err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002384 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002385 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002386}
2387
2388static void
2389output_repaint_timer_arm(struct weston_compositor *compositor)
2390{
2391 struct weston_output *output;
2392 bool any_should_repaint = false;
2393 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002394 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002395
2396 weston_compositor_read_presentation_clock(compositor, &now);
2397
2398 wl_list_for_each(output, &compositor->output_list, link) {
2399 int64_t msec_to_this;
2400
2401 if (output->repaint_status != REPAINT_SCHEDULED)
2402 continue;
2403
2404 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2405 &now);
2406 if (!any_should_repaint || msec_to_this < msec_to_next)
2407 msec_to_next = msec_to_this;
2408
2409 any_should_repaint = true;
2410 }
2411
2412 if (!any_should_repaint)
2413 return;
2414
2415 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2416 * This is a workaround to allow coalescing multiple output repaints
2417 * particularly from weston_output_finish_frame()
2418 * into the same call, which would not happen if we called
2419 * output_repaint_timer_handler() directly.
2420 */
2421 if (msec_to_next < 1)
2422 msec_to_next = 1;
2423
2424 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2425}
2426
2427static int
2428output_repaint_timer_handler(void *data)
2429{
2430 struct weston_compositor *compositor = data;
2431 struct weston_output *output;
2432 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002433 void *repaint_data = NULL;
2434 int ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002435
2436 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002437
2438 if (compositor->backend->repaint_begin)
2439 repaint_data = compositor->backend->repaint_begin(compositor);
2440
2441 wl_list_for_each(output, &compositor->output_list, link) {
2442 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2443 if (ret)
2444 break;
2445 }
2446
2447 if (ret == 0) {
2448 if (compositor->backend->repaint_flush)
2449 compositor->backend->repaint_flush(compositor,
2450 repaint_data);
2451 } else {
2452 if (compositor->backend->repaint_cancel)
2453 compositor->backend->repaint_cancel(compositor,
2454 repaint_data);
2455 }
Daniel Stone6847b852017-03-01 11:34:08 +00002456
2457 output_repaint_timer_arm(compositor);
2458
Pekka Paalanen0513a952014-05-21 16:17:27 +03002459 return 0;
2460}
2461
Kristian Høgsbergef044142011-06-21 15:02:12 -04002462WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002463weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002464 const struct timespec *stamp,
2465 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002466{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002467 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002468 int32_t refresh_nsec;
2469 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002470 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002471
Pekka Paalanenb5026542014-11-12 15:09:24 +02002472 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2473 TLP_VBLANK(stamp), TLP_END);
2474
Daniel Stone05df8c12017-03-03 16:59:42 +00002475 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002476 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2477
Daniel Stone6847b852017-03-01 11:34:08 +00002478 weston_compositor_read_presentation_clock(compositor, &now);
2479
Daniel Stone3615ce12017-03-01 11:34:05 +00002480 /* If we haven't been supplied any timestamp at all, we don't have a
2481 * timebase to work against, so any delay just wastes time. Push a
2482 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002483 if (!stamp) {
2484 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002485 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002486 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002487
Pekka Paalanend7894d02015-07-03 15:08:53 +03002488 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002489 weston_presentation_feedback_present_list(&output->feedback_list,
2490 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002491 output->msc,
2492 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002493
Daniel Stone37ad7e32017-03-01 11:34:02 +00002494 output->frame_time = timespec_to_msec(stamp);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002495
Daniel Stone6847b852017-03-01 11:34:08 +00002496 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2497 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2498 -compositor->repaint_msec);
2499 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002500
2501 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002502 static bool warned;
2503
2504 if (!warned)
2505 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002506 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002507 warned = true;
2508
Daniel Stone6847b852017-03-01 11:34:08 +00002509 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002510 }
2511
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002512 /* Called from restart_repaint_loop and restart happens already after
2513 * the deadline given by repaint_msec? In that case we delay until
2514 * the deadline of the next frame, to give clients a more predictable
2515 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002516 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2517 msec_rel < 0) {
2518 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2519 timespec_add_nsec(&output->next_repaint,
2520 &output->next_repaint,
2521 refresh_nsec);
2522 }
2523 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002524
Daniel Stone3615ce12017-03-01 11:34:05 +00002525out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002526 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002527 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002528}
2529
2530static void
2531idle_repaint(void *data)
2532{
2533 struct weston_output *output = data;
2534
Daniel Stone05df8c12017-03-03 16:59:42 +00002535 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2536 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002537 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002538}
2539
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002540WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002541weston_layer_entry_insert(struct weston_layer_entry *list,
2542 struct weston_layer_entry *entry)
2543{
2544 wl_list_insert(&list->link, &entry->link);
2545 entry->layer = list->layer;
2546}
2547
2548WL_EXPORT void
2549weston_layer_entry_remove(struct weston_layer_entry *entry)
2550{
2551 wl_list_remove(&entry->link);
2552 wl_list_init(&entry->link);
2553 entry->layer = NULL;
2554}
2555
Quentin Glidic82681572016-12-17 13:40:51 +01002556
2557/** Initialize the weston_layer struct.
2558 *
2559 * \param compositor The compositor instance
2560 * \param layer The layer to initialize
2561 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002562WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002563weston_layer_init(struct weston_layer *layer,
2564 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002565{
Quentin Glidic82681572016-12-17 13:40:51 +01002566 layer->compositor = compositor;
2567 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002568 wl_list_init(&layer->view_list.link);
2569 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002570 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002571}
2572
2573/** Sets the position of the layer in the layer list. The layer will be placed
2574 * below any layer with the same position value, if any.
2575 * This function is safe to call if the layer is already on the list, but the
2576 * layer may be moved below other layers at the same position, if any.
2577 *
2578 * \param layer The layer to modify
2579 * \param position The position the layer will be placed at
2580 */
2581WL_EXPORT void
2582weston_layer_set_position(struct weston_layer *layer,
2583 enum weston_layer_position position)
2584{
2585 struct weston_layer *below;
2586
2587 wl_list_remove(&layer->link);
2588
2589 /* layer_list is ordered from top to bottom, the last layer being the
2590 * background with the smallest position value */
2591
2592 layer->position = position;
2593 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2594 if (below->position >= layer->position) {
2595 wl_list_insert(&below->link, &layer->link);
2596 return;
2597 }
2598 }
2599 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2600}
2601
2602/** Hide a layer by taking it off the layer list.
2603 * This function is safe to call if the layer is not on the list.
2604 *
2605 * \param layer The layer to hide
2606 */
2607WL_EXPORT void
2608weston_layer_unset_position(struct weston_layer *layer)
2609{
2610 wl_list_remove(&layer->link);
2611 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002612}
2613
2614WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002615weston_layer_set_mask(struct weston_layer *layer,
2616 int x, int y, int width, int height)
2617{
2618 struct weston_view *view;
2619
2620 layer->mask.x1 = x;
2621 layer->mask.x2 = x + width;
2622 layer->mask.y1 = y;
2623 layer->mask.y2 = y + height;
2624
2625 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2626 weston_view_geometry_dirty(view);
2627 }
2628}
2629
2630WL_EXPORT void
2631weston_layer_set_mask_infinite(struct weston_layer *layer)
2632{
2633 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2634 UINT32_MAX, UINT32_MAX);
2635}
2636
2637WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002638weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002639{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002640 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002641 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002642
Bryce Harrington08976ac2016-08-30 12:05:16 -07002643 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2644 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002645 return;
2646
Pekka Paalanenb5026542014-11-12 15:09:24 +02002647 if (!output->repaint_needed)
2648 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2649
Kristian Høgsbergef044142011-06-21 15:02:12 -04002650 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002651 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002652
2653 /* If we already have a repaint scheduled for our idle handler,
2654 * no need to set it again. If the repaint has been called but
2655 * not finished, then weston_output_finish_frame() will notice
2656 * that a repaint is needed and schedule one. */
2657 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002658 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002659
Daniel Stone05df8c12017-03-03 16:59:42 +00002660 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002661 wl_event_loop_add_idle(loop, idle_repaint, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002662 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002663}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002664
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002665WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002666weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2667{
2668 struct weston_output *output;
2669
2670 wl_list_for_each(output, &compositor->output_list, link)
2671 weston_output_schedule_repaint(output);
2672}
2673
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002674static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002675surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002676{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002677 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002678}
2679
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002680static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002681surface_attach(struct wl_client *client,
2682 struct wl_resource *resource,
2683 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2684{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002685 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002686 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002687
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002688 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002689 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002690 if (buffer == NULL) {
2691 wl_client_post_no_memory(client);
2692 return;
2693 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002694 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002695
Pekka Paalanende685b82012-12-04 15:58:12 +02002696 /* Attach, attach, without commit in between does not send
2697 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002698 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002699
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002700 surface->pending.sx = sx;
2701 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002702 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002703}
2704
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002705static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002706surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002707 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002708 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002709{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002710 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002711
Derek Foreman57e92ed2015-11-17 14:11:35 -06002712 if (width <= 0 || height <= 0)
2713 return;
2714
Derek Foreman152254b2015-11-26 14:17:48 -06002715 pixman_region32_union_rect(&surface->pending.damage_surface,
2716 &surface->pending.damage_surface,
2717 x, y, width, height);
2718}
2719
2720static void
2721surface_damage_buffer(struct wl_client *client,
2722 struct wl_resource *resource,
2723 int32_t x, int32_t y, int32_t width, int32_t height)
2724{
2725 struct weston_surface *surface = wl_resource_get_user_data(resource);
2726
2727 if (width <= 0 || height <= 0)
2728 return;
2729
2730 pixman_region32_union_rect(&surface->pending.damage_buffer,
2731 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002732 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002733}
2734
Kristian Høgsberg33418202011-08-16 23:01:28 -04002735static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002736destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002737{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002738 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002739
2740 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002741 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002742}
2743
2744static void
2745surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002746 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002747{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002748 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002749 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002750
2751 cb = malloc(sizeof *cb);
2752 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002753 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002754 return;
2755 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002756
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002757 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2758 callback);
2759 if (cb->resource == NULL) {
2760 free(cb);
2761 wl_resource_post_no_memory(resource);
2762 return;
2763 }
2764
Jason Ekstranda85118c2013-06-27 20:17:02 -05002765 wl_resource_set_implementation(cb->resource, NULL, cb,
2766 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002767
Pekka Paalanenbc106382012-10-10 12:49:31 +03002768 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002769}
2770
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002771static void
2772surface_set_opaque_region(struct wl_client *client,
2773 struct wl_resource *resource,
2774 struct wl_resource *region_resource)
2775{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002776 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002777 struct weston_region *region;
2778
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002779 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002780 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002781 pixman_region32_copy(&surface->pending.opaque,
2782 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002783 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002784 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002785 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002786}
2787
2788static void
2789surface_set_input_region(struct wl_client *client,
2790 struct wl_resource *resource,
2791 struct wl_resource *region_resource)
2792{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002793 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002794 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002795
2796 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002797 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002798 pixman_region32_copy(&surface->pending.input,
2799 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002800 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002801 pixman_region32_fini(&surface->pending.input);
2802 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002803 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002804}
2805
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002806/* Cause damage to this sub-surface and all its children.
2807 *
2808 * This is useful when there are state changes that need an implicit
2809 * damage, e.g. a z-order change.
2810 */
2811static void
2812weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2813{
2814 struct weston_subsurface *child;
2815
2816 weston_surface_damage(sub->surface);
2817 sub->reordered = false;
2818
2819 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2820 if (child != sub)
2821 weston_surface_damage_subsurfaces(child);
2822}
2823
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002824static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002825weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002826{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002827 struct weston_subsurface *sub;
2828
2829 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2830 parent_link_pending) {
2831 wl_list_remove(&sub->parent_link);
2832 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002833
2834 if (sub->reordered)
2835 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002836 }
2837}
2838
2839static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002840weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002841 struct weston_matrix *matrix)
2842{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002843 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002844 double src_width, src_height, dest_width, dest_height;
2845
2846 weston_matrix_init(matrix);
2847
2848 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2849 src_width = surface->width_from_buffer;
2850 src_height = surface->height_from_buffer;
2851 } else {
2852 src_width = wl_fixed_to_double(vp->buffer.src_width);
2853 src_height = wl_fixed_to_double(vp->buffer.src_height);
2854 }
2855
2856 if (vp->surface.width == -1) {
2857 dest_width = src_width;
2858 dest_height = src_height;
2859 } else {
2860 dest_width = vp->surface.width;
2861 dest_height = vp->surface.height;
2862 }
2863
2864 if (src_width != dest_width || src_height != dest_height)
2865 weston_matrix_scale(matrix,
2866 src_width / dest_width,
2867 src_height / dest_height, 1);
2868
2869 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2870 weston_matrix_translate(matrix,
2871 wl_fixed_to_double(vp->buffer.src_x),
2872 wl_fixed_to_double(vp->buffer.src_y),
2873 0);
2874
2875 switch (vp->buffer.transform) {
2876 case WL_OUTPUT_TRANSFORM_FLIPPED:
2877 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2878 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2879 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2880 weston_matrix_scale(matrix, -1, 1, 1);
2881 weston_matrix_translate(matrix,
2882 surface->width_from_buffer, 0, 0);
2883 break;
2884 }
2885
2886 switch (vp->buffer.transform) {
2887 default:
2888 case WL_OUTPUT_TRANSFORM_NORMAL:
2889 case WL_OUTPUT_TRANSFORM_FLIPPED:
2890 break;
2891 case WL_OUTPUT_TRANSFORM_90:
2892 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2893 weston_matrix_rotate_xy(matrix, 0, 1);
2894 weston_matrix_translate(matrix,
2895 surface->height_from_buffer, 0, 0);
2896 break;
2897 case WL_OUTPUT_TRANSFORM_180:
2898 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2899 weston_matrix_rotate_xy(matrix, -1, 0);
2900 weston_matrix_translate(matrix,
2901 surface->width_from_buffer,
2902 surface->height_from_buffer, 0);
2903 break;
2904 case WL_OUTPUT_TRANSFORM_270:
2905 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2906 weston_matrix_rotate_xy(matrix, 0, -1);
2907 weston_matrix_translate(matrix,
2908 0, surface->width_from_buffer, 0);
2909 break;
2910 }
2911
2912 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2913}
2914
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002915/**
2916 * Compute a + b > c while being safe to overflows.
2917 */
2918static bool
2919fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2920{
2921 return (int64_t)a + (int64_t)b > (int64_t)c;
2922}
2923
2924static bool
2925weston_surface_is_pending_viewport_source_valid(
2926 const struct weston_surface *surface)
2927{
2928 const struct weston_surface_state *pend = &surface->pending;
2929 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2930 int width_from_buffer = 0;
2931 int height_from_buffer = 0;
2932 wl_fixed_t w;
2933 wl_fixed_t h;
2934
2935 /* If viewport source rect is not set, it is always ok. */
2936 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2937 return true;
2938
2939 if (pend->newly_attached) {
2940 if (pend->buffer) {
2941 convert_size_by_transform_scale(&width_from_buffer,
2942 &height_from_buffer,
2943 pend->buffer->width,
2944 pend->buffer->height,
2945 vp->buffer.transform,
2946 vp->buffer.scale);
2947 } else {
2948 /* No buffer: viewport is irrelevant. */
2949 return true;
2950 }
2951 } else {
2952 width_from_buffer = surface->width_from_buffer;
2953 height_from_buffer = surface->height_from_buffer;
2954 }
2955
2956 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2957 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2958
2959 /* No buffer: viewport is irrelevant. */
2960 if (width_from_buffer == 0 || height_from_buffer == 0)
2961 return true;
2962
2963 /* overflow checks for wl_fixed_from_int() */
2964 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2965 return false;
2966 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2967 return false;
2968
2969 w = wl_fixed_from_int(width_from_buffer);
2970 h = wl_fixed_from_int(height_from_buffer);
2971
2972 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2973 return false;
2974 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2975 return false;
2976
2977 return true;
2978}
2979
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002980static bool
2981fixed_is_integer(wl_fixed_t v)
2982{
2983 return (v & 0xff) == 0;
2984}
2985
2986static bool
2987weston_surface_is_pending_viewport_dst_size_int(
2988 const struct weston_surface *surface)
2989{
2990 const struct weston_buffer_viewport *vp =
2991 &surface->pending.buffer_viewport;
2992
2993 if (vp->surface.width != -1) {
2994 assert(vp->surface.width > 0 && vp->surface.height > 0);
2995 return true;
2996 }
2997
2998 return fixed_is_integer(vp->buffer.src_width) &&
2999 fixed_is_integer(vp->buffer.src_height);
3000}
3001
Derek Foreman152254b2015-11-26 14:17:48 -06003002/* Translate pending damage in buffer co-ordinates to surface
3003 * co-ordinates and union it with a pixman_region32_t.
3004 * This should only be called after the buffer is attached.
3005 */
3006static void
3007apply_damage_buffer(pixman_region32_t *dest,
3008 struct weston_surface *surface,
3009 struct weston_surface_state *state)
3010{
3011 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3012
3013 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3014 * translated into surface co-ordinates and unioned with
3015 * any other surface damage.
3016 * None of this makes sense if there is no buffer though.
3017 */
3018 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3019 pixman_region32_t buffer_damage;
3020
3021 pixman_region32_intersect_rect(&state->damage_buffer,
3022 &state->damage_buffer,
3023 0, 0, buffer->width,
3024 buffer->height);
3025 pixman_region32_init(&buffer_damage);
3026 weston_matrix_transform_region(&buffer_damage,
3027 &surface->buffer_to_surface_matrix,
3028 &state->damage_buffer);
3029 pixman_region32_union(dest, dest, &buffer_damage);
3030 pixman_region32_fini(&buffer_damage);
3031 }
3032 /* We should clear this on commit even if there was no buffer */
3033 pixman_region32_clear(&state->damage_buffer);
3034}
3035
Jason Ekstrand1e059042014-10-16 10:55:19 -05003036static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003037weston_surface_commit_state(struct weston_surface *surface,
3038 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003039{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003040 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003041 pixman_region32_t opaque;
3042
Alexander Larsson4ea95522013-05-22 14:41:37 +02003043 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003044 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003045 /* wp_viewport.set_source */
3046 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003047 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003048
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003049 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003050 if (state->newly_attached)
3051 weston_surface_attach(surface, state->buffer);
3052 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003053
Jason Ekstrand1e059042014-10-16 10:55:19 -05003054 weston_surface_build_buffer_matrix(surface,
3055 &surface->surface_to_buffer_matrix);
3056 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3057 &surface->surface_to_buffer_matrix);
3058
Jason Ekstrand7b982072014-05-20 14:33:03 -05003059 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003060 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003061 if (surface->committed)
3062 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003063 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003064
Jason Ekstrand7b982072014-05-20 14:33:03 -05003065 state->sx = 0;
3066 state->sy = 0;
3067 state->newly_attached = 0;
3068 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003069
Derek Foreman152254b2015-11-26 14:17:48 -06003070 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003071 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003072 (pixman_region32_not_empty(&state->damage_surface) ||
3073 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003074 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003075
Pekka Paalanen8e159182012-10-10 12:49:25 +03003076 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003077 &state->damage_surface);
3078
3079 apply_damage_buffer(&surface->damage, surface, state);
3080
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003081 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003082 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003083 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003084
3085 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003086 pixman_region32_init(&opaque);
3087 pixman_region32_intersect_rect(&opaque, &state->opaque,
3088 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003089
3090 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3091 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003092 wl_list_for_each(view, &surface->views, surface_link)
3093 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003094 }
3095
3096 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003097
3098 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003099 pixman_region32_intersect_rect(&surface->input, &state->input,
3100 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003101
Pekka Paalanenbc106382012-10-10 12:49:31 +03003102 /* wl_surface.frame */
3103 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003104 &state->frame_callback_list);
3105 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003106
3107 /* XXX:
3108 * What should happen with a feedback request, if there
3109 * is no wl_buffer attached for this commit?
3110 */
3111
3112 /* presentation.feedback */
3113 wl_list_insert_list(&surface->feedback_list,
3114 &state->feedback_list);
3115 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003116
3117 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003118}
3119
3120static void
3121weston_surface_commit(struct weston_surface *surface)
3122{
3123 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003124
Pekka Paalanene67858b2013-04-25 13:57:42 +03003125 weston_surface_commit_subsurface_order(surface);
3126
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003127 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003128}
3129
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003130static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003131weston_subsurface_commit(struct weston_subsurface *sub);
3132
3133static void
3134weston_subsurface_parent_commit(struct weston_subsurface *sub,
3135 int parent_is_synchronized);
3136
3137static void
3138surface_commit(struct wl_client *client, struct wl_resource *resource)
3139{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003140 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003141 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3142
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003143 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3144 assert(surface->viewport_resource);
3145
3146 wl_resource_post_error(surface->viewport_resource,
3147 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3148 "wl_surface@%d has viewport source outside buffer",
3149 wl_resource_get_id(resource));
3150 return;
3151 }
3152
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003153 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3154 assert(surface->viewport_resource);
3155
3156 wl_resource_post_error(surface->viewport_resource,
3157 WP_VIEWPORT_ERROR_BAD_SIZE,
3158 "wl_surface@%d viewport dst size not integer",
3159 wl_resource_get_id(resource));
3160 return;
3161 }
3162
Pekka Paalanene67858b2013-04-25 13:57:42 +03003163 if (sub) {
3164 weston_subsurface_commit(sub);
3165 return;
3166 }
3167
3168 weston_surface_commit(surface);
3169
3170 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3171 if (sub->surface != surface)
3172 weston_subsurface_parent_commit(sub, 0);
3173 }
3174}
3175
3176static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003177surface_set_buffer_transform(struct wl_client *client,
3178 struct wl_resource *resource, int transform)
3179{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003180 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003181
Jonny Lamba55f1392014-05-30 12:07:15 +02003182 /* if wl_output.transform grows more members this will need to be updated. */
3183 if (transform < 0 ||
3184 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3185 wl_resource_post_error(resource,
3186 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3187 "buffer transform must be a valid transform "
3188 "('%d' specified)", transform);
3189 return;
3190 }
3191
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003192 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003193 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003194}
3195
Alexander Larsson4ea95522013-05-22 14:41:37 +02003196static void
3197surface_set_buffer_scale(struct wl_client *client,
3198 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003199 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003200{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003201 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003202
Jonny Lamba55f1392014-05-30 12:07:15 +02003203 if (scale < 1) {
3204 wl_resource_post_error(resource,
3205 WL_SURFACE_ERROR_INVALID_SCALE,
3206 "buffer scale must be at least one "
3207 "('%d' specified)", scale);
3208 return;
3209 }
3210
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003211 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003212 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003213}
3214
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003215static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003216 surface_destroy,
3217 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003218 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003219 surface_frame,
3220 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003221 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003222 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003223 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003224 surface_set_buffer_scale,
3225 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003226};
3227
3228static void
3229compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003230 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003231{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003232 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003233 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003234
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003235 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003236 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003237 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003238 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003239 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003240
Jason Ekstranda85118c2013-06-27 20:17:02 -05003241 surface->resource =
3242 wl_resource_create(client, &wl_surface_interface,
3243 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003244 if (surface->resource == NULL) {
3245 weston_surface_destroy(surface);
3246 wl_resource_post_no_memory(resource);
3247 return;
3248 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003249 wl_resource_set_implementation(surface->resource, &surface_interface,
3250 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003251
3252 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003253}
3254
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003255static void
3256destroy_region(struct wl_resource *resource)
3257{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003258 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003259
3260 pixman_region32_fini(&region->region);
3261 free(region);
3262}
3263
3264static void
3265region_destroy(struct wl_client *client, struct wl_resource *resource)
3266{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003267 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003268}
3269
3270static void
3271region_add(struct wl_client *client, struct wl_resource *resource,
3272 int32_t x, int32_t y, int32_t width, int32_t height)
3273{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003274 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003275
3276 pixman_region32_union_rect(&region->region, &region->region,
3277 x, y, width, height);
3278}
3279
3280static void
3281region_subtract(struct wl_client *client, struct wl_resource *resource,
3282 int32_t x, int32_t y, int32_t width, int32_t height)
3283{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003284 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003285 pixman_region32_t rect;
3286
3287 pixman_region32_init_rect(&rect, x, y, width, height);
3288 pixman_region32_subtract(&region->region, &region->region, &rect);
3289 pixman_region32_fini(&rect);
3290}
3291
3292static const struct wl_region_interface region_interface = {
3293 region_destroy,
3294 region_add,
3295 region_subtract
3296};
3297
3298static void
3299compositor_create_region(struct wl_client *client,
3300 struct wl_resource *resource, uint32_t id)
3301{
3302 struct weston_region *region;
3303
3304 region = malloc(sizeof *region);
3305 if (region == NULL) {
3306 wl_resource_post_no_memory(resource);
3307 return;
3308 }
3309
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003310 pixman_region32_init(&region->region);
3311
Jason Ekstranda85118c2013-06-27 20:17:02 -05003312 region->resource =
3313 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003314 if (region->resource == NULL) {
3315 free(region);
3316 wl_resource_post_no_memory(resource);
3317 return;
3318 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003319 wl_resource_set_implementation(region->resource, &region_interface,
3320 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003321}
3322
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003323static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003324 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003325 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003326};
3327
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003328static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003329weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3330{
3331 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003332
Jason Ekstrand7b982072014-05-20 14:33:03 -05003333 weston_surface_commit_state(surface, &sub->cached);
3334 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003335
3336 weston_surface_commit_subsurface_order(surface);
3337
3338 weston_surface_schedule_repaint(surface);
3339
Jason Ekstrand7b982072014-05-20 14:33:03 -05003340 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003341}
3342
3343static void
3344weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3345{
3346 struct weston_surface *surface = sub->surface;
3347
3348 /*
3349 * If this commit would cause the surface to move by the
3350 * attach(dx, dy) parameters, the old damage region must be
3351 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003352 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003353 */
Derek Foreman152254b2015-11-26 14:17:48 -06003354 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003355 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003356 pixman_region32_union(&sub->cached.damage_surface,
3357 &sub->cached.damage_surface,
3358 &surface->pending.damage_surface);
3359 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003360
3361 if (surface->pending.newly_attached) {
3362 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003363 weston_surface_state_set_buffer(&sub->cached,
3364 surface->pending.buffer);
3365 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003366 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003367 weston_presentation_feedback_discard_list(
3368 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003369 }
3370 sub->cached.sx += surface->pending.sx;
3371 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003372
Derek Foreman152254b2015-11-26 14:17:48 -06003373 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3374
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003375 sub->cached.buffer_viewport.changed |=
3376 surface->pending.buffer_viewport.changed;
3377 sub->cached.buffer_viewport.buffer =
3378 surface->pending.buffer_viewport.buffer;
3379 sub->cached.buffer_viewport.surface =
3380 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003381
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003382 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003383
3384 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3385
3386 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3387
3388 wl_list_insert_list(&sub->cached.frame_callback_list,
3389 &surface->pending.frame_callback_list);
3390 wl_list_init(&surface->pending.frame_callback_list);
3391
Pekka Paalanen133e4392014-09-23 22:08:46 -04003392 wl_list_insert_list(&sub->cached.feedback_list,
3393 &surface->pending.feedback_list);
3394 wl_list_init(&surface->pending.feedback_list);
3395
Jason Ekstrand7b982072014-05-20 14:33:03 -05003396 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003397}
3398
Derek Foreman280e7dd2014-10-03 13:13:42 -05003399static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003400weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3401{
3402 while (sub) {
3403 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003404 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003405
3406 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003407 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003408
3409 sub = weston_surface_to_subsurface(sub->parent);
3410 }
3411
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003412 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003413}
3414
3415static void
3416weston_subsurface_commit(struct weston_subsurface *sub)
3417{
3418 struct weston_surface *surface = sub->surface;
3419 struct weston_subsurface *tmp;
3420
3421 /* Recursive check for effectively synchronized. */
3422 if (weston_subsurface_is_synchronized(sub)) {
3423 weston_subsurface_commit_to_cache(sub);
3424 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003425 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003426 /* flush accumulated state from cache */
3427 weston_subsurface_commit_to_cache(sub);
3428 weston_subsurface_commit_from_cache(sub);
3429 } else {
3430 weston_surface_commit(surface);
3431 }
3432
3433 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3434 if (tmp->surface != surface)
3435 weston_subsurface_parent_commit(tmp, 0);
3436 }
3437 }
3438}
3439
3440static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003441weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003442{
3443 struct weston_surface *surface = sub->surface;
3444 struct weston_subsurface *tmp;
3445
Pekka Paalanene67858b2013-04-25 13:57:42 +03003446 /* From now on, commit_from_cache the whole sub-tree, regardless of
3447 * the synchronized mode of each child. This sub-surface or some
3448 * of its ancestors were synchronized, so we are synchronized
3449 * all the way down.
3450 */
3451
Jason Ekstrand7b982072014-05-20 14:33:03 -05003452 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003453 weston_subsurface_commit_from_cache(sub);
3454
3455 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3456 if (tmp->surface != surface)
3457 weston_subsurface_parent_commit(tmp, 1);
3458 }
3459}
3460
3461static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003462weston_subsurface_parent_commit(struct weston_subsurface *sub,
3463 int parent_is_synchronized)
3464{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003465 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003466 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003467 wl_list_for_each(view, &sub->surface->views, surface_link)
3468 weston_view_set_position(view,
3469 sub->position.x,
3470 sub->position.y);
3471
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003472 sub->position.set = 0;
3473 }
3474
3475 if (parent_is_synchronized || sub->synchronized)
3476 weston_subsurface_synchronized_commit(sub);
3477}
3478
Pekka Paalanen8274d902014-08-06 19:36:51 +03003479static int
3480subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3481{
3482 return snprintf(buf, len, "sub-surface");
3483}
3484
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003485static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003486subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003487{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003488 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003489
Jason Ekstranda7af7042013-10-12 22:38:11 -05003490 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003491 weston_view_set_position(view,
3492 view->geometry.x + dx,
3493 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003494
3495 /* No need to check parent mappedness, because if parent is not
3496 * mapped, parent is not in a visible layer, so this sub-surface
3497 * will not be drawn either.
3498 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003499
Pekka Paalanene67858b2013-04-25 13:57:42 +03003500 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003501 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003502
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003503 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003504 * because that would call it also for the parent surface,
3505 * which might not be mapped yet. That would lead to
3506 * inconsistent state, where the window could never be
3507 * mapped.
3508 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003509 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003510 * weston_surface_is_mapped() return true, so that when the
3511 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003512 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003513 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003514 }
3515}
3516
3517static struct weston_subsurface *
3518weston_surface_to_subsurface(struct weston_surface *surface)
3519{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003520 if (surface->committed == subsurface_committed)
3521 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003522
3523 return NULL;
3524}
3525
Pekka Paalanen01388e22013-04-25 13:57:44 +03003526WL_EXPORT struct weston_surface *
3527weston_surface_get_main_surface(struct weston_surface *surface)
3528{
3529 struct weston_subsurface *sub;
3530
3531 while (surface && (sub = weston_surface_to_subsurface(surface)))
3532 surface = sub->parent;
3533
3534 return surface;
3535}
3536
Pekka Paalanen50b67472014-10-01 15:02:41 +03003537WL_EXPORT int
3538weston_surface_set_role(struct weston_surface *surface,
3539 const char *role_name,
3540 struct wl_resource *error_resource,
3541 uint32_t error_code)
3542{
3543 assert(role_name);
3544
3545 if (surface->role_name == NULL ||
3546 surface->role_name == role_name ||
3547 strcmp(surface->role_name, role_name) == 0) {
3548 surface->role_name = role_name;
3549
3550 return 0;
3551 }
3552
3553 wl_resource_post_error(error_resource, error_code,
3554 "Cannot assign role %s to wl_surface@%d,"
3555 " already has role %s\n",
3556 role_name,
3557 wl_resource_get_id(surface->resource),
3558 surface->role_name);
3559 return -1;
3560}
3561
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003562WL_EXPORT const char *
3563weston_surface_get_role(struct weston_surface *surface)
3564{
3565 return surface->role_name;
3566}
3567
Pekka Paalanen8274d902014-08-06 19:36:51 +03003568WL_EXPORT void
3569weston_surface_set_label_func(struct weston_surface *surface,
3570 int (*desc)(struct weston_surface *,
3571 char *, size_t))
3572{
3573 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003574 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003575}
3576
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003577/** Get the size of surface contents
3578 *
3579 * \param surface The surface to query.
3580 * \param width Returns the width of raw contents.
3581 * \param height Returns the height of raw contents.
3582 *
3583 * Retrieves the raw surface content size in pixels for the given surface.
3584 * This is the whole content size in buffer pixels. If the surface
3585 * has no content or the renderer does not implement this feature,
3586 * zeroes are returned.
3587 *
3588 * This function is used to determine the buffer size needed for
3589 * a weston_surface_copy_content() call.
3590 */
3591WL_EXPORT void
3592weston_surface_get_content_size(struct weston_surface *surface,
3593 int *width, int *height)
3594{
3595 struct weston_renderer *rer = surface->compositor->renderer;
3596
3597 if (!rer->surface_get_content_size) {
3598 *width = 0;
3599 *height = 0;
3600 return;
3601 }
3602
3603 rer->surface_get_content_size(surface, width, height);
3604}
3605
Quentin Glidic248dd102016-08-12 10:41:34 +02003606/** Get the bounding box of a surface and its subsurfaces
3607 *
3608 * \param surface The surface to query.
3609 * \return The bounding box relative to the surface origin.
3610 *
3611 */
3612WL_EXPORT struct weston_geometry
3613weston_surface_get_bounding_box(struct weston_surface *surface)
3614{
3615 pixman_region32_t region;
3616 pixman_box32_t *box;
3617 struct weston_subsurface *subsurface;
3618
3619 pixman_region32_init_rect(&region,
3620 0, 0,
3621 surface->width, surface->height);
3622
3623 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3624 pixman_region32_union_rect(&region, &region,
3625 subsurface->position.x,
3626 subsurface->position.y,
3627 subsurface->surface->width,
3628 subsurface->surface->height);
3629
3630 box = pixman_region32_extents(&region);
3631 struct weston_geometry geometry = {
3632 .x = box->x1,
3633 .y = box->y1,
3634 .width = box->x2 - box->x1,
3635 .height = box->y2 - box->y1,
3636 };
3637
3638 pixman_region32_fini(&region);
3639
3640 return geometry;
3641}
3642
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003643/** Copy surface contents to system memory.
3644 *
3645 * \param surface The surface to copy from.
3646 * \param target Pointer to the target memory buffer.
3647 * \param size Size of the target buffer in bytes.
3648 * \param src_x X location on contents to copy from.
3649 * \param src_y Y location on contents to copy from.
3650 * \param width Width in pixels of the area to copy.
3651 * \param height Height in pixels of the area to copy.
3652 * \return 0 for success, -1 for failure.
3653 *
3654 * Surface contents are maintained by the renderer. They can be in a
3655 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3656 * else.
3657 *
3658 * Surface contents are copied into memory pointed to by target,
3659 * which has size bytes of space available. The target memory
3660 * may be larger than needed, but being smaller returns an error.
3661 * The extra bytes in target may or may not be written; their content is
3662 * unspecified. Size must be large enough to hold the image.
3663 *
3664 * The image in the target memory will be arranged in rows from
3665 * top to bottom, and pixels on a row from left to right. The pixel
3666 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3667 * width * 4.
3668 *
3669 * Parameters src_x and src_y define the upper-left corner in buffer
3670 * coordinates (pixels) to copy from. Parameters width and height
3671 * define the size of the area to copy in pixels.
3672 *
3673 * The rectangle defined by src_x, src_y, width, height must fit in
3674 * the surface contents. Otherwise an error is returned.
3675 *
3676 * Use surface_get_data_size to determine the content size; the
3677 * needed target buffer size and rectangle limits.
3678 *
3679 * CURRENT IMPLEMENTATION RESTRICTIONS:
3680 * - the machine must be little-endian due to Pixman formats.
3681 *
3682 * NOTE: Pixman formats are premultiplied.
3683 */
3684WL_EXPORT int
3685weston_surface_copy_content(struct weston_surface *surface,
3686 void *target, size_t size,
3687 int src_x, int src_y,
3688 int width, int height)
3689{
3690 struct weston_renderer *rer = surface->compositor->renderer;
3691 int cw, ch;
3692 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3693
3694 if (!rer->surface_copy_content)
3695 return -1;
3696
3697 weston_surface_get_content_size(surface, &cw, &ch);
3698
3699 if (src_x < 0 || src_y < 0)
3700 return -1;
3701
3702 if (width <= 0 || height <= 0)
3703 return -1;
3704
3705 if (src_x + width > cw || src_y + height > ch)
3706 return -1;
3707
3708 if (width * bytespp * height > size)
3709 return -1;
3710
3711 return rer->surface_copy_content(surface, target, size,
3712 src_x, src_y, width, height);
3713}
3714
Pekka Paalanene67858b2013-04-25 13:57:42 +03003715static void
3716subsurface_set_position(struct wl_client *client,
3717 struct wl_resource *resource, int32_t x, int32_t y)
3718{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003719 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003720
3721 if (!sub)
3722 return;
3723
3724 sub->position.x = x;
3725 sub->position.y = y;
3726 sub->position.set = 1;
3727}
3728
3729static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003730subsurface_find_sibling(struct weston_subsurface *sub,
3731 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003732{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003733 struct weston_surface *parent = sub->parent;
3734 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003735
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003736 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3737 if (sibling->surface == surface && sibling != sub)
3738 return sibling;
3739 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003740
3741 return NULL;
3742}
3743
3744static struct weston_subsurface *
3745subsurface_sibling_check(struct weston_subsurface *sub,
3746 struct weston_surface *surface,
3747 const char *request)
3748{
3749 struct weston_subsurface *sibling;
3750
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003751 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003752 if (!sibling) {
3753 wl_resource_post_error(sub->resource,
3754 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3755 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003756 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003757 return NULL;
3758 }
3759
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003760 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003761
3762 return sibling;
3763}
3764
3765static void
3766subsurface_place_above(struct wl_client *client,
3767 struct wl_resource *resource,
3768 struct wl_resource *sibling_resource)
3769{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003770 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003771 struct weston_surface *surface =
3772 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003773 struct weston_subsurface *sibling;
3774
3775 if (!sub)
3776 return;
3777
3778 sibling = subsurface_sibling_check(sub, surface, "place_above");
3779 if (!sibling)
3780 return;
3781
3782 wl_list_remove(&sub->parent_link_pending);
3783 wl_list_insert(sibling->parent_link_pending.prev,
3784 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003785
3786 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003787}
3788
3789static void
3790subsurface_place_below(struct wl_client *client,
3791 struct wl_resource *resource,
3792 struct wl_resource *sibling_resource)
3793{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003794 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003795 struct weston_surface *surface =
3796 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003797 struct weston_subsurface *sibling;
3798
3799 if (!sub)
3800 return;
3801
3802 sibling = subsurface_sibling_check(sub, surface, "place_below");
3803 if (!sibling)
3804 return;
3805
3806 wl_list_remove(&sub->parent_link_pending);
3807 wl_list_insert(&sibling->parent_link_pending,
3808 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003809
3810 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003811}
3812
3813static void
3814subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3815{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003816 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003817
3818 if (sub)
3819 sub->synchronized = 1;
3820}
3821
3822static void
3823subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3824{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003825 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003826
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003827 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003828 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003829
3830 /* If sub became effectively desynchronized, flush. */
3831 if (!weston_subsurface_is_synchronized(sub))
3832 weston_subsurface_synchronized_commit(sub);
3833 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003834}
3835
3836static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003837weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3838{
3839 wl_list_remove(&sub->parent_link);
3840 wl_list_remove(&sub->parent_link_pending);
3841 wl_list_remove(&sub->parent_destroy_listener.link);
3842 sub->parent = NULL;
3843}
3844
3845static void
3846weston_subsurface_destroy(struct weston_subsurface *sub);
3847
3848static void
3849subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3850{
3851 struct weston_subsurface *sub =
3852 container_of(listener, struct weston_subsurface,
3853 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003854 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003855
3856 /* The protocol object (wl_resource) is left inert. */
3857 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003858 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003859
3860 weston_subsurface_destroy(sub);
3861}
3862
3863static void
3864subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3865{
3866 struct weston_subsurface *sub =
3867 container_of(listener, struct weston_subsurface,
3868 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003869 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003870 assert(sub->surface != sub->parent);
3871
3872 if (weston_surface_is_mapped(sub->surface))
3873 weston_surface_unmap(sub->surface);
3874
3875 weston_subsurface_unlink_parent(sub);
3876}
3877
3878static void
3879subsurface_resource_destroy(struct wl_resource *resource)
3880{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003881 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003882
3883 if (sub)
3884 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003885}
3886
3887static void
3888subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3889{
3890 wl_resource_destroy(resource);
3891}
3892
3893static void
3894weston_subsurface_link_parent(struct weston_subsurface *sub,
3895 struct weston_surface *parent)
3896{
3897 sub->parent = parent;
3898 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003899 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003900 &sub->parent_destroy_listener);
3901
3902 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3903 wl_list_insert(&parent->subsurface_list_pending,
3904 &sub->parent_link_pending);
3905}
3906
3907static void
3908weston_subsurface_link_surface(struct weston_subsurface *sub,
3909 struct weston_surface *surface)
3910{
3911 sub->surface = surface;
3912 sub->surface_destroy_listener.notify =
3913 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003914 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003915 &sub->surface_destroy_listener);
3916}
3917
3918static void
3919weston_subsurface_destroy(struct weston_subsurface *sub)
3920{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003921 struct weston_view *view, *next;
3922
Pekka Paalanene67858b2013-04-25 13:57:42 +03003923 assert(sub->surface);
3924
3925 if (sub->resource) {
3926 assert(weston_surface_to_subsurface(sub->surface) == sub);
3927 assert(sub->parent_destroy_listener.notify ==
3928 subsurface_handle_parent_destroy);
3929
George Kiagiadakised04d382014-06-13 18:10:26 +02003930 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3931 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003932 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003933 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003934
Pekka Paalanene67858b2013-04-25 13:57:42 +03003935 if (sub->parent)
3936 weston_subsurface_unlink_parent(sub);
3937
Jason Ekstrand7b982072014-05-20 14:33:03 -05003938 weston_surface_state_fini(&sub->cached);
3939 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003940
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003941 sub->surface->committed = NULL;
3942 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003943 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003944 } else {
3945 /* the dummy weston_subsurface for the parent itself */
3946 assert(sub->parent_destroy_listener.notify == NULL);
3947 wl_list_remove(&sub->parent_link);
3948 wl_list_remove(&sub->parent_link_pending);
3949 }
3950
3951 wl_list_remove(&sub->surface_destroy_listener.link);
3952 free(sub);
3953}
3954
3955static const struct wl_subsurface_interface subsurface_implementation = {
3956 subsurface_destroy,
3957 subsurface_set_position,
3958 subsurface_place_above,
3959 subsurface_place_below,
3960 subsurface_set_sync,
3961 subsurface_set_desync
3962};
3963
3964static struct weston_subsurface *
3965weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3966 struct weston_surface *parent)
3967{
3968 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003969 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003970
Bryce Harringtonde16d892014-11-20 22:21:57 -08003971 sub = zalloc(sizeof *sub);
3972 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003973 return NULL;
3974
Jason Ekstranda7af7042013-10-12 22:38:11 -05003975 wl_list_init(&sub->unused_views);
3976
Jason Ekstranda85118c2013-06-27 20:17:02 -05003977 sub->resource =
3978 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003979 if (!sub->resource) {
3980 free(sub);
3981 return NULL;
3982 }
3983
Jason Ekstranda85118c2013-06-27 20:17:02 -05003984 wl_resource_set_implementation(sub->resource,
3985 &subsurface_implementation,
3986 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003987 weston_subsurface_link_surface(sub, surface);
3988 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003989 weston_surface_state_init(&sub->cached);
3990 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003991 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003992
3993 return sub;
3994}
3995
3996/* Create a dummy subsurface for having the parent itself in its
3997 * sub-surface lists. Makes stacking order manipulation easy.
3998 */
3999static struct weston_subsurface *
4000weston_subsurface_create_for_parent(struct weston_surface *parent)
4001{
4002 struct weston_subsurface *sub;
4003
Bryce Harringtonde16d892014-11-20 22:21:57 -08004004 sub = zalloc(sizeof *sub);
4005 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004006 return NULL;
4007
4008 weston_subsurface_link_surface(sub, parent);
4009 sub->parent = parent;
4010 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4011 wl_list_insert(&parent->subsurface_list_pending,
4012 &sub->parent_link_pending);
4013
4014 return sub;
4015}
4016
4017static void
4018subcompositor_get_subsurface(struct wl_client *client,
4019 struct wl_resource *resource,
4020 uint32_t id,
4021 struct wl_resource *surface_resource,
4022 struct wl_resource *parent_resource)
4023{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004024 struct weston_surface *surface =
4025 wl_resource_get_user_data(surface_resource);
4026 struct weston_surface *parent =
4027 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004028 struct weston_subsurface *sub;
4029 static const char where[] = "get_subsurface: wl_subsurface@";
4030
4031 if (surface == parent) {
4032 wl_resource_post_error(resource,
4033 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4034 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004035 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004036 return;
4037 }
4038
4039 if (weston_surface_to_subsurface(surface)) {
4040 wl_resource_post_error(resource,
4041 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4042 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004043 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004044 return;
4045 }
4046
Pekka Paalanen50b67472014-10-01 15:02:41 +03004047 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4048 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004049 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004050
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004051 if (weston_surface_get_main_surface(parent) == surface) {
4052 wl_resource_post_error(resource,
4053 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4054 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004055 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004056 return;
4057 }
4058
Pekka Paalanene67858b2013-04-25 13:57:42 +03004059 /* make sure the parent is in its own list */
4060 if (wl_list_empty(&parent->subsurface_list)) {
4061 if (!weston_subsurface_create_for_parent(parent)) {
4062 wl_resource_post_no_memory(resource);
4063 return;
4064 }
4065 }
4066
4067 sub = weston_subsurface_create(id, surface, parent);
4068 if (!sub) {
4069 wl_resource_post_no_memory(resource);
4070 return;
4071 }
4072
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004073 surface->committed = subsurface_committed;
4074 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004075 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004076}
4077
4078static void
4079subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4080{
4081 wl_resource_destroy(resource);
4082}
4083
4084static const struct wl_subcompositor_interface subcompositor_interface = {
4085 subcompositor_destroy,
4086 subcompositor_get_subsurface
4087};
4088
4089static void
4090bind_subcompositor(struct wl_client *client,
4091 void *data, uint32_t version, uint32_t id)
4092{
4093 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004094 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004095
Jason Ekstranda85118c2013-06-27 20:17:02 -05004096 resource =
4097 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004098 if (resource == NULL) {
4099 wl_client_post_no_memory(client);
4100 return;
4101 }
4102 wl_resource_set_implementation(resource, &subcompositor_interface,
4103 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004104}
4105
Bryce Harrington0795ece2016-08-30 12:04:26 -07004106/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004107 *
4108 * \param compositor The compositor instance
4109 * \param state The DPMS state the outputs will be set to
4110 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004111static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004112weston_compositor_dpms(struct weston_compositor *compositor,
4113 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004114{
4115 struct weston_output *output;
4116
Bryce Harrington08976ac2016-08-30 12:05:16 -07004117 wl_list_for_each(output, &compositor->output_list, link)
4118 if (output->set_dpms)
4119 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004120}
4121
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004122/** Restores the compositor to active status
4123 *
4124 * \param compositor The compositor instance
4125 *
4126 * If the compositor was in a sleeping mode, all outputs are powered
4127 * back on via DPMS. Otherwise if the compositor was inactive
4128 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4129 * signal will fire.
4130 *
4131 * Restarts the idle timer.
4132 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004133WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004134weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004135{
Neil Roberts8b62e202013-09-30 13:14:47 +01004136 uint32_t old_state = compositor->state;
4137
4138 /* The state needs to be changed before emitting the wake
4139 * signal because that may try to schedule a repaint which
4140 * will not work if the compositor is still sleeping */
4141 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4142
4143 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004144 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004145 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004146 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004147 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004148 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004149 /* fall through */
4150 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004151 wl_event_source_timer_update(compositor->idle_source,
4152 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004153 }
4154}
4155
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004156/** Turns off rendering and frame events for the compositor.
4157 *
4158 * \param compositor The compositor instance
4159 *
4160 * This is used for example to prevent further rendering while the
4161 * compositor is shutting down.
4162 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004163 * Stops the idle timer.
4164 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004165WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004166weston_compositor_offscreen(struct weston_compositor *compositor)
4167{
4168 switch (compositor->state) {
4169 case WESTON_COMPOSITOR_OFFSCREEN:
4170 return;
4171 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004172 default:
4173 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4174 wl_event_source_timer_update(compositor->idle_source, 0);
4175 }
4176}
4177
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004178/** Powers down all attached output devices
4179 *
4180 * \param compositor The compositor instance
4181 *
4182 * Causes rendering to the outputs to cease, and no frame events to be
4183 * sent. Only powers down the outputs if the compositor is not already
4184 * in sleep mode.
4185 *
4186 * Stops the idle timer.
4187 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004188WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004189weston_compositor_sleep(struct weston_compositor *compositor)
4190{
4191 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4192 return;
4193
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004194 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004195 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4196 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4197}
4198
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004199/** Sets compositor to idle mode
4200 *
4201 * \param data The compositor instance
4202 *
4203 * This is called when the idle timer fires. Once the compositor is in
4204 * idle mode it requires a wake action (e.g. via
4205 * weston_compositor_wake()) to restore it. The compositor's
4206 * idle_signal will be triggered when the idle event occurs.
4207 *
4208 * Idleness can be inhibited by setting the compositor's idle_inhibit
4209 * property.
4210 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004211static int
4212idle_handler(void *data)
4213{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004214 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004215
4216 if (compositor->idle_inhibit)
4217 return 1;
4218
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004219 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004220 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004221
4222 return 1;
4223}
4224
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004225WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004226weston_plane_init(struct weston_plane *plane,
4227 struct weston_compositor *ec,
4228 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004229{
4230 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004231 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004232 plane->x = x;
4233 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004234 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004235
4236 /* Init the link so that the call to wl_list_remove() when releasing
4237 * the plane without ever stacking doesn't lead to a crash */
4238 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004239}
4240
4241WL_EXPORT void
4242weston_plane_release(struct weston_plane *plane)
4243{
Xiong Zhang97116532013-10-23 13:58:31 +08004244 struct weston_view *view;
4245
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004246 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004247 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004248
Xiong Zhang97116532013-10-23 13:58:31 +08004249 wl_list_for_each(view, &plane->compositor->view_list, link) {
4250 if (view->plane == plane)
4251 view->plane = NULL;
4252 }
4253
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004254 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004255}
4256
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004257WL_EXPORT void
4258weston_compositor_stack_plane(struct weston_compositor *ec,
4259 struct weston_plane *plane,
4260 struct weston_plane *above)
4261{
4262 if (above)
4263 wl_list_insert(above->link.prev, &plane->link);
4264 else
4265 wl_list_insert(&ec->plane_list, &plane->link);
4266}
4267
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004268static void
4269output_release(struct wl_client *client, struct wl_resource *resource)
4270{
4271 wl_resource_destroy(resource);
4272}
4273
4274static const struct wl_output_interface output_interface = {
4275 output_release,
4276};
4277
4278
Casey Dahlin9074db52012-04-19 22:50:09 -04004279static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004280{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004281 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004282}
4283
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004284static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004285bind_output(struct wl_client *client,
4286 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004287{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004288 struct weston_output *output = data;
4289 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004290 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004291
Jason Ekstranda85118c2013-06-27 20:17:02 -05004292 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004293 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004294 if (resource == NULL) {
4295 wl_client_post_no_memory(client);
4296 return;
4297 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004298
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004299 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004300 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004301
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004302 wl_output_send_geometry(resource,
4303 output->x,
4304 output->y,
4305 output->mm_width,
4306 output->mm_height,
4307 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004308 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004309 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004310 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004311 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004312 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004313
4314 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004315 wl_output_send_mode(resource,
4316 mode->flags,
4317 mode->width,
4318 mode->height,
4319 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004320 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004321
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004322 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004323 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004324}
4325
David Fort0de859e2016-05-27 23:22:57 +02004326/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004327static void
David Fort0de859e2016-05-27 23:22:57 +02004328weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4329 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004330{
4331 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004332 bool start_resizing = false;
4333
4334 if (!delta_width)
4335 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004336
4337 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004338 if (output == resized_output) {
4339 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004340 continue;
4341 }
4342
David Fort0de859e2016-05-27 23:22:57 +02004343 if (start_resizing) {
4344 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004345 output->dirty = 1;
4346 }
4347 }
4348}
4349
Pekka Paalanend72bad22017-03-29 17:01:41 +03004350static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004351weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004352{
Scott Moreau850ca422012-05-21 15:21:25 -06004353 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004354
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004355 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004356 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004357
Scott Moreauccbf29d2012-02-22 14:21:41 -07004358 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004359 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004360 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004361 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004362 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004363 weston_matrix_scale(&output->matrix, magnification,
4364 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004365 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004366
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004367 switch (output->transform) {
4368 case WL_OUTPUT_TRANSFORM_FLIPPED:
4369 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4370 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4371 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4372 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4373 weston_matrix_scale(&output->matrix, -1, 1, 1);
4374 break;
4375 }
4376
4377 switch (output->transform) {
4378 default:
4379 case WL_OUTPUT_TRANSFORM_NORMAL:
4380 case WL_OUTPUT_TRANSFORM_FLIPPED:
4381 break;
4382 case WL_OUTPUT_TRANSFORM_90:
4383 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4384 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4385 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4386 break;
4387 case WL_OUTPUT_TRANSFORM_180:
4388 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4389 weston_matrix_translate(&output->matrix,
4390 -output->width, -output->height, 0);
4391 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4392 break;
4393 case WL_OUTPUT_TRANSFORM_270:
4394 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4395 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4396 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4397 break;
4398 }
4399
4400 if (output->current_scale != 1)
4401 weston_matrix_scale(&output->matrix,
4402 output->current_scale,
4403 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004404
Scott Moreauccbf29d2012-02-22 14:21:41 -07004405 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004406
4407 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004408}
4409
Scott Moreau1bad5db2012-08-18 01:04:05 -06004410static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004411weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004412{
4413 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004414 output->native_scale = scale;
4415 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004416
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004417 convert_size_by_transform_scale(&output->width, &output->height,
4418 output->current_mode->width,
4419 output->current_mode->height,
4420 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004421}
4422
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004423static void
4424weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004425{
4426 output->x = x;
4427 output->y = y;
4428
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004429 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004430 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004431 output->width,
4432 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004433}
4434
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004435WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004436weston_output_move(struct weston_output *output, int x, int y)
4437{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004438 struct wl_resource *resource;
4439
4440 output->move_x = x - output->x;
4441 output->move_y = y - output->y;
4442
4443 if (output->move_x == 0 && output->move_y == 0)
4444 return;
4445
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004446 weston_output_init_geometry(output, x, y);
4447
4448 output->dirty = 1;
4449
4450 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004451 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004452
4453 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004454 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004455 wl_output_send_geometry(resource,
4456 output->x,
4457 output->y,
4458 output->mm_width,
4459 output->mm_height,
4460 output->subpixel,
4461 output->make,
4462 output->model,
4463 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004464
FORT David8a120692016-04-26 23:34:06 +02004465 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004466 wl_output_send_done(resource);
4467 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004468}
4469
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004470/** Signal that a pending output is taken into use.
4471 *
4472 * Removes the output from the pending list and adds it to the compositor's
4473 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03004474 *
4475 * \param compositor The compositor instance.
4476 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004477 *
4478 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03004479 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03004480static void
Giulio Camuffob1147152015-05-06 21:41:57 +03004481weston_compositor_add_output(struct weston_compositor *compositor,
4482 struct weston_output *output)
4483{
Armin Krezoviće5403842016-08-05 15:28:29 +02004484 struct weston_view *view, *next;
4485
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004486 assert(!output->enabled);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004487 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03004488 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004489 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004490
Giulio Camuffob1147152015-05-06 21:41:57 +03004491 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02004492
4493 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4494 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004495}
4496
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004497/** Transform device coordinates into global coordinates
4498 *
4499 * \param device_x[in] X coordinate in device units.
4500 * \param device_y[in] Y coordinate in device units.
4501 * \param x[out] X coordinate in the global space.
4502 * \param y[out] Y coordinate in the global space.
4503 *
4504 * Transforms coordinates from the device coordinate space
4505 * (physical pixel units) to the global coordinate space (logical pixel units).
4506 * This takes into account output transform and scale.
4507 *
4508 * \memberof weston_output
4509 * \internal
4510 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004511WL_EXPORT void
4512weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004513 double device_x, double device_y,
4514 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004515{
Derek Foreman0f679412014-10-02 13:41:17 -05004516 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004517 device_x,
4518 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004519 0.0,
4520 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004521
Derek Foreman67a18b92015-03-24 11:36:14 -05004522 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004523
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004524 *x = p.f[0] / p.f[3];
4525 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004526}
4527
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004528/** Undoes changes to an output done by weston_output_enable()
4529 *
4530 * \param output The weston_output object that needs the changes undone.
4531 *
4532 * Removes the repaint timer.
4533 * Destroys the Wayland global assigned to the output.
4534 * Destroys pixman regions allocated to the output.
4535 * Deallocates output's ID and updates compositor's output_id_pool.
4536 */
4537static void
4538weston_output_enable_undo(struct weston_output *output)
4539{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004540 wl_global_destroy(output->global);
4541
4542 pixman_region32_fini(&output->region);
4543 pixman_region32_fini(&output->previous_damage);
4544 output->compositor->output_id_pool &= ~(1u << output->id);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004545}
4546
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004547/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004548 *
4549 * \param output The weston_output object that is being removed.
4550 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004551 * The following happens:
4552 *
4553 * - The output assignments of all views in the current scenegraph are
4554 * recomputed.
4555 *
4556 * - Presentation feedback is discarded.
4557 *
4558 * - Compositor is notified that outputs were changed and
4559 * applies the necessary changes to re-layout outputs.
4560 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004561 * - The output is put back in the pending outputs list.
4562 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004563 * - Signal is emitted to notify all users of the weston_output
4564 * object that the output is being destroyed.
4565 *
4566 * - wl_output protocol objects referencing this weston_output
4567 * are made inert.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004568 *
4569 * \memberof weston_output
4570 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004571 */
4572static void
4573weston_compositor_remove_output(struct weston_output *output)
4574{
Pekka Paalanenbccda712017-03-29 16:16:04 +03004575 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004576 struct wl_resource *resource;
4577 struct weston_view *view;
4578
4579 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004580 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004581
Pekka Paalanenbccda712017-03-29 16:16:04 +03004582 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004583 if (view->output_mask & (1u << output->id))
4584 weston_view_assign_output(view);
4585 }
4586
4587 weston_presentation_feedback_discard_list(&output->feedback_list);
4588
Pekka Paalanenbccda712017-03-29 16:16:04 +03004589 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004590
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004591 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004592 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004593 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004594
Pekka Paalanenbccda712017-03-29 16:16:04 +03004595 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004596 wl_signal_emit(&output->destroy_signal, output);
4597
4598 wl_resource_for_each(resource, &output->resource_list) {
4599 wl_resource_set_destructor(resource, NULL);
4600 }
4601}
4602
4603/** Sets the output scale for a given output.
4604 *
4605 * \param output The weston_output object that the scale is set for.
4606 * \param scale Scale factor for the given output.
4607 *
4608 * It only supports setting scale for an output that
4609 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004610 *
4611 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004612 */
4613WL_EXPORT void
4614weston_output_set_scale(struct weston_output *output,
4615 int32_t scale)
4616{
4617 /* We can only set scale on a disabled output */
4618 assert(!output->enabled);
4619
4620 /* We only want to set scale once */
4621 assert(!output->scale);
4622
4623 output->scale = scale;
4624}
4625
4626/** Sets the output transform for a given output.
4627 *
4628 * \param output The weston_output object that the transform is set for.
4629 * \param transform Transform value for the given output.
4630 *
4631 * It only supports setting transform for an output that is
4632 * not enabled and it can only be ran once.
4633 *
4634 * Refer to wl_output::transform section located at
4635 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
4636 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004637 *
4638 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004639 */
4640WL_EXPORT void
4641weston_output_set_transform(struct weston_output *output,
4642 uint32_t transform)
4643{
4644 /* We can only set transform on a disabled output */
4645 assert(!output->enabled);
4646
4647 /* We only want to set transform once */
4648 assert(output->transform == UINT32_MAX);
4649
4650 output->transform = transform;
4651}
4652
4653/** Initializes a weston_output object with enough data so
4654 ** an output can be configured.
4655 *
4656 * \param output The weston_output object to initialize
4657 * \param compositor The compositor instance.
4658 *
4659 * Sets initial values for fields that are expected to be
4660 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004661 *
4662 * \memberof weston_output
4663 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004664 */
4665WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02004666weston_output_init(struct weston_output *output,
4667 struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004668{
4669 output->compositor = compositor;
4670 output->destroying = 0;
4671
4672 /* Backends must set output->name */
4673 assert(output->name);
4674
4675 wl_list_init(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004676 output->enabled = false;
4677
4678 /* Add some (in)sane defaults which can be used
4679 * for checking if an output was properly configured
4680 */
4681 output->mm_width = 0;
4682 output->mm_height = 0;
4683 output->scale = 0;
4684 /* Can't use -1 on uint32_t and 0 is valid enum value */
4685 output->transform = UINT32_MAX;
4686}
4687
4688/** Adds weston_output object to pending output list.
4689 *
4690 * \param output The weston_output object to add
4691 * \param compositor The compositor instance.
4692 *
4693 * Also notifies the compositor that an output is pending for
4694 * configuration.
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004695 *
4696 * The opposite of this operation is built into weston_output_destroy().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004697 *
4698 * \memberof weston_output
4699 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004700 */
4701WL_EXPORT void
4702weston_compositor_add_pending_output(struct weston_output *output,
4703 struct weston_compositor *compositor)
4704{
Pekka Paalanene952a012017-03-29 17:14:00 +03004705 assert(output->disable);
4706 assert(output->enable);
4707
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004708 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004709 wl_list_insert(compositor->pending_output_list.prev, &output->link);
4710 wl_signal_emit(&compositor->output_pending_signal, output);
4711}
4712
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004713/** Constructs a weston_output object that can be used by the compositor.
4714 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03004715 * \param output The weston_output object that needs to be enabled. Must not
4716 * be enabled already.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004717 *
4718 * Output coordinates are calculated and each new output is by default
4719 * assigned to the right of previous one.
4720 *
4721 * Sets up the transformation, zoom, and geometry of the output using
4722 * the properties that need to be configured by the compositor.
4723 *
4724 * Establishes a repaint timer for the output with the relevant display
4725 * object's event loop. See output_repaint_timer_handler().
4726 *
4727 * The output is assigned an ID. Weston can support up to 32 distinct
4728 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4729 * is referred to and used to find the first available ID number, and
4730 * then this ID is marked as used in output_id_pool.
4731 *
4732 * The output is also assigned a Wayland global with the wl_output
4733 * external interface.
4734 *
4735 * Backend specific function is called to set up the output output.
4736 *
4737 * Output is added to the compositor's output list
4738 *
4739 * If the backend specific function fails, the weston_output object
4740 * is returned to a state it was before calling this function and
4741 * is added to the compositor's pending_output_list in case it needs
4742 * to be reconfigured or just so it can be destroyed at shutdown.
4743 *
4744 * 0 is returned on success, -1 on failure.
4745 */
4746WL_EXPORT int
4747weston_output_enable(struct weston_output *output)
4748{
Armin Krezović782f5df2016-09-30 14:11:11 +02004749 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004750 struct weston_output *iterator;
4751 int x = 0, y = 0;
4752
Pekka Paalanencc201e42017-03-30 15:11:25 +03004753 if (output->enabled) {
4754 weston_log("Error: attempt to enable an enabled output '%s'\n",
4755 output->name);
4756 return -1;
4757 }
4758
Armin Krezović782f5df2016-09-30 14:11:11 +02004759 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004760 struct weston_output, link);
4761
Armin Krezović782f5df2016-09-30 14:11:11 +02004762 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004763 x = iterator->x + iterator->width;
4764
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004765 /* Make sure the scale is set up */
4766 assert(output->scale);
4767
4768 /* Make sure we have a transform set */
4769 assert(output->transform != UINT32_MAX);
4770
Armin Krezović782f5df2016-09-30 14:11:11 +02004771 /* Verify we haven't reached the limit of 32 available output IDs */
4772 assert(ffs(~c->output_id_pool) > 0);
4773
4774 output->x = x;
4775 output->y = y;
4776 output->dirty = 1;
4777 output->original_scale = output->scale;
4778
4779 weston_output_transform_scale_init(output, output->transform, output->scale);
4780 weston_output_init_zoom(output);
4781
4782 weston_output_init_geometry(output, x, y);
4783 weston_output_damage(output);
4784
4785 wl_signal_init(&output->frame_signal);
4786 wl_signal_init(&output->destroy_signal);
4787 wl_list_init(&output->animation_list);
4788 wl_list_init(&output->resource_list);
4789 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02004790
Armin Krezović782f5df2016-09-30 14:11:11 +02004791 /* Invert the output id pool and look for the lowest numbered
4792 * switch (the least significant bit). Take that bit's position
4793 * as our ID, and mark it used in the compositor's output_id_pool.
4794 */
4795 output->id = ffs(~output->compositor->output_id_pool) - 1;
4796 output->compositor->output_id_pool |= 1u << output->id;
4797
4798 output->global =
4799 wl_global_create(c->wl_display, &wl_output_interface, 3,
4800 output, bind_output);
4801
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004802 /* Enable the output (set up the crtc or create a
4803 * window representing the output, set up the
4804 * renderer, etc)
4805 */
4806 if (output->enable(output) < 0) {
4807 weston_log("Enabling output \"%s\" failed.\n", output->name);
4808
4809 weston_output_enable_undo(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004810 return -1;
4811 }
4812
4813 weston_compositor_add_output(output->compositor, output);
4814
4815 return 0;
4816}
4817
4818/** Converts a weston_output object to a pending output state, so it
4819 ** can be configured again or destroyed.
4820 *
4821 * \param output The weston_output object that needs to be disabled.
4822 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004823 * Calls a backend specific function to disable an output, in case
4824 * such function exists.
4825 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004826 * The backend specific disable function may choose to postpone the disabling
4827 * by returning a negative value, in which case this function returns early.
4828 * In that case the backend will guarantee the output will be disabled soon
4829 * by the backend calling this function again. One must not attempt to re-enable
4830 * the output until that happens.
4831 *
4832 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004833 * from weston's output_list (see weston_compositor_remove_output())
4834 * and is returned to a state it was before weston_output_enable()
4835 * was ran (see weston_output_enable_undo()).
4836 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004837 * See weston_output_init() for more information on the
4838 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03004839 *
4840 * If the output has never been enabled yet, this function can still be
4841 * called to ensure that the output is actually turned off rather than left
4842 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004843 */
4844WL_EXPORT void
4845weston_output_disable(struct weston_output *output)
4846{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004847 /* Should we rename this? */
4848 output->destroying = 1;
4849
Pekka Paalanenc65df642017-03-29 15:45:46 +03004850 /* Disable is called unconditionally also for not-enabled outputs,
4851 * because at compositor start-up, if there is an output that is
4852 * already on but the compositor wants to turn it off, we have to
4853 * forward the turn-off to the backend so it knows to do it.
4854 * The backend cannot initially turn off everything, because it
4855 * would cause unnecessary mode-sets for all outputs the compositor
4856 * wants to be on.
4857 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004858 if (output->disable(output) < 0)
4859 return;
4860
4861 if (output->enabled) {
4862 weston_compositor_remove_output(output);
4863 weston_output_enable_undo(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004864 }
4865
4866 output->destroying = 0;
4867}
4868
4869/** Emits a signal to indicate that there are outputs waiting to be configured.
4870 *
4871 * \param compositor The compositor instance
4872 */
4873WL_EXPORT void
4874weston_pending_output_coldplug(struct weston_compositor *compositor)
4875{
4876 struct weston_output *output, *next;
4877
4878 wl_list_for_each_safe(output, next, &compositor->pending_output_list, link)
4879 wl_signal_emit(&compositor->output_pending_signal, output);
4880}
4881
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004882/** Uninitialize an output
4883 *
4884 * Removes the output from the list of enabled outputs if necessary, but
4885 * does not call the backend's output disable function. The output will no
4886 * longer be in the list of pending outputs either.
4887 *
4888 * All fields of weston_output become uninitialized, i.e. should not be used
4889 * anymore. The caller can free the memory after this.
4890 *
4891 * \memberof weston_output
4892 * \internal
4893 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004894WL_EXPORT void
4895weston_output_destroy(struct weston_output *output)
4896{
4897 output->destroying = 1;
4898
4899 if (output->enabled) {
4900 weston_compositor_remove_output(output);
4901 weston_output_enable_undo(output);
4902 }
4903
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004904 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004905 free(output->name);
4906}
4907
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004908static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004909destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004910{
Jonny Lamb74130762013-11-26 18:19:46 +01004911 struct weston_surface *surface =
4912 wl_resource_get_user_data(resource);
4913
Pekka Paalanen4826f872016-04-22 14:14:38 +03004914 if (!surface)
4915 return;
4916
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004917 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004918 surface->pending.buffer_viewport.buffer.src_width =
4919 wl_fixed_from_int(-1);
4920 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004921 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004922}
4923
4924static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004925viewport_destroy(struct wl_client *client,
4926 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004927{
4928 wl_resource_destroy(resource);
4929}
4930
4931static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004932viewport_set_source(struct wl_client *client,
4933 struct wl_resource *resource,
4934 wl_fixed_t src_x,
4935 wl_fixed_t src_y,
4936 wl_fixed_t src_width,
4937 wl_fixed_t src_height)
4938{
4939 struct weston_surface *surface =
4940 wl_resource_get_user_data(resource);
4941
Pekka Paalanen4826f872016-04-22 14:14:38 +03004942 if (!surface) {
4943 wl_resource_post_error(resource,
4944 WP_VIEWPORT_ERROR_NO_SURFACE,
4945 "wl_surface for this viewport is no longer exists");
4946 return;
4947 }
4948
4949 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004950 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004951
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004952 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004953 src_height == wl_fixed_from_int(-1) &&
4954 src_x == wl_fixed_from_int(-1) &&
4955 src_y == wl_fixed_from_int(-1)) {
4956 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004957 surface->pending.buffer_viewport.buffer.src_width =
4958 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004959 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004960 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004961 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004962
Pekka Paalanen201769a2016-04-26 14:42:11 +03004963 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004964 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004965 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03004966 "wl_surface@%d viewport source "
4967 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4968 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004969 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03004970 wl_fixed_to_double(src_height),
4971 wl_fixed_to_double(src_x),
4972 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004973 return;
4974 }
4975
4976 surface->pending.buffer_viewport.buffer.src_x = src_x;
4977 surface->pending.buffer_viewport.buffer.src_y = src_y;
4978 surface->pending.buffer_viewport.buffer.src_width = src_width;
4979 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004980 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004981}
4982
4983static void
4984viewport_set_destination(struct wl_client *client,
4985 struct wl_resource *resource,
4986 int32_t dst_width,
4987 int32_t dst_height)
4988{
4989 struct weston_surface *surface =
4990 wl_resource_get_user_data(resource);
4991
Pekka Paalanen4826f872016-04-22 14:14:38 +03004992 if (!surface) {
4993 wl_resource_post_error(resource,
4994 WP_VIEWPORT_ERROR_NO_SURFACE,
4995 "wl_surface for this viewport no longer exists");
4996 return;
4997 }
4998
4999 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005000
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005001 if (dst_width == -1 && dst_height == -1) {
5002 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005003 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005004 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005005 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005006 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005007
5008 if (dst_width <= 0 || dst_height <= 0) {
5009 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005010 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005011 "destination size must be positive (%dx%d)",
5012 dst_width, dst_height);
5013 return;
5014 }
5015
5016 surface->pending.buffer_viewport.surface.width = dst_width;
5017 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005018 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005019}
5020
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005021static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005022 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005023 viewport_set_source,
5024 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01005025};
5026
5027static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005028viewporter_destroy(struct wl_client *client,
5029 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005030{
5031 wl_resource_destroy(resource);
5032}
5033
5034static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005035viewporter_get_viewport(struct wl_client *client,
5036 struct wl_resource *viewporter,
5037 uint32_t id,
5038 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005039{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005040 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005041 struct weston_surface *surface =
5042 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005043 struct wl_resource *resource;
5044
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005045 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005046 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005047 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005048 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01005049 return;
5050 }
5051
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005052 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005053 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005054 if (resource == NULL) {
5055 wl_client_post_no_memory(client);
5056 return;
5057 }
5058
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005059 wl_resource_set_implementation(resource, &viewport_interface,
5060 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01005061
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005062 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005063}
5064
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005065static const struct wp_viewporter_interface viewporter_interface = {
5066 viewporter_destroy,
5067 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01005068};
5069
5070static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005071bind_viewporter(struct wl_client *client,
5072 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005073{
5074 struct wl_resource *resource;
5075
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005076 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005077 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005078 if (resource == NULL) {
5079 wl_client_post_no_memory(client);
5080 return;
5081 }
5082
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005083 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01005084 NULL, NULL);
5085}
5086
5087static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04005088destroy_presentation_feedback(struct wl_resource *feedback_resource)
5089{
5090 struct weston_presentation_feedback *feedback;
5091
5092 feedback = wl_resource_get_user_data(feedback_resource);
5093
5094 wl_list_remove(&feedback->link);
5095 free(feedback);
5096}
5097
5098static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005099presentation_destroy(struct wl_client *client, struct wl_resource *resource)
5100{
5101 wl_resource_destroy(resource);
5102}
5103
5104static void
5105presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005106 struct wl_resource *presentation_resource,
5107 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005108 uint32_t callback)
5109{
Pekka Paalanen133e4392014-09-23 22:08:46 -04005110 struct weston_surface *surface;
5111 struct weston_presentation_feedback *feedback;
5112
5113 surface = wl_resource_get_user_data(surface_resource);
5114
Bryce Harringtonde16d892014-11-20 22:21:57 -08005115 feedback = zalloc(sizeof *feedback);
5116 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04005117 goto err_calloc;
5118
5119 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005120 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005121 1, callback);
5122 if (!feedback->resource)
5123 goto err_create;
5124
5125 wl_resource_set_implementation(feedback->resource, NULL, feedback,
5126 destroy_presentation_feedback);
5127
5128 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
5129
5130 return;
5131
5132err_create:
5133 free(feedback);
5134
5135err_calloc:
5136 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005137}
5138
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005139static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005140 presentation_destroy,
5141 presentation_feedback
5142};
5143
5144static void
5145bind_presentation(struct wl_client *client,
5146 void *data, uint32_t version, uint32_t id)
5147{
5148 struct weston_compositor *compositor = data;
5149 struct wl_resource *resource;
5150
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005151 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005152 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005153 if (resource == NULL) {
5154 wl_client_post_no_memory(client);
5155 return;
5156 }
5157
5158 wl_resource_set_implementation(resource, &presentation_implementation,
5159 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005160 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005161}
5162
5163static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05005164compositor_bind(struct wl_client *client,
5165 void *data, uint32_t version, uint32_t id)
5166{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005167 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005168 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05005169
Jason Ekstranda85118c2013-06-27 20:17:02 -05005170 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005171 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005172 if (resource == NULL) {
5173 wl_client_post_no_memory(client);
5174 return;
5175 }
5176
5177 wl_resource_set_implementation(resource, &compositor_interface,
5178 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05005179}
5180
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005181WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005182weston_environment_get_fd(const char *env)
5183{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005184 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005185 int fd, flags;
5186
5187 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005188 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005189 return -1;
5190
5191 flags = fcntl(fd, F_GETFD);
5192 if (flags == -1)
5193 return -1;
5194
5195 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
5196 unsetenv(env);
5197
5198 return fd;
5199}
5200
Pekka Paalanenb5026542014-11-12 15:09:24 +02005201static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005202timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02005203 uint32_t key, void *data)
5204{
5205 struct weston_compositor *compositor = data;
5206
5207 if (weston_timeline_enabled_)
5208 weston_timeline_close();
5209 else
5210 weston_timeline_open(compositor);
5211}
5212
Giulio Camuffo459137b2014-10-11 23:56:24 +03005213/** Create the compositor.
5214 *
5215 * This functions creates and initializes a compositor instance.
5216 *
5217 * \param display The Wayland display to be used.
5218 * \param user_data A pointer to an object that can later be retrieved
5219 * using the \ref weston_compositor_get_user_data function.
5220 * \return The compositor instance on success or NULL on failure.
5221 */
5222WL_EXPORT struct weston_compositor *
5223weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005224{
Giulio Camuffo459137b2014-10-11 23:56:24 +03005225 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05005226 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07005227
Giulio Camuffo459137b2014-10-11 23:56:24 +03005228 ec = zalloc(sizeof *ec);
5229 if (!ec)
5230 return NULL;
5231
5232 ec->wl_display = display;
5233 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005234 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07005235 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005236 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005237 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005238 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005239 wl_signal_init(&ec->idle_signal);
5240 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005241 wl_signal_init(&ec->show_input_panel_signal);
5242 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005243 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01005244 wl_signal_init(&ec->seat_created_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005245 wl_signal_init(&ec->output_pending_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01005246 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02005247 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005248 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02005249 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005250 wl_signal_init(&ec->session_signal);
5251 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005252
Casey Dahlin58ba1372012-04-19 22:50:08 -04005253 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03005254 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04005255
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02005256 ec->activate_serial = 1;
5257
Derek Foreman152254b2015-11-26 14:17:48 -06005258 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005259 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005260 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05005261
Giulio Camuffo954f1832014-10-11 18:27:30 +03005262 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005263 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005264 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03005265
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005266 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005267 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005268 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005269
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005270 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005271 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005272 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005273
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005274 if (weston_input_init(ec) != 0)
5275 goto fail;
5276
Jason Ekstranda7af7042013-10-12 22:38:11 -05005277 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005278 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005279 wl_list_init(&ec->layer_list);
5280 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005281 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005282 wl_list_init(&ec->output_list);
5283 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01005284 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005285 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005286 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005287 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005288 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005289
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005290 wl_list_init(&ec->plugin_api_list);
5291
Xiong Zhang97116532013-10-23 13:58:31 +08005292 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005293 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005294
Giulio Camuffo459137b2014-10-11 23:56:24 +03005295 wl_data_device_manager_init(ec->wl_display);
5296
5297 wl_display_init_shm(ec->wl_display);
5298
5299 loop = wl_display_get_event_loop(ec->wl_display);
5300 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00005301 ec->repaint_timer =
5302 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
5303 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005304
Quentin Glidic82681572016-12-17 13:40:51 +01005305 weston_layer_init(&ec->fade_layer, ec);
5306 weston_layer_init(&ec->cursor_layer, ec);
5307
5308 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
5309 weston_layer_set_position(&ec->cursor_layer,
5310 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005311
5312 weston_compositor_add_debug_binding(ec, KEY_T,
5313 timeline_key_binding_handler, ec);
5314
Giulio Camuffo459137b2014-10-11 23:56:24 +03005315 return ec;
5316
5317fail:
5318 free(ec);
5319 return NULL;
5320}
5321
Benjamin Franzkeb8263022011-08-30 11:32:47 +02005322WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005323weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07005324{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005325 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07005326
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005327 wl_event_source_remove(ec->idle_source);
5328
Matt Roper361d2ad2011-08-29 13:52:23 -07005329 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02005330 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07005331 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02005332
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005333 /* Destroy all pending outputs associated with this compositor */
5334 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
5335 output->destroy(output);
5336
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02005337 if (ec->renderer)
5338 ec->renderer->destroy(ec);
5339
Daniel Stone325fc2d2012-05-30 16:31:58 +01005340 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09005341 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005342 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005343 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005344 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005345 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005346
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005347 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07005348}
5349
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005350WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04005351weston_compositor_exit_with_code(struct weston_compositor *compositor,
5352 int exit_code)
5353{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02005354 if (compositor->exit_code == EXIT_SUCCESS)
5355 compositor->exit_code = exit_code;
5356
Giulio Camuffo459137b2014-10-11 23:56:24 +03005357 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04005358}
5359
5360WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005361weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
5362 const struct weston_pointer_grab_interface *interface)
5363{
5364 struct weston_seat *seat;
5365
5366 ec->default_pointer_grab = interface;
5367 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005368 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5369
5370 if (pointer)
5371 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005372 }
5373}
5374
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04005375WL_EXPORT int
5376weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
5377 clockid_t clk_id)
5378{
5379 struct timespec ts;
5380
5381 if (clock_gettime(clk_id, &ts) < 0)
5382 return -1;
5383
5384 compositor->presentation_clock = clk_id;
5385
5386 return 0;
5387}
5388
5389/*
5390 * For choosing the software clock, when the display hardware or API
5391 * does not expose a compatible presentation timestamp.
5392 */
5393WL_EXPORT int
5394weston_compositor_set_presentation_clock_software(
5395 struct weston_compositor *compositor)
5396{
5397 /* In order of preference */
5398 static const clockid_t clocks[] = {
5399 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
5400 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
5401 CLOCK_MONOTONIC, /* no jumps, may crawl */
5402 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
5403 CLOCK_REALTIME /* may jump and crawl */
5404 };
5405 unsigned i;
5406
5407 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
5408 if (weston_compositor_set_presentation_clock(compositor,
5409 clocks[i]) == 0)
5410 return 0;
5411
5412 weston_log("Error: no suitable presentation clock available.\n");
5413
5414 return -1;
5415}
5416
Pekka Paalanen662f3842015-03-18 12:17:26 +02005417/** Read the current time from the Presentation clock
5418 *
5419 * \param compositor
5420 * \param ts[out] The current time.
5421 *
5422 * \note Reading the current time in user space is always imprecise to some
5423 * degree.
5424 *
5425 * This function is never meant to fail. If reading the clock does fail,
5426 * an error message is logged and a zero time is returned. Callers are not
5427 * supposed to detect or react to failures.
5428 */
5429WL_EXPORT void
5430weston_compositor_read_presentation_clock(
5431 const struct weston_compositor *compositor,
5432 struct timespec *ts)
5433{
5434 static bool warned;
5435 int ret;
5436
5437 ret = clock_gettime(compositor->presentation_clock, ts);
5438 if (ret < 0) {
5439 ts->tv_sec = 0;
5440 ts->tv_nsec = 0;
5441
5442 if (!warned)
5443 weston_log("Error: failure to read "
5444 "the presentation clock %#x: '%m' (%d)\n",
5445 compositor->presentation_clock, errno);
5446 warned = true;
5447 }
5448}
5449
Pekka Paalanen230f3b12014-09-29 14:18:40 -04005450/** Import dmabuf buffer into current renderer
5451 *
5452 * \param compositor
5453 * \param buffer the dmabuf buffer to import
5454 * \return true on usable buffers, false otherwise
5455 *
5456 * This function tests that the linux_dmabuf_buffer is usable
5457 * for the current renderer. Returns false on unusable buffers. Usually
5458 * usability is tested by importing the dmabufs for composition.
5459 *
5460 * This hook is also used for detecting if the renderer supports
5461 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
5462 * supported.
5463 * */
5464WL_EXPORT bool
5465weston_compositor_import_dmabuf(struct weston_compositor *compositor,
5466 struct linux_dmabuf_buffer *buffer)
5467{
5468 struct weston_renderer *renderer;
5469
5470 renderer = compositor->renderer;
5471
5472 if (renderer->import_dmabuf == NULL)
5473 return false;
5474
5475 return renderer->import_dmabuf(compositor, buffer);
5476}
5477
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005478WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005479weston_version(int *major, int *minor, int *micro)
5480{
5481 *major = WESTON_VERSION_MAJOR;
5482 *minor = WESTON_VERSION_MINOR;
5483 *micro = WESTON_VERSION_MICRO;
5484}
5485
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005486WL_EXPORT void *
5487weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005488{
Derek Foreman3f86e502015-06-08 11:46:54 -05005489 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005490 char path[PATH_MAX];
5491 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00005492 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005493
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005494 if (name == NULL)
5495 return NULL;
5496
Derek Foreman3f86e502015-06-08 11:46:54 -05005497 if (name[0] != '/') {
5498 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00005499 len = snprintf(path, sizeof path, "%s/.libs/%s",
5500 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005501 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00005502 len = snprintf(path, sizeof path, "%s/%s",
5503 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005504 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00005505 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005506 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005507
Daniel Stonebeb97e52016-11-28 12:13:54 +00005508 /* snprintf returns the length of the string it would've written,
5509 * _excluding_ the NUL byte. So even being equal to the size of
5510 * our buffer is an error here. */
5511 if (len >= sizeof path)
5512 return NULL;
5513
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005514 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
5515 if (module) {
5516 weston_log("Module '%s' already loaded\n", path);
5517 dlclose(module);
5518 return NULL;
5519 }
5520
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005521 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04005522 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005523 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005524 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005525 return NULL;
5526 }
5527
5528 init = dlsym(module, entrypoint);
5529 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005530 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00005531 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005532 return NULL;
5533 }
5534
5535 return init;
5536}
5537
Giulio Camuffo459137b2014-10-11 23:56:24 +03005538
5539/** Destroys the compositor.
5540 *
5541 * This function cleans up the compositor state and destroys it.
5542 *
5543 * \param compositor The compositor to be destroyed.
5544 */
5545WL_EXPORT void
5546weston_compositor_destroy(struct weston_compositor *compositor)
5547{
5548 /* prevent further rendering while shutting down */
5549 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5550
5551 wl_signal_emit(&compositor->destroy_signal, compositor);
5552
5553 weston_compositor_xkb_destroy(compositor);
5554
Giulio Camuffo2d24e642015-10-03 16:25:15 +03005555 if (compositor->backend)
5556 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005557
5558 weston_plugin_api_destroy_list(compositor);
5559
Giulio Camuffo459137b2014-10-11 23:56:24 +03005560 free(compositor);
5561}
5562
5563/** Instruct the compositor to exit.
5564 *
5565 * This functions does not directly destroy the compositor object, it merely
5566 * command it to start the tear down process. It is not guaranteed that the
5567 * tear down will happen immediately.
5568 *
5569 * \param compositor The compositor to tear down.
5570 */
5571WL_EXPORT void
5572weston_compositor_exit(struct weston_compositor *compositor)
5573{
5574 compositor->exit(compositor);
5575}
5576
5577/** Return the user data stored in the compositor.
5578 *
5579 * This function returns the user data pointer set with user_data parameter
5580 * to the \ref weston_compositor_create function.
5581 */
5582WL_EXPORT void *
5583weston_compositor_get_user_data(struct weston_compositor *compositor)
5584{
5585 return compositor->user_data;
5586}
Pekka Paalanendd186732016-06-03 14:49:54 +03005587
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005588static const char * const backend_map[] = {
5589 [WESTON_BACKEND_DRM] = "drm-backend.so",
5590 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5591 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5592 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5593 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5594 [WESTON_BACKEND_X11] = "x11-backend.so",
5595};
5596
Pekka Paalanendd186732016-06-03 14:49:54 +03005597/** Load a backend into a weston_compositor
5598 *
5599 * A backend must be loaded to make a weston_compositor work. A backend
5600 * provides input and output capabilities, and determines the renderer to use.
5601 *
5602 * \param compositor A compositor that has not had a backend loaded yet.
5603 * \param backend Name of the backend file.
5604 * \param config_base A pointer to a backend-specific configuration
5605 * structure's 'base' member.
5606 *
5607 * \return 0 on success, or -1 on error.
5608 */
5609WL_EXPORT int
5610weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005611 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005612 struct weston_backend_config *config_base)
5613{
5614 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005615 struct weston_backend_config *config_base);
5616
Quentin Glidic887c0182016-07-10 11:00:53 +02005617 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005618 return -1;
5619
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01005620 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005621 if (!backend_init)
5622 return -1;
5623
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005624 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005625}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005626
5627WL_EXPORT int
5628weston_compositor_load_xwayland(struct weston_compositor *compositor)
5629{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005630 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005631
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005632 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005633 if (!module_init)
5634 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005635 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005636 return -1;
5637 return 0;
5638}