blob: 98c5bd61e07ffbe3208ae8a18220979838101c53 [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
Alexander Larsson0b135062013-05-28 16:23:36 +020070weston_output_transform_scale_init(struct weston_output *output,
71 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020072
Rob Bradford27b17932013-06-26 18:08:46 +010073static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050074weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010075
Derek Foreman6ae7bc92014-11-04 10:47:33 -060076static void weston_mode_switch_finish(struct weston_output *output,
77 int mode_changed,
78 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080079{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020080 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020081 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020082 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060083 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020084
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020085 pixman_region32_init(&old_output_region);
86 pixman_region32_copy(&old_output_region, &output->region);
87
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020089 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020090
91 pixman_region32_init(&output->previous_damage);
92 pixman_region32_init_rect(&output->region, output->x, output->y,
93 output->width, output->height);
94
95 weston_output_update_matrix(output);
96
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020097 /* If a pointer falls outside the outputs new geometry, move it to its
98 * lower-right corner */
99 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500100 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200101 int32_t x, y;
102
103 if (!pointer)
104 continue;
105
106 x = wl_fixed_to_int(pointer->x);
107 y = wl_fixed_to_int(pointer->y);
108
109 if (!pixman_region32_contains_point(&old_output_region,
110 x, y, NULL) ||
111 pixman_region32_contains_point(&output->region,
112 x, y, NULL))
113 continue;
114
115 if (x >= output->x + output->width)
116 x = output->x + output->width - 1;
117 if (y >= output->y + output->height)
118 y = output->y + output->height - 1;
119
120 pointer->x = wl_fixed_from_int(x);
121 pointer->y = wl_fixed_from_int(y);
122 }
123
124 pixman_region32_fini(&old_output_region);
125
Derek Foremandd4cd332014-11-10 10:29:59 -0600126 if (!mode_changed && !scale_changed)
127 return;
128
Hardening57388e42013-09-18 23:56:36 +0200129 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600130 wl_resource_for_each(resource, &output->resource_list) {
131 if (mode_changed) {
132 wl_output_send_mode(resource,
133 output->current_mode->flags,
134 output->current_mode->width,
135 output->current_mode->height,
136 output->current_mode->refresh);
137 }
Hardening57388e42013-09-18 23:56:36 +0200138
Derek Foreman41bdc272014-11-05 13:26:57 -0600139 version = wl_resource_get_version(resource);
140 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600141 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200142
Derek Foreman41bdc272014-11-05 13:26:57 -0600143 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
144 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600145 }
146}
147
David Fort0de859e2016-05-27 23:22:57 +0200148
149static void
150weston_compositor_reflow_outputs(struct weston_compositor *compositor,
151 struct weston_output *resized_output, int delta_width);
152
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600153WL_EXPORT int
154weston_output_mode_set_native(struct weston_output *output,
155 struct weston_mode *mode,
156 int32_t scale)
157{
158 int ret;
159 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200160 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600161
162 if (!output->switch_mode)
163 return -1;
164
165 if (!output->original_mode) {
166 mode_changed = 1;
167 ret = output->switch_mode(output, mode);
168 if (ret < 0)
169 return ret;
170 if (output->current_scale != scale) {
171 scale_changed = 1;
172 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200173 }
174 }
175
David Fort0de859e2016-05-27 23:22:57 +0200176 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600177 output->native_mode = mode;
178 output->native_scale = scale;
179
180 weston_mode_switch_finish(output, mode_changed, scale_changed);
181
David Fort0de859e2016-05-27 23:22:57 +0200182 if (mode_changed || scale_changed) {
183 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
184
185 wl_signal_emit(&output->compositor->output_resized_signal, output);
186 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600187 return 0;
188}
189
190WL_EXPORT int
191weston_output_mode_switch_to_native(struct weston_output *output)
192{
193 int ret;
194 int mode_changed = 0, scale_changed = 0;
195
196 if (!output->switch_mode)
197 return -1;
198
199 if (!output->original_mode) {
200 weston_log("already in the native mode\n");
201 return -1;
202 }
203 /* the non fullscreen clients haven't seen a mode set since we
204 * switched into a temporary, so we need to notify them if the
205 * mode at that time is different from the native mode now.
206 */
207 mode_changed = (output->original_mode != output->native_mode);
208 scale_changed = (output->original_scale != output->native_scale);
209
210 ret = output->switch_mode(output, output->native_mode);
211 if (ret < 0)
212 return ret;
213
214 output->current_scale = output->native_scale;
215
216 output->original_mode = NULL;
217 output->original_scale = 0;
218
219 weston_mode_switch_finish(output, mode_changed, scale_changed);
220
221 return 0;
222}
223
224WL_EXPORT int
225weston_output_mode_switch_to_temporary(struct weston_output *output,
226 struct weston_mode *mode,
227 int32_t scale)
228{
229 int ret;
230
231 if (!output->switch_mode)
232 return -1;
233
234 /* original_mode is the last mode non full screen clients have seen,
235 * so we shouldn't change it if we already have one set.
236 */
237 if (!output->original_mode) {
238 output->original_mode = output->native_mode;
239 output->original_scale = output->native_scale;
240 }
241 ret = output->switch_mode(output, mode);
242 if (ret < 0)
243 return ret;
244
245 output->current_scale = scale;
246
247 weston_mode_switch_finish(output, 0, 0);
248
249 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800250}
251
Benjamin Franzke06286262011-05-06 19:12:33 +0200252static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300253region_init_infinite(pixman_region32_t *region)
254{
255 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
256 UINT32_MAX, UINT32_MAX);
257}
258
Pekka Paalanene67858b2013-04-25 13:57:42 +0300259static struct weston_subsurface *
260weston_surface_to_subsurface(struct weston_surface *surface);
261
Jason Ekstranda7af7042013-10-12 22:38:11 -0500262WL_EXPORT struct weston_view *
263weston_view_create(struct weston_surface *surface)
264{
265 struct weston_view *view;
266
Bryce Harringtonde16d892014-11-20 22:21:57 -0800267 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500268 if (view == NULL)
269 return NULL;
270
271 view->surface = surface;
272
Jason Ekstranda7af7042013-10-12 22:38:11 -0500273 /* Assign to surface */
274 wl_list_insert(&surface->views, &view->surface_link);
275
276 wl_signal_init(&view->destroy_signal);
277 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300278 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279
Jason Ekstranda7af7042013-10-12 22:38:11 -0500280 pixman_region32_init(&view->clip);
281
282 view->alpha = 1.0;
283 pixman_region32_init(&view->transform.opaque);
284
285 wl_list_init(&view->geometry.transformation_list);
286 wl_list_insert(&view->geometry.transformation_list,
287 &view->transform.position.link);
288 weston_matrix_init(&view->transform.position.matrix);
289 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200290 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500291 pixman_region32_init(&view->transform.boundingbox);
292 view->transform.dirty = 1;
293
Jason Ekstranda7af7042013-10-12 22:38:11 -0500294 return view;
295}
296
Jason Ekstrand108865d2014-06-26 10:04:49 -0700297struct weston_frame_callback {
298 struct wl_resource *resource;
299 struct wl_list link;
300};
301
Pekka Paalanen133e4392014-09-23 22:08:46 -0400302struct weston_presentation_feedback {
303 struct wl_resource *resource;
304
305 /* XXX: could use just wl_resource_get_link() instead */
306 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200307
308 /* The per-surface feedback flags */
309 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400310};
311
312static void
313weston_presentation_feedback_discard(
314 struct weston_presentation_feedback *feedback)
315{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200316 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400317 wl_resource_destroy(feedback->resource);
318}
319
320static void
321weston_presentation_feedback_discard_list(struct wl_list *list)
322{
323 struct weston_presentation_feedback *feedback, *tmp;
324
325 wl_list_for_each_safe(feedback, tmp, list, link)
326 weston_presentation_feedback_discard(feedback);
327}
328
329static void
330weston_presentation_feedback_present(
331 struct weston_presentation_feedback *feedback,
332 struct weston_output *output,
333 uint32_t refresh_nsec,
334 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200335 uint64_t seq,
336 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400337{
338 struct wl_client *client = wl_resource_get_client(feedback->resource);
339 struct wl_resource *o;
340 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400341
342 wl_resource_for_each(o, &output->resource_list) {
343 if (wl_resource_get_client(o) != client)
344 continue;
345
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200346 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400347 }
348
349 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200350 wp_presentation_feedback_send_presented(feedback->resource,
351 secs >> 32, secs & 0xffffffff,
352 ts->tv_nsec,
353 refresh_nsec,
354 seq >> 32, seq & 0xffffffff,
355 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400356 wl_resource_destroy(feedback->resource);
357}
358
359static void
360weston_presentation_feedback_present_list(struct wl_list *list,
361 struct weston_output *output,
362 uint32_t refresh_nsec,
363 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200364 uint64_t seq,
365 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400366{
367 struct weston_presentation_feedback *feedback, *tmp;
368
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200369 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200370 wl_list_empty(list));
371
Pekka Paalanen133e4392014-09-23 22:08:46 -0400372 wl_list_for_each_safe(feedback, tmp, list, link)
373 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200374 refresh_nsec, ts, seq,
375 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400376}
377
Jason Ekstrand7b982072014-05-20 14:33:03 -0500378static void
379surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
380{
381 struct weston_surface_state *state =
382 container_of(listener, struct weston_surface_state,
383 buffer_destroy_listener);
384
385 state->buffer = NULL;
386}
387
388static void
389weston_surface_state_init(struct weston_surface_state *state)
390{
391 state->newly_attached = 0;
392 state->buffer = NULL;
393 state->buffer_destroy_listener.notify =
394 surface_state_handle_buffer_destroy;
395 state->sx = 0;
396 state->sy = 0;
397
Derek Foreman152254b2015-11-26 14:17:48 -0600398 pixman_region32_init(&state->damage_surface);
399 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500400 pixman_region32_init(&state->opaque);
401 region_init_infinite(&state->input);
402
403 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400404 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500405
406 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
407 state->buffer_viewport.buffer.scale = 1;
408 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
409 state->buffer_viewport.surface.width = -1;
410 state->buffer_viewport.changed = 0;
411}
412
413static void
414weston_surface_state_fini(struct weston_surface_state *state)
415{
416 struct weston_frame_callback *cb, *next;
417
418 wl_list_for_each_safe(cb, next,
419 &state->frame_callback_list, link)
420 wl_resource_destroy(cb->resource);
421
Pekka Paalanen133e4392014-09-23 22:08:46 -0400422 weston_presentation_feedback_discard_list(&state->feedback_list);
423
Jason Ekstrand7b982072014-05-20 14:33:03 -0500424 pixman_region32_fini(&state->input);
425 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600426 pixman_region32_fini(&state->damage_surface);
427 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500428
429 if (state->buffer)
430 wl_list_remove(&state->buffer_destroy_listener.link);
431 state->buffer = NULL;
432}
433
434static void
435weston_surface_state_set_buffer(struct weston_surface_state *state,
436 struct weston_buffer *buffer)
437{
438 if (state->buffer == buffer)
439 return;
440
441 if (state->buffer)
442 wl_list_remove(&state->buffer_destroy_listener.link);
443 state->buffer = buffer;
444 if (state->buffer)
445 wl_signal_add(&state->buffer->destroy_signal,
446 &state->buffer_destroy_listener);
447}
448
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500449WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500450weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500451{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500452 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400453
Bryce Harringtonde16d892014-11-20 22:21:57 -0800454 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400455 if (surface == NULL)
456 return NULL;
457
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500458 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800459 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500460
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500461 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200462 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400463
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200464 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
465 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200466 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
467 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500468
469 weston_surface_state_init(&surface->pending);
470
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400471 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500472 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300473 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400474
Jason Ekstranda7af7042013-10-12 22:38:11 -0500475 wl_list_init(&surface->views);
476
477 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400478 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500479
Pekka Paalanene67858b2013-04-25 13:57:42 +0300480 wl_list_init(&surface->subsurface_list);
481 wl_list_init(&surface->subsurface_list_pending);
482
Jason Ekstrand1e059042014-10-16 10:55:19 -0500483 weston_matrix_init(&surface->buffer_to_surface_matrix);
484 weston_matrix_init(&surface->surface_to_buffer_matrix);
485
Jonas Ådahld3414f22016-07-22 17:56:31 +0800486 wl_list_init(&surface->pointer_constraints);
487
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400488 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500489}
490
Alex Wu8811bf92012-02-28 18:07:54 +0800491WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500492weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200493 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500494{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100495 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500496}
497
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400498WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500499weston_view_to_global_float(struct weston_view *view,
500 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200501{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500502 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200503 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
504
Jason Ekstranda7af7042013-10-12 22:38:11 -0500505 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200506
507 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200508 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700509 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200510 v.f[3]);
511 *x = 0;
512 *y = 0;
513 return;
514 }
515
516 *x = v.f[0] / v.f[3];
517 *y = v.f[1] / v.f[3];
518 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500519 *x = sx + view->geometry.x;
520 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200521 }
522}
523
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500524WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200525weston_transformed_coord(int width, int height,
526 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200527 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200528 float sx, float sy, float *bx, float *by)
529{
530 switch (transform) {
531 case WL_OUTPUT_TRANSFORM_NORMAL:
532 default:
533 *bx = sx;
534 *by = sy;
535 break;
536 case WL_OUTPUT_TRANSFORM_FLIPPED:
537 *bx = width - sx;
538 *by = sy;
539 break;
540 case WL_OUTPUT_TRANSFORM_90:
541 *bx = height - sy;
542 *by = sx;
543 break;
544 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
545 *bx = height - sy;
546 *by = width - sx;
547 break;
548 case WL_OUTPUT_TRANSFORM_180:
549 *bx = width - sx;
550 *by = height - sy;
551 break;
552 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
553 *bx = sx;
554 *by = height - sy;
555 break;
556 case WL_OUTPUT_TRANSFORM_270:
557 *bx = sy;
558 *by = width - sx;
559 break;
560 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
561 *bx = sy;
562 *by = sx;
563 break;
564 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200565
566 *bx *= scale;
567 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200568}
569
570WL_EXPORT pixman_box32_t
571weston_transformed_rect(int width, int height,
572 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200573 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200574 pixman_box32_t rect)
575{
576 float x1, x2, y1, y2;
577
578 pixman_box32_t ret;
579
Alexander Larsson4ea95522013-05-22 14:41:37 +0200580 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200581 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200582 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200583 rect.x2, rect.y2, &x2, &y2);
584
585 if (x1 <= x2) {
586 ret.x1 = x1;
587 ret.x2 = x2;
588 } else {
589 ret.x1 = x2;
590 ret.x2 = x1;
591 }
592
593 if (y1 <= y2) {
594 ret.y1 = y1;
595 ret.y2 = y2;
596 } else {
597 ret.y1 = y2;
598 ret.y2 = y1;
599 }
600
601 return ret;
602}
603
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600604/** Transform a region by a matrix, restricted to axis-aligned transformations
605 *
606 * Warning: This function does not work for projective, affine, or matrices
607 * that encode arbitrary rotations. Only 90-degree step rotations are
608 * supported.
609 */
610WL_EXPORT void
611weston_matrix_transform_region(pixman_region32_t *dest,
612 struct weston_matrix *matrix,
613 pixman_region32_t *src)
614{
615 pixman_box32_t *src_rects, *dest_rects;
616 int nrects, i;
617
618 src_rects = pixman_region32_rectangles(src, &nrects);
619 dest_rects = malloc(nrects * sizeof(*dest_rects));
620 if (!dest_rects)
621 return;
622
623 for (i = 0; i < nrects; i++) {
624 struct weston_vector vec1 = {{
625 src_rects[i].x1, src_rects[i].y1, 0, 1
626 }};
627 weston_matrix_transform(matrix, &vec1);
628 vec1.f[0] /= vec1.f[3];
629 vec1.f[1] /= vec1.f[3];
630
631 struct weston_vector vec2 = {{
632 src_rects[i].x2, src_rects[i].y2, 0, 1
633 }};
634 weston_matrix_transform(matrix, &vec2);
635 vec2.f[0] /= vec2.f[3];
636 vec2.f[1] /= vec2.f[3];
637
638 if (vec1.f[0] < vec2.f[0]) {
639 dest_rects[i].x1 = floor(vec1.f[0]);
640 dest_rects[i].x2 = ceil(vec2.f[0]);
641 } else {
642 dest_rects[i].x1 = floor(vec2.f[0]);
643 dest_rects[i].x2 = ceil(vec1.f[0]);
644 }
645
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600646 if (vec1.f[1] < vec2.f[1]) {
647 dest_rects[i].y1 = floor(vec1.f[1]);
648 dest_rects[i].y2 = ceil(vec2.f[1]);
649 } else {
650 dest_rects[i].y1 = floor(vec2.f[1]);
651 dest_rects[i].y2 = ceil(vec1.f[1]);
652 }
653 }
654
655 pixman_region32_clear(dest);
656 pixman_region32_init_rects(dest, dest_rects, nrects);
657 free(dest_rects);
658}
659
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200660WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500661weston_transformed_region(int width, int height,
662 enum wl_output_transform transform,
663 int32_t scale,
664 pixman_region32_t *src, pixman_region32_t *dest)
665{
666 pixman_box32_t *src_rects, *dest_rects;
667 int nrects, i;
668
669 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
670 if (src != dest)
671 pixman_region32_copy(dest, src);
672 return;
673 }
674
675 src_rects = pixman_region32_rectangles(src, &nrects);
676 dest_rects = malloc(nrects * sizeof(*dest_rects));
677 if (!dest_rects)
678 return;
679
680 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
681 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
682 } else {
683 for (i = 0; i < nrects; i++) {
684 switch (transform) {
685 default:
686 case WL_OUTPUT_TRANSFORM_NORMAL:
687 dest_rects[i].x1 = src_rects[i].x1;
688 dest_rects[i].y1 = src_rects[i].y1;
689 dest_rects[i].x2 = src_rects[i].x2;
690 dest_rects[i].y2 = src_rects[i].y2;
691 break;
692 case WL_OUTPUT_TRANSFORM_90:
693 dest_rects[i].x1 = height - src_rects[i].y2;
694 dest_rects[i].y1 = src_rects[i].x1;
695 dest_rects[i].x2 = height - src_rects[i].y1;
696 dest_rects[i].y2 = src_rects[i].x2;
697 break;
698 case WL_OUTPUT_TRANSFORM_180:
699 dest_rects[i].x1 = width - src_rects[i].x2;
700 dest_rects[i].y1 = height - src_rects[i].y2;
701 dest_rects[i].x2 = width - src_rects[i].x1;
702 dest_rects[i].y2 = height - src_rects[i].y1;
703 break;
704 case WL_OUTPUT_TRANSFORM_270:
705 dest_rects[i].x1 = src_rects[i].y1;
706 dest_rects[i].y1 = width - src_rects[i].x2;
707 dest_rects[i].x2 = src_rects[i].y2;
708 dest_rects[i].y2 = width - src_rects[i].x1;
709 break;
710 case WL_OUTPUT_TRANSFORM_FLIPPED:
711 dest_rects[i].x1 = width - src_rects[i].x2;
712 dest_rects[i].y1 = src_rects[i].y1;
713 dest_rects[i].x2 = width - src_rects[i].x1;
714 dest_rects[i].y2 = src_rects[i].y2;
715 break;
716 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
717 dest_rects[i].x1 = height - src_rects[i].y2;
718 dest_rects[i].y1 = width - src_rects[i].x2;
719 dest_rects[i].x2 = height - src_rects[i].y1;
720 dest_rects[i].y2 = width - src_rects[i].x1;
721 break;
722 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
723 dest_rects[i].x1 = src_rects[i].x1;
724 dest_rects[i].y1 = height - src_rects[i].y2;
725 dest_rects[i].x2 = src_rects[i].x2;
726 dest_rects[i].y2 = height - src_rects[i].y1;
727 break;
728 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
729 dest_rects[i].x1 = src_rects[i].y1;
730 dest_rects[i].y1 = src_rects[i].x1;
731 dest_rects[i].x2 = src_rects[i].y2;
732 dest_rects[i].y2 = src_rects[i].x2;
733 break;
734 }
735 }
736 }
737
738 if (scale != 1) {
739 for (i = 0; i < nrects; i++) {
740 dest_rects[i].x1 *= scale;
741 dest_rects[i].x2 *= scale;
742 dest_rects[i].y1 *= scale;
743 dest_rects[i].y2 *= scale;
744 }
745 }
746
747 pixman_region32_clear(dest);
748 pixman_region32_init_rects(dest, dest_rects, nrects);
749 free(dest_rects);
750}
751
Jonny Lamb74130762013-11-26 18:19:46 +0100752static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300753viewport_surface_to_buffer(struct weston_surface *surface,
754 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100755{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200756 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200757 double src_width, src_height;
758 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200759
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200760 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
761 if (vp->surface.width == -1) {
762 *bx = sx;
763 *by = sy;
764 return;
765 }
Jonny Lamb74130762013-11-26 18:19:46 +0100766
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200767 src_x = 0.0;
768 src_y = 0.0;
769 src_width = surface->width_from_buffer;
770 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100771 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200772 src_x = wl_fixed_to_double(vp->buffer.src_x);
773 src_y = wl_fixed_to_double(vp->buffer.src_y);
774 src_width = wl_fixed_to_double(vp->buffer.src_width);
775 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100776 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200777
778 *bx = sx * src_width / surface->width + src_x;
779 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100780}
781
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500782WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200783weston_surface_to_buffer_float(struct weston_surface *surface,
784 float sx, float sy, float *bx, float *by)
785{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200786 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
787
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300788 /* first transform coordinates if the viewport is set */
789 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100790
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500791 weston_transformed_coord(surface->width_from_buffer,
792 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200793 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100794 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200795}
796
Derek Foreman99739672015-12-03 16:38:11 -0600797/** Transform a rectangle from surface coordinates to buffer coordinates
798 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300799 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600800 * from.
801 * \param rect The rectangle to transform.
802 * \return The transformed rectangle.
803 *
804 * Viewport and buffer transformations can only do translation, scaling,
805 * and rotations in 90-degree steps. Therefore the only loss in the
806 * conversion is coordinate rounding.
807 *
808 * However, some coordinate rounding takes place as an intermediate
809 * step before the buffer scale factor is applied, so the rectangle
810 * boundary may not be exactly as expected.
811 *
812 * This is OK for damage tracking since a little extra coverage is
813 * not a problem.
814 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200815WL_EXPORT pixman_box32_t
816weston_surface_to_buffer_rect(struct weston_surface *surface,
817 pixman_box32_t rect)
818{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200819 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100820 float xf, yf;
821
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300822 /* first transform box coordinates if the viewport is set */
823 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100824 rect.x1 = floorf(xf);
825 rect.y1 = floorf(yf);
826
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300827 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600828 rect.x2 = ceilf(xf);
829 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100830
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500831 return weston_transformed_rect(surface->width_from_buffer,
832 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200833 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200834 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200835}
836
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200837/** Transform a region from surface coordinates to buffer coordinates
838 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300839 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200840 * from.
841 * \param surface_region[in] The region in surface coordinates.
842 * \param buffer_region[out] The region converted to buffer coordinates.
843 *
844 * Buffer_region must be init'd, but will be completely overwritten.
845 *
846 * Viewport and buffer transformations can only do translation, scaling,
847 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600848 * conversion is from the coordinate rounding that takes place in
849 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200850 */
851WL_EXPORT void
852weston_surface_to_buffer_region(struct weston_surface *surface,
853 pixman_region32_t *surface_region,
854 pixman_region32_t *buffer_region)
855{
856 pixman_box32_t *src_rects, *dest_rects;
857 int nrects, i;
858
859 src_rects = pixman_region32_rectangles(surface_region, &nrects);
860 dest_rects = malloc(nrects * sizeof(*dest_rects));
861 if (!dest_rects)
862 return;
863
864 for (i = 0; i < nrects; i++) {
865 dest_rects[i] = weston_surface_to_buffer_rect(surface,
866 src_rects[i]);
867 }
868
869 pixman_region32_fini(buffer_region);
870 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
871 free(dest_rects);
872}
873
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200874WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500875weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400876 struct weston_plane *plane)
877{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500878 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400879 return;
880
Jason Ekstranda7af7042013-10-12 22:38:11 -0500881 weston_view_damage_below(view);
882 view->plane = plane;
883 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400884}
885
Pekka Paalanen51723d52015-02-17 13:10:01 +0200886/** Inflict damage on the plane where the view is visible.
887 *
888 * \param view The view that causes the damage.
889 *
890 * If the view is currently on a plane (including the primary plane),
891 * take the view's boundingbox, subtract all the opaque views that cover it,
892 * and add the remaining region as damage to the plane. This corresponds
893 * to the damage inflicted to the plane if this view disappeared.
894 *
895 * A repaint is scheduled for this view.
896 *
897 * The region of all opaque views covering this view is stored in
898 * weston_view::clip and updated by view_accumulate_damage() during
899 * weston_output_repaint(). Specifically, that region matches the
900 * scenegraph as it was last painted.
901 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400902WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500903weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200904{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500905 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200906
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500907 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200908 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500909 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800910 if (view->plane)
911 pixman_region32_union(&view->plane->damage,
912 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500913 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800914 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200915}
916
Bryce Harrington3f650b82015-12-23 11:01:58 -0800917/**
918 * \param es The surface
919 * \param mask The new set of outputs for the surface
920 *
921 * Sets the surface's set of outputs to the ones specified by
922 * the new output mask provided. Identifies the outputs that
923 * have changed, the posts enter and leave events for these
924 * outputs as appropriate.
925 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400926static void
927weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
928{
929 uint32_t different = es->output_mask ^ mask;
930 uint32_t entered = mask & different;
931 uint32_t left = es->output_mask & different;
932 struct weston_output *output;
933 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500934 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400935
936 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500937 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400938 return;
939 if (different == 0)
940 return;
941
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500942 client = wl_resource_get_client(es->resource);
943
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400944 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800945 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400946 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500947 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400948 client);
949 if (resource == NULL)
950 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800951 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500952 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800953 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500954 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400955 }
956}
957
Bryce Harrington3f650b82015-12-23 11:01:58 -0800958/** Recalculate which output(s) the surface has views displayed on
959 *
960 * \param es The surface to remap to outputs
961 *
962 * Finds the output that is showing the largest amount of one
963 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300964 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800965 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300966 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800967 * in the output_mask for the surface.
968 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400969static void
970weston_surface_assign_output(struct weston_surface *es)
971{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 struct weston_output *new_output;
973 struct weston_view *view;
974 pixman_region32_t region;
975 uint32_t max, area, mask;
976 pixman_box32_t *e;
977
978 new_output = NULL;
979 max = 0;
980 mask = 0;
981 pixman_region32_init(&region);
982 wl_list_for_each(view, &es->views, surface_link) {
983 if (!view->output)
984 continue;
985
986 pixman_region32_intersect(&region, &view->transform.boundingbox,
987 &view->output->region);
988
989 e = pixman_region32_extents(&region);
990 area = (e->x2 - e->x1) * (e->y2 - e->y1);
991
992 mask |= view->output_mask;
993
994 if (area >= max) {
995 new_output = view->output;
996 max = area;
997 }
998 }
999 pixman_region32_fini(&region);
1000
1001 es->output = new_output;
1002 weston_surface_update_output_mask(es, mask);
1003}
1004
Bryce Harrington3f650b82015-12-23 11:01:58 -08001005/** Recalculate which output(s) the view is displayed on
1006 *
1007 * \param ev The view to remap to outputs
1008 *
1009 * Identifies the set of outputs that the view is visible on,
1010 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001011 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001012 *
1013 * Also does the same for the view's surface. See
1014 * weston_surface_assign_output().
1015 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016static void
1017weston_view_assign_output(struct weston_view *ev)
1018{
1019 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001020 struct weston_output *output, *new_output;
1021 pixman_region32_t region;
1022 uint32_t max, area, mask;
1023 pixman_box32_t *e;
1024
1025 new_output = NULL;
1026 max = 0;
1027 mask = 0;
1028 pixman_region32_init(&region);
1029 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001030 if (output->destroying)
1031 continue;
1032
Jason Ekstranda7af7042013-10-12 22:38:11 -05001033 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001034 &output->region);
1035
1036 e = pixman_region32_extents(&region);
1037 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1038
1039 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001040 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001041
1042 if (area >= max) {
1043 new_output = output;
1044 max = area;
1045 }
1046 }
1047 pixman_region32_fini(&region);
1048
Jason Ekstranda7af7042013-10-12 22:38:11 -05001049 ev->output = new_output;
1050 ev->output_mask = mask;
1051
1052 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001053}
1054
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001055static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001056weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1057 int from_x, int from_y, int *to_x, int *to_y)
1058{
1059 float x, y;
1060
1061 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1062 weston_view_from_global_float(to, x, y, &x, &y);
1063
1064 *to_x = round(x);
1065 *to_y = round(y);
1066}
1067
1068static void
1069weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1070{
1071 pixman_box32_t *a;
1072 pixman_box32_t b;
1073
1074 a = pixman_region32_extents(&from->geometry.scissor);
1075
1076 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1077 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1078
1079 pixman_region32_fini(&to->geometry.scissor);
1080 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1081}
1082
1083static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001084view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001085 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001086{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001087 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1088 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001089 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001090 { inbox->x1, inbox->y1 },
1091 { inbox->x1, inbox->y2 },
1092 { inbox->x2, inbox->y1 },
1093 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001094 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001095 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001096 int i;
1097
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001098 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001099 /* avoid rounding empty bbox to 1x1 */
1100 pixman_region32_init(bbox);
1101 return;
1102 }
1103
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001104 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001105 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001106 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001107 if (x < min_x)
1108 min_x = x;
1109 if (x > max_x)
1110 max_x = x;
1111 if (y < min_y)
1112 min_y = y;
1113 if (y > max_y)
1114 max_y = y;
1115 }
1116
Pekka Paalanen219b9822012-02-08 15:38:37 +02001117 int_x = floorf(min_x);
1118 int_y = floorf(min_y);
1119 pixman_region32_init_rect(bbox, int_x, int_y,
1120 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001121}
1122
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001123static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001125{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001126 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001127
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001128 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001129 view->geometry.x = roundf(view->geometry.x);
1130 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001131
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001132 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1134 view->transform.position.matrix.d[12] = view->geometry.x;
1135 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001136
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001138
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 view->transform.inverse = view->transform.position.matrix;
1140 view->transform.inverse.d[12] = -view->geometry.x;
1141 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001142
Jason Ekstranda7af7042013-10-12 22:38:11 -05001143 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001144 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001145 view->surface->width,
1146 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001147 if (view->geometry.scissor_enabled)
1148 pixman_region32_intersect(&view->transform.boundingbox,
1149 &view->transform.boundingbox,
1150 &view->geometry.scissor);
1151
1152 pixman_region32_translate(&view->transform.boundingbox,
1153 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001154
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 if (view->alpha == 1.0) {
1156 pixman_region32_copy(&view->transform.opaque,
1157 &view->surface->opaque);
1158 pixman_region32_translate(&view->transform.opaque,
1159 view->geometry.x,
1160 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001161 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001162}
1163
1164static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001166{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 struct weston_view *parent = view->geometry.parent;
1168 struct weston_matrix *matrix = &view->transform.matrix;
1169 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001170 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001171 pixman_region32_t surfregion;
1172 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001173
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001175
1176 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001177 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1178 view->transform.position.matrix.d[12] = view->geometry.x;
1179 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001180
1181 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001183 weston_matrix_multiply(matrix, &tform->matrix);
1184
Pekka Paalanen483243f2013-03-08 14:56:50 +02001185 if (parent)
1186 weston_matrix_multiply(matrix, &parent->transform.matrix);
1187
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001188 if (weston_matrix_invert(inverse, matrix) < 0) {
1189 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190 weston_log("error: weston_view %p"
1191 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001192 return -1;
1193 }
1194
Pekka Paalanen380adf52015-02-16 14:39:11 +02001195 pixman_region32_init_rect(&surfregion, 0, 0,
1196 view->surface->width, view->surface->height);
1197 if (view->geometry.scissor_enabled)
1198 pixman_region32_intersect(&surfregion, &surfregion,
1199 &view->geometry.scissor);
1200 surfbox = pixman_region32_extents(&surfregion);
1201
1202 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1203 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001204
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001205 return 0;
1206}
1207
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001208static struct weston_layer *
1209get_view_layer(struct weston_view *view)
1210{
1211 if (view->parent_view)
1212 return get_view_layer(view->parent_view);
1213 return view->layer_link.layer;
1214}
1215
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001216WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001218{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001220 struct weston_layer *layer;
1221 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001222
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001224 return;
1225
Pekka Paalanen483243f2013-03-08 14:56:50 +02001226 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001228
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001230
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001232
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 pixman_region32_fini(&view->transform.boundingbox);
1234 pixman_region32_fini(&view->transform.opaque);
1235 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001236
Pekka Paalanencd403622012-01-25 13:37:39 +02001237 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001238 if (view->geometry.transformation_list.next ==
1239 &view->transform.position.link &&
1240 view->geometry.transformation_list.prev ==
1241 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001242 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001244 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 if (weston_view_update_transform_enable(view) < 0)
1246 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001247 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001248
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001249 layer = get_view_layer(view);
1250 if (layer) {
1251 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001252 pixman_region32_intersect(&view->transform.boundingbox,
1253 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001254 pixman_region32_intersect(&view->transform.opaque,
1255 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001256 pixman_region32_fini(&mask);
1257 }
1258
Pekka Paalanen380adf52015-02-16 14:39:11 +02001259 if (parent) {
1260 if (parent->geometry.scissor_enabled) {
1261 view->geometry.scissor_enabled = true;
1262 weston_view_transfer_scissor(parent, view);
1263 } else {
1264 view->geometry.scissor_enabled = false;
1265 }
1266 }
1267
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001269
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001271
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 wl_signal_emit(&view->surface->compositor->transform_signal,
1273 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001274}
1275
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001276WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001278{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001280
1281 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 * The invariant: if view->geometry.dirty, then all views
1283 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001284 * Corollary: if not parent->geometry.dirty, then all ancestors
1285 * are not dirty.
1286 */
1287
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001289 return;
1290
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001292
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001294 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001296}
1297
1298WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299weston_view_to_global_fixed(struct weston_view *view,
1300 wl_fixed_t vx, wl_fixed_t vy,
1301 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001302{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001303 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001304
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 weston_view_to_global_float(view,
1306 wl_fixed_to_double(vx),
1307 wl_fixed_to_double(vy),
1308 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001309 *x = wl_fixed_from_double(xf);
1310 *y = wl_fixed_from_double(yf);
1311}
1312
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001313WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314weston_view_from_global_float(struct weston_view *view,
1315 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001316{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001318 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1319
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001321
1322 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001323 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001325 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326 *vx = 0;
1327 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001328 return;
1329 }
1330
Jason Ekstranda7af7042013-10-12 22:38:11 -05001331 *vx = v.f[0] / v.f[3];
1332 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001333 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 *vx = x - view->geometry.x;
1335 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001336 }
1337}
1338
1339WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340weston_view_from_global_fixed(struct weston_view *view,
1341 wl_fixed_t x, wl_fixed_t y,
1342 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001343{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001345
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346 weston_view_from_global_float(view,
1347 wl_fixed_to_double(x),
1348 wl_fixed_to_double(y),
1349 &vxf, &vyf);
1350 *vx = wl_fixed_from_double(vxf);
1351 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001352}
1353
1354WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355weston_view_from_global(struct weston_view *view,
1356 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001357{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1361 *vx = floorf(vxf);
1362 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001363}
1364
Bryce Harrington3f650b82015-12-23 11:01:58 -08001365/**
1366 * \param surface The surface to be repainted
1367 *
1368 * Marks the output(s) that the surface is shown on as needing to be
1369 * repainted. See weston_output_schedule_repaint().
1370 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001371WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001372weston_surface_schedule_repaint(struct weston_surface *surface)
1373{
1374 struct weston_output *output;
1375
1376 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001377 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001378 weston_output_schedule_repaint(output);
1379}
1380
Bryce Harrington3f650b82015-12-23 11:01:58 -08001381/**
1382 * \param view The view to be repainted
1383 *
1384 * Marks the output(s) that the view is shown on as needing to be
1385 * repainted. See weston_output_schedule_repaint().
1386 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001387WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388weston_view_schedule_repaint(struct weston_view *view)
1389{
1390 struct weston_output *output;
1391
1392 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001393 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001394 weston_output_schedule_repaint(output);
1395}
1396
Pekka Paalanene508ce62015-02-19 13:59:55 +02001397/**
1398 * XXX: This function does it the wrong way.
1399 * surface->damage is the damage from the client, and causes
1400 * surface_flush_damage() to copy pixels. No window management action can
1401 * cause damage to the client-provided content, warranting re-upload!
1402 *
1403 * Instead of surface->damage, this function should record the damage
1404 * with all the views for this surface to avoid extraneous texture
1405 * uploads.
1406 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001408weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001409{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001410 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 0, 0, surface->width,
1412 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001413
Kristian Høgsberg98238702012-08-03 16:29:12 -04001414 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001415}
1416
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001417WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001419{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001420 if (view->geometry.x == x && view->geometry.y == y)
1421 return;
1422
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423 view->geometry.x = x;
1424 view->geometry.y = y;
1425 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001426}
1427
Pekka Paalanen483243f2013-03-08 14:56:50 +02001428static void
1429transform_parent_handle_parent_destroy(struct wl_listener *listener,
1430 void *data)
1431{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 struct weston_view *view =
1433 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001434 geometry.parent_destroy_listener);
1435
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001437}
1438
1439WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001441 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001442{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443 if (view->geometry.parent) {
1444 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1445 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001446
1447 if (!parent)
1448 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001449 }
1450
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001452
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001454 transform_parent_handle_parent_destroy;
1455 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001456 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001458 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001460 }
1461
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 weston_view_geometry_dirty(view);
1463}
1464
Pekka Paalanen380adf52015-02-16 14:39:11 +02001465/** Set a clip mask rectangle on a view
1466 *
1467 * \param view The view to set the clip mask on.
1468 * \param x Top-left corner X coordinate of the clip rectangle.
1469 * \param y Top-left corner Y coordinate of the clip rectangle.
1470 * \param width Width of the clip rectangle, non-negative.
1471 * \param height Height of the clip rectangle, non-negative.
1472 *
1473 * A shell may set a clip mask rectangle on a view. Everything outside
1474 * the rectangle is cut away for input and output purposes: it is
1475 * not drawn and cannot be hit by hit-test based input like pointer
1476 * motion or touch-downs. Everything inside the rectangle will behave
1477 * normally. Clients are unaware of clipping.
1478 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001479 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001480 * mask rectangle does not affect the view position, the view is positioned
1481 * as it would be without a clip. The clip also does not change
1482 * weston_surface::width,height.
1483 *
1484 * The clip mask rectangle is part of transformation inheritance
1485 * (weston_view_set_transform_parent()). A clip set in the root of the
1486 * transformation inheritance tree will affect all views in the tree.
1487 * A clip can be set only on the root view. Attempting to set a clip
1488 * on view that has a transformation parent will fail. Assigning a parent
1489 * to a view that has a clip set will cause the clip to be forgotten.
1490 *
1491 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1492 * on the additional transformations in the child views. These transformations
1493 * may not rotate the coordinate axes, i.e., only translation and scaling
1494 * are allowed. Violating this restriction causes the clipping to malfunction.
1495 * Furthermore, using scaling may cause rounding errors in child clipping.
1496 *
1497 * The clip mask rectangle is not automatically adjusted based on
1498 * wl_surface.attach dx and dy arguments.
1499 *
1500 * A clip mask rectangle can be set only if the compositor capability
1501 * WESTON_CAP_VIEW_CLIP_MASK is present.
1502 *
1503 * This function sets the clip mask rectangle and schedules a repaint for
1504 * the view.
1505 */
1506WL_EXPORT void
1507weston_view_set_mask(struct weston_view *view,
1508 int x, int y, int width, int height)
1509{
1510 struct weston_compositor *compositor = view->surface->compositor;
1511
1512 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1513 weston_log("%s not allowed without capability!\n", __func__);
1514 return;
1515 }
1516
1517 if (view->geometry.parent) {
1518 weston_log("view %p has a parent, clip forbidden!\n", view);
1519 return;
1520 }
1521
1522 if (width < 0 || height < 0) {
1523 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1524 x, y, width, height);
1525 return;
1526 }
1527
1528 pixman_region32_fini(&view->geometry.scissor);
1529 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1530 view->geometry.scissor_enabled = true;
1531 weston_view_geometry_dirty(view);
1532 weston_view_schedule_repaint(view);
1533}
1534
1535/** Remove the clip mask from a view
1536 *
1537 * \param view The view to remove the clip mask from.
1538 *
1539 * Removed the clip mask rectangle and schedules a repaint.
1540 *
1541 * \sa weston_view_set_mask
1542 */
1543WL_EXPORT void
1544weston_view_set_mask_infinite(struct weston_view *view)
1545{
1546 view->geometry.scissor_enabled = false;
1547 weston_view_geometry_dirty(view);
1548 weston_view_schedule_repaint(view);
1549}
1550
Armin Krezović0da12b82016-06-30 06:04:33 +02001551/* Check if view should be displayed
1552 *
1553 * The indicator is set manually when assigning
1554 * a view to a surface.
1555 *
1556 * This needs reworking. See the thread starting at:
1557 *
1558 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1559 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001560WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001561weston_view_is_mapped(struct weston_view *view)
1562{
Armin Krezović0da12b82016-06-30 06:04:33 +02001563 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001564}
1565
Armin Krezović0da12b82016-06-30 06:04:33 +02001566/* Check if a surface has a view assigned to it
1567 *
1568 * The indicator is set manually when mapping
1569 * a surface and creating a view for it.
1570 *
1571 * This needs to go. See the thread starting at:
1572 *
1573 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1574 *
1575 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001576WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001577weston_surface_is_mapped(struct weston_surface *surface)
1578{
Armin Krezović0da12b82016-06-30 06:04:33 +02001579 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001580}
1581
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001582static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001583surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001584{
1585 struct weston_view *view;
1586
1587 if (surface->width == width && surface->height == height)
1588 return;
1589
1590 surface->width = width;
1591 surface->height = height;
1592
1593 wl_list_for_each(view, &surface->views, surface_link)
1594 weston_view_geometry_dirty(view);
1595}
1596
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001597WL_EXPORT void
1598weston_surface_set_size(struct weston_surface *surface,
1599 int32_t width, int32_t height)
1600{
1601 assert(!surface->resource);
1602 surface_set_size(surface, width, height);
1603}
1604
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001605static int
1606fixed_round_up_to_int(wl_fixed_t f)
1607{
1608 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1609}
1610
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001611static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001612convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1613 int32_t width, int32_t height,
1614 uint32_t transform,
1615 int32_t scale)
1616{
1617 assert(scale > 0);
1618
1619 switch (transform) {
1620 case WL_OUTPUT_TRANSFORM_NORMAL:
1621 case WL_OUTPUT_TRANSFORM_180:
1622 case WL_OUTPUT_TRANSFORM_FLIPPED:
1623 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1624 *width_out = width / scale;
1625 *height_out = height / scale;
1626 break;
1627 case WL_OUTPUT_TRANSFORM_90:
1628 case WL_OUTPUT_TRANSFORM_270:
1629 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1630 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1631 *width_out = height / scale;
1632 *height_out = width / scale;
1633 break;
1634 default:
1635 assert(0 && "invalid transform");
1636 }
1637}
1638
1639static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001640weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001641{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001642 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001643
1644 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001645 surface->width_from_buffer = 0;
1646 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001647 return;
1648 }
1649
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001650 convert_size_by_transform_scale(&surface->width_from_buffer,
1651 &surface->height_from_buffer,
1652 surface->buffer_ref.buffer->width,
1653 surface->buffer_ref.buffer->height,
1654 vp->buffer.transform,
1655 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001656}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001657
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001658static void
1659weston_surface_update_size(struct weston_surface *surface)
1660{
1661 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1662 int32_t width, height;
1663
1664 width = surface->width_from_buffer;
1665 height = surface->height_from_buffer;
1666
1667 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001668 surface_set_size(surface,
1669 vp->surface.width, vp->surface.height);
1670 return;
1671 }
1672
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001673 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001674 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1675 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1676
1677 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001678 return;
1679 }
1680
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001681 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001682}
1683
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001684WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001685weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001686{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001687 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001688
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001689 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001690
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001691 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001692}
1693
Jason Ekstranda7af7042013-10-12 22:38:11 -05001694WL_EXPORT struct weston_view *
1695weston_compositor_pick_view(struct weston_compositor *compositor,
1696 wl_fixed_t x, wl_fixed_t y,
1697 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001698{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001699 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001700 wl_fixed_t view_x, view_y;
1701 int view_ix, view_iy;
1702 int ix = wl_fixed_to_int(x);
1703 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001704
Jason Ekstranda7af7042013-10-12 22:38:11 -05001705 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001706 if (!pixman_region32_contains_point(
1707 &view->transform.boundingbox, ix, iy, NULL))
1708 continue;
1709
1710 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1711 view_ix = wl_fixed_to_int(view_x);
1712 view_iy = wl_fixed_to_int(view_y);
1713
1714 if (!pixman_region32_contains_point(&view->surface->input,
1715 view_ix, view_iy, NULL))
1716 continue;
1717
Pekka Paalanen380adf52015-02-16 14:39:11 +02001718 if (view->geometry.scissor_enabled &&
1719 !pixman_region32_contains_point(&view->geometry.scissor,
1720 view_ix, view_iy, NULL))
1721 continue;
1722
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001723 *vx = view_x;
1724 *vy = view_y;
1725 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001726 }
1727
Derek Foremanf9318d12015-05-11 15:40:11 -05001728 *vx = wl_fixed_from_int(-1000000);
1729 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001730 return NULL;
1731}
1732
1733static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001734weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001735{
Daniel Stone37816df2012-05-16 18:45:18 +01001736 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001737
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001738 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001739 return;
1740
Daniel Stone37816df2012-05-16 18:45:18 +01001741 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001742 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001743}
1744
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001745WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001747{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001748 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001749
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 if (!weston_view_is_mapped(view))
1751 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001752
Jason Ekstranda7af7042013-10-12 22:38:11 -05001753 weston_view_damage_below(view);
1754 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001755 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001756 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001757 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758 wl_list_remove(&view->link);
1759 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001760 view->output_mask = 0;
1761 weston_surface_assign_output(view->surface);
1762
1763 if (weston_surface_is_mapped(view->surface))
1764 return;
1765
1766 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001767 struct weston_touch *touch = weston_seat_get_touch(seat);
1768 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1769 struct weston_keyboard *keyboard =
1770 weston_seat_get_keyboard(seat);
1771
1772 if (keyboard && keyboard->focus == view->surface)
1773 weston_keyboard_set_focus(keyboard, NULL);
1774 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001775 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001776 if (touch && touch->focus == view)
1777 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001778 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001779}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001780
Jason Ekstranda7af7042013-10-12 22:38:11 -05001781WL_EXPORT void
1782weston_surface_unmap(struct weston_surface *surface)
1783{
1784 struct weston_view *view;
1785
Armin Krezovićf8486c32016-06-30 06:04:28 +02001786 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001787 wl_list_for_each(view, &surface->views, surface_link)
1788 weston_view_unmap(view);
1789 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001790}
1791
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001792static void
1793weston_surface_reset_pending_buffer(struct weston_surface *surface)
1794{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001795 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001796 surface->pending.sx = 0;
1797 surface->pending.sy = 0;
1798 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001799 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001800}
1801
Jason Ekstranda7af7042013-10-12 22:38:11 -05001802WL_EXPORT void
1803weston_view_destroy(struct weston_view *view)
1804{
1805 wl_signal_emit(&view->destroy_signal, view);
1806
1807 assert(wl_list_empty(&view->geometry.child_list));
1808
1809 if (weston_view_is_mapped(view)) {
1810 weston_view_unmap(view);
1811 weston_compositor_build_view_list(view->surface->compositor);
1812 }
1813
1814 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001815 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816
1817 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001818 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001819 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001820 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821
1822 weston_view_set_transform_parent(view, NULL);
1823
Jason Ekstranda7af7042013-10-12 22:38:11 -05001824 wl_list_remove(&view->surface_link);
1825
1826 free(view);
1827}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001828
1829WL_EXPORT void
1830weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001831{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001832 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001834 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001835
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001836 if (--surface->ref_count > 0)
1837 return;
1838
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001839 assert(surface->resource == NULL);
1840
Pekka Paalanenca790762015-04-17 14:23:38 +03001841 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001842
Pekka Paalanene67858b2013-04-25 13:57:42 +03001843 assert(wl_list_empty(&surface->subsurface_list_pending));
1844 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001845
Jason Ekstranda7af7042013-10-12 22:38:11 -05001846 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1847 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001848
Jason Ekstrand7b982072014-05-20 14:33:03 -05001849 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001850
Pekka Paalanende685b82012-12-04 15:58:12 +02001851 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001852
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001853 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001854 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001855 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001856
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001857 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001858 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001859
Pekka Paalanen133e4392014-09-23 22:08:46 -04001860 weston_presentation_feedback_discard_list(&surface->feedback_list);
1861
Jonas Ådahld3414f22016-07-22 17:56:31 +08001862 wl_list_for_each_safe(constraint, next_constraint,
1863 &surface->pointer_constraints,
1864 link)
1865 weston_pointer_constraint_destroy(constraint);
1866
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001867 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001868}
1869
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001870static void
1871destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001872{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001873 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001874
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001875 assert(surface);
1876
Giulio Camuffo0d379742013-11-15 22:06:15 +01001877 /* Set the resource to NULL, since we don't want to leave a
1878 * dangling pointer if the surface was refcounted and survives
1879 * the weston_surface_destroy() call. */
1880 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001881
1882 if (surface->viewport_resource)
1883 wl_resource_set_user_data(surface->viewport_resource, NULL);
1884
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001885 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001886}
1887
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001888static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001889weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1890{
1891 struct weston_buffer *buffer =
1892 container_of(listener, struct weston_buffer, destroy_listener);
1893
1894 wl_signal_emit(&buffer->destroy_signal, buffer);
1895 free(buffer);
1896}
1897
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001898WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001899weston_buffer_from_resource(struct wl_resource *resource)
1900{
1901 struct weston_buffer *buffer;
1902 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001903
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001904 listener = wl_resource_get_destroy_listener(resource,
1905 weston_buffer_destroy_handler);
1906
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001907 if (listener)
1908 return container_of(listener, struct weston_buffer,
1909 destroy_listener);
1910
1911 buffer = zalloc(sizeof *buffer);
1912 if (buffer == NULL)
1913 return NULL;
1914
1915 buffer->resource = resource;
1916 wl_signal_init(&buffer->destroy_signal);
1917 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001918 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001919 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001920
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001921 return buffer;
1922}
1923
1924static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001925weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1926 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001927{
Pekka Paalanende685b82012-12-04 15:58:12 +02001928 struct weston_buffer_reference *ref =
1929 container_of(listener, struct weston_buffer_reference,
1930 destroy_listener);
1931
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001932 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001933 ref->buffer = NULL;
1934}
1935
1936WL_EXPORT void
1937weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001938 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001939{
1940 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001941 ref->buffer->busy_count--;
1942 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001943 assert(wl_resource_get_client(ref->buffer->resource));
1944 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001945 WL_BUFFER_RELEASE);
1946 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001947 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001948 }
1949
Pekka Paalanende685b82012-12-04 15:58:12 +02001950 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001951 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001952 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001953 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001954 }
1955
Pekka Paalanende685b82012-12-04 15:58:12 +02001956 ref->buffer = buffer;
1957 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1958}
1959
1960static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001961weston_surface_attach(struct weston_surface *surface,
1962 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001963{
1964 weston_buffer_reference(&surface->buffer_ref, buffer);
1965
Pekka Paalanena6421c42012-12-04 15:58:10 +02001966 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001967 if (weston_surface_is_mapped(surface))
1968 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001969 }
1970
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001971 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001972
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001973 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001974 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001975}
1976
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001977WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001978weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001979{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001980 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001981
1982 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001983 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001984}
1985
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001986WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001987weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001988{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001989 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001990
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001991 pixman_region32_union(&compositor->primary_plane.damage,
1992 &compositor->primary_plane.damage,
1993 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001994 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001995}
1996
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001997static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001998surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001999{
Pekka Paalanende685b82012-12-04 15:58:12 +02002000 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002001 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002002 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002003
Pekka Paalanenb5026542014-11-12 15:09:24 +02002004 if (weston_timeline_enabled_ &&
2005 pixman_region32_not_empty(&surface->damage))
2006 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2007 TLP_OUTPUT(surface->output), TLP_END);
2008
Jason Ekstrandef540082014-06-26 10:37:36 -07002009 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002010}
2011
2012static void
2013view_accumulate_damage(struct weston_view *view,
2014 pixman_region32_t *opaque)
2015{
2016 pixman_region32_t damage;
2017
2018 pixman_region32_init(&damage);
2019 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002020 pixman_box32_t *extents;
2021
Jason Ekstranda7af7042013-10-12 22:38:11 -05002022 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002023 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002024 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002025 pixman_region32_copy(&damage, &view->surface->damage);
2026 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002027 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002028 }
2029
Pekka Paalanen380adf52015-02-16 14:39:11 +02002030 pixman_region32_intersect(&damage, &damage,
2031 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002032 pixman_region32_subtract(&damage, &damage, opaque);
2033 pixman_region32_union(&view->plane->damage,
2034 &view->plane->damage, &damage);
2035 pixman_region32_fini(&damage);
2036 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002037 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002038}
2039
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002040static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002041compositor_accumulate_damage(struct weston_compositor *ec)
2042{
2043 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002044 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002045 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002046
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002047 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002048
2049 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002050 pixman_region32_copy(&plane->clip, &clip);
2051
2052 pixman_region32_init(&opaque);
2053
Jason Ekstranda7af7042013-10-12 22:38:11 -05002054 wl_list_for_each(ev, &ec->view_list, link) {
2055 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002056 continue;
2057
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002059 }
2060
2061 pixman_region32_union(&clip, &clip, &opaque);
2062 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002063 }
2064
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002065 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002066
Jason Ekstranda7af7042013-10-12 22:38:11 -05002067 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002068 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002069
2070 wl_list_for_each(ev, &ec->view_list, link) {
2071 if (ev->surface->touched)
2072 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002073 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002074
2075 surface_flush_damage(ev->surface);
2076
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002077 /* Both the renderer and the backend have seen the buffer
2078 * by now. If renderer needs the buffer, it has its own
2079 * reference set. If the backend wants to keep the buffer
2080 * around for migrating the surface into a non-primary plane
2081 * later, keep_buffer is true. Otherwise, drop the core
2082 * reference now, and allow early buffer release. This enables
2083 * clients to use single-buffering.
2084 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002085 if (!ev->surface->keep_buffer)
2086 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002087 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002088}
2089
2090static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002091surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002092{
2093 struct weston_subsurface *sub;
2094
Pekka Paalanene67858b2013-04-25 13:57:42 +03002095 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002096 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002097 continue;
2098
Jason Ekstranda7af7042013-10-12 22:38:11 -05002099 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2100 wl_list_init(&sub->surface->views);
2101
2102 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002103 }
2104}
2105
2106static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002107surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002108{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109 struct weston_subsurface *sub;
2110 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002111
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2113 if (sub->surface == surface)
2114 continue;
2115
George Kiagiadakised04d382014-06-13 18:10:26 +02002116 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2117 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002118 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002119 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002120
2121 surface_free_unused_subsurface_views(sub->surface);
2122 }
2123}
2124
2125static void
2126view_list_add_subsurface_view(struct weston_compositor *compositor,
2127 struct weston_subsurface *sub,
2128 struct weston_view *parent)
2129{
2130 struct weston_subsurface *child;
2131 struct weston_view *view = NULL, *iv;
2132
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002133 if (!weston_surface_is_mapped(sub->surface))
2134 return;
2135
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2137 if (iv->geometry.parent == parent) {
2138 view = iv;
2139 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002140 }
2141 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002142
2143 if (view) {
2144 /* Put it back in the surface's list of views */
2145 wl_list_remove(&view->surface_link);
2146 wl_list_insert(&sub->surface->views, &view->surface_link);
2147 } else {
2148 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002149 weston_view_set_position(view,
2150 sub->position.x,
2151 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002152 weston_view_set_transform_parent(view, parent);
2153 }
2154
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002155 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002156 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002157 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002158
Pekka Paalanenb188e912013-11-19 14:03:35 +02002159 if (wl_list_empty(&sub->surface->subsurface_list)) {
2160 wl_list_insert(compositor->view_list.prev, &view->link);
2161 return;
2162 }
2163
2164 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2165 if (child->surface == sub->surface)
2166 wl_list_insert(compositor->view_list.prev, &view->link);
2167 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002168 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002169 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002170}
2171
2172static void
2173view_list_add(struct weston_compositor *compositor,
2174 struct weston_view *view)
2175{
2176 struct weston_subsurface *sub;
2177
2178 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179
Pekka Paalanenb188e912013-11-19 14:03:35 +02002180 if (wl_list_empty(&view->surface->subsurface_list)) {
2181 wl_list_insert(compositor->view_list.prev, &view->link);
2182 return;
2183 }
2184
2185 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2186 if (sub->surface == view->surface)
2187 wl_list_insert(compositor->view_list.prev, &view->link);
2188 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002189 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002190 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191}
2192
2193static void
2194weston_compositor_build_view_list(struct weston_compositor *compositor)
2195{
2196 struct weston_view *view;
2197 struct weston_layer *layer;
2198
2199 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002200 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002201 surface_stash_subsurface_views(view->surface);
2202
2203 wl_list_init(&compositor->view_list);
2204 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002205 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002206 view_list_add(compositor, view);
2207 }
2208 }
2209
2210 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002211 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002213}
2214
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002215static void
2216weston_output_take_feedback_list(struct weston_output *output,
2217 struct weston_surface *surface)
2218{
2219 struct weston_view *view;
2220 struct weston_presentation_feedback *feedback;
2221 uint32_t flags = 0xffffffff;
2222
2223 if (wl_list_empty(&surface->feedback_list))
2224 return;
2225
2226 /* All views must have the flag for the flag to survive. */
2227 wl_list_for_each(view, &surface->views, surface_link) {
2228 /* ignore views that are not on this output at all */
2229 if (view->output_mask & (1u << output->id))
2230 flags &= view->psf_flags;
2231 }
2232
2233 wl_list_for_each(feedback, &surface->feedback_list, link)
2234 feedback->psf_flags = flags;
2235
2236 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2237 wl_list_init(&surface->feedback_list);
2238}
2239
David Herrmann1edf44c2013-10-22 17:11:26 +02002240static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002241weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002242{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002243 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002244 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002245 struct weston_animation *animation, *next;
2246 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002247 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002248 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002249 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002250
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002251 if (output->destroying)
2252 return 0;
2253
Pekka Paalanenb5026542014-11-12 15:09:24 +02002254 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2255
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002256 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002257 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002258
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002259 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002260 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002261 } else {
2262 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002263 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002264 ev->psf_flags = 0;
2265 }
2266 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002267
Pekka Paalanene67858b2013-04-25 13:57:42 +03002268 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002269 wl_list_for_each(ev, &ec->view_list, link) {
2270 /* Note: This operation is safe to do multiple times on the
2271 * same surface.
2272 */
2273 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002274 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002275 &ev->surface->frame_callback_list);
2276 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002277
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002278 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002279 }
2280 }
2281
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002282 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002283
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002284 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002285 pixman_region32_intersect(&output_damage,
2286 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002287 pixman_region32_subtract(&output_damage,
2288 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002289
Scott Moreauccbf29d2012-02-22 14:21:41 -07002290 if (output->dirty)
2291 weston_output_update_matrix(output);
2292
David Herrmann1edf44c2013-10-22 17:11:26 +02002293 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002294
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002295 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002296
Kristian Høgsbergef044142011-06-21 15:02:12 -04002297 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002298
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002299 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002300
Jonas Ådahldb773762012-06-13 00:01:21 +02002301 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002302 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002303 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002304 }
2305
Scott Moreaud64cf212012-06-08 19:40:54 -06002306 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002307 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002308 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002309 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002310
Pekka Paalanenb5026542014-11-12 15:09:24 +02002311 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2312
David Herrmann1edf44c2013-10-22 17:11:26 +02002313 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002314}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002315
Pekka Paalanen82919792014-05-21 13:51:49 +03002316static void
2317weston_output_schedule_repaint_reset(struct weston_output *output)
2318{
Pekka Paalanen82919792014-05-21 13:51:49 +03002319 output->repaint_scheduled = 0;
2320 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002321}
2322
Pekka Paalanen0513a952014-05-21 16:17:27 +03002323static int
2324output_repaint_timer_handler(void *data)
2325{
2326 struct weston_output *output = data;
2327 struct weston_compositor *compositor = output->compositor;
2328
2329 if (output->repaint_needed &&
2330 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2331 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2332 weston_output_repaint(output) == 0)
2333 return 0;
2334
2335 weston_output_schedule_repaint_reset(output);
2336
2337 return 0;
2338}
2339
Kristian Høgsbergef044142011-06-21 15:02:12 -04002340WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002341weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002342 const struct timespec *stamp,
2343 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002344{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002345 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002346 int32_t refresh_nsec;
2347 struct timespec now;
2348 struct timespec gone;
2349 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002350
Pekka Paalanenb5026542014-11-12 15:09:24 +02002351 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2352 TLP_VBLANK(stamp), TLP_END);
2353
Pekka Paalanend7894d02015-07-03 15:08:53 +03002354 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002355 weston_presentation_feedback_present_list(&output->feedback_list,
2356 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002357 output->msc,
2358 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002359
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002360 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002361
Pekka Paalanen0513a952014-05-21 16:17:27 +03002362 weston_compositor_read_presentation_clock(compositor, &now);
2363 timespec_sub(&gone, &now, stamp);
2364 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2365 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002366
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002367 if (msec < -1000 || msec > 1000) {
2368 static bool warned;
2369
2370 if (!warned)
2371 weston_log("Warning: computed repaint delay is "
2372 "insane: %d msec\n", msec);
2373 warned = true;
2374
2375 msec = 0;
2376 }
2377
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002378 /* Called from restart_repaint_loop and restart happens already after
2379 * the deadline given by repaint_msec? In that case we delay until
2380 * the deadline of the next frame, to give clients a more predictable
2381 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002382 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002383 msec += refresh_nsec / 1000000;
2384
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002385 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002386 output_repaint_timer_handler(output);
2387 else
2388 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002389}
2390
2391static void
2392idle_repaint(void *data)
2393{
2394 struct weston_output *output = data;
2395
Jonas Ådahle5a12252013-04-05 23:07:11 +02002396 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002397}
2398
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002399WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002400weston_layer_entry_insert(struct weston_layer_entry *list,
2401 struct weston_layer_entry *entry)
2402{
2403 wl_list_insert(&list->link, &entry->link);
2404 entry->layer = list->layer;
2405}
2406
2407WL_EXPORT void
2408weston_layer_entry_remove(struct weston_layer_entry *entry)
2409{
2410 wl_list_remove(&entry->link);
2411 wl_list_init(&entry->link);
2412 entry->layer = NULL;
2413}
2414
2415WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002416weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2417{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002418 wl_list_init(&layer->view_list.link);
2419 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002420 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002421 if (below != NULL)
2422 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002423}
2424
2425WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002426weston_layer_set_mask(struct weston_layer *layer,
2427 int x, int y, int width, int height)
2428{
2429 struct weston_view *view;
2430
2431 layer->mask.x1 = x;
2432 layer->mask.x2 = x + width;
2433 layer->mask.y1 = y;
2434 layer->mask.y2 = y + height;
2435
2436 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2437 weston_view_geometry_dirty(view);
2438 }
2439}
2440
2441WL_EXPORT void
2442weston_layer_set_mask_infinite(struct weston_layer *layer)
2443{
2444 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2445 UINT32_MAX, UINT32_MAX);
2446}
2447
2448WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002449weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002450{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002451 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002452 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002453
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002454 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2455 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002456 return;
2457
Pekka Paalanenb5026542014-11-12 15:09:24 +02002458 if (!output->repaint_needed)
2459 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2460
Kristian Høgsbergef044142011-06-21 15:02:12 -04002461 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002462 output->repaint_needed = 1;
2463 if (output->repaint_scheduled)
2464 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002465
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002466 wl_event_loop_add_idle(loop, idle_repaint, output);
2467 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002468 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002469}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002470
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002471WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002472weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2473{
2474 struct weston_output *output;
2475
2476 wl_list_for_each(output, &compositor->output_list, link)
2477 weston_output_schedule_repaint(output);
2478}
2479
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002480static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002481surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002482{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002483 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002484}
2485
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002486static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002487surface_attach(struct wl_client *client,
2488 struct wl_resource *resource,
2489 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2490{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002491 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002492 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002493
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002494 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002495 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002496 if (buffer == NULL) {
2497 wl_client_post_no_memory(client);
2498 return;
2499 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002500 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002501
Pekka Paalanende685b82012-12-04 15:58:12 +02002502 /* Attach, attach, without commit in between does not send
2503 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002504 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002505
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002506 surface->pending.sx = sx;
2507 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002508 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002509}
2510
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002511static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002512surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002513 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002514 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002515{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002516 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002517
Derek Foreman57e92ed2015-11-17 14:11:35 -06002518 if (width <= 0 || height <= 0)
2519 return;
2520
Derek Foreman152254b2015-11-26 14:17:48 -06002521 pixman_region32_union_rect(&surface->pending.damage_surface,
2522 &surface->pending.damage_surface,
2523 x, y, width, height);
2524}
2525
2526static void
2527surface_damage_buffer(struct wl_client *client,
2528 struct wl_resource *resource,
2529 int32_t x, int32_t y, int32_t width, int32_t height)
2530{
2531 struct weston_surface *surface = wl_resource_get_user_data(resource);
2532
2533 if (width <= 0 || height <= 0)
2534 return;
2535
2536 pixman_region32_union_rect(&surface->pending.damage_buffer,
2537 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002538 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002539}
2540
Kristian Høgsberg33418202011-08-16 23:01:28 -04002541static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002542destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002543{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002544 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002545
2546 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002547 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002548}
2549
2550static void
2551surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002552 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002553{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002554 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002555 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002556
2557 cb = malloc(sizeof *cb);
2558 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002559 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002560 return;
2561 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002562
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002563 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2564 callback);
2565 if (cb->resource == NULL) {
2566 free(cb);
2567 wl_resource_post_no_memory(resource);
2568 return;
2569 }
2570
Jason Ekstranda85118c2013-06-27 20:17:02 -05002571 wl_resource_set_implementation(cb->resource, NULL, cb,
2572 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002573
Pekka Paalanenbc106382012-10-10 12:49:31 +03002574 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002575}
2576
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002577static void
2578surface_set_opaque_region(struct wl_client *client,
2579 struct wl_resource *resource,
2580 struct wl_resource *region_resource)
2581{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002582 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002583 struct weston_region *region;
2584
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002585 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002586 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002587 pixman_region32_copy(&surface->pending.opaque,
2588 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002589 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002590 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002591 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002592}
2593
2594static void
2595surface_set_input_region(struct wl_client *client,
2596 struct wl_resource *resource,
2597 struct wl_resource *region_resource)
2598{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002599 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002600 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002601
2602 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002603 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002604 pixman_region32_copy(&surface->pending.input,
2605 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002606 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002607 pixman_region32_fini(&surface->pending.input);
2608 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002609 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002610}
2611
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002612static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002613weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002614{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002615 struct weston_subsurface *sub;
2616
2617 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2618 parent_link_pending) {
2619 wl_list_remove(&sub->parent_link);
2620 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2621 }
2622}
2623
2624static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002625weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002626 struct weston_matrix *matrix)
2627{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002628 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002629 double src_width, src_height, dest_width, dest_height;
2630
2631 weston_matrix_init(matrix);
2632
2633 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2634 src_width = surface->width_from_buffer;
2635 src_height = surface->height_from_buffer;
2636 } else {
2637 src_width = wl_fixed_to_double(vp->buffer.src_width);
2638 src_height = wl_fixed_to_double(vp->buffer.src_height);
2639 }
2640
2641 if (vp->surface.width == -1) {
2642 dest_width = src_width;
2643 dest_height = src_height;
2644 } else {
2645 dest_width = vp->surface.width;
2646 dest_height = vp->surface.height;
2647 }
2648
2649 if (src_width != dest_width || src_height != dest_height)
2650 weston_matrix_scale(matrix,
2651 src_width / dest_width,
2652 src_height / dest_height, 1);
2653
2654 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2655 weston_matrix_translate(matrix,
2656 wl_fixed_to_double(vp->buffer.src_x),
2657 wl_fixed_to_double(vp->buffer.src_y),
2658 0);
2659
2660 switch (vp->buffer.transform) {
2661 case WL_OUTPUT_TRANSFORM_FLIPPED:
2662 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2663 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2664 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2665 weston_matrix_scale(matrix, -1, 1, 1);
2666 weston_matrix_translate(matrix,
2667 surface->width_from_buffer, 0, 0);
2668 break;
2669 }
2670
2671 switch (vp->buffer.transform) {
2672 default:
2673 case WL_OUTPUT_TRANSFORM_NORMAL:
2674 case WL_OUTPUT_TRANSFORM_FLIPPED:
2675 break;
2676 case WL_OUTPUT_TRANSFORM_90:
2677 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2678 weston_matrix_rotate_xy(matrix, 0, 1);
2679 weston_matrix_translate(matrix,
2680 surface->height_from_buffer, 0, 0);
2681 break;
2682 case WL_OUTPUT_TRANSFORM_180:
2683 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2684 weston_matrix_rotate_xy(matrix, -1, 0);
2685 weston_matrix_translate(matrix,
2686 surface->width_from_buffer,
2687 surface->height_from_buffer, 0);
2688 break;
2689 case WL_OUTPUT_TRANSFORM_270:
2690 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2691 weston_matrix_rotate_xy(matrix, 0, -1);
2692 weston_matrix_translate(matrix,
2693 0, surface->width_from_buffer, 0);
2694 break;
2695 }
2696
2697 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2698}
2699
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002700/**
2701 * Compute a + b > c while being safe to overflows.
2702 */
2703static bool
2704fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2705{
2706 return (int64_t)a + (int64_t)b > (int64_t)c;
2707}
2708
2709static bool
2710weston_surface_is_pending_viewport_source_valid(
2711 const struct weston_surface *surface)
2712{
2713 const struct weston_surface_state *pend = &surface->pending;
2714 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2715 int width_from_buffer = 0;
2716 int height_from_buffer = 0;
2717 wl_fixed_t w;
2718 wl_fixed_t h;
2719
2720 /* If viewport source rect is not set, it is always ok. */
2721 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2722 return true;
2723
2724 if (pend->newly_attached) {
2725 if (pend->buffer) {
2726 convert_size_by_transform_scale(&width_from_buffer,
2727 &height_from_buffer,
2728 pend->buffer->width,
2729 pend->buffer->height,
2730 vp->buffer.transform,
2731 vp->buffer.scale);
2732 } else {
2733 /* No buffer: viewport is irrelevant. */
2734 return true;
2735 }
2736 } else {
2737 width_from_buffer = surface->width_from_buffer;
2738 height_from_buffer = surface->height_from_buffer;
2739 }
2740
2741 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2742 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2743
2744 /* No buffer: viewport is irrelevant. */
2745 if (width_from_buffer == 0 || height_from_buffer == 0)
2746 return true;
2747
2748 /* overflow checks for wl_fixed_from_int() */
2749 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2750 return false;
2751 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2752 return false;
2753
2754 w = wl_fixed_from_int(width_from_buffer);
2755 h = wl_fixed_from_int(height_from_buffer);
2756
2757 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2758 return false;
2759 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2760 return false;
2761
2762 return true;
2763}
2764
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002765static bool
2766fixed_is_integer(wl_fixed_t v)
2767{
2768 return (v & 0xff) == 0;
2769}
2770
2771static bool
2772weston_surface_is_pending_viewport_dst_size_int(
2773 const struct weston_surface *surface)
2774{
2775 const struct weston_buffer_viewport *vp =
2776 &surface->pending.buffer_viewport;
2777
2778 if (vp->surface.width != -1) {
2779 assert(vp->surface.width > 0 && vp->surface.height > 0);
2780 return true;
2781 }
2782
2783 return fixed_is_integer(vp->buffer.src_width) &&
2784 fixed_is_integer(vp->buffer.src_height);
2785}
2786
Derek Foreman152254b2015-11-26 14:17:48 -06002787/* Translate pending damage in buffer co-ordinates to surface
2788 * co-ordinates and union it with a pixman_region32_t.
2789 * This should only be called after the buffer is attached.
2790 */
2791static void
2792apply_damage_buffer(pixman_region32_t *dest,
2793 struct weston_surface *surface,
2794 struct weston_surface_state *state)
2795{
2796 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2797
2798 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2799 * translated into surface co-ordinates and unioned with
2800 * any other surface damage.
2801 * None of this makes sense if there is no buffer though.
2802 */
2803 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2804 pixman_region32_t buffer_damage;
2805
2806 pixman_region32_intersect_rect(&state->damage_buffer,
2807 &state->damage_buffer,
2808 0, 0, buffer->width,
2809 buffer->height);
2810 pixman_region32_init(&buffer_damage);
2811 weston_matrix_transform_region(&buffer_damage,
2812 &surface->buffer_to_surface_matrix,
2813 &state->damage_buffer);
2814 pixman_region32_union(dest, dest, &buffer_damage);
2815 pixman_region32_fini(&buffer_damage);
2816 }
2817 /* We should clear this on commit even if there was no buffer */
2818 pixman_region32_clear(&state->damage_buffer);
2819}
2820
Jason Ekstrand1e059042014-10-16 10:55:19 -05002821static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002822weston_surface_commit_state(struct weston_surface *surface,
2823 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002824{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002826 pixman_region32_t opaque;
2827
Alexander Larsson4ea95522013-05-22 14:41:37 +02002828 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002829 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002830 /* wp_viewport.set_source */
2831 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002832 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002833
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002834 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002835 if (state->newly_attached)
2836 weston_surface_attach(surface, state->buffer);
2837 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002838
Jason Ekstrand1e059042014-10-16 10:55:19 -05002839 weston_surface_build_buffer_matrix(surface,
2840 &surface->surface_to_buffer_matrix);
2841 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2842 &surface->surface_to_buffer_matrix);
2843
Jason Ekstrand7b982072014-05-20 14:33:03 -05002844 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002845 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002846 if (surface->committed)
2847 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002848 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002849
Jason Ekstrand7b982072014-05-20 14:33:03 -05002850 state->sx = 0;
2851 state->sy = 0;
2852 state->newly_attached = 0;
2853 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002854
Derek Foreman152254b2015-11-26 14:17:48 -06002855 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002856 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002857 (pixman_region32_not_empty(&state->damage_surface) ||
2858 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002859 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002860
Pekka Paalanen8e159182012-10-10 12:49:25 +03002861 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002862 &state->damage_surface);
2863
2864 apply_damage_buffer(&surface->damage, surface, state);
2865
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002866 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002867 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002868 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002869
2870 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002871 pixman_region32_init(&opaque);
2872 pixman_region32_intersect_rect(&opaque, &state->opaque,
2873 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002874
2875 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2876 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002877 wl_list_for_each(view, &surface->views, surface_link)
2878 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002879 }
2880
2881 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002882
2883 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002884 pixman_region32_intersect_rect(&surface->input, &state->input,
2885 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002886
Pekka Paalanenbc106382012-10-10 12:49:31 +03002887 /* wl_surface.frame */
2888 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002889 &state->frame_callback_list);
2890 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002891
2892 /* XXX:
2893 * What should happen with a feedback request, if there
2894 * is no wl_buffer attached for this commit?
2895 */
2896
2897 /* presentation.feedback */
2898 wl_list_insert_list(&surface->feedback_list,
2899 &state->feedback_list);
2900 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08002901
2902 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002903}
2904
2905static void
2906weston_surface_commit(struct weston_surface *surface)
2907{
2908 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002909
Pekka Paalanene67858b2013-04-25 13:57:42 +03002910 weston_surface_commit_subsurface_order(surface);
2911
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002912 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002913}
2914
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002915static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002916weston_subsurface_commit(struct weston_subsurface *sub);
2917
2918static void
2919weston_subsurface_parent_commit(struct weston_subsurface *sub,
2920 int parent_is_synchronized);
2921
2922static void
2923surface_commit(struct wl_client *client, struct wl_resource *resource)
2924{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002925 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002926 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2927
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002928 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
2929 assert(surface->viewport_resource);
2930
2931 wl_resource_post_error(surface->viewport_resource,
2932 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
2933 "wl_surface@%d has viewport source outside buffer",
2934 wl_resource_get_id(resource));
2935 return;
2936 }
2937
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002938 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
2939 assert(surface->viewport_resource);
2940
2941 wl_resource_post_error(surface->viewport_resource,
2942 WP_VIEWPORT_ERROR_BAD_SIZE,
2943 "wl_surface@%d viewport dst size not integer",
2944 wl_resource_get_id(resource));
2945 return;
2946 }
2947
Pekka Paalanene67858b2013-04-25 13:57:42 +03002948 if (sub) {
2949 weston_subsurface_commit(sub);
2950 return;
2951 }
2952
2953 weston_surface_commit(surface);
2954
2955 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2956 if (sub->surface != surface)
2957 weston_subsurface_parent_commit(sub, 0);
2958 }
2959}
2960
2961static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002962surface_set_buffer_transform(struct wl_client *client,
2963 struct wl_resource *resource, int transform)
2964{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002965 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002966
Jonny Lamba55f1392014-05-30 12:07:15 +02002967 /* if wl_output.transform grows more members this will need to be updated. */
2968 if (transform < 0 ||
2969 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2970 wl_resource_post_error(resource,
2971 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2972 "buffer transform must be a valid transform "
2973 "('%d' specified)", transform);
2974 return;
2975 }
2976
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002977 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002978 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002979}
2980
Alexander Larsson4ea95522013-05-22 14:41:37 +02002981static void
2982surface_set_buffer_scale(struct wl_client *client,
2983 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002984 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002985{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002986 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002987
Jonny Lamba55f1392014-05-30 12:07:15 +02002988 if (scale < 1) {
2989 wl_resource_post_error(resource,
2990 WL_SURFACE_ERROR_INVALID_SCALE,
2991 "buffer scale must be at least one "
2992 "('%d' specified)", scale);
2993 return;
2994 }
2995
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002996 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002997 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002998}
2999
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003000static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003001 surface_destroy,
3002 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003003 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003004 surface_frame,
3005 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003006 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003007 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003008 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003009 surface_set_buffer_scale,
3010 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003011};
3012
3013static void
3014compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003015 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003016{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003017 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003018 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003019
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003020 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003021 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003022 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003023 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003024 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003025
Jason Ekstranda85118c2013-06-27 20:17:02 -05003026 surface->resource =
3027 wl_resource_create(client, &wl_surface_interface,
3028 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003029 if (surface->resource == NULL) {
3030 weston_surface_destroy(surface);
3031 wl_resource_post_no_memory(resource);
3032 return;
3033 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003034 wl_resource_set_implementation(surface->resource, &surface_interface,
3035 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003036
3037 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003038}
3039
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003040static void
3041destroy_region(struct wl_resource *resource)
3042{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003043 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003044
3045 pixman_region32_fini(&region->region);
3046 free(region);
3047}
3048
3049static void
3050region_destroy(struct wl_client *client, struct wl_resource *resource)
3051{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003052 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003053}
3054
3055static void
3056region_add(struct wl_client *client, struct wl_resource *resource,
3057 int32_t x, int32_t y, int32_t width, int32_t height)
3058{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003059 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003060
3061 pixman_region32_union_rect(&region->region, &region->region,
3062 x, y, width, height);
3063}
3064
3065static void
3066region_subtract(struct wl_client *client, struct wl_resource *resource,
3067 int32_t x, int32_t y, int32_t width, int32_t height)
3068{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003069 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003070 pixman_region32_t rect;
3071
3072 pixman_region32_init_rect(&rect, x, y, width, height);
3073 pixman_region32_subtract(&region->region, &region->region, &rect);
3074 pixman_region32_fini(&rect);
3075}
3076
3077static const struct wl_region_interface region_interface = {
3078 region_destroy,
3079 region_add,
3080 region_subtract
3081};
3082
3083static void
3084compositor_create_region(struct wl_client *client,
3085 struct wl_resource *resource, uint32_t id)
3086{
3087 struct weston_region *region;
3088
3089 region = malloc(sizeof *region);
3090 if (region == NULL) {
3091 wl_resource_post_no_memory(resource);
3092 return;
3093 }
3094
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003095 pixman_region32_init(&region->region);
3096
Jason Ekstranda85118c2013-06-27 20:17:02 -05003097 region->resource =
3098 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003099 if (region->resource == NULL) {
3100 free(region);
3101 wl_resource_post_no_memory(resource);
3102 return;
3103 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003104 wl_resource_set_implementation(region->resource, &region_interface,
3105 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003106}
3107
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003108static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003109 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003110 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003111};
3112
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003113static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003114weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3115{
3116 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003117
Jason Ekstrand7b982072014-05-20 14:33:03 -05003118 weston_surface_commit_state(surface, &sub->cached);
3119 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003120
3121 weston_surface_commit_subsurface_order(surface);
3122
3123 weston_surface_schedule_repaint(surface);
3124
Jason Ekstrand7b982072014-05-20 14:33:03 -05003125 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003126}
3127
3128static void
3129weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3130{
3131 struct weston_surface *surface = sub->surface;
3132
3133 /*
3134 * If this commit would cause the surface to move by the
3135 * attach(dx, dy) parameters, the old damage region must be
3136 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003137 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003138 */
Derek Foreman152254b2015-11-26 14:17:48 -06003139 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003140 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003141 pixman_region32_union(&sub->cached.damage_surface,
3142 &sub->cached.damage_surface,
3143 &surface->pending.damage_surface);
3144 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003145
3146 if (surface->pending.newly_attached) {
3147 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003148 weston_surface_state_set_buffer(&sub->cached,
3149 surface->pending.buffer);
3150 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003151 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003152 weston_presentation_feedback_discard_list(
3153 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003154 }
3155 sub->cached.sx += surface->pending.sx;
3156 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003157
Derek Foreman152254b2015-11-26 14:17:48 -06003158 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3159
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003160 sub->cached.buffer_viewport.changed |=
3161 surface->pending.buffer_viewport.changed;
3162 sub->cached.buffer_viewport.buffer =
3163 surface->pending.buffer_viewport.buffer;
3164 sub->cached.buffer_viewport.surface =
3165 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003166
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003167 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003168
3169 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3170
3171 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3172
3173 wl_list_insert_list(&sub->cached.frame_callback_list,
3174 &surface->pending.frame_callback_list);
3175 wl_list_init(&surface->pending.frame_callback_list);
3176
Pekka Paalanen133e4392014-09-23 22:08:46 -04003177 wl_list_insert_list(&sub->cached.feedback_list,
3178 &surface->pending.feedback_list);
3179 wl_list_init(&surface->pending.feedback_list);
3180
Jason Ekstrand7b982072014-05-20 14:33:03 -05003181 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003182}
3183
Derek Foreman280e7dd2014-10-03 13:13:42 -05003184static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003185weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3186{
3187 while (sub) {
3188 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003189 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003190
3191 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003192 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003193
3194 sub = weston_surface_to_subsurface(sub->parent);
3195 }
3196
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003197 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003198}
3199
3200static void
3201weston_subsurface_commit(struct weston_subsurface *sub)
3202{
3203 struct weston_surface *surface = sub->surface;
3204 struct weston_subsurface *tmp;
3205
3206 /* Recursive check for effectively synchronized. */
3207 if (weston_subsurface_is_synchronized(sub)) {
3208 weston_subsurface_commit_to_cache(sub);
3209 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003210 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003211 /* flush accumulated state from cache */
3212 weston_subsurface_commit_to_cache(sub);
3213 weston_subsurface_commit_from_cache(sub);
3214 } else {
3215 weston_surface_commit(surface);
3216 }
3217
3218 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3219 if (tmp->surface != surface)
3220 weston_subsurface_parent_commit(tmp, 0);
3221 }
3222 }
3223}
3224
3225static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003226weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003227{
3228 struct weston_surface *surface = sub->surface;
3229 struct weston_subsurface *tmp;
3230
Pekka Paalanene67858b2013-04-25 13:57:42 +03003231 /* From now on, commit_from_cache the whole sub-tree, regardless of
3232 * the synchronized mode of each child. This sub-surface or some
3233 * of its ancestors were synchronized, so we are synchronized
3234 * all the way down.
3235 */
3236
Jason Ekstrand7b982072014-05-20 14:33:03 -05003237 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003238 weston_subsurface_commit_from_cache(sub);
3239
3240 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3241 if (tmp->surface != surface)
3242 weston_subsurface_parent_commit(tmp, 1);
3243 }
3244}
3245
3246static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003247weston_subsurface_parent_commit(struct weston_subsurface *sub,
3248 int parent_is_synchronized)
3249{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003250 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003251 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003252 wl_list_for_each(view, &sub->surface->views, surface_link)
3253 weston_view_set_position(view,
3254 sub->position.x,
3255 sub->position.y);
3256
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003257 sub->position.set = 0;
3258 }
3259
3260 if (parent_is_synchronized || sub->synchronized)
3261 weston_subsurface_synchronized_commit(sub);
3262}
3263
Pekka Paalanen8274d902014-08-06 19:36:51 +03003264static int
3265subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3266{
3267 return snprintf(buf, len, "sub-surface");
3268}
3269
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003270static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003271subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003272{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003273 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003274
Jason Ekstranda7af7042013-10-12 22:38:11 -05003275 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003276 weston_view_set_position(view,
3277 view->geometry.x + dx,
3278 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003279
3280 /* No need to check parent mappedness, because if parent is not
3281 * mapped, parent is not in a visible layer, so this sub-surface
3282 * will not be drawn either.
3283 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003284
Pekka Paalanene67858b2013-04-25 13:57:42 +03003285 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003286 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003287
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003288 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003289 * because that would call it also for the parent surface,
3290 * which might not be mapped yet. That would lead to
3291 * inconsistent state, where the window could never be
3292 * mapped.
3293 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003294 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003295 * weston_surface_is_mapped() return true, so that when the
3296 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003297 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003298 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003299 }
3300}
3301
3302static struct weston_subsurface *
3303weston_surface_to_subsurface(struct weston_surface *surface)
3304{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003305 if (surface->committed == subsurface_committed)
3306 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003307
3308 return NULL;
3309}
3310
Pekka Paalanen01388e22013-04-25 13:57:44 +03003311WL_EXPORT struct weston_surface *
3312weston_surface_get_main_surface(struct weston_surface *surface)
3313{
3314 struct weston_subsurface *sub;
3315
3316 while (surface && (sub = weston_surface_to_subsurface(surface)))
3317 surface = sub->parent;
3318
3319 return surface;
3320}
3321
Pekka Paalanen50b67472014-10-01 15:02:41 +03003322WL_EXPORT int
3323weston_surface_set_role(struct weston_surface *surface,
3324 const char *role_name,
3325 struct wl_resource *error_resource,
3326 uint32_t error_code)
3327{
3328 assert(role_name);
3329
3330 if (surface->role_name == NULL ||
3331 surface->role_name == role_name ||
3332 strcmp(surface->role_name, role_name) == 0) {
3333 surface->role_name = role_name;
3334
3335 return 0;
3336 }
3337
3338 wl_resource_post_error(error_resource, error_code,
3339 "Cannot assign role %s to wl_surface@%d,"
3340 " already has role %s\n",
3341 role_name,
3342 wl_resource_get_id(surface->resource),
3343 surface->role_name);
3344 return -1;
3345}
3346
Pekka Paalanen8274d902014-08-06 19:36:51 +03003347WL_EXPORT void
3348weston_surface_set_label_func(struct weston_surface *surface,
3349 int (*desc)(struct weston_surface *,
3350 char *, size_t))
3351{
3352 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003353 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003354}
3355
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003356/** Get the size of surface contents
3357 *
3358 * \param surface The surface to query.
3359 * \param width Returns the width of raw contents.
3360 * \param height Returns the height of raw contents.
3361 *
3362 * Retrieves the raw surface content size in pixels for the given surface.
3363 * This is the whole content size in buffer pixels. If the surface
3364 * has no content or the renderer does not implement this feature,
3365 * zeroes are returned.
3366 *
3367 * This function is used to determine the buffer size needed for
3368 * a weston_surface_copy_content() call.
3369 */
3370WL_EXPORT void
3371weston_surface_get_content_size(struct weston_surface *surface,
3372 int *width, int *height)
3373{
3374 struct weston_renderer *rer = surface->compositor->renderer;
3375
3376 if (!rer->surface_get_content_size) {
3377 *width = 0;
3378 *height = 0;
3379 return;
3380 }
3381
3382 rer->surface_get_content_size(surface, width, height);
3383}
3384
Quentin Glidic248dd102016-08-12 10:41:34 +02003385/** Get the bounding box of a surface and its subsurfaces
3386 *
3387 * \param surface The surface to query.
3388 * \return The bounding box relative to the surface origin.
3389 *
3390 */
3391WL_EXPORT struct weston_geometry
3392weston_surface_get_bounding_box(struct weston_surface *surface)
3393{
3394 pixman_region32_t region;
3395 pixman_box32_t *box;
3396 struct weston_subsurface *subsurface;
3397
3398 pixman_region32_init_rect(&region,
3399 0, 0,
3400 surface->width, surface->height);
3401
3402 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3403 pixman_region32_union_rect(&region, &region,
3404 subsurface->position.x,
3405 subsurface->position.y,
3406 subsurface->surface->width,
3407 subsurface->surface->height);
3408
3409 box = pixman_region32_extents(&region);
3410 struct weston_geometry geometry = {
3411 .x = box->x1,
3412 .y = box->y1,
3413 .width = box->x2 - box->x1,
3414 .height = box->y2 - box->y1,
3415 };
3416
3417 pixman_region32_fini(&region);
3418
3419 return geometry;
3420}
3421
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003422/** Copy surface contents to system memory.
3423 *
3424 * \param surface The surface to copy from.
3425 * \param target Pointer to the target memory buffer.
3426 * \param size Size of the target buffer in bytes.
3427 * \param src_x X location on contents to copy from.
3428 * \param src_y Y location on contents to copy from.
3429 * \param width Width in pixels of the area to copy.
3430 * \param height Height in pixels of the area to copy.
3431 * \return 0 for success, -1 for failure.
3432 *
3433 * Surface contents are maintained by the renderer. They can be in a
3434 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3435 * else.
3436 *
3437 * Surface contents are copied into memory pointed to by target,
3438 * which has size bytes of space available. The target memory
3439 * may be larger than needed, but being smaller returns an error.
3440 * The extra bytes in target may or may not be written; their content is
3441 * unspecified. Size must be large enough to hold the image.
3442 *
3443 * The image in the target memory will be arranged in rows from
3444 * top to bottom, and pixels on a row from left to right. The pixel
3445 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3446 * width * 4.
3447 *
3448 * Parameters src_x and src_y define the upper-left corner in buffer
3449 * coordinates (pixels) to copy from. Parameters width and height
3450 * define the size of the area to copy in pixels.
3451 *
3452 * The rectangle defined by src_x, src_y, width, height must fit in
3453 * the surface contents. Otherwise an error is returned.
3454 *
3455 * Use surface_get_data_size to determine the content size; the
3456 * needed target buffer size and rectangle limits.
3457 *
3458 * CURRENT IMPLEMENTATION RESTRICTIONS:
3459 * - the machine must be little-endian due to Pixman formats.
3460 *
3461 * NOTE: Pixman formats are premultiplied.
3462 */
3463WL_EXPORT int
3464weston_surface_copy_content(struct weston_surface *surface,
3465 void *target, size_t size,
3466 int src_x, int src_y,
3467 int width, int height)
3468{
3469 struct weston_renderer *rer = surface->compositor->renderer;
3470 int cw, ch;
3471 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3472
3473 if (!rer->surface_copy_content)
3474 return -1;
3475
3476 weston_surface_get_content_size(surface, &cw, &ch);
3477
3478 if (src_x < 0 || src_y < 0)
3479 return -1;
3480
3481 if (width <= 0 || height <= 0)
3482 return -1;
3483
3484 if (src_x + width > cw || src_y + height > ch)
3485 return -1;
3486
3487 if (width * bytespp * height > size)
3488 return -1;
3489
3490 return rer->surface_copy_content(surface, target, size,
3491 src_x, src_y, width, height);
3492}
3493
Pekka Paalanene67858b2013-04-25 13:57:42 +03003494static void
3495subsurface_set_position(struct wl_client *client,
3496 struct wl_resource *resource, int32_t x, int32_t y)
3497{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003498 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003499
3500 if (!sub)
3501 return;
3502
3503 sub->position.x = x;
3504 sub->position.y = y;
3505 sub->position.set = 1;
3506}
3507
3508static struct weston_subsurface *
3509subsurface_from_surface(struct weston_surface *surface)
3510{
3511 struct weston_subsurface *sub;
3512
3513 sub = weston_surface_to_subsurface(surface);
3514 if (sub)
3515 return sub;
3516
3517 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3518 if (sub->surface == surface)
3519 return sub;
3520
3521 return NULL;
3522}
3523
3524static struct weston_subsurface *
3525subsurface_sibling_check(struct weston_subsurface *sub,
3526 struct weston_surface *surface,
3527 const char *request)
3528{
3529 struct weston_subsurface *sibling;
3530
3531 sibling = subsurface_from_surface(surface);
3532
3533 if (!sibling) {
3534 wl_resource_post_error(sub->resource,
3535 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3536 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003537 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003538 return NULL;
3539 }
3540
3541 if (sibling->parent != sub->parent) {
3542 wl_resource_post_error(sub->resource,
3543 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3544 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003545 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003546 return NULL;
3547 }
3548
3549 return sibling;
3550}
3551
3552static void
3553subsurface_place_above(struct wl_client *client,
3554 struct wl_resource *resource,
3555 struct wl_resource *sibling_resource)
3556{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003557 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003558 struct weston_surface *surface =
3559 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003560 struct weston_subsurface *sibling;
3561
3562 if (!sub)
3563 return;
3564
3565 sibling = subsurface_sibling_check(sub, surface, "place_above");
3566 if (!sibling)
3567 return;
3568
3569 wl_list_remove(&sub->parent_link_pending);
3570 wl_list_insert(sibling->parent_link_pending.prev,
3571 &sub->parent_link_pending);
3572}
3573
3574static void
3575subsurface_place_below(struct wl_client *client,
3576 struct wl_resource *resource,
3577 struct wl_resource *sibling_resource)
3578{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003579 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003580 struct weston_surface *surface =
3581 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003582 struct weston_subsurface *sibling;
3583
3584 if (!sub)
3585 return;
3586
3587 sibling = subsurface_sibling_check(sub, surface, "place_below");
3588 if (!sibling)
3589 return;
3590
3591 wl_list_remove(&sub->parent_link_pending);
3592 wl_list_insert(&sibling->parent_link_pending,
3593 &sub->parent_link_pending);
3594}
3595
3596static void
3597subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3598{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003599 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003600
3601 if (sub)
3602 sub->synchronized = 1;
3603}
3604
3605static void
3606subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3607{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003608 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003609
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003610 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003611 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003612
3613 /* If sub became effectively desynchronized, flush. */
3614 if (!weston_subsurface_is_synchronized(sub))
3615 weston_subsurface_synchronized_commit(sub);
3616 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003617}
3618
3619static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003620weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3621{
3622 wl_list_remove(&sub->parent_link);
3623 wl_list_remove(&sub->parent_link_pending);
3624 wl_list_remove(&sub->parent_destroy_listener.link);
3625 sub->parent = NULL;
3626}
3627
3628static void
3629weston_subsurface_destroy(struct weston_subsurface *sub);
3630
3631static void
3632subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3633{
3634 struct weston_subsurface *sub =
3635 container_of(listener, struct weston_subsurface,
3636 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003637 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003638
3639 /* The protocol object (wl_resource) is left inert. */
3640 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003641 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003642
3643 weston_subsurface_destroy(sub);
3644}
3645
3646static void
3647subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3648{
3649 struct weston_subsurface *sub =
3650 container_of(listener, struct weston_subsurface,
3651 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003652 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003653 assert(sub->surface != sub->parent);
3654
3655 if (weston_surface_is_mapped(sub->surface))
3656 weston_surface_unmap(sub->surface);
3657
3658 weston_subsurface_unlink_parent(sub);
3659}
3660
3661static void
3662subsurface_resource_destroy(struct wl_resource *resource)
3663{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003664 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003665
3666 if (sub)
3667 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003668}
3669
3670static void
3671subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3672{
3673 wl_resource_destroy(resource);
3674}
3675
3676static void
3677weston_subsurface_link_parent(struct weston_subsurface *sub,
3678 struct weston_surface *parent)
3679{
3680 sub->parent = parent;
3681 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003682 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003683 &sub->parent_destroy_listener);
3684
3685 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3686 wl_list_insert(&parent->subsurface_list_pending,
3687 &sub->parent_link_pending);
3688}
3689
3690static void
3691weston_subsurface_link_surface(struct weston_subsurface *sub,
3692 struct weston_surface *surface)
3693{
3694 sub->surface = surface;
3695 sub->surface_destroy_listener.notify =
3696 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003697 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003698 &sub->surface_destroy_listener);
3699}
3700
3701static void
3702weston_subsurface_destroy(struct weston_subsurface *sub)
3703{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003704 struct weston_view *view, *next;
3705
Pekka Paalanene67858b2013-04-25 13:57:42 +03003706 assert(sub->surface);
3707
3708 if (sub->resource) {
3709 assert(weston_surface_to_subsurface(sub->surface) == sub);
3710 assert(sub->parent_destroy_listener.notify ==
3711 subsurface_handle_parent_destroy);
3712
George Kiagiadakised04d382014-06-13 18:10:26 +02003713 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3714 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003715 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003716 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003717
Pekka Paalanene67858b2013-04-25 13:57:42 +03003718 if (sub->parent)
3719 weston_subsurface_unlink_parent(sub);
3720
Jason Ekstrand7b982072014-05-20 14:33:03 -05003721 weston_surface_state_fini(&sub->cached);
3722 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003723
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003724 sub->surface->committed = NULL;
3725 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003726 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003727 } else {
3728 /* the dummy weston_subsurface for the parent itself */
3729 assert(sub->parent_destroy_listener.notify == NULL);
3730 wl_list_remove(&sub->parent_link);
3731 wl_list_remove(&sub->parent_link_pending);
3732 }
3733
3734 wl_list_remove(&sub->surface_destroy_listener.link);
3735 free(sub);
3736}
3737
3738static const struct wl_subsurface_interface subsurface_implementation = {
3739 subsurface_destroy,
3740 subsurface_set_position,
3741 subsurface_place_above,
3742 subsurface_place_below,
3743 subsurface_set_sync,
3744 subsurface_set_desync
3745};
3746
3747static struct weston_subsurface *
3748weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3749 struct weston_surface *parent)
3750{
3751 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003752 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003753
Bryce Harringtonde16d892014-11-20 22:21:57 -08003754 sub = zalloc(sizeof *sub);
3755 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003756 return NULL;
3757
Jason Ekstranda7af7042013-10-12 22:38:11 -05003758 wl_list_init(&sub->unused_views);
3759
Jason Ekstranda85118c2013-06-27 20:17:02 -05003760 sub->resource =
3761 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003762 if (!sub->resource) {
3763 free(sub);
3764 return NULL;
3765 }
3766
Jason Ekstranda85118c2013-06-27 20:17:02 -05003767 wl_resource_set_implementation(sub->resource,
3768 &subsurface_implementation,
3769 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003770 weston_subsurface_link_surface(sub, surface);
3771 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003772 weston_surface_state_init(&sub->cached);
3773 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003774 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003775
3776 return sub;
3777}
3778
3779/* Create a dummy subsurface for having the parent itself in its
3780 * sub-surface lists. Makes stacking order manipulation easy.
3781 */
3782static struct weston_subsurface *
3783weston_subsurface_create_for_parent(struct weston_surface *parent)
3784{
3785 struct weston_subsurface *sub;
3786
Bryce Harringtonde16d892014-11-20 22:21:57 -08003787 sub = zalloc(sizeof *sub);
3788 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003789 return NULL;
3790
3791 weston_subsurface_link_surface(sub, parent);
3792 sub->parent = parent;
3793 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3794 wl_list_insert(&parent->subsurface_list_pending,
3795 &sub->parent_link_pending);
3796
3797 return sub;
3798}
3799
3800static void
3801subcompositor_get_subsurface(struct wl_client *client,
3802 struct wl_resource *resource,
3803 uint32_t id,
3804 struct wl_resource *surface_resource,
3805 struct wl_resource *parent_resource)
3806{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003807 struct weston_surface *surface =
3808 wl_resource_get_user_data(surface_resource);
3809 struct weston_surface *parent =
3810 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003811 struct weston_subsurface *sub;
3812 static const char where[] = "get_subsurface: wl_subsurface@";
3813
3814 if (surface == parent) {
3815 wl_resource_post_error(resource,
3816 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3817 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003818 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003819 return;
3820 }
3821
3822 if (weston_surface_to_subsurface(surface)) {
3823 wl_resource_post_error(resource,
3824 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3825 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003826 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003827 return;
3828 }
3829
Pekka Paalanen50b67472014-10-01 15:02:41 +03003830 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3831 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003832 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003833
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003834 if (weston_surface_get_main_surface(parent) == surface) {
3835 wl_resource_post_error(resource,
3836 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3837 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003838 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003839 return;
3840 }
3841
Pekka Paalanene67858b2013-04-25 13:57:42 +03003842 /* make sure the parent is in its own list */
3843 if (wl_list_empty(&parent->subsurface_list)) {
3844 if (!weston_subsurface_create_for_parent(parent)) {
3845 wl_resource_post_no_memory(resource);
3846 return;
3847 }
3848 }
3849
3850 sub = weston_subsurface_create(id, surface, parent);
3851 if (!sub) {
3852 wl_resource_post_no_memory(resource);
3853 return;
3854 }
3855
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003856 surface->committed = subsurface_committed;
3857 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003858 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003859}
3860
3861static void
3862subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3863{
3864 wl_resource_destroy(resource);
3865}
3866
3867static const struct wl_subcompositor_interface subcompositor_interface = {
3868 subcompositor_destroy,
3869 subcompositor_get_subsurface
3870};
3871
3872static void
3873bind_subcompositor(struct wl_client *client,
3874 void *data, uint32_t version, uint32_t id)
3875{
3876 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003877 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003878
Jason Ekstranda85118c2013-06-27 20:17:02 -05003879 resource =
3880 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003881 if (resource == NULL) {
3882 wl_client_post_no_memory(client);
3883 return;
3884 }
3885 wl_resource_set_implementation(resource, &subcompositor_interface,
3886 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003887}
3888
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003889/** Set a DPMS mode on all of the compositor's outputs
3890 *
3891 * \param compositor The compositor instance
3892 * \param state The DPMS state the outputs will be set to
3893 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003894static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003895weston_compositor_dpms(struct weston_compositor *compositor,
3896 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003897{
3898 struct weston_output *output;
3899
3900 wl_list_for_each(output, &compositor->output_list, link)
3901 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003902 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003903}
3904
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003905/** Restores the compositor to active status
3906 *
3907 * \param compositor The compositor instance
3908 *
3909 * If the compositor was in a sleeping mode, all outputs are powered
3910 * back on via DPMS. Otherwise if the compositor was inactive
3911 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3912 * signal will fire.
3913 *
3914 * Restarts the idle timer.
3915 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003916WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003917weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003918{
Neil Roberts8b62e202013-09-30 13:14:47 +01003919 uint32_t old_state = compositor->state;
3920
3921 /* The state needs to be changed before emitting the wake
3922 * signal because that may try to schedule a repaint which
3923 * will not work if the compositor is still sleeping */
3924 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3925
3926 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003927 case WESTON_COMPOSITOR_SLEEPING:
3928 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3929 /* fall through */
3930 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003931 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003932 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003933 /* fall through */
3934 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003935 wl_event_source_timer_update(compositor->idle_source,
3936 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003937 }
3938}
3939
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003940/** Turns off rendering and frame events for the compositor.
3941 *
3942 * \param compositor The compositor instance
3943 *
3944 * This is used for example to prevent further rendering while the
3945 * compositor is shutting down.
3946 *
3947 * \note When offscreen state is entered, outputs will be powered
3948 * back on if they were sleeping (in DPMS off mode), even though
3949 * no rendering will be performed.
3950 *
3951 * Stops the idle timer.
3952 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003953WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003954weston_compositor_offscreen(struct weston_compositor *compositor)
3955{
3956 switch (compositor->state) {
3957 case WESTON_COMPOSITOR_OFFSCREEN:
3958 return;
3959 case WESTON_COMPOSITOR_SLEEPING:
3960 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3961 /* fall through */
3962 default:
3963 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3964 wl_event_source_timer_update(compositor->idle_source, 0);
3965 }
3966}
3967
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003968/** Powers down all attached output devices
3969 *
3970 * \param compositor The compositor instance
3971 *
3972 * Causes rendering to the outputs to cease, and no frame events to be
3973 * sent. Only powers down the outputs if the compositor is not already
3974 * in sleep mode.
3975 *
3976 * Stops the idle timer.
3977 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003978WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003979weston_compositor_sleep(struct weston_compositor *compositor)
3980{
3981 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3982 return;
3983
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003984 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003985 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3986 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3987}
3988
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003989/** Sets compositor to idle mode
3990 *
3991 * \param data The compositor instance
3992 *
3993 * This is called when the idle timer fires. Once the compositor is in
3994 * idle mode it requires a wake action (e.g. via
3995 * weston_compositor_wake()) to restore it. The compositor's
3996 * idle_signal will be triggered when the idle event occurs.
3997 *
3998 * Idleness can be inhibited by setting the compositor's idle_inhibit
3999 * property.
4000 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004001static int
4002idle_handler(void *data)
4003{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004004 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004005
4006 if (compositor->idle_inhibit)
4007 return 1;
4008
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004009 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004010 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004011
4012 return 1;
4013}
4014
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004015WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004016weston_plane_init(struct weston_plane *plane,
4017 struct weston_compositor *ec,
4018 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004019{
4020 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004021 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004022 plane->x = x;
4023 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004024 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004025
4026 /* Init the link so that the call to wl_list_remove() when releasing
4027 * the plane without ever stacking doesn't lead to a crash */
4028 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004029}
4030
4031WL_EXPORT void
4032weston_plane_release(struct weston_plane *plane)
4033{
Xiong Zhang97116532013-10-23 13:58:31 +08004034 struct weston_view *view;
4035
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004036 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004037 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004038
Xiong Zhang97116532013-10-23 13:58:31 +08004039 wl_list_for_each(view, &plane->compositor->view_list, link) {
4040 if (view->plane == plane)
4041 view->plane = NULL;
4042 }
4043
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004044 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004045}
4046
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004047WL_EXPORT void
4048weston_compositor_stack_plane(struct weston_compositor *ec,
4049 struct weston_plane *plane,
4050 struct weston_plane *above)
4051{
4052 if (above)
4053 wl_list_insert(above->link.prev, &plane->link);
4054 else
4055 wl_list_insert(&ec->plane_list, &plane->link);
4056}
4057
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004058static void
4059output_release(struct wl_client *client, struct wl_resource *resource)
4060{
4061 wl_resource_destroy(resource);
4062}
4063
4064static const struct wl_output_interface output_interface = {
4065 output_release,
4066};
4067
4068
Casey Dahlin9074db52012-04-19 22:50:09 -04004069static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004070{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004071 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004072}
4073
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004074static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004075bind_output(struct wl_client *client,
4076 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004077{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004078 struct weston_output *output = data;
4079 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004080 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004081
Jason Ekstranda85118c2013-06-27 20:17:02 -05004082 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004083 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004084 if (resource == NULL) {
4085 wl_client_post_no_memory(client);
4086 return;
4087 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004088
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004089 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004090 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004091
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004092 wl_output_send_geometry(resource,
4093 output->x,
4094 output->y,
4095 output->mm_width,
4096 output->mm_height,
4097 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004098 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004099 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004100 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004101 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004102 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004103
4104 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004105 wl_output_send_mode(resource,
4106 mode->flags,
4107 mode->width,
4108 mode->height,
4109 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004110 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004111
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004112 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004113 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004114}
4115
David Fort0de859e2016-05-27 23:22:57 +02004116/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004117static void
David Fort0de859e2016-05-27 23:22:57 +02004118weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4119 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004120{
4121 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004122 bool start_resizing = false;
4123
4124 if (!delta_width)
4125 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004126
4127 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004128 if (output == resized_output) {
4129 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004130 continue;
4131 }
4132
David Fort0de859e2016-05-27 23:22:57 +02004133 if (start_resizing) {
4134 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004135 output->dirty = 1;
4136 }
4137 }
4138}
4139
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004140WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004141weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004142{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004143 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004144 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004145
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004146 output->destroying = 1;
4147
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004148 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08004149 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004150 weston_view_assign_output(view);
4151 }
4152
Pekka Paalanen0513a952014-05-21 16:17:27 +03004153 wl_event_source_remove(output->repaint_timer);
4154
Pekka Paalanen133e4392014-09-23 22:08:46 -04004155 weston_presentation_feedback_discard_list(&output->feedback_list);
4156
David Fort0de859e2016-05-27 23:22:57 +02004157 weston_compositor_reflow_outputs(output->compositor, output, output->width);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004158 wl_list_remove(&output->link);
4159
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004160 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004161 wl_signal_emit(&output->destroy_signal, output);
4162
Richard Hughesafe690c2013-05-02 10:10:04 +01004163 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004164 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004165 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08004166 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004167
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004168 wl_resource_for_each(resource, &output->resource_list) {
4169 wl_resource_set_destructor(resource, NULL);
4170 }
4171
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004172 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004173}
4174
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004175WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004176weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004177{
Scott Moreau850ca422012-05-21 15:21:25 -06004178 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004179
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004180 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004181 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004182
Scott Moreauccbf29d2012-02-22 14:21:41 -07004183 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004184 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004185 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004186 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004187 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004188 weston_matrix_scale(&output->matrix, magnification,
4189 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004190 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004191
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004192 switch (output->transform) {
4193 case WL_OUTPUT_TRANSFORM_FLIPPED:
4194 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4195 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4196 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4197 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4198 weston_matrix_scale(&output->matrix, -1, 1, 1);
4199 break;
4200 }
4201
4202 switch (output->transform) {
4203 default:
4204 case WL_OUTPUT_TRANSFORM_NORMAL:
4205 case WL_OUTPUT_TRANSFORM_FLIPPED:
4206 break;
4207 case WL_OUTPUT_TRANSFORM_90:
4208 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4209 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4210 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4211 break;
4212 case WL_OUTPUT_TRANSFORM_180:
4213 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4214 weston_matrix_translate(&output->matrix,
4215 -output->width, -output->height, 0);
4216 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4217 break;
4218 case WL_OUTPUT_TRANSFORM_270:
4219 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4220 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4221 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4222 break;
4223 }
4224
4225 if (output->current_scale != 1)
4226 weston_matrix_scale(&output->matrix,
4227 output->current_scale,
4228 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004229
Scott Moreauccbf29d2012-02-22 14:21:41 -07004230 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004231
4232 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004233}
4234
Scott Moreau1bad5db2012-08-18 01:04:05 -06004235static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004236weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004237{
4238 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004239 output->native_scale = scale;
4240 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004241
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004242 convert_size_by_transform_scale(&output->width, &output->height,
4243 output->current_mode->width,
4244 output->current_mode->height,
4245 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004246}
4247
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004248static void
4249weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004250{
4251 output->x = x;
4252 output->y = y;
4253
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004254 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004255 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004256 output->width,
4257 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004258}
4259
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004260WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004261weston_output_move(struct weston_output *output, int x, int y)
4262{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004263 struct wl_resource *resource;
4264
4265 output->move_x = x - output->x;
4266 output->move_y = y - output->y;
4267
4268 if (output->move_x == 0 && output->move_y == 0)
4269 return;
4270
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004271 weston_output_init_geometry(output, x, y);
4272
4273 output->dirty = 1;
4274
4275 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004276 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004277
4278 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004279 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004280 wl_output_send_geometry(resource,
4281 output->x,
4282 output->y,
4283 output->mm_width,
4284 output->mm_height,
4285 output->subpixel,
4286 output->make,
4287 output->model,
4288 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004289
FORT David8a120692016-04-26 23:34:06 +02004290 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004291 wl_output_send_done(resource);
4292 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004293}
4294
Bryce Harrington3f650b82015-12-23 11:01:58 -08004295/** Initialize a weston_output object's parameters
4296 *
4297 * \param output The weston_output object to initialize
4298 * \param c The output's compositor
4299 * \param x x coordinate for the output in global coordinate space
4300 * \param y y coordinate for the output in global coordinate space
4301 * \param mm_width Physical width of the output as reported by the backend
4302 * \param mm_height Physical height of the output as reported by the backend
4303 * \param transform Rotation of the output
4304 * \param scale Native scaling factor for the output
4305 *
4306 * Sets up the transformation, zoom, and geometry of the output using
4307 * the input properties.
4308 *
4309 * Establishes a repaint timer for the output with the relevant display
4310 * object's event loop. See output_repaint_timer_handler().
4311 *
4312 * The output is assigned an ID. Weston can support up to 32 distinct
4313 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4314 * is referred to and used to find the first available ID number, and
4315 * then this ID is marked as used in output_id_pool.
4316 *
4317 * The output is also assigned a Wayland global with the wl_output
4318 * external interface.
4319 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004320WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004321weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004322 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004323 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004324{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004325 struct wl_event_loop *loop;
4326
Bryce Harrington18e45732015-12-23 20:53:53 -08004327 /* Verify we haven't reached the limit of 32 available output IDs */
4328 assert(ffs(~c->output_id_pool) > 0);
4329
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004330 output->compositor = c;
4331 output->x = x;
4332 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004333 output->mm_width = mm_width;
4334 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004335 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004336 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004337
Alexander Larsson0b135062013-05-28 16:23:36 +02004338 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004339 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004340
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004341 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004342 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004343
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004344 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004345 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004346 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004347 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004348 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004349 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004350
Pekka Paalanen0513a952014-05-21 16:17:27 +03004351 loop = wl_display_get_event_loop(c->wl_display);
4352 output->repaint_timer = wl_event_loop_add_timer(loop,
4353 output_repaint_timer_handler, output);
4354
Bryce Harrington3f650b82015-12-23 11:01:58 -08004355 /* Invert the output id pool and look for the lowest numbered
4356 * switch (the least significant bit). Take that bit's position
4357 * as our ID, and mark it used in the compositor's output_id_pool.
4358 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004359 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004360 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004361
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004362 output->global =
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004363 wl_global_create(c->wl_display, &wl_output_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004364 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004365}
4366
4367/** Adds an output to the compositor's output list and
4368 * send the compositor's output_created signal.
4369 *
4370 * \param compositor The compositor instance.
4371 * \param output The output to be added.
4372 */
4373WL_EXPORT void
4374weston_compositor_add_output(struct weston_compositor *compositor,
4375 struct weston_output *output)
4376{
Armin Krezoviće5403842016-08-05 15:28:29 +02004377 struct weston_view *view, *next;
4378
Giulio Camuffob1147152015-05-06 21:41:57 +03004379 wl_list_insert(compositor->output_list.prev, &output->link);
4380 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02004381
4382 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4383 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004384}
4385
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004386WL_EXPORT void
4387weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004388 double device_x, double device_y,
4389 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004390{
Derek Foreman0f679412014-10-02 13:41:17 -05004391 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004392 device_x,
4393 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004394 0.0,
4395 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004396
Derek Foreman67a18b92015-03-24 11:36:14 -05004397 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004398
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004399 *x = p.f[0] / p.f[3];
4400 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004401}
4402
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004403static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004404destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004405{
Jonny Lamb74130762013-11-26 18:19:46 +01004406 struct weston_surface *surface =
4407 wl_resource_get_user_data(resource);
4408
Pekka Paalanen4826f872016-04-22 14:14:38 +03004409 if (!surface)
4410 return;
4411
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004412 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004413 surface->pending.buffer_viewport.buffer.src_width =
4414 wl_fixed_from_int(-1);
4415 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004416 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004417}
4418
4419static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004420viewport_destroy(struct wl_client *client,
4421 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004422{
4423 wl_resource_destroy(resource);
4424}
4425
4426static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004427viewport_set_source(struct wl_client *client,
4428 struct wl_resource *resource,
4429 wl_fixed_t src_x,
4430 wl_fixed_t src_y,
4431 wl_fixed_t src_width,
4432 wl_fixed_t src_height)
4433{
4434 struct weston_surface *surface =
4435 wl_resource_get_user_data(resource);
4436
Pekka Paalanen4826f872016-04-22 14:14:38 +03004437 if (!surface) {
4438 wl_resource_post_error(resource,
4439 WP_VIEWPORT_ERROR_NO_SURFACE,
4440 "wl_surface for this viewport is no longer exists");
4441 return;
4442 }
4443
4444 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004445 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004446
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004447 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004448 src_height == wl_fixed_from_int(-1) &&
4449 src_x == wl_fixed_from_int(-1) &&
4450 src_y == wl_fixed_from_int(-1)) {
4451 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004452 surface->pending.buffer_viewport.buffer.src_width =
4453 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004454 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004455 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004456 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004457
Pekka Paalanen201769a2016-04-26 14:42:11 +03004458 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004459 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004460 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03004461 "wl_surface@%d viewport source "
4462 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4463 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004464 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03004465 wl_fixed_to_double(src_height),
4466 wl_fixed_to_double(src_x),
4467 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004468 return;
4469 }
4470
4471 surface->pending.buffer_viewport.buffer.src_x = src_x;
4472 surface->pending.buffer_viewport.buffer.src_y = src_y;
4473 surface->pending.buffer_viewport.buffer.src_width = src_width;
4474 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004475 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004476}
4477
4478static void
4479viewport_set_destination(struct wl_client *client,
4480 struct wl_resource *resource,
4481 int32_t dst_width,
4482 int32_t dst_height)
4483{
4484 struct weston_surface *surface =
4485 wl_resource_get_user_data(resource);
4486
Pekka Paalanen4826f872016-04-22 14:14:38 +03004487 if (!surface) {
4488 wl_resource_post_error(resource,
4489 WP_VIEWPORT_ERROR_NO_SURFACE,
4490 "wl_surface for this viewport no longer exists");
4491 return;
4492 }
4493
4494 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004495
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004496 if (dst_width == -1 && dst_height == -1) {
4497 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004498 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004499 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004500 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004501 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004502
4503 if (dst_width <= 0 || dst_height <= 0) {
4504 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004505 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004506 "destination size must be positive (%dx%d)",
4507 dst_width, dst_height);
4508 return;
4509 }
4510
4511 surface->pending.buffer_viewport.surface.width = dst_width;
4512 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004513 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004514}
4515
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004516static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004517 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004518 viewport_set_source,
4519 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004520};
4521
4522static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004523viewporter_destroy(struct wl_client *client,
4524 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004525{
4526 wl_resource_destroy(resource);
4527}
4528
4529static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004530viewporter_get_viewport(struct wl_client *client,
4531 struct wl_resource *viewporter,
4532 uint32_t id,
4533 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004534{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004535 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004536 struct weston_surface *surface =
4537 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004538 struct wl_resource *resource;
4539
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004540 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004541 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004542 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004543 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004544 return;
4545 }
4546
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004547 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004548 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004549 if (resource == NULL) {
4550 wl_client_post_no_memory(client);
4551 return;
4552 }
4553
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004554 wl_resource_set_implementation(resource, &viewport_interface,
4555 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004556
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004557 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004558}
4559
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004560static const struct wp_viewporter_interface viewporter_interface = {
4561 viewporter_destroy,
4562 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004563};
4564
4565static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004566bind_viewporter(struct wl_client *client,
4567 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004568{
4569 struct wl_resource *resource;
4570
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004571 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004572 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004573 if (resource == NULL) {
4574 wl_client_post_no_memory(client);
4575 return;
4576 }
4577
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004578 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004579 NULL, NULL);
4580}
4581
4582static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004583destroy_presentation_feedback(struct wl_resource *feedback_resource)
4584{
4585 struct weston_presentation_feedback *feedback;
4586
4587 feedback = wl_resource_get_user_data(feedback_resource);
4588
4589 wl_list_remove(&feedback->link);
4590 free(feedback);
4591}
4592
4593static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004594presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4595{
4596 wl_resource_destroy(resource);
4597}
4598
4599static void
4600presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004601 struct wl_resource *presentation_resource,
4602 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004603 uint32_t callback)
4604{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004605 struct weston_surface *surface;
4606 struct weston_presentation_feedback *feedback;
4607
4608 surface = wl_resource_get_user_data(surface_resource);
4609
Bryce Harringtonde16d892014-11-20 22:21:57 -08004610 feedback = zalloc(sizeof *feedback);
4611 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004612 goto err_calloc;
4613
4614 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004615 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004616 1, callback);
4617 if (!feedback->resource)
4618 goto err_create;
4619
4620 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4621 destroy_presentation_feedback);
4622
4623 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4624
4625 return;
4626
4627err_create:
4628 free(feedback);
4629
4630err_calloc:
4631 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004632}
4633
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004634static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004635 presentation_destroy,
4636 presentation_feedback
4637};
4638
4639static void
4640bind_presentation(struct wl_client *client,
4641 void *data, uint32_t version, uint32_t id)
4642{
4643 struct weston_compositor *compositor = data;
4644 struct wl_resource *resource;
4645
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004646 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004647 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004648 if (resource == NULL) {
4649 wl_client_post_no_memory(client);
4650 return;
4651 }
4652
4653 wl_resource_set_implementation(resource, &presentation_implementation,
4654 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004655 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004656}
4657
4658static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004659compositor_bind(struct wl_client *client,
4660 void *data, uint32_t version, uint32_t id)
4661{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004662 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004663 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004664
Jason Ekstranda85118c2013-06-27 20:17:02 -05004665 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004666 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004667 if (resource == NULL) {
4668 wl_client_post_no_memory(client);
4669 return;
4670 }
4671
4672 wl_resource_set_implementation(resource, &compositor_interface,
4673 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004674}
4675
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004676WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004677weston_environment_get_fd(const char *env)
4678{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07004679 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004680 int fd, flags;
4681
4682 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07004683 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004684 return -1;
4685
4686 flags = fcntl(fd, F_GETFD);
4687 if (flags == -1)
4688 return -1;
4689
4690 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4691 unsetenv(env);
4692
4693 return fd;
4694}
4695
Pekka Paalanenb5026542014-11-12 15:09:24 +02004696static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004697timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004698 uint32_t key, void *data)
4699{
4700 struct weston_compositor *compositor = data;
4701
4702 if (weston_timeline_enabled_)
4703 weston_timeline_close();
4704 else
4705 weston_timeline_open(compositor);
4706}
4707
Giulio Camuffo459137b2014-10-11 23:56:24 +03004708/** Create the compositor.
4709 *
4710 * This functions creates and initializes a compositor instance.
4711 *
4712 * \param display The Wayland display to be used.
4713 * \param user_data A pointer to an object that can later be retrieved
4714 * using the \ref weston_compositor_get_user_data function.
4715 * \return The compositor instance on success or NULL on failure.
4716 */
4717WL_EXPORT struct weston_compositor *
4718weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004719{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004720 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004721 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004722
Giulio Camuffo459137b2014-10-11 23:56:24 +03004723 ec = zalloc(sizeof *ec);
4724 if (!ec)
4725 return NULL;
4726
4727 ec->wl_display = display;
4728 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004729 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004730 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004731 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004732 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004733 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004734 wl_signal_init(&ec->idle_signal);
4735 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004736 wl_signal_init(&ec->show_input_panel_signal);
4737 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004738 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004739 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004740 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004741 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004742 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004743 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004744 wl_signal_init(&ec->session_signal);
4745 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004746
Casey Dahlin58ba1372012-04-19 22:50:08 -04004747 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004748 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004749
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02004750 ec->activate_serial = 1;
4751
Derek Foreman152254b2015-11-26 14:17:48 -06004752 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004753 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004754 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004755
Giulio Camuffo954f1832014-10-11 18:27:30 +03004756 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004757 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004758 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004759
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004760 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004761 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004762 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004763
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004764 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004765 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004766 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004767
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004768 if (weston_input_init(ec) != 0)
4769 goto fail;
4770
Jason Ekstranda7af7042013-10-12 22:38:11 -05004771 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004772 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004773 wl_list_init(&ec->layer_list);
4774 wl_list_init(&ec->seat_list);
4775 wl_list_init(&ec->output_list);
4776 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004777 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004778 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004779 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004780 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004781 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004782
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004783 wl_list_init(&ec->plugin_api_list);
4784
Xiong Zhang97116532013-10-23 13:58:31 +08004785 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004786 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004787
Giulio Camuffo459137b2014-10-11 23:56:24 +03004788 wl_data_device_manager_init(ec->wl_display);
4789
4790 wl_display_init_shm(ec->wl_display);
4791
4792 loop = wl_display_get_event_loop(ec->wl_display);
4793 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004794
Giulio Camuffo459137b2014-10-11 23:56:24 +03004795 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4796 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4797
4798 weston_compositor_add_debug_binding(ec, KEY_T,
4799 timeline_key_binding_handler, ec);
4800
Giulio Camuffo459137b2014-10-11 23:56:24 +03004801 return ec;
4802
4803fail:
4804 free(ec);
4805 return NULL;
4806}
4807
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004808WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004809weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004810{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004811 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004812
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004813 wl_event_source_remove(ec->idle_source);
4814
Matt Roper361d2ad2011-08-29 13:52:23 -07004815 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004816 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004817 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004818
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004819 if (ec->renderer)
4820 ec->renderer->destroy(ec);
4821
Daniel Stone325fc2d2012-05-30 16:31:58 +01004822 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004823 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004824 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004825 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004826 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004827 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004828
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004829 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004830}
4831
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004832WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004833weston_compositor_exit_with_code(struct weston_compositor *compositor,
4834 int exit_code)
4835{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004836 if (compositor->exit_code == EXIT_SUCCESS)
4837 compositor->exit_code = exit_code;
4838
Giulio Camuffo459137b2014-10-11 23:56:24 +03004839 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004840}
4841
4842WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004843weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4844 const struct weston_pointer_grab_interface *interface)
4845{
4846 struct weston_seat *seat;
4847
4848 ec->default_pointer_grab = interface;
4849 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004850 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4851
4852 if (pointer)
4853 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004854 }
4855}
4856
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004857WL_EXPORT int
4858weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4859 clockid_t clk_id)
4860{
4861 struct timespec ts;
4862
4863 if (clock_gettime(clk_id, &ts) < 0)
4864 return -1;
4865
4866 compositor->presentation_clock = clk_id;
4867
4868 return 0;
4869}
4870
4871/*
4872 * For choosing the software clock, when the display hardware or API
4873 * does not expose a compatible presentation timestamp.
4874 */
4875WL_EXPORT int
4876weston_compositor_set_presentation_clock_software(
4877 struct weston_compositor *compositor)
4878{
4879 /* In order of preference */
4880 static const clockid_t clocks[] = {
4881 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4882 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4883 CLOCK_MONOTONIC, /* no jumps, may crawl */
4884 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4885 CLOCK_REALTIME /* may jump and crawl */
4886 };
4887 unsigned i;
4888
4889 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4890 if (weston_compositor_set_presentation_clock(compositor,
4891 clocks[i]) == 0)
4892 return 0;
4893
4894 weston_log("Error: no suitable presentation clock available.\n");
4895
4896 return -1;
4897}
4898
Pekka Paalanen662f3842015-03-18 12:17:26 +02004899/** Read the current time from the Presentation clock
4900 *
4901 * \param compositor
4902 * \param ts[out] The current time.
4903 *
4904 * \note Reading the current time in user space is always imprecise to some
4905 * degree.
4906 *
4907 * This function is never meant to fail. If reading the clock does fail,
4908 * an error message is logged and a zero time is returned. Callers are not
4909 * supposed to detect or react to failures.
4910 */
4911WL_EXPORT void
4912weston_compositor_read_presentation_clock(
4913 const struct weston_compositor *compositor,
4914 struct timespec *ts)
4915{
4916 static bool warned;
4917 int ret;
4918
4919 ret = clock_gettime(compositor->presentation_clock, ts);
4920 if (ret < 0) {
4921 ts->tv_sec = 0;
4922 ts->tv_nsec = 0;
4923
4924 if (!warned)
4925 weston_log("Error: failure to read "
4926 "the presentation clock %#x: '%m' (%d)\n",
4927 compositor->presentation_clock, errno);
4928 warned = true;
4929 }
4930}
4931
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004932/** Import dmabuf buffer into current renderer
4933 *
4934 * \param compositor
4935 * \param buffer the dmabuf buffer to import
4936 * \return true on usable buffers, false otherwise
4937 *
4938 * This function tests that the linux_dmabuf_buffer is usable
4939 * for the current renderer. Returns false on unusable buffers. Usually
4940 * usability is tested by importing the dmabufs for composition.
4941 *
4942 * This hook is also used for detecting if the renderer supports
4943 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4944 * supported.
4945 * */
4946WL_EXPORT bool
4947weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4948 struct linux_dmabuf_buffer *buffer)
4949{
4950 struct weston_renderer *renderer;
4951
4952 renderer = compositor->renderer;
4953
4954 if (renderer->import_dmabuf == NULL)
4955 return false;
4956
4957 return renderer->import_dmabuf(compositor, buffer);
4958}
4959
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004960WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004961weston_version(int *major, int *minor, int *micro)
4962{
4963 *major = WESTON_VERSION_MAJOR;
4964 *minor = WESTON_VERSION_MINOR;
4965 *micro = WESTON_VERSION_MICRO;
4966}
4967
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004968WL_EXPORT void *
4969weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004970{
Derek Foreman3f86e502015-06-08 11:46:54 -05004971 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004972 char path[PATH_MAX];
4973 void *module, *init;
4974
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004975 if (name == NULL)
4976 return NULL;
4977
Derek Foreman3f86e502015-06-08 11:46:54 -05004978 if (name[0] != '/') {
4979 if (builddir)
4980 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4981 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03004982 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004983 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004984 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004985 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004986
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004987 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4988 if (module) {
4989 weston_log("Module '%s' already loaded\n", path);
4990 dlclose(module);
4991 return NULL;
4992 }
4993
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004994 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004995 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004996 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004997 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004998 return NULL;
4999 }
5000
5001 init = dlsym(module, entrypoint);
5002 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005003 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00005004 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005005 return NULL;
5006 }
5007
5008 return init;
5009}
5010
Giulio Camuffo459137b2014-10-11 23:56:24 +03005011
5012/** Destroys the compositor.
5013 *
5014 * This function cleans up the compositor state and destroys it.
5015 *
5016 * \param compositor The compositor to be destroyed.
5017 */
5018WL_EXPORT void
5019weston_compositor_destroy(struct weston_compositor *compositor)
5020{
5021 /* prevent further rendering while shutting down */
5022 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5023
5024 wl_signal_emit(&compositor->destroy_signal, compositor);
5025
5026 weston_compositor_xkb_destroy(compositor);
5027
Giulio Camuffo2d24e642015-10-03 16:25:15 +03005028 if (compositor->backend)
5029 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005030
5031 weston_plugin_api_destroy_list(compositor);
5032
Giulio Camuffo459137b2014-10-11 23:56:24 +03005033 free(compositor);
5034}
5035
5036/** Instruct the compositor to exit.
5037 *
5038 * This functions does not directly destroy the compositor object, it merely
5039 * command it to start the tear down process. It is not guaranteed that the
5040 * tear down will happen immediately.
5041 *
5042 * \param compositor The compositor to tear down.
5043 */
5044WL_EXPORT void
5045weston_compositor_exit(struct weston_compositor *compositor)
5046{
5047 compositor->exit(compositor);
5048}
5049
5050/** Return the user data stored in the compositor.
5051 *
5052 * This function returns the user data pointer set with user_data parameter
5053 * to the \ref weston_compositor_create function.
5054 */
5055WL_EXPORT void *
5056weston_compositor_get_user_data(struct weston_compositor *compositor)
5057{
5058 return compositor->user_data;
5059}
Pekka Paalanendd186732016-06-03 14:49:54 +03005060
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005061static const char * const backend_map[] = {
5062 [WESTON_BACKEND_DRM] = "drm-backend.so",
5063 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5064 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5065 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5066 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5067 [WESTON_BACKEND_X11] = "x11-backend.so",
5068};
5069
Pekka Paalanendd186732016-06-03 14:49:54 +03005070/** Load a backend into a weston_compositor
5071 *
5072 * A backend must be loaded to make a weston_compositor work. A backend
5073 * provides input and output capabilities, and determines the renderer to use.
5074 *
5075 * \param compositor A compositor that has not had a backend loaded yet.
5076 * \param backend Name of the backend file.
5077 * \param config_base A pointer to a backend-specific configuration
5078 * structure's 'base' member.
5079 *
5080 * \return 0 on success, or -1 on error.
5081 */
5082WL_EXPORT int
5083weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005084 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005085 struct weston_backend_config *config_base)
5086{
5087 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005088 struct weston_backend_config *config_base);
5089
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005090 if (backend < 0 || backend >= ARRAY_LENGTH(backend_map))
5091 return -1;
5092
5093 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005094 if (!backend_init)
5095 return -1;
5096
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005097 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005098}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005099
5100WL_EXPORT int
5101weston_compositor_load_xwayland(struct weston_compositor *compositor)
5102{
5103 int (*module_init)(struct weston_compositor *ec,
5104 int *argc, char *argv[]);
5105 int argc = 0;
5106
5107 module_init = weston_load_module("xwayland.so", "module_init");
5108 if (!module_init)
5109 return -1;
5110 if (module_init(compositor, &argc, NULL) < 0)
5111 return -1;
5112 return 0;
5113}