blob: 113f5fcd76249668ec7f2450d67d2b44c5360242 [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"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030061#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040062#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050063#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020064#include "plugin-registry.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050065
Pekka Paalanen0513a952014-05-21 16:17:27 +030066#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
67
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020068static void
Alexander Larsson0b135062013-05-28 16:23:36 +020069weston_output_transform_scale_init(struct weston_output *output,
70 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020071
Rob Bradford27b17932013-06-26 18:08:46 +010072static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050073weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010074
Derek Foreman6ae7bc92014-11-04 10:47:33 -060075static void weston_mode_switch_finish(struct weston_output *output,
76 int mode_changed,
77 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080078{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020079 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020080 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020081 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060082 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020083
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020084 pixman_region32_init(&old_output_region);
85 pixman_region32_copy(&old_output_region, &output->region);
86
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020087 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020088 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020089
90 pixman_region32_init(&output->previous_damage);
91 pixman_region32_init_rect(&output->region, output->x, output->y,
92 output->width, output->height);
93
94 weston_output_update_matrix(output);
95
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020096 /* If a pointer falls outside the outputs new geometry, move it to its
97 * lower-right corner */
98 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -050099 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200100 int32_t x, y;
101
102 if (!pointer)
103 continue;
104
105 x = wl_fixed_to_int(pointer->x);
106 y = wl_fixed_to_int(pointer->y);
107
108 if (!pixman_region32_contains_point(&old_output_region,
109 x, y, NULL) ||
110 pixman_region32_contains_point(&output->region,
111 x, y, NULL))
112 continue;
113
114 if (x >= output->x + output->width)
115 x = output->x + output->width - 1;
116 if (y >= output->y + output->height)
117 y = output->y + output->height - 1;
118
119 pointer->x = wl_fixed_from_int(x);
120 pointer->y = wl_fixed_from_int(y);
121 }
122
123 pixman_region32_fini(&old_output_region);
124
Derek Foremandd4cd332014-11-10 10:29:59 -0600125 if (!mode_changed && !scale_changed)
126 return;
127
Hardening57388e42013-09-18 23:56:36 +0200128 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600129 wl_resource_for_each(resource, &output->resource_list) {
130 if (mode_changed) {
131 wl_output_send_mode(resource,
132 output->current_mode->flags,
133 output->current_mode->width,
134 output->current_mode->height,
135 output->current_mode->refresh);
136 }
Hardening57388e42013-09-18 23:56:36 +0200137
Derek Foreman41bdc272014-11-05 13:26:57 -0600138 version = wl_resource_get_version(resource);
139 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600140 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200141
Derek Foreman41bdc272014-11-05 13:26:57 -0600142 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
143 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600144 }
145}
146
David Fort0de859e2016-05-27 23:22:57 +0200147
148static void
149weston_compositor_reflow_outputs(struct weston_compositor *compositor,
150 struct weston_output *resized_output, int delta_width);
151
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600152WL_EXPORT int
153weston_output_mode_set_native(struct weston_output *output,
154 struct weston_mode *mode,
155 int32_t scale)
156{
157 int ret;
158 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200159 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600160
161 if (!output->switch_mode)
162 return -1;
163
164 if (!output->original_mode) {
165 mode_changed = 1;
166 ret = output->switch_mode(output, mode);
167 if (ret < 0)
168 return ret;
169 if (output->current_scale != scale) {
170 scale_changed = 1;
171 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200172 }
173 }
174
David Fort0de859e2016-05-27 23:22:57 +0200175 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600176 output->native_mode = mode;
177 output->native_scale = scale;
178
179 weston_mode_switch_finish(output, mode_changed, scale_changed);
180
David Fort0de859e2016-05-27 23:22:57 +0200181 if (mode_changed || scale_changed) {
182 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
183
184 wl_signal_emit(&output->compositor->output_resized_signal, output);
185 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600186 return 0;
187}
188
189WL_EXPORT int
190weston_output_mode_switch_to_native(struct weston_output *output)
191{
192 int ret;
193 int mode_changed = 0, scale_changed = 0;
194
195 if (!output->switch_mode)
196 return -1;
197
198 if (!output->original_mode) {
199 weston_log("already in the native mode\n");
200 return -1;
201 }
202 /* the non fullscreen clients haven't seen a mode set since we
203 * switched into a temporary, so we need to notify them if the
204 * mode at that time is different from the native mode now.
205 */
206 mode_changed = (output->original_mode != output->native_mode);
207 scale_changed = (output->original_scale != output->native_scale);
208
209 ret = output->switch_mode(output, output->native_mode);
210 if (ret < 0)
211 return ret;
212
213 output->current_scale = output->native_scale;
214
215 output->original_mode = NULL;
216 output->original_scale = 0;
217
218 weston_mode_switch_finish(output, mode_changed, scale_changed);
219
220 return 0;
221}
222
223WL_EXPORT int
224weston_output_mode_switch_to_temporary(struct weston_output *output,
225 struct weston_mode *mode,
226 int32_t scale)
227{
228 int ret;
229
230 if (!output->switch_mode)
231 return -1;
232
233 /* original_mode is the last mode non full screen clients have seen,
234 * so we shouldn't change it if we already have one set.
235 */
236 if (!output->original_mode) {
237 output->original_mode = output->native_mode;
238 output->original_scale = output->native_scale;
239 }
240 ret = output->switch_mode(output, mode);
241 if (ret < 0)
242 return ret;
243
244 output->current_scale = scale;
245
246 weston_mode_switch_finish(output, 0, 0);
247
248 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800249}
250
Benjamin Franzke06286262011-05-06 19:12:33 +0200251static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300252region_init_infinite(pixman_region32_t *region)
253{
254 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
255 UINT32_MAX, UINT32_MAX);
256}
257
Pekka Paalanene67858b2013-04-25 13:57:42 +0300258static struct weston_subsurface *
259weston_surface_to_subsurface(struct weston_surface *surface);
260
Jason Ekstranda7af7042013-10-12 22:38:11 -0500261WL_EXPORT struct weston_view *
262weston_view_create(struct weston_surface *surface)
263{
264 struct weston_view *view;
265
Bryce Harringtonde16d892014-11-20 22:21:57 -0800266 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500267 if (view == NULL)
268 return NULL;
269
270 view->surface = surface;
271
Jason Ekstranda7af7042013-10-12 22:38:11 -0500272 /* Assign to surface */
273 wl_list_insert(&surface->views, &view->surface_link);
274
275 wl_signal_init(&view->destroy_signal);
276 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300277 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 pixman_region32_init(&view->clip);
280
281 view->alpha = 1.0;
282 pixman_region32_init(&view->transform.opaque);
283
284 wl_list_init(&view->geometry.transformation_list);
285 wl_list_insert(&view->geometry.transformation_list,
286 &view->transform.position.link);
287 weston_matrix_init(&view->transform.position.matrix);
288 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200289 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500290 pixman_region32_init(&view->transform.boundingbox);
291 view->transform.dirty = 1;
292
Jason Ekstranda7af7042013-10-12 22:38:11 -0500293 return view;
294}
295
Jason Ekstrand108865d2014-06-26 10:04:49 -0700296struct weston_frame_callback {
297 struct wl_resource *resource;
298 struct wl_list link;
299};
300
Pekka Paalanen133e4392014-09-23 22:08:46 -0400301struct weston_presentation_feedback {
302 struct wl_resource *resource;
303
304 /* XXX: could use just wl_resource_get_link() instead */
305 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200306
307 /* The per-surface feedback flags */
308 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400309};
310
311static void
312weston_presentation_feedback_discard(
313 struct weston_presentation_feedback *feedback)
314{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200315 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400316 wl_resource_destroy(feedback->resource);
317}
318
319static void
320weston_presentation_feedback_discard_list(struct wl_list *list)
321{
322 struct weston_presentation_feedback *feedback, *tmp;
323
324 wl_list_for_each_safe(feedback, tmp, list, link)
325 weston_presentation_feedback_discard(feedback);
326}
327
328static void
329weston_presentation_feedback_present(
330 struct weston_presentation_feedback *feedback,
331 struct weston_output *output,
332 uint32_t refresh_nsec,
333 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200334 uint64_t seq,
335 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400336{
337 struct wl_client *client = wl_resource_get_client(feedback->resource);
338 struct wl_resource *o;
339 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400340
341 wl_resource_for_each(o, &output->resource_list) {
342 if (wl_resource_get_client(o) != client)
343 continue;
344
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200345 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400346 }
347
348 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200349 wp_presentation_feedback_send_presented(feedback->resource,
350 secs >> 32, secs & 0xffffffff,
351 ts->tv_nsec,
352 refresh_nsec,
353 seq >> 32, seq & 0xffffffff,
354 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400355 wl_resource_destroy(feedback->resource);
356}
357
358static void
359weston_presentation_feedback_present_list(struct wl_list *list,
360 struct weston_output *output,
361 uint32_t refresh_nsec,
362 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200363 uint64_t seq,
364 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400365{
366 struct weston_presentation_feedback *feedback, *tmp;
367
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200368 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200369 wl_list_empty(list));
370
Pekka Paalanen133e4392014-09-23 22:08:46 -0400371 wl_list_for_each_safe(feedback, tmp, list, link)
372 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200373 refresh_nsec, ts, seq,
374 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400375}
376
Jason Ekstrand7b982072014-05-20 14:33:03 -0500377static void
378surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
379{
380 struct weston_surface_state *state =
381 container_of(listener, struct weston_surface_state,
382 buffer_destroy_listener);
383
384 state->buffer = NULL;
385}
386
387static void
388weston_surface_state_init(struct weston_surface_state *state)
389{
390 state->newly_attached = 0;
391 state->buffer = NULL;
392 state->buffer_destroy_listener.notify =
393 surface_state_handle_buffer_destroy;
394 state->sx = 0;
395 state->sy = 0;
396
Derek Foreman152254b2015-11-26 14:17:48 -0600397 pixman_region32_init(&state->damage_surface);
398 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500399 pixman_region32_init(&state->opaque);
400 region_init_infinite(&state->input);
401
402 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400403 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500404
405 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
406 state->buffer_viewport.buffer.scale = 1;
407 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
408 state->buffer_viewport.surface.width = -1;
409 state->buffer_viewport.changed = 0;
410}
411
412static void
413weston_surface_state_fini(struct weston_surface_state *state)
414{
415 struct weston_frame_callback *cb, *next;
416
417 wl_list_for_each_safe(cb, next,
418 &state->frame_callback_list, link)
419 wl_resource_destroy(cb->resource);
420
Pekka Paalanen133e4392014-09-23 22:08:46 -0400421 weston_presentation_feedback_discard_list(&state->feedback_list);
422
Jason Ekstrand7b982072014-05-20 14:33:03 -0500423 pixman_region32_fini(&state->input);
424 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600425 pixman_region32_fini(&state->damage_surface);
426 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500427
428 if (state->buffer)
429 wl_list_remove(&state->buffer_destroy_listener.link);
430 state->buffer = NULL;
431}
432
433static void
434weston_surface_state_set_buffer(struct weston_surface_state *state,
435 struct weston_buffer *buffer)
436{
437 if (state->buffer == buffer)
438 return;
439
440 if (state->buffer)
441 wl_list_remove(&state->buffer_destroy_listener.link);
442 state->buffer = buffer;
443 if (state->buffer)
444 wl_signal_add(&state->buffer->destroy_signal,
445 &state->buffer_destroy_listener);
446}
447
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500448WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500449weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500450{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500451 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400452
Bryce Harringtonde16d892014-11-20 22:21:57 -0800453 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400454 if (surface == NULL)
455 return NULL;
456
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500457 wl_signal_init(&surface->destroy_signal);
458
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500459 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200460 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400461
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200462 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
463 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200464 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
465 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500466
467 weston_surface_state_init(&surface->pending);
468
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400469 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500470 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300471 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400472
Jason Ekstranda7af7042013-10-12 22:38:11 -0500473 wl_list_init(&surface->views);
474
475 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400476 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500477
Pekka Paalanene67858b2013-04-25 13:57:42 +0300478 wl_list_init(&surface->subsurface_list);
479 wl_list_init(&surface->subsurface_list_pending);
480
Jason Ekstrand1e059042014-10-16 10:55:19 -0500481 weston_matrix_init(&surface->buffer_to_surface_matrix);
482 weston_matrix_init(&surface->surface_to_buffer_matrix);
483
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400484 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500485}
486
Alex Wu8811bf92012-02-28 18:07:54 +0800487WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500488weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200489 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500490{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100491 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500492}
493
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400494WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500495weston_view_to_global_float(struct weston_view *view,
496 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200497{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500498 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200499 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
500
Jason Ekstranda7af7042013-10-12 22:38:11 -0500501 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200502
503 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200504 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700505 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200506 v.f[3]);
507 *x = 0;
508 *y = 0;
509 return;
510 }
511
512 *x = v.f[0] / v.f[3];
513 *y = v.f[1] / v.f[3];
514 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500515 *x = sx + view->geometry.x;
516 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200517 }
518}
519
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500520WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200521weston_transformed_coord(int width, int height,
522 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200523 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200524 float sx, float sy, float *bx, float *by)
525{
526 switch (transform) {
527 case WL_OUTPUT_TRANSFORM_NORMAL:
528 default:
529 *bx = sx;
530 *by = sy;
531 break;
532 case WL_OUTPUT_TRANSFORM_FLIPPED:
533 *bx = width - sx;
534 *by = sy;
535 break;
536 case WL_OUTPUT_TRANSFORM_90:
537 *bx = height - sy;
538 *by = sx;
539 break;
540 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
541 *bx = height - sy;
542 *by = width - sx;
543 break;
544 case WL_OUTPUT_TRANSFORM_180:
545 *bx = width - sx;
546 *by = height - sy;
547 break;
548 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
549 *bx = sx;
550 *by = height - sy;
551 break;
552 case WL_OUTPUT_TRANSFORM_270:
553 *bx = sy;
554 *by = width - sx;
555 break;
556 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
557 *bx = sy;
558 *by = sx;
559 break;
560 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200561
562 *bx *= scale;
563 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200564}
565
566WL_EXPORT pixman_box32_t
567weston_transformed_rect(int width, int height,
568 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200569 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200570 pixman_box32_t rect)
571{
572 float x1, x2, y1, y2;
573
574 pixman_box32_t ret;
575
Alexander Larsson4ea95522013-05-22 14:41:37 +0200576 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200577 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200578 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200579 rect.x2, rect.y2, &x2, &y2);
580
581 if (x1 <= x2) {
582 ret.x1 = x1;
583 ret.x2 = x2;
584 } else {
585 ret.x1 = x2;
586 ret.x2 = x1;
587 }
588
589 if (y1 <= y2) {
590 ret.y1 = y1;
591 ret.y2 = y2;
592 } else {
593 ret.y1 = y2;
594 ret.y2 = y1;
595 }
596
597 return ret;
598}
599
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600600/** Transform a region by a matrix, restricted to axis-aligned transformations
601 *
602 * Warning: This function does not work for projective, affine, or matrices
603 * that encode arbitrary rotations. Only 90-degree step rotations are
604 * supported.
605 */
606WL_EXPORT void
607weston_matrix_transform_region(pixman_region32_t *dest,
608 struct weston_matrix *matrix,
609 pixman_region32_t *src)
610{
611 pixman_box32_t *src_rects, *dest_rects;
612 int nrects, i;
613
614 src_rects = pixman_region32_rectangles(src, &nrects);
615 dest_rects = malloc(nrects * sizeof(*dest_rects));
616 if (!dest_rects)
617 return;
618
619 for (i = 0; i < nrects; i++) {
620 struct weston_vector vec1 = {{
621 src_rects[i].x1, src_rects[i].y1, 0, 1
622 }};
623 weston_matrix_transform(matrix, &vec1);
624 vec1.f[0] /= vec1.f[3];
625 vec1.f[1] /= vec1.f[3];
626
627 struct weston_vector vec2 = {{
628 src_rects[i].x2, src_rects[i].y2, 0, 1
629 }};
630 weston_matrix_transform(matrix, &vec2);
631 vec2.f[0] /= vec2.f[3];
632 vec2.f[1] /= vec2.f[3];
633
634 if (vec1.f[0] < vec2.f[0]) {
635 dest_rects[i].x1 = floor(vec1.f[0]);
636 dest_rects[i].x2 = ceil(vec2.f[0]);
637 } else {
638 dest_rects[i].x1 = floor(vec2.f[0]);
639 dest_rects[i].x2 = ceil(vec1.f[0]);
640 }
641
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600642 if (vec1.f[1] < vec2.f[1]) {
643 dest_rects[i].y1 = floor(vec1.f[1]);
644 dest_rects[i].y2 = ceil(vec2.f[1]);
645 } else {
646 dest_rects[i].y1 = floor(vec2.f[1]);
647 dest_rects[i].y2 = ceil(vec1.f[1]);
648 }
649 }
650
651 pixman_region32_clear(dest);
652 pixman_region32_init_rects(dest, dest_rects, nrects);
653 free(dest_rects);
654}
655
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200656WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500657weston_transformed_region(int width, int height,
658 enum wl_output_transform transform,
659 int32_t scale,
660 pixman_region32_t *src, pixman_region32_t *dest)
661{
662 pixman_box32_t *src_rects, *dest_rects;
663 int nrects, i;
664
665 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
666 if (src != dest)
667 pixman_region32_copy(dest, src);
668 return;
669 }
670
671 src_rects = pixman_region32_rectangles(src, &nrects);
672 dest_rects = malloc(nrects * sizeof(*dest_rects));
673 if (!dest_rects)
674 return;
675
676 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
677 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
678 } else {
679 for (i = 0; i < nrects; i++) {
680 switch (transform) {
681 default:
682 case WL_OUTPUT_TRANSFORM_NORMAL:
683 dest_rects[i].x1 = src_rects[i].x1;
684 dest_rects[i].y1 = src_rects[i].y1;
685 dest_rects[i].x2 = src_rects[i].x2;
686 dest_rects[i].y2 = src_rects[i].y2;
687 break;
688 case WL_OUTPUT_TRANSFORM_90:
689 dest_rects[i].x1 = height - src_rects[i].y2;
690 dest_rects[i].y1 = src_rects[i].x1;
691 dest_rects[i].x2 = height - src_rects[i].y1;
692 dest_rects[i].y2 = src_rects[i].x2;
693 break;
694 case WL_OUTPUT_TRANSFORM_180:
695 dest_rects[i].x1 = width - src_rects[i].x2;
696 dest_rects[i].y1 = height - src_rects[i].y2;
697 dest_rects[i].x2 = width - src_rects[i].x1;
698 dest_rects[i].y2 = height - src_rects[i].y1;
699 break;
700 case WL_OUTPUT_TRANSFORM_270:
701 dest_rects[i].x1 = src_rects[i].y1;
702 dest_rects[i].y1 = width - src_rects[i].x2;
703 dest_rects[i].x2 = src_rects[i].y2;
704 dest_rects[i].y2 = width - src_rects[i].x1;
705 break;
706 case WL_OUTPUT_TRANSFORM_FLIPPED:
707 dest_rects[i].x1 = width - src_rects[i].x2;
708 dest_rects[i].y1 = src_rects[i].y1;
709 dest_rects[i].x2 = width - src_rects[i].x1;
710 dest_rects[i].y2 = src_rects[i].y2;
711 break;
712 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
713 dest_rects[i].x1 = height - src_rects[i].y2;
714 dest_rects[i].y1 = width - src_rects[i].x2;
715 dest_rects[i].x2 = height - src_rects[i].y1;
716 dest_rects[i].y2 = width - src_rects[i].x1;
717 break;
718 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
719 dest_rects[i].x1 = src_rects[i].x1;
720 dest_rects[i].y1 = height - src_rects[i].y2;
721 dest_rects[i].x2 = src_rects[i].x2;
722 dest_rects[i].y2 = height - src_rects[i].y1;
723 break;
724 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
725 dest_rects[i].x1 = src_rects[i].y1;
726 dest_rects[i].y1 = src_rects[i].x1;
727 dest_rects[i].x2 = src_rects[i].y2;
728 dest_rects[i].y2 = src_rects[i].x2;
729 break;
730 }
731 }
732 }
733
734 if (scale != 1) {
735 for (i = 0; i < nrects; i++) {
736 dest_rects[i].x1 *= scale;
737 dest_rects[i].x2 *= scale;
738 dest_rects[i].y1 *= scale;
739 dest_rects[i].y2 *= scale;
740 }
741 }
742
743 pixman_region32_clear(dest);
744 pixman_region32_init_rects(dest, dest_rects, nrects);
745 free(dest_rects);
746}
747
Jonny Lamb74130762013-11-26 18:19:46 +0100748static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300749viewport_surface_to_buffer(struct weston_surface *surface,
750 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100751{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200752 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200753 double src_width, src_height;
754 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200755
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200756 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
757 if (vp->surface.width == -1) {
758 *bx = sx;
759 *by = sy;
760 return;
761 }
Jonny Lamb74130762013-11-26 18:19:46 +0100762
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200763 src_x = 0.0;
764 src_y = 0.0;
765 src_width = surface->width_from_buffer;
766 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100767 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200768 src_x = wl_fixed_to_double(vp->buffer.src_x);
769 src_y = wl_fixed_to_double(vp->buffer.src_y);
770 src_width = wl_fixed_to_double(vp->buffer.src_width);
771 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100772 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200773
774 *bx = sx * src_width / surface->width + src_x;
775 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100776}
777
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500778WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200779weston_surface_to_buffer_float(struct weston_surface *surface,
780 float sx, float sy, float *bx, float *by)
781{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200782 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
783
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300784 /* first transform coordinates if the viewport is set */
785 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100786
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500787 weston_transformed_coord(surface->width_from_buffer,
788 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200789 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100790 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200791}
792
Derek Foreman99739672015-12-03 16:38:11 -0600793/** Transform a rectangle from surface coordinates to buffer coordinates
794 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300795 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600796 * from.
797 * \param rect The rectangle to transform.
798 * \return The transformed rectangle.
799 *
800 * Viewport and buffer transformations can only do translation, scaling,
801 * and rotations in 90-degree steps. Therefore the only loss in the
802 * conversion is coordinate rounding.
803 *
804 * However, some coordinate rounding takes place as an intermediate
805 * step before the buffer scale factor is applied, so the rectangle
806 * boundary may not be exactly as expected.
807 *
808 * This is OK for damage tracking since a little extra coverage is
809 * not a problem.
810 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200811WL_EXPORT pixman_box32_t
812weston_surface_to_buffer_rect(struct weston_surface *surface,
813 pixman_box32_t rect)
814{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200815 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100816 float xf, yf;
817
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300818 /* first transform box coordinates if the viewport is set */
819 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100820 rect.x1 = floorf(xf);
821 rect.y1 = floorf(yf);
822
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300823 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600824 rect.x2 = ceilf(xf);
825 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100826
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500827 return weston_transformed_rect(surface->width_from_buffer,
828 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200829 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200830 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200831}
832
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200833/** Transform a region from surface coordinates to buffer coordinates
834 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300835 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200836 * from.
837 * \param surface_region[in] The region in surface coordinates.
838 * \param buffer_region[out] The region converted to buffer coordinates.
839 *
840 * Buffer_region must be init'd, but will be completely overwritten.
841 *
842 * Viewport and buffer transformations can only do translation, scaling,
843 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600844 * conversion is from the coordinate rounding that takes place in
845 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200846 */
847WL_EXPORT void
848weston_surface_to_buffer_region(struct weston_surface *surface,
849 pixman_region32_t *surface_region,
850 pixman_region32_t *buffer_region)
851{
852 pixman_box32_t *src_rects, *dest_rects;
853 int nrects, i;
854
855 src_rects = pixman_region32_rectangles(surface_region, &nrects);
856 dest_rects = malloc(nrects * sizeof(*dest_rects));
857 if (!dest_rects)
858 return;
859
860 for (i = 0; i < nrects; i++) {
861 dest_rects[i] = weston_surface_to_buffer_rect(surface,
862 src_rects[i]);
863 }
864
865 pixman_region32_fini(buffer_region);
866 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
867 free(dest_rects);
868}
869
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200870WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500871weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400872 struct weston_plane *plane)
873{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500874 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400875 return;
876
Jason Ekstranda7af7042013-10-12 22:38:11 -0500877 weston_view_damage_below(view);
878 view->plane = plane;
879 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400880}
881
Pekka Paalanen51723d52015-02-17 13:10:01 +0200882/** Inflict damage on the plane where the view is visible.
883 *
884 * \param view The view that causes the damage.
885 *
886 * If the view is currently on a plane (including the primary plane),
887 * take the view's boundingbox, subtract all the opaque views that cover it,
888 * and add the remaining region as damage to the plane. This corresponds
889 * to the damage inflicted to the plane if this view disappeared.
890 *
891 * A repaint is scheduled for this view.
892 *
893 * The region of all opaque views covering this view is stored in
894 * weston_view::clip and updated by view_accumulate_damage() during
895 * weston_output_repaint(). Specifically, that region matches the
896 * scenegraph as it was last painted.
897 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400898WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500899weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200900{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500901 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200902
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500903 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200904 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500905 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800906 if (view->plane)
907 pixman_region32_union(&view->plane->damage,
908 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500909 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800910 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200911}
912
Bryce Harrington3f650b82015-12-23 11:01:58 -0800913/**
914 * \param es The surface
915 * \param mask The new set of outputs for the surface
916 *
917 * Sets the surface's set of outputs to the ones specified by
918 * the new output mask provided. Identifies the outputs that
919 * have changed, the posts enter and leave events for these
920 * outputs as appropriate.
921 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400922static void
923weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
924{
925 uint32_t different = es->output_mask ^ mask;
926 uint32_t entered = mask & different;
927 uint32_t left = es->output_mask & different;
928 struct weston_output *output;
929 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500930 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400931
932 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500933 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400934 return;
935 if (different == 0)
936 return;
937
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500938 client = wl_resource_get_client(es->resource);
939
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400940 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800941 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400942 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500943 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400944 client);
945 if (resource == NULL)
946 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800947 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500948 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800949 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500950 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400951 }
952}
953
Bryce Harrington3f650b82015-12-23 11:01:58 -0800954/** Recalculate which output(s) the surface has views displayed on
955 *
956 * \param es The surface to remap to outputs
957 *
958 * Finds the output that is showing the largest amount of one
959 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300960 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800961 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300962 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800963 * in the output_mask for the surface.
964 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400965static void
966weston_surface_assign_output(struct weston_surface *es)
967{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 struct weston_output *new_output;
969 struct weston_view *view;
970 pixman_region32_t region;
971 uint32_t max, area, mask;
972 pixman_box32_t *e;
973
974 new_output = NULL;
975 max = 0;
976 mask = 0;
977 pixman_region32_init(&region);
978 wl_list_for_each(view, &es->views, surface_link) {
979 if (!view->output)
980 continue;
981
982 pixman_region32_intersect(&region, &view->transform.boundingbox,
983 &view->output->region);
984
985 e = pixman_region32_extents(&region);
986 area = (e->x2 - e->x1) * (e->y2 - e->y1);
987
988 mask |= view->output_mask;
989
990 if (area >= max) {
991 new_output = view->output;
992 max = area;
993 }
994 }
995 pixman_region32_fini(&region);
996
997 es->output = new_output;
998 weston_surface_update_output_mask(es, mask);
999}
1000
Bryce Harrington3f650b82015-12-23 11:01:58 -08001001/** Recalculate which output(s) the view is displayed on
1002 *
1003 * \param ev The view to remap to outputs
1004 *
1005 * Identifies the set of outputs that the view is visible on,
1006 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001007 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001008 *
1009 * Also does the same for the view's surface. See
1010 * weston_surface_assign_output().
1011 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012static void
1013weston_view_assign_output(struct weston_view *ev)
1014{
1015 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001016 struct weston_output *output, *new_output;
1017 pixman_region32_t region;
1018 uint32_t max, area, mask;
1019 pixman_box32_t *e;
1020
1021 new_output = NULL;
1022 max = 0;
1023 mask = 0;
1024 pixman_region32_init(&region);
1025 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001026 if (output->destroying)
1027 continue;
1028
Jason Ekstranda7af7042013-10-12 22:38:11 -05001029 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001030 &output->region);
1031
1032 e = pixman_region32_extents(&region);
1033 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1034
1035 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001036 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001037
1038 if (area >= max) {
1039 new_output = output;
1040 max = area;
1041 }
1042 }
1043 pixman_region32_fini(&region);
1044
Jason Ekstranda7af7042013-10-12 22:38:11 -05001045 ev->output = new_output;
1046 ev->output_mask = mask;
1047
1048 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001049}
1050
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001051static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001052weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1053 int from_x, int from_y, int *to_x, int *to_y)
1054{
1055 float x, y;
1056
1057 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1058 weston_view_from_global_float(to, x, y, &x, &y);
1059
1060 *to_x = round(x);
1061 *to_y = round(y);
1062}
1063
1064static void
1065weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1066{
1067 pixman_box32_t *a;
1068 pixman_box32_t b;
1069
1070 a = pixman_region32_extents(&from->geometry.scissor);
1071
1072 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1073 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1074
1075 pixman_region32_fini(&to->geometry.scissor);
1076 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1077}
1078
1079static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001080view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001081 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001082{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001083 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1084 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001085 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001086 { inbox->x1, inbox->y1 },
1087 { inbox->x1, inbox->y2 },
1088 { inbox->x2, inbox->y1 },
1089 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001090 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001091 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001092 int i;
1093
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001094 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001095 /* avoid rounding empty bbox to 1x1 */
1096 pixman_region32_init(bbox);
1097 return;
1098 }
1099
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001100 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001101 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001102 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001103 if (x < min_x)
1104 min_x = x;
1105 if (x > max_x)
1106 max_x = x;
1107 if (y < min_y)
1108 min_y = y;
1109 if (y > max_y)
1110 max_y = y;
1111 }
1112
Pekka Paalanen219b9822012-02-08 15:38:37 +02001113 int_x = floorf(min_x);
1114 int_y = floorf(min_y);
1115 pixman_region32_init_rect(bbox, int_x, int_y,
1116 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001117}
1118
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001119static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001121{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001123
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001124 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 view->geometry.x = roundf(view->geometry.x);
1126 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001127
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001128 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001129 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1130 view->transform.position.matrix.d[12] = view->geometry.x;
1131 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001132
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001134
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 view->transform.inverse = view->transform.position.matrix;
1136 view->transform.inverse.d[12] = -view->geometry.x;
1137 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001138
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001140 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001141 view->surface->width,
1142 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001143 if (view->geometry.scissor_enabled)
1144 pixman_region32_intersect(&view->transform.boundingbox,
1145 &view->transform.boundingbox,
1146 &view->geometry.scissor);
1147
1148 pixman_region32_translate(&view->transform.boundingbox,
1149 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001150
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 if (view->alpha == 1.0) {
1152 pixman_region32_copy(&view->transform.opaque,
1153 &view->surface->opaque);
1154 pixman_region32_translate(&view->transform.opaque,
1155 view->geometry.x,
1156 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001157 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001158}
1159
1160static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001162{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 struct weston_view *parent = view->geometry.parent;
1164 struct weston_matrix *matrix = &view->transform.matrix;
1165 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001166 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001167 pixman_region32_t surfregion;
1168 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001171
1172 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1174 view->transform.position.matrix.d[12] = view->geometry.x;
1175 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001176
1177 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001179 weston_matrix_multiply(matrix, &tform->matrix);
1180
Pekka Paalanen483243f2013-03-08 14:56:50 +02001181 if (parent)
1182 weston_matrix_multiply(matrix, &parent->transform.matrix);
1183
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001184 if (weston_matrix_invert(inverse, matrix) < 0) {
1185 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 weston_log("error: weston_view %p"
1187 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001188 return -1;
1189 }
1190
Pekka Paalanen380adf52015-02-16 14:39:11 +02001191 pixman_region32_init_rect(&surfregion, 0, 0,
1192 view->surface->width, view->surface->height);
1193 if (view->geometry.scissor_enabled)
1194 pixman_region32_intersect(&surfregion, &surfregion,
1195 &view->geometry.scissor);
1196 surfbox = pixman_region32_extents(&surfregion);
1197
1198 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1199 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001200
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001201 return 0;
1202}
1203
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001204static struct weston_layer *
1205get_view_layer(struct weston_view *view)
1206{
1207 if (view->parent_view)
1208 return get_view_layer(view->parent_view);
1209 return view->layer_link.layer;
1210}
1211
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001212WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001214{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001216 struct weston_layer *layer;
1217 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001218
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001220 return;
1221
Pekka Paalanen483243f2013-03-08 14:56:50 +02001222 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001224
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001226
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001228
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 pixman_region32_fini(&view->transform.boundingbox);
1230 pixman_region32_fini(&view->transform.opaque);
1231 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001232
Pekka Paalanencd403622012-01-25 13:37:39 +02001233 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 if (view->geometry.transformation_list.next ==
1235 &view->transform.position.link &&
1236 view->geometry.transformation_list.prev ==
1237 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001238 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001240 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 if (weston_view_update_transform_enable(view) < 0)
1242 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001243 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001244
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001245 layer = get_view_layer(view);
1246 if (layer) {
1247 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001248 pixman_region32_intersect(&view->transform.boundingbox,
1249 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001250 pixman_region32_intersect(&view->transform.opaque,
1251 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001252 pixman_region32_fini(&mask);
1253 }
1254
Pekka Paalanen380adf52015-02-16 14:39:11 +02001255 if (parent) {
1256 if (parent->geometry.scissor_enabled) {
1257 view->geometry.scissor_enabled = true;
1258 weston_view_transfer_scissor(parent, view);
1259 } else {
1260 view->geometry.scissor_enabled = false;
1261 }
1262 }
1263
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001265
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001267
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 wl_signal_emit(&view->surface->compositor->transform_signal,
1269 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001270}
1271
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001272WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001274{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001276
1277 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 * The invariant: if view->geometry.dirty, then all views
1279 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001280 * Corollary: if not parent->geometry.dirty, then all ancestors
1281 * are not dirty.
1282 */
1283
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001285 return;
1286
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001288
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001290 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001292}
1293
1294WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295weston_view_to_global_fixed(struct weston_view *view,
1296 wl_fixed_t vx, wl_fixed_t vy,
1297 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001298{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001299 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 weston_view_to_global_float(view,
1302 wl_fixed_to_double(vx),
1303 wl_fixed_to_double(vy),
1304 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001305 *x = wl_fixed_from_double(xf);
1306 *y = wl_fixed_from_double(yf);
1307}
1308
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001309WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310weston_view_from_global_float(struct weston_view *view,
1311 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001312{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001314 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1315
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001317
1318 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001319 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001321 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 *vx = 0;
1323 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001324 return;
1325 }
1326
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327 *vx = v.f[0] / v.f[3];
1328 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001329 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 *vx = x - view->geometry.x;
1331 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001332 }
1333}
1334
1335WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336weston_view_from_global_fixed(struct weston_view *view,
1337 wl_fixed_t x, wl_fixed_t y,
1338 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001339{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001341
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 weston_view_from_global_float(view,
1343 wl_fixed_to_double(x),
1344 wl_fixed_to_double(y),
1345 &vxf, &vyf);
1346 *vx = wl_fixed_from_double(vxf);
1347 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001348}
1349
1350WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001351weston_view_from_global(struct weston_view *view,
1352 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001353{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001355
Jason Ekstranda7af7042013-10-12 22:38:11 -05001356 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1357 *vx = floorf(vxf);
1358 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001359}
1360
Bryce Harrington3f650b82015-12-23 11:01:58 -08001361/**
1362 * \param surface The surface to be repainted
1363 *
1364 * Marks the output(s) that the surface is shown on as needing to be
1365 * repainted. See weston_output_schedule_repaint().
1366 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001367WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001368weston_surface_schedule_repaint(struct weston_surface *surface)
1369{
1370 struct weston_output *output;
1371
1372 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001373 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001374 weston_output_schedule_repaint(output);
1375}
1376
Bryce Harrington3f650b82015-12-23 11:01:58 -08001377/**
1378 * \param view The view to be repainted
1379 *
1380 * Marks the output(s) that the view is shown on as needing to be
1381 * repainted. See weston_output_schedule_repaint().
1382 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001383WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384weston_view_schedule_repaint(struct weston_view *view)
1385{
1386 struct weston_output *output;
1387
1388 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001389 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390 weston_output_schedule_repaint(output);
1391}
1392
Pekka Paalanene508ce62015-02-19 13:59:55 +02001393/**
1394 * XXX: This function does it the wrong way.
1395 * surface->damage is the damage from the client, and causes
1396 * surface_flush_damage() to copy pixels. No window management action can
1397 * cause damage to the client-provided content, warranting re-upload!
1398 *
1399 * Instead of surface->damage, this function should record the damage
1400 * with all the views for this surface to avoid extraneous texture
1401 * uploads.
1402 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001404weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001405{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001406 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 0, 0, surface->width,
1408 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001409
Kristian Høgsberg98238702012-08-03 16:29:12 -04001410 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001411}
1412
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001413WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001415{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001416 if (view->geometry.x == x && view->geometry.y == y)
1417 return;
1418
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 view->geometry.x = x;
1420 view->geometry.y = y;
1421 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001422}
1423
Pekka Paalanen483243f2013-03-08 14:56:50 +02001424static void
1425transform_parent_handle_parent_destroy(struct wl_listener *listener,
1426 void *data)
1427{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 struct weston_view *view =
1429 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001430 geometry.parent_destroy_listener);
1431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001433}
1434
1435WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001437 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001438{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439 if (view->geometry.parent) {
1440 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1441 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001442
1443 if (!parent)
1444 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001445 }
1446
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001448
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001450 transform_parent_handle_parent_destroy;
1451 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001452 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001454 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001456 }
1457
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458 weston_view_geometry_dirty(view);
1459}
1460
Pekka Paalanen380adf52015-02-16 14:39:11 +02001461/** Set a clip mask rectangle on a view
1462 *
1463 * \param view The view to set the clip mask on.
1464 * \param x Top-left corner X coordinate of the clip rectangle.
1465 * \param y Top-left corner Y coordinate of the clip rectangle.
1466 * \param width Width of the clip rectangle, non-negative.
1467 * \param height Height of the clip rectangle, non-negative.
1468 *
1469 * A shell may set a clip mask rectangle on a view. Everything outside
1470 * the rectangle is cut away for input and output purposes: it is
1471 * not drawn and cannot be hit by hit-test based input like pointer
1472 * motion or touch-downs. Everything inside the rectangle will behave
1473 * normally. Clients are unaware of clipping.
1474 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001475 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001476 * mask rectangle does not affect the view position, the view is positioned
1477 * as it would be without a clip. The clip also does not change
1478 * weston_surface::width,height.
1479 *
1480 * The clip mask rectangle is part of transformation inheritance
1481 * (weston_view_set_transform_parent()). A clip set in the root of the
1482 * transformation inheritance tree will affect all views in the tree.
1483 * A clip can be set only on the root view. Attempting to set a clip
1484 * on view that has a transformation parent will fail. Assigning a parent
1485 * to a view that has a clip set will cause the clip to be forgotten.
1486 *
1487 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1488 * on the additional transformations in the child views. These transformations
1489 * may not rotate the coordinate axes, i.e., only translation and scaling
1490 * are allowed. Violating this restriction causes the clipping to malfunction.
1491 * Furthermore, using scaling may cause rounding errors in child clipping.
1492 *
1493 * The clip mask rectangle is not automatically adjusted based on
1494 * wl_surface.attach dx and dy arguments.
1495 *
1496 * A clip mask rectangle can be set only if the compositor capability
1497 * WESTON_CAP_VIEW_CLIP_MASK is present.
1498 *
1499 * This function sets the clip mask rectangle and schedules a repaint for
1500 * the view.
1501 */
1502WL_EXPORT void
1503weston_view_set_mask(struct weston_view *view,
1504 int x, int y, int width, int height)
1505{
1506 struct weston_compositor *compositor = view->surface->compositor;
1507
1508 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1509 weston_log("%s not allowed without capability!\n", __func__);
1510 return;
1511 }
1512
1513 if (view->geometry.parent) {
1514 weston_log("view %p has a parent, clip forbidden!\n", view);
1515 return;
1516 }
1517
1518 if (width < 0 || height < 0) {
1519 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1520 x, y, width, height);
1521 return;
1522 }
1523
1524 pixman_region32_fini(&view->geometry.scissor);
1525 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1526 view->geometry.scissor_enabled = true;
1527 weston_view_geometry_dirty(view);
1528 weston_view_schedule_repaint(view);
1529}
1530
1531/** Remove the clip mask from a view
1532 *
1533 * \param view The view to remove the clip mask from.
1534 *
1535 * Removed the clip mask rectangle and schedules a repaint.
1536 *
1537 * \sa weston_view_set_mask
1538 */
1539WL_EXPORT void
1540weston_view_set_mask_infinite(struct weston_view *view)
1541{
1542 view->geometry.scissor_enabled = false;
1543 weston_view_geometry_dirty(view);
1544 weston_view_schedule_repaint(view);
1545}
1546
Derek Foreman280e7dd2014-10-03 13:13:42 -05001547WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548weston_view_is_mapped(struct weston_view *view)
1549{
1550 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001551 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001553 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001554}
1555
Derek Foreman280e7dd2014-10-03 13:13:42 -05001556WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001557weston_surface_is_mapped(struct weston_surface *surface)
1558{
1559 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001560 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001561 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001562 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001563}
1564
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001565static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001566surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001567{
1568 struct weston_view *view;
1569
1570 if (surface->width == width && surface->height == height)
1571 return;
1572
1573 surface->width = width;
1574 surface->height = height;
1575
1576 wl_list_for_each(view, &surface->views, surface_link)
1577 weston_view_geometry_dirty(view);
1578}
1579
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001580WL_EXPORT void
1581weston_surface_set_size(struct weston_surface *surface,
1582 int32_t width, int32_t height)
1583{
1584 assert(!surface->resource);
1585 surface_set_size(surface, width, height);
1586}
1587
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001588static int
1589fixed_round_up_to_int(wl_fixed_t f)
1590{
1591 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1592}
1593
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001594static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001595convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1596 int32_t width, int32_t height,
1597 uint32_t transform,
1598 int32_t scale)
1599{
1600 assert(scale > 0);
1601
1602 switch (transform) {
1603 case WL_OUTPUT_TRANSFORM_NORMAL:
1604 case WL_OUTPUT_TRANSFORM_180:
1605 case WL_OUTPUT_TRANSFORM_FLIPPED:
1606 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1607 *width_out = width / scale;
1608 *height_out = height / scale;
1609 break;
1610 case WL_OUTPUT_TRANSFORM_90:
1611 case WL_OUTPUT_TRANSFORM_270:
1612 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1613 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1614 *width_out = height / scale;
1615 *height_out = width / scale;
1616 break;
1617 default:
1618 assert(0 && "invalid transform");
1619 }
1620}
1621
1622static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001623weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001624{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001625 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001626
1627 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001628 surface->width_from_buffer = 0;
1629 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001630 return;
1631 }
1632
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001633 convert_size_by_transform_scale(&surface->width_from_buffer,
1634 &surface->height_from_buffer,
1635 surface->buffer_ref.buffer->width,
1636 surface->buffer_ref.buffer->height,
1637 vp->buffer.transform,
1638 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001639}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001640
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001641static void
1642weston_surface_update_size(struct weston_surface *surface)
1643{
1644 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1645 int32_t width, height;
1646
1647 width = surface->width_from_buffer;
1648 height = surface->height_from_buffer;
1649
1650 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001651 surface_set_size(surface,
1652 vp->surface.width, vp->surface.height);
1653 return;
1654 }
1655
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001656 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001657 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1658 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1659
1660 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001661 return;
1662 }
1663
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001664 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001665}
1666
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001667WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001668weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001669{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001670 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001671
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001672 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001673
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001674 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001675}
1676
Jason Ekstranda7af7042013-10-12 22:38:11 -05001677WL_EXPORT struct weston_view *
1678weston_compositor_pick_view(struct weston_compositor *compositor,
1679 wl_fixed_t x, wl_fixed_t y,
1680 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001681{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001683 wl_fixed_t view_x, view_y;
1684 int view_ix, view_iy;
1685 int ix = wl_fixed_to_int(x);
1686 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001687
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001689 if (!pixman_region32_contains_point(
1690 &view->transform.boundingbox, ix, iy, NULL))
1691 continue;
1692
1693 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1694 view_ix = wl_fixed_to_int(view_x);
1695 view_iy = wl_fixed_to_int(view_y);
1696
1697 if (!pixman_region32_contains_point(&view->surface->input,
1698 view_ix, view_iy, NULL))
1699 continue;
1700
Pekka Paalanen380adf52015-02-16 14:39:11 +02001701 if (view->geometry.scissor_enabled &&
1702 !pixman_region32_contains_point(&view->geometry.scissor,
1703 view_ix, view_iy, NULL))
1704 continue;
1705
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001706 *vx = view_x;
1707 *vy = view_y;
1708 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001709 }
1710
Derek Foremanf9318d12015-05-11 15:40:11 -05001711 *vx = wl_fixed_from_int(-1000000);
1712 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001713 return NULL;
1714}
1715
1716static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001717weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001718{
Daniel Stone37816df2012-05-16 18:45:18 +01001719 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001720
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001721 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001722 return;
1723
Daniel Stone37816df2012-05-16 18:45:18 +01001724 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001725 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001726}
1727
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001728WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001729weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001730{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001731 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001732
Jason Ekstranda7af7042013-10-12 22:38:11 -05001733 if (!weston_view_is_mapped(view))
1734 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001735
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736 weston_view_damage_below(view);
1737 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001738 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001739 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001740 wl_list_remove(&view->link);
1741 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001742 view->output_mask = 0;
1743 weston_surface_assign_output(view->surface);
1744
1745 if (weston_surface_is_mapped(view->surface))
1746 return;
1747
1748 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001749 struct weston_touch *touch = weston_seat_get_touch(seat);
1750 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1751 struct weston_keyboard *keyboard =
1752 weston_seat_get_keyboard(seat);
1753
1754 if (keyboard && keyboard->focus == view->surface)
1755 weston_keyboard_set_focus(keyboard, NULL);
1756 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001757 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001758 if (touch && touch->focus == view)
1759 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001760 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001761}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001762
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763WL_EXPORT void
1764weston_surface_unmap(struct weston_surface *surface)
1765{
1766 struct weston_view *view;
1767
1768 wl_list_for_each(view, &surface->views, surface_link)
1769 weston_view_unmap(view);
1770 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001771}
1772
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001773static void
1774weston_surface_reset_pending_buffer(struct weston_surface *surface)
1775{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001776 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001777 surface->pending.sx = 0;
1778 surface->pending.sy = 0;
1779 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001780 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001781}
1782
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783WL_EXPORT void
1784weston_view_destroy(struct weston_view *view)
1785{
1786 wl_signal_emit(&view->destroy_signal, view);
1787
1788 assert(wl_list_empty(&view->geometry.child_list));
1789
1790 if (weston_view_is_mapped(view)) {
1791 weston_view_unmap(view);
1792 weston_compositor_build_view_list(view->surface->compositor);
1793 }
1794
1795 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001796 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001797
1798 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001799 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001800 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001801 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001802
1803 weston_view_set_transform_parent(view, NULL);
1804
Jason Ekstranda7af7042013-10-12 22:38:11 -05001805 wl_list_remove(&view->surface_link);
1806
1807 free(view);
1808}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001809
1810WL_EXPORT void
1811weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001812{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001813 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001814 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001815
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001816 if (--surface->ref_count > 0)
1817 return;
1818
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001819 assert(surface->resource == NULL);
1820
Pekka Paalanenca790762015-04-17 14:23:38 +03001821 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001822
Pekka Paalanene67858b2013-04-25 13:57:42 +03001823 assert(wl_list_empty(&surface->subsurface_list_pending));
1824 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001825
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1827 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001828
Jason Ekstrand7b982072014-05-20 14:33:03 -05001829 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001830
Pekka Paalanende685b82012-12-04 15:58:12 +02001831 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001832
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001833 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001834 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001835 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001836
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001837 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001838 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001839
Pekka Paalanen133e4392014-09-23 22:08:46 -04001840 weston_presentation_feedback_discard_list(&surface->feedback_list);
1841
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001842 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001843}
1844
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001845static void
1846destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001847{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001848 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001849
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001850 assert(surface);
1851
Giulio Camuffo0d379742013-11-15 22:06:15 +01001852 /* Set the resource to NULL, since we don't want to leave a
1853 * dangling pointer if the surface was refcounted and survives
1854 * the weston_surface_destroy() call. */
1855 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001856
1857 if (surface->viewport_resource)
1858 wl_resource_set_user_data(surface->viewport_resource, NULL);
1859
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001860 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001861}
1862
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001863static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001864weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1865{
1866 struct weston_buffer *buffer =
1867 container_of(listener, struct weston_buffer, destroy_listener);
1868
1869 wl_signal_emit(&buffer->destroy_signal, buffer);
1870 free(buffer);
1871}
1872
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001873WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001874weston_buffer_from_resource(struct wl_resource *resource)
1875{
1876 struct weston_buffer *buffer;
1877 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001878
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001879 listener = wl_resource_get_destroy_listener(resource,
1880 weston_buffer_destroy_handler);
1881
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001882 if (listener)
1883 return container_of(listener, struct weston_buffer,
1884 destroy_listener);
1885
1886 buffer = zalloc(sizeof *buffer);
1887 if (buffer == NULL)
1888 return NULL;
1889
1890 buffer->resource = resource;
1891 wl_signal_init(&buffer->destroy_signal);
1892 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001893 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001894 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001895
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001896 return buffer;
1897}
1898
1899static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001900weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1901 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001902{
Pekka Paalanende685b82012-12-04 15:58:12 +02001903 struct weston_buffer_reference *ref =
1904 container_of(listener, struct weston_buffer_reference,
1905 destroy_listener);
1906
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001907 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001908 ref->buffer = NULL;
1909}
1910
1911WL_EXPORT void
1912weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001913 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001914{
1915 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001916 ref->buffer->busy_count--;
1917 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001918 assert(wl_resource_get_client(ref->buffer->resource));
1919 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001920 WL_BUFFER_RELEASE);
1921 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001922 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001923 }
1924
Pekka Paalanende685b82012-12-04 15:58:12 +02001925 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001926 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001927 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001928 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001929 }
1930
Pekka Paalanende685b82012-12-04 15:58:12 +02001931 ref->buffer = buffer;
1932 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1933}
1934
1935static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001936weston_surface_attach(struct weston_surface *surface,
1937 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001938{
1939 weston_buffer_reference(&surface->buffer_ref, buffer);
1940
Pekka Paalanena6421c42012-12-04 15:58:10 +02001941 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001942 if (weston_surface_is_mapped(surface))
1943 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001944 }
1945
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001946 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001947
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001948 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001949 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001950}
1951
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001952WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001953weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001954{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001955 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001956
1957 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001958 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001959}
1960
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001961WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001962weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001963{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001964 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001965
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001966 pixman_region32_union(&compositor->primary_plane.damage,
1967 &compositor->primary_plane.damage,
1968 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001969 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001970}
1971
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001972static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001973surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001974{
Pekka Paalanende685b82012-12-04 15:58:12 +02001975 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001976 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001977 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001978
Pekka Paalanenb5026542014-11-12 15:09:24 +02001979 if (weston_timeline_enabled_ &&
1980 pixman_region32_not_empty(&surface->damage))
1981 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1982 TLP_OUTPUT(surface->output), TLP_END);
1983
Jason Ekstrandef540082014-06-26 10:37:36 -07001984 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001985}
1986
1987static void
1988view_accumulate_damage(struct weston_view *view,
1989 pixman_region32_t *opaque)
1990{
1991 pixman_region32_t damage;
1992
1993 pixman_region32_init(&damage);
1994 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001995 pixman_box32_t *extents;
1996
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001998 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001999 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002000 pixman_region32_copy(&damage, &view->surface->damage);
2001 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002002 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002003 }
2004
Pekka Paalanen380adf52015-02-16 14:39:11 +02002005 pixman_region32_intersect(&damage, &damage,
2006 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002007 pixman_region32_subtract(&damage, &damage, opaque);
2008 pixman_region32_union(&view->plane->damage,
2009 &view->plane->damage, &damage);
2010 pixman_region32_fini(&damage);
2011 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002012 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002013}
2014
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002015static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002016compositor_accumulate_damage(struct weston_compositor *ec)
2017{
2018 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002019 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002020 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002021
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002022 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002023
2024 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002025 pixman_region32_copy(&plane->clip, &clip);
2026
2027 pixman_region32_init(&opaque);
2028
Jason Ekstranda7af7042013-10-12 22:38:11 -05002029 wl_list_for_each(ev, &ec->view_list, link) {
2030 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002031 continue;
2032
Jason Ekstranda7af7042013-10-12 22:38:11 -05002033 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002034 }
2035
2036 pixman_region32_union(&clip, &clip, &opaque);
2037 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002038 }
2039
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002040 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002041
Jason Ekstranda7af7042013-10-12 22:38:11 -05002042 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002043 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002044
2045 wl_list_for_each(ev, &ec->view_list, link) {
2046 if (ev->surface->touched)
2047 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002048 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049
2050 surface_flush_damage(ev->surface);
2051
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002052 /* Both the renderer and the backend have seen the buffer
2053 * by now. If renderer needs the buffer, it has its own
2054 * reference set. If the backend wants to keep the buffer
2055 * around for migrating the surface into a non-primary plane
2056 * later, keep_buffer is true. Otherwise, drop the core
2057 * reference now, and allow early buffer release. This enables
2058 * clients to use single-buffering.
2059 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060 if (!ev->surface->keep_buffer)
2061 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002062 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002063}
2064
2065static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002066surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002067{
2068 struct weston_subsurface *sub;
2069
Pekka Paalanene67858b2013-04-25 13:57:42 +03002070 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002071 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002072 continue;
2073
Jason Ekstranda7af7042013-10-12 22:38:11 -05002074 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2075 wl_list_init(&sub->surface->views);
2076
2077 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002078 }
2079}
2080
2081static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002083{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002084 struct weston_subsurface *sub;
2085 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002086
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2088 if (sub->surface == surface)
2089 continue;
2090
George Kiagiadakised04d382014-06-13 18:10:26 +02002091 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2092 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002094 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002095
2096 surface_free_unused_subsurface_views(sub->surface);
2097 }
2098}
2099
2100static void
2101view_list_add_subsurface_view(struct weston_compositor *compositor,
2102 struct weston_subsurface *sub,
2103 struct weston_view *parent)
2104{
2105 struct weston_subsurface *child;
2106 struct weston_view *view = NULL, *iv;
2107
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002108 if (!weston_surface_is_mapped(sub->surface))
2109 return;
2110
Jason Ekstranda7af7042013-10-12 22:38:11 -05002111 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2112 if (iv->geometry.parent == parent) {
2113 view = iv;
2114 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002115 }
2116 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002117
2118 if (view) {
2119 /* Put it back in the surface's list of views */
2120 wl_list_remove(&view->surface_link);
2121 wl_list_insert(&sub->surface->views, &view->surface_link);
2122 } else {
2123 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002124 weston_view_set_position(view,
2125 sub->position.x,
2126 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127 weston_view_set_transform_parent(view, parent);
2128 }
2129
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002130 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002132
Pekka Paalanenb188e912013-11-19 14:03:35 +02002133 if (wl_list_empty(&sub->surface->subsurface_list)) {
2134 wl_list_insert(compositor->view_list.prev, &view->link);
2135 return;
2136 }
2137
2138 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2139 if (child->surface == sub->surface)
2140 wl_list_insert(compositor->view_list.prev, &view->link);
2141 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002142 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002143 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144}
2145
2146static void
2147view_list_add(struct weston_compositor *compositor,
2148 struct weston_view *view)
2149{
2150 struct weston_subsurface *sub;
2151
2152 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002153
Pekka Paalanenb188e912013-11-19 14:03:35 +02002154 if (wl_list_empty(&view->surface->subsurface_list)) {
2155 wl_list_insert(compositor->view_list.prev, &view->link);
2156 return;
2157 }
2158
2159 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2160 if (sub->surface == view->surface)
2161 wl_list_insert(compositor->view_list.prev, &view->link);
2162 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002163 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002164 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002165}
2166
2167static void
2168weston_compositor_build_view_list(struct weston_compositor *compositor)
2169{
2170 struct weston_view *view;
2171 struct weston_layer *layer;
2172
2173 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002174 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002175 surface_stash_subsurface_views(view->surface);
2176
2177 wl_list_init(&compositor->view_list);
2178 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002179 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180 view_list_add(compositor, view);
2181 }
2182 }
2183
2184 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002185 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002186 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002187}
2188
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002189static void
2190weston_output_take_feedback_list(struct weston_output *output,
2191 struct weston_surface *surface)
2192{
2193 struct weston_view *view;
2194 struct weston_presentation_feedback *feedback;
2195 uint32_t flags = 0xffffffff;
2196
2197 if (wl_list_empty(&surface->feedback_list))
2198 return;
2199
2200 /* All views must have the flag for the flag to survive. */
2201 wl_list_for_each(view, &surface->views, surface_link) {
2202 /* ignore views that are not on this output at all */
2203 if (view->output_mask & (1u << output->id))
2204 flags &= view->psf_flags;
2205 }
2206
2207 wl_list_for_each(feedback, &surface->feedback_list, link)
2208 feedback->psf_flags = flags;
2209
2210 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2211 wl_list_init(&surface->feedback_list);
2212}
2213
David Herrmann1edf44c2013-10-22 17:11:26 +02002214static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002215weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002216{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002217 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002218 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002219 struct weston_animation *animation, *next;
2220 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002221 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002222 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002223 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002224
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002225 if (output->destroying)
2226 return 0;
2227
Pekka Paalanenb5026542014-11-12 15:09:24 +02002228 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2229
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002230 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002231 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002232
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002233 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002234 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002235 } else {
2236 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002237 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002238 ev->psf_flags = 0;
2239 }
2240 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002241
Pekka Paalanene67858b2013-04-25 13:57:42 +03002242 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243 wl_list_for_each(ev, &ec->view_list, link) {
2244 /* Note: This operation is safe to do multiple times on the
2245 * same surface.
2246 */
2247 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002248 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002249 &ev->surface->frame_callback_list);
2250 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002251
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002252 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002253 }
2254 }
2255
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002256 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002257
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002258 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002259 pixman_region32_intersect(&output_damage,
2260 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002261 pixman_region32_subtract(&output_damage,
2262 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002263
Scott Moreauccbf29d2012-02-22 14:21:41 -07002264 if (output->dirty)
2265 weston_output_update_matrix(output);
2266
David Herrmann1edf44c2013-10-22 17:11:26 +02002267 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002268
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002269 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002270
Kristian Høgsbergef044142011-06-21 15:02:12 -04002271 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002272
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002273 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002274
Jonas Ådahldb773762012-06-13 00:01:21 +02002275 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002276 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002277 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002278 }
2279
Scott Moreaud64cf212012-06-08 19:40:54 -06002280 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002281 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002282 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002283 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002284
Pekka Paalanenb5026542014-11-12 15:09:24 +02002285 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2286
David Herrmann1edf44c2013-10-22 17:11:26 +02002287 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002288}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002289
Pekka Paalanen82919792014-05-21 13:51:49 +03002290static void
2291weston_output_schedule_repaint_reset(struct weston_output *output)
2292{
Pekka Paalanen82919792014-05-21 13:51:49 +03002293 output->repaint_scheduled = 0;
2294 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002295}
2296
Pekka Paalanen0513a952014-05-21 16:17:27 +03002297static int
2298output_repaint_timer_handler(void *data)
2299{
2300 struct weston_output *output = data;
2301 struct weston_compositor *compositor = output->compositor;
2302
2303 if (output->repaint_needed &&
2304 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2305 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2306 weston_output_repaint(output) == 0)
2307 return 0;
2308
2309 weston_output_schedule_repaint_reset(output);
2310
2311 return 0;
2312}
2313
Kristian Høgsbergef044142011-06-21 15:02:12 -04002314WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002315weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002316 const struct timespec *stamp,
2317 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002318{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002319 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002320 int32_t refresh_nsec;
2321 struct timespec now;
2322 struct timespec gone;
2323 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002324
Pekka Paalanenb5026542014-11-12 15:09:24 +02002325 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2326 TLP_VBLANK(stamp), TLP_END);
2327
Pekka Paalanend7894d02015-07-03 15:08:53 +03002328 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002329 weston_presentation_feedback_present_list(&output->feedback_list,
2330 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002331 output->msc,
2332 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002333
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002334 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002335
Pekka Paalanen0513a952014-05-21 16:17:27 +03002336 weston_compositor_read_presentation_clock(compositor, &now);
2337 timespec_sub(&gone, &now, stamp);
2338 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2339 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002340
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002341 if (msec < -1000 || msec > 1000) {
2342 static bool warned;
2343
2344 if (!warned)
2345 weston_log("Warning: computed repaint delay is "
2346 "insane: %d msec\n", msec);
2347 warned = true;
2348
2349 msec = 0;
2350 }
2351
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002352 /* Called from restart_repaint_loop and restart happens already after
2353 * the deadline given by repaint_msec? In that case we delay until
2354 * the deadline of the next frame, to give clients a more predictable
2355 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002356 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002357 msec += refresh_nsec / 1000000;
2358
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002359 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002360 output_repaint_timer_handler(output);
2361 else
2362 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002363}
2364
2365static void
2366idle_repaint(void *data)
2367{
2368 struct weston_output *output = data;
2369
Jonas Ådahle5a12252013-04-05 23:07:11 +02002370 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002371}
2372
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002373WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002374weston_layer_entry_insert(struct weston_layer_entry *list,
2375 struct weston_layer_entry *entry)
2376{
2377 wl_list_insert(&list->link, &entry->link);
2378 entry->layer = list->layer;
2379}
2380
2381WL_EXPORT void
2382weston_layer_entry_remove(struct weston_layer_entry *entry)
2383{
2384 wl_list_remove(&entry->link);
2385 wl_list_init(&entry->link);
2386 entry->layer = NULL;
2387}
2388
2389WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002390weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2391{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002392 wl_list_init(&layer->view_list.link);
2393 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002394 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002395 if (below != NULL)
2396 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002397}
2398
2399WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002400weston_layer_set_mask(struct weston_layer *layer,
2401 int x, int y, int width, int height)
2402{
2403 struct weston_view *view;
2404
2405 layer->mask.x1 = x;
2406 layer->mask.x2 = x + width;
2407 layer->mask.y1 = y;
2408 layer->mask.y2 = y + height;
2409
2410 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2411 weston_view_geometry_dirty(view);
2412 }
2413}
2414
2415WL_EXPORT void
2416weston_layer_set_mask_infinite(struct weston_layer *layer)
2417{
2418 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2419 UINT32_MAX, UINT32_MAX);
2420}
2421
2422WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002423weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002424{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002425 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002426 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002427
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002428 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2429 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002430 return;
2431
Pekka Paalanenb5026542014-11-12 15:09:24 +02002432 if (!output->repaint_needed)
2433 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2434
Kristian Høgsbergef044142011-06-21 15:02:12 -04002435 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002436 output->repaint_needed = 1;
2437 if (output->repaint_scheduled)
2438 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002439
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002440 wl_event_loop_add_idle(loop, idle_repaint, output);
2441 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002442 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002443}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002444
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002445WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002446weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2447{
2448 struct weston_output *output;
2449
2450 wl_list_for_each(output, &compositor->output_list, link)
2451 weston_output_schedule_repaint(output);
2452}
2453
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002454static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002455surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002456{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002457 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002458}
2459
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002460static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002461surface_attach(struct wl_client *client,
2462 struct wl_resource *resource,
2463 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2464{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002465 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002466 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002467
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002468 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002469 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002470 if (buffer == NULL) {
2471 wl_client_post_no_memory(client);
2472 return;
2473 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002474 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002475
Pekka Paalanende685b82012-12-04 15:58:12 +02002476 /* Attach, attach, without commit in between does not send
2477 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002478 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002479
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002480 surface->pending.sx = sx;
2481 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002482 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002483}
2484
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002485static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002486surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002487 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002488 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002489{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002490 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002491
Derek Foreman57e92ed2015-11-17 14:11:35 -06002492 if (width <= 0 || height <= 0)
2493 return;
2494
Derek Foreman152254b2015-11-26 14:17:48 -06002495 pixman_region32_union_rect(&surface->pending.damage_surface,
2496 &surface->pending.damage_surface,
2497 x, y, width, height);
2498}
2499
2500static void
2501surface_damage_buffer(struct wl_client *client,
2502 struct wl_resource *resource,
2503 int32_t x, int32_t y, int32_t width, int32_t height)
2504{
2505 struct weston_surface *surface = wl_resource_get_user_data(resource);
2506
2507 if (width <= 0 || height <= 0)
2508 return;
2509
2510 pixman_region32_union_rect(&surface->pending.damage_buffer,
2511 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002512 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002513}
2514
Kristian Høgsberg33418202011-08-16 23:01:28 -04002515static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002516destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002517{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002518 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002519
2520 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002521 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002522}
2523
2524static void
2525surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002526 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002527{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002528 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002529 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002530
2531 cb = malloc(sizeof *cb);
2532 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002533 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002534 return;
2535 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002536
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002537 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2538 callback);
2539 if (cb->resource == NULL) {
2540 free(cb);
2541 wl_resource_post_no_memory(resource);
2542 return;
2543 }
2544
Jason Ekstranda85118c2013-06-27 20:17:02 -05002545 wl_resource_set_implementation(cb->resource, NULL, cb,
2546 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002547
Pekka Paalanenbc106382012-10-10 12:49:31 +03002548 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002549}
2550
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002551static void
2552surface_set_opaque_region(struct wl_client *client,
2553 struct wl_resource *resource,
2554 struct wl_resource *region_resource)
2555{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002556 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002557 struct weston_region *region;
2558
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002559 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002560 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002561 pixman_region32_copy(&surface->pending.opaque,
2562 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002563 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002564 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002565 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002566}
2567
2568static void
2569surface_set_input_region(struct wl_client *client,
2570 struct wl_resource *resource,
2571 struct wl_resource *region_resource)
2572{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002573 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002574 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002575
2576 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002577 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002578 pixman_region32_copy(&surface->pending.input,
2579 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002580 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002581 pixman_region32_fini(&surface->pending.input);
2582 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002583 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002584}
2585
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002586static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002587weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002588{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002589 struct weston_subsurface *sub;
2590
2591 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2592 parent_link_pending) {
2593 wl_list_remove(&sub->parent_link);
2594 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2595 }
2596}
2597
2598static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002599weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002600 struct weston_matrix *matrix)
2601{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002602 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002603 double src_width, src_height, dest_width, dest_height;
2604
2605 weston_matrix_init(matrix);
2606
2607 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2608 src_width = surface->width_from_buffer;
2609 src_height = surface->height_from_buffer;
2610 } else {
2611 src_width = wl_fixed_to_double(vp->buffer.src_width);
2612 src_height = wl_fixed_to_double(vp->buffer.src_height);
2613 }
2614
2615 if (vp->surface.width == -1) {
2616 dest_width = src_width;
2617 dest_height = src_height;
2618 } else {
2619 dest_width = vp->surface.width;
2620 dest_height = vp->surface.height;
2621 }
2622
2623 if (src_width != dest_width || src_height != dest_height)
2624 weston_matrix_scale(matrix,
2625 src_width / dest_width,
2626 src_height / dest_height, 1);
2627
2628 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2629 weston_matrix_translate(matrix,
2630 wl_fixed_to_double(vp->buffer.src_x),
2631 wl_fixed_to_double(vp->buffer.src_y),
2632 0);
2633
2634 switch (vp->buffer.transform) {
2635 case WL_OUTPUT_TRANSFORM_FLIPPED:
2636 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2637 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2638 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2639 weston_matrix_scale(matrix, -1, 1, 1);
2640 weston_matrix_translate(matrix,
2641 surface->width_from_buffer, 0, 0);
2642 break;
2643 }
2644
2645 switch (vp->buffer.transform) {
2646 default:
2647 case WL_OUTPUT_TRANSFORM_NORMAL:
2648 case WL_OUTPUT_TRANSFORM_FLIPPED:
2649 break;
2650 case WL_OUTPUT_TRANSFORM_90:
2651 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2652 weston_matrix_rotate_xy(matrix, 0, 1);
2653 weston_matrix_translate(matrix,
2654 surface->height_from_buffer, 0, 0);
2655 break;
2656 case WL_OUTPUT_TRANSFORM_180:
2657 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2658 weston_matrix_rotate_xy(matrix, -1, 0);
2659 weston_matrix_translate(matrix,
2660 surface->width_from_buffer,
2661 surface->height_from_buffer, 0);
2662 break;
2663 case WL_OUTPUT_TRANSFORM_270:
2664 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2665 weston_matrix_rotate_xy(matrix, 0, -1);
2666 weston_matrix_translate(matrix,
2667 0, surface->width_from_buffer, 0);
2668 break;
2669 }
2670
2671 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2672}
2673
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002674/**
2675 * Compute a + b > c while being safe to overflows.
2676 */
2677static bool
2678fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2679{
2680 return (int64_t)a + (int64_t)b > (int64_t)c;
2681}
2682
2683static bool
2684weston_surface_is_pending_viewport_source_valid(
2685 const struct weston_surface *surface)
2686{
2687 const struct weston_surface_state *pend = &surface->pending;
2688 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2689 int width_from_buffer = 0;
2690 int height_from_buffer = 0;
2691 wl_fixed_t w;
2692 wl_fixed_t h;
2693
2694 /* If viewport source rect is not set, it is always ok. */
2695 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2696 return true;
2697
2698 if (pend->newly_attached) {
2699 if (pend->buffer) {
2700 convert_size_by_transform_scale(&width_from_buffer,
2701 &height_from_buffer,
2702 pend->buffer->width,
2703 pend->buffer->height,
2704 vp->buffer.transform,
2705 vp->buffer.scale);
2706 } else {
2707 /* No buffer: viewport is irrelevant. */
2708 return true;
2709 }
2710 } else {
2711 width_from_buffer = surface->width_from_buffer;
2712 height_from_buffer = surface->height_from_buffer;
2713 }
2714
2715 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2716 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2717
2718 /* No buffer: viewport is irrelevant. */
2719 if (width_from_buffer == 0 || height_from_buffer == 0)
2720 return true;
2721
2722 /* overflow checks for wl_fixed_from_int() */
2723 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2724 return false;
2725 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2726 return false;
2727
2728 w = wl_fixed_from_int(width_from_buffer);
2729 h = wl_fixed_from_int(height_from_buffer);
2730
2731 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2732 return false;
2733 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2734 return false;
2735
2736 return true;
2737}
2738
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002739static bool
2740fixed_is_integer(wl_fixed_t v)
2741{
2742 return (v & 0xff) == 0;
2743}
2744
2745static bool
2746weston_surface_is_pending_viewport_dst_size_int(
2747 const struct weston_surface *surface)
2748{
2749 const struct weston_buffer_viewport *vp =
2750 &surface->pending.buffer_viewport;
2751
2752 if (vp->surface.width != -1) {
2753 assert(vp->surface.width > 0 && vp->surface.height > 0);
2754 return true;
2755 }
2756
2757 return fixed_is_integer(vp->buffer.src_width) &&
2758 fixed_is_integer(vp->buffer.src_height);
2759}
2760
Derek Foreman152254b2015-11-26 14:17:48 -06002761/* Translate pending damage in buffer co-ordinates to surface
2762 * co-ordinates and union it with a pixman_region32_t.
2763 * This should only be called after the buffer is attached.
2764 */
2765static void
2766apply_damage_buffer(pixman_region32_t *dest,
2767 struct weston_surface *surface,
2768 struct weston_surface_state *state)
2769{
2770 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2771
2772 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2773 * translated into surface co-ordinates and unioned with
2774 * any other surface damage.
2775 * None of this makes sense if there is no buffer though.
2776 */
2777 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2778 pixman_region32_t buffer_damage;
2779
2780 pixman_region32_intersect_rect(&state->damage_buffer,
2781 &state->damage_buffer,
2782 0, 0, buffer->width,
2783 buffer->height);
2784 pixman_region32_init(&buffer_damage);
2785 weston_matrix_transform_region(&buffer_damage,
2786 &surface->buffer_to_surface_matrix,
2787 &state->damage_buffer);
2788 pixman_region32_union(dest, dest, &buffer_damage);
2789 pixman_region32_fini(&buffer_damage);
2790 }
2791 /* We should clear this on commit even if there was no buffer */
2792 pixman_region32_clear(&state->damage_buffer);
2793}
2794
Jason Ekstrand1e059042014-10-16 10:55:19 -05002795static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002796weston_surface_commit_state(struct weston_surface *surface,
2797 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002798{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002799 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002800 pixman_region32_t opaque;
2801
Alexander Larsson4ea95522013-05-22 14:41:37 +02002802 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002803 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002804 /* wp_viewport.set_source */
2805 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002806 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002807
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002808 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002809 if (state->newly_attached)
2810 weston_surface_attach(surface, state->buffer);
2811 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002812
Jason Ekstrand1e059042014-10-16 10:55:19 -05002813 weston_surface_build_buffer_matrix(surface,
2814 &surface->surface_to_buffer_matrix);
2815 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2816 &surface->surface_to_buffer_matrix);
2817
Jason Ekstrand7b982072014-05-20 14:33:03 -05002818 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002819 weston_surface_update_size(surface);
2820 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002821 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002822 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002823
Jason Ekstrand7b982072014-05-20 14:33:03 -05002824 state->sx = 0;
2825 state->sy = 0;
2826 state->newly_attached = 0;
2827 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002828
Derek Foreman152254b2015-11-26 14:17:48 -06002829 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002830 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002831 (pixman_region32_not_empty(&state->damage_surface) ||
2832 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002833 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002834
Pekka Paalanen8e159182012-10-10 12:49:25 +03002835 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002836 &state->damage_surface);
2837
2838 apply_damage_buffer(&surface->damage, surface, state);
2839
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002840 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002841 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002842 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002843
2844 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002845 pixman_region32_init(&opaque);
2846 pixman_region32_intersect_rect(&opaque, &state->opaque,
2847 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002848
2849 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2850 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002851 wl_list_for_each(view, &surface->views, surface_link)
2852 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002853 }
2854
2855 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002856
2857 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002858 pixman_region32_intersect_rect(&surface->input, &state->input,
2859 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002860
Pekka Paalanenbc106382012-10-10 12:49:31 +03002861 /* wl_surface.frame */
2862 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002863 &state->frame_callback_list);
2864 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002865
2866 /* XXX:
2867 * What should happen with a feedback request, if there
2868 * is no wl_buffer attached for this commit?
2869 */
2870
2871 /* presentation.feedback */
2872 wl_list_insert_list(&surface->feedback_list,
2873 &state->feedback_list);
2874 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002875}
2876
2877static void
2878weston_surface_commit(struct weston_surface *surface)
2879{
2880 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002881
Pekka Paalanene67858b2013-04-25 13:57:42 +03002882 weston_surface_commit_subsurface_order(surface);
2883
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002884 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002885}
2886
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002887static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002888weston_subsurface_commit(struct weston_subsurface *sub);
2889
2890static void
2891weston_subsurface_parent_commit(struct weston_subsurface *sub,
2892 int parent_is_synchronized);
2893
2894static void
2895surface_commit(struct wl_client *client, struct wl_resource *resource)
2896{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002897 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002898 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2899
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002900 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
2901 assert(surface->viewport_resource);
2902
2903 wl_resource_post_error(surface->viewport_resource,
2904 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
2905 "wl_surface@%d has viewport source outside buffer",
2906 wl_resource_get_id(resource));
2907 return;
2908 }
2909
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002910 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
2911 assert(surface->viewport_resource);
2912
2913 wl_resource_post_error(surface->viewport_resource,
2914 WP_VIEWPORT_ERROR_BAD_SIZE,
2915 "wl_surface@%d viewport dst size not integer",
2916 wl_resource_get_id(resource));
2917 return;
2918 }
2919
Pekka Paalanene67858b2013-04-25 13:57:42 +03002920 if (sub) {
2921 weston_subsurface_commit(sub);
2922 return;
2923 }
2924
2925 weston_surface_commit(surface);
2926
2927 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2928 if (sub->surface != surface)
2929 weston_subsurface_parent_commit(sub, 0);
2930 }
2931}
2932
2933static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002934surface_set_buffer_transform(struct wl_client *client,
2935 struct wl_resource *resource, int transform)
2936{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002937 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002938
Jonny Lamba55f1392014-05-30 12:07:15 +02002939 /* if wl_output.transform grows more members this will need to be updated. */
2940 if (transform < 0 ||
2941 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2942 wl_resource_post_error(resource,
2943 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2944 "buffer transform must be a valid transform "
2945 "('%d' specified)", transform);
2946 return;
2947 }
2948
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002949 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002950 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002951}
2952
Alexander Larsson4ea95522013-05-22 14:41:37 +02002953static void
2954surface_set_buffer_scale(struct wl_client *client,
2955 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002956 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002957{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002958 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002959
Jonny Lamba55f1392014-05-30 12:07:15 +02002960 if (scale < 1) {
2961 wl_resource_post_error(resource,
2962 WL_SURFACE_ERROR_INVALID_SCALE,
2963 "buffer scale must be at least one "
2964 "('%d' specified)", scale);
2965 return;
2966 }
2967
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002968 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002969 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002970}
2971
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002972static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002973 surface_destroy,
2974 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002975 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002976 surface_frame,
2977 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002978 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002979 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002980 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06002981 surface_set_buffer_scale,
2982 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002983};
2984
2985static void
2986compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002987 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002988{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002989 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002990 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002991
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002992 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002993 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002994 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002995 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002996 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002997
Jason Ekstranda85118c2013-06-27 20:17:02 -05002998 surface->resource =
2999 wl_resource_create(client, &wl_surface_interface,
3000 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003001 if (surface->resource == NULL) {
3002 weston_surface_destroy(surface);
3003 wl_resource_post_no_memory(resource);
3004 return;
3005 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003006 wl_resource_set_implementation(surface->resource, &surface_interface,
3007 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003008
3009 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003010}
3011
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003012static void
3013destroy_region(struct wl_resource *resource)
3014{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003015 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003016
3017 pixman_region32_fini(&region->region);
3018 free(region);
3019}
3020
3021static void
3022region_destroy(struct wl_client *client, struct wl_resource *resource)
3023{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003024 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003025}
3026
3027static void
3028region_add(struct wl_client *client, struct wl_resource *resource,
3029 int32_t x, int32_t y, int32_t width, int32_t height)
3030{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003031 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003032
3033 pixman_region32_union_rect(&region->region, &region->region,
3034 x, y, width, height);
3035}
3036
3037static void
3038region_subtract(struct wl_client *client, struct wl_resource *resource,
3039 int32_t x, int32_t y, int32_t width, int32_t height)
3040{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003041 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003042 pixman_region32_t rect;
3043
3044 pixman_region32_init_rect(&rect, x, y, width, height);
3045 pixman_region32_subtract(&region->region, &region->region, &rect);
3046 pixman_region32_fini(&rect);
3047}
3048
3049static const struct wl_region_interface region_interface = {
3050 region_destroy,
3051 region_add,
3052 region_subtract
3053};
3054
3055static void
3056compositor_create_region(struct wl_client *client,
3057 struct wl_resource *resource, uint32_t id)
3058{
3059 struct weston_region *region;
3060
3061 region = malloc(sizeof *region);
3062 if (region == NULL) {
3063 wl_resource_post_no_memory(resource);
3064 return;
3065 }
3066
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003067 pixman_region32_init(&region->region);
3068
Jason Ekstranda85118c2013-06-27 20:17:02 -05003069 region->resource =
3070 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003071 if (region->resource == NULL) {
3072 free(region);
3073 wl_resource_post_no_memory(resource);
3074 return;
3075 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003076 wl_resource_set_implementation(region->resource, &region_interface,
3077 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003078}
3079
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003080static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003081 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003082 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003083};
3084
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003085static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003086weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3087{
3088 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003089
Jason Ekstrand7b982072014-05-20 14:33:03 -05003090 weston_surface_commit_state(surface, &sub->cached);
3091 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003092
3093 weston_surface_commit_subsurface_order(surface);
3094
3095 weston_surface_schedule_repaint(surface);
3096
Jason Ekstrand7b982072014-05-20 14:33:03 -05003097 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003098}
3099
3100static void
3101weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3102{
3103 struct weston_surface *surface = sub->surface;
3104
3105 /*
3106 * If this commit would cause the surface to move by the
3107 * attach(dx, dy) parameters, the old damage region must be
3108 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003109 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003110 */
Derek Foreman152254b2015-11-26 14:17:48 -06003111 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003112 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003113 pixman_region32_union(&sub->cached.damage_surface,
3114 &sub->cached.damage_surface,
3115 &surface->pending.damage_surface);
3116 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003117
3118 if (surface->pending.newly_attached) {
3119 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003120 weston_surface_state_set_buffer(&sub->cached,
3121 surface->pending.buffer);
3122 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003123 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003124 weston_presentation_feedback_discard_list(
3125 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003126 }
3127 sub->cached.sx += surface->pending.sx;
3128 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003129
Derek Foreman152254b2015-11-26 14:17:48 -06003130 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3131
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003132 sub->cached.buffer_viewport.changed |=
3133 surface->pending.buffer_viewport.changed;
3134 sub->cached.buffer_viewport.buffer =
3135 surface->pending.buffer_viewport.buffer;
3136 sub->cached.buffer_viewport.surface =
3137 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003138
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003139 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003140
3141 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3142
3143 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3144
3145 wl_list_insert_list(&sub->cached.frame_callback_list,
3146 &surface->pending.frame_callback_list);
3147 wl_list_init(&surface->pending.frame_callback_list);
3148
Pekka Paalanen133e4392014-09-23 22:08:46 -04003149 wl_list_insert_list(&sub->cached.feedback_list,
3150 &surface->pending.feedback_list);
3151 wl_list_init(&surface->pending.feedback_list);
3152
Jason Ekstrand7b982072014-05-20 14:33:03 -05003153 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003154}
3155
Derek Foreman280e7dd2014-10-03 13:13:42 -05003156static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003157weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3158{
3159 while (sub) {
3160 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003161 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003162
3163 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003164 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003165
3166 sub = weston_surface_to_subsurface(sub->parent);
3167 }
3168
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003169 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003170}
3171
3172static void
3173weston_subsurface_commit(struct weston_subsurface *sub)
3174{
3175 struct weston_surface *surface = sub->surface;
3176 struct weston_subsurface *tmp;
3177
3178 /* Recursive check for effectively synchronized. */
3179 if (weston_subsurface_is_synchronized(sub)) {
3180 weston_subsurface_commit_to_cache(sub);
3181 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003182 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003183 /* flush accumulated state from cache */
3184 weston_subsurface_commit_to_cache(sub);
3185 weston_subsurface_commit_from_cache(sub);
3186 } else {
3187 weston_surface_commit(surface);
3188 }
3189
3190 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3191 if (tmp->surface != surface)
3192 weston_subsurface_parent_commit(tmp, 0);
3193 }
3194 }
3195}
3196
3197static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003198weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003199{
3200 struct weston_surface *surface = sub->surface;
3201 struct weston_subsurface *tmp;
3202
Pekka Paalanene67858b2013-04-25 13:57:42 +03003203 /* From now on, commit_from_cache the whole sub-tree, regardless of
3204 * the synchronized mode of each child. This sub-surface or some
3205 * of its ancestors were synchronized, so we are synchronized
3206 * all the way down.
3207 */
3208
Jason Ekstrand7b982072014-05-20 14:33:03 -05003209 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003210 weston_subsurface_commit_from_cache(sub);
3211
3212 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3213 if (tmp->surface != surface)
3214 weston_subsurface_parent_commit(tmp, 1);
3215 }
3216}
3217
3218static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003219weston_subsurface_parent_commit(struct weston_subsurface *sub,
3220 int parent_is_synchronized)
3221{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003222 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003223 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003224 wl_list_for_each(view, &sub->surface->views, surface_link)
3225 weston_view_set_position(view,
3226 sub->position.x,
3227 sub->position.y);
3228
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003229 sub->position.set = 0;
3230 }
3231
3232 if (parent_is_synchronized || sub->synchronized)
3233 weston_subsurface_synchronized_commit(sub);
3234}
3235
Pekka Paalanen8274d902014-08-06 19:36:51 +03003236static int
3237subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3238{
3239 return snprintf(buf, len, "sub-surface");
3240}
3241
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003242static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003243subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003244{
3245 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003246 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003247
Jason Ekstranda7af7042013-10-12 22:38:11 -05003248 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003249 weston_view_set_position(view,
3250 view->geometry.x + dx,
3251 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003252
3253 /* No need to check parent mappedness, because if parent is not
3254 * mapped, parent is not in a visible layer, so this sub-surface
3255 * will not be drawn either.
3256 */
3257 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003258 struct weston_output *output;
3259
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003260 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003261 * because that would call it also for the parent surface,
3262 * which might not be mapped yet. That would lead to
3263 * inconsistent state, where the window could never be
3264 * mapped.
3265 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003266 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003267 * weston_surface_is_mapped() return true, so that when the
3268 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003269 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003270 */
3271 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003272 output = container_of(compositor->output_list.next,
3273 struct weston_output, link);
3274
3275 surface->output = output;
Bryce Harrington89324ce2015-12-23 18:38:07 -08003276 weston_surface_update_output_mask(surface, 1u << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003277 }
3278}
3279
3280static struct weston_subsurface *
3281weston_surface_to_subsurface(struct weston_surface *surface)
3282{
3283 if (surface->configure == subsurface_configure)
3284 return surface->configure_private;
3285
3286 return NULL;
3287}
3288
Pekka Paalanen01388e22013-04-25 13:57:44 +03003289WL_EXPORT struct weston_surface *
3290weston_surface_get_main_surface(struct weston_surface *surface)
3291{
3292 struct weston_subsurface *sub;
3293
3294 while (surface && (sub = weston_surface_to_subsurface(surface)))
3295 surface = sub->parent;
3296
3297 return surface;
3298}
3299
Pekka Paalanen50b67472014-10-01 15:02:41 +03003300WL_EXPORT int
3301weston_surface_set_role(struct weston_surface *surface,
3302 const char *role_name,
3303 struct wl_resource *error_resource,
3304 uint32_t error_code)
3305{
3306 assert(role_name);
3307
3308 if (surface->role_name == NULL ||
3309 surface->role_name == role_name ||
3310 strcmp(surface->role_name, role_name) == 0) {
3311 surface->role_name = role_name;
3312
3313 return 0;
3314 }
3315
3316 wl_resource_post_error(error_resource, error_code,
3317 "Cannot assign role %s to wl_surface@%d,"
3318 " already has role %s\n",
3319 role_name,
3320 wl_resource_get_id(surface->resource),
3321 surface->role_name);
3322 return -1;
3323}
3324
Pekka Paalanen8274d902014-08-06 19:36:51 +03003325WL_EXPORT void
3326weston_surface_set_label_func(struct weston_surface *surface,
3327 int (*desc)(struct weston_surface *,
3328 char *, size_t))
3329{
3330 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003331 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003332}
3333
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003334/** Get the size of surface contents
3335 *
3336 * \param surface The surface to query.
3337 * \param width Returns the width of raw contents.
3338 * \param height Returns the height of raw contents.
3339 *
3340 * Retrieves the raw surface content size in pixels for the given surface.
3341 * This is the whole content size in buffer pixels. If the surface
3342 * has no content or the renderer does not implement this feature,
3343 * zeroes are returned.
3344 *
3345 * This function is used to determine the buffer size needed for
3346 * a weston_surface_copy_content() call.
3347 */
3348WL_EXPORT void
3349weston_surface_get_content_size(struct weston_surface *surface,
3350 int *width, int *height)
3351{
3352 struct weston_renderer *rer = surface->compositor->renderer;
3353
3354 if (!rer->surface_get_content_size) {
3355 *width = 0;
3356 *height = 0;
3357 return;
3358 }
3359
3360 rer->surface_get_content_size(surface, width, height);
3361}
3362
3363/** Copy surface contents to system memory.
3364 *
3365 * \param surface The surface to copy from.
3366 * \param target Pointer to the target memory buffer.
3367 * \param size Size of the target buffer in bytes.
3368 * \param src_x X location on contents to copy from.
3369 * \param src_y Y location on contents to copy from.
3370 * \param width Width in pixels of the area to copy.
3371 * \param height Height in pixels of the area to copy.
3372 * \return 0 for success, -1 for failure.
3373 *
3374 * Surface contents are maintained by the renderer. They can be in a
3375 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3376 * else.
3377 *
3378 * Surface contents are copied into memory pointed to by target,
3379 * which has size bytes of space available. The target memory
3380 * may be larger than needed, but being smaller returns an error.
3381 * The extra bytes in target may or may not be written; their content is
3382 * unspecified. Size must be large enough to hold the image.
3383 *
3384 * The image in the target memory will be arranged in rows from
3385 * top to bottom, and pixels on a row from left to right. The pixel
3386 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3387 * width * 4.
3388 *
3389 * Parameters src_x and src_y define the upper-left corner in buffer
3390 * coordinates (pixels) to copy from. Parameters width and height
3391 * define the size of the area to copy in pixels.
3392 *
3393 * The rectangle defined by src_x, src_y, width, height must fit in
3394 * the surface contents. Otherwise an error is returned.
3395 *
3396 * Use surface_get_data_size to determine the content size; the
3397 * needed target buffer size and rectangle limits.
3398 *
3399 * CURRENT IMPLEMENTATION RESTRICTIONS:
3400 * - the machine must be little-endian due to Pixman formats.
3401 *
3402 * NOTE: Pixman formats are premultiplied.
3403 */
3404WL_EXPORT int
3405weston_surface_copy_content(struct weston_surface *surface,
3406 void *target, size_t size,
3407 int src_x, int src_y,
3408 int width, int height)
3409{
3410 struct weston_renderer *rer = surface->compositor->renderer;
3411 int cw, ch;
3412 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3413
3414 if (!rer->surface_copy_content)
3415 return -1;
3416
3417 weston_surface_get_content_size(surface, &cw, &ch);
3418
3419 if (src_x < 0 || src_y < 0)
3420 return -1;
3421
3422 if (width <= 0 || height <= 0)
3423 return -1;
3424
3425 if (src_x + width > cw || src_y + height > ch)
3426 return -1;
3427
3428 if (width * bytespp * height > size)
3429 return -1;
3430
3431 return rer->surface_copy_content(surface, target, size,
3432 src_x, src_y, width, height);
3433}
3434
Pekka Paalanene67858b2013-04-25 13:57:42 +03003435static void
3436subsurface_set_position(struct wl_client *client,
3437 struct wl_resource *resource, int32_t x, int32_t y)
3438{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003439 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003440
3441 if (!sub)
3442 return;
3443
3444 sub->position.x = x;
3445 sub->position.y = y;
3446 sub->position.set = 1;
3447}
3448
3449static struct weston_subsurface *
3450subsurface_from_surface(struct weston_surface *surface)
3451{
3452 struct weston_subsurface *sub;
3453
3454 sub = weston_surface_to_subsurface(surface);
3455 if (sub)
3456 return sub;
3457
3458 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3459 if (sub->surface == surface)
3460 return sub;
3461
3462 return NULL;
3463}
3464
3465static struct weston_subsurface *
3466subsurface_sibling_check(struct weston_subsurface *sub,
3467 struct weston_surface *surface,
3468 const char *request)
3469{
3470 struct weston_subsurface *sibling;
3471
3472 sibling = subsurface_from_surface(surface);
3473
3474 if (!sibling) {
3475 wl_resource_post_error(sub->resource,
3476 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3477 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003478 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003479 return NULL;
3480 }
3481
3482 if (sibling->parent != sub->parent) {
3483 wl_resource_post_error(sub->resource,
3484 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3485 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003486 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003487 return NULL;
3488 }
3489
3490 return sibling;
3491}
3492
3493static void
3494subsurface_place_above(struct wl_client *client,
3495 struct wl_resource *resource,
3496 struct wl_resource *sibling_resource)
3497{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003498 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003499 struct weston_surface *surface =
3500 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003501 struct weston_subsurface *sibling;
3502
3503 if (!sub)
3504 return;
3505
3506 sibling = subsurface_sibling_check(sub, surface, "place_above");
3507 if (!sibling)
3508 return;
3509
3510 wl_list_remove(&sub->parent_link_pending);
3511 wl_list_insert(sibling->parent_link_pending.prev,
3512 &sub->parent_link_pending);
3513}
3514
3515static void
3516subsurface_place_below(struct wl_client *client,
3517 struct wl_resource *resource,
3518 struct wl_resource *sibling_resource)
3519{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003520 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003521 struct weston_surface *surface =
3522 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003523 struct weston_subsurface *sibling;
3524
3525 if (!sub)
3526 return;
3527
3528 sibling = subsurface_sibling_check(sub, surface, "place_below");
3529 if (!sibling)
3530 return;
3531
3532 wl_list_remove(&sub->parent_link_pending);
3533 wl_list_insert(&sibling->parent_link_pending,
3534 &sub->parent_link_pending);
3535}
3536
3537static void
3538subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3539{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003540 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003541
3542 if (sub)
3543 sub->synchronized = 1;
3544}
3545
3546static void
3547subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3548{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003549 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003550
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003551 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003552 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003553
3554 /* If sub became effectively desynchronized, flush. */
3555 if (!weston_subsurface_is_synchronized(sub))
3556 weston_subsurface_synchronized_commit(sub);
3557 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003558}
3559
3560static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003561weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3562{
3563 wl_list_remove(&sub->parent_link);
3564 wl_list_remove(&sub->parent_link_pending);
3565 wl_list_remove(&sub->parent_destroy_listener.link);
3566 sub->parent = NULL;
3567}
3568
3569static void
3570weston_subsurface_destroy(struct weston_subsurface *sub);
3571
3572static void
3573subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3574{
3575 struct weston_subsurface *sub =
3576 container_of(listener, struct weston_subsurface,
3577 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003578 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003579
3580 /* The protocol object (wl_resource) is left inert. */
3581 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003582 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003583
3584 weston_subsurface_destroy(sub);
3585}
3586
3587static void
3588subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3589{
3590 struct weston_subsurface *sub =
3591 container_of(listener, struct weston_subsurface,
3592 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003593 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003594 assert(sub->surface != sub->parent);
3595
3596 if (weston_surface_is_mapped(sub->surface))
3597 weston_surface_unmap(sub->surface);
3598
3599 weston_subsurface_unlink_parent(sub);
3600}
3601
3602static void
3603subsurface_resource_destroy(struct wl_resource *resource)
3604{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003605 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003606
3607 if (sub)
3608 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003609}
3610
3611static void
3612subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3613{
3614 wl_resource_destroy(resource);
3615}
3616
3617static void
3618weston_subsurface_link_parent(struct weston_subsurface *sub,
3619 struct weston_surface *parent)
3620{
3621 sub->parent = parent;
3622 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003623 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003624 &sub->parent_destroy_listener);
3625
3626 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3627 wl_list_insert(&parent->subsurface_list_pending,
3628 &sub->parent_link_pending);
3629}
3630
3631static void
3632weston_subsurface_link_surface(struct weston_subsurface *sub,
3633 struct weston_surface *surface)
3634{
3635 sub->surface = surface;
3636 sub->surface_destroy_listener.notify =
3637 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003638 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003639 &sub->surface_destroy_listener);
3640}
3641
3642static void
3643weston_subsurface_destroy(struct weston_subsurface *sub)
3644{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003645 struct weston_view *view, *next;
3646
Pekka Paalanene67858b2013-04-25 13:57:42 +03003647 assert(sub->surface);
3648
3649 if (sub->resource) {
3650 assert(weston_surface_to_subsurface(sub->surface) == sub);
3651 assert(sub->parent_destroy_listener.notify ==
3652 subsurface_handle_parent_destroy);
3653
George Kiagiadakised04d382014-06-13 18:10:26 +02003654 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3655 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003656 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003657 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003658
Pekka Paalanene67858b2013-04-25 13:57:42 +03003659 if (sub->parent)
3660 weston_subsurface_unlink_parent(sub);
3661
Jason Ekstrand7b982072014-05-20 14:33:03 -05003662 weston_surface_state_fini(&sub->cached);
3663 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003664
3665 sub->surface->configure = NULL;
3666 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003667 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003668 } else {
3669 /* the dummy weston_subsurface for the parent itself */
3670 assert(sub->parent_destroy_listener.notify == NULL);
3671 wl_list_remove(&sub->parent_link);
3672 wl_list_remove(&sub->parent_link_pending);
3673 }
3674
3675 wl_list_remove(&sub->surface_destroy_listener.link);
3676 free(sub);
3677}
3678
3679static const struct wl_subsurface_interface subsurface_implementation = {
3680 subsurface_destroy,
3681 subsurface_set_position,
3682 subsurface_place_above,
3683 subsurface_place_below,
3684 subsurface_set_sync,
3685 subsurface_set_desync
3686};
3687
3688static struct weston_subsurface *
3689weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3690 struct weston_surface *parent)
3691{
3692 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003693 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003694
Bryce Harringtonde16d892014-11-20 22:21:57 -08003695 sub = zalloc(sizeof *sub);
3696 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003697 return NULL;
3698
Jason Ekstranda7af7042013-10-12 22:38:11 -05003699 wl_list_init(&sub->unused_views);
3700
Jason Ekstranda85118c2013-06-27 20:17:02 -05003701 sub->resource =
3702 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003703 if (!sub->resource) {
3704 free(sub);
3705 return NULL;
3706 }
3707
Jason Ekstranda85118c2013-06-27 20:17:02 -05003708 wl_resource_set_implementation(sub->resource,
3709 &subsurface_implementation,
3710 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003711 weston_subsurface_link_surface(sub, surface);
3712 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003713 weston_surface_state_init(&sub->cached);
3714 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003715 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003716
3717 return sub;
3718}
3719
3720/* Create a dummy subsurface for having the parent itself in its
3721 * sub-surface lists. Makes stacking order manipulation easy.
3722 */
3723static struct weston_subsurface *
3724weston_subsurface_create_for_parent(struct weston_surface *parent)
3725{
3726 struct weston_subsurface *sub;
3727
Bryce Harringtonde16d892014-11-20 22:21:57 -08003728 sub = zalloc(sizeof *sub);
3729 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003730 return NULL;
3731
3732 weston_subsurface_link_surface(sub, parent);
3733 sub->parent = parent;
3734 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3735 wl_list_insert(&parent->subsurface_list_pending,
3736 &sub->parent_link_pending);
3737
3738 return sub;
3739}
3740
3741static void
3742subcompositor_get_subsurface(struct wl_client *client,
3743 struct wl_resource *resource,
3744 uint32_t id,
3745 struct wl_resource *surface_resource,
3746 struct wl_resource *parent_resource)
3747{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003748 struct weston_surface *surface =
3749 wl_resource_get_user_data(surface_resource);
3750 struct weston_surface *parent =
3751 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003752 struct weston_subsurface *sub;
3753 static const char where[] = "get_subsurface: wl_subsurface@";
3754
3755 if (surface == parent) {
3756 wl_resource_post_error(resource,
3757 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3758 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003759 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003760 return;
3761 }
3762
3763 if (weston_surface_to_subsurface(surface)) {
3764 wl_resource_post_error(resource,
3765 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3766 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003767 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003768 return;
3769 }
3770
Pekka Paalanen50b67472014-10-01 15:02:41 +03003771 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3772 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003773 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003774
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003775 if (weston_surface_get_main_surface(parent) == surface) {
3776 wl_resource_post_error(resource,
3777 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3778 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003779 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003780 return;
3781 }
3782
Pekka Paalanene67858b2013-04-25 13:57:42 +03003783 /* make sure the parent is in its own list */
3784 if (wl_list_empty(&parent->subsurface_list)) {
3785 if (!weston_subsurface_create_for_parent(parent)) {
3786 wl_resource_post_no_memory(resource);
3787 return;
3788 }
3789 }
3790
3791 sub = weston_subsurface_create(id, surface, parent);
3792 if (!sub) {
3793 wl_resource_post_no_memory(resource);
3794 return;
3795 }
3796
3797 surface->configure = subsurface_configure;
3798 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003799 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003800}
3801
3802static void
3803subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3804{
3805 wl_resource_destroy(resource);
3806}
3807
3808static const struct wl_subcompositor_interface subcompositor_interface = {
3809 subcompositor_destroy,
3810 subcompositor_get_subsurface
3811};
3812
3813static void
3814bind_subcompositor(struct wl_client *client,
3815 void *data, uint32_t version, uint32_t id)
3816{
3817 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003818 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003819
Jason Ekstranda85118c2013-06-27 20:17:02 -05003820 resource =
3821 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003822 if (resource == NULL) {
3823 wl_client_post_no_memory(client);
3824 return;
3825 }
3826 wl_resource_set_implementation(resource, &subcompositor_interface,
3827 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003828}
3829
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003830/** Set a DPMS mode on all of the compositor's outputs
3831 *
3832 * \param compositor The compositor instance
3833 * \param state The DPMS state the outputs will be set to
3834 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003835static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003836weston_compositor_dpms(struct weston_compositor *compositor,
3837 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003838{
3839 struct weston_output *output;
3840
3841 wl_list_for_each(output, &compositor->output_list, link)
3842 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003843 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003844}
3845
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003846/** Restores the compositor to active status
3847 *
3848 * \param compositor The compositor instance
3849 *
3850 * If the compositor was in a sleeping mode, all outputs are powered
3851 * back on via DPMS. Otherwise if the compositor was inactive
3852 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3853 * signal will fire.
3854 *
3855 * Restarts the idle timer.
3856 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003857WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003858weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003859{
Neil Roberts8b62e202013-09-30 13:14:47 +01003860 uint32_t old_state = compositor->state;
3861
3862 /* The state needs to be changed before emitting the wake
3863 * signal because that may try to schedule a repaint which
3864 * will not work if the compositor is still sleeping */
3865 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3866
3867 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003868 case WESTON_COMPOSITOR_SLEEPING:
3869 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3870 /* fall through */
3871 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003872 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003873 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003874 /* fall through */
3875 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003876 wl_event_source_timer_update(compositor->idle_source,
3877 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003878 }
3879}
3880
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003881/** Turns off rendering and frame events for the compositor.
3882 *
3883 * \param compositor The compositor instance
3884 *
3885 * This is used for example to prevent further rendering while the
3886 * compositor is shutting down.
3887 *
3888 * \note When offscreen state is entered, outputs will be powered
3889 * back on if they were sleeping (in DPMS off mode), even though
3890 * no rendering will be performed.
3891 *
3892 * Stops the idle timer.
3893 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003894WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003895weston_compositor_offscreen(struct weston_compositor *compositor)
3896{
3897 switch (compositor->state) {
3898 case WESTON_COMPOSITOR_OFFSCREEN:
3899 return;
3900 case WESTON_COMPOSITOR_SLEEPING:
3901 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3902 /* fall through */
3903 default:
3904 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3905 wl_event_source_timer_update(compositor->idle_source, 0);
3906 }
3907}
3908
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003909/** Powers down all attached output devices
3910 *
3911 * \param compositor The compositor instance
3912 *
3913 * Causes rendering to the outputs to cease, and no frame events to be
3914 * sent. Only powers down the outputs if the compositor is not already
3915 * in sleep mode.
3916 *
3917 * Stops the idle timer.
3918 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003919WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003920weston_compositor_sleep(struct weston_compositor *compositor)
3921{
3922 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3923 return;
3924
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003925 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003926 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3927 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3928}
3929
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003930/** Sets compositor to idle mode
3931 *
3932 * \param data The compositor instance
3933 *
3934 * This is called when the idle timer fires. Once the compositor is in
3935 * idle mode it requires a wake action (e.g. via
3936 * weston_compositor_wake()) to restore it. The compositor's
3937 * idle_signal will be triggered when the idle event occurs.
3938 *
3939 * Idleness can be inhibited by setting the compositor's idle_inhibit
3940 * property.
3941 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003942static int
3943idle_handler(void *data)
3944{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003945 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003946
3947 if (compositor->idle_inhibit)
3948 return 1;
3949
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003950 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003951 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003952
3953 return 1;
3954}
3955
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003956WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003957weston_plane_init(struct weston_plane *plane,
3958 struct weston_compositor *ec,
3959 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003960{
3961 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003962 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003963 plane->x = x;
3964 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003965 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003966
3967 /* Init the link so that the call to wl_list_remove() when releasing
3968 * the plane without ever stacking doesn't lead to a crash */
3969 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003970}
3971
3972WL_EXPORT void
3973weston_plane_release(struct weston_plane *plane)
3974{
Xiong Zhang97116532013-10-23 13:58:31 +08003975 struct weston_view *view;
3976
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003977 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003978 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003979
Xiong Zhang97116532013-10-23 13:58:31 +08003980 wl_list_for_each(view, &plane->compositor->view_list, link) {
3981 if (view->plane == plane)
3982 view->plane = NULL;
3983 }
3984
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003985 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003986}
3987
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003988WL_EXPORT void
3989weston_compositor_stack_plane(struct weston_compositor *ec,
3990 struct weston_plane *plane,
3991 struct weston_plane *above)
3992{
3993 if (above)
3994 wl_list_insert(above->link.prev, &plane->link);
3995 else
3996 wl_list_insert(&ec->plane_list, &plane->link);
3997}
3998
Casey Dahlin9074db52012-04-19 22:50:09 -04003999static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004000{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004001 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004002}
4003
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004004static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004005bind_output(struct wl_client *client,
4006 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004007{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004008 struct weston_output *output = data;
4009 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004010 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004011
Jason Ekstranda85118c2013-06-27 20:17:02 -05004012 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004013 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004014 if (resource == NULL) {
4015 wl_client_post_no_memory(client);
4016 return;
4017 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004018
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004019 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05004020 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004021
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004022 wl_output_send_geometry(resource,
4023 output->x,
4024 output->y,
4025 output->mm_width,
4026 output->mm_height,
4027 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004028 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004029 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004030 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004031 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004032 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004033
4034 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004035 wl_output_send_mode(resource,
4036 mode->flags,
4037 mode->width,
4038 mode->height,
4039 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004040 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004041
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004042 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004043 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004044}
4045
David Fort0de859e2016-05-27 23:22:57 +02004046/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004047static void
David Fort0de859e2016-05-27 23:22:57 +02004048weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4049 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004050{
4051 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004052 bool start_resizing = false;
4053
4054 if (!delta_width)
4055 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004056
4057 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004058 if (output == resized_output) {
4059 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004060 continue;
4061 }
4062
David Fort0de859e2016-05-27 23:22:57 +02004063 if (start_resizing) {
4064 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004065 output->dirty = 1;
4066 }
4067 }
4068}
4069
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004070WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004071weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004072{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004073 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004074 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004075
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004076 output->destroying = 1;
4077
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004078 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08004079 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004080 weston_view_assign_output(view);
4081 }
4082
Pekka Paalanen0513a952014-05-21 16:17:27 +03004083 wl_event_source_remove(output->repaint_timer);
4084
Pekka Paalanen133e4392014-09-23 22:08:46 -04004085 weston_presentation_feedback_discard_list(&output->feedback_list);
4086
David Fort0de859e2016-05-27 23:22:57 +02004087 weston_compositor_reflow_outputs(output->compositor, output, output->width);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004088 wl_list_remove(&output->link);
4089
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004090 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004091 wl_signal_emit(&output->destroy_signal, output);
4092
Richard Hughesafe690c2013-05-02 10:10:04 +01004093 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004094 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004095 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08004096 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004097
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004098 wl_resource_for_each(resource, &output->resource_list) {
4099 wl_resource_set_destructor(resource, NULL);
4100 }
4101
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004102 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004103}
4104
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004105WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004106weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004107{
Scott Moreau850ca422012-05-21 15:21:25 -06004108 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004109
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004110 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004111 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004112
Scott Moreauccbf29d2012-02-22 14:21:41 -07004113 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004114 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004115 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004116 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004117 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004118 weston_matrix_scale(&output->matrix, magnification,
4119 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004120 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004121
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004122 switch (output->transform) {
4123 case WL_OUTPUT_TRANSFORM_FLIPPED:
4124 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4125 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4126 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4127 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4128 weston_matrix_scale(&output->matrix, -1, 1, 1);
4129 break;
4130 }
4131
4132 switch (output->transform) {
4133 default:
4134 case WL_OUTPUT_TRANSFORM_NORMAL:
4135 case WL_OUTPUT_TRANSFORM_FLIPPED:
4136 break;
4137 case WL_OUTPUT_TRANSFORM_90:
4138 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4139 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4140 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4141 break;
4142 case WL_OUTPUT_TRANSFORM_180:
4143 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4144 weston_matrix_translate(&output->matrix,
4145 -output->width, -output->height, 0);
4146 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4147 break;
4148 case WL_OUTPUT_TRANSFORM_270:
4149 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4150 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4151 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4152 break;
4153 }
4154
4155 if (output->current_scale != 1)
4156 weston_matrix_scale(&output->matrix,
4157 output->current_scale,
4158 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004159
Scott Moreauccbf29d2012-02-22 14:21:41 -07004160 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004161
4162 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004163}
4164
Scott Moreau1bad5db2012-08-18 01:04:05 -06004165static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004166weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004167{
4168 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004169 output->native_scale = scale;
4170 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004171
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004172 convert_size_by_transform_scale(&output->width, &output->height,
4173 output->current_mode->width,
4174 output->current_mode->height,
4175 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004176}
4177
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004178static void
4179weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004180{
4181 output->x = x;
4182 output->y = y;
4183
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004184 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004185 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004186 output->width,
4187 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004188}
4189
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004190WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004191weston_output_move(struct weston_output *output, int x, int y)
4192{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004193 struct wl_resource *resource;
4194
4195 output->move_x = x - output->x;
4196 output->move_y = y - output->y;
4197
4198 if (output->move_x == 0 && output->move_y == 0)
4199 return;
4200
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004201 weston_output_init_geometry(output, x, y);
4202
4203 output->dirty = 1;
4204
4205 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004206 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004207
4208 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004209 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004210 wl_output_send_geometry(resource,
4211 output->x,
4212 output->y,
4213 output->mm_width,
4214 output->mm_height,
4215 output->subpixel,
4216 output->make,
4217 output->model,
4218 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004219
FORT David8a120692016-04-26 23:34:06 +02004220 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004221 wl_output_send_done(resource);
4222 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004223}
4224
Bryce Harrington3f650b82015-12-23 11:01:58 -08004225/** Initialize a weston_output object's parameters
4226 *
4227 * \param output The weston_output object to initialize
4228 * \param c The output's compositor
4229 * \param x x coordinate for the output in global coordinate space
4230 * \param y y coordinate for the output in global coordinate space
4231 * \param mm_width Physical width of the output as reported by the backend
4232 * \param mm_height Physical height of the output as reported by the backend
4233 * \param transform Rotation of the output
4234 * \param scale Native scaling factor for the output
4235 *
4236 * Sets up the transformation, zoom, and geometry of the output using
4237 * the input properties.
4238 *
4239 * Establishes a repaint timer for the output with the relevant display
4240 * object's event loop. See output_repaint_timer_handler().
4241 *
4242 * The output is assigned an ID. Weston can support up to 32 distinct
4243 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4244 * is referred to and used to find the first available ID number, and
4245 * then this ID is marked as used in output_id_pool.
4246 *
4247 * The output is also assigned a Wayland global with the wl_output
4248 * external interface.
4249 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004250WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004251weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004252 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004253 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004254{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004255 struct wl_event_loop *loop;
4256
Bryce Harrington18e45732015-12-23 20:53:53 -08004257 /* Verify we haven't reached the limit of 32 available output IDs */
4258 assert(ffs(~c->output_id_pool) > 0);
4259
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004260 output->compositor = c;
4261 output->x = x;
4262 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004263 output->mm_width = mm_width;
4264 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004265 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004266 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004267
Alexander Larsson0b135062013-05-28 16:23:36 +02004268 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004269 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004270
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004271 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004272 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004273
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004274 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004275 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004276 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004277 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004278 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004279 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004280
Pekka Paalanen0513a952014-05-21 16:17:27 +03004281 loop = wl_display_get_event_loop(c->wl_display);
4282 output->repaint_timer = wl_event_loop_add_timer(loop,
4283 output_repaint_timer_handler, output);
4284
Bryce Harrington3f650b82015-12-23 11:01:58 -08004285 /* Invert the output id pool and look for the lowest numbered
4286 * switch (the least significant bit). Take that bit's position
4287 * as our ID, and mark it used in the compositor's output_id_pool.
4288 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004289 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004290 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004291
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004292 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004293 wl_global_create(c->wl_display, &wl_output_interface, 2,
4294 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004295}
4296
4297/** Adds an output to the compositor's output list and
4298 * send the compositor's output_created signal.
4299 *
4300 * \param compositor The compositor instance.
4301 * \param output The output to be added.
4302 */
4303WL_EXPORT void
4304weston_compositor_add_output(struct weston_compositor *compositor,
4305 struct weston_output *output)
4306{
4307 wl_list_insert(compositor->output_list.prev, &output->link);
4308 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004309}
4310
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004311WL_EXPORT void
4312weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004313 double device_x, double device_y,
4314 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004315{
Derek Foreman0f679412014-10-02 13:41:17 -05004316 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004317 device_x,
4318 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004319 0.0,
4320 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004321
Derek Foreman67a18b92015-03-24 11:36:14 -05004322 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004323
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004324 *x = p.f[0] / p.f[3];
4325 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004326}
4327
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004328static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004329destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004330{
Jonny Lamb74130762013-11-26 18:19:46 +01004331 struct weston_surface *surface =
4332 wl_resource_get_user_data(resource);
4333
Pekka Paalanen4826f872016-04-22 14:14:38 +03004334 if (!surface)
4335 return;
4336
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004337 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004338 surface->pending.buffer_viewport.buffer.src_width =
4339 wl_fixed_from_int(-1);
4340 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004341 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004342}
4343
4344static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004345viewport_destroy(struct wl_client *client,
4346 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004347{
4348 wl_resource_destroy(resource);
4349}
4350
4351static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004352viewport_set_source(struct wl_client *client,
4353 struct wl_resource *resource,
4354 wl_fixed_t src_x,
4355 wl_fixed_t src_y,
4356 wl_fixed_t src_width,
4357 wl_fixed_t src_height)
4358{
4359 struct weston_surface *surface =
4360 wl_resource_get_user_data(resource);
4361
Pekka Paalanen4826f872016-04-22 14:14:38 +03004362 if (!surface) {
4363 wl_resource_post_error(resource,
4364 WP_VIEWPORT_ERROR_NO_SURFACE,
4365 "wl_surface for this viewport is no longer exists");
4366 return;
4367 }
4368
4369 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004370 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004371
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004372 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004373 src_height == wl_fixed_from_int(-1) &&
4374 src_x == wl_fixed_from_int(-1) &&
4375 src_y == wl_fixed_from_int(-1)) {
4376 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004377 surface->pending.buffer_viewport.buffer.src_width =
4378 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004379 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004380 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004381 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004382
Pekka Paalanen201769a2016-04-26 14:42:11 +03004383 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004384 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004385 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03004386 "wl_surface@%d viewport source "
4387 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4388 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004389 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03004390 wl_fixed_to_double(src_height),
4391 wl_fixed_to_double(src_x),
4392 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004393 return;
4394 }
4395
4396 surface->pending.buffer_viewport.buffer.src_x = src_x;
4397 surface->pending.buffer_viewport.buffer.src_y = src_y;
4398 surface->pending.buffer_viewport.buffer.src_width = src_width;
4399 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004400 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004401}
4402
4403static void
4404viewport_set_destination(struct wl_client *client,
4405 struct wl_resource *resource,
4406 int32_t dst_width,
4407 int32_t dst_height)
4408{
4409 struct weston_surface *surface =
4410 wl_resource_get_user_data(resource);
4411
Pekka Paalanen4826f872016-04-22 14:14:38 +03004412 if (!surface) {
4413 wl_resource_post_error(resource,
4414 WP_VIEWPORT_ERROR_NO_SURFACE,
4415 "wl_surface for this viewport no longer exists");
4416 return;
4417 }
4418
4419 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004420
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004421 if (dst_width == -1 && dst_height == -1) {
4422 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004423 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004424 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004425 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004426 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004427
4428 if (dst_width <= 0 || dst_height <= 0) {
4429 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004430 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004431 "destination size must be positive (%dx%d)",
4432 dst_width, dst_height);
4433 return;
4434 }
4435
4436 surface->pending.buffer_viewport.surface.width = dst_width;
4437 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004438 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004439}
4440
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004441static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004442 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004443 viewport_set_source,
4444 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004445};
4446
4447static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004448viewporter_destroy(struct wl_client *client,
4449 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004450{
4451 wl_resource_destroy(resource);
4452}
4453
4454static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004455viewporter_get_viewport(struct wl_client *client,
4456 struct wl_resource *viewporter,
4457 uint32_t id,
4458 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004459{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004460 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004461 struct weston_surface *surface =
4462 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004463 struct wl_resource *resource;
4464
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004465 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004466 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004467 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004468 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004469 return;
4470 }
4471
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004472 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004473 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004474 if (resource == NULL) {
4475 wl_client_post_no_memory(client);
4476 return;
4477 }
4478
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004479 wl_resource_set_implementation(resource, &viewport_interface,
4480 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004481
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004482 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004483}
4484
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004485static const struct wp_viewporter_interface viewporter_interface = {
4486 viewporter_destroy,
4487 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004488};
4489
4490static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004491bind_viewporter(struct wl_client *client,
4492 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004493{
4494 struct wl_resource *resource;
4495
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004496 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004497 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004498 if (resource == NULL) {
4499 wl_client_post_no_memory(client);
4500 return;
4501 }
4502
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004503 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004504 NULL, NULL);
4505}
4506
4507static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004508destroy_presentation_feedback(struct wl_resource *feedback_resource)
4509{
4510 struct weston_presentation_feedback *feedback;
4511
4512 feedback = wl_resource_get_user_data(feedback_resource);
4513
4514 wl_list_remove(&feedback->link);
4515 free(feedback);
4516}
4517
4518static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004519presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4520{
4521 wl_resource_destroy(resource);
4522}
4523
4524static void
4525presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004526 struct wl_resource *presentation_resource,
4527 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004528 uint32_t callback)
4529{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004530 struct weston_surface *surface;
4531 struct weston_presentation_feedback *feedback;
4532
4533 surface = wl_resource_get_user_data(surface_resource);
4534
Bryce Harringtonde16d892014-11-20 22:21:57 -08004535 feedback = zalloc(sizeof *feedback);
4536 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004537 goto err_calloc;
4538
4539 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004540 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004541 1, callback);
4542 if (!feedback->resource)
4543 goto err_create;
4544
4545 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4546 destroy_presentation_feedback);
4547
4548 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4549
4550 return;
4551
4552err_create:
4553 free(feedback);
4554
4555err_calloc:
4556 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004557}
4558
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004559static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004560 presentation_destroy,
4561 presentation_feedback
4562};
4563
4564static void
4565bind_presentation(struct wl_client *client,
4566 void *data, uint32_t version, uint32_t id)
4567{
4568 struct weston_compositor *compositor = data;
4569 struct wl_resource *resource;
4570
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004571 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004572 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004573 if (resource == NULL) {
4574 wl_client_post_no_memory(client);
4575 return;
4576 }
4577
4578 wl_resource_set_implementation(resource, &presentation_implementation,
4579 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004580 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004581}
4582
4583static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004584compositor_bind(struct wl_client *client,
4585 void *data, uint32_t version, uint32_t id)
4586{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004587 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004588 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004589
Jason Ekstranda85118c2013-06-27 20:17:02 -05004590 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004591 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004592 if (resource == NULL) {
4593 wl_client_post_no_memory(client);
4594 return;
4595 }
4596
4597 wl_resource_set_implementation(resource, &compositor_interface,
4598 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004599}
4600
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004601WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004602weston_environment_get_fd(const char *env)
4603{
4604 char *e, *end;
4605 int fd, flags;
4606
4607 e = getenv(env);
4608 if (!e)
4609 return -1;
4610 fd = strtol(e, &end, 0);
4611 if (*end != '\0')
4612 return -1;
4613
4614 flags = fcntl(fd, F_GETFD);
4615 if (flags == -1)
4616 return -1;
4617
4618 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4619 unsetenv(env);
4620
4621 return fd;
4622}
4623
Pekka Paalanenb5026542014-11-12 15:09:24 +02004624static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004625timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004626 uint32_t key, void *data)
4627{
4628 struct weston_compositor *compositor = data;
4629
4630 if (weston_timeline_enabled_)
4631 weston_timeline_close();
4632 else
4633 weston_timeline_open(compositor);
4634}
4635
Giulio Camuffo459137b2014-10-11 23:56:24 +03004636/** Create the compositor.
4637 *
4638 * This functions creates and initializes a compositor instance.
4639 *
4640 * \param display The Wayland display to be used.
4641 * \param user_data A pointer to an object that can later be retrieved
4642 * using the \ref weston_compositor_get_user_data function.
4643 * \return The compositor instance on success or NULL on failure.
4644 */
4645WL_EXPORT struct weston_compositor *
4646weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004647{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004648 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004649 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004650
Giulio Camuffo459137b2014-10-11 23:56:24 +03004651 ec = zalloc(sizeof *ec);
4652 if (!ec)
4653 return NULL;
4654
4655 ec->wl_display = display;
4656 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004657 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004658 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004659 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004660 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004661 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004662 wl_signal_init(&ec->idle_signal);
4663 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004664 wl_signal_init(&ec->show_input_panel_signal);
4665 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004666 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004667 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004668 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004669 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004670 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004671 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004672 wl_signal_init(&ec->session_signal);
4673 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004674
Casey Dahlin58ba1372012-04-19 22:50:08 -04004675 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004676 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004677
Derek Foreman152254b2015-11-26 14:17:48 -06004678 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004679 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004680 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004681
Giulio Camuffo954f1832014-10-11 18:27:30 +03004682 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004683 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004684 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004685
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004686 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004687 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004688 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004689
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004690 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004691 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004692 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004693
Jason Ekstranda7af7042013-10-12 22:38:11 -05004694 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004695 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004696 wl_list_init(&ec->layer_list);
4697 wl_list_init(&ec->seat_list);
4698 wl_list_init(&ec->output_list);
4699 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004700 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004701 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004702 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004703 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004704 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004705
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004706 wl_list_init(&ec->plugin_api_list);
4707
Xiong Zhang97116532013-10-23 13:58:31 +08004708 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004709 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004710
Giulio Camuffo459137b2014-10-11 23:56:24 +03004711 wl_data_device_manager_init(ec->wl_display);
4712
4713 wl_display_init_shm(ec->wl_display);
4714
4715 loop = wl_display_get_event_loop(ec->wl_display);
4716 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004717
Giulio Camuffo459137b2014-10-11 23:56:24 +03004718 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4719 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4720
4721 weston_compositor_add_debug_binding(ec, KEY_T,
4722 timeline_key_binding_handler, ec);
4723
Giulio Camuffo459137b2014-10-11 23:56:24 +03004724 return ec;
4725
4726fail:
4727 free(ec);
4728 return NULL;
4729}
4730
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004731WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004732weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004733{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004734 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004735
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004736 wl_event_source_remove(ec->idle_source);
4737
Matt Roper361d2ad2011-08-29 13:52:23 -07004738 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004739 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004740 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004741
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004742 if (ec->renderer)
4743 ec->renderer->destroy(ec);
4744
Daniel Stone325fc2d2012-05-30 16:31:58 +01004745 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004746 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004747 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004748 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004749 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004750 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004751
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004752 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004753}
4754
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004755WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004756weston_compositor_exit_with_code(struct weston_compositor *compositor,
4757 int exit_code)
4758{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004759 if (compositor->exit_code == EXIT_SUCCESS)
4760 compositor->exit_code = exit_code;
4761
Giulio Camuffo459137b2014-10-11 23:56:24 +03004762 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004763}
4764
4765WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004766weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4767 const struct weston_pointer_grab_interface *interface)
4768{
4769 struct weston_seat *seat;
4770
4771 ec->default_pointer_grab = interface;
4772 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004773 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4774
4775 if (pointer)
4776 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004777 }
4778}
4779
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004780WL_EXPORT int
4781weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4782 clockid_t clk_id)
4783{
4784 struct timespec ts;
4785
4786 if (clock_gettime(clk_id, &ts) < 0)
4787 return -1;
4788
4789 compositor->presentation_clock = clk_id;
4790
4791 return 0;
4792}
4793
4794/*
4795 * For choosing the software clock, when the display hardware or API
4796 * does not expose a compatible presentation timestamp.
4797 */
4798WL_EXPORT int
4799weston_compositor_set_presentation_clock_software(
4800 struct weston_compositor *compositor)
4801{
4802 /* In order of preference */
4803 static const clockid_t clocks[] = {
4804 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4805 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4806 CLOCK_MONOTONIC, /* no jumps, may crawl */
4807 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4808 CLOCK_REALTIME /* may jump and crawl */
4809 };
4810 unsigned i;
4811
4812 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4813 if (weston_compositor_set_presentation_clock(compositor,
4814 clocks[i]) == 0)
4815 return 0;
4816
4817 weston_log("Error: no suitable presentation clock available.\n");
4818
4819 return -1;
4820}
4821
Pekka Paalanen662f3842015-03-18 12:17:26 +02004822/** Read the current time from the Presentation clock
4823 *
4824 * \param compositor
4825 * \param ts[out] The current time.
4826 *
4827 * \note Reading the current time in user space is always imprecise to some
4828 * degree.
4829 *
4830 * This function is never meant to fail. If reading the clock does fail,
4831 * an error message is logged and a zero time is returned. Callers are not
4832 * supposed to detect or react to failures.
4833 */
4834WL_EXPORT void
4835weston_compositor_read_presentation_clock(
4836 const struct weston_compositor *compositor,
4837 struct timespec *ts)
4838{
4839 static bool warned;
4840 int ret;
4841
4842 ret = clock_gettime(compositor->presentation_clock, ts);
4843 if (ret < 0) {
4844 ts->tv_sec = 0;
4845 ts->tv_nsec = 0;
4846
4847 if (!warned)
4848 weston_log("Error: failure to read "
4849 "the presentation clock %#x: '%m' (%d)\n",
4850 compositor->presentation_clock, errno);
4851 warned = true;
4852 }
4853}
4854
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004855/** Import dmabuf buffer into current renderer
4856 *
4857 * \param compositor
4858 * \param buffer the dmabuf buffer to import
4859 * \return true on usable buffers, false otherwise
4860 *
4861 * This function tests that the linux_dmabuf_buffer is usable
4862 * for the current renderer. Returns false on unusable buffers. Usually
4863 * usability is tested by importing the dmabufs for composition.
4864 *
4865 * This hook is also used for detecting if the renderer supports
4866 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4867 * supported.
4868 * */
4869WL_EXPORT bool
4870weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4871 struct linux_dmabuf_buffer *buffer)
4872{
4873 struct weston_renderer *renderer;
4874
4875 renderer = compositor->renderer;
4876
4877 if (renderer->import_dmabuf == NULL)
4878 return false;
4879
4880 return renderer->import_dmabuf(compositor, buffer);
4881}
4882
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004883WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004884weston_version(int *major, int *minor, int *micro)
4885{
4886 *major = WESTON_VERSION_MAJOR;
4887 *minor = WESTON_VERSION_MINOR;
4888 *micro = WESTON_VERSION_MICRO;
4889}
4890
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004891WL_EXPORT void *
4892weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004893{
Derek Foreman3f86e502015-06-08 11:46:54 -05004894 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004895 char path[PATH_MAX];
4896 void *module, *init;
4897
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004898 if (name == NULL)
4899 return NULL;
4900
Derek Foreman3f86e502015-06-08 11:46:54 -05004901 if (name[0] != '/') {
4902 if (builddir)
4903 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4904 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03004905 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004906 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004907 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004908 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004909
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004910 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4911 if (module) {
4912 weston_log("Module '%s' already loaded\n", path);
4913 dlclose(module);
4914 return NULL;
4915 }
4916
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004917 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004918 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004919 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004920 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004921 return NULL;
4922 }
4923
4924 init = dlsym(module, entrypoint);
4925 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004926 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004927 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004928 return NULL;
4929 }
4930
4931 return init;
4932}
4933
Giulio Camuffo459137b2014-10-11 23:56:24 +03004934
4935/** Destroys the compositor.
4936 *
4937 * This function cleans up the compositor state and destroys it.
4938 *
4939 * \param compositor The compositor to be destroyed.
4940 */
4941WL_EXPORT void
4942weston_compositor_destroy(struct weston_compositor *compositor)
4943{
4944 /* prevent further rendering while shutting down */
4945 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4946
4947 wl_signal_emit(&compositor->destroy_signal, compositor);
4948
4949 weston_compositor_xkb_destroy(compositor);
4950
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004951 if (compositor->backend)
4952 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004953
4954 weston_plugin_api_destroy_list(compositor);
4955
Giulio Camuffo459137b2014-10-11 23:56:24 +03004956 free(compositor);
4957}
4958
4959/** Instruct the compositor to exit.
4960 *
4961 * This functions does not directly destroy the compositor object, it merely
4962 * command it to start the tear down process. It is not guaranteed that the
4963 * tear down will happen immediately.
4964 *
4965 * \param compositor The compositor to tear down.
4966 */
4967WL_EXPORT void
4968weston_compositor_exit(struct weston_compositor *compositor)
4969{
4970 compositor->exit(compositor);
4971}
4972
4973/** Return the user data stored in the compositor.
4974 *
4975 * This function returns the user data pointer set with user_data parameter
4976 * to the \ref weston_compositor_create function.
4977 */
4978WL_EXPORT void *
4979weston_compositor_get_user_data(struct weston_compositor *compositor)
4980{
4981 return compositor->user_data;
4982}
Pekka Paalanendd186732016-06-03 14:49:54 +03004983
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004984static const char * const backend_map[] = {
4985 [WESTON_BACKEND_DRM] = "drm-backend.so",
4986 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
4987 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
4988 [WESTON_BACKEND_RDP] = "rdp-backend.so",
4989 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
4990 [WESTON_BACKEND_X11] = "x11-backend.so",
4991};
4992
Pekka Paalanendd186732016-06-03 14:49:54 +03004993/** Load a backend into a weston_compositor
4994 *
4995 * A backend must be loaded to make a weston_compositor work. A backend
4996 * provides input and output capabilities, and determines the renderer to use.
4997 *
4998 * \param compositor A compositor that has not had a backend loaded yet.
4999 * \param backend Name of the backend file.
5000 * \param config_base A pointer to a backend-specific configuration
5001 * structure's 'base' member.
5002 *
5003 * \return 0 on success, or -1 on error.
5004 */
5005WL_EXPORT int
5006weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005007 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005008 struct weston_backend_config *config_base)
5009{
5010 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005011 struct weston_backend_config *config_base);
5012
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005013 if (backend < 0 || backend >= ARRAY_LENGTH(backend_map))
5014 return -1;
5015
5016 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005017 if (!backend_init)
5018 return -1;
5019
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005020 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005021}