blob: 71a9b38c9137015f329abf76e0fa701f332e17f4 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050026 */
27
Kristian Høgsberga9410222011-01-14 17:22:35 -050028#include "config.h"
29
Daniel Stoneb7452fe2012-06-01 12:14:06 +010030#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010035#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020037#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010039#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040040#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020041#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020042#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020043#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050045#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040046#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040047#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040048#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050049#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040050#include <sys/time.h>
51#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030052#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050053
Pekka Paalanenb5026542014-11-12 15:09:24 +020054#include "timeline.h"
55
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030057#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020058#include "presentation-time-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070059#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070060#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070061#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030062#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040063#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050064#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020065#include "plugin-registry.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066
Pekka Paalanen0513a952014-05-21 16:17:27 +030067#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
68
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020069static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030070weston_output_update_matrix(struct weston_output *output);
71
72static void
Alexander Larsson0b135062013-05-28 16:23:36 +020073weston_output_transform_scale_init(struct weston_output *output,
74 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020075
Rob Bradford27b17932013-06-26 18:08:46 +010076static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050077weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010078
Derek Foreman6ae7bc92014-11-04 10:47:33 -060079static void weston_mode_switch_finish(struct weston_output *output,
80 int mode_changed,
81 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080082{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020083 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020084 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020085 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060086 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020087
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020088 pixman_region32_init(&old_output_region);
89 pixman_region32_copy(&old_output_region, &output->region);
90
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020092 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020093
94 pixman_region32_init(&output->previous_damage);
95 pixman_region32_init_rect(&output->region, output->x, output->y,
96 output->width, output->height);
97
98 weston_output_update_matrix(output);
99
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200100 /* If a pointer falls outside the outputs new geometry, move it to its
101 * lower-right corner */
102 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500103 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200104 int32_t x, y;
105
106 if (!pointer)
107 continue;
108
109 x = wl_fixed_to_int(pointer->x);
110 y = wl_fixed_to_int(pointer->y);
111
112 if (!pixman_region32_contains_point(&old_output_region,
113 x, y, NULL) ||
114 pixman_region32_contains_point(&output->region,
115 x, y, NULL))
116 continue;
117
118 if (x >= output->x + output->width)
119 x = output->x + output->width - 1;
120 if (y >= output->y + output->height)
121 y = output->y + output->height - 1;
122
123 pointer->x = wl_fixed_from_int(x);
124 pointer->y = wl_fixed_from_int(y);
125 }
126
127 pixman_region32_fini(&old_output_region);
128
Derek Foremandd4cd332014-11-10 10:29:59 -0600129 if (!mode_changed && !scale_changed)
130 return;
131
Hardening57388e42013-09-18 23:56:36 +0200132 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600133 wl_resource_for_each(resource, &output->resource_list) {
134 if (mode_changed) {
135 wl_output_send_mode(resource,
136 output->current_mode->flags,
137 output->current_mode->width,
138 output->current_mode->height,
139 output->current_mode->refresh);
140 }
Hardening57388e42013-09-18 23:56:36 +0200141
Derek Foreman41bdc272014-11-05 13:26:57 -0600142 version = wl_resource_get_version(resource);
143 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600144 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200145
Derek Foreman41bdc272014-11-05 13:26:57 -0600146 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
147 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600148 }
149}
150
David Fort0de859e2016-05-27 23:22:57 +0200151
152static void
153weston_compositor_reflow_outputs(struct weston_compositor *compositor,
154 struct weston_output *resized_output, int delta_width);
155
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600156WL_EXPORT int
157weston_output_mode_set_native(struct weston_output *output,
158 struct weston_mode *mode,
159 int32_t scale)
160{
161 int ret;
162 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200163 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600164
165 if (!output->switch_mode)
166 return -1;
167
168 if (!output->original_mode) {
169 mode_changed = 1;
170 ret = output->switch_mode(output, mode);
171 if (ret < 0)
172 return ret;
173 if (output->current_scale != scale) {
174 scale_changed = 1;
175 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200176 }
177 }
178
David Fort0de859e2016-05-27 23:22:57 +0200179 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600180 output->native_mode = mode;
181 output->native_scale = scale;
182
183 weston_mode_switch_finish(output, mode_changed, scale_changed);
184
David Fort0de859e2016-05-27 23:22:57 +0200185 if (mode_changed || scale_changed) {
186 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
187
188 wl_signal_emit(&output->compositor->output_resized_signal, output);
189 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600190 return 0;
191}
192
193WL_EXPORT int
194weston_output_mode_switch_to_native(struct weston_output *output)
195{
196 int ret;
197 int mode_changed = 0, scale_changed = 0;
198
199 if (!output->switch_mode)
200 return -1;
201
202 if (!output->original_mode) {
203 weston_log("already in the native mode\n");
204 return -1;
205 }
206 /* the non fullscreen clients haven't seen a mode set since we
207 * switched into a temporary, so we need to notify them if the
208 * mode at that time is different from the native mode now.
209 */
210 mode_changed = (output->original_mode != output->native_mode);
211 scale_changed = (output->original_scale != output->native_scale);
212
213 ret = output->switch_mode(output, output->native_mode);
214 if (ret < 0)
215 return ret;
216
217 output->current_scale = output->native_scale;
218
219 output->original_mode = NULL;
220 output->original_scale = 0;
221
222 weston_mode_switch_finish(output, mode_changed, scale_changed);
223
224 return 0;
225}
226
227WL_EXPORT int
228weston_output_mode_switch_to_temporary(struct weston_output *output,
229 struct weston_mode *mode,
230 int32_t scale)
231{
232 int ret;
233
234 if (!output->switch_mode)
235 return -1;
236
237 /* original_mode is the last mode non full screen clients have seen,
238 * so we shouldn't change it if we already have one set.
239 */
240 if (!output->original_mode) {
241 output->original_mode = output->native_mode;
242 output->original_scale = output->native_scale;
243 }
244 ret = output->switch_mode(output, mode);
245 if (ret < 0)
246 return ret;
247
248 output->current_scale = scale;
249
250 weston_mode_switch_finish(output, 0, 0);
251
252 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800253}
254
Benjamin Franzke06286262011-05-06 19:12:33 +0200255static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300256region_init_infinite(pixman_region32_t *region)
257{
258 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
259 UINT32_MAX, UINT32_MAX);
260}
261
Pekka Paalanene67858b2013-04-25 13:57:42 +0300262static struct weston_subsurface *
263weston_surface_to_subsurface(struct weston_surface *surface);
264
Jason Ekstranda7af7042013-10-12 22:38:11 -0500265WL_EXPORT struct weston_view *
266weston_view_create(struct weston_surface *surface)
267{
268 struct weston_view *view;
269
Bryce Harringtonde16d892014-11-20 22:21:57 -0800270 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 if (view == NULL)
272 return NULL;
273
274 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000275 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500276
Jason Ekstranda7af7042013-10-12 22:38:11 -0500277 /* Assign to surface */
278 wl_list_insert(&surface->views, &view->surface_link);
279
280 wl_signal_init(&view->destroy_signal);
281 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300282 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500283
Jason Ekstranda7af7042013-10-12 22:38:11 -0500284 pixman_region32_init(&view->clip);
285
286 view->alpha = 1.0;
287 pixman_region32_init(&view->transform.opaque);
288
289 wl_list_init(&view->geometry.transformation_list);
290 wl_list_insert(&view->geometry.transformation_list,
291 &view->transform.position.link);
292 weston_matrix_init(&view->transform.position.matrix);
293 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200294 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500295 pixman_region32_init(&view->transform.boundingbox);
296 view->transform.dirty = 1;
297
Jason Ekstranda7af7042013-10-12 22:38:11 -0500298 return view;
299}
300
Jason Ekstrand108865d2014-06-26 10:04:49 -0700301struct weston_frame_callback {
302 struct wl_resource *resource;
303 struct wl_list link;
304};
305
Pekka Paalanen133e4392014-09-23 22:08:46 -0400306struct weston_presentation_feedback {
307 struct wl_resource *resource;
308
309 /* XXX: could use just wl_resource_get_link() instead */
310 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200311
312 /* The per-surface feedback flags */
313 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400314};
315
316static void
317weston_presentation_feedback_discard(
318 struct weston_presentation_feedback *feedback)
319{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200320 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400321 wl_resource_destroy(feedback->resource);
322}
323
324static void
325weston_presentation_feedback_discard_list(struct wl_list *list)
326{
327 struct weston_presentation_feedback *feedback, *tmp;
328
329 wl_list_for_each_safe(feedback, tmp, list, link)
330 weston_presentation_feedback_discard(feedback);
331}
332
333static void
334weston_presentation_feedback_present(
335 struct weston_presentation_feedback *feedback,
336 struct weston_output *output,
337 uint32_t refresh_nsec,
338 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200339 uint64_t seq,
340 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400341{
342 struct wl_client *client = wl_resource_get_client(feedback->resource);
343 struct wl_resource *o;
344 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400345
346 wl_resource_for_each(o, &output->resource_list) {
347 if (wl_resource_get_client(o) != client)
348 continue;
349
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200350 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400351 }
352
353 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200354 wp_presentation_feedback_send_presented(feedback->resource,
355 secs >> 32, secs & 0xffffffff,
356 ts->tv_nsec,
357 refresh_nsec,
358 seq >> 32, seq & 0xffffffff,
359 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400360 wl_resource_destroy(feedback->resource);
361}
362
363static void
364weston_presentation_feedback_present_list(struct wl_list *list,
365 struct weston_output *output,
366 uint32_t refresh_nsec,
367 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200368 uint64_t seq,
369 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400370{
371 struct weston_presentation_feedback *feedback, *tmp;
372
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200373 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200374 wl_list_empty(list));
375
Pekka Paalanen133e4392014-09-23 22:08:46 -0400376 wl_list_for_each_safe(feedback, tmp, list, link)
377 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200378 refresh_nsec, ts, seq,
379 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400380}
381
Jason Ekstrand7b982072014-05-20 14:33:03 -0500382static void
383surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
384{
385 struct weston_surface_state *state =
386 container_of(listener, struct weston_surface_state,
387 buffer_destroy_listener);
388
389 state->buffer = NULL;
390}
391
392static void
393weston_surface_state_init(struct weston_surface_state *state)
394{
395 state->newly_attached = 0;
396 state->buffer = NULL;
397 state->buffer_destroy_listener.notify =
398 surface_state_handle_buffer_destroy;
399 state->sx = 0;
400 state->sy = 0;
401
Derek Foreman152254b2015-11-26 14:17:48 -0600402 pixman_region32_init(&state->damage_surface);
403 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500404 pixman_region32_init(&state->opaque);
405 region_init_infinite(&state->input);
406
407 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400408 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500409
410 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
411 state->buffer_viewport.buffer.scale = 1;
412 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
413 state->buffer_viewport.surface.width = -1;
414 state->buffer_viewport.changed = 0;
415}
416
417static void
418weston_surface_state_fini(struct weston_surface_state *state)
419{
420 struct weston_frame_callback *cb, *next;
421
422 wl_list_for_each_safe(cb, next,
423 &state->frame_callback_list, link)
424 wl_resource_destroy(cb->resource);
425
Pekka Paalanen133e4392014-09-23 22:08:46 -0400426 weston_presentation_feedback_discard_list(&state->feedback_list);
427
Jason Ekstrand7b982072014-05-20 14:33:03 -0500428 pixman_region32_fini(&state->input);
429 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600430 pixman_region32_fini(&state->damage_surface);
431 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500432
433 if (state->buffer)
434 wl_list_remove(&state->buffer_destroy_listener.link);
435 state->buffer = NULL;
436}
437
438static void
439weston_surface_state_set_buffer(struct weston_surface_state *state,
440 struct weston_buffer *buffer)
441{
442 if (state->buffer == buffer)
443 return;
444
445 if (state->buffer)
446 wl_list_remove(&state->buffer_destroy_listener.link);
447 state->buffer = buffer;
448 if (state->buffer)
449 wl_signal_add(&state->buffer->destroy_signal,
450 &state->buffer_destroy_listener);
451}
452
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500453WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500454weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500455{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500456 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400457
Bryce Harringtonde16d892014-11-20 22:21:57 -0800458 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400459 if (surface == NULL)
460 return NULL;
461
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500462 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800463 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500464
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500465 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200466 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400467
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200468 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
469 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200470 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
471 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500472
473 weston_surface_state_init(&surface->pending);
474
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400475 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500476 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300477 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400478
Jason Ekstranda7af7042013-10-12 22:38:11 -0500479 wl_list_init(&surface->views);
480
481 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400482 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500483
Pekka Paalanene67858b2013-04-25 13:57:42 +0300484 wl_list_init(&surface->subsurface_list);
485 wl_list_init(&surface->subsurface_list_pending);
486
Jason Ekstrand1e059042014-10-16 10:55:19 -0500487 weston_matrix_init(&surface->buffer_to_surface_matrix);
488 weston_matrix_init(&surface->surface_to_buffer_matrix);
489
Jonas Ådahld3414f22016-07-22 17:56:31 +0800490 wl_list_init(&surface->pointer_constraints);
491
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400492 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500493}
494
Alex Wu8811bf92012-02-28 18:07:54 +0800495WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500496weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200497 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500498{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100499 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500500}
501
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400502WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500503weston_view_to_global_float(struct weston_view *view,
504 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200505{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500506 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200507 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
508
Jason Ekstranda7af7042013-10-12 22:38:11 -0500509 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200510
511 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200512 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700513 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200514 v.f[3]);
515 *x = 0;
516 *y = 0;
517 return;
518 }
519
520 *x = v.f[0] / v.f[3];
521 *y = v.f[1] / v.f[3];
522 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500523 *x = sx + view->geometry.x;
524 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200525 }
526}
527
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500528WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200529weston_transformed_coord(int width, int height,
530 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200531 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200532 float sx, float sy, float *bx, float *by)
533{
534 switch (transform) {
535 case WL_OUTPUT_TRANSFORM_NORMAL:
536 default:
537 *bx = sx;
538 *by = sy;
539 break;
540 case WL_OUTPUT_TRANSFORM_FLIPPED:
541 *bx = width - sx;
542 *by = sy;
543 break;
544 case WL_OUTPUT_TRANSFORM_90:
545 *bx = height - sy;
546 *by = sx;
547 break;
548 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
549 *bx = height - sy;
550 *by = width - sx;
551 break;
552 case WL_OUTPUT_TRANSFORM_180:
553 *bx = width - sx;
554 *by = height - sy;
555 break;
556 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
557 *bx = sx;
558 *by = height - sy;
559 break;
560 case WL_OUTPUT_TRANSFORM_270:
561 *bx = sy;
562 *by = width - sx;
563 break;
564 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
565 *bx = sy;
566 *by = sx;
567 break;
568 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200569
570 *bx *= scale;
571 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200572}
573
574WL_EXPORT pixman_box32_t
575weston_transformed_rect(int width, int height,
576 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200577 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200578 pixman_box32_t rect)
579{
580 float x1, x2, y1, y2;
581
582 pixman_box32_t ret;
583
Alexander Larsson4ea95522013-05-22 14:41:37 +0200584 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200585 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200586 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200587 rect.x2, rect.y2, &x2, &y2);
588
589 if (x1 <= x2) {
590 ret.x1 = x1;
591 ret.x2 = x2;
592 } else {
593 ret.x1 = x2;
594 ret.x2 = x1;
595 }
596
597 if (y1 <= y2) {
598 ret.y1 = y1;
599 ret.y2 = y2;
600 } else {
601 ret.y1 = y2;
602 ret.y2 = y1;
603 }
604
605 return ret;
606}
607
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600608/** Transform a region by a matrix, restricted to axis-aligned transformations
609 *
610 * Warning: This function does not work for projective, affine, or matrices
611 * that encode arbitrary rotations. Only 90-degree step rotations are
612 * supported.
613 */
614WL_EXPORT void
615weston_matrix_transform_region(pixman_region32_t *dest,
616 struct weston_matrix *matrix,
617 pixman_region32_t *src)
618{
619 pixman_box32_t *src_rects, *dest_rects;
620 int nrects, i;
621
622 src_rects = pixman_region32_rectangles(src, &nrects);
623 dest_rects = malloc(nrects * sizeof(*dest_rects));
624 if (!dest_rects)
625 return;
626
627 for (i = 0; i < nrects; i++) {
628 struct weston_vector vec1 = {{
629 src_rects[i].x1, src_rects[i].y1, 0, 1
630 }};
631 weston_matrix_transform(matrix, &vec1);
632 vec1.f[0] /= vec1.f[3];
633 vec1.f[1] /= vec1.f[3];
634
635 struct weston_vector vec2 = {{
636 src_rects[i].x2, src_rects[i].y2, 0, 1
637 }};
638 weston_matrix_transform(matrix, &vec2);
639 vec2.f[0] /= vec2.f[3];
640 vec2.f[1] /= vec2.f[3];
641
642 if (vec1.f[0] < vec2.f[0]) {
643 dest_rects[i].x1 = floor(vec1.f[0]);
644 dest_rects[i].x2 = ceil(vec2.f[0]);
645 } else {
646 dest_rects[i].x1 = floor(vec2.f[0]);
647 dest_rects[i].x2 = ceil(vec1.f[0]);
648 }
649
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600650 if (vec1.f[1] < vec2.f[1]) {
651 dest_rects[i].y1 = floor(vec1.f[1]);
652 dest_rects[i].y2 = ceil(vec2.f[1]);
653 } else {
654 dest_rects[i].y1 = floor(vec2.f[1]);
655 dest_rects[i].y2 = ceil(vec1.f[1]);
656 }
657 }
658
659 pixman_region32_clear(dest);
660 pixman_region32_init_rects(dest, dest_rects, nrects);
661 free(dest_rects);
662}
663
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200664WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500665weston_transformed_region(int width, int height,
666 enum wl_output_transform transform,
667 int32_t scale,
668 pixman_region32_t *src, pixman_region32_t *dest)
669{
670 pixman_box32_t *src_rects, *dest_rects;
671 int nrects, i;
672
673 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
674 if (src != dest)
675 pixman_region32_copy(dest, src);
676 return;
677 }
678
679 src_rects = pixman_region32_rectangles(src, &nrects);
680 dest_rects = malloc(nrects * sizeof(*dest_rects));
681 if (!dest_rects)
682 return;
683
684 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
685 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
686 } else {
687 for (i = 0; i < nrects; i++) {
688 switch (transform) {
689 default:
690 case WL_OUTPUT_TRANSFORM_NORMAL:
691 dest_rects[i].x1 = src_rects[i].x1;
692 dest_rects[i].y1 = src_rects[i].y1;
693 dest_rects[i].x2 = src_rects[i].x2;
694 dest_rects[i].y2 = src_rects[i].y2;
695 break;
696 case WL_OUTPUT_TRANSFORM_90:
697 dest_rects[i].x1 = height - src_rects[i].y2;
698 dest_rects[i].y1 = src_rects[i].x1;
699 dest_rects[i].x2 = height - src_rects[i].y1;
700 dest_rects[i].y2 = src_rects[i].x2;
701 break;
702 case WL_OUTPUT_TRANSFORM_180:
703 dest_rects[i].x1 = width - src_rects[i].x2;
704 dest_rects[i].y1 = height - src_rects[i].y2;
705 dest_rects[i].x2 = width - src_rects[i].x1;
706 dest_rects[i].y2 = height - src_rects[i].y1;
707 break;
708 case WL_OUTPUT_TRANSFORM_270:
709 dest_rects[i].x1 = src_rects[i].y1;
710 dest_rects[i].y1 = width - src_rects[i].x2;
711 dest_rects[i].x2 = src_rects[i].y2;
712 dest_rects[i].y2 = width - src_rects[i].x1;
713 break;
714 case WL_OUTPUT_TRANSFORM_FLIPPED:
715 dest_rects[i].x1 = width - src_rects[i].x2;
716 dest_rects[i].y1 = src_rects[i].y1;
717 dest_rects[i].x2 = width - src_rects[i].x1;
718 dest_rects[i].y2 = src_rects[i].y2;
719 break;
720 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
721 dest_rects[i].x1 = height - src_rects[i].y2;
722 dest_rects[i].y1 = width - src_rects[i].x2;
723 dest_rects[i].x2 = height - src_rects[i].y1;
724 dest_rects[i].y2 = width - src_rects[i].x1;
725 break;
726 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
727 dest_rects[i].x1 = src_rects[i].x1;
728 dest_rects[i].y1 = height - src_rects[i].y2;
729 dest_rects[i].x2 = src_rects[i].x2;
730 dest_rects[i].y2 = height - src_rects[i].y1;
731 break;
732 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
733 dest_rects[i].x1 = src_rects[i].y1;
734 dest_rects[i].y1 = src_rects[i].x1;
735 dest_rects[i].x2 = src_rects[i].y2;
736 dest_rects[i].y2 = src_rects[i].x2;
737 break;
738 }
739 }
740 }
741
742 if (scale != 1) {
743 for (i = 0; i < nrects; i++) {
744 dest_rects[i].x1 *= scale;
745 dest_rects[i].x2 *= scale;
746 dest_rects[i].y1 *= scale;
747 dest_rects[i].y2 *= scale;
748 }
749 }
750
751 pixman_region32_clear(dest);
752 pixman_region32_init_rects(dest, dest_rects, nrects);
753 free(dest_rects);
754}
755
Jonny Lamb74130762013-11-26 18:19:46 +0100756static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300757viewport_surface_to_buffer(struct weston_surface *surface,
758 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100759{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200760 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200761 double src_width, src_height;
762 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200763
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200764 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
765 if (vp->surface.width == -1) {
766 *bx = sx;
767 *by = sy;
768 return;
769 }
Jonny Lamb74130762013-11-26 18:19:46 +0100770
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200771 src_x = 0.0;
772 src_y = 0.0;
773 src_width = surface->width_from_buffer;
774 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100775 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200776 src_x = wl_fixed_to_double(vp->buffer.src_x);
777 src_y = wl_fixed_to_double(vp->buffer.src_y);
778 src_width = wl_fixed_to_double(vp->buffer.src_width);
779 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100780 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200781
782 *bx = sx * src_width / surface->width + src_x;
783 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100784}
785
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500786WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200787weston_surface_to_buffer_float(struct weston_surface *surface,
788 float sx, float sy, float *bx, float *by)
789{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200790 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
791
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300792 /* first transform coordinates if the viewport is set */
793 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100794
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500795 weston_transformed_coord(surface->width_from_buffer,
796 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200797 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100798 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200799}
800
Derek Foreman99739672015-12-03 16:38:11 -0600801/** Transform a rectangle from surface coordinates to buffer coordinates
802 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300803 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600804 * from.
805 * \param rect The rectangle to transform.
806 * \return The transformed rectangle.
807 *
808 * Viewport and buffer transformations can only do translation, scaling,
809 * and rotations in 90-degree steps. Therefore the only loss in the
810 * conversion is coordinate rounding.
811 *
812 * However, some coordinate rounding takes place as an intermediate
813 * step before the buffer scale factor is applied, so the rectangle
814 * boundary may not be exactly as expected.
815 *
816 * This is OK for damage tracking since a little extra coverage is
817 * not a problem.
818 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200819WL_EXPORT pixman_box32_t
820weston_surface_to_buffer_rect(struct weston_surface *surface,
821 pixman_box32_t rect)
822{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200823 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100824 float xf, yf;
825
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300826 /* first transform box coordinates if the viewport is set */
827 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100828 rect.x1 = floorf(xf);
829 rect.y1 = floorf(yf);
830
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300831 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600832 rect.x2 = ceilf(xf);
833 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100834
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500835 return weston_transformed_rect(surface->width_from_buffer,
836 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200837 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200838 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200839}
840
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200841/** Transform a region from surface coordinates to buffer coordinates
842 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300843 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200844 * from.
845 * \param surface_region[in] The region in surface coordinates.
846 * \param buffer_region[out] The region converted to buffer coordinates.
847 *
848 * Buffer_region must be init'd, but will be completely overwritten.
849 *
850 * Viewport and buffer transformations can only do translation, scaling,
851 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600852 * conversion is from the coordinate rounding that takes place in
853 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200854 */
855WL_EXPORT void
856weston_surface_to_buffer_region(struct weston_surface *surface,
857 pixman_region32_t *surface_region,
858 pixman_region32_t *buffer_region)
859{
860 pixman_box32_t *src_rects, *dest_rects;
861 int nrects, i;
862
863 src_rects = pixman_region32_rectangles(surface_region, &nrects);
864 dest_rects = malloc(nrects * sizeof(*dest_rects));
865 if (!dest_rects)
866 return;
867
868 for (i = 0; i < nrects; i++) {
869 dest_rects[i] = weston_surface_to_buffer_rect(surface,
870 src_rects[i]);
871 }
872
873 pixman_region32_fini(buffer_region);
874 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
875 free(dest_rects);
876}
877
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200878WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400880 struct weston_plane *plane)
881{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500882 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400883 return;
884
Jason Ekstranda7af7042013-10-12 22:38:11 -0500885 weston_view_damage_below(view);
886 view->plane = plane;
887 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400888}
889
Pekka Paalanen51723d52015-02-17 13:10:01 +0200890/** Inflict damage on the plane where the view is visible.
891 *
892 * \param view The view that causes the damage.
893 *
894 * If the view is currently on a plane (including the primary plane),
895 * take the view's boundingbox, subtract all the opaque views that cover it,
896 * and add the remaining region as damage to the plane. This corresponds
897 * to the damage inflicted to the plane if this view disappeared.
898 *
899 * A repaint is scheduled for this view.
900 *
901 * The region of all opaque views covering this view is stored in
902 * weston_view::clip and updated by view_accumulate_damage() during
903 * weston_output_repaint(). Specifically, that region matches the
904 * scenegraph as it was last painted.
905 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400906WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500907weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200908{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500909 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200910
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500911 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200912 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500913 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800914 if (view->plane)
915 pixman_region32_union(&view->plane->damage,
916 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500917 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800918 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200919}
920
Pekka Paalanen01e00682017-03-24 16:21:06 +0200921/** Send wl_surface.enter/leave events
922 *
923 * \param surface The surface.
924 * \param output The entered/left output.
925 * \param enter True if entered.
926 * \param left True if left.
927 *
928 * Send the enter/leave events for all protocol objects bound to the given
929 * output by the client owning the surface.
930 */
931static void
932weston_surface_send_enter_leave(struct weston_surface *surface,
933 struct weston_output *output,
934 bool enter,
935 bool leave)
936{
937 struct wl_resource *wloutput;
938 struct wl_client *client;
939
940 assert(enter != leave);
941
942 client = wl_resource_get_client(surface->resource);
943 wl_resource_for_each(wloutput, &output->resource_list) {
944 if (wl_resource_get_client(wloutput) != client)
945 continue;
946
947 if (enter)
948 wl_surface_send_enter(surface->resource, wloutput);
949 if (leave)
950 wl_surface_send_leave(surface->resource, wloutput);
951 }
952}
953
Bryce Harrington3f650b82015-12-23 11:01:58 -0800954/**
955 * \param es The surface
956 * \param mask The new set of outputs for the surface
957 *
958 * Sets the surface's set of outputs to the ones specified by
959 * the new output mask provided. Identifies the outputs that
960 * have changed, the posts enter and leave events for these
961 * outputs as appropriate.
962 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400963static void
964weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
965{
966 uint32_t different = es->output_mask ^ mask;
967 uint32_t entered = mask & different;
968 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +0200969 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400970 struct weston_output *output;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400971
972 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500973 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400974 return;
975 if (different == 0)
976 return;
977
978 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +0200979 output_bit = 1u << output->id;
980 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400981 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +0200982
983 weston_surface_send_enter_leave(es, output,
984 output_bit & entered,
985 output_bit & left);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400986 }
987}
988
Bryce Harrington3f650b82015-12-23 11:01:58 -0800989/** Recalculate which output(s) the surface has views displayed on
990 *
991 * \param es The surface to remap to outputs
992 *
993 * Finds the output that is showing the largest amount of one
994 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300995 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800996 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300997 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800998 * in the output_mask for the surface.
999 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001000static void
1001weston_surface_assign_output(struct weston_surface *es)
1002{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 struct weston_output *new_output;
1004 struct weston_view *view;
1005 pixman_region32_t region;
1006 uint32_t max, area, mask;
1007 pixman_box32_t *e;
1008
1009 new_output = NULL;
1010 max = 0;
1011 mask = 0;
1012 pixman_region32_init(&region);
1013 wl_list_for_each(view, &es->views, surface_link) {
1014 if (!view->output)
1015 continue;
1016
1017 pixman_region32_intersect(&region, &view->transform.boundingbox,
1018 &view->output->region);
1019
1020 e = pixman_region32_extents(&region);
1021 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1022
1023 mask |= view->output_mask;
1024
1025 if (area >= max) {
1026 new_output = view->output;
1027 max = area;
1028 }
1029 }
1030 pixman_region32_fini(&region);
1031
1032 es->output = new_output;
1033 weston_surface_update_output_mask(es, mask);
1034}
1035
Bryce Harrington3f650b82015-12-23 11:01:58 -08001036/** Recalculate which output(s) the view is displayed on
1037 *
1038 * \param ev The view to remap to outputs
1039 *
1040 * Identifies the set of outputs that the view is visible on,
1041 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001042 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001043 *
1044 * Also does the same for the view's surface. See
1045 * weston_surface_assign_output().
1046 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001047static void
1048weston_view_assign_output(struct weston_view *ev)
1049{
1050 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001051 struct weston_output *output, *new_output;
1052 pixman_region32_t region;
1053 uint32_t max, area, mask;
1054 pixman_box32_t *e;
1055
1056 new_output = NULL;
1057 max = 0;
1058 mask = 0;
1059 pixman_region32_init(&region);
1060 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001061 if (output->destroying)
1062 continue;
1063
Jason Ekstranda7af7042013-10-12 22:38:11 -05001064 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001065 &output->region);
1066
1067 e = pixman_region32_extents(&region);
1068 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1069
1070 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001071 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001072
1073 if (area >= max) {
1074 new_output = output;
1075 max = area;
1076 }
1077 }
1078 pixman_region32_fini(&region);
1079
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 ev->output = new_output;
1081 ev->output_mask = mask;
1082
1083 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001084}
1085
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001086static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001087weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1088 int from_x, int from_y, int *to_x, int *to_y)
1089{
1090 float x, y;
1091
1092 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1093 weston_view_from_global_float(to, x, y, &x, &y);
1094
1095 *to_x = round(x);
1096 *to_y = round(y);
1097}
1098
1099static void
1100weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1101{
1102 pixman_box32_t *a;
1103 pixman_box32_t b;
1104
1105 a = pixman_region32_extents(&from->geometry.scissor);
1106
1107 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1108 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1109
1110 pixman_region32_fini(&to->geometry.scissor);
1111 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1112}
1113
1114static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001115view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001117{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001118 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1119 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001120 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001121 { inbox->x1, inbox->y1 },
1122 { inbox->x1, inbox->y2 },
1123 { inbox->x2, inbox->y1 },
1124 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001125 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001126 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001127 int i;
1128
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001129 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001130 /* avoid rounding empty bbox to 1x1 */
1131 pixman_region32_init(bbox);
1132 return;
1133 }
1134
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001135 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001136 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001138 if (x < min_x)
1139 min_x = x;
1140 if (x > max_x)
1141 max_x = x;
1142 if (y < min_y)
1143 min_y = y;
1144 if (y > max_y)
1145 max_y = y;
1146 }
1147
Pekka Paalanen219b9822012-02-08 15:38:37 +02001148 int_x = floorf(min_x);
1149 int_y = floorf(min_y);
1150 pixman_region32_init_rect(bbox, int_x, int_y,
1151 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001152}
1153
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001154static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001156{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001158
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001159 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160 view->geometry.x = roundf(view->geometry.x);
1161 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001162
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001163 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1165 view->transform.position.matrix.d[12] = view->geometry.x;
1166 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001167
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 view->transform.inverse = view->transform.position.matrix;
1171 view->transform.inverse.d[12] = -view->geometry.x;
1172 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001173
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001175 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001176 view->surface->width,
1177 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001178 if (view->geometry.scissor_enabled)
1179 pixman_region32_intersect(&view->transform.boundingbox,
1180 &view->transform.boundingbox,
1181 &view->geometry.scissor);
1182
1183 pixman_region32_translate(&view->transform.boundingbox,
1184 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001185
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 if (view->alpha == 1.0) {
1187 pixman_region32_copy(&view->transform.opaque,
1188 &view->surface->opaque);
1189 pixman_region32_translate(&view->transform.opaque,
1190 view->geometry.x,
1191 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001192 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001193}
1194
1195static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001197{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198 struct weston_view *parent = view->geometry.parent;
1199 struct weston_matrix *matrix = &view->transform.matrix;
1200 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001201 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001202 pixman_region32_t surfregion;
1203 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001204
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001206
1207 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1209 view->transform.position.matrix.d[12] = view->geometry.x;
1210 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001211
1212 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001214 weston_matrix_multiply(matrix, &tform->matrix);
1215
Pekka Paalanen483243f2013-03-08 14:56:50 +02001216 if (parent)
1217 weston_matrix_multiply(matrix, &parent->transform.matrix);
1218
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001219 if (weston_matrix_invert(inverse, matrix) < 0) {
1220 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 weston_log("error: weston_view %p"
1222 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001223 return -1;
1224 }
1225
Giulio Camuffo148c1992016-09-04 18:50:46 +03001226 if (view->alpha == 1.0 &&
1227 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1228 pixman_region32_copy(&view->transform.opaque,
1229 &view->surface->opaque);
1230 pixman_region32_translate(&view->transform.opaque,
1231 matrix->d[12],
1232 matrix->d[13]);
1233 }
1234
Pekka Paalanen380adf52015-02-16 14:39:11 +02001235 pixman_region32_init_rect(&surfregion, 0, 0,
1236 view->surface->width, view->surface->height);
1237 if (view->geometry.scissor_enabled)
1238 pixman_region32_intersect(&surfregion, &surfregion,
1239 &view->geometry.scissor);
1240 surfbox = pixman_region32_extents(&surfregion);
1241
1242 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1243 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001244
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001245 return 0;
1246}
1247
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001248static struct weston_layer *
1249get_view_layer(struct weston_view *view)
1250{
1251 if (view->parent_view)
1252 return get_view_layer(view->parent_view);
1253 return view->layer_link.layer;
1254}
1255
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001256WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001258{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001260 struct weston_layer *layer;
1261 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001262
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001264 return;
1265
Pekka Paalanen483243f2013-03-08 14:56:50 +02001266 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001268
Jason Ekstranda7af7042013-10-12 22:38:11 -05001269 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001270
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001272
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 pixman_region32_fini(&view->transform.boundingbox);
1274 pixman_region32_fini(&view->transform.opaque);
1275 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001276
Pekka Paalanencd403622012-01-25 13:37:39 +02001277 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 if (view->geometry.transformation_list.next ==
1279 &view->transform.position.link &&
1280 view->geometry.transformation_list.prev ==
1281 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001282 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001284 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 if (weston_view_update_transform_enable(view) < 0)
1286 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001287 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001288
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001289 layer = get_view_layer(view);
1290 if (layer) {
1291 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001292 pixman_region32_intersect(&view->transform.boundingbox,
1293 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001294 pixman_region32_intersect(&view->transform.opaque,
1295 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001296 pixman_region32_fini(&mask);
1297 }
1298
Pekka Paalanen380adf52015-02-16 14:39:11 +02001299 if (parent) {
1300 if (parent->geometry.scissor_enabled) {
1301 view->geometry.scissor_enabled = true;
1302 weston_view_transfer_scissor(parent, view);
1303 } else {
1304 view->geometry.scissor_enabled = false;
1305 }
1306 }
1307
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001309
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001311
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 wl_signal_emit(&view->surface->compositor->transform_signal,
1313 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001314}
1315
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001316WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001318{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001320
1321 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 * The invariant: if view->geometry.dirty, then all views
1323 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001324 * Corollary: if not parent->geometry.dirty, then all ancestors
1325 * are not dirty.
1326 */
1327
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001329 return;
1330
Jason Ekstranda7af7042013-10-12 22:38:11 -05001331 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001332
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001334 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001336}
1337
1338WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339weston_view_to_global_fixed(struct weston_view *view,
1340 wl_fixed_t vx, wl_fixed_t vy,
1341 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001342{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001343 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001344
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 weston_view_to_global_float(view,
1346 wl_fixed_to_double(vx),
1347 wl_fixed_to_double(vy),
1348 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001349 *x = wl_fixed_from_double(xf);
1350 *y = wl_fixed_from_double(yf);
1351}
1352
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001353WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354weston_view_from_global_float(struct weston_view *view,
1355 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001356{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001358 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001361
1362 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001363 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001365 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 *vx = 0;
1367 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001368 return;
1369 }
1370
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371 *vx = v.f[0] / v.f[3];
1372 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001373 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374 *vx = x - view->geometry.x;
1375 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001376 }
1377}
1378
1379WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380weston_view_from_global_fixed(struct weston_view *view,
1381 wl_fixed_t x, wl_fixed_t y,
1382 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001383{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001385
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 weston_view_from_global_float(view,
1387 wl_fixed_to_double(x),
1388 wl_fixed_to_double(y),
1389 &vxf, &vyf);
1390 *vx = wl_fixed_from_double(vxf);
1391 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001392}
1393
1394WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395weston_view_from_global(struct weston_view *view,
1396 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001397{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001399
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1401 *vx = floorf(vxf);
1402 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001403}
1404
Bryce Harrington3f650b82015-12-23 11:01:58 -08001405/**
1406 * \param surface The surface to be repainted
1407 *
1408 * Marks the output(s) that the surface is shown on as needing to be
1409 * repainted. See weston_output_schedule_repaint().
1410 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001411WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001412weston_surface_schedule_repaint(struct weston_surface *surface)
1413{
1414 struct weston_output *output;
1415
1416 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001417 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001418 weston_output_schedule_repaint(output);
1419}
1420
Bryce Harrington3f650b82015-12-23 11:01:58 -08001421/**
1422 * \param view The view to be repainted
1423 *
1424 * Marks the output(s) that the view is shown on as needing to be
1425 * repainted. See weston_output_schedule_repaint().
1426 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001427WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428weston_view_schedule_repaint(struct weston_view *view)
1429{
1430 struct weston_output *output;
1431
1432 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001433 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 weston_output_schedule_repaint(output);
1435}
1436
Pekka Paalanene508ce62015-02-19 13:59:55 +02001437/**
1438 * XXX: This function does it the wrong way.
1439 * surface->damage is the damage from the client, and causes
1440 * surface_flush_damage() to copy pixels. No window management action can
1441 * cause damage to the client-provided content, warranting re-upload!
1442 *
1443 * Instead of surface->damage, this function should record the damage
1444 * with all the views for this surface to avoid extraneous texture
1445 * uploads.
1446 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001448weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001449{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001450 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 0, 0, surface->width,
1452 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001453
Kristian Høgsberg98238702012-08-03 16:29:12 -04001454 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001455}
1456
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001457WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001459{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001460 if (view->geometry.x == x && view->geometry.y == y)
1461 return;
1462
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 view->geometry.x = x;
1464 view->geometry.y = y;
1465 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001466}
1467
Pekka Paalanen483243f2013-03-08 14:56:50 +02001468static void
1469transform_parent_handle_parent_destroy(struct wl_listener *listener,
1470 void *data)
1471{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 struct weston_view *view =
1473 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001474 geometry.parent_destroy_listener);
1475
Jason Ekstranda7af7042013-10-12 22:38:11 -05001476 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001477}
1478
1479WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001481 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001482{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 if (view->geometry.parent) {
1484 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1485 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001486
1487 if (!parent)
1488 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001489 }
1490
Jason Ekstranda7af7042013-10-12 22:38:11 -05001491 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001492
Jason Ekstranda7af7042013-10-12 22:38:11 -05001493 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001494 transform_parent_handle_parent_destroy;
1495 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001496 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001498 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001499 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001500 }
1501
Jason Ekstranda7af7042013-10-12 22:38:11 -05001502 weston_view_geometry_dirty(view);
1503}
1504
Pekka Paalanen380adf52015-02-16 14:39:11 +02001505/** Set a clip mask rectangle on a view
1506 *
1507 * \param view The view to set the clip mask on.
1508 * \param x Top-left corner X coordinate of the clip rectangle.
1509 * \param y Top-left corner Y coordinate of the clip rectangle.
1510 * \param width Width of the clip rectangle, non-negative.
1511 * \param height Height of the clip rectangle, non-negative.
1512 *
1513 * A shell may set a clip mask rectangle on a view. Everything outside
1514 * the rectangle is cut away for input and output purposes: it is
1515 * not drawn and cannot be hit by hit-test based input like pointer
1516 * motion or touch-downs. Everything inside the rectangle will behave
1517 * normally. Clients are unaware of clipping.
1518 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001519 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001520 * mask rectangle does not affect the view position, the view is positioned
1521 * as it would be without a clip. The clip also does not change
1522 * weston_surface::width,height.
1523 *
1524 * The clip mask rectangle is part of transformation inheritance
1525 * (weston_view_set_transform_parent()). A clip set in the root of the
1526 * transformation inheritance tree will affect all views in the tree.
1527 * A clip can be set only on the root view. Attempting to set a clip
1528 * on view that has a transformation parent will fail. Assigning a parent
1529 * to a view that has a clip set will cause the clip to be forgotten.
1530 *
1531 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1532 * on the additional transformations in the child views. These transformations
1533 * may not rotate the coordinate axes, i.e., only translation and scaling
1534 * are allowed. Violating this restriction causes the clipping to malfunction.
1535 * Furthermore, using scaling may cause rounding errors in child clipping.
1536 *
1537 * The clip mask rectangle is not automatically adjusted based on
1538 * wl_surface.attach dx and dy arguments.
1539 *
1540 * A clip mask rectangle can be set only if the compositor capability
1541 * WESTON_CAP_VIEW_CLIP_MASK is present.
1542 *
1543 * This function sets the clip mask rectangle and schedules a repaint for
1544 * the view.
1545 */
1546WL_EXPORT void
1547weston_view_set_mask(struct weston_view *view,
1548 int x, int y, int width, int height)
1549{
1550 struct weston_compositor *compositor = view->surface->compositor;
1551
1552 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1553 weston_log("%s not allowed without capability!\n", __func__);
1554 return;
1555 }
1556
1557 if (view->geometry.parent) {
1558 weston_log("view %p has a parent, clip forbidden!\n", view);
1559 return;
1560 }
1561
1562 if (width < 0 || height < 0) {
1563 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1564 x, y, width, height);
1565 return;
1566 }
1567
1568 pixman_region32_fini(&view->geometry.scissor);
1569 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1570 view->geometry.scissor_enabled = true;
1571 weston_view_geometry_dirty(view);
1572 weston_view_schedule_repaint(view);
1573}
1574
1575/** Remove the clip mask from a view
1576 *
1577 * \param view The view to remove the clip mask from.
1578 *
1579 * Removed the clip mask rectangle and schedules a repaint.
1580 *
1581 * \sa weston_view_set_mask
1582 */
1583WL_EXPORT void
1584weston_view_set_mask_infinite(struct weston_view *view)
1585{
1586 view->geometry.scissor_enabled = false;
1587 weston_view_geometry_dirty(view);
1588 weston_view_schedule_repaint(view);
1589}
1590
Armin Krezović0da12b82016-06-30 06:04:33 +02001591/* Check if view should be displayed
1592 *
1593 * The indicator is set manually when assigning
1594 * a view to a surface.
1595 *
1596 * This needs reworking. See the thread starting at:
1597 *
1598 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1599 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001600WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001601weston_view_is_mapped(struct weston_view *view)
1602{
Armin Krezović0da12b82016-06-30 06:04:33 +02001603 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001604}
1605
Armin Krezović0da12b82016-06-30 06:04:33 +02001606/* Check if a surface has a view assigned to it
1607 *
1608 * The indicator is set manually when mapping
1609 * a surface and creating a view for it.
1610 *
1611 * This needs to go. See the thread starting at:
1612 *
1613 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1614 *
1615 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001616WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001617weston_surface_is_mapped(struct weston_surface *surface)
1618{
Armin Krezović0da12b82016-06-30 06:04:33 +02001619 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001620}
1621
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001622static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001623surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001624{
1625 struct weston_view *view;
1626
1627 if (surface->width == width && surface->height == height)
1628 return;
1629
1630 surface->width = width;
1631 surface->height = height;
1632
1633 wl_list_for_each(view, &surface->views, surface_link)
1634 weston_view_geometry_dirty(view);
1635}
1636
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001637WL_EXPORT void
1638weston_surface_set_size(struct weston_surface *surface,
1639 int32_t width, int32_t height)
1640{
1641 assert(!surface->resource);
1642 surface_set_size(surface, width, height);
1643}
1644
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001645static int
1646fixed_round_up_to_int(wl_fixed_t f)
1647{
1648 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1649}
1650
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001651static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001652convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1653 int32_t width, int32_t height,
1654 uint32_t transform,
1655 int32_t scale)
1656{
1657 assert(scale > 0);
1658
1659 switch (transform) {
1660 case WL_OUTPUT_TRANSFORM_NORMAL:
1661 case WL_OUTPUT_TRANSFORM_180:
1662 case WL_OUTPUT_TRANSFORM_FLIPPED:
1663 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1664 *width_out = width / scale;
1665 *height_out = height / scale;
1666 break;
1667 case WL_OUTPUT_TRANSFORM_90:
1668 case WL_OUTPUT_TRANSFORM_270:
1669 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1670 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1671 *width_out = height / scale;
1672 *height_out = width / scale;
1673 break;
1674 default:
1675 assert(0 && "invalid transform");
1676 }
1677}
1678
1679static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001680weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001681{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001682 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001683
1684 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001685 surface->width_from_buffer = 0;
1686 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001687 return;
1688 }
1689
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001690 convert_size_by_transform_scale(&surface->width_from_buffer,
1691 &surface->height_from_buffer,
1692 surface->buffer_ref.buffer->width,
1693 surface->buffer_ref.buffer->height,
1694 vp->buffer.transform,
1695 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001696}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001697
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001698static void
1699weston_surface_update_size(struct weston_surface *surface)
1700{
1701 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1702 int32_t width, height;
1703
1704 width = surface->width_from_buffer;
1705 height = surface->height_from_buffer;
1706
1707 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001708 surface_set_size(surface,
1709 vp->surface.width, vp->surface.height);
1710 return;
1711 }
1712
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001713 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001714 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1715 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1716
1717 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001718 return;
1719 }
1720
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001721 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001722}
1723
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001724WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001725weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001726{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001727 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001728
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001729 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001730
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001731 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001732}
1733
Jason Ekstranda7af7042013-10-12 22:38:11 -05001734WL_EXPORT struct weston_view *
1735weston_compositor_pick_view(struct weston_compositor *compositor,
1736 wl_fixed_t x, wl_fixed_t y,
1737 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001738{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001739 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001740 wl_fixed_t view_x, view_y;
1741 int view_ix, view_iy;
1742 int ix = wl_fixed_to_int(x);
1743 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001744
Jason Ekstranda7af7042013-10-12 22:38:11 -05001745 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001746 if (!pixman_region32_contains_point(
1747 &view->transform.boundingbox, ix, iy, NULL))
1748 continue;
1749
1750 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1751 view_ix = wl_fixed_to_int(view_x);
1752 view_iy = wl_fixed_to_int(view_y);
1753
1754 if (!pixman_region32_contains_point(&view->surface->input,
1755 view_ix, view_iy, NULL))
1756 continue;
1757
Pekka Paalanen380adf52015-02-16 14:39:11 +02001758 if (view->geometry.scissor_enabled &&
1759 !pixman_region32_contains_point(&view->geometry.scissor,
1760 view_ix, view_iy, NULL))
1761 continue;
1762
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001763 *vx = view_x;
1764 *vy = view_y;
1765 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001766 }
1767
Derek Foremanf9318d12015-05-11 15:40:11 -05001768 *vx = wl_fixed_from_int(-1000000);
1769 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001770 return NULL;
1771}
1772
1773static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001774weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001775{
Daniel Stone37816df2012-05-16 18:45:18 +01001776 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001777
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001778 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001779 return;
1780
Daniel Stone37816df2012-05-16 18:45:18 +01001781 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001782 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001783}
1784
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001785WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001787{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001788 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001789
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790 if (!weston_view_is_mapped(view))
1791 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001792
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 weston_view_damage_below(view);
1794 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001795 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001796 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001797 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001798 wl_list_remove(&view->link);
1799 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001800 view->output_mask = 0;
1801 weston_surface_assign_output(view->surface);
1802
1803 if (weston_surface_is_mapped(view->surface))
1804 return;
1805
1806 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001807 struct weston_touch *touch = weston_seat_get_touch(seat);
1808 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1809 struct weston_keyboard *keyboard =
1810 weston_seat_get_keyboard(seat);
1811
1812 if (keyboard && keyboard->focus == view->surface)
1813 weston_keyboard_set_focus(keyboard, NULL);
1814 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001815 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001816 if (touch && touch->focus == view)
1817 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001818 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001819}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821WL_EXPORT void
1822weston_surface_unmap(struct weston_surface *surface)
1823{
1824 struct weston_view *view;
1825
Armin Krezovićf8486c32016-06-30 06:04:28 +02001826 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001827 wl_list_for_each(view, &surface->views, surface_link)
1828 weston_view_unmap(view);
1829 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001830}
1831
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001832static void
1833weston_surface_reset_pending_buffer(struct weston_surface *surface)
1834{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001835 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001836 surface->pending.sx = 0;
1837 surface->pending.sy = 0;
1838 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001839 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001840}
1841
Jason Ekstranda7af7042013-10-12 22:38:11 -05001842WL_EXPORT void
1843weston_view_destroy(struct weston_view *view)
1844{
1845 wl_signal_emit(&view->destroy_signal, view);
1846
1847 assert(wl_list_empty(&view->geometry.child_list));
1848
1849 if (weston_view_is_mapped(view)) {
1850 weston_view_unmap(view);
1851 weston_compositor_build_view_list(view->surface->compositor);
1852 }
1853
1854 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001855 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001856
1857 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001858 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001859 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001860 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001861
1862 weston_view_set_transform_parent(view, NULL);
1863
Jason Ekstranda7af7042013-10-12 22:38:11 -05001864 wl_list_remove(&view->surface_link);
1865
1866 free(view);
1867}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001868
1869WL_EXPORT void
1870weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001871{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001872 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001873 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001874 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001875
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001876 if (--surface->ref_count > 0)
1877 return;
1878
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001879 assert(surface->resource == NULL);
1880
Pekka Paalanenca790762015-04-17 14:23:38 +03001881 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001882
Pekka Paalanene67858b2013-04-25 13:57:42 +03001883 assert(wl_list_empty(&surface->subsurface_list_pending));
1884 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001885
Jason Ekstranda7af7042013-10-12 22:38:11 -05001886 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1887 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001888
Jason Ekstrand7b982072014-05-20 14:33:03 -05001889 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001890
Pekka Paalanende685b82012-12-04 15:58:12 +02001891 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001892
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001893 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001894 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001895 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001896
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001897 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001898 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001899
Pekka Paalanen133e4392014-09-23 22:08:46 -04001900 weston_presentation_feedback_discard_list(&surface->feedback_list);
1901
Jonas Ådahld3414f22016-07-22 17:56:31 +08001902 wl_list_for_each_safe(constraint, next_constraint,
1903 &surface->pointer_constraints,
1904 link)
1905 weston_pointer_constraint_destroy(constraint);
1906
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001907 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001908}
1909
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001910static void
1911destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001912{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001913 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001914
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001915 assert(surface);
1916
Giulio Camuffo0d379742013-11-15 22:06:15 +01001917 /* Set the resource to NULL, since we don't want to leave a
1918 * dangling pointer if the surface was refcounted and survives
1919 * the weston_surface_destroy() call. */
1920 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001921
1922 if (surface->viewport_resource)
1923 wl_resource_set_user_data(surface->viewport_resource, NULL);
1924
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001925 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001926}
1927
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001928static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001929weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1930{
1931 struct weston_buffer *buffer =
1932 container_of(listener, struct weston_buffer, destroy_listener);
1933
1934 wl_signal_emit(&buffer->destroy_signal, buffer);
1935 free(buffer);
1936}
1937
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001938WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001939weston_buffer_from_resource(struct wl_resource *resource)
1940{
1941 struct weston_buffer *buffer;
1942 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001943
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001944 listener = wl_resource_get_destroy_listener(resource,
1945 weston_buffer_destroy_handler);
1946
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001947 if (listener)
1948 return container_of(listener, struct weston_buffer,
1949 destroy_listener);
1950
1951 buffer = zalloc(sizeof *buffer);
1952 if (buffer == NULL)
1953 return NULL;
1954
1955 buffer->resource = resource;
1956 wl_signal_init(&buffer->destroy_signal);
1957 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001958 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001959 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001960
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001961 return buffer;
1962}
1963
1964static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001965weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1966 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001967{
Pekka Paalanende685b82012-12-04 15:58:12 +02001968 struct weston_buffer_reference *ref =
1969 container_of(listener, struct weston_buffer_reference,
1970 destroy_listener);
1971
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001972 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001973 ref->buffer = NULL;
1974}
1975
1976WL_EXPORT void
1977weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001978 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001979{
1980 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001981 ref->buffer->busy_count--;
1982 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001983 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05001984 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05001985 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001986 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001987 }
1988
Pekka Paalanende685b82012-12-04 15:58:12 +02001989 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001990 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001991 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001992 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001993 }
1994
Pekka Paalanende685b82012-12-04 15:58:12 +02001995 ref->buffer = buffer;
1996 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1997}
1998
1999static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002000weston_surface_attach(struct weston_surface *surface,
2001 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002002{
2003 weston_buffer_reference(&surface->buffer_ref, buffer);
2004
Pekka Paalanena6421c42012-12-04 15:58:10 +02002005 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002006 if (weston_surface_is_mapped(surface))
2007 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002008 }
2009
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002010 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002011
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002012 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002013 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002014}
2015
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002016WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002017weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002018{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002019 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002020
2021 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002022 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002023}
2024
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002025WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002026weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002027{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002028 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002029
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002030 pixman_region32_union(&compositor->primary_plane.damage,
2031 &compositor->primary_plane.damage,
2032 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002033 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002034}
2035
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002036static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002037surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002038{
Pekka Paalanende685b82012-12-04 15:58:12 +02002039 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002040 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002041 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002042
Pekka Paalanenb5026542014-11-12 15:09:24 +02002043 if (weston_timeline_enabled_ &&
2044 pixman_region32_not_empty(&surface->damage))
2045 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2046 TLP_OUTPUT(surface->output), TLP_END);
2047
Jason Ekstrandef540082014-06-26 10:37:36 -07002048 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049}
2050
2051static void
2052view_accumulate_damage(struct weston_view *view,
2053 pixman_region32_t *opaque)
2054{
2055 pixman_region32_t damage;
2056
2057 pixman_region32_init(&damage);
2058 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002059 pixman_box32_t *extents;
2060
Jason Ekstranda7af7042013-10-12 22:38:11 -05002061 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002062 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002063 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002064 pixman_region32_copy(&damage, &view->surface->damage);
2065 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002066 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002067 }
2068
Pekka Paalanen380adf52015-02-16 14:39:11 +02002069 pixman_region32_intersect(&damage, &damage,
2070 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002071 pixman_region32_subtract(&damage, &damage, opaque);
2072 pixman_region32_union(&view->plane->damage,
2073 &view->plane->damage, &damage);
2074 pixman_region32_fini(&damage);
2075 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002076 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002077}
2078
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002079static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002080compositor_accumulate_damage(struct weston_compositor *ec)
2081{
2082 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002084 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002085
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002086 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002087
2088 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002089 pixman_region32_copy(&plane->clip, &clip);
2090
2091 pixman_region32_init(&opaque);
2092
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093 wl_list_for_each(ev, &ec->view_list, link) {
2094 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002095 continue;
2096
Jason Ekstranda7af7042013-10-12 22:38:11 -05002097 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002098 }
2099
2100 pixman_region32_union(&clip, &clip, &opaque);
2101 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002102 }
2103
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002104 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002105
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002107 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002108
2109 wl_list_for_each(ev, &ec->view_list, link) {
2110 if (ev->surface->touched)
2111 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002112 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002113
2114 surface_flush_damage(ev->surface);
2115
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002116 /* Both the renderer and the backend have seen the buffer
2117 * by now. If renderer needs the buffer, it has its own
2118 * reference set. If the backend wants to keep the buffer
2119 * around for migrating the surface into a non-primary plane
2120 * later, keep_buffer is true. Otherwise, drop the core
2121 * reference now, and allow early buffer release. This enables
2122 * clients to use single-buffering.
2123 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124 if (!ev->surface->keep_buffer)
2125 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002126 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002127}
2128
2129static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002130surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002131{
2132 struct weston_subsurface *sub;
2133
Pekka Paalanene67858b2013-04-25 13:57:42 +03002134 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002135 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002136 continue;
2137
Jason Ekstranda7af7042013-10-12 22:38:11 -05002138 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2139 wl_list_init(&sub->surface->views);
2140
2141 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002142 }
2143}
2144
2145static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002146surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002147{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002148 struct weston_subsurface *sub;
2149 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002150
Jason Ekstranda7af7042013-10-12 22:38:11 -05002151 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2152 if (sub->surface == surface)
2153 continue;
2154
George Kiagiadakised04d382014-06-13 18:10:26 +02002155 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2156 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002157 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002158 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002159
2160 surface_free_unused_subsurface_views(sub->surface);
2161 }
2162}
2163
2164static void
2165view_list_add_subsurface_view(struct weston_compositor *compositor,
2166 struct weston_subsurface *sub,
2167 struct weston_view *parent)
2168{
2169 struct weston_subsurface *child;
2170 struct weston_view *view = NULL, *iv;
2171
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002172 if (!weston_surface_is_mapped(sub->surface))
2173 return;
2174
Jason Ekstranda7af7042013-10-12 22:38:11 -05002175 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2176 if (iv->geometry.parent == parent) {
2177 view = iv;
2178 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002179 }
2180 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181
2182 if (view) {
2183 /* Put it back in the surface's list of views */
2184 wl_list_remove(&view->surface_link);
2185 wl_list_insert(&sub->surface->views, &view->surface_link);
2186 } else {
2187 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002188 weston_view_set_position(view,
2189 sub->position.x,
2190 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191 weston_view_set_transform_parent(view, parent);
2192 }
2193
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002194 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002195 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002196 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002197
Pekka Paalanenb188e912013-11-19 14:03:35 +02002198 if (wl_list_empty(&sub->surface->subsurface_list)) {
2199 wl_list_insert(compositor->view_list.prev, &view->link);
2200 return;
2201 }
2202
2203 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2204 if (child->surface == sub->surface)
2205 wl_list_insert(compositor->view_list.prev, &view->link);
2206 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002207 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002208 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002209}
2210
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002211/* This recursively adds the sub-surfaces for a view, relying on the
2212 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2213 * change first happens to the sub-surface list, and then automatically
2214 * propagates here. See weston_surface_damage_subsurfaces() for how the
2215 * sub-surfaces receive damage when the client changes the state.
2216 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002217static void
2218view_list_add(struct weston_compositor *compositor,
2219 struct weston_view *view)
2220{
2221 struct weston_subsurface *sub;
2222
2223 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002224
Pekka Paalanenb188e912013-11-19 14:03:35 +02002225 if (wl_list_empty(&view->surface->subsurface_list)) {
2226 wl_list_insert(compositor->view_list.prev, &view->link);
2227 return;
2228 }
2229
2230 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2231 if (sub->surface == view->surface)
2232 wl_list_insert(compositor->view_list.prev, &view->link);
2233 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002234 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002235 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002236}
2237
2238static void
2239weston_compositor_build_view_list(struct weston_compositor *compositor)
2240{
2241 struct weston_view *view;
2242 struct weston_layer *layer;
2243
2244 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002245 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002246 surface_stash_subsurface_views(view->surface);
2247
2248 wl_list_init(&compositor->view_list);
2249 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002250 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002251 view_list_add(compositor, view);
2252 }
2253 }
2254
2255 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002256 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002257 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002258}
2259
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002260static void
2261weston_output_take_feedback_list(struct weston_output *output,
2262 struct weston_surface *surface)
2263{
2264 struct weston_view *view;
2265 struct weston_presentation_feedback *feedback;
2266 uint32_t flags = 0xffffffff;
2267
2268 if (wl_list_empty(&surface->feedback_list))
2269 return;
2270
2271 /* All views must have the flag for the flag to survive. */
2272 wl_list_for_each(view, &surface->views, surface_link) {
2273 /* ignore views that are not on this output at all */
2274 if (view->output_mask & (1u << output->id))
2275 flags &= view->psf_flags;
2276 }
2277
2278 wl_list_for_each(feedback, &surface->feedback_list, link)
2279 feedback->psf_flags = flags;
2280
2281 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2282 wl_list_init(&surface->feedback_list);
2283}
2284
David Herrmann1edf44c2013-10-22 17:11:26 +02002285static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002286weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002287{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002288 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002289 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002290 struct weston_animation *animation, *next;
2291 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002292 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002293 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002294 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002295
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002296 if (output->destroying)
2297 return 0;
2298
Pekka Paalanenb5026542014-11-12 15:09:24 +02002299 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2300
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002301 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002302 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002303
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002304 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002305 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002306 } else {
2307 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002308 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002309 ev->psf_flags = 0;
2310 }
2311 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002312
Pekka Paalanene67858b2013-04-25 13:57:42 +03002313 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002314 wl_list_for_each(ev, &ec->view_list, link) {
2315 /* Note: This operation is safe to do multiple times on the
2316 * same surface.
2317 */
2318 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002319 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002320 &ev->surface->frame_callback_list);
2321 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002322
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002323 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002324 }
2325 }
2326
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002327 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002328
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002329 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002330 pixman_region32_intersect(&output_damage,
2331 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002332 pixman_region32_subtract(&output_damage,
2333 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002334
Scott Moreauccbf29d2012-02-22 14:21:41 -07002335 if (output->dirty)
2336 weston_output_update_matrix(output);
2337
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002338 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002339
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002340 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002341
Daniel Stone09a97e22017-03-01 11:34:06 +00002342 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002343 if (r == 0)
2344 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002345
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002346 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002347
Jonas Ådahldb773762012-06-13 00:01:21 +02002348 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002349 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002350 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002351 }
2352
Scott Moreaud64cf212012-06-08 19:40:54 -06002353 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002354 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002355 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002356 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002357
Pekka Paalanenb5026542014-11-12 15:09:24 +02002358 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2359
David Herrmann1edf44c2013-10-22 17:11:26 +02002360 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002361}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002362
Pekka Paalanen82919792014-05-21 13:51:49 +03002363static void
2364weston_output_schedule_repaint_reset(struct weston_output *output)
2365{
Daniel Stone05df8c12017-03-03 16:59:42 +00002366 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002367 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002368}
2369
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002370static int
2371weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2372 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002373{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002374 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002375 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002376 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002377
Daniel Stone6847b852017-03-01 11:34:08 +00002378 /* We're not ready yet; come back to make a decision later. */
2379 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002380 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002381
2382 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2383 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002384 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002385
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002386 /* If we're sleeping, drop the repaint machinery entirely; we will
2387 * explicitly repaint all outputs when we come back. */
2388 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2389 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2390 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002391
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002392 /* We don't actually need to repaint this output; drop it from
2393 * repaint until something causes damage. */
2394 if (!output->repaint_needed)
2395 goto err;
2396
2397 /* If repaint fails, we aren't going to get weston_output_finish_frame
2398 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002399 * something schedules a successful repaint later. As repainting may
2400 * take some time, re-read our clock as a courtesy to the next
2401 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002402 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002403 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002404 if (ret != 0)
2405 goto err;
2406
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002407 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002408
2409err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002410 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002411 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002412}
2413
2414static void
2415output_repaint_timer_arm(struct weston_compositor *compositor)
2416{
2417 struct weston_output *output;
2418 bool any_should_repaint = false;
2419 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002420 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002421
2422 weston_compositor_read_presentation_clock(compositor, &now);
2423
2424 wl_list_for_each(output, &compositor->output_list, link) {
2425 int64_t msec_to_this;
2426
2427 if (output->repaint_status != REPAINT_SCHEDULED)
2428 continue;
2429
2430 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2431 &now);
2432 if (!any_should_repaint || msec_to_this < msec_to_next)
2433 msec_to_next = msec_to_this;
2434
2435 any_should_repaint = true;
2436 }
2437
2438 if (!any_should_repaint)
2439 return;
2440
2441 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2442 * This is a workaround to allow coalescing multiple output repaints
2443 * particularly from weston_output_finish_frame()
2444 * into the same call, which would not happen if we called
2445 * output_repaint_timer_handler() directly.
2446 */
2447 if (msec_to_next < 1)
2448 msec_to_next = 1;
2449
2450 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2451}
2452
2453static int
2454output_repaint_timer_handler(void *data)
2455{
2456 struct weston_compositor *compositor = data;
2457 struct weston_output *output;
2458 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002459 void *repaint_data = NULL;
2460 int ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002461
2462 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002463
2464 if (compositor->backend->repaint_begin)
2465 repaint_data = compositor->backend->repaint_begin(compositor);
2466
2467 wl_list_for_each(output, &compositor->output_list, link) {
2468 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2469 if (ret)
2470 break;
2471 }
2472
2473 if (ret == 0) {
2474 if (compositor->backend->repaint_flush)
2475 compositor->backend->repaint_flush(compositor,
2476 repaint_data);
2477 } else {
2478 if (compositor->backend->repaint_cancel)
2479 compositor->backend->repaint_cancel(compositor,
2480 repaint_data);
2481 }
Daniel Stone6847b852017-03-01 11:34:08 +00002482
2483 output_repaint_timer_arm(compositor);
2484
Pekka Paalanen0513a952014-05-21 16:17:27 +03002485 return 0;
2486}
2487
Kristian Høgsbergef044142011-06-21 15:02:12 -04002488WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002489weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002490 const struct timespec *stamp,
2491 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002492{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002493 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002494 int32_t refresh_nsec;
2495 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002496 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002497
Pekka Paalanenb5026542014-11-12 15:09:24 +02002498 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2499 TLP_VBLANK(stamp), TLP_END);
2500
Daniel Stone05df8c12017-03-03 16:59:42 +00002501 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002502 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2503
Daniel Stone6847b852017-03-01 11:34:08 +00002504 weston_compositor_read_presentation_clock(compositor, &now);
2505
Daniel Stone3615ce12017-03-01 11:34:05 +00002506 /* If we haven't been supplied any timestamp at all, we don't have a
2507 * timebase to work against, so any delay just wastes time. Push a
2508 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002509 if (!stamp) {
2510 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002511 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002512 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002513
Pekka Paalanend7894d02015-07-03 15:08:53 +03002514 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002515 weston_presentation_feedback_present_list(&output->feedback_list,
2516 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002517 output->msc,
2518 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002519
Daniel Stone37ad7e32017-03-01 11:34:02 +00002520 output->frame_time = timespec_to_msec(stamp);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002521
Daniel Stone6847b852017-03-01 11:34:08 +00002522 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2523 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2524 -compositor->repaint_msec);
2525 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002526
2527 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002528 static bool warned;
2529
2530 if (!warned)
2531 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002532 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002533 warned = true;
2534
Daniel Stone6847b852017-03-01 11:34:08 +00002535 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002536 }
2537
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002538 /* Called from restart_repaint_loop and restart happens already after
2539 * the deadline given by repaint_msec? In that case we delay until
2540 * the deadline of the next frame, to give clients a more predictable
2541 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002542 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2543 msec_rel < 0) {
2544 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2545 timespec_add_nsec(&output->next_repaint,
2546 &output->next_repaint,
2547 refresh_nsec);
2548 }
2549 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002550
Daniel Stone3615ce12017-03-01 11:34:05 +00002551out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002552 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002553 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002554}
2555
2556static void
2557idle_repaint(void *data)
2558{
2559 struct weston_output *output = data;
2560
Daniel Stone05df8c12017-03-03 16:59:42 +00002561 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2562 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002563 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002564}
2565
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002566WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002567weston_layer_entry_insert(struct weston_layer_entry *list,
2568 struct weston_layer_entry *entry)
2569{
2570 wl_list_insert(&list->link, &entry->link);
2571 entry->layer = list->layer;
2572}
2573
2574WL_EXPORT void
2575weston_layer_entry_remove(struct weston_layer_entry *entry)
2576{
2577 wl_list_remove(&entry->link);
2578 wl_list_init(&entry->link);
2579 entry->layer = NULL;
2580}
2581
Quentin Glidic82681572016-12-17 13:40:51 +01002582
2583/** Initialize the weston_layer struct.
2584 *
2585 * \param compositor The compositor instance
2586 * \param layer The layer to initialize
2587 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002588WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002589weston_layer_init(struct weston_layer *layer,
2590 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002591{
Quentin Glidic82681572016-12-17 13:40:51 +01002592 layer->compositor = compositor;
2593 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002594 wl_list_init(&layer->view_list.link);
2595 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002596 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002597}
2598
2599/** Sets the position of the layer in the layer list. The layer will be placed
2600 * below any layer with the same position value, if any.
2601 * This function is safe to call if the layer is already on the list, but the
2602 * layer may be moved below other layers at the same position, if any.
2603 *
2604 * \param layer The layer to modify
2605 * \param position The position the layer will be placed at
2606 */
2607WL_EXPORT void
2608weston_layer_set_position(struct weston_layer *layer,
2609 enum weston_layer_position position)
2610{
2611 struct weston_layer *below;
2612
2613 wl_list_remove(&layer->link);
2614
2615 /* layer_list is ordered from top to bottom, the last layer being the
2616 * background with the smallest position value */
2617
2618 layer->position = position;
2619 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2620 if (below->position >= layer->position) {
2621 wl_list_insert(&below->link, &layer->link);
2622 return;
2623 }
2624 }
2625 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2626}
2627
2628/** Hide a layer by taking it off the layer list.
2629 * This function is safe to call if the layer is not on the list.
2630 *
2631 * \param layer The layer to hide
2632 */
2633WL_EXPORT void
2634weston_layer_unset_position(struct weston_layer *layer)
2635{
2636 wl_list_remove(&layer->link);
2637 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002638}
2639
2640WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002641weston_layer_set_mask(struct weston_layer *layer,
2642 int x, int y, int width, int height)
2643{
2644 struct weston_view *view;
2645
2646 layer->mask.x1 = x;
2647 layer->mask.x2 = x + width;
2648 layer->mask.y1 = y;
2649 layer->mask.y2 = y + height;
2650
2651 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2652 weston_view_geometry_dirty(view);
2653 }
2654}
2655
2656WL_EXPORT void
2657weston_layer_set_mask_infinite(struct weston_layer *layer)
2658{
2659 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2660 UINT32_MAX, UINT32_MAX);
2661}
2662
2663WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002664weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002665{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002666 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002667 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002668
Bryce Harrington08976ac2016-08-30 12:05:16 -07002669 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2670 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002671 return;
2672
Pekka Paalanenb5026542014-11-12 15:09:24 +02002673 if (!output->repaint_needed)
2674 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2675
Kristian Høgsbergef044142011-06-21 15:02:12 -04002676 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002677 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002678
2679 /* If we already have a repaint scheduled for our idle handler,
2680 * no need to set it again. If the repaint has been called but
2681 * not finished, then weston_output_finish_frame() will notice
2682 * that a repaint is needed and schedule one. */
2683 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002684 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002685
Daniel Stone05df8c12017-03-03 16:59:42 +00002686 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002687 wl_event_loop_add_idle(loop, idle_repaint, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002688 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002689}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002690
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002691WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002692weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2693{
2694 struct weston_output *output;
2695
2696 wl_list_for_each(output, &compositor->output_list, link)
2697 weston_output_schedule_repaint(output);
2698}
2699
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002700static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002701surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002702{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002703 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002704}
2705
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002706static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002707surface_attach(struct wl_client *client,
2708 struct wl_resource *resource,
2709 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2710{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002711 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002712 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002713
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002714 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002715 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002716 if (buffer == NULL) {
2717 wl_client_post_no_memory(client);
2718 return;
2719 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002720 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002721
Pekka Paalanende685b82012-12-04 15:58:12 +02002722 /* Attach, attach, without commit in between does not send
2723 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002724 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002725
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002726 surface->pending.sx = sx;
2727 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002728 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002729}
2730
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002731static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002732surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002733 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002734 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002735{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002736 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002737
Derek Foreman57e92ed2015-11-17 14:11:35 -06002738 if (width <= 0 || height <= 0)
2739 return;
2740
Derek Foreman152254b2015-11-26 14:17:48 -06002741 pixman_region32_union_rect(&surface->pending.damage_surface,
2742 &surface->pending.damage_surface,
2743 x, y, width, height);
2744}
2745
2746static void
2747surface_damage_buffer(struct wl_client *client,
2748 struct wl_resource *resource,
2749 int32_t x, int32_t y, int32_t width, int32_t height)
2750{
2751 struct weston_surface *surface = wl_resource_get_user_data(resource);
2752
2753 if (width <= 0 || height <= 0)
2754 return;
2755
2756 pixman_region32_union_rect(&surface->pending.damage_buffer,
2757 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002758 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002759}
2760
Kristian Høgsberg33418202011-08-16 23:01:28 -04002761static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002762destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002763{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002764 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002765
2766 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002767 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002768}
2769
2770static void
2771surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002772 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002773{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002774 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002775 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002776
2777 cb = malloc(sizeof *cb);
2778 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002779 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002780 return;
2781 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002782
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002783 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2784 callback);
2785 if (cb->resource == NULL) {
2786 free(cb);
2787 wl_resource_post_no_memory(resource);
2788 return;
2789 }
2790
Jason Ekstranda85118c2013-06-27 20:17:02 -05002791 wl_resource_set_implementation(cb->resource, NULL, cb,
2792 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002793
Pekka Paalanenbc106382012-10-10 12:49:31 +03002794 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002795}
2796
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002797static void
2798surface_set_opaque_region(struct wl_client *client,
2799 struct wl_resource *resource,
2800 struct wl_resource *region_resource)
2801{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002802 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002803 struct weston_region *region;
2804
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002805 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002806 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002807 pixman_region32_copy(&surface->pending.opaque,
2808 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002809 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002810 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002811 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002812}
2813
2814static void
2815surface_set_input_region(struct wl_client *client,
2816 struct wl_resource *resource,
2817 struct wl_resource *region_resource)
2818{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002819 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002820 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002821
2822 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002823 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002824 pixman_region32_copy(&surface->pending.input,
2825 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002826 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002827 pixman_region32_fini(&surface->pending.input);
2828 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002829 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002830}
2831
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002832/* Cause damage to this sub-surface and all its children.
2833 *
2834 * This is useful when there are state changes that need an implicit
2835 * damage, e.g. a z-order change.
2836 */
2837static void
2838weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2839{
2840 struct weston_subsurface *child;
2841
2842 weston_surface_damage(sub->surface);
2843 sub->reordered = false;
2844
2845 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2846 if (child != sub)
2847 weston_surface_damage_subsurfaces(child);
2848}
2849
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002850static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002851weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002852{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002853 struct weston_subsurface *sub;
2854
2855 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2856 parent_link_pending) {
2857 wl_list_remove(&sub->parent_link);
2858 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002859
2860 if (sub->reordered)
2861 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002862 }
2863}
2864
2865static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002866weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002867 struct weston_matrix *matrix)
2868{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002869 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002870 double src_width, src_height, dest_width, dest_height;
2871
2872 weston_matrix_init(matrix);
2873
2874 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2875 src_width = surface->width_from_buffer;
2876 src_height = surface->height_from_buffer;
2877 } else {
2878 src_width = wl_fixed_to_double(vp->buffer.src_width);
2879 src_height = wl_fixed_to_double(vp->buffer.src_height);
2880 }
2881
2882 if (vp->surface.width == -1) {
2883 dest_width = src_width;
2884 dest_height = src_height;
2885 } else {
2886 dest_width = vp->surface.width;
2887 dest_height = vp->surface.height;
2888 }
2889
2890 if (src_width != dest_width || src_height != dest_height)
2891 weston_matrix_scale(matrix,
2892 src_width / dest_width,
2893 src_height / dest_height, 1);
2894
2895 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2896 weston_matrix_translate(matrix,
2897 wl_fixed_to_double(vp->buffer.src_x),
2898 wl_fixed_to_double(vp->buffer.src_y),
2899 0);
2900
2901 switch (vp->buffer.transform) {
2902 case WL_OUTPUT_TRANSFORM_FLIPPED:
2903 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2904 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2905 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2906 weston_matrix_scale(matrix, -1, 1, 1);
2907 weston_matrix_translate(matrix,
2908 surface->width_from_buffer, 0, 0);
2909 break;
2910 }
2911
2912 switch (vp->buffer.transform) {
2913 default:
2914 case WL_OUTPUT_TRANSFORM_NORMAL:
2915 case WL_OUTPUT_TRANSFORM_FLIPPED:
2916 break;
2917 case WL_OUTPUT_TRANSFORM_90:
2918 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2919 weston_matrix_rotate_xy(matrix, 0, 1);
2920 weston_matrix_translate(matrix,
2921 surface->height_from_buffer, 0, 0);
2922 break;
2923 case WL_OUTPUT_TRANSFORM_180:
2924 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2925 weston_matrix_rotate_xy(matrix, -1, 0);
2926 weston_matrix_translate(matrix,
2927 surface->width_from_buffer,
2928 surface->height_from_buffer, 0);
2929 break;
2930 case WL_OUTPUT_TRANSFORM_270:
2931 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2932 weston_matrix_rotate_xy(matrix, 0, -1);
2933 weston_matrix_translate(matrix,
2934 0, surface->width_from_buffer, 0);
2935 break;
2936 }
2937
2938 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2939}
2940
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002941/**
2942 * Compute a + b > c while being safe to overflows.
2943 */
2944static bool
2945fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2946{
2947 return (int64_t)a + (int64_t)b > (int64_t)c;
2948}
2949
2950static bool
2951weston_surface_is_pending_viewport_source_valid(
2952 const struct weston_surface *surface)
2953{
2954 const struct weston_surface_state *pend = &surface->pending;
2955 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2956 int width_from_buffer = 0;
2957 int height_from_buffer = 0;
2958 wl_fixed_t w;
2959 wl_fixed_t h;
2960
2961 /* If viewport source rect is not set, it is always ok. */
2962 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2963 return true;
2964
2965 if (pend->newly_attached) {
2966 if (pend->buffer) {
2967 convert_size_by_transform_scale(&width_from_buffer,
2968 &height_from_buffer,
2969 pend->buffer->width,
2970 pend->buffer->height,
2971 vp->buffer.transform,
2972 vp->buffer.scale);
2973 } else {
2974 /* No buffer: viewport is irrelevant. */
2975 return true;
2976 }
2977 } else {
2978 width_from_buffer = surface->width_from_buffer;
2979 height_from_buffer = surface->height_from_buffer;
2980 }
2981
2982 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2983 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2984
2985 /* No buffer: viewport is irrelevant. */
2986 if (width_from_buffer == 0 || height_from_buffer == 0)
2987 return true;
2988
2989 /* overflow checks for wl_fixed_from_int() */
2990 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2991 return false;
2992 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2993 return false;
2994
2995 w = wl_fixed_from_int(width_from_buffer);
2996 h = wl_fixed_from_int(height_from_buffer);
2997
2998 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2999 return false;
3000 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3001 return false;
3002
3003 return true;
3004}
3005
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003006static bool
3007fixed_is_integer(wl_fixed_t v)
3008{
3009 return (v & 0xff) == 0;
3010}
3011
3012static bool
3013weston_surface_is_pending_viewport_dst_size_int(
3014 const struct weston_surface *surface)
3015{
3016 const struct weston_buffer_viewport *vp =
3017 &surface->pending.buffer_viewport;
3018
3019 if (vp->surface.width != -1) {
3020 assert(vp->surface.width > 0 && vp->surface.height > 0);
3021 return true;
3022 }
3023
3024 return fixed_is_integer(vp->buffer.src_width) &&
3025 fixed_is_integer(vp->buffer.src_height);
3026}
3027
Derek Foreman152254b2015-11-26 14:17:48 -06003028/* Translate pending damage in buffer co-ordinates to surface
3029 * co-ordinates and union it with a pixman_region32_t.
3030 * This should only be called after the buffer is attached.
3031 */
3032static void
3033apply_damage_buffer(pixman_region32_t *dest,
3034 struct weston_surface *surface,
3035 struct weston_surface_state *state)
3036{
3037 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3038
3039 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3040 * translated into surface co-ordinates and unioned with
3041 * any other surface damage.
3042 * None of this makes sense if there is no buffer though.
3043 */
3044 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3045 pixman_region32_t buffer_damage;
3046
3047 pixman_region32_intersect_rect(&state->damage_buffer,
3048 &state->damage_buffer,
3049 0, 0, buffer->width,
3050 buffer->height);
3051 pixman_region32_init(&buffer_damage);
3052 weston_matrix_transform_region(&buffer_damage,
3053 &surface->buffer_to_surface_matrix,
3054 &state->damage_buffer);
3055 pixman_region32_union(dest, dest, &buffer_damage);
3056 pixman_region32_fini(&buffer_damage);
3057 }
3058 /* We should clear this on commit even if there was no buffer */
3059 pixman_region32_clear(&state->damage_buffer);
3060}
3061
Jason Ekstrand1e059042014-10-16 10:55:19 -05003062static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003063weston_surface_commit_state(struct weston_surface *surface,
3064 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003065{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003066 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003067 pixman_region32_t opaque;
3068
Alexander Larsson4ea95522013-05-22 14:41:37 +02003069 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003070 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003071 /* wp_viewport.set_source */
3072 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003073 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003074
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003075 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003076 if (state->newly_attached)
3077 weston_surface_attach(surface, state->buffer);
3078 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003079
Jason Ekstrand1e059042014-10-16 10:55:19 -05003080 weston_surface_build_buffer_matrix(surface,
3081 &surface->surface_to_buffer_matrix);
3082 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3083 &surface->surface_to_buffer_matrix);
3084
Jason Ekstrand7b982072014-05-20 14:33:03 -05003085 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003086 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003087 if (surface->committed)
3088 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003089 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003090
Jason Ekstrand7b982072014-05-20 14:33:03 -05003091 state->sx = 0;
3092 state->sy = 0;
3093 state->newly_attached = 0;
3094 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003095
Derek Foreman152254b2015-11-26 14:17:48 -06003096 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003097 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003098 (pixman_region32_not_empty(&state->damage_surface) ||
3099 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003100 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003101
Pekka Paalanen8e159182012-10-10 12:49:25 +03003102 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003103 &state->damage_surface);
3104
3105 apply_damage_buffer(&surface->damage, surface, state);
3106
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003107 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003108 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003109 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003110
3111 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003112 pixman_region32_init(&opaque);
3113 pixman_region32_intersect_rect(&opaque, &state->opaque,
3114 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003115
3116 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3117 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003118 wl_list_for_each(view, &surface->views, surface_link)
3119 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003120 }
3121
3122 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003123
3124 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003125 pixman_region32_intersect_rect(&surface->input, &state->input,
3126 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003127
Pekka Paalanenbc106382012-10-10 12:49:31 +03003128 /* wl_surface.frame */
3129 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003130 &state->frame_callback_list);
3131 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003132
3133 /* XXX:
3134 * What should happen with a feedback request, if there
3135 * is no wl_buffer attached for this commit?
3136 */
3137
3138 /* presentation.feedback */
3139 wl_list_insert_list(&surface->feedback_list,
3140 &state->feedback_list);
3141 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003142
3143 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003144}
3145
3146static void
3147weston_surface_commit(struct weston_surface *surface)
3148{
3149 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003150
Pekka Paalanene67858b2013-04-25 13:57:42 +03003151 weston_surface_commit_subsurface_order(surface);
3152
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003153 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003154}
3155
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003156static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003157weston_subsurface_commit(struct weston_subsurface *sub);
3158
3159static void
3160weston_subsurface_parent_commit(struct weston_subsurface *sub,
3161 int parent_is_synchronized);
3162
3163static void
3164surface_commit(struct wl_client *client, struct wl_resource *resource)
3165{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003166 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003167 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3168
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003169 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3170 assert(surface->viewport_resource);
3171
3172 wl_resource_post_error(surface->viewport_resource,
3173 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3174 "wl_surface@%d has viewport source outside buffer",
3175 wl_resource_get_id(resource));
3176 return;
3177 }
3178
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003179 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3180 assert(surface->viewport_resource);
3181
3182 wl_resource_post_error(surface->viewport_resource,
3183 WP_VIEWPORT_ERROR_BAD_SIZE,
3184 "wl_surface@%d viewport dst size not integer",
3185 wl_resource_get_id(resource));
3186 return;
3187 }
3188
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189 if (sub) {
3190 weston_subsurface_commit(sub);
3191 return;
3192 }
3193
3194 weston_surface_commit(surface);
3195
3196 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3197 if (sub->surface != surface)
3198 weston_subsurface_parent_commit(sub, 0);
3199 }
3200}
3201
3202static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003203surface_set_buffer_transform(struct wl_client *client,
3204 struct wl_resource *resource, int transform)
3205{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003206 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003207
Jonny Lamba55f1392014-05-30 12:07:15 +02003208 /* if wl_output.transform grows more members this will need to be updated. */
3209 if (transform < 0 ||
3210 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3211 wl_resource_post_error(resource,
3212 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3213 "buffer transform must be a valid transform "
3214 "('%d' specified)", transform);
3215 return;
3216 }
3217
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003218 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003219 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003220}
3221
Alexander Larsson4ea95522013-05-22 14:41:37 +02003222static void
3223surface_set_buffer_scale(struct wl_client *client,
3224 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003225 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003226{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003227 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003228
Jonny Lamba55f1392014-05-30 12:07:15 +02003229 if (scale < 1) {
3230 wl_resource_post_error(resource,
3231 WL_SURFACE_ERROR_INVALID_SCALE,
3232 "buffer scale must be at least one "
3233 "('%d' specified)", scale);
3234 return;
3235 }
3236
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003237 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003238 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003239}
3240
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003241static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003242 surface_destroy,
3243 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003244 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003245 surface_frame,
3246 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003247 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003248 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003249 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003250 surface_set_buffer_scale,
3251 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003252};
3253
3254static void
3255compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003256 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003257{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003258 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003259 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003260
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003261 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003262 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003263 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003264 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003265 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003266
Jason Ekstranda85118c2013-06-27 20:17:02 -05003267 surface->resource =
3268 wl_resource_create(client, &wl_surface_interface,
3269 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003270 if (surface->resource == NULL) {
3271 weston_surface_destroy(surface);
3272 wl_resource_post_no_memory(resource);
3273 return;
3274 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003275 wl_resource_set_implementation(surface->resource, &surface_interface,
3276 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003277
3278 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003279}
3280
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003281static void
3282destroy_region(struct wl_resource *resource)
3283{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003284 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003285
3286 pixman_region32_fini(&region->region);
3287 free(region);
3288}
3289
3290static void
3291region_destroy(struct wl_client *client, struct wl_resource *resource)
3292{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003293 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003294}
3295
3296static void
3297region_add(struct wl_client *client, struct wl_resource *resource,
3298 int32_t x, int32_t y, int32_t width, int32_t height)
3299{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003300 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003301
3302 pixman_region32_union_rect(&region->region, &region->region,
3303 x, y, width, height);
3304}
3305
3306static void
3307region_subtract(struct wl_client *client, struct wl_resource *resource,
3308 int32_t x, int32_t y, int32_t width, int32_t height)
3309{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003310 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003311 pixman_region32_t rect;
3312
3313 pixman_region32_init_rect(&rect, x, y, width, height);
3314 pixman_region32_subtract(&region->region, &region->region, &rect);
3315 pixman_region32_fini(&rect);
3316}
3317
3318static const struct wl_region_interface region_interface = {
3319 region_destroy,
3320 region_add,
3321 region_subtract
3322};
3323
3324static void
3325compositor_create_region(struct wl_client *client,
3326 struct wl_resource *resource, uint32_t id)
3327{
3328 struct weston_region *region;
3329
3330 region = malloc(sizeof *region);
3331 if (region == NULL) {
3332 wl_resource_post_no_memory(resource);
3333 return;
3334 }
3335
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003336 pixman_region32_init(&region->region);
3337
Jason Ekstranda85118c2013-06-27 20:17:02 -05003338 region->resource =
3339 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003340 if (region->resource == NULL) {
3341 free(region);
3342 wl_resource_post_no_memory(resource);
3343 return;
3344 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003345 wl_resource_set_implementation(region->resource, &region_interface,
3346 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003347}
3348
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003349static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003350 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003351 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003352};
3353
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003354static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003355weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3356{
3357 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003358
Jason Ekstrand7b982072014-05-20 14:33:03 -05003359 weston_surface_commit_state(surface, &sub->cached);
3360 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003361
3362 weston_surface_commit_subsurface_order(surface);
3363
3364 weston_surface_schedule_repaint(surface);
3365
Jason Ekstrand7b982072014-05-20 14:33:03 -05003366 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003367}
3368
3369static void
3370weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3371{
3372 struct weston_surface *surface = sub->surface;
3373
3374 /*
3375 * If this commit would cause the surface to move by the
3376 * attach(dx, dy) parameters, the old damage region must be
3377 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003378 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003379 */
Derek Foreman152254b2015-11-26 14:17:48 -06003380 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003381 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003382 pixman_region32_union(&sub->cached.damage_surface,
3383 &sub->cached.damage_surface,
3384 &surface->pending.damage_surface);
3385 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003386
3387 if (surface->pending.newly_attached) {
3388 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003389 weston_surface_state_set_buffer(&sub->cached,
3390 surface->pending.buffer);
3391 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003392 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003393 weston_presentation_feedback_discard_list(
3394 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003395 }
3396 sub->cached.sx += surface->pending.sx;
3397 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003398
Derek Foreman152254b2015-11-26 14:17:48 -06003399 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3400
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003401 sub->cached.buffer_viewport.changed |=
3402 surface->pending.buffer_viewport.changed;
3403 sub->cached.buffer_viewport.buffer =
3404 surface->pending.buffer_viewport.buffer;
3405 sub->cached.buffer_viewport.surface =
3406 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003407
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003408 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003409
3410 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3411
3412 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3413
3414 wl_list_insert_list(&sub->cached.frame_callback_list,
3415 &surface->pending.frame_callback_list);
3416 wl_list_init(&surface->pending.frame_callback_list);
3417
Pekka Paalanen133e4392014-09-23 22:08:46 -04003418 wl_list_insert_list(&sub->cached.feedback_list,
3419 &surface->pending.feedback_list);
3420 wl_list_init(&surface->pending.feedback_list);
3421
Jason Ekstrand7b982072014-05-20 14:33:03 -05003422 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003423}
3424
Derek Foreman280e7dd2014-10-03 13:13:42 -05003425static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003426weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3427{
3428 while (sub) {
3429 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003430 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003431
3432 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003433 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003434
3435 sub = weston_surface_to_subsurface(sub->parent);
3436 }
3437
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003438 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003439}
3440
3441static void
3442weston_subsurface_commit(struct weston_subsurface *sub)
3443{
3444 struct weston_surface *surface = sub->surface;
3445 struct weston_subsurface *tmp;
3446
3447 /* Recursive check for effectively synchronized. */
3448 if (weston_subsurface_is_synchronized(sub)) {
3449 weston_subsurface_commit_to_cache(sub);
3450 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003451 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003452 /* flush accumulated state from cache */
3453 weston_subsurface_commit_to_cache(sub);
3454 weston_subsurface_commit_from_cache(sub);
3455 } else {
3456 weston_surface_commit(surface);
3457 }
3458
3459 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3460 if (tmp->surface != surface)
3461 weston_subsurface_parent_commit(tmp, 0);
3462 }
3463 }
3464}
3465
3466static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003467weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003468{
3469 struct weston_surface *surface = sub->surface;
3470 struct weston_subsurface *tmp;
3471
Pekka Paalanene67858b2013-04-25 13:57:42 +03003472 /* From now on, commit_from_cache the whole sub-tree, regardless of
3473 * the synchronized mode of each child. This sub-surface or some
3474 * of its ancestors were synchronized, so we are synchronized
3475 * all the way down.
3476 */
3477
Jason Ekstrand7b982072014-05-20 14:33:03 -05003478 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003479 weston_subsurface_commit_from_cache(sub);
3480
3481 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3482 if (tmp->surface != surface)
3483 weston_subsurface_parent_commit(tmp, 1);
3484 }
3485}
3486
3487static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003488weston_subsurface_parent_commit(struct weston_subsurface *sub,
3489 int parent_is_synchronized)
3490{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003491 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003492 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003493 wl_list_for_each(view, &sub->surface->views, surface_link)
3494 weston_view_set_position(view,
3495 sub->position.x,
3496 sub->position.y);
3497
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003498 sub->position.set = 0;
3499 }
3500
3501 if (parent_is_synchronized || sub->synchronized)
3502 weston_subsurface_synchronized_commit(sub);
3503}
3504
Pekka Paalanen8274d902014-08-06 19:36:51 +03003505static int
3506subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3507{
3508 return snprintf(buf, len, "sub-surface");
3509}
3510
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003511static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003512subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003513{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003514 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003515
Jason Ekstranda7af7042013-10-12 22:38:11 -05003516 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003517 weston_view_set_position(view,
3518 view->geometry.x + dx,
3519 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003520
3521 /* No need to check parent mappedness, because if parent is not
3522 * mapped, parent is not in a visible layer, so this sub-surface
3523 * will not be drawn either.
3524 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003525
Pekka Paalanene67858b2013-04-25 13:57:42 +03003526 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003527 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003528
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003529 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003530 * because that would call it also for the parent surface,
3531 * which might not be mapped yet. That would lead to
3532 * inconsistent state, where the window could never be
3533 * mapped.
3534 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003535 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003536 * weston_surface_is_mapped() return true, so that when the
3537 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003538 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003539 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003540 }
3541}
3542
3543static struct weston_subsurface *
3544weston_surface_to_subsurface(struct weston_surface *surface)
3545{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003546 if (surface->committed == subsurface_committed)
3547 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003548
3549 return NULL;
3550}
3551
Pekka Paalanen01388e22013-04-25 13:57:44 +03003552WL_EXPORT struct weston_surface *
3553weston_surface_get_main_surface(struct weston_surface *surface)
3554{
3555 struct weston_subsurface *sub;
3556
3557 while (surface && (sub = weston_surface_to_subsurface(surface)))
3558 surface = sub->parent;
3559
3560 return surface;
3561}
3562
Pekka Paalanen50b67472014-10-01 15:02:41 +03003563WL_EXPORT int
3564weston_surface_set_role(struct weston_surface *surface,
3565 const char *role_name,
3566 struct wl_resource *error_resource,
3567 uint32_t error_code)
3568{
3569 assert(role_name);
3570
3571 if (surface->role_name == NULL ||
3572 surface->role_name == role_name ||
3573 strcmp(surface->role_name, role_name) == 0) {
3574 surface->role_name = role_name;
3575
3576 return 0;
3577 }
3578
3579 wl_resource_post_error(error_resource, error_code,
3580 "Cannot assign role %s to wl_surface@%d,"
3581 " already has role %s\n",
3582 role_name,
3583 wl_resource_get_id(surface->resource),
3584 surface->role_name);
3585 return -1;
3586}
3587
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003588WL_EXPORT const char *
3589weston_surface_get_role(struct weston_surface *surface)
3590{
3591 return surface->role_name;
3592}
3593
Pekka Paalanen8274d902014-08-06 19:36:51 +03003594WL_EXPORT void
3595weston_surface_set_label_func(struct weston_surface *surface,
3596 int (*desc)(struct weston_surface *,
3597 char *, size_t))
3598{
3599 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003600 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003601}
3602
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003603/** Get the size of surface contents
3604 *
3605 * \param surface The surface to query.
3606 * \param width Returns the width of raw contents.
3607 * \param height Returns the height of raw contents.
3608 *
3609 * Retrieves the raw surface content size in pixels for the given surface.
3610 * This is the whole content size in buffer pixels. If the surface
3611 * has no content or the renderer does not implement this feature,
3612 * zeroes are returned.
3613 *
3614 * This function is used to determine the buffer size needed for
3615 * a weston_surface_copy_content() call.
3616 */
3617WL_EXPORT void
3618weston_surface_get_content_size(struct weston_surface *surface,
3619 int *width, int *height)
3620{
3621 struct weston_renderer *rer = surface->compositor->renderer;
3622
3623 if (!rer->surface_get_content_size) {
3624 *width = 0;
3625 *height = 0;
3626 return;
3627 }
3628
3629 rer->surface_get_content_size(surface, width, height);
3630}
3631
Quentin Glidic248dd102016-08-12 10:41:34 +02003632/** Get the bounding box of a surface and its subsurfaces
3633 *
3634 * \param surface The surface to query.
3635 * \return The bounding box relative to the surface origin.
3636 *
3637 */
3638WL_EXPORT struct weston_geometry
3639weston_surface_get_bounding_box(struct weston_surface *surface)
3640{
3641 pixman_region32_t region;
3642 pixman_box32_t *box;
3643 struct weston_subsurface *subsurface;
3644
3645 pixman_region32_init_rect(&region,
3646 0, 0,
3647 surface->width, surface->height);
3648
3649 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3650 pixman_region32_union_rect(&region, &region,
3651 subsurface->position.x,
3652 subsurface->position.y,
3653 subsurface->surface->width,
3654 subsurface->surface->height);
3655
3656 box = pixman_region32_extents(&region);
3657 struct weston_geometry geometry = {
3658 .x = box->x1,
3659 .y = box->y1,
3660 .width = box->x2 - box->x1,
3661 .height = box->y2 - box->y1,
3662 };
3663
3664 pixman_region32_fini(&region);
3665
3666 return geometry;
3667}
3668
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003669/** Copy surface contents to system memory.
3670 *
3671 * \param surface The surface to copy from.
3672 * \param target Pointer to the target memory buffer.
3673 * \param size Size of the target buffer in bytes.
3674 * \param src_x X location on contents to copy from.
3675 * \param src_y Y location on contents to copy from.
3676 * \param width Width in pixels of the area to copy.
3677 * \param height Height in pixels of the area to copy.
3678 * \return 0 for success, -1 for failure.
3679 *
3680 * Surface contents are maintained by the renderer. They can be in a
3681 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3682 * else.
3683 *
3684 * Surface contents are copied into memory pointed to by target,
3685 * which has size bytes of space available. The target memory
3686 * may be larger than needed, but being smaller returns an error.
3687 * The extra bytes in target may or may not be written; their content is
3688 * unspecified. Size must be large enough to hold the image.
3689 *
3690 * The image in the target memory will be arranged in rows from
3691 * top to bottom, and pixels on a row from left to right. The pixel
3692 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3693 * width * 4.
3694 *
3695 * Parameters src_x and src_y define the upper-left corner in buffer
3696 * coordinates (pixels) to copy from. Parameters width and height
3697 * define the size of the area to copy in pixels.
3698 *
3699 * The rectangle defined by src_x, src_y, width, height must fit in
3700 * the surface contents. Otherwise an error is returned.
3701 *
3702 * Use surface_get_data_size to determine the content size; the
3703 * needed target buffer size and rectangle limits.
3704 *
3705 * CURRENT IMPLEMENTATION RESTRICTIONS:
3706 * - the machine must be little-endian due to Pixman formats.
3707 *
3708 * NOTE: Pixman formats are premultiplied.
3709 */
3710WL_EXPORT int
3711weston_surface_copy_content(struct weston_surface *surface,
3712 void *target, size_t size,
3713 int src_x, int src_y,
3714 int width, int height)
3715{
3716 struct weston_renderer *rer = surface->compositor->renderer;
3717 int cw, ch;
3718 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3719
3720 if (!rer->surface_copy_content)
3721 return -1;
3722
3723 weston_surface_get_content_size(surface, &cw, &ch);
3724
3725 if (src_x < 0 || src_y < 0)
3726 return -1;
3727
3728 if (width <= 0 || height <= 0)
3729 return -1;
3730
3731 if (src_x + width > cw || src_y + height > ch)
3732 return -1;
3733
3734 if (width * bytespp * height > size)
3735 return -1;
3736
3737 return rer->surface_copy_content(surface, target, size,
3738 src_x, src_y, width, height);
3739}
3740
Pekka Paalanene67858b2013-04-25 13:57:42 +03003741static void
3742subsurface_set_position(struct wl_client *client,
3743 struct wl_resource *resource, int32_t x, int32_t y)
3744{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003745 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003746
3747 if (!sub)
3748 return;
3749
3750 sub->position.x = x;
3751 sub->position.y = y;
3752 sub->position.set = 1;
3753}
3754
3755static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003756subsurface_find_sibling(struct weston_subsurface *sub,
3757 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003758{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003759 struct weston_surface *parent = sub->parent;
3760 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003761
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003762 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3763 if (sibling->surface == surface && sibling != sub)
3764 return sibling;
3765 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003766
3767 return NULL;
3768}
3769
3770static struct weston_subsurface *
3771subsurface_sibling_check(struct weston_subsurface *sub,
3772 struct weston_surface *surface,
3773 const char *request)
3774{
3775 struct weston_subsurface *sibling;
3776
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003777 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003778 if (!sibling) {
3779 wl_resource_post_error(sub->resource,
3780 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3781 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003782 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003783 return NULL;
3784 }
3785
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003786 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003787
3788 return sibling;
3789}
3790
3791static void
3792subsurface_place_above(struct wl_client *client,
3793 struct wl_resource *resource,
3794 struct wl_resource *sibling_resource)
3795{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003796 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003797 struct weston_surface *surface =
3798 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003799 struct weston_subsurface *sibling;
3800
3801 if (!sub)
3802 return;
3803
3804 sibling = subsurface_sibling_check(sub, surface, "place_above");
3805 if (!sibling)
3806 return;
3807
3808 wl_list_remove(&sub->parent_link_pending);
3809 wl_list_insert(sibling->parent_link_pending.prev,
3810 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003811
3812 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003813}
3814
3815static void
3816subsurface_place_below(struct wl_client *client,
3817 struct wl_resource *resource,
3818 struct wl_resource *sibling_resource)
3819{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003820 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003821 struct weston_surface *surface =
3822 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003823 struct weston_subsurface *sibling;
3824
3825 if (!sub)
3826 return;
3827
3828 sibling = subsurface_sibling_check(sub, surface, "place_below");
3829 if (!sibling)
3830 return;
3831
3832 wl_list_remove(&sub->parent_link_pending);
3833 wl_list_insert(&sibling->parent_link_pending,
3834 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003835
3836 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003837}
3838
3839static void
3840subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3841{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003842 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003843
3844 if (sub)
3845 sub->synchronized = 1;
3846}
3847
3848static void
3849subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3850{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003851 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003852
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003853 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003854 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003855
3856 /* If sub became effectively desynchronized, flush. */
3857 if (!weston_subsurface_is_synchronized(sub))
3858 weston_subsurface_synchronized_commit(sub);
3859 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003860}
3861
3862static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003863weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3864{
3865 wl_list_remove(&sub->parent_link);
3866 wl_list_remove(&sub->parent_link_pending);
3867 wl_list_remove(&sub->parent_destroy_listener.link);
3868 sub->parent = NULL;
3869}
3870
3871static void
3872weston_subsurface_destroy(struct weston_subsurface *sub);
3873
3874static void
3875subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3876{
3877 struct weston_subsurface *sub =
3878 container_of(listener, struct weston_subsurface,
3879 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003880 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003881
3882 /* The protocol object (wl_resource) is left inert. */
3883 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003884 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003885
3886 weston_subsurface_destroy(sub);
3887}
3888
3889static void
3890subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3891{
3892 struct weston_subsurface *sub =
3893 container_of(listener, struct weston_subsurface,
3894 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003895 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003896 assert(sub->surface != sub->parent);
3897
3898 if (weston_surface_is_mapped(sub->surface))
3899 weston_surface_unmap(sub->surface);
3900
3901 weston_subsurface_unlink_parent(sub);
3902}
3903
3904static void
3905subsurface_resource_destroy(struct wl_resource *resource)
3906{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003907 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003908
3909 if (sub)
3910 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003911}
3912
3913static void
3914subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3915{
3916 wl_resource_destroy(resource);
3917}
3918
3919static void
3920weston_subsurface_link_parent(struct weston_subsurface *sub,
3921 struct weston_surface *parent)
3922{
3923 sub->parent = parent;
3924 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003925 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003926 &sub->parent_destroy_listener);
3927
3928 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3929 wl_list_insert(&parent->subsurface_list_pending,
3930 &sub->parent_link_pending);
3931}
3932
3933static void
3934weston_subsurface_link_surface(struct weston_subsurface *sub,
3935 struct weston_surface *surface)
3936{
3937 sub->surface = surface;
3938 sub->surface_destroy_listener.notify =
3939 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003940 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003941 &sub->surface_destroy_listener);
3942}
3943
3944static void
3945weston_subsurface_destroy(struct weston_subsurface *sub)
3946{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003947 struct weston_view *view, *next;
3948
Pekka Paalanene67858b2013-04-25 13:57:42 +03003949 assert(sub->surface);
3950
3951 if (sub->resource) {
3952 assert(weston_surface_to_subsurface(sub->surface) == sub);
3953 assert(sub->parent_destroy_listener.notify ==
3954 subsurface_handle_parent_destroy);
3955
George Kiagiadakised04d382014-06-13 18:10:26 +02003956 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3957 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003958 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003959 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003960
Pekka Paalanene67858b2013-04-25 13:57:42 +03003961 if (sub->parent)
3962 weston_subsurface_unlink_parent(sub);
3963
Jason Ekstrand7b982072014-05-20 14:33:03 -05003964 weston_surface_state_fini(&sub->cached);
3965 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003966
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003967 sub->surface->committed = NULL;
3968 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003969 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003970 } else {
3971 /* the dummy weston_subsurface for the parent itself */
3972 assert(sub->parent_destroy_listener.notify == NULL);
3973 wl_list_remove(&sub->parent_link);
3974 wl_list_remove(&sub->parent_link_pending);
3975 }
3976
3977 wl_list_remove(&sub->surface_destroy_listener.link);
3978 free(sub);
3979}
3980
3981static const struct wl_subsurface_interface subsurface_implementation = {
3982 subsurface_destroy,
3983 subsurface_set_position,
3984 subsurface_place_above,
3985 subsurface_place_below,
3986 subsurface_set_sync,
3987 subsurface_set_desync
3988};
3989
3990static struct weston_subsurface *
3991weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3992 struct weston_surface *parent)
3993{
3994 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003995 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003996
Bryce Harringtonde16d892014-11-20 22:21:57 -08003997 sub = zalloc(sizeof *sub);
3998 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003999 return NULL;
4000
Jason Ekstranda7af7042013-10-12 22:38:11 -05004001 wl_list_init(&sub->unused_views);
4002
Jason Ekstranda85118c2013-06-27 20:17:02 -05004003 sub->resource =
4004 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004005 if (!sub->resource) {
4006 free(sub);
4007 return NULL;
4008 }
4009
Jason Ekstranda85118c2013-06-27 20:17:02 -05004010 wl_resource_set_implementation(sub->resource,
4011 &subsurface_implementation,
4012 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004013 weston_subsurface_link_surface(sub, surface);
4014 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004015 weston_surface_state_init(&sub->cached);
4016 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004017 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004018
4019 return sub;
4020}
4021
4022/* Create a dummy subsurface for having the parent itself in its
4023 * sub-surface lists. Makes stacking order manipulation easy.
4024 */
4025static struct weston_subsurface *
4026weston_subsurface_create_for_parent(struct weston_surface *parent)
4027{
4028 struct weston_subsurface *sub;
4029
Bryce Harringtonde16d892014-11-20 22:21:57 -08004030 sub = zalloc(sizeof *sub);
4031 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004032 return NULL;
4033
4034 weston_subsurface_link_surface(sub, parent);
4035 sub->parent = parent;
4036 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4037 wl_list_insert(&parent->subsurface_list_pending,
4038 &sub->parent_link_pending);
4039
4040 return sub;
4041}
4042
4043static void
4044subcompositor_get_subsurface(struct wl_client *client,
4045 struct wl_resource *resource,
4046 uint32_t id,
4047 struct wl_resource *surface_resource,
4048 struct wl_resource *parent_resource)
4049{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004050 struct weston_surface *surface =
4051 wl_resource_get_user_data(surface_resource);
4052 struct weston_surface *parent =
4053 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004054 struct weston_subsurface *sub;
4055 static const char where[] = "get_subsurface: wl_subsurface@";
4056
4057 if (surface == parent) {
4058 wl_resource_post_error(resource,
4059 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4060 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004061 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004062 return;
4063 }
4064
4065 if (weston_surface_to_subsurface(surface)) {
4066 wl_resource_post_error(resource,
4067 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4068 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004069 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004070 return;
4071 }
4072
Pekka Paalanen50b67472014-10-01 15:02:41 +03004073 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4074 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004075 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004076
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004077 if (weston_surface_get_main_surface(parent) == surface) {
4078 wl_resource_post_error(resource,
4079 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4080 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004081 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004082 return;
4083 }
4084
Pekka Paalanene67858b2013-04-25 13:57:42 +03004085 /* make sure the parent is in its own list */
4086 if (wl_list_empty(&parent->subsurface_list)) {
4087 if (!weston_subsurface_create_for_parent(parent)) {
4088 wl_resource_post_no_memory(resource);
4089 return;
4090 }
4091 }
4092
4093 sub = weston_subsurface_create(id, surface, parent);
4094 if (!sub) {
4095 wl_resource_post_no_memory(resource);
4096 return;
4097 }
4098
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004099 surface->committed = subsurface_committed;
4100 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004101 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004102}
4103
4104static void
4105subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4106{
4107 wl_resource_destroy(resource);
4108}
4109
4110static const struct wl_subcompositor_interface subcompositor_interface = {
4111 subcompositor_destroy,
4112 subcompositor_get_subsurface
4113};
4114
4115static void
4116bind_subcompositor(struct wl_client *client,
4117 void *data, uint32_t version, uint32_t id)
4118{
4119 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004120 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004121
Jason Ekstranda85118c2013-06-27 20:17:02 -05004122 resource =
4123 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004124 if (resource == NULL) {
4125 wl_client_post_no_memory(client);
4126 return;
4127 }
4128 wl_resource_set_implementation(resource, &subcompositor_interface,
4129 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004130}
4131
Bryce Harrington0795ece2016-08-30 12:04:26 -07004132/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004133 *
4134 * \param compositor The compositor instance
4135 * \param state The DPMS state the outputs will be set to
4136 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004137static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004138weston_compositor_dpms(struct weston_compositor *compositor,
4139 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004140{
4141 struct weston_output *output;
4142
Bryce Harrington08976ac2016-08-30 12:05:16 -07004143 wl_list_for_each(output, &compositor->output_list, link)
4144 if (output->set_dpms)
4145 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004146}
4147
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004148/** Restores the compositor to active status
4149 *
4150 * \param compositor The compositor instance
4151 *
4152 * If the compositor was in a sleeping mode, all outputs are powered
4153 * back on via DPMS. Otherwise if the compositor was inactive
4154 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4155 * signal will fire.
4156 *
4157 * Restarts the idle timer.
4158 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004159WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004160weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004161{
Neil Roberts8b62e202013-09-30 13:14:47 +01004162 uint32_t old_state = compositor->state;
4163
4164 /* The state needs to be changed before emitting the wake
4165 * signal because that may try to schedule a repaint which
4166 * will not work if the compositor is still sleeping */
4167 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4168
4169 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004170 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004171 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004172 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004173 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004174 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004175 /* fall through */
4176 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004177 wl_event_source_timer_update(compositor->idle_source,
4178 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004179 }
4180}
4181
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004182/** Turns off rendering and frame events for the compositor.
4183 *
4184 * \param compositor The compositor instance
4185 *
4186 * This is used for example to prevent further rendering while the
4187 * compositor is shutting down.
4188 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004189 * Stops the idle timer.
4190 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004191WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004192weston_compositor_offscreen(struct weston_compositor *compositor)
4193{
4194 switch (compositor->state) {
4195 case WESTON_COMPOSITOR_OFFSCREEN:
4196 return;
4197 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004198 default:
4199 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4200 wl_event_source_timer_update(compositor->idle_source, 0);
4201 }
4202}
4203
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004204/** Powers down all attached output devices
4205 *
4206 * \param compositor The compositor instance
4207 *
4208 * Causes rendering to the outputs to cease, and no frame events to be
4209 * sent. Only powers down the outputs if the compositor is not already
4210 * in sleep mode.
4211 *
4212 * Stops the idle timer.
4213 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004214WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004215weston_compositor_sleep(struct weston_compositor *compositor)
4216{
4217 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4218 return;
4219
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004220 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004221 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4222 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4223}
4224
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004225/** Sets compositor to idle mode
4226 *
4227 * \param data The compositor instance
4228 *
4229 * This is called when the idle timer fires. Once the compositor is in
4230 * idle mode it requires a wake action (e.g. via
4231 * weston_compositor_wake()) to restore it. The compositor's
4232 * idle_signal will be triggered when the idle event occurs.
4233 *
4234 * Idleness can be inhibited by setting the compositor's idle_inhibit
4235 * property.
4236 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004237static int
4238idle_handler(void *data)
4239{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004240 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004241
4242 if (compositor->idle_inhibit)
4243 return 1;
4244
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004245 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004246 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004247
4248 return 1;
4249}
4250
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004251WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004252weston_plane_init(struct weston_plane *plane,
4253 struct weston_compositor *ec,
4254 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004255{
4256 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004257 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004258 plane->x = x;
4259 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004260 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004261
4262 /* Init the link so that the call to wl_list_remove() when releasing
4263 * the plane without ever stacking doesn't lead to a crash */
4264 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004265}
4266
4267WL_EXPORT void
4268weston_plane_release(struct weston_plane *plane)
4269{
Xiong Zhang97116532013-10-23 13:58:31 +08004270 struct weston_view *view;
4271
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004272 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004273 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004274
Xiong Zhang97116532013-10-23 13:58:31 +08004275 wl_list_for_each(view, &plane->compositor->view_list, link) {
4276 if (view->plane == plane)
4277 view->plane = NULL;
4278 }
4279
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004280 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004281}
4282
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004283WL_EXPORT void
4284weston_compositor_stack_plane(struct weston_compositor *ec,
4285 struct weston_plane *plane,
4286 struct weston_plane *above)
4287{
4288 if (above)
4289 wl_list_insert(above->link.prev, &plane->link);
4290 else
4291 wl_list_insert(&ec->plane_list, &plane->link);
4292}
4293
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004294static void
4295output_release(struct wl_client *client, struct wl_resource *resource)
4296{
4297 wl_resource_destroy(resource);
4298}
4299
4300static const struct wl_output_interface output_interface = {
4301 output_release,
4302};
4303
4304
Casey Dahlin9074db52012-04-19 22:50:09 -04004305static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004306{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004307 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004308}
4309
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004310static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004311bind_output(struct wl_client *client,
4312 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004313{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004314 struct weston_output *output = data;
4315 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004316 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004317
Jason Ekstranda85118c2013-06-27 20:17:02 -05004318 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004319 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004320 if (resource == NULL) {
4321 wl_client_post_no_memory(client);
4322 return;
4323 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004324
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004325 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004326 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004327
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004328 wl_output_send_geometry(resource,
4329 output->x,
4330 output->y,
4331 output->mm_width,
4332 output->mm_height,
4333 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004334 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004335 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004336 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004337 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004338 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004339
4340 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004341 wl_output_send_mode(resource,
4342 mode->flags,
4343 mode->width,
4344 mode->height,
4345 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004346 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004347
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004348 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004349 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004350}
4351
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004352/** Get the backing object of wl_output
4353 *
4354 * \param resource A wl_output protocol object.
4355 * \return The backing object (user data) of a wl_resource representing a
4356 * wl_output protocol object.
4357 */
4358WL_EXPORT struct weston_output *
4359weston_output_from_resource(struct wl_resource *resource)
4360{
4361 assert(wl_resource_instance_of(resource, &wl_output_interface,
4362 &output_interface));
4363
4364 return wl_resource_get_user_data(resource);
4365}
4366
4367
David Fort0de859e2016-05-27 23:22:57 +02004368/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004369static void
David Fort0de859e2016-05-27 23:22:57 +02004370weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4371 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004372{
4373 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004374 bool start_resizing = false;
4375
4376 if (!delta_width)
4377 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004378
4379 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004380 if (output == resized_output) {
4381 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004382 continue;
4383 }
4384
David Fort0de859e2016-05-27 23:22:57 +02004385 if (start_resizing) {
4386 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004387 output->dirty = 1;
4388 }
4389 }
4390}
4391
Pekka Paalanend72bad22017-03-29 17:01:41 +03004392static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004393weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004394{
Scott Moreau850ca422012-05-21 15:21:25 -06004395 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004396
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004397 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004398 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004399
Scott Moreauccbf29d2012-02-22 14:21:41 -07004400 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004401 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004402 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004403 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004404 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004405 weston_matrix_scale(&output->matrix, magnification,
4406 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004407 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004408
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004409 switch (output->transform) {
4410 case WL_OUTPUT_TRANSFORM_FLIPPED:
4411 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4412 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4413 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4414 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4415 weston_matrix_scale(&output->matrix, -1, 1, 1);
4416 break;
4417 }
4418
4419 switch (output->transform) {
4420 default:
4421 case WL_OUTPUT_TRANSFORM_NORMAL:
4422 case WL_OUTPUT_TRANSFORM_FLIPPED:
4423 break;
4424 case WL_OUTPUT_TRANSFORM_90:
4425 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4426 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4427 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4428 break;
4429 case WL_OUTPUT_TRANSFORM_180:
4430 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4431 weston_matrix_translate(&output->matrix,
4432 -output->width, -output->height, 0);
4433 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4434 break;
4435 case WL_OUTPUT_TRANSFORM_270:
4436 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4437 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4438 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4439 break;
4440 }
4441
4442 if (output->current_scale != 1)
4443 weston_matrix_scale(&output->matrix,
4444 output->current_scale,
4445 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004446
Scott Moreauccbf29d2012-02-22 14:21:41 -07004447 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004448
4449 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004450}
4451
Scott Moreau1bad5db2012-08-18 01:04:05 -06004452static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004453weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004454{
4455 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004456 output->native_scale = scale;
4457 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004458
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004459 convert_size_by_transform_scale(&output->width, &output->height,
4460 output->current_mode->width,
4461 output->current_mode->height,
4462 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004463}
4464
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004465static void
4466weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004467{
4468 output->x = x;
4469 output->y = y;
4470
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004471 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004472 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004473
4474 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004475 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004476 output->width,
4477 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004478}
4479
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004480WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004481weston_output_move(struct weston_output *output, int x, int y)
4482{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004483 struct wl_resource *resource;
4484
4485 output->move_x = x - output->x;
4486 output->move_y = y - output->y;
4487
4488 if (output->move_x == 0 && output->move_y == 0)
4489 return;
4490
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004491 weston_output_init_geometry(output, x, y);
4492
4493 output->dirty = 1;
4494
4495 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004496 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004497
4498 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004499 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004500 wl_output_send_geometry(resource,
4501 output->x,
4502 output->y,
4503 output->mm_width,
4504 output->mm_height,
4505 output->subpixel,
4506 output->make,
4507 output->model,
4508 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004509
FORT David8a120692016-04-26 23:34:06 +02004510 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004511 wl_output_send_done(resource);
4512 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004513}
4514
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004515/** Signal that a pending output is taken into use.
4516 *
4517 * Removes the output from the pending list and adds it to the compositor's
4518 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03004519 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004520 * The output gets an internal ID assigned, and the wl_output global is
4521 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004522 *
Giulio Camuffob1147152015-05-06 21:41:57 +03004523 * \param compositor The compositor instance.
4524 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004525 *
4526 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03004527 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03004528static void
Giulio Camuffob1147152015-05-06 21:41:57 +03004529weston_compositor_add_output(struct weston_compositor *compositor,
4530 struct weston_output *output)
4531{
Armin Krezoviće5403842016-08-05 15:28:29 +02004532 struct weston_view *view, *next;
4533
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004534 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004535
4536 /* Verify we haven't reached the limit of 32 available output IDs */
4537 assert(ffs(~compositor->output_id_pool) > 0);
4538
4539 /* Invert the output id pool and look for the lowest numbered
4540 * switch (the least significant bit). Take that bit's position
4541 * as our ID, and mark it used in the compositor's output_id_pool.
4542 */
4543 output->id = ffs(~compositor->output_id_pool) - 1;
4544 compositor->output_id_pool |= 1u << output->id;
4545
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004546 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03004547 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004548 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004549
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004550 output->global = wl_global_create(compositor->wl_display,
4551 &wl_output_interface, 3,
4552 output, bind_output);
4553
Giulio Camuffob1147152015-05-06 21:41:57 +03004554 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02004555
4556 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4557 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004558}
4559
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004560/** Transform device coordinates into global coordinates
4561 *
4562 * \param device_x[in] X coordinate in device units.
4563 * \param device_y[in] Y coordinate in device units.
4564 * \param x[out] X coordinate in the global space.
4565 * \param y[out] Y coordinate in the global space.
4566 *
4567 * Transforms coordinates from the device coordinate space
4568 * (physical pixel units) to the global coordinate space (logical pixel units).
4569 * This takes into account output transform and scale.
4570 *
4571 * \memberof weston_output
4572 * \internal
4573 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004574WL_EXPORT void
4575weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004576 double device_x, double device_y,
4577 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004578{
Derek Foreman0f679412014-10-02 13:41:17 -05004579 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004580 device_x,
4581 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004582 0.0,
4583 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004584
Derek Foreman67a18b92015-03-24 11:36:14 -05004585 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004586
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004587 *x = p.f[0] / p.f[3];
4588 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004589}
4590
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004591/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004592 *
4593 * \param output The weston_output object that is being removed.
4594 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004595 * The following happens:
4596 *
4597 * - The output assignments of all views in the current scenegraph are
4598 * recomputed.
4599 *
4600 * - Presentation feedback is discarded.
4601 *
4602 * - Compositor is notified that outputs were changed and
4603 * applies the necessary changes to re-layout outputs.
4604 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004605 * - The output is put back in the pending outputs list.
4606 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004607 * - Signal is emitted to notify all users of the weston_output
4608 * object that the output is being destroyed.
4609 *
4610 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004611 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004612 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004613 * - The output's internal ID is released.
4614 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004615 * \memberof weston_output
4616 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004617 */
4618static void
4619weston_compositor_remove_output(struct weston_output *output)
4620{
Pekka Paalanenbccda712017-03-29 16:16:04 +03004621 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004622 struct wl_resource *resource;
4623 struct weston_view *view;
4624
4625 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004626 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004627
Pekka Paalanenbccda712017-03-29 16:16:04 +03004628 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004629 if (view->output_mask & (1u << output->id))
4630 weston_view_assign_output(view);
4631 }
4632
4633 weston_presentation_feedback_discard_list(&output->feedback_list);
4634
Pekka Paalanenbccda712017-03-29 16:16:04 +03004635 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004636
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004637 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004638 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03004639 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004640
Pekka Paalanenbccda712017-03-29 16:16:04 +03004641 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004642 wl_signal_emit(&output->destroy_signal, output);
4643
Pekka Paalanen2210ad02017-03-30 15:48:06 +03004644 wl_global_destroy(output->global);
4645 output->global = NULL;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004646 wl_resource_for_each(resource, &output->resource_list) {
4647 wl_resource_set_destructor(resource, NULL);
4648 }
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03004649
4650 compositor->output_id_pool &= ~(1u << output->id);
4651 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004652}
4653
4654/** Sets the output scale for a given output.
4655 *
4656 * \param output The weston_output object that the scale is set for.
4657 * \param scale Scale factor for the given output.
4658 *
4659 * It only supports setting scale for an output that
4660 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004661 *
4662 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004663 */
4664WL_EXPORT void
4665weston_output_set_scale(struct weston_output *output,
4666 int32_t scale)
4667{
4668 /* We can only set scale on a disabled output */
4669 assert(!output->enabled);
4670
4671 /* We only want to set scale once */
4672 assert(!output->scale);
4673
4674 output->scale = scale;
4675}
4676
4677/** Sets the output transform for a given output.
4678 *
4679 * \param output The weston_output object that the transform is set for.
4680 * \param transform Transform value for the given output.
4681 *
4682 * It only supports setting transform for an output that is
4683 * not enabled and it can only be ran once.
4684 *
4685 * Refer to wl_output::transform section located at
4686 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
4687 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004688 *
4689 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004690 */
4691WL_EXPORT void
4692weston_output_set_transform(struct weston_output *output,
4693 uint32_t transform)
4694{
4695 /* We can only set transform on a disabled output */
4696 assert(!output->enabled);
4697
4698 /* We only want to set transform once */
4699 assert(output->transform == UINT32_MAX);
4700
4701 output->transform = transform;
4702}
4703
4704/** Initializes a weston_output object with enough data so
4705 ** an output can be configured.
4706 *
4707 * \param output The weston_output object to initialize
4708 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004709 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004710 *
4711 * Sets initial values for fields that are expected to be
4712 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004713 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004714 * The name is used in logs, and can be used by compositors as a configuration
4715 * identifier.
4716 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004717 * \memberof weston_output
4718 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004719 */
4720WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02004721weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004722 struct weston_compositor *compositor,
4723 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004724{
4725 output->compositor = compositor;
4726 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03004727 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004728 wl_list_init(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004729 output->enabled = false;
4730
4731 /* Add some (in)sane defaults which can be used
4732 * for checking if an output was properly configured
4733 */
4734 output->mm_width = 0;
4735 output->mm_height = 0;
4736 output->scale = 0;
4737 /* Can't use -1 on uint32_t and 0 is valid enum value */
4738 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004739
4740 pixman_region32_init(&output->previous_damage);
4741 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03004742 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004743}
4744
4745/** Adds weston_output object to pending output list.
4746 *
4747 * \param output The weston_output object to add
4748 * \param compositor The compositor instance.
4749 *
4750 * Also notifies the compositor that an output is pending for
4751 * configuration.
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004752 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03004753 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004754 *
4755 * \memberof weston_output
4756 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004757 */
4758WL_EXPORT void
4759weston_compositor_add_pending_output(struct weston_output *output,
4760 struct weston_compositor *compositor)
4761{
Pekka Paalanene952a012017-03-29 17:14:00 +03004762 assert(output->disable);
4763 assert(output->enable);
4764
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004765 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004766 wl_list_insert(compositor->pending_output_list.prev, &output->link);
4767 wl_signal_emit(&compositor->output_pending_signal, output);
4768}
4769
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004770/** Constructs a weston_output object that can be used by the compositor.
4771 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03004772 * \param output The weston_output object that needs to be enabled. Must not
4773 * be enabled already.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004774 *
4775 * Output coordinates are calculated and each new output is by default
4776 * assigned to the right of previous one.
4777 *
4778 * Sets up the transformation, zoom, and geometry of the output using
4779 * the properties that need to be configured by the compositor.
4780 *
4781 * Establishes a repaint timer for the output with the relevant display
4782 * object's event loop. See output_repaint_timer_handler().
4783 *
4784 * The output is assigned an ID. Weston can support up to 32 distinct
4785 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4786 * is referred to and used to find the first available ID number, and
4787 * then this ID is marked as used in output_id_pool.
4788 *
4789 * The output is also assigned a Wayland global with the wl_output
4790 * external interface.
4791 *
4792 * Backend specific function is called to set up the output output.
4793 *
4794 * Output is added to the compositor's output list
4795 *
4796 * If the backend specific function fails, the weston_output object
4797 * is returned to a state it was before calling this function and
4798 * is added to the compositor's pending_output_list in case it needs
4799 * to be reconfigured or just so it can be destroyed at shutdown.
4800 *
4801 * 0 is returned on success, -1 on failure.
4802 */
4803WL_EXPORT int
4804weston_output_enable(struct weston_output *output)
4805{
Armin Krezović782f5df2016-09-30 14:11:11 +02004806 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004807 struct weston_output *iterator;
4808 int x = 0, y = 0;
4809
Pekka Paalanencc201e42017-03-30 15:11:25 +03004810 if (output->enabled) {
4811 weston_log("Error: attempt to enable an enabled output '%s'\n",
4812 output->name);
4813 return -1;
4814 }
4815
Armin Krezović782f5df2016-09-30 14:11:11 +02004816 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004817 struct weston_output, link);
4818
Armin Krezović782f5df2016-09-30 14:11:11 +02004819 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004820 x = iterator->x + iterator->width;
4821
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004822 /* Make sure the scale is set up */
4823 assert(output->scale);
4824
4825 /* Make sure we have a transform set */
4826 assert(output->transform != UINT32_MAX);
4827
Armin Krezović782f5df2016-09-30 14:11:11 +02004828 output->x = x;
4829 output->y = y;
4830 output->dirty = 1;
4831 output->original_scale = output->scale;
4832
4833 weston_output_transform_scale_init(output, output->transform, output->scale);
4834 weston_output_init_zoom(output);
4835
4836 weston_output_init_geometry(output, x, y);
4837 weston_output_damage(output);
4838
4839 wl_signal_init(&output->frame_signal);
4840 wl_signal_init(&output->destroy_signal);
4841 wl_list_init(&output->animation_list);
4842 wl_list_init(&output->resource_list);
4843 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02004844
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004845 /* Enable the output (set up the crtc or create a
4846 * window representing the output, set up the
4847 * renderer, etc)
4848 */
4849 if (output->enable(output) < 0) {
4850 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004851 return -1;
4852 }
4853
4854 weston_compositor_add_output(output->compositor, output);
4855
4856 return 0;
4857}
4858
4859/** Converts a weston_output object to a pending output state, so it
4860 ** can be configured again or destroyed.
4861 *
4862 * \param output The weston_output object that needs to be disabled.
4863 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004864 * Calls a backend specific function to disable an output, in case
4865 * such function exists.
4866 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004867 * The backend specific disable function may choose to postpone the disabling
4868 * by returning a negative value, in which case this function returns early.
4869 * In that case the backend will guarantee the output will be disabled soon
4870 * by the backend calling this function again. One must not attempt to re-enable
4871 * the output until that happens.
4872 *
4873 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004874 * from weston's output_list (see weston_compositor_remove_output())
4875 * and is returned to a state it was before weston_output_enable()
4876 * was ran (see weston_output_enable_undo()).
4877 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004878 * See weston_output_init() for more information on the
4879 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03004880 *
4881 * If the output has never been enabled yet, this function can still be
4882 * called to ensure that the output is actually turned off rather than left
4883 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004884 */
4885WL_EXPORT void
4886weston_output_disable(struct weston_output *output)
4887{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004888 /* Should we rename this? */
4889 output->destroying = 1;
4890
Pekka Paalanenc65df642017-03-29 15:45:46 +03004891 /* Disable is called unconditionally also for not-enabled outputs,
4892 * because at compositor start-up, if there is an output that is
4893 * already on but the compositor wants to turn it off, we have to
4894 * forward the turn-off to the backend so it knows to do it.
4895 * The backend cannot initially turn off everything, because it
4896 * would cause unnecessary mode-sets for all outputs the compositor
4897 * wants to be on.
4898 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004899 if (output->disable(output) < 0)
4900 return;
4901
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004902 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004903 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004904
4905 output->destroying = 0;
4906}
4907
4908/** Emits a signal to indicate that there are outputs waiting to be configured.
4909 *
4910 * \param compositor The compositor instance
4911 */
4912WL_EXPORT void
4913weston_pending_output_coldplug(struct weston_compositor *compositor)
4914{
4915 struct weston_output *output, *next;
4916
4917 wl_list_for_each_safe(output, next, &compositor->pending_output_list, link)
4918 wl_signal_emit(&compositor->output_pending_signal, output);
4919}
4920
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004921/** Uninitialize an output
4922 *
4923 * Removes the output from the list of enabled outputs if necessary, but
4924 * does not call the backend's output disable function. The output will no
4925 * longer be in the list of pending outputs either.
4926 *
4927 * All fields of weston_output become uninitialized, i.e. should not be used
4928 * anymore. The caller can free the memory after this.
4929 *
4930 * \memberof weston_output
4931 * \internal
4932 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004933WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03004934weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004935{
4936 output->destroying = 1;
4937
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004938 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004939 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004940
Pekka Paalanen4b582c72017-03-30 16:04:58 +03004941 pixman_region32_fini(&output->region);
4942 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004943 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004944 free(output->name);
4945}
4946
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004947static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004948destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004949{
Jonny Lamb74130762013-11-26 18:19:46 +01004950 struct weston_surface *surface =
4951 wl_resource_get_user_data(resource);
4952
Pekka Paalanen4826f872016-04-22 14:14:38 +03004953 if (!surface)
4954 return;
4955
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004956 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004957 surface->pending.buffer_viewport.buffer.src_width =
4958 wl_fixed_from_int(-1);
4959 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004960 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004961}
4962
4963static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004964viewport_destroy(struct wl_client *client,
4965 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004966{
4967 wl_resource_destroy(resource);
4968}
4969
4970static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004971viewport_set_source(struct wl_client *client,
4972 struct wl_resource *resource,
4973 wl_fixed_t src_x,
4974 wl_fixed_t src_y,
4975 wl_fixed_t src_width,
4976 wl_fixed_t src_height)
4977{
4978 struct weston_surface *surface =
4979 wl_resource_get_user_data(resource);
4980
Pekka Paalanen4826f872016-04-22 14:14:38 +03004981 if (!surface) {
4982 wl_resource_post_error(resource,
4983 WP_VIEWPORT_ERROR_NO_SURFACE,
4984 "wl_surface for this viewport is no longer exists");
4985 return;
4986 }
4987
4988 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004989 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004990
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004991 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004992 src_height == wl_fixed_from_int(-1) &&
4993 src_x == wl_fixed_from_int(-1) &&
4994 src_y == wl_fixed_from_int(-1)) {
4995 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004996 surface->pending.buffer_viewport.buffer.src_width =
4997 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004998 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004999 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005000 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005001
Pekka Paalanen201769a2016-04-26 14:42:11 +03005002 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005003 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005004 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03005005 "wl_surface@%d viewport source "
5006 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
5007 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005008 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03005009 wl_fixed_to_double(src_height),
5010 wl_fixed_to_double(src_x),
5011 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005012 return;
5013 }
5014
5015 surface->pending.buffer_viewport.buffer.src_x = src_x;
5016 surface->pending.buffer_viewport.buffer.src_y = src_y;
5017 surface->pending.buffer_viewport.buffer.src_width = src_width;
5018 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005019 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005020}
5021
5022static void
5023viewport_set_destination(struct wl_client *client,
5024 struct wl_resource *resource,
5025 int32_t dst_width,
5026 int32_t dst_height)
5027{
5028 struct weston_surface *surface =
5029 wl_resource_get_user_data(resource);
5030
Pekka Paalanen4826f872016-04-22 14:14:38 +03005031 if (!surface) {
5032 wl_resource_post_error(resource,
5033 WP_VIEWPORT_ERROR_NO_SURFACE,
5034 "wl_surface for this viewport no longer exists");
5035 return;
5036 }
5037
5038 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005039
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005040 if (dst_width == -1 && dst_height == -1) {
5041 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005042 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005043 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005044 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005045 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005046
5047 if (dst_width <= 0 || dst_height <= 0) {
5048 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005049 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005050 "destination size must be positive (%dx%d)",
5051 dst_width, dst_height);
5052 return;
5053 }
5054
5055 surface->pending.buffer_viewport.surface.width = dst_width;
5056 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005057 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005058}
5059
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005060static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005061 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005062 viewport_set_source,
5063 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01005064};
5065
5066static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005067viewporter_destroy(struct wl_client *client,
5068 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005069{
5070 wl_resource_destroy(resource);
5071}
5072
5073static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005074viewporter_get_viewport(struct wl_client *client,
5075 struct wl_resource *viewporter,
5076 uint32_t id,
5077 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005078{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005079 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005080 struct weston_surface *surface =
5081 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005082 struct wl_resource *resource;
5083
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005084 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005085 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005086 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005087 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01005088 return;
5089 }
5090
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005091 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005092 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005093 if (resource == NULL) {
5094 wl_client_post_no_memory(client);
5095 return;
5096 }
5097
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005098 wl_resource_set_implementation(resource, &viewport_interface,
5099 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01005100
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005101 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005102}
5103
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005104static const struct wp_viewporter_interface viewporter_interface = {
5105 viewporter_destroy,
5106 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01005107};
5108
5109static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005110bind_viewporter(struct wl_client *client,
5111 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005112{
5113 struct wl_resource *resource;
5114
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005115 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005116 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005117 if (resource == NULL) {
5118 wl_client_post_no_memory(client);
5119 return;
5120 }
5121
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005122 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01005123 NULL, NULL);
5124}
5125
5126static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04005127destroy_presentation_feedback(struct wl_resource *feedback_resource)
5128{
5129 struct weston_presentation_feedback *feedback;
5130
5131 feedback = wl_resource_get_user_data(feedback_resource);
5132
5133 wl_list_remove(&feedback->link);
5134 free(feedback);
5135}
5136
5137static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005138presentation_destroy(struct wl_client *client, struct wl_resource *resource)
5139{
5140 wl_resource_destroy(resource);
5141}
5142
5143static void
5144presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005145 struct wl_resource *presentation_resource,
5146 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005147 uint32_t callback)
5148{
Pekka Paalanen133e4392014-09-23 22:08:46 -04005149 struct weston_surface *surface;
5150 struct weston_presentation_feedback *feedback;
5151
5152 surface = wl_resource_get_user_data(surface_resource);
5153
Bryce Harringtonde16d892014-11-20 22:21:57 -08005154 feedback = zalloc(sizeof *feedback);
5155 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04005156 goto err_calloc;
5157
5158 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005159 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005160 1, callback);
5161 if (!feedback->resource)
5162 goto err_create;
5163
5164 wl_resource_set_implementation(feedback->resource, NULL, feedback,
5165 destroy_presentation_feedback);
5166
5167 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
5168
5169 return;
5170
5171err_create:
5172 free(feedback);
5173
5174err_calloc:
5175 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005176}
5177
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005178static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005179 presentation_destroy,
5180 presentation_feedback
5181};
5182
5183static void
5184bind_presentation(struct wl_client *client,
5185 void *data, uint32_t version, uint32_t id)
5186{
5187 struct weston_compositor *compositor = data;
5188 struct wl_resource *resource;
5189
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005190 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005191 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005192 if (resource == NULL) {
5193 wl_client_post_no_memory(client);
5194 return;
5195 }
5196
5197 wl_resource_set_implementation(resource, &presentation_implementation,
5198 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005199 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005200}
5201
5202static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05005203compositor_bind(struct wl_client *client,
5204 void *data, uint32_t version, uint32_t id)
5205{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005206 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005207 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05005208
Jason Ekstranda85118c2013-06-27 20:17:02 -05005209 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005210 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005211 if (resource == NULL) {
5212 wl_client_post_no_memory(client);
5213 return;
5214 }
5215
5216 wl_resource_set_implementation(resource, &compositor_interface,
5217 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05005218}
5219
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005220WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005221weston_environment_get_fd(const char *env)
5222{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005223 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005224 int fd, flags;
5225
5226 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005227 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005228 return -1;
5229
5230 flags = fcntl(fd, F_GETFD);
5231 if (flags == -1)
5232 return -1;
5233
5234 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
5235 unsetenv(env);
5236
5237 return fd;
5238}
5239
Pekka Paalanenb5026542014-11-12 15:09:24 +02005240static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005241timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02005242 uint32_t key, void *data)
5243{
5244 struct weston_compositor *compositor = data;
5245
5246 if (weston_timeline_enabled_)
5247 weston_timeline_close();
5248 else
5249 weston_timeline_open(compositor);
5250}
5251
Giulio Camuffo459137b2014-10-11 23:56:24 +03005252/** Create the compositor.
5253 *
5254 * This functions creates and initializes a compositor instance.
5255 *
5256 * \param display The Wayland display to be used.
5257 * \param user_data A pointer to an object that can later be retrieved
5258 * using the \ref weston_compositor_get_user_data function.
5259 * \return The compositor instance on success or NULL on failure.
5260 */
5261WL_EXPORT struct weston_compositor *
5262weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005263{
Giulio Camuffo459137b2014-10-11 23:56:24 +03005264 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05005265 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07005266
Giulio Camuffo459137b2014-10-11 23:56:24 +03005267 ec = zalloc(sizeof *ec);
5268 if (!ec)
5269 return NULL;
5270
5271 ec->wl_display = display;
5272 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005273 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07005274 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005275 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005276 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005277 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005278 wl_signal_init(&ec->idle_signal);
5279 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005280 wl_signal_init(&ec->show_input_panel_signal);
5281 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005282 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01005283 wl_signal_init(&ec->seat_created_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005284 wl_signal_init(&ec->output_pending_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01005285 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02005286 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005287 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02005288 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005289 wl_signal_init(&ec->session_signal);
5290 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005291
Casey Dahlin58ba1372012-04-19 22:50:08 -04005292 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03005293 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04005294
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02005295 ec->activate_serial = 1;
5296
Derek Foreman152254b2015-11-26 14:17:48 -06005297 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005298 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005299 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05005300
Giulio Camuffo954f1832014-10-11 18:27:30 +03005301 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005302 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005303 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03005304
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005305 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005306 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005307 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005308
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005309 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005310 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005311 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005312
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005313 if (weston_input_init(ec) != 0)
5314 goto fail;
5315
Jason Ekstranda7af7042013-10-12 22:38:11 -05005316 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005317 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005318 wl_list_init(&ec->layer_list);
5319 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005320 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005321 wl_list_init(&ec->output_list);
5322 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01005323 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005324 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005325 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005326 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005327 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005328
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005329 wl_list_init(&ec->plugin_api_list);
5330
Xiong Zhang97116532013-10-23 13:58:31 +08005331 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005332 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005333
Giulio Camuffo459137b2014-10-11 23:56:24 +03005334 wl_data_device_manager_init(ec->wl_display);
5335
5336 wl_display_init_shm(ec->wl_display);
5337
5338 loop = wl_display_get_event_loop(ec->wl_display);
5339 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00005340 ec->repaint_timer =
5341 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
5342 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005343
Quentin Glidic82681572016-12-17 13:40:51 +01005344 weston_layer_init(&ec->fade_layer, ec);
5345 weston_layer_init(&ec->cursor_layer, ec);
5346
5347 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
5348 weston_layer_set_position(&ec->cursor_layer,
5349 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005350
5351 weston_compositor_add_debug_binding(ec, KEY_T,
5352 timeline_key_binding_handler, ec);
5353
Giulio Camuffo459137b2014-10-11 23:56:24 +03005354 return ec;
5355
5356fail:
5357 free(ec);
5358 return NULL;
5359}
5360
Benjamin Franzkeb8263022011-08-30 11:32:47 +02005361WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005362weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07005363{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005364 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07005365
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005366 wl_event_source_remove(ec->idle_source);
5367
Matt Roper361d2ad2011-08-29 13:52:23 -07005368 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02005369 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07005370 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02005371
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005372 /* Destroy all pending outputs associated with this compositor */
5373 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
5374 output->destroy(output);
5375
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02005376 if (ec->renderer)
5377 ec->renderer->destroy(ec);
5378
Daniel Stone325fc2d2012-05-30 16:31:58 +01005379 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09005380 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005381 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005382 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005383 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005384 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005385
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005386 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07005387}
5388
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005389WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04005390weston_compositor_exit_with_code(struct weston_compositor *compositor,
5391 int exit_code)
5392{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02005393 if (compositor->exit_code == EXIT_SUCCESS)
5394 compositor->exit_code = exit_code;
5395
Giulio Camuffo459137b2014-10-11 23:56:24 +03005396 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04005397}
5398
5399WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005400weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
5401 const struct weston_pointer_grab_interface *interface)
5402{
5403 struct weston_seat *seat;
5404
5405 ec->default_pointer_grab = interface;
5406 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005407 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5408
5409 if (pointer)
5410 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005411 }
5412}
5413
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04005414WL_EXPORT int
5415weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
5416 clockid_t clk_id)
5417{
5418 struct timespec ts;
5419
5420 if (clock_gettime(clk_id, &ts) < 0)
5421 return -1;
5422
5423 compositor->presentation_clock = clk_id;
5424
5425 return 0;
5426}
5427
5428/*
5429 * For choosing the software clock, when the display hardware or API
5430 * does not expose a compatible presentation timestamp.
5431 */
5432WL_EXPORT int
5433weston_compositor_set_presentation_clock_software(
5434 struct weston_compositor *compositor)
5435{
5436 /* In order of preference */
5437 static const clockid_t clocks[] = {
5438 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
5439 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
5440 CLOCK_MONOTONIC, /* no jumps, may crawl */
5441 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
5442 CLOCK_REALTIME /* may jump and crawl */
5443 };
5444 unsigned i;
5445
5446 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
5447 if (weston_compositor_set_presentation_clock(compositor,
5448 clocks[i]) == 0)
5449 return 0;
5450
5451 weston_log("Error: no suitable presentation clock available.\n");
5452
5453 return -1;
5454}
5455
Pekka Paalanen662f3842015-03-18 12:17:26 +02005456/** Read the current time from the Presentation clock
5457 *
5458 * \param compositor
5459 * \param ts[out] The current time.
5460 *
5461 * \note Reading the current time in user space is always imprecise to some
5462 * degree.
5463 *
5464 * This function is never meant to fail. If reading the clock does fail,
5465 * an error message is logged and a zero time is returned. Callers are not
5466 * supposed to detect or react to failures.
5467 */
5468WL_EXPORT void
5469weston_compositor_read_presentation_clock(
5470 const struct weston_compositor *compositor,
5471 struct timespec *ts)
5472{
5473 static bool warned;
5474 int ret;
5475
5476 ret = clock_gettime(compositor->presentation_clock, ts);
5477 if (ret < 0) {
5478 ts->tv_sec = 0;
5479 ts->tv_nsec = 0;
5480
5481 if (!warned)
5482 weston_log("Error: failure to read "
5483 "the presentation clock %#x: '%m' (%d)\n",
5484 compositor->presentation_clock, errno);
5485 warned = true;
5486 }
5487}
5488
Pekka Paalanen230f3b12014-09-29 14:18:40 -04005489/** Import dmabuf buffer into current renderer
5490 *
5491 * \param compositor
5492 * \param buffer the dmabuf buffer to import
5493 * \return true on usable buffers, false otherwise
5494 *
5495 * This function tests that the linux_dmabuf_buffer is usable
5496 * for the current renderer. Returns false on unusable buffers. Usually
5497 * usability is tested by importing the dmabufs for composition.
5498 *
5499 * This hook is also used for detecting if the renderer supports
5500 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
5501 * supported.
5502 * */
5503WL_EXPORT bool
5504weston_compositor_import_dmabuf(struct weston_compositor *compositor,
5505 struct linux_dmabuf_buffer *buffer)
5506{
5507 struct weston_renderer *renderer;
5508
5509 renderer = compositor->renderer;
5510
5511 if (renderer->import_dmabuf == NULL)
5512 return false;
5513
5514 return renderer->import_dmabuf(compositor, buffer);
5515}
5516
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005517WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005518weston_version(int *major, int *minor, int *micro)
5519{
5520 *major = WESTON_VERSION_MAJOR;
5521 *minor = WESTON_VERSION_MINOR;
5522 *micro = WESTON_VERSION_MICRO;
5523}
5524
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005525WL_EXPORT void *
5526weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005527{
Derek Foreman3f86e502015-06-08 11:46:54 -05005528 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005529 char path[PATH_MAX];
5530 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00005531 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005532
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005533 if (name == NULL)
5534 return NULL;
5535
Derek Foreman3f86e502015-06-08 11:46:54 -05005536 if (name[0] != '/') {
5537 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00005538 len = snprintf(path, sizeof path, "%s/.libs/%s",
5539 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005540 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00005541 len = snprintf(path, sizeof path, "%s/%s",
5542 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005543 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00005544 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005545 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005546
Daniel Stonebeb97e52016-11-28 12:13:54 +00005547 /* snprintf returns the length of the string it would've written,
5548 * _excluding_ the NUL byte. So even being equal to the size of
5549 * our buffer is an error here. */
5550 if (len >= sizeof path)
5551 return NULL;
5552
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005553 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
5554 if (module) {
5555 weston_log("Module '%s' already loaded\n", path);
5556 dlclose(module);
5557 return NULL;
5558 }
5559
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005560 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04005561 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005562 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005563 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005564 return NULL;
5565 }
5566
5567 init = dlsym(module, entrypoint);
5568 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005569 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00005570 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005571 return NULL;
5572 }
5573
5574 return init;
5575}
5576
Giulio Camuffo459137b2014-10-11 23:56:24 +03005577
5578/** Destroys the compositor.
5579 *
5580 * This function cleans up the compositor state and destroys it.
5581 *
5582 * \param compositor The compositor to be destroyed.
5583 */
5584WL_EXPORT void
5585weston_compositor_destroy(struct weston_compositor *compositor)
5586{
5587 /* prevent further rendering while shutting down */
5588 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5589
5590 wl_signal_emit(&compositor->destroy_signal, compositor);
5591
5592 weston_compositor_xkb_destroy(compositor);
5593
Giulio Camuffo2d24e642015-10-03 16:25:15 +03005594 if (compositor->backend)
5595 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005596
5597 weston_plugin_api_destroy_list(compositor);
5598
Giulio Camuffo459137b2014-10-11 23:56:24 +03005599 free(compositor);
5600}
5601
5602/** Instruct the compositor to exit.
5603 *
5604 * This functions does not directly destroy the compositor object, it merely
5605 * command it to start the tear down process. It is not guaranteed that the
5606 * tear down will happen immediately.
5607 *
5608 * \param compositor The compositor to tear down.
5609 */
5610WL_EXPORT void
5611weston_compositor_exit(struct weston_compositor *compositor)
5612{
5613 compositor->exit(compositor);
5614}
5615
5616/** Return the user data stored in the compositor.
5617 *
5618 * This function returns the user data pointer set with user_data parameter
5619 * to the \ref weston_compositor_create function.
5620 */
5621WL_EXPORT void *
5622weston_compositor_get_user_data(struct weston_compositor *compositor)
5623{
5624 return compositor->user_data;
5625}
Pekka Paalanendd186732016-06-03 14:49:54 +03005626
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005627static const char * const backend_map[] = {
5628 [WESTON_BACKEND_DRM] = "drm-backend.so",
5629 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5630 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5631 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5632 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5633 [WESTON_BACKEND_X11] = "x11-backend.so",
5634};
5635
Pekka Paalanendd186732016-06-03 14:49:54 +03005636/** Load a backend into a weston_compositor
5637 *
5638 * A backend must be loaded to make a weston_compositor work. A backend
5639 * provides input and output capabilities, and determines the renderer to use.
5640 *
5641 * \param compositor A compositor that has not had a backend loaded yet.
5642 * \param backend Name of the backend file.
5643 * \param config_base A pointer to a backend-specific configuration
5644 * structure's 'base' member.
5645 *
5646 * \return 0 on success, or -1 on error.
5647 */
5648WL_EXPORT int
5649weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005650 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005651 struct weston_backend_config *config_base)
5652{
5653 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005654 struct weston_backend_config *config_base);
5655
Pekka Paalanend7e35112017-08-29 17:04:12 +03005656 if (compositor->backend) {
5657 weston_log("Error: attempt to load a backend when one is already loaded\n");
5658 return -1;
5659 }
5660
Quentin Glidic887c0182016-07-10 11:00:53 +02005661 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005662 return -1;
5663
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01005664 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005665 if (!backend_init)
5666 return -1;
5667
Pekka Paalanend7e35112017-08-29 17:04:12 +03005668 if (backend_init(compositor, config_base) < 0) {
5669 compositor->backend = NULL;
5670 return -1;
5671 }
5672
5673 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03005674}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005675
5676WL_EXPORT int
5677weston_compositor_load_xwayland(struct weston_compositor *compositor)
5678{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005679 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005680
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005681 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005682 if (!module_init)
5683 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005684 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005685 return -1;
5686 return 0;
5687}