blob: 64578584f4df2bb15c79c3dcd6c52dfa276426b1 [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
Giulio Camuffo148c1992016-09-04 18:50:46 +03001195 if (view->alpha == 1.0 &&
1196 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1197 pixman_region32_copy(&view->transform.opaque,
1198 &view->surface->opaque);
1199 pixman_region32_translate(&view->transform.opaque,
1200 matrix->d[12],
1201 matrix->d[13]);
1202 }
1203
Pekka Paalanen380adf52015-02-16 14:39:11 +02001204 pixman_region32_init_rect(&surfregion, 0, 0,
1205 view->surface->width, view->surface->height);
1206 if (view->geometry.scissor_enabled)
1207 pixman_region32_intersect(&surfregion, &surfregion,
1208 &view->geometry.scissor);
1209 surfbox = pixman_region32_extents(&surfregion);
1210
1211 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1212 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001213
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001214 return 0;
1215}
1216
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001217static struct weston_layer *
1218get_view_layer(struct weston_view *view)
1219{
1220 if (view->parent_view)
1221 return get_view_layer(view->parent_view);
1222 return view->layer_link.layer;
1223}
1224
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001225WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001227{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001229 struct weston_layer *layer;
1230 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001231
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001233 return;
1234
Pekka Paalanen483243f2013-03-08 14:56:50 +02001235 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001236 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001237
Jason Ekstranda7af7042013-10-12 22:38:11 -05001238 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001239
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001241
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 pixman_region32_fini(&view->transform.boundingbox);
1243 pixman_region32_fini(&view->transform.opaque);
1244 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001245
Pekka Paalanencd403622012-01-25 13:37:39 +02001246 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 if (view->geometry.transformation_list.next ==
1248 &view->transform.position.link &&
1249 view->geometry.transformation_list.prev ==
1250 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001251 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001253 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254 if (weston_view_update_transform_enable(view) < 0)
1255 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001256 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001257
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001258 layer = get_view_layer(view);
1259 if (layer) {
1260 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001261 pixman_region32_intersect(&view->transform.boundingbox,
1262 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001263 pixman_region32_intersect(&view->transform.opaque,
1264 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001265 pixman_region32_fini(&mask);
1266 }
1267
Pekka Paalanen380adf52015-02-16 14:39:11 +02001268 if (parent) {
1269 if (parent->geometry.scissor_enabled) {
1270 view->geometry.scissor_enabled = true;
1271 weston_view_transfer_scissor(parent, view);
1272 } else {
1273 view->geometry.scissor_enabled = false;
1274 }
1275 }
1276
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001278
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001280
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 wl_signal_emit(&view->surface->compositor->transform_signal,
1282 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001283}
1284
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001285WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001287{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001289
1290 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 * The invariant: if view->geometry.dirty, then all views
1292 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001293 * Corollary: if not parent->geometry.dirty, then all ancestors
1294 * are not dirty.
1295 */
1296
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001298 return;
1299
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001301
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001303 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001305}
1306
1307WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308weston_view_to_global_fixed(struct weston_view *view,
1309 wl_fixed_t vx, wl_fixed_t vy,
1310 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001311{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001312 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001313
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 weston_view_to_global_float(view,
1315 wl_fixed_to_double(vx),
1316 wl_fixed_to_double(vy),
1317 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001318 *x = wl_fixed_from_double(xf);
1319 *y = wl_fixed_from_double(yf);
1320}
1321
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001322WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323weston_view_from_global_float(struct weston_view *view,
1324 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001325{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001327 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1328
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001330
1331 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001332 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001334 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 *vx = 0;
1336 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001337 return;
1338 }
1339
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 *vx = v.f[0] / v.f[3];
1341 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001342 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 *vx = x - view->geometry.x;
1344 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001345 }
1346}
1347
1348WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349weston_view_from_global_fixed(struct weston_view *view,
1350 wl_fixed_t x, wl_fixed_t y,
1351 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001352{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001354
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355 weston_view_from_global_float(view,
1356 wl_fixed_to_double(x),
1357 wl_fixed_to_double(y),
1358 &vxf, &vyf);
1359 *vx = wl_fixed_from_double(vxf);
1360 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001361}
1362
1363WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364weston_view_from_global(struct weston_view *view,
1365 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001366{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001368
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1370 *vx = floorf(vxf);
1371 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001372}
1373
Bryce Harrington3f650b82015-12-23 11:01:58 -08001374/**
1375 * \param surface The surface to be repainted
1376 *
1377 * Marks the output(s) that the surface is shown on as needing to be
1378 * repainted. See weston_output_schedule_repaint().
1379 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001380WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001381weston_surface_schedule_repaint(struct weston_surface *surface)
1382{
1383 struct weston_output *output;
1384
1385 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001386 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001387 weston_output_schedule_repaint(output);
1388}
1389
Bryce Harrington3f650b82015-12-23 11:01:58 -08001390/**
1391 * \param view The view to be repainted
1392 *
1393 * Marks the output(s) that the view is shown on as needing to be
1394 * repainted. See weston_output_schedule_repaint().
1395 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001396WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001397weston_view_schedule_repaint(struct weston_view *view)
1398{
1399 struct weston_output *output;
1400
1401 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001402 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403 weston_output_schedule_repaint(output);
1404}
1405
Pekka Paalanene508ce62015-02-19 13:59:55 +02001406/**
1407 * XXX: This function does it the wrong way.
1408 * surface->damage is the damage from the client, and causes
1409 * surface_flush_damage() to copy pixels. No window management action can
1410 * cause damage to the client-provided content, warranting re-upload!
1411 *
1412 * Instead of surface->damage, this function should record the damage
1413 * with all the views for this surface to avoid extraneous texture
1414 * uploads.
1415 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001417weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001418{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001419 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 0, 0, surface->width,
1421 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001422
Kristian Høgsberg98238702012-08-03 16:29:12 -04001423 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001424}
1425
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001426WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001427weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001428{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001429 if (view->geometry.x == x && view->geometry.y == y)
1430 return;
1431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 view->geometry.x = x;
1433 view->geometry.y = y;
1434 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001435}
1436
Pekka Paalanen483243f2013-03-08 14:56:50 +02001437static void
1438transform_parent_handle_parent_destroy(struct wl_listener *listener,
1439 void *data)
1440{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441 struct weston_view *view =
1442 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001443 geometry.parent_destroy_listener);
1444
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001446}
1447
1448WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001450 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001451{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 if (view->geometry.parent) {
1453 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1454 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001455
1456 if (!parent)
1457 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001458 }
1459
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001461
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001463 transform_parent_handle_parent_destroy;
1464 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001465 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001467 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001468 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001469 }
1470
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471 weston_view_geometry_dirty(view);
1472}
1473
Pekka Paalanen380adf52015-02-16 14:39:11 +02001474/** Set a clip mask rectangle on a view
1475 *
1476 * \param view The view to set the clip mask on.
1477 * \param x Top-left corner X coordinate of the clip rectangle.
1478 * \param y Top-left corner Y coordinate of the clip rectangle.
1479 * \param width Width of the clip rectangle, non-negative.
1480 * \param height Height of the clip rectangle, non-negative.
1481 *
1482 * A shell may set a clip mask rectangle on a view. Everything outside
1483 * the rectangle is cut away for input and output purposes: it is
1484 * not drawn and cannot be hit by hit-test based input like pointer
1485 * motion or touch-downs. Everything inside the rectangle will behave
1486 * normally. Clients are unaware of clipping.
1487 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001488 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001489 * mask rectangle does not affect the view position, the view is positioned
1490 * as it would be without a clip. The clip also does not change
1491 * weston_surface::width,height.
1492 *
1493 * The clip mask rectangle is part of transformation inheritance
1494 * (weston_view_set_transform_parent()). A clip set in the root of the
1495 * transformation inheritance tree will affect all views in the tree.
1496 * A clip can be set only on the root view. Attempting to set a clip
1497 * on view that has a transformation parent will fail. Assigning a parent
1498 * to a view that has a clip set will cause the clip to be forgotten.
1499 *
1500 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1501 * on the additional transformations in the child views. These transformations
1502 * may not rotate the coordinate axes, i.e., only translation and scaling
1503 * are allowed. Violating this restriction causes the clipping to malfunction.
1504 * Furthermore, using scaling may cause rounding errors in child clipping.
1505 *
1506 * The clip mask rectangle is not automatically adjusted based on
1507 * wl_surface.attach dx and dy arguments.
1508 *
1509 * A clip mask rectangle can be set only if the compositor capability
1510 * WESTON_CAP_VIEW_CLIP_MASK is present.
1511 *
1512 * This function sets the clip mask rectangle and schedules a repaint for
1513 * the view.
1514 */
1515WL_EXPORT void
1516weston_view_set_mask(struct weston_view *view,
1517 int x, int y, int width, int height)
1518{
1519 struct weston_compositor *compositor = view->surface->compositor;
1520
1521 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1522 weston_log("%s not allowed without capability!\n", __func__);
1523 return;
1524 }
1525
1526 if (view->geometry.parent) {
1527 weston_log("view %p has a parent, clip forbidden!\n", view);
1528 return;
1529 }
1530
1531 if (width < 0 || height < 0) {
1532 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1533 x, y, width, height);
1534 return;
1535 }
1536
1537 pixman_region32_fini(&view->geometry.scissor);
1538 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1539 view->geometry.scissor_enabled = true;
1540 weston_view_geometry_dirty(view);
1541 weston_view_schedule_repaint(view);
1542}
1543
1544/** Remove the clip mask from a view
1545 *
1546 * \param view The view to remove the clip mask from.
1547 *
1548 * Removed the clip mask rectangle and schedules a repaint.
1549 *
1550 * \sa weston_view_set_mask
1551 */
1552WL_EXPORT void
1553weston_view_set_mask_infinite(struct weston_view *view)
1554{
1555 view->geometry.scissor_enabled = false;
1556 weston_view_geometry_dirty(view);
1557 weston_view_schedule_repaint(view);
1558}
1559
Armin Krezović0da12b82016-06-30 06:04:33 +02001560/* Check if view should be displayed
1561 *
1562 * The indicator is set manually when assigning
1563 * a view to a surface.
1564 *
1565 * This needs reworking. See the thread starting at:
1566 *
1567 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1568 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001569WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001570weston_view_is_mapped(struct weston_view *view)
1571{
Armin Krezović0da12b82016-06-30 06:04:33 +02001572 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001573}
1574
Armin Krezović0da12b82016-06-30 06:04:33 +02001575/* Check if a surface has a view assigned to it
1576 *
1577 * The indicator is set manually when mapping
1578 * a surface and creating a view for it.
1579 *
1580 * This needs to go. See the thread starting at:
1581 *
1582 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1583 *
1584 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001585WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001586weston_surface_is_mapped(struct weston_surface *surface)
1587{
Armin Krezović0da12b82016-06-30 06:04:33 +02001588 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001589}
1590
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001591static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001592surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001593{
1594 struct weston_view *view;
1595
1596 if (surface->width == width && surface->height == height)
1597 return;
1598
1599 surface->width = width;
1600 surface->height = height;
1601
1602 wl_list_for_each(view, &surface->views, surface_link)
1603 weston_view_geometry_dirty(view);
1604}
1605
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001606WL_EXPORT void
1607weston_surface_set_size(struct weston_surface *surface,
1608 int32_t width, int32_t height)
1609{
1610 assert(!surface->resource);
1611 surface_set_size(surface, width, height);
1612}
1613
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001614static int
1615fixed_round_up_to_int(wl_fixed_t f)
1616{
1617 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1618}
1619
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001620static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001621convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1622 int32_t width, int32_t height,
1623 uint32_t transform,
1624 int32_t scale)
1625{
1626 assert(scale > 0);
1627
1628 switch (transform) {
1629 case WL_OUTPUT_TRANSFORM_NORMAL:
1630 case WL_OUTPUT_TRANSFORM_180:
1631 case WL_OUTPUT_TRANSFORM_FLIPPED:
1632 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1633 *width_out = width / scale;
1634 *height_out = height / scale;
1635 break;
1636 case WL_OUTPUT_TRANSFORM_90:
1637 case WL_OUTPUT_TRANSFORM_270:
1638 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1639 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1640 *width_out = height / scale;
1641 *height_out = width / scale;
1642 break;
1643 default:
1644 assert(0 && "invalid transform");
1645 }
1646}
1647
1648static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001649weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001650{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001651 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001652
1653 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001654 surface->width_from_buffer = 0;
1655 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001656 return;
1657 }
1658
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001659 convert_size_by_transform_scale(&surface->width_from_buffer,
1660 &surface->height_from_buffer,
1661 surface->buffer_ref.buffer->width,
1662 surface->buffer_ref.buffer->height,
1663 vp->buffer.transform,
1664 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001665}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001666
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001667static void
1668weston_surface_update_size(struct weston_surface *surface)
1669{
1670 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1671 int32_t width, height;
1672
1673 width = surface->width_from_buffer;
1674 height = surface->height_from_buffer;
1675
1676 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001677 surface_set_size(surface,
1678 vp->surface.width, vp->surface.height);
1679 return;
1680 }
1681
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001682 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001683 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1684 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1685
1686 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001687 return;
1688 }
1689
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001690 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001691}
1692
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001693WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001694weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001695{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001696 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001697
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001698 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001699
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001700 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001701}
1702
Jason Ekstranda7af7042013-10-12 22:38:11 -05001703WL_EXPORT struct weston_view *
1704weston_compositor_pick_view(struct weston_compositor *compositor,
1705 wl_fixed_t x, wl_fixed_t y,
1706 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001707{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001708 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001709 wl_fixed_t view_x, view_y;
1710 int view_ix, view_iy;
1711 int ix = wl_fixed_to_int(x);
1712 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001713
Jason Ekstranda7af7042013-10-12 22:38:11 -05001714 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001715 if (!pixman_region32_contains_point(
1716 &view->transform.boundingbox, ix, iy, NULL))
1717 continue;
1718
1719 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1720 view_ix = wl_fixed_to_int(view_x);
1721 view_iy = wl_fixed_to_int(view_y);
1722
1723 if (!pixman_region32_contains_point(&view->surface->input,
1724 view_ix, view_iy, NULL))
1725 continue;
1726
Pekka Paalanen380adf52015-02-16 14:39:11 +02001727 if (view->geometry.scissor_enabled &&
1728 !pixman_region32_contains_point(&view->geometry.scissor,
1729 view_ix, view_iy, NULL))
1730 continue;
1731
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001732 *vx = view_x;
1733 *vy = view_y;
1734 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001735 }
1736
Derek Foremanf9318d12015-05-11 15:40:11 -05001737 *vx = wl_fixed_from_int(-1000000);
1738 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001739 return NULL;
1740}
1741
1742static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001743weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001744{
Daniel Stone37816df2012-05-16 18:45:18 +01001745 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001746
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001747 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001748 return;
1749
Daniel Stone37816df2012-05-16 18:45:18 +01001750 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001751 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001752}
1753
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001754WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001755weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001756{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001757 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001758
Jason Ekstranda7af7042013-10-12 22:38:11 -05001759 if (!weston_view_is_mapped(view))
1760 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001761
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762 weston_view_damage_below(view);
1763 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001764 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001765 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001766 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767 wl_list_remove(&view->link);
1768 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001769 view->output_mask = 0;
1770 weston_surface_assign_output(view->surface);
1771
1772 if (weston_surface_is_mapped(view->surface))
1773 return;
1774
1775 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001776 struct weston_touch *touch = weston_seat_get_touch(seat);
1777 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1778 struct weston_keyboard *keyboard =
1779 weston_seat_get_keyboard(seat);
1780
1781 if (keyboard && keyboard->focus == view->surface)
1782 weston_keyboard_set_focus(keyboard, NULL);
1783 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001784 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001785 if (touch && touch->focus == view)
1786 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001787 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001788}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001789
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790WL_EXPORT void
1791weston_surface_unmap(struct weston_surface *surface)
1792{
1793 struct weston_view *view;
1794
Armin Krezovićf8486c32016-06-30 06:04:28 +02001795 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796 wl_list_for_each(view, &surface->views, surface_link)
1797 weston_view_unmap(view);
1798 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001799}
1800
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001801static void
1802weston_surface_reset_pending_buffer(struct weston_surface *surface)
1803{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001804 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001805 surface->pending.sx = 0;
1806 surface->pending.sy = 0;
1807 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001808 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001809}
1810
Jason Ekstranda7af7042013-10-12 22:38:11 -05001811WL_EXPORT void
1812weston_view_destroy(struct weston_view *view)
1813{
1814 wl_signal_emit(&view->destroy_signal, view);
1815
1816 assert(wl_list_empty(&view->geometry.child_list));
1817
1818 if (weston_view_is_mapped(view)) {
1819 weston_view_unmap(view);
1820 weston_compositor_build_view_list(view->surface->compositor);
1821 }
1822
1823 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001824 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825
1826 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001827 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001829 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001830
1831 weston_view_set_transform_parent(view, NULL);
1832
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833 wl_list_remove(&view->surface_link);
1834
1835 free(view);
1836}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001837
1838WL_EXPORT void
1839weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001840{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001841 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001842 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001843 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001844
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001845 if (--surface->ref_count > 0)
1846 return;
1847
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001848 assert(surface->resource == NULL);
1849
Pekka Paalanenca790762015-04-17 14:23:38 +03001850 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001851
Pekka Paalanene67858b2013-04-25 13:57:42 +03001852 assert(wl_list_empty(&surface->subsurface_list_pending));
1853 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001854
Jason Ekstranda7af7042013-10-12 22:38:11 -05001855 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1856 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001857
Jason Ekstrand7b982072014-05-20 14:33:03 -05001858 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001859
Pekka Paalanende685b82012-12-04 15:58:12 +02001860 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001861
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001862 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001863 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001864 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001865
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001866 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001867 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001868
Pekka Paalanen133e4392014-09-23 22:08:46 -04001869 weston_presentation_feedback_discard_list(&surface->feedback_list);
1870
Jonas Ådahld3414f22016-07-22 17:56:31 +08001871 wl_list_for_each_safe(constraint, next_constraint,
1872 &surface->pointer_constraints,
1873 link)
1874 weston_pointer_constraint_destroy(constraint);
1875
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001876 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001877}
1878
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001879static void
1880destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001881{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001882 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001883
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001884 assert(surface);
1885
Giulio Camuffo0d379742013-11-15 22:06:15 +01001886 /* Set the resource to NULL, since we don't want to leave a
1887 * dangling pointer if the surface was refcounted and survives
1888 * the weston_surface_destroy() call. */
1889 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001890
1891 if (surface->viewport_resource)
1892 wl_resource_set_user_data(surface->viewport_resource, NULL);
1893
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001894 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001895}
1896
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001897static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001898weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1899{
1900 struct weston_buffer *buffer =
1901 container_of(listener, struct weston_buffer, destroy_listener);
1902
1903 wl_signal_emit(&buffer->destroy_signal, buffer);
1904 free(buffer);
1905}
1906
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001907WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001908weston_buffer_from_resource(struct wl_resource *resource)
1909{
1910 struct weston_buffer *buffer;
1911 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001912
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001913 listener = wl_resource_get_destroy_listener(resource,
1914 weston_buffer_destroy_handler);
1915
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001916 if (listener)
1917 return container_of(listener, struct weston_buffer,
1918 destroy_listener);
1919
1920 buffer = zalloc(sizeof *buffer);
1921 if (buffer == NULL)
1922 return NULL;
1923
1924 buffer->resource = resource;
1925 wl_signal_init(&buffer->destroy_signal);
1926 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001927 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001928 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001929
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001930 return buffer;
1931}
1932
1933static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001934weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1935 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001936{
Pekka Paalanende685b82012-12-04 15:58:12 +02001937 struct weston_buffer_reference *ref =
1938 container_of(listener, struct weston_buffer_reference,
1939 destroy_listener);
1940
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001941 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001942 ref->buffer = NULL;
1943}
1944
1945WL_EXPORT void
1946weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001947 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001948{
1949 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001950 ref->buffer->busy_count--;
1951 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001952 assert(wl_resource_get_client(ref->buffer->resource));
1953 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001954 WL_BUFFER_RELEASE);
1955 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001956 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001957 }
1958
Pekka Paalanende685b82012-12-04 15:58:12 +02001959 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001960 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001961 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001962 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001963 }
1964
Pekka Paalanende685b82012-12-04 15:58:12 +02001965 ref->buffer = buffer;
1966 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1967}
1968
1969static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001970weston_surface_attach(struct weston_surface *surface,
1971 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001972{
1973 weston_buffer_reference(&surface->buffer_ref, buffer);
1974
Pekka Paalanena6421c42012-12-04 15:58:10 +02001975 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001976 if (weston_surface_is_mapped(surface))
1977 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001978 }
1979
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001980 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001981
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001982 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001983 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001984}
1985
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001986WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001987weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001988{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001989 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001990
1991 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001992 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001993}
1994
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001995WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001996weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001997{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001998 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001999
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002000 pixman_region32_union(&compositor->primary_plane.damage,
2001 &compositor->primary_plane.damage,
2002 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002003 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002004}
2005
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002006static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002007surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002008{
Pekka Paalanende685b82012-12-04 15:58:12 +02002009 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002010 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002011 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002012
Pekka Paalanenb5026542014-11-12 15:09:24 +02002013 if (weston_timeline_enabled_ &&
2014 pixman_region32_not_empty(&surface->damage))
2015 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2016 TLP_OUTPUT(surface->output), TLP_END);
2017
Jason Ekstrandef540082014-06-26 10:37:36 -07002018 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002019}
2020
2021static void
2022view_accumulate_damage(struct weston_view *view,
2023 pixman_region32_t *opaque)
2024{
2025 pixman_region32_t damage;
2026
2027 pixman_region32_init(&damage);
2028 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002029 pixman_box32_t *extents;
2030
Jason Ekstranda7af7042013-10-12 22:38:11 -05002031 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002032 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002033 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002034 pixman_region32_copy(&damage, &view->surface->damage);
2035 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002036 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002037 }
2038
Pekka Paalanen380adf52015-02-16 14:39:11 +02002039 pixman_region32_intersect(&damage, &damage,
2040 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002041 pixman_region32_subtract(&damage, &damage, opaque);
2042 pixman_region32_union(&view->plane->damage,
2043 &view->plane->damage, &damage);
2044 pixman_region32_fini(&damage);
2045 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002046 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002047}
2048
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002049static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002050compositor_accumulate_damage(struct weston_compositor *ec)
2051{
2052 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002053 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002054 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002055
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002056 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002057
2058 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002059 pixman_region32_copy(&plane->clip, &clip);
2060
2061 pixman_region32_init(&opaque);
2062
Jason Ekstranda7af7042013-10-12 22:38:11 -05002063 wl_list_for_each(ev, &ec->view_list, link) {
2064 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002065 continue;
2066
Jason Ekstranda7af7042013-10-12 22:38:11 -05002067 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002068 }
2069
2070 pixman_region32_union(&clip, &clip, &opaque);
2071 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002072 }
2073
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002074 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002075
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002077 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002078
2079 wl_list_for_each(ev, &ec->view_list, link) {
2080 if (ev->surface->touched)
2081 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002082 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083
2084 surface_flush_damage(ev->surface);
2085
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002086 /* Both the renderer and the backend have seen the buffer
2087 * by now. If renderer needs the buffer, it has its own
2088 * reference set. If the backend wants to keep the buffer
2089 * around for migrating the surface into a non-primary plane
2090 * later, keep_buffer is true. Otherwise, drop the core
2091 * reference now, and allow early buffer release. This enables
2092 * clients to use single-buffering.
2093 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002094 if (!ev->surface->keep_buffer)
2095 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002096 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002097}
2098
2099static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002100surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002101{
2102 struct weston_subsurface *sub;
2103
Pekka Paalanene67858b2013-04-25 13:57:42 +03002104 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002105 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002106 continue;
2107
Jason Ekstranda7af7042013-10-12 22:38:11 -05002108 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2109 wl_list_init(&sub->surface->views);
2110
2111 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002112 }
2113}
2114
2115static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002116surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002117{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002118 struct weston_subsurface *sub;
2119 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002120
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2122 if (sub->surface == surface)
2123 continue;
2124
George Kiagiadakised04d382014-06-13 18:10:26 +02002125 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2126 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002128 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002129
2130 surface_free_unused_subsurface_views(sub->surface);
2131 }
2132}
2133
2134static void
2135view_list_add_subsurface_view(struct weston_compositor *compositor,
2136 struct weston_subsurface *sub,
2137 struct weston_view *parent)
2138{
2139 struct weston_subsurface *child;
2140 struct weston_view *view = NULL, *iv;
2141
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002142 if (!weston_surface_is_mapped(sub->surface))
2143 return;
2144
Jason Ekstranda7af7042013-10-12 22:38:11 -05002145 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2146 if (iv->geometry.parent == parent) {
2147 view = iv;
2148 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002149 }
2150 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002151
2152 if (view) {
2153 /* Put it back in the surface's list of views */
2154 wl_list_remove(&view->surface_link);
2155 wl_list_insert(&sub->surface->views, &view->surface_link);
2156 } else {
2157 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002158 weston_view_set_position(view,
2159 sub->position.x,
2160 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161 weston_view_set_transform_parent(view, parent);
2162 }
2163
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002164 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002165 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002166 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167
Pekka Paalanenb188e912013-11-19 14:03:35 +02002168 if (wl_list_empty(&sub->surface->subsurface_list)) {
2169 wl_list_insert(compositor->view_list.prev, &view->link);
2170 return;
2171 }
2172
2173 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2174 if (child->surface == sub->surface)
2175 wl_list_insert(compositor->view_list.prev, &view->link);
2176 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002178 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179}
2180
2181static void
2182view_list_add(struct weston_compositor *compositor,
2183 struct weston_view *view)
2184{
2185 struct weston_subsurface *sub;
2186
2187 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002188
Pekka Paalanenb188e912013-11-19 14:03:35 +02002189 if (wl_list_empty(&view->surface->subsurface_list)) {
2190 wl_list_insert(compositor->view_list.prev, &view->link);
2191 return;
2192 }
2193
2194 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2195 if (sub->surface == view->surface)
2196 wl_list_insert(compositor->view_list.prev, &view->link);
2197 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002198 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002199 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002200}
2201
2202static void
2203weston_compositor_build_view_list(struct weston_compositor *compositor)
2204{
2205 struct weston_view *view;
2206 struct weston_layer *layer;
2207
2208 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002209 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002210 surface_stash_subsurface_views(view->surface);
2211
2212 wl_list_init(&compositor->view_list);
2213 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002214 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002215 view_list_add(compositor, view);
2216 }
2217 }
2218
2219 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002220 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002221 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002222}
2223
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002224static void
2225weston_output_take_feedback_list(struct weston_output *output,
2226 struct weston_surface *surface)
2227{
2228 struct weston_view *view;
2229 struct weston_presentation_feedback *feedback;
2230 uint32_t flags = 0xffffffff;
2231
2232 if (wl_list_empty(&surface->feedback_list))
2233 return;
2234
2235 /* All views must have the flag for the flag to survive. */
2236 wl_list_for_each(view, &surface->views, surface_link) {
2237 /* ignore views that are not on this output at all */
2238 if (view->output_mask & (1u << output->id))
2239 flags &= view->psf_flags;
2240 }
2241
2242 wl_list_for_each(feedback, &surface->feedback_list, link)
2243 feedback->psf_flags = flags;
2244
2245 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2246 wl_list_init(&surface->feedback_list);
2247}
2248
David Herrmann1edf44c2013-10-22 17:11:26 +02002249static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002250weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002251{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002252 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002253 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002254 struct weston_animation *animation, *next;
2255 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002256 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002257 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002258 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002259
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002260 if (output->destroying)
2261 return 0;
2262
Pekka Paalanenb5026542014-11-12 15:09:24 +02002263 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2264
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002265 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002266 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002267
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002268 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002269 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002270 } else {
2271 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002272 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002273 ev->psf_flags = 0;
2274 }
2275 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002276
Pekka Paalanene67858b2013-04-25 13:57:42 +03002277 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002278 wl_list_for_each(ev, &ec->view_list, link) {
2279 /* Note: This operation is safe to do multiple times on the
2280 * same surface.
2281 */
2282 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002283 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002284 &ev->surface->frame_callback_list);
2285 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002286
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002287 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002288 }
2289 }
2290
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002291 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002292
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002293 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002294 pixman_region32_intersect(&output_damage,
2295 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002296 pixman_region32_subtract(&output_damage,
2297 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002298
Scott Moreauccbf29d2012-02-22 14:21:41 -07002299 if (output->dirty)
2300 weston_output_update_matrix(output);
2301
David Herrmann1edf44c2013-10-22 17:11:26 +02002302 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002303
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002304 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002305
Kristian Høgsbergef044142011-06-21 15:02:12 -04002306 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002307
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002308 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002309
Jonas Ådahldb773762012-06-13 00:01:21 +02002310 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002311 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002312 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002313 }
2314
Scott Moreaud64cf212012-06-08 19:40:54 -06002315 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002316 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002317 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002318 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002319
Pekka Paalanenb5026542014-11-12 15:09:24 +02002320 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2321
David Herrmann1edf44c2013-10-22 17:11:26 +02002322 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002323}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002324
Pekka Paalanen82919792014-05-21 13:51:49 +03002325static void
2326weston_output_schedule_repaint_reset(struct weston_output *output)
2327{
Pekka Paalanen82919792014-05-21 13:51:49 +03002328 output->repaint_scheduled = 0;
2329 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002330}
2331
Pekka Paalanen0513a952014-05-21 16:17:27 +03002332static int
2333output_repaint_timer_handler(void *data)
2334{
2335 struct weston_output *output = data;
2336 struct weston_compositor *compositor = output->compositor;
2337
2338 if (output->repaint_needed &&
Bryce Harrington08976ac2016-08-30 12:05:16 -07002339 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
Pekka Paalanen0513a952014-05-21 16:17:27 +03002340 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2341 weston_output_repaint(output) == 0)
2342 return 0;
2343
2344 weston_output_schedule_repaint_reset(output);
2345
2346 return 0;
2347}
2348
Kristian Høgsbergef044142011-06-21 15:02:12 -04002349WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002350weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002351 const struct timespec *stamp,
2352 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002353{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002354 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002355 int32_t refresh_nsec;
2356 struct timespec now;
2357 struct timespec gone;
2358 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002359
Pekka Paalanenb5026542014-11-12 15:09:24 +02002360 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2361 TLP_VBLANK(stamp), TLP_END);
2362
Pekka Paalanend7894d02015-07-03 15:08:53 +03002363 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002364 weston_presentation_feedback_present_list(&output->feedback_list,
2365 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002366 output->msc,
2367 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002368
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002369 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002370
Pekka Paalanen0513a952014-05-21 16:17:27 +03002371 weston_compositor_read_presentation_clock(compositor, &now);
2372 timespec_sub(&gone, &now, stamp);
2373 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2374 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002375
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002376 if (msec < -1000 || msec > 1000) {
2377 static bool warned;
2378
2379 if (!warned)
2380 weston_log("Warning: computed repaint delay is "
2381 "insane: %d msec\n", msec);
2382 warned = true;
2383
2384 msec = 0;
2385 }
2386
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002387 /* Called from restart_repaint_loop and restart happens already after
2388 * the deadline given by repaint_msec? In that case we delay until
2389 * the deadline of the next frame, to give clients a more predictable
2390 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002391 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002392 msec += refresh_nsec / 1000000;
2393
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002394 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002395 output_repaint_timer_handler(output);
2396 else
2397 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002398}
2399
2400static void
2401idle_repaint(void *data)
2402{
2403 struct weston_output *output = data;
2404
Jonas Ådahle5a12252013-04-05 23:07:11 +02002405 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002406}
2407
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002408WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002409weston_layer_entry_insert(struct weston_layer_entry *list,
2410 struct weston_layer_entry *entry)
2411{
2412 wl_list_insert(&list->link, &entry->link);
2413 entry->layer = list->layer;
2414}
2415
2416WL_EXPORT void
2417weston_layer_entry_remove(struct weston_layer_entry *entry)
2418{
2419 wl_list_remove(&entry->link);
2420 wl_list_init(&entry->link);
2421 entry->layer = NULL;
2422}
2423
2424WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002425weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2426{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002427 wl_list_init(&layer->view_list.link);
2428 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002429 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002430 if (below != NULL)
2431 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002432}
2433
2434WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002435weston_layer_set_mask(struct weston_layer *layer,
2436 int x, int y, int width, int height)
2437{
2438 struct weston_view *view;
2439
2440 layer->mask.x1 = x;
2441 layer->mask.x2 = x + width;
2442 layer->mask.y1 = y;
2443 layer->mask.y2 = y + height;
2444
2445 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2446 weston_view_geometry_dirty(view);
2447 }
2448}
2449
2450WL_EXPORT void
2451weston_layer_set_mask_infinite(struct weston_layer *layer)
2452{
2453 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2454 UINT32_MAX, UINT32_MAX);
2455}
2456
2457WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002458weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002459{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002460 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002461 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002462
Bryce Harrington08976ac2016-08-30 12:05:16 -07002463 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2464 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002465 return;
2466
Pekka Paalanenb5026542014-11-12 15:09:24 +02002467 if (!output->repaint_needed)
2468 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2469
Kristian Høgsbergef044142011-06-21 15:02:12 -04002470 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002471 output->repaint_needed = 1;
2472 if (output->repaint_scheduled)
2473 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002474
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002475 wl_event_loop_add_idle(loop, idle_repaint, output);
2476 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002477 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002478}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002479
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002480WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002481weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2482{
2483 struct weston_output *output;
2484
2485 wl_list_for_each(output, &compositor->output_list, link)
2486 weston_output_schedule_repaint(output);
2487}
2488
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002489static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002490surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002491{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002492 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002493}
2494
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002495static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002496surface_attach(struct wl_client *client,
2497 struct wl_resource *resource,
2498 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2499{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002500 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002501 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002502
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002503 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002504 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002505 if (buffer == NULL) {
2506 wl_client_post_no_memory(client);
2507 return;
2508 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002509 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002510
Pekka Paalanende685b82012-12-04 15:58:12 +02002511 /* Attach, attach, without commit in between does not send
2512 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002513 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002514
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002515 surface->pending.sx = sx;
2516 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002517 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002518}
2519
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002520static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002521surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002522 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002523 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002524{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002525 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002526
Derek Foreman57e92ed2015-11-17 14:11:35 -06002527 if (width <= 0 || height <= 0)
2528 return;
2529
Derek Foreman152254b2015-11-26 14:17:48 -06002530 pixman_region32_union_rect(&surface->pending.damage_surface,
2531 &surface->pending.damage_surface,
2532 x, y, width, height);
2533}
2534
2535static void
2536surface_damage_buffer(struct wl_client *client,
2537 struct wl_resource *resource,
2538 int32_t x, int32_t y, int32_t width, int32_t height)
2539{
2540 struct weston_surface *surface = wl_resource_get_user_data(resource);
2541
2542 if (width <= 0 || height <= 0)
2543 return;
2544
2545 pixman_region32_union_rect(&surface->pending.damage_buffer,
2546 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002547 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002548}
2549
Kristian Høgsberg33418202011-08-16 23:01:28 -04002550static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002551destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002552{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002553 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002554
2555 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002556 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002557}
2558
2559static void
2560surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002561 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002562{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002563 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002564 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002565
2566 cb = malloc(sizeof *cb);
2567 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002568 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002569 return;
2570 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002571
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002572 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2573 callback);
2574 if (cb->resource == NULL) {
2575 free(cb);
2576 wl_resource_post_no_memory(resource);
2577 return;
2578 }
2579
Jason Ekstranda85118c2013-06-27 20:17:02 -05002580 wl_resource_set_implementation(cb->resource, NULL, cb,
2581 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002582
Pekka Paalanenbc106382012-10-10 12:49:31 +03002583 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002584}
2585
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002586static void
2587surface_set_opaque_region(struct wl_client *client,
2588 struct wl_resource *resource,
2589 struct wl_resource *region_resource)
2590{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002591 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002592 struct weston_region *region;
2593
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002594 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002595 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002596 pixman_region32_copy(&surface->pending.opaque,
2597 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002598 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002599 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002600 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002601}
2602
2603static void
2604surface_set_input_region(struct wl_client *client,
2605 struct wl_resource *resource,
2606 struct wl_resource *region_resource)
2607{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002608 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002609 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002610
2611 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002612 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002613 pixman_region32_copy(&surface->pending.input,
2614 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002615 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002616 pixman_region32_fini(&surface->pending.input);
2617 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002618 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002619}
2620
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002621static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002622weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002623{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002624 struct weston_subsurface *sub;
2625
2626 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2627 parent_link_pending) {
2628 wl_list_remove(&sub->parent_link);
2629 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2630 }
2631}
2632
2633static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002634weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002635 struct weston_matrix *matrix)
2636{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002637 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002638 double src_width, src_height, dest_width, dest_height;
2639
2640 weston_matrix_init(matrix);
2641
2642 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2643 src_width = surface->width_from_buffer;
2644 src_height = surface->height_from_buffer;
2645 } else {
2646 src_width = wl_fixed_to_double(vp->buffer.src_width);
2647 src_height = wl_fixed_to_double(vp->buffer.src_height);
2648 }
2649
2650 if (vp->surface.width == -1) {
2651 dest_width = src_width;
2652 dest_height = src_height;
2653 } else {
2654 dest_width = vp->surface.width;
2655 dest_height = vp->surface.height;
2656 }
2657
2658 if (src_width != dest_width || src_height != dest_height)
2659 weston_matrix_scale(matrix,
2660 src_width / dest_width,
2661 src_height / dest_height, 1);
2662
2663 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2664 weston_matrix_translate(matrix,
2665 wl_fixed_to_double(vp->buffer.src_x),
2666 wl_fixed_to_double(vp->buffer.src_y),
2667 0);
2668
2669 switch (vp->buffer.transform) {
2670 case WL_OUTPUT_TRANSFORM_FLIPPED:
2671 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2672 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2673 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2674 weston_matrix_scale(matrix, -1, 1, 1);
2675 weston_matrix_translate(matrix,
2676 surface->width_from_buffer, 0, 0);
2677 break;
2678 }
2679
2680 switch (vp->buffer.transform) {
2681 default:
2682 case WL_OUTPUT_TRANSFORM_NORMAL:
2683 case WL_OUTPUT_TRANSFORM_FLIPPED:
2684 break;
2685 case WL_OUTPUT_TRANSFORM_90:
2686 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2687 weston_matrix_rotate_xy(matrix, 0, 1);
2688 weston_matrix_translate(matrix,
2689 surface->height_from_buffer, 0, 0);
2690 break;
2691 case WL_OUTPUT_TRANSFORM_180:
2692 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2693 weston_matrix_rotate_xy(matrix, -1, 0);
2694 weston_matrix_translate(matrix,
2695 surface->width_from_buffer,
2696 surface->height_from_buffer, 0);
2697 break;
2698 case WL_OUTPUT_TRANSFORM_270:
2699 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2700 weston_matrix_rotate_xy(matrix, 0, -1);
2701 weston_matrix_translate(matrix,
2702 0, surface->width_from_buffer, 0);
2703 break;
2704 }
2705
2706 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2707}
2708
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002709/**
2710 * Compute a + b > c while being safe to overflows.
2711 */
2712static bool
2713fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2714{
2715 return (int64_t)a + (int64_t)b > (int64_t)c;
2716}
2717
2718static bool
2719weston_surface_is_pending_viewport_source_valid(
2720 const struct weston_surface *surface)
2721{
2722 const struct weston_surface_state *pend = &surface->pending;
2723 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2724 int width_from_buffer = 0;
2725 int height_from_buffer = 0;
2726 wl_fixed_t w;
2727 wl_fixed_t h;
2728
2729 /* If viewport source rect is not set, it is always ok. */
2730 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2731 return true;
2732
2733 if (pend->newly_attached) {
2734 if (pend->buffer) {
2735 convert_size_by_transform_scale(&width_from_buffer,
2736 &height_from_buffer,
2737 pend->buffer->width,
2738 pend->buffer->height,
2739 vp->buffer.transform,
2740 vp->buffer.scale);
2741 } else {
2742 /* No buffer: viewport is irrelevant. */
2743 return true;
2744 }
2745 } else {
2746 width_from_buffer = surface->width_from_buffer;
2747 height_from_buffer = surface->height_from_buffer;
2748 }
2749
2750 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2751 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2752
2753 /* No buffer: viewport is irrelevant. */
2754 if (width_from_buffer == 0 || height_from_buffer == 0)
2755 return true;
2756
2757 /* overflow checks for wl_fixed_from_int() */
2758 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2759 return false;
2760 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2761 return false;
2762
2763 w = wl_fixed_from_int(width_from_buffer);
2764 h = wl_fixed_from_int(height_from_buffer);
2765
2766 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2767 return false;
2768 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2769 return false;
2770
2771 return true;
2772}
2773
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002774static bool
2775fixed_is_integer(wl_fixed_t v)
2776{
2777 return (v & 0xff) == 0;
2778}
2779
2780static bool
2781weston_surface_is_pending_viewport_dst_size_int(
2782 const struct weston_surface *surface)
2783{
2784 const struct weston_buffer_viewport *vp =
2785 &surface->pending.buffer_viewport;
2786
2787 if (vp->surface.width != -1) {
2788 assert(vp->surface.width > 0 && vp->surface.height > 0);
2789 return true;
2790 }
2791
2792 return fixed_is_integer(vp->buffer.src_width) &&
2793 fixed_is_integer(vp->buffer.src_height);
2794}
2795
Derek Foreman152254b2015-11-26 14:17:48 -06002796/* Translate pending damage in buffer co-ordinates to surface
2797 * co-ordinates and union it with a pixman_region32_t.
2798 * This should only be called after the buffer is attached.
2799 */
2800static void
2801apply_damage_buffer(pixman_region32_t *dest,
2802 struct weston_surface *surface,
2803 struct weston_surface_state *state)
2804{
2805 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2806
2807 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2808 * translated into surface co-ordinates and unioned with
2809 * any other surface damage.
2810 * None of this makes sense if there is no buffer though.
2811 */
2812 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2813 pixman_region32_t buffer_damage;
2814
2815 pixman_region32_intersect_rect(&state->damage_buffer,
2816 &state->damage_buffer,
2817 0, 0, buffer->width,
2818 buffer->height);
2819 pixman_region32_init(&buffer_damage);
2820 weston_matrix_transform_region(&buffer_damage,
2821 &surface->buffer_to_surface_matrix,
2822 &state->damage_buffer);
2823 pixman_region32_union(dest, dest, &buffer_damage);
2824 pixman_region32_fini(&buffer_damage);
2825 }
2826 /* We should clear this on commit even if there was no buffer */
2827 pixman_region32_clear(&state->damage_buffer);
2828}
2829
Jason Ekstrand1e059042014-10-16 10:55:19 -05002830static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002831weston_surface_commit_state(struct weston_surface *surface,
2832 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002833{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002834 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002835 pixman_region32_t opaque;
2836
Alexander Larsson4ea95522013-05-22 14:41:37 +02002837 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002838 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002839 /* wp_viewport.set_source */
2840 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002841 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002842
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002843 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002844 if (state->newly_attached)
2845 weston_surface_attach(surface, state->buffer);
2846 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002847
Jason Ekstrand1e059042014-10-16 10:55:19 -05002848 weston_surface_build_buffer_matrix(surface,
2849 &surface->surface_to_buffer_matrix);
2850 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2851 &surface->surface_to_buffer_matrix);
2852
Jason Ekstrand7b982072014-05-20 14:33:03 -05002853 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002854 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002855 if (surface->committed)
2856 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002857 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002858
Jason Ekstrand7b982072014-05-20 14:33:03 -05002859 state->sx = 0;
2860 state->sy = 0;
2861 state->newly_attached = 0;
2862 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002863
Derek Foreman152254b2015-11-26 14:17:48 -06002864 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002865 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002866 (pixman_region32_not_empty(&state->damage_surface) ||
2867 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002868 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002869
Pekka Paalanen8e159182012-10-10 12:49:25 +03002870 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002871 &state->damage_surface);
2872
2873 apply_damage_buffer(&surface->damage, surface, state);
2874
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002875 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002876 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002877 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002878
2879 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002880 pixman_region32_init(&opaque);
2881 pixman_region32_intersect_rect(&opaque, &state->opaque,
2882 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002883
2884 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2885 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002886 wl_list_for_each(view, &surface->views, surface_link)
2887 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002888 }
2889
2890 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002891
2892 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002893 pixman_region32_intersect_rect(&surface->input, &state->input,
2894 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002895
Pekka Paalanenbc106382012-10-10 12:49:31 +03002896 /* wl_surface.frame */
2897 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002898 &state->frame_callback_list);
2899 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002900
2901 /* XXX:
2902 * What should happen with a feedback request, if there
2903 * is no wl_buffer attached for this commit?
2904 */
2905
2906 /* presentation.feedback */
2907 wl_list_insert_list(&surface->feedback_list,
2908 &state->feedback_list);
2909 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08002910
2911 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002912}
2913
2914static void
2915weston_surface_commit(struct weston_surface *surface)
2916{
2917 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002918
Pekka Paalanene67858b2013-04-25 13:57:42 +03002919 weston_surface_commit_subsurface_order(surface);
2920
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002921 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002922}
2923
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002924static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002925weston_subsurface_commit(struct weston_subsurface *sub);
2926
2927static void
2928weston_subsurface_parent_commit(struct weston_subsurface *sub,
2929 int parent_is_synchronized);
2930
2931static void
2932surface_commit(struct wl_client *client, struct wl_resource *resource)
2933{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002934 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002935 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2936
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002937 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
2938 assert(surface->viewport_resource);
2939
2940 wl_resource_post_error(surface->viewport_resource,
2941 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
2942 "wl_surface@%d has viewport source outside buffer",
2943 wl_resource_get_id(resource));
2944 return;
2945 }
2946
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002947 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
2948 assert(surface->viewport_resource);
2949
2950 wl_resource_post_error(surface->viewport_resource,
2951 WP_VIEWPORT_ERROR_BAD_SIZE,
2952 "wl_surface@%d viewport dst size not integer",
2953 wl_resource_get_id(resource));
2954 return;
2955 }
2956
Pekka Paalanene67858b2013-04-25 13:57:42 +03002957 if (sub) {
2958 weston_subsurface_commit(sub);
2959 return;
2960 }
2961
2962 weston_surface_commit(surface);
2963
2964 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2965 if (sub->surface != surface)
2966 weston_subsurface_parent_commit(sub, 0);
2967 }
2968}
2969
2970static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002971surface_set_buffer_transform(struct wl_client *client,
2972 struct wl_resource *resource, int transform)
2973{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002974 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002975
Jonny Lamba55f1392014-05-30 12:07:15 +02002976 /* if wl_output.transform grows more members this will need to be updated. */
2977 if (transform < 0 ||
2978 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2979 wl_resource_post_error(resource,
2980 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2981 "buffer transform must be a valid transform "
2982 "('%d' specified)", transform);
2983 return;
2984 }
2985
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002986 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002987 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002988}
2989
Alexander Larsson4ea95522013-05-22 14:41:37 +02002990static void
2991surface_set_buffer_scale(struct wl_client *client,
2992 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002993 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002994{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002995 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002996
Jonny Lamba55f1392014-05-30 12:07:15 +02002997 if (scale < 1) {
2998 wl_resource_post_error(resource,
2999 WL_SURFACE_ERROR_INVALID_SCALE,
3000 "buffer scale must be at least one "
3001 "('%d' specified)", scale);
3002 return;
3003 }
3004
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003005 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003006 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003007}
3008
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003009static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003010 surface_destroy,
3011 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003012 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003013 surface_frame,
3014 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003015 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003016 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003017 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003018 surface_set_buffer_scale,
3019 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003020};
3021
3022static void
3023compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003024 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003025{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003026 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003027 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003028
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003029 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003030 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003031 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003032 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003033 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003034
Jason Ekstranda85118c2013-06-27 20:17:02 -05003035 surface->resource =
3036 wl_resource_create(client, &wl_surface_interface,
3037 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003038 if (surface->resource == NULL) {
3039 weston_surface_destroy(surface);
3040 wl_resource_post_no_memory(resource);
3041 return;
3042 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003043 wl_resource_set_implementation(surface->resource, &surface_interface,
3044 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003045
3046 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003047}
3048
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003049static void
3050destroy_region(struct wl_resource *resource)
3051{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003052 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003053
3054 pixman_region32_fini(&region->region);
3055 free(region);
3056}
3057
3058static void
3059region_destroy(struct wl_client *client, struct wl_resource *resource)
3060{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003061 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003062}
3063
3064static void
3065region_add(struct wl_client *client, struct wl_resource *resource,
3066 int32_t x, int32_t y, int32_t width, int32_t height)
3067{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003068 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003069
3070 pixman_region32_union_rect(&region->region, &region->region,
3071 x, y, width, height);
3072}
3073
3074static void
3075region_subtract(struct wl_client *client, struct wl_resource *resource,
3076 int32_t x, int32_t y, int32_t width, int32_t height)
3077{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003078 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003079 pixman_region32_t rect;
3080
3081 pixman_region32_init_rect(&rect, x, y, width, height);
3082 pixman_region32_subtract(&region->region, &region->region, &rect);
3083 pixman_region32_fini(&rect);
3084}
3085
3086static const struct wl_region_interface region_interface = {
3087 region_destroy,
3088 region_add,
3089 region_subtract
3090};
3091
3092static void
3093compositor_create_region(struct wl_client *client,
3094 struct wl_resource *resource, uint32_t id)
3095{
3096 struct weston_region *region;
3097
3098 region = malloc(sizeof *region);
3099 if (region == NULL) {
3100 wl_resource_post_no_memory(resource);
3101 return;
3102 }
3103
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003104 pixman_region32_init(&region->region);
3105
Jason Ekstranda85118c2013-06-27 20:17:02 -05003106 region->resource =
3107 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003108 if (region->resource == NULL) {
3109 free(region);
3110 wl_resource_post_no_memory(resource);
3111 return;
3112 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003113 wl_resource_set_implementation(region->resource, &region_interface,
3114 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003115}
3116
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003117static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003118 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003119 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003120};
3121
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003122static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003123weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3124{
3125 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003126
Jason Ekstrand7b982072014-05-20 14:33:03 -05003127 weston_surface_commit_state(surface, &sub->cached);
3128 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003129
3130 weston_surface_commit_subsurface_order(surface);
3131
3132 weston_surface_schedule_repaint(surface);
3133
Jason Ekstrand7b982072014-05-20 14:33:03 -05003134 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003135}
3136
3137static void
3138weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3139{
3140 struct weston_surface *surface = sub->surface;
3141
3142 /*
3143 * If this commit would cause the surface to move by the
3144 * attach(dx, dy) parameters, the old damage region must be
3145 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003146 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003147 */
Derek Foreman152254b2015-11-26 14:17:48 -06003148 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003149 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003150 pixman_region32_union(&sub->cached.damage_surface,
3151 &sub->cached.damage_surface,
3152 &surface->pending.damage_surface);
3153 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003154
3155 if (surface->pending.newly_attached) {
3156 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003157 weston_surface_state_set_buffer(&sub->cached,
3158 surface->pending.buffer);
3159 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003160 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003161 weston_presentation_feedback_discard_list(
3162 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003163 }
3164 sub->cached.sx += surface->pending.sx;
3165 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003166
Derek Foreman152254b2015-11-26 14:17:48 -06003167 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3168
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003169 sub->cached.buffer_viewport.changed |=
3170 surface->pending.buffer_viewport.changed;
3171 sub->cached.buffer_viewport.buffer =
3172 surface->pending.buffer_viewport.buffer;
3173 sub->cached.buffer_viewport.surface =
3174 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003175
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003176 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003177
3178 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3179
3180 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3181
3182 wl_list_insert_list(&sub->cached.frame_callback_list,
3183 &surface->pending.frame_callback_list);
3184 wl_list_init(&surface->pending.frame_callback_list);
3185
Pekka Paalanen133e4392014-09-23 22:08:46 -04003186 wl_list_insert_list(&sub->cached.feedback_list,
3187 &surface->pending.feedback_list);
3188 wl_list_init(&surface->pending.feedback_list);
3189
Jason Ekstrand7b982072014-05-20 14:33:03 -05003190 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003191}
3192
Derek Foreman280e7dd2014-10-03 13:13:42 -05003193static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003194weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3195{
3196 while (sub) {
3197 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003198 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003199
3200 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003201 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003202
3203 sub = weston_surface_to_subsurface(sub->parent);
3204 }
3205
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003206 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003207}
3208
3209static void
3210weston_subsurface_commit(struct weston_subsurface *sub)
3211{
3212 struct weston_surface *surface = sub->surface;
3213 struct weston_subsurface *tmp;
3214
3215 /* Recursive check for effectively synchronized. */
3216 if (weston_subsurface_is_synchronized(sub)) {
3217 weston_subsurface_commit_to_cache(sub);
3218 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003219 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003220 /* flush accumulated state from cache */
3221 weston_subsurface_commit_to_cache(sub);
3222 weston_subsurface_commit_from_cache(sub);
3223 } else {
3224 weston_surface_commit(surface);
3225 }
3226
3227 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3228 if (tmp->surface != surface)
3229 weston_subsurface_parent_commit(tmp, 0);
3230 }
3231 }
3232}
3233
3234static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003235weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003236{
3237 struct weston_surface *surface = sub->surface;
3238 struct weston_subsurface *tmp;
3239
Pekka Paalanene67858b2013-04-25 13:57:42 +03003240 /* From now on, commit_from_cache the whole sub-tree, regardless of
3241 * the synchronized mode of each child. This sub-surface or some
3242 * of its ancestors were synchronized, so we are synchronized
3243 * all the way down.
3244 */
3245
Jason Ekstrand7b982072014-05-20 14:33:03 -05003246 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003247 weston_subsurface_commit_from_cache(sub);
3248
3249 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3250 if (tmp->surface != surface)
3251 weston_subsurface_parent_commit(tmp, 1);
3252 }
3253}
3254
3255static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003256weston_subsurface_parent_commit(struct weston_subsurface *sub,
3257 int parent_is_synchronized)
3258{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003259 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003260 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003261 wl_list_for_each(view, &sub->surface->views, surface_link)
3262 weston_view_set_position(view,
3263 sub->position.x,
3264 sub->position.y);
3265
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003266 sub->position.set = 0;
3267 }
3268
3269 if (parent_is_synchronized || sub->synchronized)
3270 weston_subsurface_synchronized_commit(sub);
3271}
3272
Pekka Paalanen8274d902014-08-06 19:36:51 +03003273static int
3274subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3275{
3276 return snprintf(buf, len, "sub-surface");
3277}
3278
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003279static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003280subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003281{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003282 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003283
Jason Ekstranda7af7042013-10-12 22:38:11 -05003284 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003285 weston_view_set_position(view,
3286 view->geometry.x + dx,
3287 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003288
3289 /* No need to check parent mappedness, because if parent is not
3290 * mapped, parent is not in a visible layer, so this sub-surface
3291 * will not be drawn either.
3292 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003293
Pekka Paalanene67858b2013-04-25 13:57:42 +03003294 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003295 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003296
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003297 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003298 * because that would call it also for the parent surface,
3299 * which might not be mapped yet. That would lead to
3300 * inconsistent state, where the window could never be
3301 * mapped.
3302 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003303 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003304 * weston_surface_is_mapped() return true, so that when the
3305 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003306 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003307 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003308 }
3309}
3310
3311static struct weston_subsurface *
3312weston_surface_to_subsurface(struct weston_surface *surface)
3313{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003314 if (surface->committed == subsurface_committed)
3315 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003316
3317 return NULL;
3318}
3319
Pekka Paalanen01388e22013-04-25 13:57:44 +03003320WL_EXPORT struct weston_surface *
3321weston_surface_get_main_surface(struct weston_surface *surface)
3322{
3323 struct weston_subsurface *sub;
3324
3325 while (surface && (sub = weston_surface_to_subsurface(surface)))
3326 surface = sub->parent;
3327
3328 return surface;
3329}
3330
Pekka Paalanen50b67472014-10-01 15:02:41 +03003331WL_EXPORT int
3332weston_surface_set_role(struct weston_surface *surface,
3333 const char *role_name,
3334 struct wl_resource *error_resource,
3335 uint32_t error_code)
3336{
3337 assert(role_name);
3338
3339 if (surface->role_name == NULL ||
3340 surface->role_name == role_name ||
3341 strcmp(surface->role_name, role_name) == 0) {
3342 surface->role_name = role_name;
3343
3344 return 0;
3345 }
3346
3347 wl_resource_post_error(error_resource, error_code,
3348 "Cannot assign role %s to wl_surface@%d,"
3349 " already has role %s\n",
3350 role_name,
3351 wl_resource_get_id(surface->resource),
3352 surface->role_name);
3353 return -1;
3354}
3355
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003356WL_EXPORT const char *
3357weston_surface_get_role(struct weston_surface *surface)
3358{
3359 return surface->role_name;
3360}
3361
Pekka Paalanen8274d902014-08-06 19:36:51 +03003362WL_EXPORT void
3363weston_surface_set_label_func(struct weston_surface *surface,
3364 int (*desc)(struct weston_surface *,
3365 char *, size_t))
3366{
3367 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003368 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003369}
3370
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003371/** Get the size of surface contents
3372 *
3373 * \param surface The surface to query.
3374 * \param width Returns the width of raw contents.
3375 * \param height Returns the height of raw contents.
3376 *
3377 * Retrieves the raw surface content size in pixels for the given surface.
3378 * This is the whole content size in buffer pixels. If the surface
3379 * has no content or the renderer does not implement this feature,
3380 * zeroes are returned.
3381 *
3382 * This function is used to determine the buffer size needed for
3383 * a weston_surface_copy_content() call.
3384 */
3385WL_EXPORT void
3386weston_surface_get_content_size(struct weston_surface *surface,
3387 int *width, int *height)
3388{
3389 struct weston_renderer *rer = surface->compositor->renderer;
3390
3391 if (!rer->surface_get_content_size) {
3392 *width = 0;
3393 *height = 0;
3394 return;
3395 }
3396
3397 rer->surface_get_content_size(surface, width, height);
3398}
3399
Quentin Glidic248dd102016-08-12 10:41:34 +02003400/** Get the bounding box of a surface and its subsurfaces
3401 *
3402 * \param surface The surface to query.
3403 * \return The bounding box relative to the surface origin.
3404 *
3405 */
3406WL_EXPORT struct weston_geometry
3407weston_surface_get_bounding_box(struct weston_surface *surface)
3408{
3409 pixman_region32_t region;
3410 pixman_box32_t *box;
3411 struct weston_subsurface *subsurface;
3412
3413 pixman_region32_init_rect(&region,
3414 0, 0,
3415 surface->width, surface->height);
3416
3417 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3418 pixman_region32_union_rect(&region, &region,
3419 subsurface->position.x,
3420 subsurface->position.y,
3421 subsurface->surface->width,
3422 subsurface->surface->height);
3423
3424 box = pixman_region32_extents(&region);
3425 struct weston_geometry geometry = {
3426 .x = box->x1,
3427 .y = box->y1,
3428 .width = box->x2 - box->x1,
3429 .height = box->y2 - box->y1,
3430 };
3431
3432 pixman_region32_fini(&region);
3433
3434 return geometry;
3435}
3436
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003437/** Copy surface contents to system memory.
3438 *
3439 * \param surface The surface to copy from.
3440 * \param target Pointer to the target memory buffer.
3441 * \param size Size of the target buffer in bytes.
3442 * \param src_x X location on contents to copy from.
3443 * \param src_y Y location on contents to copy from.
3444 * \param width Width in pixels of the area to copy.
3445 * \param height Height in pixels of the area to copy.
3446 * \return 0 for success, -1 for failure.
3447 *
3448 * Surface contents are maintained by the renderer. They can be in a
3449 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3450 * else.
3451 *
3452 * Surface contents are copied into memory pointed to by target,
3453 * which has size bytes of space available. The target memory
3454 * may be larger than needed, but being smaller returns an error.
3455 * The extra bytes in target may or may not be written; their content is
3456 * unspecified. Size must be large enough to hold the image.
3457 *
3458 * The image in the target memory will be arranged in rows from
3459 * top to bottom, and pixels on a row from left to right. The pixel
3460 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3461 * width * 4.
3462 *
3463 * Parameters src_x and src_y define the upper-left corner in buffer
3464 * coordinates (pixels) to copy from. Parameters width and height
3465 * define the size of the area to copy in pixels.
3466 *
3467 * The rectangle defined by src_x, src_y, width, height must fit in
3468 * the surface contents. Otherwise an error is returned.
3469 *
3470 * Use surface_get_data_size to determine the content size; the
3471 * needed target buffer size and rectangle limits.
3472 *
3473 * CURRENT IMPLEMENTATION RESTRICTIONS:
3474 * - the machine must be little-endian due to Pixman formats.
3475 *
3476 * NOTE: Pixman formats are premultiplied.
3477 */
3478WL_EXPORT int
3479weston_surface_copy_content(struct weston_surface *surface,
3480 void *target, size_t size,
3481 int src_x, int src_y,
3482 int width, int height)
3483{
3484 struct weston_renderer *rer = surface->compositor->renderer;
3485 int cw, ch;
3486 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3487
3488 if (!rer->surface_copy_content)
3489 return -1;
3490
3491 weston_surface_get_content_size(surface, &cw, &ch);
3492
3493 if (src_x < 0 || src_y < 0)
3494 return -1;
3495
3496 if (width <= 0 || height <= 0)
3497 return -1;
3498
3499 if (src_x + width > cw || src_y + height > ch)
3500 return -1;
3501
3502 if (width * bytespp * height > size)
3503 return -1;
3504
3505 return rer->surface_copy_content(surface, target, size,
3506 src_x, src_y, width, height);
3507}
3508
Pekka Paalanene67858b2013-04-25 13:57:42 +03003509static void
3510subsurface_set_position(struct wl_client *client,
3511 struct wl_resource *resource, int32_t x, int32_t y)
3512{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003513 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003514
3515 if (!sub)
3516 return;
3517
3518 sub->position.x = x;
3519 sub->position.y = y;
3520 sub->position.set = 1;
3521}
3522
3523static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003524subsurface_find_sibling(struct weston_subsurface *sub,
3525 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003526{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003527 struct weston_surface *parent = sub->parent;
3528 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003529
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003530 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3531 if (sibling->surface == surface && sibling != sub)
3532 return sibling;
3533 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003534
3535 return NULL;
3536}
3537
3538static struct weston_subsurface *
3539subsurface_sibling_check(struct weston_subsurface *sub,
3540 struct weston_surface *surface,
3541 const char *request)
3542{
3543 struct weston_subsurface *sibling;
3544
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003545 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003546 if (!sibling) {
3547 wl_resource_post_error(sub->resource,
3548 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3549 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003550 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551 return NULL;
3552 }
3553
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003554 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003555
3556 return sibling;
3557}
3558
3559static void
3560subsurface_place_above(struct wl_client *client,
3561 struct wl_resource *resource,
3562 struct wl_resource *sibling_resource)
3563{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003564 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003565 struct weston_surface *surface =
3566 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003567 struct weston_subsurface *sibling;
3568
3569 if (!sub)
3570 return;
3571
3572 sibling = subsurface_sibling_check(sub, surface, "place_above");
3573 if (!sibling)
3574 return;
3575
3576 wl_list_remove(&sub->parent_link_pending);
3577 wl_list_insert(sibling->parent_link_pending.prev,
3578 &sub->parent_link_pending);
3579}
3580
3581static void
3582subsurface_place_below(struct wl_client *client,
3583 struct wl_resource *resource,
3584 struct wl_resource *sibling_resource)
3585{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003586 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003587 struct weston_surface *surface =
3588 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003589 struct weston_subsurface *sibling;
3590
3591 if (!sub)
3592 return;
3593
3594 sibling = subsurface_sibling_check(sub, surface, "place_below");
3595 if (!sibling)
3596 return;
3597
3598 wl_list_remove(&sub->parent_link_pending);
3599 wl_list_insert(&sibling->parent_link_pending,
3600 &sub->parent_link_pending);
3601}
3602
3603static void
3604subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3605{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003606 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003607
3608 if (sub)
3609 sub->synchronized = 1;
3610}
3611
3612static void
3613subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3614{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003615 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003616
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003617 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003618 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003619
3620 /* If sub became effectively desynchronized, flush. */
3621 if (!weston_subsurface_is_synchronized(sub))
3622 weston_subsurface_synchronized_commit(sub);
3623 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003624}
3625
3626static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3628{
3629 wl_list_remove(&sub->parent_link);
3630 wl_list_remove(&sub->parent_link_pending);
3631 wl_list_remove(&sub->parent_destroy_listener.link);
3632 sub->parent = NULL;
3633}
3634
3635static void
3636weston_subsurface_destroy(struct weston_subsurface *sub);
3637
3638static void
3639subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3640{
3641 struct weston_subsurface *sub =
3642 container_of(listener, struct weston_subsurface,
3643 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003644 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003645
3646 /* The protocol object (wl_resource) is left inert. */
3647 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003648 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003649
3650 weston_subsurface_destroy(sub);
3651}
3652
3653static void
3654subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3655{
3656 struct weston_subsurface *sub =
3657 container_of(listener, struct weston_subsurface,
3658 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003659 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003660 assert(sub->surface != sub->parent);
3661
3662 if (weston_surface_is_mapped(sub->surface))
3663 weston_surface_unmap(sub->surface);
3664
3665 weston_subsurface_unlink_parent(sub);
3666}
3667
3668static void
3669subsurface_resource_destroy(struct wl_resource *resource)
3670{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003671 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003672
3673 if (sub)
3674 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003675}
3676
3677static void
3678subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3679{
3680 wl_resource_destroy(resource);
3681}
3682
3683static void
3684weston_subsurface_link_parent(struct weston_subsurface *sub,
3685 struct weston_surface *parent)
3686{
3687 sub->parent = parent;
3688 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003689 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003690 &sub->parent_destroy_listener);
3691
3692 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3693 wl_list_insert(&parent->subsurface_list_pending,
3694 &sub->parent_link_pending);
3695}
3696
3697static void
3698weston_subsurface_link_surface(struct weston_subsurface *sub,
3699 struct weston_surface *surface)
3700{
3701 sub->surface = surface;
3702 sub->surface_destroy_listener.notify =
3703 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003704 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003705 &sub->surface_destroy_listener);
3706}
3707
3708static void
3709weston_subsurface_destroy(struct weston_subsurface *sub)
3710{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003711 struct weston_view *view, *next;
3712
Pekka Paalanene67858b2013-04-25 13:57:42 +03003713 assert(sub->surface);
3714
3715 if (sub->resource) {
3716 assert(weston_surface_to_subsurface(sub->surface) == sub);
3717 assert(sub->parent_destroy_listener.notify ==
3718 subsurface_handle_parent_destroy);
3719
George Kiagiadakised04d382014-06-13 18:10:26 +02003720 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3721 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003722 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003723 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003724
Pekka Paalanene67858b2013-04-25 13:57:42 +03003725 if (sub->parent)
3726 weston_subsurface_unlink_parent(sub);
3727
Jason Ekstrand7b982072014-05-20 14:33:03 -05003728 weston_surface_state_fini(&sub->cached);
3729 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003730
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003731 sub->surface->committed = NULL;
3732 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003733 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003734 } else {
3735 /* the dummy weston_subsurface for the parent itself */
3736 assert(sub->parent_destroy_listener.notify == NULL);
3737 wl_list_remove(&sub->parent_link);
3738 wl_list_remove(&sub->parent_link_pending);
3739 }
3740
3741 wl_list_remove(&sub->surface_destroy_listener.link);
3742 free(sub);
3743}
3744
3745static const struct wl_subsurface_interface subsurface_implementation = {
3746 subsurface_destroy,
3747 subsurface_set_position,
3748 subsurface_place_above,
3749 subsurface_place_below,
3750 subsurface_set_sync,
3751 subsurface_set_desync
3752};
3753
3754static struct weston_subsurface *
3755weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3756 struct weston_surface *parent)
3757{
3758 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003759 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003760
Bryce Harringtonde16d892014-11-20 22:21:57 -08003761 sub = zalloc(sizeof *sub);
3762 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003763 return NULL;
3764
Jason Ekstranda7af7042013-10-12 22:38:11 -05003765 wl_list_init(&sub->unused_views);
3766
Jason Ekstranda85118c2013-06-27 20:17:02 -05003767 sub->resource =
3768 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003769 if (!sub->resource) {
3770 free(sub);
3771 return NULL;
3772 }
3773
Jason Ekstranda85118c2013-06-27 20:17:02 -05003774 wl_resource_set_implementation(sub->resource,
3775 &subsurface_implementation,
3776 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003777 weston_subsurface_link_surface(sub, surface);
3778 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003779 weston_surface_state_init(&sub->cached);
3780 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003781 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003782
3783 return sub;
3784}
3785
3786/* Create a dummy subsurface for having the parent itself in its
3787 * sub-surface lists. Makes stacking order manipulation easy.
3788 */
3789static struct weston_subsurface *
3790weston_subsurface_create_for_parent(struct weston_surface *parent)
3791{
3792 struct weston_subsurface *sub;
3793
Bryce Harringtonde16d892014-11-20 22:21:57 -08003794 sub = zalloc(sizeof *sub);
3795 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003796 return NULL;
3797
3798 weston_subsurface_link_surface(sub, parent);
3799 sub->parent = parent;
3800 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3801 wl_list_insert(&parent->subsurface_list_pending,
3802 &sub->parent_link_pending);
3803
3804 return sub;
3805}
3806
3807static void
3808subcompositor_get_subsurface(struct wl_client *client,
3809 struct wl_resource *resource,
3810 uint32_t id,
3811 struct wl_resource *surface_resource,
3812 struct wl_resource *parent_resource)
3813{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003814 struct weston_surface *surface =
3815 wl_resource_get_user_data(surface_resource);
3816 struct weston_surface *parent =
3817 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003818 struct weston_subsurface *sub;
3819 static const char where[] = "get_subsurface: wl_subsurface@";
3820
3821 if (surface == parent) {
3822 wl_resource_post_error(resource,
3823 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3824 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003825 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003826 return;
3827 }
3828
3829 if (weston_surface_to_subsurface(surface)) {
3830 wl_resource_post_error(resource,
3831 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3832 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003833 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003834 return;
3835 }
3836
Pekka Paalanen50b67472014-10-01 15:02:41 +03003837 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3838 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003839 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003840
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003841 if (weston_surface_get_main_surface(parent) == surface) {
3842 wl_resource_post_error(resource,
3843 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3844 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003845 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003846 return;
3847 }
3848
Pekka Paalanene67858b2013-04-25 13:57:42 +03003849 /* make sure the parent is in its own list */
3850 if (wl_list_empty(&parent->subsurface_list)) {
3851 if (!weston_subsurface_create_for_parent(parent)) {
3852 wl_resource_post_no_memory(resource);
3853 return;
3854 }
3855 }
3856
3857 sub = weston_subsurface_create(id, surface, parent);
3858 if (!sub) {
3859 wl_resource_post_no_memory(resource);
3860 return;
3861 }
3862
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003863 surface->committed = subsurface_committed;
3864 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003865 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003866}
3867
3868static void
3869subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3870{
3871 wl_resource_destroy(resource);
3872}
3873
3874static const struct wl_subcompositor_interface subcompositor_interface = {
3875 subcompositor_destroy,
3876 subcompositor_get_subsurface
3877};
3878
3879static void
3880bind_subcompositor(struct wl_client *client,
3881 void *data, uint32_t version, uint32_t id)
3882{
3883 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003884 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003885
Jason Ekstranda85118c2013-06-27 20:17:02 -05003886 resource =
3887 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003888 if (resource == NULL) {
3889 wl_client_post_no_memory(client);
3890 return;
3891 }
3892 wl_resource_set_implementation(resource, &subcompositor_interface,
3893 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003894}
3895
Bryce Harrington0795ece2016-08-30 12:04:26 -07003896/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003897 *
3898 * \param compositor The compositor instance
3899 * \param state The DPMS state the outputs will be set to
3900 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003901static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003902weston_compositor_dpms(struct weston_compositor *compositor,
3903 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003904{
3905 struct weston_output *output;
3906
Bryce Harrington08976ac2016-08-30 12:05:16 -07003907 wl_list_for_each(output, &compositor->output_list, link)
3908 if (output->set_dpms)
3909 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003910}
3911
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003912/** Restores the compositor to active status
3913 *
3914 * \param compositor The compositor instance
3915 *
3916 * If the compositor was in a sleeping mode, all outputs are powered
3917 * back on via DPMS. Otherwise if the compositor was inactive
3918 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3919 * signal will fire.
3920 *
3921 * Restarts the idle timer.
3922 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003923WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003924weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003925{
Neil Roberts8b62e202013-09-30 13:14:47 +01003926 uint32_t old_state = compositor->state;
3927
3928 /* The state needs to be changed before emitting the wake
3929 * signal because that may try to schedule a repaint which
3930 * will not work if the compositor is still sleeping */
3931 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3932
3933 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003934 case WESTON_COMPOSITOR_SLEEPING:
3935 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3936 /* fall through */
3937 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003938 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003939 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003940 /* fall through */
3941 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003942 wl_event_source_timer_update(compositor->idle_source,
3943 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003944 }
3945}
3946
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003947/** Turns off rendering and frame events for the compositor.
3948 *
3949 * \param compositor The compositor instance
3950 *
3951 * This is used for example to prevent further rendering while the
3952 * compositor is shutting down.
3953 *
3954 * \note When offscreen state is entered, outputs will be powered
3955 * back on if they were sleeping (in DPMS off mode), even though
3956 * no rendering will be performed.
3957 *
3958 * Stops the idle timer.
3959 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003960WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003961weston_compositor_offscreen(struct weston_compositor *compositor)
3962{
3963 switch (compositor->state) {
3964 case WESTON_COMPOSITOR_OFFSCREEN:
3965 return;
3966 case WESTON_COMPOSITOR_SLEEPING:
3967 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3968 /* fall through */
3969 default:
3970 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3971 wl_event_source_timer_update(compositor->idle_source, 0);
3972 }
3973}
3974
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003975/** Powers down all attached output devices
3976 *
3977 * \param compositor The compositor instance
3978 *
3979 * Causes rendering to the outputs to cease, and no frame events to be
3980 * sent. Only powers down the outputs if the compositor is not already
3981 * in sleep mode.
3982 *
3983 * Stops the idle timer.
3984 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003985WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003986weston_compositor_sleep(struct weston_compositor *compositor)
3987{
3988 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3989 return;
3990
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003991 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003992 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3993 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3994}
3995
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003996/** Sets compositor to idle mode
3997 *
3998 * \param data The compositor instance
3999 *
4000 * This is called when the idle timer fires. Once the compositor is in
4001 * idle mode it requires a wake action (e.g. via
4002 * weston_compositor_wake()) to restore it. The compositor's
4003 * idle_signal will be triggered when the idle event occurs.
4004 *
4005 * Idleness can be inhibited by setting the compositor's idle_inhibit
4006 * property.
4007 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004008static int
4009idle_handler(void *data)
4010{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004011 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004012
4013 if (compositor->idle_inhibit)
4014 return 1;
4015
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004016 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004017 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004018
4019 return 1;
4020}
4021
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004022WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004023weston_plane_init(struct weston_plane *plane,
4024 struct weston_compositor *ec,
4025 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004026{
4027 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004028 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004029 plane->x = x;
4030 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004031 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004032
4033 /* Init the link so that the call to wl_list_remove() when releasing
4034 * the plane without ever stacking doesn't lead to a crash */
4035 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004036}
4037
4038WL_EXPORT void
4039weston_plane_release(struct weston_plane *plane)
4040{
Xiong Zhang97116532013-10-23 13:58:31 +08004041 struct weston_view *view;
4042
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004043 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004044 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004045
Xiong Zhang97116532013-10-23 13:58:31 +08004046 wl_list_for_each(view, &plane->compositor->view_list, link) {
4047 if (view->plane == plane)
4048 view->plane = NULL;
4049 }
4050
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004051 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004052}
4053
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004054WL_EXPORT void
4055weston_compositor_stack_plane(struct weston_compositor *ec,
4056 struct weston_plane *plane,
4057 struct weston_plane *above)
4058{
4059 if (above)
4060 wl_list_insert(above->link.prev, &plane->link);
4061 else
4062 wl_list_insert(&ec->plane_list, &plane->link);
4063}
4064
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004065static void
4066output_release(struct wl_client *client, struct wl_resource *resource)
4067{
4068 wl_resource_destroy(resource);
4069}
4070
4071static const struct wl_output_interface output_interface = {
4072 output_release,
4073};
4074
4075
Casey Dahlin9074db52012-04-19 22:50:09 -04004076static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004077{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004078 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004079}
4080
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004081static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004082bind_output(struct wl_client *client,
4083 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004084{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004085 struct weston_output *output = data;
4086 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004087 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004088
Jason Ekstranda85118c2013-06-27 20:17:02 -05004089 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004090 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004091 if (resource == NULL) {
4092 wl_client_post_no_memory(client);
4093 return;
4094 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004095
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004096 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004097 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004098
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004099 wl_output_send_geometry(resource,
4100 output->x,
4101 output->y,
4102 output->mm_width,
4103 output->mm_height,
4104 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004105 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004106 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004107 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004108 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004109 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004110
4111 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004112 wl_output_send_mode(resource,
4113 mode->flags,
4114 mode->width,
4115 mode->height,
4116 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004117 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004118
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004119 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004120 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004121}
4122
David Fort0de859e2016-05-27 23:22:57 +02004123/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004124static void
David Fort0de859e2016-05-27 23:22:57 +02004125weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4126 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004127{
4128 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004129 bool start_resizing = false;
4130
4131 if (!delta_width)
4132 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004133
4134 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004135 if (output == resized_output) {
4136 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004137 continue;
4138 }
4139
David Fort0de859e2016-05-27 23:22:57 +02004140 if (start_resizing) {
4141 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004142 output->dirty = 1;
4143 }
4144 }
4145}
4146
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004147WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004148weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004149{
Scott Moreau850ca422012-05-21 15:21:25 -06004150 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004151
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004152 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004153 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004154
Scott Moreauccbf29d2012-02-22 14:21:41 -07004155 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004156 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004157 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004158 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004159 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004160 weston_matrix_scale(&output->matrix, magnification,
4161 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004162 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004163
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004164 switch (output->transform) {
4165 case WL_OUTPUT_TRANSFORM_FLIPPED:
4166 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4167 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4168 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4169 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4170 weston_matrix_scale(&output->matrix, -1, 1, 1);
4171 break;
4172 }
4173
4174 switch (output->transform) {
4175 default:
4176 case WL_OUTPUT_TRANSFORM_NORMAL:
4177 case WL_OUTPUT_TRANSFORM_FLIPPED:
4178 break;
4179 case WL_OUTPUT_TRANSFORM_90:
4180 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4181 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4182 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4183 break;
4184 case WL_OUTPUT_TRANSFORM_180:
4185 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4186 weston_matrix_translate(&output->matrix,
4187 -output->width, -output->height, 0);
4188 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4189 break;
4190 case WL_OUTPUT_TRANSFORM_270:
4191 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4192 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4193 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4194 break;
4195 }
4196
4197 if (output->current_scale != 1)
4198 weston_matrix_scale(&output->matrix,
4199 output->current_scale,
4200 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004201
Scott Moreauccbf29d2012-02-22 14:21:41 -07004202 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004203
4204 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004205}
4206
Scott Moreau1bad5db2012-08-18 01:04:05 -06004207static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004208weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004209{
4210 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004211 output->native_scale = scale;
4212 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004213
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004214 convert_size_by_transform_scale(&output->width, &output->height,
4215 output->current_mode->width,
4216 output->current_mode->height,
4217 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004218}
4219
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004220static void
4221weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004222{
4223 output->x = x;
4224 output->y = y;
4225
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004226 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004227 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004228 output->width,
4229 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004230}
4231
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004232WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004233weston_output_move(struct weston_output *output, int x, int y)
4234{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004235 struct wl_resource *resource;
4236
4237 output->move_x = x - output->x;
4238 output->move_y = y - output->y;
4239
4240 if (output->move_x == 0 && output->move_y == 0)
4241 return;
4242
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004243 weston_output_init_geometry(output, x, y);
4244
4245 output->dirty = 1;
4246
4247 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004248 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004249
4250 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004251 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004252 wl_output_send_geometry(resource,
4253 output->x,
4254 output->y,
4255 output->mm_width,
4256 output->mm_height,
4257 output->subpixel,
4258 output->make,
4259 output->model,
4260 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004261
FORT David8a120692016-04-26 23:34:06 +02004262 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004263 wl_output_send_done(resource);
4264 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004265}
4266
Giulio Camuffob1147152015-05-06 21:41:57 +03004267/** Adds an output to the compositor's output list and
4268 * send the compositor's output_created signal.
4269 *
4270 * \param compositor The compositor instance.
4271 * \param output The output to be added.
4272 */
4273WL_EXPORT void
4274weston_compositor_add_output(struct weston_compositor *compositor,
4275 struct weston_output *output)
4276{
Armin Krezoviće5403842016-08-05 15:28:29 +02004277 struct weston_view *view, *next;
4278
Giulio Camuffob1147152015-05-06 21:41:57 +03004279 wl_list_insert(compositor->output_list.prev, &output->link);
4280 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02004281
4282 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4283 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004284}
4285
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004286WL_EXPORT void
4287weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004288 double device_x, double device_y,
4289 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004290{
Derek Foreman0f679412014-10-02 13:41:17 -05004291 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004292 device_x,
4293 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004294 0.0,
4295 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004296
Derek Foreman67a18b92015-03-24 11:36:14 -05004297 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004298
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004299 *x = p.f[0] / p.f[3];
4300 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004301}
4302
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004303/** Undoes changes to an output done by weston_output_enable()
4304 *
4305 * \param output The weston_output object that needs the changes undone.
4306 *
4307 * Removes the repaint timer.
4308 * Destroys the Wayland global assigned to the output.
4309 * Destroys pixman regions allocated to the output.
4310 * Deallocates output's ID and updates compositor's output_id_pool.
4311 */
4312static void
4313weston_output_enable_undo(struct weston_output *output)
4314{
4315 wl_event_source_remove(output->repaint_timer);
4316
4317 wl_global_destroy(output->global);
4318
4319 pixman_region32_fini(&output->region);
4320 pixman_region32_fini(&output->previous_damage);
4321 output->compositor->output_id_pool &= ~(1u << output->id);
4322
4323 output->enabled = false;
4324}
4325
4326/** Removes output from compositor's output list
4327 *
4328 * \param output The weston_output object that is being removed.
4329 *
4330 * Presentation feedback is discarded.
4331 * Compositor is notified that outputs were changed and
4332 * applies the necessary changes.
4333 * All views assigned to the weston_output object are
4334 * moved to a new output.
4335 * Signal is emited to notify all users of the weston_output
4336 * object that the output is being destroyed.
4337 * wl_output protocol objects referencing this weston_output
4338 * are made inert.
4339 */
4340static void
4341weston_compositor_remove_output(struct weston_output *output)
4342{
4343 struct wl_resource *resource;
4344 struct weston_view *view;
4345
4346 assert(output->destroying);
4347
4348 wl_list_for_each(view, &output->compositor->view_list, link) {
4349 if (view->output_mask & (1u << output->id))
4350 weston_view_assign_output(view);
4351 }
4352
4353 weston_presentation_feedback_discard_list(&output->feedback_list);
4354
4355 weston_compositor_reflow_outputs(output->compositor, output, output->width);
4356 wl_list_remove(&output->link);
4357
4358 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
4359 wl_signal_emit(&output->destroy_signal, output);
4360
4361 wl_resource_for_each(resource, &output->resource_list) {
4362 wl_resource_set_destructor(resource, NULL);
4363 }
4364}
4365
4366/** Sets the output scale for a given output.
4367 *
4368 * \param output The weston_output object that the scale is set for.
4369 * \param scale Scale factor for the given output.
4370 *
4371 * It only supports setting scale for an output that
4372 * is not enabled and it can only be ran once.
4373 */
4374WL_EXPORT void
4375weston_output_set_scale(struct weston_output *output,
4376 int32_t scale)
4377{
4378 /* We can only set scale on a disabled output */
4379 assert(!output->enabled);
4380
4381 /* We only want to set scale once */
4382 assert(!output->scale);
4383
4384 output->scale = scale;
4385}
4386
4387/** Sets the output transform for a given output.
4388 *
4389 * \param output The weston_output object that the transform is set for.
4390 * \param transform Transform value for the given output.
4391 *
4392 * It only supports setting transform for an output that is
4393 * not enabled and it can only be ran once.
4394 *
4395 * Refer to wl_output::transform section located at
4396 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
4397 * for list of values that can be passed to this function.
4398 */
4399WL_EXPORT void
4400weston_output_set_transform(struct weston_output *output,
4401 uint32_t transform)
4402{
4403 /* We can only set transform on a disabled output */
4404 assert(!output->enabled);
4405
4406 /* We only want to set transform once */
4407 assert(output->transform == UINT32_MAX);
4408
4409 output->transform = transform;
4410}
4411
4412/** Initializes a weston_output object with enough data so
4413 ** an output can be configured.
4414 *
4415 * \param output The weston_output object to initialize
4416 * \param compositor The compositor instance.
4417 *
4418 * Sets initial values for fields that are expected to be
4419 * configured either by compositors or backends.
4420 */
4421WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02004422weston_output_init(struct weston_output *output,
4423 struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004424{
4425 output->compositor = compositor;
4426 output->destroying = 0;
4427
4428 /* Backends must set output->name */
4429 assert(output->name);
4430
4431 wl_list_init(&output->link);
4432
4433 output->enabled = false;
4434
4435 /* Add some (in)sane defaults which can be used
4436 * for checking if an output was properly configured
4437 */
4438 output->mm_width = 0;
4439 output->mm_height = 0;
4440 output->scale = 0;
4441 /* Can't use -1 on uint32_t and 0 is valid enum value */
4442 output->transform = UINT32_MAX;
4443}
4444
4445/** Adds weston_output object to pending output list.
4446 *
4447 * \param output The weston_output object to add
4448 * \param compositor The compositor instance.
4449 *
4450 * Also notifies the compositor that an output is pending for
4451 * configuration.
4452 */
4453WL_EXPORT void
4454weston_compositor_add_pending_output(struct weston_output *output,
4455 struct weston_compositor *compositor)
4456{
4457 wl_list_insert(compositor->pending_output_list.prev, &output->link);
4458 wl_signal_emit(&compositor->output_pending_signal, output);
4459}
4460
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004461/** Constructs a weston_output object that can be used by the compositor.
4462 *
4463 * \param output The weston_output object that needs to be enabled.
4464 *
4465 * Output coordinates are calculated and each new output is by default
4466 * assigned to the right of previous one.
4467 *
4468 * Sets up the transformation, zoom, and geometry of the output using
4469 * the properties that need to be configured by the compositor.
4470 *
4471 * Establishes a repaint timer for the output with the relevant display
4472 * object's event loop. See output_repaint_timer_handler().
4473 *
4474 * The output is assigned an ID. Weston can support up to 32 distinct
4475 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4476 * is referred to and used to find the first available ID number, and
4477 * then this ID is marked as used in output_id_pool.
4478 *
4479 * The output is also assigned a Wayland global with the wl_output
4480 * external interface.
4481 *
4482 * Backend specific function is called to set up the output output.
4483 *
4484 * Output is added to the compositor's output list
4485 *
4486 * If the backend specific function fails, the weston_output object
4487 * is returned to a state it was before calling this function and
4488 * is added to the compositor's pending_output_list in case it needs
4489 * to be reconfigured or just so it can be destroyed at shutdown.
4490 *
4491 * 0 is returned on success, -1 on failure.
4492 */
4493WL_EXPORT int
4494weston_output_enable(struct weston_output *output)
4495{
Armin Krezović782f5df2016-09-30 14:11:11 +02004496 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004497 struct weston_output *iterator;
Armin Krezović782f5df2016-09-30 14:11:11 +02004498 struct wl_event_loop *loop;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004499 int x = 0, y = 0;
4500
4501 assert(output->enable);
4502
Armin Krezović782f5df2016-09-30 14:11:11 +02004503 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004504 struct weston_output, link);
4505
Armin Krezović782f5df2016-09-30 14:11:11 +02004506 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004507 x = iterator->x + iterator->width;
4508
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004509 /* Make sure the scale is set up */
4510 assert(output->scale);
4511
4512 /* Make sure we have a transform set */
4513 assert(output->transform != UINT32_MAX);
4514
4515 /* Remove it from pending/disabled output list */
4516 wl_list_remove(&output->link);
4517
Armin Krezović782f5df2016-09-30 14:11:11 +02004518 /* Verify we haven't reached the limit of 32 available output IDs */
4519 assert(ffs(~c->output_id_pool) > 0);
4520
4521 output->x = x;
4522 output->y = y;
4523 output->dirty = 1;
4524 output->original_scale = output->scale;
4525
4526 weston_output_transform_scale_init(output, output->transform, output->scale);
4527 weston_output_init_zoom(output);
4528
4529 weston_output_init_geometry(output, x, y);
4530 weston_output_damage(output);
4531
4532 wl_signal_init(&output->frame_signal);
4533 wl_signal_init(&output->destroy_signal);
4534 wl_list_init(&output->animation_list);
4535 wl_list_init(&output->resource_list);
4536 wl_list_init(&output->feedback_list);
4537 wl_list_init(&output->link);
4538
4539 loop = wl_display_get_event_loop(c->wl_display);
4540 output->repaint_timer = wl_event_loop_add_timer(loop,
4541 output_repaint_timer_handler, output);
4542
4543 /* Invert the output id pool and look for the lowest numbered
4544 * switch (the least significant bit). Take that bit's position
4545 * as our ID, and mark it used in the compositor's output_id_pool.
4546 */
4547 output->id = ffs(~output->compositor->output_id_pool) - 1;
4548 output->compositor->output_id_pool |= 1u << output->id;
4549
4550 output->global =
4551 wl_global_create(c->wl_display, &wl_output_interface, 3,
4552 output, bind_output);
4553
4554 output->enabled = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004555
4556 /* Enable the output (set up the crtc or create a
4557 * window representing the output, set up the
4558 * renderer, etc)
4559 */
4560 if (output->enable(output) < 0) {
4561 weston_log("Enabling output \"%s\" failed.\n", output->name);
4562
4563 weston_output_enable_undo(output);
4564 wl_list_insert(output->compositor->pending_output_list.prev,
4565 &output->link);
4566 return -1;
4567 }
4568
4569 weston_compositor_add_output(output->compositor, output);
4570
4571 return 0;
4572}
4573
4574/** Converts a weston_output object to a pending output state, so it
4575 ** can be configured again or destroyed.
4576 *
4577 * \param output The weston_output object that needs to be disabled.
4578 *
Armin Krezović40087402016-09-30 14:11:12 +02004579 * See weston_output_init() for more information on the
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004580 * state output is returned to.
4581 *
4582 * Calls a backend specific function to disable an output, in case
4583 * such function exists.
4584 *
4585 * If the output is being used by the compositor, it is first removed
4586 * from weston's output_list (see weston_compositor_remove_output())
4587 * and is returned to a state it was before weston_output_enable()
4588 * was ran (see weston_output_enable_undo()).
4589 *
4590 * Output is added to pending_output_list so it will get destroyed
4591 * if the output does not get configured again when the compositor
4592 * shuts down. If an output is to be used immediately, it needs to
4593 * be manually removed from the list (the compositor specific functions
4594 * for handling pending outputs will take care of that).
4595 *
4596 * If backend specific disable function returns negative value,
4597 * this function will return too. It can be used as an indicator
4598 * that output cannot be disabled at the present time. In that case
4599 * backend needs to make sure the output is disabled when it is
4600 * possible.
4601 */
4602WL_EXPORT void
4603weston_output_disable(struct weston_output *output)
4604{
4605 assert(output->disable);
4606
4607 /* Should we rename this? */
4608 output->destroying = 1;
4609
4610 if (output->disable(output) < 0)
4611 return;
4612
4613 if (output->enabled) {
4614 weston_compositor_remove_output(output);
4615 weston_output_enable_undo(output);
4616
4617 /* We need to preserve it somewhere so it can be destroyed on shutdown
4618 if nobody wants to configure it again */
4619 wl_list_insert(output->compositor->pending_output_list.prev, &output->link);
4620 }
4621
4622 output->destroying = 0;
4623}
4624
4625/** Emits a signal to indicate that there are outputs waiting to be configured.
4626 *
4627 * \param compositor The compositor instance
4628 */
4629WL_EXPORT void
4630weston_pending_output_coldplug(struct weston_compositor *compositor)
4631{
4632 struct weston_output *output, *next;
4633
4634 wl_list_for_each_safe(output, next, &compositor->pending_output_list, link)
4635 wl_signal_emit(&compositor->output_pending_signal, output);
4636}
4637
4638WL_EXPORT void
4639weston_output_destroy(struct weston_output *output)
4640{
4641 output->destroying = 1;
4642
4643 if (output->enabled) {
4644 weston_compositor_remove_output(output);
4645 weston_output_enable_undo(output);
4646 }
4647
4648 free(output->name);
4649}
4650
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004651static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004652destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004653{
Jonny Lamb74130762013-11-26 18:19:46 +01004654 struct weston_surface *surface =
4655 wl_resource_get_user_data(resource);
4656
Pekka Paalanen4826f872016-04-22 14:14:38 +03004657 if (!surface)
4658 return;
4659
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004660 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004661 surface->pending.buffer_viewport.buffer.src_width =
4662 wl_fixed_from_int(-1);
4663 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004664 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004665}
4666
4667static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004668viewport_destroy(struct wl_client *client,
4669 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004670{
4671 wl_resource_destroy(resource);
4672}
4673
4674static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004675viewport_set_source(struct wl_client *client,
4676 struct wl_resource *resource,
4677 wl_fixed_t src_x,
4678 wl_fixed_t src_y,
4679 wl_fixed_t src_width,
4680 wl_fixed_t src_height)
4681{
4682 struct weston_surface *surface =
4683 wl_resource_get_user_data(resource);
4684
Pekka Paalanen4826f872016-04-22 14:14:38 +03004685 if (!surface) {
4686 wl_resource_post_error(resource,
4687 WP_VIEWPORT_ERROR_NO_SURFACE,
4688 "wl_surface for this viewport is no longer exists");
4689 return;
4690 }
4691
4692 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004693 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004694
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004695 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004696 src_height == wl_fixed_from_int(-1) &&
4697 src_x == wl_fixed_from_int(-1) &&
4698 src_y == wl_fixed_from_int(-1)) {
4699 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004700 surface->pending.buffer_viewport.buffer.src_width =
4701 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004702 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004703 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004704 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004705
Pekka Paalanen201769a2016-04-26 14:42:11 +03004706 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004707 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004708 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03004709 "wl_surface@%d viewport source "
4710 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4711 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004712 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03004713 wl_fixed_to_double(src_height),
4714 wl_fixed_to_double(src_x),
4715 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004716 return;
4717 }
4718
4719 surface->pending.buffer_viewport.buffer.src_x = src_x;
4720 surface->pending.buffer_viewport.buffer.src_y = src_y;
4721 surface->pending.buffer_viewport.buffer.src_width = src_width;
4722 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004723 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004724}
4725
4726static void
4727viewport_set_destination(struct wl_client *client,
4728 struct wl_resource *resource,
4729 int32_t dst_width,
4730 int32_t dst_height)
4731{
4732 struct weston_surface *surface =
4733 wl_resource_get_user_data(resource);
4734
Pekka Paalanen4826f872016-04-22 14:14:38 +03004735 if (!surface) {
4736 wl_resource_post_error(resource,
4737 WP_VIEWPORT_ERROR_NO_SURFACE,
4738 "wl_surface for this viewport no longer exists");
4739 return;
4740 }
4741
4742 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004743
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004744 if (dst_width == -1 && dst_height == -1) {
4745 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004746 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004747 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004748 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004749 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004750
4751 if (dst_width <= 0 || dst_height <= 0) {
4752 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004753 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004754 "destination size must be positive (%dx%d)",
4755 dst_width, dst_height);
4756 return;
4757 }
4758
4759 surface->pending.buffer_viewport.surface.width = dst_width;
4760 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004761 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004762}
4763
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004764static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004765 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004766 viewport_set_source,
4767 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004768};
4769
4770static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004771viewporter_destroy(struct wl_client *client,
4772 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004773{
4774 wl_resource_destroy(resource);
4775}
4776
4777static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004778viewporter_get_viewport(struct wl_client *client,
4779 struct wl_resource *viewporter,
4780 uint32_t id,
4781 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004782{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004783 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004784 struct weston_surface *surface =
4785 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004786 struct wl_resource *resource;
4787
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004788 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004789 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004790 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004791 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004792 return;
4793 }
4794
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004795 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004796 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004797 if (resource == NULL) {
4798 wl_client_post_no_memory(client);
4799 return;
4800 }
4801
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004802 wl_resource_set_implementation(resource, &viewport_interface,
4803 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004804
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004805 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004806}
4807
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004808static const struct wp_viewporter_interface viewporter_interface = {
4809 viewporter_destroy,
4810 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004811};
4812
4813static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004814bind_viewporter(struct wl_client *client,
4815 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004816{
4817 struct wl_resource *resource;
4818
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004819 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004820 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004821 if (resource == NULL) {
4822 wl_client_post_no_memory(client);
4823 return;
4824 }
4825
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004826 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004827 NULL, NULL);
4828}
4829
4830static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004831destroy_presentation_feedback(struct wl_resource *feedback_resource)
4832{
4833 struct weston_presentation_feedback *feedback;
4834
4835 feedback = wl_resource_get_user_data(feedback_resource);
4836
4837 wl_list_remove(&feedback->link);
4838 free(feedback);
4839}
4840
4841static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004842presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4843{
4844 wl_resource_destroy(resource);
4845}
4846
4847static void
4848presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004849 struct wl_resource *presentation_resource,
4850 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004851 uint32_t callback)
4852{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004853 struct weston_surface *surface;
4854 struct weston_presentation_feedback *feedback;
4855
4856 surface = wl_resource_get_user_data(surface_resource);
4857
Bryce Harringtonde16d892014-11-20 22:21:57 -08004858 feedback = zalloc(sizeof *feedback);
4859 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004860 goto err_calloc;
4861
4862 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004863 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004864 1, callback);
4865 if (!feedback->resource)
4866 goto err_create;
4867
4868 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4869 destroy_presentation_feedback);
4870
4871 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4872
4873 return;
4874
4875err_create:
4876 free(feedback);
4877
4878err_calloc:
4879 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004880}
4881
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004882static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004883 presentation_destroy,
4884 presentation_feedback
4885};
4886
4887static void
4888bind_presentation(struct wl_client *client,
4889 void *data, uint32_t version, uint32_t id)
4890{
4891 struct weston_compositor *compositor = data;
4892 struct wl_resource *resource;
4893
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004894 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004895 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004896 if (resource == NULL) {
4897 wl_client_post_no_memory(client);
4898 return;
4899 }
4900
4901 wl_resource_set_implementation(resource, &presentation_implementation,
4902 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004903 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004904}
4905
4906static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004907compositor_bind(struct wl_client *client,
4908 void *data, uint32_t version, uint32_t id)
4909{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004910 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004911 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004912
Jason Ekstranda85118c2013-06-27 20:17:02 -05004913 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004914 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004915 if (resource == NULL) {
4916 wl_client_post_no_memory(client);
4917 return;
4918 }
4919
4920 wl_resource_set_implementation(resource, &compositor_interface,
4921 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004922}
4923
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004924WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004925weston_environment_get_fd(const char *env)
4926{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07004927 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004928 int fd, flags;
4929
4930 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07004931 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004932 return -1;
4933
4934 flags = fcntl(fd, F_GETFD);
4935 if (flags == -1)
4936 return -1;
4937
4938 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4939 unsetenv(env);
4940
4941 return fd;
4942}
4943
Pekka Paalanenb5026542014-11-12 15:09:24 +02004944static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004945timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004946 uint32_t key, void *data)
4947{
4948 struct weston_compositor *compositor = data;
4949
4950 if (weston_timeline_enabled_)
4951 weston_timeline_close();
4952 else
4953 weston_timeline_open(compositor);
4954}
4955
Giulio Camuffo459137b2014-10-11 23:56:24 +03004956/** Create the compositor.
4957 *
4958 * This functions creates and initializes a compositor instance.
4959 *
4960 * \param display The Wayland display to be used.
4961 * \param user_data A pointer to an object that can later be retrieved
4962 * using the \ref weston_compositor_get_user_data function.
4963 * \return The compositor instance on success or NULL on failure.
4964 */
4965WL_EXPORT struct weston_compositor *
4966weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004967{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004968 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004969 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004970
Giulio Camuffo459137b2014-10-11 23:56:24 +03004971 ec = zalloc(sizeof *ec);
4972 if (!ec)
4973 return NULL;
4974
4975 ec->wl_display = display;
4976 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004977 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004978 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004979 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004980 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004981 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004982 wl_signal_init(&ec->idle_signal);
4983 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004984 wl_signal_init(&ec->show_input_panel_signal);
4985 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004986 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004987 wl_signal_init(&ec->seat_created_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004988 wl_signal_init(&ec->output_pending_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004989 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004990 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004991 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004992 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004993 wl_signal_init(&ec->session_signal);
4994 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004995
Casey Dahlin58ba1372012-04-19 22:50:08 -04004996 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004997 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004998
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02004999 ec->activate_serial = 1;
5000
Derek Foreman152254b2015-11-26 14:17:48 -06005001 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005002 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005003 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05005004
Giulio Camuffo954f1832014-10-11 18:27:30 +03005005 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005006 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005007 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03005008
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005009 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005010 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005011 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005012
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005013 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005014 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005015 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005016
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005017 if (weston_input_init(ec) != 0)
5018 goto fail;
5019
Jason Ekstranda7af7042013-10-12 22:38:11 -05005020 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005021 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005022 wl_list_init(&ec->layer_list);
5023 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005024 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005025 wl_list_init(&ec->output_list);
5026 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01005027 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005028 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005029 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005030 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005031 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005032
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005033 wl_list_init(&ec->plugin_api_list);
5034
Xiong Zhang97116532013-10-23 13:58:31 +08005035 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005036 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005037
Giulio Camuffo459137b2014-10-11 23:56:24 +03005038 wl_data_device_manager_init(ec->wl_display);
5039
5040 wl_display_init_shm(ec->wl_display);
5041
5042 loop = wl_display_get_event_loop(ec->wl_display);
5043 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005044
Giulio Camuffo459137b2014-10-11 23:56:24 +03005045 weston_layer_init(&ec->fade_layer, &ec->layer_list);
5046 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
5047
5048 weston_compositor_add_debug_binding(ec, KEY_T,
5049 timeline_key_binding_handler, ec);
5050
Giulio Camuffo459137b2014-10-11 23:56:24 +03005051 return ec;
5052
5053fail:
5054 free(ec);
5055 return NULL;
5056}
5057
Benjamin Franzkeb8263022011-08-30 11:32:47 +02005058WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005059weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07005060{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005061 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07005062
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005063 wl_event_source_remove(ec->idle_source);
5064
Matt Roper361d2ad2011-08-29 13:52:23 -07005065 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02005066 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07005067 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02005068
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005069 /* Destroy all pending outputs associated with this compositor */
5070 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
5071 output->destroy(output);
5072
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02005073 if (ec->renderer)
5074 ec->renderer->destroy(ec);
5075
Daniel Stone325fc2d2012-05-30 16:31:58 +01005076 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09005077 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005078 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005079 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005080 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005081 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005082
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005083 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07005084}
5085
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005086WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04005087weston_compositor_exit_with_code(struct weston_compositor *compositor,
5088 int exit_code)
5089{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02005090 if (compositor->exit_code == EXIT_SUCCESS)
5091 compositor->exit_code = exit_code;
5092
Giulio Camuffo459137b2014-10-11 23:56:24 +03005093 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04005094}
5095
5096WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005097weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
5098 const struct weston_pointer_grab_interface *interface)
5099{
5100 struct weston_seat *seat;
5101
5102 ec->default_pointer_grab = interface;
5103 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005104 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5105
5106 if (pointer)
5107 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005108 }
5109}
5110
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04005111WL_EXPORT int
5112weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
5113 clockid_t clk_id)
5114{
5115 struct timespec ts;
5116
5117 if (clock_gettime(clk_id, &ts) < 0)
5118 return -1;
5119
5120 compositor->presentation_clock = clk_id;
5121
5122 return 0;
5123}
5124
5125/*
5126 * For choosing the software clock, when the display hardware or API
5127 * does not expose a compatible presentation timestamp.
5128 */
5129WL_EXPORT int
5130weston_compositor_set_presentation_clock_software(
5131 struct weston_compositor *compositor)
5132{
5133 /* In order of preference */
5134 static const clockid_t clocks[] = {
5135 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
5136 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
5137 CLOCK_MONOTONIC, /* no jumps, may crawl */
5138 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
5139 CLOCK_REALTIME /* may jump and crawl */
5140 };
5141 unsigned i;
5142
5143 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
5144 if (weston_compositor_set_presentation_clock(compositor,
5145 clocks[i]) == 0)
5146 return 0;
5147
5148 weston_log("Error: no suitable presentation clock available.\n");
5149
5150 return -1;
5151}
5152
Pekka Paalanen662f3842015-03-18 12:17:26 +02005153/** Read the current time from the Presentation clock
5154 *
5155 * \param compositor
5156 * \param ts[out] The current time.
5157 *
5158 * \note Reading the current time in user space is always imprecise to some
5159 * degree.
5160 *
5161 * This function is never meant to fail. If reading the clock does fail,
5162 * an error message is logged and a zero time is returned. Callers are not
5163 * supposed to detect or react to failures.
5164 */
5165WL_EXPORT void
5166weston_compositor_read_presentation_clock(
5167 const struct weston_compositor *compositor,
5168 struct timespec *ts)
5169{
5170 static bool warned;
5171 int ret;
5172
5173 ret = clock_gettime(compositor->presentation_clock, ts);
5174 if (ret < 0) {
5175 ts->tv_sec = 0;
5176 ts->tv_nsec = 0;
5177
5178 if (!warned)
5179 weston_log("Error: failure to read "
5180 "the presentation clock %#x: '%m' (%d)\n",
5181 compositor->presentation_clock, errno);
5182 warned = true;
5183 }
5184}
5185
Pekka Paalanen230f3b12014-09-29 14:18:40 -04005186/** Import dmabuf buffer into current renderer
5187 *
5188 * \param compositor
5189 * \param buffer the dmabuf buffer to import
5190 * \return true on usable buffers, false otherwise
5191 *
5192 * This function tests that the linux_dmabuf_buffer is usable
5193 * for the current renderer. Returns false on unusable buffers. Usually
5194 * usability is tested by importing the dmabufs for composition.
5195 *
5196 * This hook is also used for detecting if the renderer supports
5197 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
5198 * supported.
5199 * */
5200WL_EXPORT bool
5201weston_compositor_import_dmabuf(struct weston_compositor *compositor,
5202 struct linux_dmabuf_buffer *buffer)
5203{
5204 struct weston_renderer *renderer;
5205
5206 renderer = compositor->renderer;
5207
5208 if (renderer->import_dmabuf == NULL)
5209 return false;
5210
5211 return renderer->import_dmabuf(compositor, buffer);
5212}
5213
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005214WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005215weston_version(int *major, int *minor, int *micro)
5216{
5217 *major = WESTON_VERSION_MAJOR;
5218 *minor = WESTON_VERSION_MINOR;
5219 *micro = WESTON_VERSION_MICRO;
5220}
5221
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005222WL_EXPORT void *
5223weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005224{
Derek Foreman3f86e502015-06-08 11:46:54 -05005225 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005226 char path[PATH_MAX];
5227 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00005228 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005229
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005230 if (name == NULL)
5231 return NULL;
5232
Derek Foreman3f86e502015-06-08 11:46:54 -05005233 if (name[0] != '/') {
5234 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00005235 len = snprintf(path, sizeof path, "%s/.libs/%s",
5236 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005237 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00005238 len = snprintf(path, sizeof path, "%s/%s",
5239 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005240 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00005241 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005242 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005243
Daniel Stonebeb97e52016-11-28 12:13:54 +00005244 /* snprintf returns the length of the string it would've written,
5245 * _excluding_ the NUL byte. So even being equal to the size of
5246 * our buffer is an error here. */
5247 if (len >= sizeof path)
5248 return NULL;
5249
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005250 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
5251 if (module) {
5252 weston_log("Module '%s' already loaded\n", path);
5253 dlclose(module);
5254 return NULL;
5255 }
5256
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005257 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04005258 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005259 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005260 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005261 return NULL;
5262 }
5263
5264 init = dlsym(module, entrypoint);
5265 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005266 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00005267 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005268 return NULL;
5269 }
5270
5271 return init;
5272}
5273
Giulio Camuffo459137b2014-10-11 23:56:24 +03005274
5275/** Destroys the compositor.
5276 *
5277 * This function cleans up the compositor state and destroys it.
5278 *
5279 * \param compositor The compositor to be destroyed.
5280 */
5281WL_EXPORT void
5282weston_compositor_destroy(struct weston_compositor *compositor)
5283{
5284 /* prevent further rendering while shutting down */
5285 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5286
5287 wl_signal_emit(&compositor->destroy_signal, compositor);
5288
5289 weston_compositor_xkb_destroy(compositor);
5290
Giulio Camuffo2d24e642015-10-03 16:25:15 +03005291 if (compositor->backend)
5292 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005293
5294 weston_plugin_api_destroy_list(compositor);
5295
Giulio Camuffo459137b2014-10-11 23:56:24 +03005296 free(compositor);
5297}
5298
5299/** Instruct the compositor to exit.
5300 *
5301 * This functions does not directly destroy the compositor object, it merely
5302 * command it to start the tear down process. It is not guaranteed that the
5303 * tear down will happen immediately.
5304 *
5305 * \param compositor The compositor to tear down.
5306 */
5307WL_EXPORT void
5308weston_compositor_exit(struct weston_compositor *compositor)
5309{
5310 compositor->exit(compositor);
5311}
5312
5313/** Return the user data stored in the compositor.
5314 *
5315 * This function returns the user data pointer set with user_data parameter
5316 * to the \ref weston_compositor_create function.
5317 */
5318WL_EXPORT void *
5319weston_compositor_get_user_data(struct weston_compositor *compositor)
5320{
5321 return compositor->user_data;
5322}
Pekka Paalanendd186732016-06-03 14:49:54 +03005323
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005324static const char * const backend_map[] = {
5325 [WESTON_BACKEND_DRM] = "drm-backend.so",
5326 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5327 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5328 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5329 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5330 [WESTON_BACKEND_X11] = "x11-backend.so",
5331};
5332
Pekka Paalanendd186732016-06-03 14:49:54 +03005333/** Load a backend into a weston_compositor
5334 *
5335 * A backend must be loaded to make a weston_compositor work. A backend
5336 * provides input and output capabilities, and determines the renderer to use.
5337 *
5338 * \param compositor A compositor that has not had a backend loaded yet.
5339 * \param backend Name of the backend file.
5340 * \param config_base A pointer to a backend-specific configuration
5341 * structure's 'base' member.
5342 *
5343 * \return 0 on success, or -1 on error.
5344 */
5345WL_EXPORT int
5346weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005347 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005348 struct weston_backend_config *config_base)
5349{
5350 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005351 struct weston_backend_config *config_base);
5352
Quentin Glidic887c0182016-07-10 11:00:53 +02005353 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005354 return -1;
5355
5356 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005357 if (!backend_init)
5358 return -1;
5359
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005360 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005361}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005362
5363WL_EXPORT int
5364weston_compositor_load_xwayland(struct weston_compositor *compositor)
5365{
5366 int (*module_init)(struct weston_compositor *ec,
5367 int *argc, char *argv[]);
5368 int argc = 0;
5369
5370 module_init = weston_load_module("xwayland.so", "module_init");
5371 if (!module_init)
5372 return -1;
5373 if (module_init(compositor, &argc, NULL) < 0)
5374 return -1;
5375 return 0;
5376}