blob: b045381d4a5e468e73ffa011b73eb5f0a9cc8450 [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);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800458 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500459
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500460 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200461 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400462
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200463 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
464 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200465 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
466 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500467
468 weston_surface_state_init(&surface->pending);
469
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400470 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500471 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300472 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400473
Jason Ekstranda7af7042013-10-12 22:38:11 -0500474 wl_list_init(&surface->views);
475
476 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400477 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500478
Pekka Paalanene67858b2013-04-25 13:57:42 +0300479 wl_list_init(&surface->subsurface_list);
480 wl_list_init(&surface->subsurface_list_pending);
481
Jason Ekstrand1e059042014-10-16 10:55:19 -0500482 weston_matrix_init(&surface->buffer_to_surface_matrix);
483 weston_matrix_init(&surface->surface_to_buffer_matrix);
484
Jonas Ådahld3414f22016-07-22 17:56:31 +0800485 wl_list_init(&surface->pointer_constraints);
486
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400487 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500488}
489
Alex Wu8811bf92012-02-28 18:07:54 +0800490WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500491weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200492 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500493{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100494 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500495}
496
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400497WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500498weston_view_to_global_float(struct weston_view *view,
499 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200500{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500501 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200502 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
503
Jason Ekstranda7af7042013-10-12 22:38:11 -0500504 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200505
506 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200507 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700508 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200509 v.f[3]);
510 *x = 0;
511 *y = 0;
512 return;
513 }
514
515 *x = v.f[0] / v.f[3];
516 *y = v.f[1] / v.f[3];
517 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500518 *x = sx + view->geometry.x;
519 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200520 }
521}
522
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500523WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200524weston_transformed_coord(int width, int height,
525 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200526 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200527 float sx, float sy, float *bx, float *by)
528{
529 switch (transform) {
530 case WL_OUTPUT_TRANSFORM_NORMAL:
531 default:
532 *bx = sx;
533 *by = sy;
534 break;
535 case WL_OUTPUT_TRANSFORM_FLIPPED:
536 *bx = width - sx;
537 *by = sy;
538 break;
539 case WL_OUTPUT_TRANSFORM_90:
540 *bx = height - sy;
541 *by = sx;
542 break;
543 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
544 *bx = height - sy;
545 *by = width - sx;
546 break;
547 case WL_OUTPUT_TRANSFORM_180:
548 *bx = width - sx;
549 *by = height - sy;
550 break;
551 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
552 *bx = sx;
553 *by = height - sy;
554 break;
555 case WL_OUTPUT_TRANSFORM_270:
556 *bx = sy;
557 *by = width - sx;
558 break;
559 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
560 *bx = sy;
561 *by = sx;
562 break;
563 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200564
565 *bx *= scale;
566 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200567}
568
569WL_EXPORT pixman_box32_t
570weston_transformed_rect(int width, int height,
571 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200572 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200573 pixman_box32_t rect)
574{
575 float x1, x2, y1, y2;
576
577 pixman_box32_t ret;
578
Alexander Larsson4ea95522013-05-22 14:41:37 +0200579 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200580 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200581 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200582 rect.x2, rect.y2, &x2, &y2);
583
584 if (x1 <= x2) {
585 ret.x1 = x1;
586 ret.x2 = x2;
587 } else {
588 ret.x1 = x2;
589 ret.x2 = x1;
590 }
591
592 if (y1 <= y2) {
593 ret.y1 = y1;
594 ret.y2 = y2;
595 } else {
596 ret.y1 = y2;
597 ret.y2 = y1;
598 }
599
600 return ret;
601}
602
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600603/** Transform a region by a matrix, restricted to axis-aligned transformations
604 *
605 * Warning: This function does not work for projective, affine, or matrices
606 * that encode arbitrary rotations. Only 90-degree step rotations are
607 * supported.
608 */
609WL_EXPORT void
610weston_matrix_transform_region(pixman_region32_t *dest,
611 struct weston_matrix *matrix,
612 pixman_region32_t *src)
613{
614 pixman_box32_t *src_rects, *dest_rects;
615 int nrects, i;
616
617 src_rects = pixman_region32_rectangles(src, &nrects);
618 dest_rects = malloc(nrects * sizeof(*dest_rects));
619 if (!dest_rects)
620 return;
621
622 for (i = 0; i < nrects; i++) {
623 struct weston_vector vec1 = {{
624 src_rects[i].x1, src_rects[i].y1, 0, 1
625 }};
626 weston_matrix_transform(matrix, &vec1);
627 vec1.f[0] /= vec1.f[3];
628 vec1.f[1] /= vec1.f[3];
629
630 struct weston_vector vec2 = {{
631 src_rects[i].x2, src_rects[i].y2, 0, 1
632 }};
633 weston_matrix_transform(matrix, &vec2);
634 vec2.f[0] /= vec2.f[3];
635 vec2.f[1] /= vec2.f[3];
636
637 if (vec1.f[0] < vec2.f[0]) {
638 dest_rects[i].x1 = floor(vec1.f[0]);
639 dest_rects[i].x2 = ceil(vec2.f[0]);
640 } else {
641 dest_rects[i].x1 = floor(vec2.f[0]);
642 dest_rects[i].x2 = ceil(vec1.f[0]);
643 }
644
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600645 if (vec1.f[1] < vec2.f[1]) {
646 dest_rects[i].y1 = floor(vec1.f[1]);
647 dest_rects[i].y2 = ceil(vec2.f[1]);
648 } else {
649 dest_rects[i].y1 = floor(vec2.f[1]);
650 dest_rects[i].y2 = ceil(vec1.f[1]);
651 }
652 }
653
654 pixman_region32_clear(dest);
655 pixman_region32_init_rects(dest, dest_rects, nrects);
656 free(dest_rects);
657}
658
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200659WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500660weston_transformed_region(int width, int height,
661 enum wl_output_transform transform,
662 int32_t scale,
663 pixman_region32_t *src, pixman_region32_t *dest)
664{
665 pixman_box32_t *src_rects, *dest_rects;
666 int nrects, i;
667
668 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
669 if (src != dest)
670 pixman_region32_copy(dest, src);
671 return;
672 }
673
674 src_rects = pixman_region32_rectangles(src, &nrects);
675 dest_rects = malloc(nrects * sizeof(*dest_rects));
676 if (!dest_rects)
677 return;
678
679 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
680 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
681 } else {
682 for (i = 0; i < nrects; i++) {
683 switch (transform) {
684 default:
685 case WL_OUTPUT_TRANSFORM_NORMAL:
686 dest_rects[i].x1 = src_rects[i].x1;
687 dest_rects[i].y1 = src_rects[i].y1;
688 dest_rects[i].x2 = src_rects[i].x2;
689 dest_rects[i].y2 = src_rects[i].y2;
690 break;
691 case WL_OUTPUT_TRANSFORM_90:
692 dest_rects[i].x1 = height - src_rects[i].y2;
693 dest_rects[i].y1 = src_rects[i].x1;
694 dest_rects[i].x2 = height - src_rects[i].y1;
695 dest_rects[i].y2 = src_rects[i].x2;
696 break;
697 case WL_OUTPUT_TRANSFORM_180:
698 dest_rects[i].x1 = width - src_rects[i].x2;
699 dest_rects[i].y1 = height - src_rects[i].y2;
700 dest_rects[i].x2 = width - src_rects[i].x1;
701 dest_rects[i].y2 = height - src_rects[i].y1;
702 break;
703 case WL_OUTPUT_TRANSFORM_270:
704 dest_rects[i].x1 = src_rects[i].y1;
705 dest_rects[i].y1 = width - src_rects[i].x2;
706 dest_rects[i].x2 = src_rects[i].y2;
707 dest_rects[i].y2 = width - src_rects[i].x1;
708 break;
709 case WL_OUTPUT_TRANSFORM_FLIPPED:
710 dest_rects[i].x1 = width - src_rects[i].x2;
711 dest_rects[i].y1 = src_rects[i].y1;
712 dest_rects[i].x2 = width - src_rects[i].x1;
713 dest_rects[i].y2 = src_rects[i].y2;
714 break;
715 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
716 dest_rects[i].x1 = height - src_rects[i].y2;
717 dest_rects[i].y1 = width - src_rects[i].x2;
718 dest_rects[i].x2 = height - src_rects[i].y1;
719 dest_rects[i].y2 = width - src_rects[i].x1;
720 break;
721 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
722 dest_rects[i].x1 = src_rects[i].x1;
723 dest_rects[i].y1 = height - src_rects[i].y2;
724 dest_rects[i].x2 = src_rects[i].x2;
725 dest_rects[i].y2 = height - src_rects[i].y1;
726 break;
727 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
728 dest_rects[i].x1 = src_rects[i].y1;
729 dest_rects[i].y1 = src_rects[i].x1;
730 dest_rects[i].x2 = src_rects[i].y2;
731 dest_rects[i].y2 = src_rects[i].x2;
732 break;
733 }
734 }
735 }
736
737 if (scale != 1) {
738 for (i = 0; i < nrects; i++) {
739 dest_rects[i].x1 *= scale;
740 dest_rects[i].x2 *= scale;
741 dest_rects[i].y1 *= scale;
742 dest_rects[i].y2 *= scale;
743 }
744 }
745
746 pixman_region32_clear(dest);
747 pixman_region32_init_rects(dest, dest_rects, nrects);
748 free(dest_rects);
749}
750
Jonny Lamb74130762013-11-26 18:19:46 +0100751static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300752viewport_surface_to_buffer(struct weston_surface *surface,
753 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100754{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200755 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200756 double src_width, src_height;
757 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200758
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200759 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
760 if (vp->surface.width == -1) {
761 *bx = sx;
762 *by = sy;
763 return;
764 }
Jonny Lamb74130762013-11-26 18:19:46 +0100765
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200766 src_x = 0.0;
767 src_y = 0.0;
768 src_width = surface->width_from_buffer;
769 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100770 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200771 src_x = wl_fixed_to_double(vp->buffer.src_x);
772 src_y = wl_fixed_to_double(vp->buffer.src_y);
773 src_width = wl_fixed_to_double(vp->buffer.src_width);
774 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100775 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200776
777 *bx = sx * src_width / surface->width + src_x;
778 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100779}
780
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500781WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200782weston_surface_to_buffer_float(struct weston_surface *surface,
783 float sx, float sy, float *bx, float *by)
784{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200785 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
786
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300787 /* first transform coordinates if the viewport is set */
788 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100789
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500790 weston_transformed_coord(surface->width_from_buffer,
791 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200792 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100793 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200794}
795
Derek Foreman99739672015-12-03 16:38:11 -0600796/** Transform a rectangle from surface coordinates to buffer coordinates
797 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300798 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600799 * from.
800 * \param rect The rectangle to transform.
801 * \return The transformed rectangle.
802 *
803 * Viewport and buffer transformations can only do translation, scaling,
804 * and rotations in 90-degree steps. Therefore the only loss in the
805 * conversion is coordinate rounding.
806 *
807 * However, some coordinate rounding takes place as an intermediate
808 * step before the buffer scale factor is applied, so the rectangle
809 * boundary may not be exactly as expected.
810 *
811 * This is OK for damage tracking since a little extra coverage is
812 * not a problem.
813 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200814WL_EXPORT pixman_box32_t
815weston_surface_to_buffer_rect(struct weston_surface *surface,
816 pixman_box32_t rect)
817{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200818 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100819 float xf, yf;
820
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300821 /* first transform box coordinates if the viewport is set */
822 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100823 rect.x1 = floorf(xf);
824 rect.y1 = floorf(yf);
825
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300826 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600827 rect.x2 = ceilf(xf);
828 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100829
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500830 return weston_transformed_rect(surface->width_from_buffer,
831 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200832 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200833 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200834}
835
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200836/** Transform a region from surface coordinates to buffer coordinates
837 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300838 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200839 * from.
840 * \param surface_region[in] The region in surface coordinates.
841 * \param buffer_region[out] The region converted to buffer coordinates.
842 *
843 * Buffer_region must be init'd, but will be completely overwritten.
844 *
845 * Viewport and buffer transformations can only do translation, scaling,
846 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600847 * conversion is from the coordinate rounding that takes place in
848 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200849 */
850WL_EXPORT void
851weston_surface_to_buffer_region(struct weston_surface *surface,
852 pixman_region32_t *surface_region,
853 pixman_region32_t *buffer_region)
854{
855 pixman_box32_t *src_rects, *dest_rects;
856 int nrects, i;
857
858 src_rects = pixman_region32_rectangles(surface_region, &nrects);
859 dest_rects = malloc(nrects * sizeof(*dest_rects));
860 if (!dest_rects)
861 return;
862
863 for (i = 0; i < nrects; i++) {
864 dest_rects[i] = weston_surface_to_buffer_rect(surface,
865 src_rects[i]);
866 }
867
868 pixman_region32_fini(buffer_region);
869 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
870 free(dest_rects);
871}
872
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200873WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500874weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400875 struct weston_plane *plane)
876{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500877 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400878 return;
879
Jason Ekstranda7af7042013-10-12 22:38:11 -0500880 weston_view_damage_below(view);
881 view->plane = plane;
882 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400883}
884
Pekka Paalanen51723d52015-02-17 13:10:01 +0200885/** Inflict damage on the plane where the view is visible.
886 *
887 * \param view The view that causes the damage.
888 *
889 * If the view is currently on a plane (including the primary plane),
890 * take the view's boundingbox, subtract all the opaque views that cover it,
891 * and add the remaining region as damage to the plane. This corresponds
892 * to the damage inflicted to the plane if this view disappeared.
893 *
894 * A repaint is scheduled for this view.
895 *
896 * The region of all opaque views covering this view is stored in
897 * weston_view::clip and updated by view_accumulate_damage() during
898 * weston_output_repaint(). Specifically, that region matches the
899 * scenegraph as it was last painted.
900 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400901WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500902weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200903{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500904 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200905
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500906 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200907 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500908 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800909 if (view->plane)
910 pixman_region32_union(&view->plane->damage,
911 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500912 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800913 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200914}
915
Bryce Harrington3f650b82015-12-23 11:01:58 -0800916/**
917 * \param es The surface
918 * \param mask The new set of outputs for the surface
919 *
920 * Sets the surface's set of outputs to the ones specified by
921 * the new output mask provided. Identifies the outputs that
922 * have changed, the posts enter and leave events for these
923 * outputs as appropriate.
924 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400925static void
926weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
927{
928 uint32_t different = es->output_mask ^ mask;
929 uint32_t entered = mask & different;
930 uint32_t left = es->output_mask & different;
931 struct weston_output *output;
932 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500933 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400934
935 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500936 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400937 return;
938 if (different == 0)
939 return;
940
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500941 client = wl_resource_get_client(es->resource);
942
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400943 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800944 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400945 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500946 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400947 client);
948 if (resource == NULL)
949 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800950 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500951 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800952 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500953 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400954 }
955}
956
Bryce Harrington3f650b82015-12-23 11:01:58 -0800957/** Recalculate which output(s) the surface has views displayed on
958 *
959 * \param es The surface to remap to outputs
960 *
961 * Finds the output that is showing the largest amount of one
962 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300963 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800964 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300965 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800966 * in the output_mask for the surface.
967 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400968static void
969weston_surface_assign_output(struct weston_surface *es)
970{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971 struct weston_output *new_output;
972 struct weston_view *view;
973 pixman_region32_t region;
974 uint32_t max, area, mask;
975 pixman_box32_t *e;
976
977 new_output = NULL;
978 max = 0;
979 mask = 0;
980 pixman_region32_init(&region);
981 wl_list_for_each(view, &es->views, surface_link) {
982 if (!view->output)
983 continue;
984
985 pixman_region32_intersect(&region, &view->transform.boundingbox,
986 &view->output->region);
987
988 e = pixman_region32_extents(&region);
989 area = (e->x2 - e->x1) * (e->y2 - e->y1);
990
991 mask |= view->output_mask;
992
993 if (area >= max) {
994 new_output = view->output;
995 max = area;
996 }
997 }
998 pixman_region32_fini(&region);
999
1000 es->output = new_output;
1001 weston_surface_update_output_mask(es, mask);
1002}
1003
Bryce Harrington3f650b82015-12-23 11:01:58 -08001004/** Recalculate which output(s) the view is displayed on
1005 *
1006 * \param ev The view to remap to outputs
1007 *
1008 * Identifies the set of outputs that the view is visible on,
1009 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001010 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001011 *
1012 * Also does the same for the view's surface. See
1013 * weston_surface_assign_output().
1014 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001015static void
1016weston_view_assign_output(struct weston_view *ev)
1017{
1018 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001019 struct weston_output *output, *new_output;
1020 pixman_region32_t region;
1021 uint32_t max, area, mask;
1022 pixman_box32_t *e;
1023
1024 new_output = NULL;
1025 max = 0;
1026 mask = 0;
1027 pixman_region32_init(&region);
1028 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001029 if (output->destroying)
1030 continue;
1031
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001033 &output->region);
1034
1035 e = pixman_region32_extents(&region);
1036 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1037
1038 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001039 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001040
1041 if (area >= max) {
1042 new_output = output;
1043 max = area;
1044 }
1045 }
1046 pixman_region32_fini(&region);
1047
Jason Ekstranda7af7042013-10-12 22:38:11 -05001048 ev->output = new_output;
1049 ev->output_mask = mask;
1050
1051 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001052}
1053
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001054static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001055weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1056 int from_x, int from_y, int *to_x, int *to_y)
1057{
1058 float x, y;
1059
1060 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1061 weston_view_from_global_float(to, x, y, &x, &y);
1062
1063 *to_x = round(x);
1064 *to_y = round(y);
1065}
1066
1067static void
1068weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1069{
1070 pixman_box32_t *a;
1071 pixman_box32_t b;
1072
1073 a = pixman_region32_extents(&from->geometry.scissor);
1074
1075 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1076 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1077
1078 pixman_region32_fini(&to->geometry.scissor);
1079 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1080}
1081
1082static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001083view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001085{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001086 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1087 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001088 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001089 { inbox->x1, inbox->y1 },
1090 { inbox->x1, inbox->y2 },
1091 { inbox->x2, inbox->y1 },
1092 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001093 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001094 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001095 int i;
1096
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001097 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001098 /* avoid rounding empty bbox to 1x1 */
1099 pixman_region32_init(bbox);
1100 return;
1101 }
1102
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001103 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001104 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001105 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001106 if (x < min_x)
1107 min_x = x;
1108 if (x > max_x)
1109 max_x = x;
1110 if (y < min_y)
1111 min_y = y;
1112 if (y > max_y)
1113 max_y = y;
1114 }
1115
Pekka Paalanen219b9822012-02-08 15:38:37 +02001116 int_x = floorf(min_x);
1117 int_y = floorf(min_y);
1118 pixman_region32_init_rect(bbox, int_x, int_y,
1119 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001120}
1121
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001122static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001124{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001126
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001127 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128 view->geometry.x = roundf(view->geometry.x);
1129 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001130
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001131 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001132 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1133 view->transform.position.matrix.d[12] = view->geometry.x;
1134 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001135
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001137
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 view->transform.inverse = view->transform.position.matrix;
1139 view->transform.inverse.d[12] = -view->geometry.x;
1140 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001141
Jason Ekstranda7af7042013-10-12 22:38:11 -05001142 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001143 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001144 view->surface->width,
1145 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001146 if (view->geometry.scissor_enabled)
1147 pixman_region32_intersect(&view->transform.boundingbox,
1148 &view->transform.boundingbox,
1149 &view->geometry.scissor);
1150
1151 pixman_region32_translate(&view->transform.boundingbox,
1152 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001153
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 if (view->alpha == 1.0) {
1155 pixman_region32_copy(&view->transform.opaque,
1156 &view->surface->opaque);
1157 pixman_region32_translate(&view->transform.opaque,
1158 view->geometry.x,
1159 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001160 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001161}
1162
1163static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001165{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 struct weston_view *parent = view->geometry.parent;
1167 struct weston_matrix *matrix = &view->transform.matrix;
1168 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001169 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001170 pixman_region32_t surfregion;
1171 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001172
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001174
1175 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001176 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1177 view->transform.position.matrix.d[12] = view->geometry.x;
1178 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001179
1180 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001182 weston_matrix_multiply(matrix, &tform->matrix);
1183
Pekka Paalanen483243f2013-03-08 14:56:50 +02001184 if (parent)
1185 weston_matrix_multiply(matrix, &parent->transform.matrix);
1186
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001187 if (weston_matrix_invert(inverse, matrix) < 0) {
1188 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 weston_log("error: weston_view %p"
1190 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001191 return -1;
1192 }
1193
Pekka Paalanen380adf52015-02-16 14:39:11 +02001194 pixman_region32_init_rect(&surfregion, 0, 0,
1195 view->surface->width, view->surface->height);
1196 if (view->geometry.scissor_enabled)
1197 pixman_region32_intersect(&surfregion, &surfregion,
1198 &view->geometry.scissor);
1199 surfbox = pixman_region32_extents(&surfregion);
1200
1201 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1202 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001203
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001204 return 0;
1205}
1206
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001207static struct weston_layer *
1208get_view_layer(struct weston_view *view)
1209{
1210 if (view->parent_view)
1211 return get_view_layer(view->parent_view);
1212 return view->layer_link.layer;
1213}
1214
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001215WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001217{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001218 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001219 struct weston_layer *layer;
1220 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001221
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001223 return;
1224
Pekka Paalanen483243f2013-03-08 14:56:50 +02001225 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001229
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001231
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 pixman_region32_fini(&view->transform.boundingbox);
1233 pixman_region32_fini(&view->transform.opaque);
1234 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001235
Pekka Paalanencd403622012-01-25 13:37:39 +02001236 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 if (view->geometry.transformation_list.next ==
1238 &view->transform.position.link &&
1239 view->geometry.transformation_list.prev ==
1240 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001241 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001243 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 if (weston_view_update_transform_enable(view) < 0)
1245 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001246 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001247
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001248 layer = get_view_layer(view);
1249 if (layer) {
1250 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001251 pixman_region32_intersect(&view->transform.boundingbox,
1252 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001253 pixman_region32_intersect(&view->transform.opaque,
1254 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001255 pixman_region32_fini(&mask);
1256 }
1257
Pekka Paalanen380adf52015-02-16 14:39:11 +02001258 if (parent) {
1259 if (parent->geometry.scissor_enabled) {
1260 view->geometry.scissor_enabled = true;
1261 weston_view_transfer_scissor(parent, view);
1262 } else {
1263 view->geometry.scissor_enabled = false;
1264 }
1265 }
1266
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001268
Jason Ekstranda7af7042013-10-12 22:38:11 -05001269 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001270
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 wl_signal_emit(&view->surface->compositor->transform_signal,
1272 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001273}
1274
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001275WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001277{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001279
1280 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 * The invariant: if view->geometry.dirty, then all views
1282 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001283 * Corollary: if not parent->geometry.dirty, then all ancestors
1284 * are not dirty.
1285 */
1286
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001288 return;
1289
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001291
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001293 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001295}
1296
1297WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298weston_view_to_global_fixed(struct weston_view *view,
1299 wl_fixed_t vx, wl_fixed_t vy,
1300 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001301{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001302 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001303
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 weston_view_to_global_float(view,
1305 wl_fixed_to_double(vx),
1306 wl_fixed_to_double(vy),
1307 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001308 *x = wl_fixed_from_double(xf);
1309 *y = wl_fixed_from_double(yf);
1310}
1311
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001312WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313weston_view_from_global_float(struct weston_view *view,
1314 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001315{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001317 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1318
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001320
1321 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001322 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001324 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001325 *vx = 0;
1326 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001327 return;
1328 }
1329
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 *vx = v.f[0] / v.f[3];
1331 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001332 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 *vx = x - view->geometry.x;
1334 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001335 }
1336}
1337
1338WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339weston_view_from_global_fixed(struct weston_view *view,
1340 wl_fixed_t x, wl_fixed_t y,
1341 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001342{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001344
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 weston_view_from_global_float(view,
1346 wl_fixed_to_double(x),
1347 wl_fixed_to_double(y),
1348 &vxf, &vyf);
1349 *vx = wl_fixed_from_double(vxf);
1350 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001351}
1352
1353WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354weston_view_from_global(struct weston_view *view,
1355 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001356{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1360 *vx = floorf(vxf);
1361 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001362}
1363
Bryce Harrington3f650b82015-12-23 11:01:58 -08001364/**
1365 * \param surface The surface to be repainted
1366 *
1367 * Marks the output(s) that the surface is shown on as needing to be
1368 * repainted. See weston_output_schedule_repaint().
1369 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001370WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001371weston_surface_schedule_repaint(struct weston_surface *surface)
1372{
1373 struct weston_output *output;
1374
1375 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001376 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001377 weston_output_schedule_repaint(output);
1378}
1379
Bryce Harrington3f650b82015-12-23 11:01:58 -08001380/**
1381 * \param view The view to be repainted
1382 *
1383 * Marks the output(s) that the view is shown on as needing to be
1384 * repainted. See weston_output_schedule_repaint().
1385 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001386WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387weston_view_schedule_repaint(struct weston_view *view)
1388{
1389 struct weston_output *output;
1390
1391 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001392 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001393 weston_output_schedule_repaint(output);
1394}
1395
Pekka Paalanene508ce62015-02-19 13:59:55 +02001396/**
1397 * XXX: This function does it the wrong way.
1398 * surface->damage is the damage from the client, and causes
1399 * surface_flush_damage() to copy pixels. No window management action can
1400 * cause damage to the client-provided content, warranting re-upload!
1401 *
1402 * Instead of surface->damage, this function should record the damage
1403 * with all the views for this surface to avoid extraneous texture
1404 * uploads.
1405 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001407weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001408{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001409 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 0, 0, surface->width,
1411 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001412
Kristian Høgsberg98238702012-08-03 16:29:12 -04001413 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001414}
1415
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001416WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001417weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001418{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001419 if (view->geometry.x == x && view->geometry.y == y)
1420 return;
1421
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 view->geometry.x = x;
1423 view->geometry.y = y;
1424 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001425}
1426
Pekka Paalanen483243f2013-03-08 14:56:50 +02001427static void
1428transform_parent_handle_parent_destroy(struct wl_listener *listener,
1429 void *data)
1430{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431 struct weston_view *view =
1432 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001433 geometry.parent_destroy_listener);
1434
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001436}
1437
1438WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001440 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001441{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 if (view->geometry.parent) {
1443 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1444 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001445
1446 if (!parent)
1447 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001448 }
1449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001451
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001453 transform_parent_handle_parent_destroy;
1454 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001455 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001457 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001459 }
1460
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 weston_view_geometry_dirty(view);
1462}
1463
Pekka Paalanen380adf52015-02-16 14:39:11 +02001464/** Set a clip mask rectangle on a view
1465 *
1466 * \param view The view to set the clip mask on.
1467 * \param x Top-left corner X coordinate of the clip rectangle.
1468 * \param y Top-left corner Y coordinate of the clip rectangle.
1469 * \param width Width of the clip rectangle, non-negative.
1470 * \param height Height of the clip rectangle, non-negative.
1471 *
1472 * A shell may set a clip mask rectangle on a view. Everything outside
1473 * the rectangle is cut away for input and output purposes: it is
1474 * not drawn and cannot be hit by hit-test based input like pointer
1475 * motion or touch-downs. Everything inside the rectangle will behave
1476 * normally. Clients are unaware of clipping.
1477 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001478 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001479 * mask rectangle does not affect the view position, the view is positioned
1480 * as it would be without a clip. The clip also does not change
1481 * weston_surface::width,height.
1482 *
1483 * The clip mask rectangle is part of transformation inheritance
1484 * (weston_view_set_transform_parent()). A clip set in the root of the
1485 * transformation inheritance tree will affect all views in the tree.
1486 * A clip can be set only on the root view. Attempting to set a clip
1487 * on view that has a transformation parent will fail. Assigning a parent
1488 * to a view that has a clip set will cause the clip to be forgotten.
1489 *
1490 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1491 * on the additional transformations in the child views. These transformations
1492 * may not rotate the coordinate axes, i.e., only translation and scaling
1493 * are allowed. Violating this restriction causes the clipping to malfunction.
1494 * Furthermore, using scaling may cause rounding errors in child clipping.
1495 *
1496 * The clip mask rectangle is not automatically adjusted based on
1497 * wl_surface.attach dx and dy arguments.
1498 *
1499 * A clip mask rectangle can be set only if the compositor capability
1500 * WESTON_CAP_VIEW_CLIP_MASK is present.
1501 *
1502 * This function sets the clip mask rectangle and schedules a repaint for
1503 * the view.
1504 */
1505WL_EXPORT void
1506weston_view_set_mask(struct weston_view *view,
1507 int x, int y, int width, int height)
1508{
1509 struct weston_compositor *compositor = view->surface->compositor;
1510
1511 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1512 weston_log("%s not allowed without capability!\n", __func__);
1513 return;
1514 }
1515
1516 if (view->geometry.parent) {
1517 weston_log("view %p has a parent, clip forbidden!\n", view);
1518 return;
1519 }
1520
1521 if (width < 0 || height < 0) {
1522 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1523 x, y, width, height);
1524 return;
1525 }
1526
1527 pixman_region32_fini(&view->geometry.scissor);
1528 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1529 view->geometry.scissor_enabled = true;
1530 weston_view_geometry_dirty(view);
1531 weston_view_schedule_repaint(view);
1532}
1533
1534/** Remove the clip mask from a view
1535 *
1536 * \param view The view to remove the clip mask from.
1537 *
1538 * Removed the clip mask rectangle and schedules a repaint.
1539 *
1540 * \sa weston_view_set_mask
1541 */
1542WL_EXPORT void
1543weston_view_set_mask_infinite(struct weston_view *view)
1544{
1545 view->geometry.scissor_enabled = false;
1546 weston_view_geometry_dirty(view);
1547 weston_view_schedule_repaint(view);
1548}
1549
Armin Krezović0da12b82016-06-30 06:04:33 +02001550/* Check if view should be displayed
1551 *
1552 * The indicator is set manually when assigning
1553 * a view to a surface.
1554 *
1555 * This needs reworking. See the thread starting at:
1556 *
1557 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1558 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001559WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560weston_view_is_mapped(struct weston_view *view)
1561{
Armin Krezović0da12b82016-06-30 06:04:33 +02001562 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001563}
1564
Armin Krezović0da12b82016-06-30 06:04:33 +02001565/* Check if a surface has a view assigned to it
1566 *
1567 * The indicator is set manually when mapping
1568 * a surface and creating a view for it.
1569 *
1570 * This needs to go. See the thread starting at:
1571 *
1572 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1573 *
1574 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001575WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001576weston_surface_is_mapped(struct weston_surface *surface)
1577{
Armin Krezović0da12b82016-06-30 06:04:33 +02001578 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001579}
1580
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001581static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001582surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001583{
1584 struct weston_view *view;
1585
1586 if (surface->width == width && surface->height == height)
1587 return;
1588
1589 surface->width = width;
1590 surface->height = height;
1591
1592 wl_list_for_each(view, &surface->views, surface_link)
1593 weston_view_geometry_dirty(view);
1594}
1595
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001596WL_EXPORT void
1597weston_surface_set_size(struct weston_surface *surface,
1598 int32_t width, int32_t height)
1599{
1600 assert(!surface->resource);
1601 surface_set_size(surface, width, height);
1602}
1603
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001604static int
1605fixed_round_up_to_int(wl_fixed_t f)
1606{
1607 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1608}
1609
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001610static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001611convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1612 int32_t width, int32_t height,
1613 uint32_t transform,
1614 int32_t scale)
1615{
1616 assert(scale > 0);
1617
1618 switch (transform) {
1619 case WL_OUTPUT_TRANSFORM_NORMAL:
1620 case WL_OUTPUT_TRANSFORM_180:
1621 case WL_OUTPUT_TRANSFORM_FLIPPED:
1622 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1623 *width_out = width / scale;
1624 *height_out = height / scale;
1625 break;
1626 case WL_OUTPUT_TRANSFORM_90:
1627 case WL_OUTPUT_TRANSFORM_270:
1628 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1629 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1630 *width_out = height / scale;
1631 *height_out = width / scale;
1632 break;
1633 default:
1634 assert(0 && "invalid transform");
1635 }
1636}
1637
1638static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001639weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001640{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001641 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001642
1643 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001644 surface->width_from_buffer = 0;
1645 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001646 return;
1647 }
1648
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001649 convert_size_by_transform_scale(&surface->width_from_buffer,
1650 &surface->height_from_buffer,
1651 surface->buffer_ref.buffer->width,
1652 surface->buffer_ref.buffer->height,
1653 vp->buffer.transform,
1654 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001655}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001656
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001657static void
1658weston_surface_update_size(struct weston_surface *surface)
1659{
1660 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1661 int32_t width, height;
1662
1663 width = surface->width_from_buffer;
1664 height = surface->height_from_buffer;
1665
1666 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001667 surface_set_size(surface,
1668 vp->surface.width, vp->surface.height);
1669 return;
1670 }
1671
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001672 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001673 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1674 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1675
1676 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001677 return;
1678 }
1679
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001680 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001681}
1682
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001683WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001684weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001685{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001686 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001687
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001688 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001689
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001690 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001691}
1692
Jason Ekstranda7af7042013-10-12 22:38:11 -05001693WL_EXPORT struct weston_view *
1694weston_compositor_pick_view(struct weston_compositor *compositor,
1695 wl_fixed_t x, wl_fixed_t y,
1696 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001697{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001699 wl_fixed_t view_x, view_y;
1700 int view_ix, view_iy;
1701 int ix = wl_fixed_to_int(x);
1702 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001703
Jason Ekstranda7af7042013-10-12 22:38:11 -05001704 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001705 if (!pixman_region32_contains_point(
1706 &view->transform.boundingbox, ix, iy, NULL))
1707 continue;
1708
1709 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1710 view_ix = wl_fixed_to_int(view_x);
1711 view_iy = wl_fixed_to_int(view_y);
1712
1713 if (!pixman_region32_contains_point(&view->surface->input,
1714 view_ix, view_iy, NULL))
1715 continue;
1716
Pekka Paalanen380adf52015-02-16 14:39:11 +02001717 if (view->geometry.scissor_enabled &&
1718 !pixman_region32_contains_point(&view->geometry.scissor,
1719 view_ix, view_iy, NULL))
1720 continue;
1721
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001722 *vx = view_x;
1723 *vy = view_y;
1724 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001725 }
1726
Derek Foremanf9318d12015-05-11 15:40:11 -05001727 *vx = wl_fixed_from_int(-1000000);
1728 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001729 return NULL;
1730}
1731
1732static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001733weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001734{
Daniel Stone37816df2012-05-16 18:45:18 +01001735 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001736
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001737 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001738 return;
1739
Daniel Stone37816df2012-05-16 18:45:18 +01001740 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001741 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001742}
1743
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001744WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001745weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001746{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001747 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001748
Jason Ekstranda7af7042013-10-12 22:38:11 -05001749 if (!weston_view_is_mapped(view))
1750 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001751
Jason Ekstranda7af7042013-10-12 22:38:11 -05001752 weston_view_damage_below(view);
1753 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001754 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001755 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001756 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001757 wl_list_remove(&view->link);
1758 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001759 view->output_mask = 0;
1760 weston_surface_assign_output(view->surface);
1761
1762 if (weston_surface_is_mapped(view->surface))
1763 return;
1764
1765 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001766 struct weston_touch *touch = weston_seat_get_touch(seat);
1767 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1768 struct weston_keyboard *keyboard =
1769 weston_seat_get_keyboard(seat);
1770
1771 if (keyboard && keyboard->focus == view->surface)
1772 weston_keyboard_set_focus(keyboard, NULL);
1773 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001774 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001775 if (touch && touch->focus == view)
1776 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001777 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001778}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001779
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780WL_EXPORT void
1781weston_surface_unmap(struct weston_surface *surface)
1782{
1783 struct weston_view *view;
1784
Armin Krezovićf8486c32016-06-30 06:04:28 +02001785 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786 wl_list_for_each(view, &surface->views, surface_link)
1787 weston_view_unmap(view);
1788 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001789}
1790
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001791static void
1792weston_surface_reset_pending_buffer(struct weston_surface *surface)
1793{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001794 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001795 surface->pending.sx = 0;
1796 surface->pending.sy = 0;
1797 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001798 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001799}
1800
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801WL_EXPORT void
1802weston_view_destroy(struct weston_view *view)
1803{
1804 wl_signal_emit(&view->destroy_signal, view);
1805
1806 assert(wl_list_empty(&view->geometry.child_list));
1807
1808 if (weston_view_is_mapped(view)) {
1809 weston_view_unmap(view);
1810 weston_compositor_build_view_list(view->surface->compositor);
1811 }
1812
1813 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001814 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815
1816 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001817 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001819 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001820
1821 weston_view_set_transform_parent(view, NULL);
1822
Jason Ekstranda7af7042013-10-12 22:38:11 -05001823 wl_list_remove(&view->surface_link);
1824
1825 free(view);
1826}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001827
1828WL_EXPORT void
1829weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001830{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001831 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001832 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001833 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001834
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001835 if (--surface->ref_count > 0)
1836 return;
1837
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001838 assert(surface->resource == NULL);
1839
Pekka Paalanenca790762015-04-17 14:23:38 +03001840 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001841
Pekka Paalanene67858b2013-04-25 13:57:42 +03001842 assert(wl_list_empty(&surface->subsurface_list_pending));
1843 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001844
Jason Ekstranda7af7042013-10-12 22:38:11 -05001845 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1846 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001847
Jason Ekstrand7b982072014-05-20 14:33:03 -05001848 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001849
Pekka Paalanende685b82012-12-04 15:58:12 +02001850 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001851
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001852 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001853 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001854 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001855
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001856 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001857 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001858
Pekka Paalanen133e4392014-09-23 22:08:46 -04001859 weston_presentation_feedback_discard_list(&surface->feedback_list);
1860
Jonas Ådahld3414f22016-07-22 17:56:31 +08001861 wl_list_for_each_safe(constraint, next_constraint,
1862 &surface->pointer_constraints,
1863 link)
1864 weston_pointer_constraint_destroy(constraint);
1865
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001866 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001867}
1868
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001869static void
1870destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001871{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001872 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001873
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001874 assert(surface);
1875
Giulio Camuffo0d379742013-11-15 22:06:15 +01001876 /* Set the resource to NULL, since we don't want to leave a
1877 * dangling pointer if the surface was refcounted and survives
1878 * the weston_surface_destroy() call. */
1879 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001880
1881 if (surface->viewport_resource)
1882 wl_resource_set_user_data(surface->viewport_resource, NULL);
1883
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001884 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001885}
1886
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001887static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001888weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1889{
1890 struct weston_buffer *buffer =
1891 container_of(listener, struct weston_buffer, destroy_listener);
1892
1893 wl_signal_emit(&buffer->destroy_signal, buffer);
1894 free(buffer);
1895}
1896
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001897WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001898weston_buffer_from_resource(struct wl_resource *resource)
1899{
1900 struct weston_buffer *buffer;
1901 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001902
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001903 listener = wl_resource_get_destroy_listener(resource,
1904 weston_buffer_destroy_handler);
1905
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001906 if (listener)
1907 return container_of(listener, struct weston_buffer,
1908 destroy_listener);
1909
1910 buffer = zalloc(sizeof *buffer);
1911 if (buffer == NULL)
1912 return NULL;
1913
1914 buffer->resource = resource;
1915 wl_signal_init(&buffer->destroy_signal);
1916 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001917 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001918 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001919
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001920 return buffer;
1921}
1922
1923static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001924weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1925 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001926{
Pekka Paalanende685b82012-12-04 15:58:12 +02001927 struct weston_buffer_reference *ref =
1928 container_of(listener, struct weston_buffer_reference,
1929 destroy_listener);
1930
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001931 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001932 ref->buffer = NULL;
1933}
1934
1935WL_EXPORT void
1936weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001937 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001938{
1939 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001940 ref->buffer->busy_count--;
1941 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001942 assert(wl_resource_get_client(ref->buffer->resource));
1943 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001944 WL_BUFFER_RELEASE);
1945 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001946 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001947 }
1948
Pekka Paalanende685b82012-12-04 15:58:12 +02001949 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001950 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001951 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001952 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001953 }
1954
Pekka Paalanende685b82012-12-04 15:58:12 +02001955 ref->buffer = buffer;
1956 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1957}
1958
1959static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001960weston_surface_attach(struct weston_surface *surface,
1961 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001962{
1963 weston_buffer_reference(&surface->buffer_ref, buffer);
1964
Pekka Paalanena6421c42012-12-04 15:58:10 +02001965 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001966 if (weston_surface_is_mapped(surface))
1967 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001968 }
1969
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001970 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001971
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001972 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001973 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001974}
1975
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001976WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001977weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001978{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001979 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001980
1981 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001982 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001983}
1984
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001985WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001986weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001987{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001988 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001989
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001990 pixman_region32_union(&compositor->primary_plane.damage,
1991 &compositor->primary_plane.damage,
1992 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001993 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001994}
1995
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001996static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001998{
Pekka Paalanende685b82012-12-04 15:58:12 +02001999 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002000 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002001 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002002
Pekka Paalanenb5026542014-11-12 15:09:24 +02002003 if (weston_timeline_enabled_ &&
2004 pixman_region32_not_empty(&surface->damage))
2005 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2006 TLP_OUTPUT(surface->output), TLP_END);
2007
Jason Ekstrandef540082014-06-26 10:37:36 -07002008 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002009}
2010
2011static void
2012view_accumulate_damage(struct weston_view *view,
2013 pixman_region32_t *opaque)
2014{
2015 pixman_region32_t damage;
2016
2017 pixman_region32_init(&damage);
2018 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002019 pixman_box32_t *extents;
2020
Jason Ekstranda7af7042013-10-12 22:38:11 -05002021 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002022 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002023 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002024 pixman_region32_copy(&damage, &view->surface->damage);
2025 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002026 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002027 }
2028
Pekka Paalanen380adf52015-02-16 14:39:11 +02002029 pixman_region32_intersect(&damage, &damage,
2030 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002031 pixman_region32_subtract(&damage, &damage, opaque);
2032 pixman_region32_union(&view->plane->damage,
2033 &view->plane->damage, &damage);
2034 pixman_region32_fini(&damage);
2035 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002036 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002037}
2038
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002039static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002040compositor_accumulate_damage(struct weston_compositor *ec)
2041{
2042 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002043 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002044 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002045
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002046 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002047
2048 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002049 pixman_region32_copy(&plane->clip, &clip);
2050
2051 pixman_region32_init(&opaque);
2052
Jason Ekstranda7af7042013-10-12 22:38:11 -05002053 wl_list_for_each(ev, &ec->view_list, link) {
2054 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002055 continue;
2056
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002058 }
2059
2060 pixman_region32_union(&clip, &clip, &opaque);
2061 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002062 }
2063
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002064 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002065
Jason Ekstranda7af7042013-10-12 22:38:11 -05002066 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002067 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002068
2069 wl_list_for_each(ev, &ec->view_list, link) {
2070 if (ev->surface->touched)
2071 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002072 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002073
2074 surface_flush_damage(ev->surface);
2075
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002076 /* Both the renderer and the backend have seen the buffer
2077 * by now. If renderer needs the buffer, it has its own
2078 * reference set. If the backend wants to keep the buffer
2079 * around for migrating the surface into a non-primary plane
2080 * later, keep_buffer is true. Otherwise, drop the core
2081 * reference now, and allow early buffer release. This enables
2082 * clients to use single-buffering.
2083 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002084 if (!ev->surface->keep_buffer)
2085 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002086 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002087}
2088
2089static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002091{
2092 struct weston_subsurface *sub;
2093
Pekka Paalanene67858b2013-04-25 13:57:42 +03002094 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002095 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002096 continue;
2097
Jason Ekstranda7af7042013-10-12 22:38:11 -05002098 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2099 wl_list_init(&sub->surface->views);
2100
2101 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002102 }
2103}
2104
2105static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002107{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002108 struct weston_subsurface *sub;
2109 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002110
Jason Ekstranda7af7042013-10-12 22:38:11 -05002111 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2112 if (sub->surface == surface)
2113 continue;
2114
George Kiagiadakised04d382014-06-13 18:10:26 +02002115 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2116 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002117 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002118 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002119
2120 surface_free_unused_subsurface_views(sub->surface);
2121 }
2122}
2123
2124static void
2125view_list_add_subsurface_view(struct weston_compositor *compositor,
2126 struct weston_subsurface *sub,
2127 struct weston_view *parent)
2128{
2129 struct weston_subsurface *child;
2130 struct weston_view *view = NULL, *iv;
2131
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002132 if (!weston_surface_is_mapped(sub->surface))
2133 return;
2134
Jason Ekstranda7af7042013-10-12 22:38:11 -05002135 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2136 if (iv->geometry.parent == parent) {
2137 view = iv;
2138 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002139 }
2140 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002141
2142 if (view) {
2143 /* Put it back in the surface's list of views */
2144 wl_list_remove(&view->surface_link);
2145 wl_list_insert(&sub->surface->views, &view->surface_link);
2146 } else {
2147 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002148 weston_view_set_position(view,
2149 sub->position.x,
2150 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002151 weston_view_set_transform_parent(view, parent);
2152 }
2153
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002154 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002155 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002156 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002157
Pekka Paalanenb188e912013-11-19 14:03:35 +02002158 if (wl_list_empty(&sub->surface->subsurface_list)) {
2159 wl_list_insert(compositor->view_list.prev, &view->link);
2160 return;
2161 }
2162
2163 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2164 if (child->surface == sub->surface)
2165 wl_list_insert(compositor->view_list.prev, &view->link);
2166 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002168 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169}
2170
2171static void
2172view_list_add(struct weston_compositor *compositor,
2173 struct weston_view *view)
2174{
2175 struct weston_subsurface *sub;
2176
2177 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178
Pekka Paalanenb188e912013-11-19 14:03:35 +02002179 if (wl_list_empty(&view->surface->subsurface_list)) {
2180 wl_list_insert(compositor->view_list.prev, &view->link);
2181 return;
2182 }
2183
2184 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2185 if (sub->surface == view->surface)
2186 wl_list_insert(compositor->view_list.prev, &view->link);
2187 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002188 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002189 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002190}
2191
2192static void
2193weston_compositor_build_view_list(struct weston_compositor *compositor)
2194{
2195 struct weston_view *view;
2196 struct weston_layer *layer;
2197
2198 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002199 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002200 surface_stash_subsurface_views(view->surface);
2201
2202 wl_list_init(&compositor->view_list);
2203 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002204 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002205 view_list_add(compositor, view);
2206 }
2207 }
2208
2209 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002210 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002211 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002212}
2213
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002214static void
2215weston_output_take_feedback_list(struct weston_output *output,
2216 struct weston_surface *surface)
2217{
2218 struct weston_view *view;
2219 struct weston_presentation_feedback *feedback;
2220 uint32_t flags = 0xffffffff;
2221
2222 if (wl_list_empty(&surface->feedback_list))
2223 return;
2224
2225 /* All views must have the flag for the flag to survive. */
2226 wl_list_for_each(view, &surface->views, surface_link) {
2227 /* ignore views that are not on this output at all */
2228 if (view->output_mask & (1u << output->id))
2229 flags &= view->psf_flags;
2230 }
2231
2232 wl_list_for_each(feedback, &surface->feedback_list, link)
2233 feedback->psf_flags = flags;
2234
2235 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2236 wl_list_init(&surface->feedback_list);
2237}
2238
David Herrmann1edf44c2013-10-22 17:11:26 +02002239static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002240weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002241{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002242 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002244 struct weston_animation *animation, *next;
2245 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002246 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002247 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002248 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002249
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002250 if (output->destroying)
2251 return 0;
2252
Pekka Paalanenb5026542014-11-12 15:09:24 +02002253 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2254
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002255 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002256 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002257
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002258 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002259 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002260 } else {
2261 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002262 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002263 ev->psf_flags = 0;
2264 }
2265 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002266
Pekka Paalanene67858b2013-04-25 13:57:42 +03002267 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002268 wl_list_for_each(ev, &ec->view_list, link) {
2269 /* Note: This operation is safe to do multiple times on the
2270 * same surface.
2271 */
2272 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002273 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002274 &ev->surface->frame_callback_list);
2275 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002276
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002277 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002278 }
2279 }
2280
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002281 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002282
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002283 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002284 pixman_region32_intersect(&output_damage,
2285 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002286 pixman_region32_subtract(&output_damage,
2287 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002288
Scott Moreauccbf29d2012-02-22 14:21:41 -07002289 if (output->dirty)
2290 weston_output_update_matrix(output);
2291
David Herrmann1edf44c2013-10-22 17:11:26 +02002292 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002293
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002294 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002295
Kristian Høgsbergef044142011-06-21 15:02:12 -04002296 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002297
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002298 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002299
Jonas Ådahldb773762012-06-13 00:01:21 +02002300 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002301 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002302 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002303 }
2304
Scott Moreaud64cf212012-06-08 19:40:54 -06002305 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002306 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002307 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002308 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002309
Pekka Paalanenb5026542014-11-12 15:09:24 +02002310 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2311
David Herrmann1edf44c2013-10-22 17:11:26 +02002312 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002313}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002314
Pekka Paalanen82919792014-05-21 13:51:49 +03002315static void
2316weston_output_schedule_repaint_reset(struct weston_output *output)
2317{
Pekka Paalanen82919792014-05-21 13:51:49 +03002318 output->repaint_scheduled = 0;
2319 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002320}
2321
Pekka Paalanen0513a952014-05-21 16:17:27 +03002322static int
2323output_repaint_timer_handler(void *data)
2324{
2325 struct weston_output *output = data;
2326 struct weston_compositor *compositor = output->compositor;
2327
2328 if (output->repaint_needed &&
2329 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2330 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2331 weston_output_repaint(output) == 0)
2332 return 0;
2333
2334 weston_output_schedule_repaint_reset(output);
2335
2336 return 0;
2337}
2338
Kristian Høgsbergef044142011-06-21 15:02:12 -04002339WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002340weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002341 const struct timespec *stamp,
2342 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002343{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002344 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002345 int32_t refresh_nsec;
2346 struct timespec now;
2347 struct timespec gone;
2348 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002349
Pekka Paalanenb5026542014-11-12 15:09:24 +02002350 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2351 TLP_VBLANK(stamp), TLP_END);
2352
Pekka Paalanend7894d02015-07-03 15:08:53 +03002353 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002354 weston_presentation_feedback_present_list(&output->feedback_list,
2355 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002356 output->msc,
2357 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002358
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002359 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002360
Pekka Paalanen0513a952014-05-21 16:17:27 +03002361 weston_compositor_read_presentation_clock(compositor, &now);
2362 timespec_sub(&gone, &now, stamp);
2363 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2364 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002365
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002366 if (msec < -1000 || msec > 1000) {
2367 static bool warned;
2368
2369 if (!warned)
2370 weston_log("Warning: computed repaint delay is "
2371 "insane: %d msec\n", msec);
2372 warned = true;
2373
2374 msec = 0;
2375 }
2376
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002377 /* Called from restart_repaint_loop and restart happens already after
2378 * the deadline given by repaint_msec? In that case we delay until
2379 * the deadline of the next frame, to give clients a more predictable
2380 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002381 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002382 msec += refresh_nsec / 1000000;
2383
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002384 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002385 output_repaint_timer_handler(output);
2386 else
2387 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002388}
2389
2390static void
2391idle_repaint(void *data)
2392{
2393 struct weston_output *output = data;
2394
Jonas Ådahle5a12252013-04-05 23:07:11 +02002395 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002396}
2397
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002398WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002399weston_layer_entry_insert(struct weston_layer_entry *list,
2400 struct weston_layer_entry *entry)
2401{
2402 wl_list_insert(&list->link, &entry->link);
2403 entry->layer = list->layer;
2404}
2405
2406WL_EXPORT void
2407weston_layer_entry_remove(struct weston_layer_entry *entry)
2408{
2409 wl_list_remove(&entry->link);
2410 wl_list_init(&entry->link);
2411 entry->layer = NULL;
2412}
2413
2414WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002415weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2416{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002417 wl_list_init(&layer->view_list.link);
2418 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002419 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002420 if (below != NULL)
2421 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002422}
2423
2424WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002425weston_layer_set_mask(struct weston_layer *layer,
2426 int x, int y, int width, int height)
2427{
2428 struct weston_view *view;
2429
2430 layer->mask.x1 = x;
2431 layer->mask.x2 = x + width;
2432 layer->mask.y1 = y;
2433 layer->mask.y2 = y + height;
2434
2435 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2436 weston_view_geometry_dirty(view);
2437 }
2438}
2439
2440WL_EXPORT void
2441weston_layer_set_mask_infinite(struct weston_layer *layer)
2442{
2443 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2444 UINT32_MAX, UINT32_MAX);
2445}
2446
2447WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002448weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002449{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002450 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002451 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002452
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002453 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2454 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002455 return;
2456
Pekka Paalanenb5026542014-11-12 15:09:24 +02002457 if (!output->repaint_needed)
2458 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2459
Kristian Høgsbergef044142011-06-21 15:02:12 -04002460 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002461 output->repaint_needed = 1;
2462 if (output->repaint_scheduled)
2463 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002464
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002465 wl_event_loop_add_idle(loop, idle_repaint, output);
2466 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002467 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002468}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002469
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002470WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002471weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2472{
2473 struct weston_output *output;
2474
2475 wl_list_for_each(output, &compositor->output_list, link)
2476 weston_output_schedule_repaint(output);
2477}
2478
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002479static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002480surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002481{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002482 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002483}
2484
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002485static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002486surface_attach(struct wl_client *client,
2487 struct wl_resource *resource,
2488 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2489{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002490 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002491 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002492
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002493 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002494 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002495 if (buffer == NULL) {
2496 wl_client_post_no_memory(client);
2497 return;
2498 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002499 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002500
Pekka Paalanende685b82012-12-04 15:58:12 +02002501 /* Attach, attach, without commit in between does not send
2502 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002503 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002504
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002505 surface->pending.sx = sx;
2506 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002507 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002508}
2509
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002510static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002511surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002512 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002513 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002514{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002515 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002516
Derek Foreman57e92ed2015-11-17 14:11:35 -06002517 if (width <= 0 || height <= 0)
2518 return;
2519
Derek Foreman152254b2015-11-26 14:17:48 -06002520 pixman_region32_union_rect(&surface->pending.damage_surface,
2521 &surface->pending.damage_surface,
2522 x, y, width, height);
2523}
2524
2525static void
2526surface_damage_buffer(struct wl_client *client,
2527 struct wl_resource *resource,
2528 int32_t x, int32_t y, int32_t width, int32_t height)
2529{
2530 struct weston_surface *surface = wl_resource_get_user_data(resource);
2531
2532 if (width <= 0 || height <= 0)
2533 return;
2534
2535 pixman_region32_union_rect(&surface->pending.damage_buffer,
2536 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002537 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002538}
2539
Kristian Høgsberg33418202011-08-16 23:01:28 -04002540static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002541destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002542{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002543 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002544
2545 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002546 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002547}
2548
2549static void
2550surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002551 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002552{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002553 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002554 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002555
2556 cb = malloc(sizeof *cb);
2557 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002558 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002559 return;
2560 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002561
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002562 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2563 callback);
2564 if (cb->resource == NULL) {
2565 free(cb);
2566 wl_resource_post_no_memory(resource);
2567 return;
2568 }
2569
Jason Ekstranda85118c2013-06-27 20:17:02 -05002570 wl_resource_set_implementation(cb->resource, NULL, cb,
2571 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002572
Pekka Paalanenbc106382012-10-10 12:49:31 +03002573 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002574}
2575
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002576static void
2577surface_set_opaque_region(struct wl_client *client,
2578 struct wl_resource *resource,
2579 struct wl_resource *region_resource)
2580{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002581 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002582 struct weston_region *region;
2583
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002584 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002585 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002586 pixman_region32_copy(&surface->pending.opaque,
2587 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002588 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002589 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002590 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002591}
2592
2593static void
2594surface_set_input_region(struct wl_client *client,
2595 struct wl_resource *resource,
2596 struct wl_resource *region_resource)
2597{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002598 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002599 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002600
2601 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002602 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002603 pixman_region32_copy(&surface->pending.input,
2604 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002605 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002606 pixman_region32_fini(&surface->pending.input);
2607 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002608 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002609}
2610
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002611static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002612weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002613{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002614 struct weston_subsurface *sub;
2615
2616 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2617 parent_link_pending) {
2618 wl_list_remove(&sub->parent_link);
2619 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2620 }
2621}
2622
2623static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002624weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002625 struct weston_matrix *matrix)
2626{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002627 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002628 double src_width, src_height, dest_width, dest_height;
2629
2630 weston_matrix_init(matrix);
2631
2632 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2633 src_width = surface->width_from_buffer;
2634 src_height = surface->height_from_buffer;
2635 } else {
2636 src_width = wl_fixed_to_double(vp->buffer.src_width);
2637 src_height = wl_fixed_to_double(vp->buffer.src_height);
2638 }
2639
2640 if (vp->surface.width == -1) {
2641 dest_width = src_width;
2642 dest_height = src_height;
2643 } else {
2644 dest_width = vp->surface.width;
2645 dest_height = vp->surface.height;
2646 }
2647
2648 if (src_width != dest_width || src_height != dest_height)
2649 weston_matrix_scale(matrix,
2650 src_width / dest_width,
2651 src_height / dest_height, 1);
2652
2653 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2654 weston_matrix_translate(matrix,
2655 wl_fixed_to_double(vp->buffer.src_x),
2656 wl_fixed_to_double(vp->buffer.src_y),
2657 0);
2658
2659 switch (vp->buffer.transform) {
2660 case WL_OUTPUT_TRANSFORM_FLIPPED:
2661 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2662 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2663 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2664 weston_matrix_scale(matrix, -1, 1, 1);
2665 weston_matrix_translate(matrix,
2666 surface->width_from_buffer, 0, 0);
2667 break;
2668 }
2669
2670 switch (vp->buffer.transform) {
2671 default:
2672 case WL_OUTPUT_TRANSFORM_NORMAL:
2673 case WL_OUTPUT_TRANSFORM_FLIPPED:
2674 break;
2675 case WL_OUTPUT_TRANSFORM_90:
2676 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2677 weston_matrix_rotate_xy(matrix, 0, 1);
2678 weston_matrix_translate(matrix,
2679 surface->height_from_buffer, 0, 0);
2680 break;
2681 case WL_OUTPUT_TRANSFORM_180:
2682 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2683 weston_matrix_rotate_xy(matrix, -1, 0);
2684 weston_matrix_translate(matrix,
2685 surface->width_from_buffer,
2686 surface->height_from_buffer, 0);
2687 break;
2688 case WL_OUTPUT_TRANSFORM_270:
2689 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2690 weston_matrix_rotate_xy(matrix, 0, -1);
2691 weston_matrix_translate(matrix,
2692 0, surface->width_from_buffer, 0);
2693 break;
2694 }
2695
2696 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2697}
2698
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002699/**
2700 * Compute a + b > c while being safe to overflows.
2701 */
2702static bool
2703fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2704{
2705 return (int64_t)a + (int64_t)b > (int64_t)c;
2706}
2707
2708static bool
2709weston_surface_is_pending_viewport_source_valid(
2710 const struct weston_surface *surface)
2711{
2712 const struct weston_surface_state *pend = &surface->pending;
2713 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2714 int width_from_buffer = 0;
2715 int height_from_buffer = 0;
2716 wl_fixed_t w;
2717 wl_fixed_t h;
2718
2719 /* If viewport source rect is not set, it is always ok. */
2720 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2721 return true;
2722
2723 if (pend->newly_attached) {
2724 if (pend->buffer) {
2725 convert_size_by_transform_scale(&width_from_buffer,
2726 &height_from_buffer,
2727 pend->buffer->width,
2728 pend->buffer->height,
2729 vp->buffer.transform,
2730 vp->buffer.scale);
2731 } else {
2732 /* No buffer: viewport is irrelevant. */
2733 return true;
2734 }
2735 } else {
2736 width_from_buffer = surface->width_from_buffer;
2737 height_from_buffer = surface->height_from_buffer;
2738 }
2739
2740 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2741 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2742
2743 /* No buffer: viewport is irrelevant. */
2744 if (width_from_buffer == 0 || height_from_buffer == 0)
2745 return true;
2746
2747 /* overflow checks for wl_fixed_from_int() */
2748 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2749 return false;
2750 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2751 return false;
2752
2753 w = wl_fixed_from_int(width_from_buffer);
2754 h = wl_fixed_from_int(height_from_buffer);
2755
2756 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2757 return false;
2758 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2759 return false;
2760
2761 return true;
2762}
2763
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002764static bool
2765fixed_is_integer(wl_fixed_t v)
2766{
2767 return (v & 0xff) == 0;
2768}
2769
2770static bool
2771weston_surface_is_pending_viewport_dst_size_int(
2772 const struct weston_surface *surface)
2773{
2774 const struct weston_buffer_viewport *vp =
2775 &surface->pending.buffer_viewport;
2776
2777 if (vp->surface.width != -1) {
2778 assert(vp->surface.width > 0 && vp->surface.height > 0);
2779 return true;
2780 }
2781
2782 return fixed_is_integer(vp->buffer.src_width) &&
2783 fixed_is_integer(vp->buffer.src_height);
2784}
2785
Derek Foreman152254b2015-11-26 14:17:48 -06002786/* Translate pending damage in buffer co-ordinates to surface
2787 * co-ordinates and union it with a pixman_region32_t.
2788 * This should only be called after the buffer is attached.
2789 */
2790static void
2791apply_damage_buffer(pixman_region32_t *dest,
2792 struct weston_surface *surface,
2793 struct weston_surface_state *state)
2794{
2795 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2796
2797 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2798 * translated into surface co-ordinates and unioned with
2799 * any other surface damage.
2800 * None of this makes sense if there is no buffer though.
2801 */
2802 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2803 pixman_region32_t buffer_damage;
2804
2805 pixman_region32_intersect_rect(&state->damage_buffer,
2806 &state->damage_buffer,
2807 0, 0, buffer->width,
2808 buffer->height);
2809 pixman_region32_init(&buffer_damage);
2810 weston_matrix_transform_region(&buffer_damage,
2811 &surface->buffer_to_surface_matrix,
2812 &state->damage_buffer);
2813 pixman_region32_union(dest, dest, &buffer_damage);
2814 pixman_region32_fini(&buffer_damage);
2815 }
2816 /* We should clear this on commit even if there was no buffer */
2817 pixman_region32_clear(&state->damage_buffer);
2818}
2819
Jason Ekstrand1e059042014-10-16 10:55:19 -05002820static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002821weston_surface_commit_state(struct weston_surface *surface,
2822 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002823{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002824 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002825 pixman_region32_t opaque;
2826
Alexander Larsson4ea95522013-05-22 14:41:37 +02002827 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002828 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002829 /* wp_viewport.set_source */
2830 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002831 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002832
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002833 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002834 if (state->newly_attached)
2835 weston_surface_attach(surface, state->buffer);
2836 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002837
Jason Ekstrand1e059042014-10-16 10:55:19 -05002838 weston_surface_build_buffer_matrix(surface,
2839 &surface->surface_to_buffer_matrix);
2840 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2841 &surface->surface_to_buffer_matrix);
2842
Jason Ekstrand7b982072014-05-20 14:33:03 -05002843 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002844 weston_surface_update_size(surface);
2845 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002846 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002847 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002848
Jason Ekstrand7b982072014-05-20 14:33:03 -05002849 state->sx = 0;
2850 state->sy = 0;
2851 state->newly_attached = 0;
2852 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002853
Derek Foreman152254b2015-11-26 14:17:48 -06002854 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002855 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002856 (pixman_region32_not_empty(&state->damage_surface) ||
2857 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002858 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002859
Pekka Paalanen8e159182012-10-10 12:49:25 +03002860 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002861 &state->damage_surface);
2862
2863 apply_damage_buffer(&surface->damage, surface, state);
2864
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002865 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002866 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002867 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002868
2869 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002870 pixman_region32_init(&opaque);
2871 pixman_region32_intersect_rect(&opaque, &state->opaque,
2872 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002873
2874 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2875 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002876 wl_list_for_each(view, &surface->views, surface_link)
2877 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002878 }
2879
2880 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002881
2882 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002883 pixman_region32_intersect_rect(&surface->input, &state->input,
2884 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002885
Pekka Paalanenbc106382012-10-10 12:49:31 +03002886 /* wl_surface.frame */
2887 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002888 &state->frame_callback_list);
2889 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002890
2891 /* XXX:
2892 * What should happen with a feedback request, if there
2893 * is no wl_buffer attached for this commit?
2894 */
2895
2896 /* presentation.feedback */
2897 wl_list_insert_list(&surface->feedback_list,
2898 &state->feedback_list);
2899 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08002900
2901 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002902}
2903
2904static void
2905weston_surface_commit(struct weston_surface *surface)
2906{
2907 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002908
Pekka Paalanene67858b2013-04-25 13:57:42 +03002909 weston_surface_commit_subsurface_order(surface);
2910
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002911 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002912}
2913
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002914static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002915weston_subsurface_commit(struct weston_subsurface *sub);
2916
2917static void
2918weston_subsurface_parent_commit(struct weston_subsurface *sub,
2919 int parent_is_synchronized);
2920
2921static void
2922surface_commit(struct wl_client *client, struct wl_resource *resource)
2923{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002924 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002925 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2926
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002927 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
2928 assert(surface->viewport_resource);
2929
2930 wl_resource_post_error(surface->viewport_resource,
2931 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
2932 "wl_surface@%d has viewport source outside buffer",
2933 wl_resource_get_id(resource));
2934 return;
2935 }
2936
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002937 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
2938 assert(surface->viewport_resource);
2939
2940 wl_resource_post_error(surface->viewport_resource,
2941 WP_VIEWPORT_ERROR_BAD_SIZE,
2942 "wl_surface@%d viewport dst size not integer",
2943 wl_resource_get_id(resource));
2944 return;
2945 }
2946
Pekka Paalanene67858b2013-04-25 13:57:42 +03002947 if (sub) {
2948 weston_subsurface_commit(sub);
2949 return;
2950 }
2951
2952 weston_surface_commit(surface);
2953
2954 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2955 if (sub->surface != surface)
2956 weston_subsurface_parent_commit(sub, 0);
2957 }
2958}
2959
2960static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002961surface_set_buffer_transform(struct wl_client *client,
2962 struct wl_resource *resource, int transform)
2963{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002964 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002965
Jonny Lamba55f1392014-05-30 12:07:15 +02002966 /* if wl_output.transform grows more members this will need to be updated. */
2967 if (transform < 0 ||
2968 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2969 wl_resource_post_error(resource,
2970 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2971 "buffer transform must be a valid transform "
2972 "('%d' specified)", transform);
2973 return;
2974 }
2975
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002976 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002977 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002978}
2979
Alexander Larsson4ea95522013-05-22 14:41:37 +02002980static void
2981surface_set_buffer_scale(struct wl_client *client,
2982 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002983 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002984{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002985 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002986
Jonny Lamba55f1392014-05-30 12:07:15 +02002987 if (scale < 1) {
2988 wl_resource_post_error(resource,
2989 WL_SURFACE_ERROR_INVALID_SCALE,
2990 "buffer scale must be at least one "
2991 "('%d' specified)", scale);
2992 return;
2993 }
2994
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002995 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002996 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002997}
2998
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002999static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003000 surface_destroy,
3001 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003002 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003003 surface_frame,
3004 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003005 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003006 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003007 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003008 surface_set_buffer_scale,
3009 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003010};
3011
3012static void
3013compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003014 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003015{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003016 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003017 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003018
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003019 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003020 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003021 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003022 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003023 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003024
Jason Ekstranda85118c2013-06-27 20:17:02 -05003025 surface->resource =
3026 wl_resource_create(client, &wl_surface_interface,
3027 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003028 if (surface->resource == NULL) {
3029 weston_surface_destroy(surface);
3030 wl_resource_post_no_memory(resource);
3031 return;
3032 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003033 wl_resource_set_implementation(surface->resource, &surface_interface,
3034 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003035
3036 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003037}
3038
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003039static void
3040destroy_region(struct wl_resource *resource)
3041{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003042 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003043
3044 pixman_region32_fini(&region->region);
3045 free(region);
3046}
3047
3048static void
3049region_destroy(struct wl_client *client, struct wl_resource *resource)
3050{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003051 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003052}
3053
3054static void
3055region_add(struct wl_client *client, struct wl_resource *resource,
3056 int32_t x, int32_t y, int32_t width, int32_t height)
3057{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003058 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003059
3060 pixman_region32_union_rect(&region->region, &region->region,
3061 x, y, width, height);
3062}
3063
3064static void
3065region_subtract(struct wl_client *client, struct wl_resource *resource,
3066 int32_t x, int32_t y, int32_t width, int32_t height)
3067{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003068 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003069 pixman_region32_t rect;
3070
3071 pixman_region32_init_rect(&rect, x, y, width, height);
3072 pixman_region32_subtract(&region->region, &region->region, &rect);
3073 pixman_region32_fini(&rect);
3074}
3075
3076static const struct wl_region_interface region_interface = {
3077 region_destroy,
3078 region_add,
3079 region_subtract
3080};
3081
3082static void
3083compositor_create_region(struct wl_client *client,
3084 struct wl_resource *resource, uint32_t id)
3085{
3086 struct weston_region *region;
3087
3088 region = malloc(sizeof *region);
3089 if (region == NULL) {
3090 wl_resource_post_no_memory(resource);
3091 return;
3092 }
3093
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003094 pixman_region32_init(&region->region);
3095
Jason Ekstranda85118c2013-06-27 20:17:02 -05003096 region->resource =
3097 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003098 if (region->resource == NULL) {
3099 free(region);
3100 wl_resource_post_no_memory(resource);
3101 return;
3102 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003103 wl_resource_set_implementation(region->resource, &region_interface,
3104 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003105}
3106
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003107static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003108 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003109 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003110};
3111
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003112static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003113weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3114{
3115 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003116
Jason Ekstrand7b982072014-05-20 14:33:03 -05003117 weston_surface_commit_state(surface, &sub->cached);
3118 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003119
3120 weston_surface_commit_subsurface_order(surface);
3121
3122 weston_surface_schedule_repaint(surface);
3123
Jason Ekstrand7b982072014-05-20 14:33:03 -05003124 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003125}
3126
3127static void
3128weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3129{
3130 struct weston_surface *surface = sub->surface;
3131
3132 /*
3133 * If this commit would cause the surface to move by the
3134 * attach(dx, dy) parameters, the old damage region must be
3135 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003136 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003137 */
Derek Foreman152254b2015-11-26 14:17:48 -06003138 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003139 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003140 pixman_region32_union(&sub->cached.damage_surface,
3141 &sub->cached.damage_surface,
3142 &surface->pending.damage_surface);
3143 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003144
3145 if (surface->pending.newly_attached) {
3146 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003147 weston_surface_state_set_buffer(&sub->cached,
3148 surface->pending.buffer);
3149 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003150 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003151 weston_presentation_feedback_discard_list(
3152 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003153 }
3154 sub->cached.sx += surface->pending.sx;
3155 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003156
Derek Foreman152254b2015-11-26 14:17:48 -06003157 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3158
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003159 sub->cached.buffer_viewport.changed |=
3160 surface->pending.buffer_viewport.changed;
3161 sub->cached.buffer_viewport.buffer =
3162 surface->pending.buffer_viewport.buffer;
3163 sub->cached.buffer_viewport.surface =
3164 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003165
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003166 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003167
3168 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3169
3170 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3171
3172 wl_list_insert_list(&sub->cached.frame_callback_list,
3173 &surface->pending.frame_callback_list);
3174 wl_list_init(&surface->pending.frame_callback_list);
3175
Pekka Paalanen133e4392014-09-23 22:08:46 -04003176 wl_list_insert_list(&sub->cached.feedback_list,
3177 &surface->pending.feedback_list);
3178 wl_list_init(&surface->pending.feedback_list);
3179
Jason Ekstrand7b982072014-05-20 14:33:03 -05003180 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003181}
3182
Derek Foreman280e7dd2014-10-03 13:13:42 -05003183static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003184weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3185{
3186 while (sub) {
3187 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003188 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189
3190 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003191 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003192
3193 sub = weston_surface_to_subsurface(sub->parent);
3194 }
3195
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003196 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003197}
3198
3199static void
3200weston_subsurface_commit(struct weston_subsurface *sub)
3201{
3202 struct weston_surface *surface = sub->surface;
3203 struct weston_subsurface *tmp;
3204
3205 /* Recursive check for effectively synchronized. */
3206 if (weston_subsurface_is_synchronized(sub)) {
3207 weston_subsurface_commit_to_cache(sub);
3208 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003209 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003210 /* flush accumulated state from cache */
3211 weston_subsurface_commit_to_cache(sub);
3212 weston_subsurface_commit_from_cache(sub);
3213 } else {
3214 weston_surface_commit(surface);
3215 }
3216
3217 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3218 if (tmp->surface != surface)
3219 weston_subsurface_parent_commit(tmp, 0);
3220 }
3221 }
3222}
3223
3224static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003225weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003226{
3227 struct weston_surface *surface = sub->surface;
3228 struct weston_subsurface *tmp;
3229
Pekka Paalanene67858b2013-04-25 13:57:42 +03003230 /* From now on, commit_from_cache the whole sub-tree, regardless of
3231 * the synchronized mode of each child. This sub-surface or some
3232 * of its ancestors were synchronized, so we are synchronized
3233 * all the way down.
3234 */
3235
Jason Ekstrand7b982072014-05-20 14:33:03 -05003236 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003237 weston_subsurface_commit_from_cache(sub);
3238
3239 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3240 if (tmp->surface != surface)
3241 weston_subsurface_parent_commit(tmp, 1);
3242 }
3243}
3244
3245static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003246weston_subsurface_parent_commit(struct weston_subsurface *sub,
3247 int parent_is_synchronized)
3248{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003249 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003250 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003251 wl_list_for_each(view, &sub->surface->views, surface_link)
3252 weston_view_set_position(view,
3253 sub->position.x,
3254 sub->position.y);
3255
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003256 sub->position.set = 0;
3257 }
3258
3259 if (parent_is_synchronized || sub->synchronized)
3260 weston_subsurface_synchronized_commit(sub);
3261}
3262
Pekka Paalanen8274d902014-08-06 19:36:51 +03003263static int
3264subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3265{
3266 return snprintf(buf, len, "sub-surface");
3267}
3268
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003269static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003270subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003271{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003272 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003273
Jason Ekstranda7af7042013-10-12 22:38:11 -05003274 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003275 weston_view_set_position(view,
3276 view->geometry.x + dx,
3277 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003278
3279 /* No need to check parent mappedness, because if parent is not
3280 * mapped, parent is not in a visible layer, so this sub-surface
3281 * will not be drawn either.
3282 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003283
Pekka Paalanene67858b2013-04-25 13:57:42 +03003284 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003285 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003286
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003287 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003288 * because that would call it also for the parent surface,
3289 * which might not be mapped yet. That would lead to
3290 * inconsistent state, where the window could never be
3291 * mapped.
3292 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003293 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003294 * weston_surface_is_mapped() return true, so that when the
3295 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003296 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003297 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003298 }
3299}
3300
3301static struct weston_subsurface *
3302weston_surface_to_subsurface(struct weston_surface *surface)
3303{
3304 if (surface->configure == subsurface_configure)
3305 return surface->configure_private;
3306
3307 return NULL;
3308}
3309
Pekka Paalanen01388e22013-04-25 13:57:44 +03003310WL_EXPORT struct weston_surface *
3311weston_surface_get_main_surface(struct weston_surface *surface)
3312{
3313 struct weston_subsurface *sub;
3314
3315 while (surface && (sub = weston_surface_to_subsurface(surface)))
3316 surface = sub->parent;
3317
3318 return surface;
3319}
3320
Pekka Paalanen50b67472014-10-01 15:02:41 +03003321WL_EXPORT int
3322weston_surface_set_role(struct weston_surface *surface,
3323 const char *role_name,
3324 struct wl_resource *error_resource,
3325 uint32_t error_code)
3326{
3327 assert(role_name);
3328
3329 if (surface->role_name == NULL ||
3330 surface->role_name == role_name ||
3331 strcmp(surface->role_name, role_name) == 0) {
3332 surface->role_name = role_name;
3333
3334 return 0;
3335 }
3336
3337 wl_resource_post_error(error_resource, error_code,
3338 "Cannot assign role %s to wl_surface@%d,"
3339 " already has role %s\n",
3340 role_name,
3341 wl_resource_get_id(surface->resource),
3342 surface->role_name);
3343 return -1;
3344}
3345
Pekka Paalanen8274d902014-08-06 19:36:51 +03003346WL_EXPORT void
3347weston_surface_set_label_func(struct weston_surface *surface,
3348 int (*desc)(struct weston_surface *,
3349 char *, size_t))
3350{
3351 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003352 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003353}
3354
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003355/** Get the size of surface contents
3356 *
3357 * \param surface The surface to query.
3358 * \param width Returns the width of raw contents.
3359 * \param height Returns the height of raw contents.
3360 *
3361 * Retrieves the raw surface content size in pixels for the given surface.
3362 * This is the whole content size in buffer pixels. If the surface
3363 * has no content or the renderer does not implement this feature,
3364 * zeroes are returned.
3365 *
3366 * This function is used to determine the buffer size needed for
3367 * a weston_surface_copy_content() call.
3368 */
3369WL_EXPORT void
3370weston_surface_get_content_size(struct weston_surface *surface,
3371 int *width, int *height)
3372{
3373 struct weston_renderer *rer = surface->compositor->renderer;
3374
3375 if (!rer->surface_get_content_size) {
3376 *width = 0;
3377 *height = 0;
3378 return;
3379 }
3380
3381 rer->surface_get_content_size(surface, width, height);
3382}
3383
3384/** Copy surface contents to system memory.
3385 *
3386 * \param surface The surface to copy from.
3387 * \param target Pointer to the target memory buffer.
3388 * \param size Size of the target buffer in bytes.
3389 * \param src_x X location on contents to copy from.
3390 * \param src_y Y location on contents to copy from.
3391 * \param width Width in pixels of the area to copy.
3392 * \param height Height in pixels of the area to copy.
3393 * \return 0 for success, -1 for failure.
3394 *
3395 * Surface contents are maintained by the renderer. They can be in a
3396 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3397 * else.
3398 *
3399 * Surface contents are copied into memory pointed to by target,
3400 * which has size bytes of space available. The target memory
3401 * may be larger than needed, but being smaller returns an error.
3402 * The extra bytes in target may or may not be written; their content is
3403 * unspecified. Size must be large enough to hold the image.
3404 *
3405 * The image in the target memory will be arranged in rows from
3406 * top to bottom, and pixels on a row from left to right. The pixel
3407 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3408 * width * 4.
3409 *
3410 * Parameters src_x and src_y define the upper-left corner in buffer
3411 * coordinates (pixels) to copy from. Parameters width and height
3412 * define the size of the area to copy in pixels.
3413 *
3414 * The rectangle defined by src_x, src_y, width, height must fit in
3415 * the surface contents. Otherwise an error is returned.
3416 *
3417 * Use surface_get_data_size to determine the content size; the
3418 * needed target buffer size and rectangle limits.
3419 *
3420 * CURRENT IMPLEMENTATION RESTRICTIONS:
3421 * - the machine must be little-endian due to Pixman formats.
3422 *
3423 * NOTE: Pixman formats are premultiplied.
3424 */
3425WL_EXPORT int
3426weston_surface_copy_content(struct weston_surface *surface,
3427 void *target, size_t size,
3428 int src_x, int src_y,
3429 int width, int height)
3430{
3431 struct weston_renderer *rer = surface->compositor->renderer;
3432 int cw, ch;
3433 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3434
3435 if (!rer->surface_copy_content)
3436 return -1;
3437
3438 weston_surface_get_content_size(surface, &cw, &ch);
3439
3440 if (src_x < 0 || src_y < 0)
3441 return -1;
3442
3443 if (width <= 0 || height <= 0)
3444 return -1;
3445
3446 if (src_x + width > cw || src_y + height > ch)
3447 return -1;
3448
3449 if (width * bytespp * height > size)
3450 return -1;
3451
3452 return rer->surface_copy_content(surface, target, size,
3453 src_x, src_y, width, height);
3454}
3455
Pekka Paalanene67858b2013-04-25 13:57:42 +03003456static void
3457subsurface_set_position(struct wl_client *client,
3458 struct wl_resource *resource, int32_t x, int32_t y)
3459{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003460 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003461
3462 if (!sub)
3463 return;
3464
3465 sub->position.x = x;
3466 sub->position.y = y;
3467 sub->position.set = 1;
3468}
3469
3470static struct weston_subsurface *
3471subsurface_from_surface(struct weston_surface *surface)
3472{
3473 struct weston_subsurface *sub;
3474
3475 sub = weston_surface_to_subsurface(surface);
3476 if (sub)
3477 return sub;
3478
3479 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3480 if (sub->surface == surface)
3481 return sub;
3482
3483 return NULL;
3484}
3485
3486static struct weston_subsurface *
3487subsurface_sibling_check(struct weston_subsurface *sub,
3488 struct weston_surface *surface,
3489 const char *request)
3490{
3491 struct weston_subsurface *sibling;
3492
3493 sibling = subsurface_from_surface(surface);
3494
3495 if (!sibling) {
3496 wl_resource_post_error(sub->resource,
3497 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3498 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003499 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003500 return NULL;
3501 }
3502
3503 if (sibling->parent != sub->parent) {
3504 wl_resource_post_error(sub->resource,
3505 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3506 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003507 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003508 return NULL;
3509 }
3510
3511 return sibling;
3512}
3513
3514static void
3515subsurface_place_above(struct wl_client *client,
3516 struct wl_resource *resource,
3517 struct wl_resource *sibling_resource)
3518{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003519 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003520 struct weston_surface *surface =
3521 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003522 struct weston_subsurface *sibling;
3523
3524 if (!sub)
3525 return;
3526
3527 sibling = subsurface_sibling_check(sub, surface, "place_above");
3528 if (!sibling)
3529 return;
3530
3531 wl_list_remove(&sub->parent_link_pending);
3532 wl_list_insert(sibling->parent_link_pending.prev,
3533 &sub->parent_link_pending);
3534}
3535
3536static void
3537subsurface_place_below(struct wl_client *client,
3538 struct wl_resource *resource,
3539 struct wl_resource *sibling_resource)
3540{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003541 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003542 struct weston_surface *surface =
3543 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003544 struct weston_subsurface *sibling;
3545
3546 if (!sub)
3547 return;
3548
3549 sibling = subsurface_sibling_check(sub, surface, "place_below");
3550 if (!sibling)
3551 return;
3552
3553 wl_list_remove(&sub->parent_link_pending);
3554 wl_list_insert(&sibling->parent_link_pending,
3555 &sub->parent_link_pending);
3556}
3557
3558static void
3559subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3560{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003561 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003562
3563 if (sub)
3564 sub->synchronized = 1;
3565}
3566
3567static void
3568subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3569{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003570 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003571
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003572 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003573 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003574
3575 /* If sub became effectively desynchronized, flush. */
3576 if (!weston_subsurface_is_synchronized(sub))
3577 weston_subsurface_synchronized_commit(sub);
3578 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003579}
3580
3581static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003582weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3583{
3584 wl_list_remove(&sub->parent_link);
3585 wl_list_remove(&sub->parent_link_pending);
3586 wl_list_remove(&sub->parent_destroy_listener.link);
3587 sub->parent = NULL;
3588}
3589
3590static void
3591weston_subsurface_destroy(struct weston_subsurface *sub);
3592
3593static void
3594subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3595{
3596 struct weston_subsurface *sub =
3597 container_of(listener, struct weston_subsurface,
3598 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003599 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003600
3601 /* The protocol object (wl_resource) is left inert. */
3602 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003603 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003604
3605 weston_subsurface_destroy(sub);
3606}
3607
3608static void
3609subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3610{
3611 struct weston_subsurface *sub =
3612 container_of(listener, struct weston_subsurface,
3613 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003614 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003615 assert(sub->surface != sub->parent);
3616
3617 if (weston_surface_is_mapped(sub->surface))
3618 weston_surface_unmap(sub->surface);
3619
3620 weston_subsurface_unlink_parent(sub);
3621}
3622
3623static void
3624subsurface_resource_destroy(struct wl_resource *resource)
3625{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003626 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627
3628 if (sub)
3629 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003630}
3631
3632static void
3633subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3634{
3635 wl_resource_destroy(resource);
3636}
3637
3638static void
3639weston_subsurface_link_parent(struct weston_subsurface *sub,
3640 struct weston_surface *parent)
3641{
3642 sub->parent = parent;
3643 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003644 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003645 &sub->parent_destroy_listener);
3646
3647 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3648 wl_list_insert(&parent->subsurface_list_pending,
3649 &sub->parent_link_pending);
3650}
3651
3652static void
3653weston_subsurface_link_surface(struct weston_subsurface *sub,
3654 struct weston_surface *surface)
3655{
3656 sub->surface = surface;
3657 sub->surface_destroy_listener.notify =
3658 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003659 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003660 &sub->surface_destroy_listener);
3661}
3662
3663static void
3664weston_subsurface_destroy(struct weston_subsurface *sub)
3665{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003666 struct weston_view *view, *next;
3667
Pekka Paalanene67858b2013-04-25 13:57:42 +03003668 assert(sub->surface);
3669
3670 if (sub->resource) {
3671 assert(weston_surface_to_subsurface(sub->surface) == sub);
3672 assert(sub->parent_destroy_listener.notify ==
3673 subsurface_handle_parent_destroy);
3674
George Kiagiadakised04d382014-06-13 18:10:26 +02003675 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3676 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003677 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003678 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003679
Pekka Paalanene67858b2013-04-25 13:57:42 +03003680 if (sub->parent)
3681 weston_subsurface_unlink_parent(sub);
3682
Jason Ekstrand7b982072014-05-20 14:33:03 -05003683 weston_surface_state_fini(&sub->cached);
3684 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003685
3686 sub->surface->configure = NULL;
3687 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003688 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003689 } else {
3690 /* the dummy weston_subsurface for the parent itself */
3691 assert(sub->parent_destroy_listener.notify == NULL);
3692 wl_list_remove(&sub->parent_link);
3693 wl_list_remove(&sub->parent_link_pending);
3694 }
3695
3696 wl_list_remove(&sub->surface_destroy_listener.link);
3697 free(sub);
3698}
3699
3700static const struct wl_subsurface_interface subsurface_implementation = {
3701 subsurface_destroy,
3702 subsurface_set_position,
3703 subsurface_place_above,
3704 subsurface_place_below,
3705 subsurface_set_sync,
3706 subsurface_set_desync
3707};
3708
3709static struct weston_subsurface *
3710weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3711 struct weston_surface *parent)
3712{
3713 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003714 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003715
Bryce Harringtonde16d892014-11-20 22:21:57 -08003716 sub = zalloc(sizeof *sub);
3717 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003718 return NULL;
3719
Jason Ekstranda7af7042013-10-12 22:38:11 -05003720 wl_list_init(&sub->unused_views);
3721
Jason Ekstranda85118c2013-06-27 20:17:02 -05003722 sub->resource =
3723 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003724 if (!sub->resource) {
3725 free(sub);
3726 return NULL;
3727 }
3728
Jason Ekstranda85118c2013-06-27 20:17:02 -05003729 wl_resource_set_implementation(sub->resource,
3730 &subsurface_implementation,
3731 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003732 weston_subsurface_link_surface(sub, surface);
3733 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003734 weston_surface_state_init(&sub->cached);
3735 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003736 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003737
3738 return sub;
3739}
3740
3741/* Create a dummy subsurface for having the parent itself in its
3742 * sub-surface lists. Makes stacking order manipulation easy.
3743 */
3744static struct weston_subsurface *
3745weston_subsurface_create_for_parent(struct weston_surface *parent)
3746{
3747 struct weston_subsurface *sub;
3748
Bryce Harringtonde16d892014-11-20 22:21:57 -08003749 sub = zalloc(sizeof *sub);
3750 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003751 return NULL;
3752
3753 weston_subsurface_link_surface(sub, parent);
3754 sub->parent = parent;
3755 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3756 wl_list_insert(&parent->subsurface_list_pending,
3757 &sub->parent_link_pending);
3758
3759 return sub;
3760}
3761
3762static void
3763subcompositor_get_subsurface(struct wl_client *client,
3764 struct wl_resource *resource,
3765 uint32_t id,
3766 struct wl_resource *surface_resource,
3767 struct wl_resource *parent_resource)
3768{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003769 struct weston_surface *surface =
3770 wl_resource_get_user_data(surface_resource);
3771 struct weston_surface *parent =
3772 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003773 struct weston_subsurface *sub;
3774 static const char where[] = "get_subsurface: wl_subsurface@";
3775
3776 if (surface == parent) {
3777 wl_resource_post_error(resource,
3778 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3779 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003780 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003781 return;
3782 }
3783
3784 if (weston_surface_to_subsurface(surface)) {
3785 wl_resource_post_error(resource,
3786 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3787 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003788 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003789 return;
3790 }
3791
Pekka Paalanen50b67472014-10-01 15:02:41 +03003792 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3793 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003794 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003795
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003796 if (weston_surface_get_main_surface(parent) == surface) {
3797 wl_resource_post_error(resource,
3798 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3799 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003800 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003801 return;
3802 }
3803
Pekka Paalanene67858b2013-04-25 13:57:42 +03003804 /* make sure the parent is in its own list */
3805 if (wl_list_empty(&parent->subsurface_list)) {
3806 if (!weston_subsurface_create_for_parent(parent)) {
3807 wl_resource_post_no_memory(resource);
3808 return;
3809 }
3810 }
3811
3812 sub = weston_subsurface_create(id, surface, parent);
3813 if (!sub) {
3814 wl_resource_post_no_memory(resource);
3815 return;
3816 }
3817
3818 surface->configure = subsurface_configure;
3819 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003820 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003821}
3822
3823static void
3824subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3825{
3826 wl_resource_destroy(resource);
3827}
3828
3829static const struct wl_subcompositor_interface subcompositor_interface = {
3830 subcompositor_destroy,
3831 subcompositor_get_subsurface
3832};
3833
3834static void
3835bind_subcompositor(struct wl_client *client,
3836 void *data, uint32_t version, uint32_t id)
3837{
3838 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003839 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003840
Jason Ekstranda85118c2013-06-27 20:17:02 -05003841 resource =
3842 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003843 if (resource == NULL) {
3844 wl_client_post_no_memory(client);
3845 return;
3846 }
3847 wl_resource_set_implementation(resource, &subcompositor_interface,
3848 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003849}
3850
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003851/** Set a DPMS mode on all of the compositor's outputs
3852 *
3853 * \param compositor The compositor instance
3854 * \param state The DPMS state the outputs will be set to
3855 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003856static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003857weston_compositor_dpms(struct weston_compositor *compositor,
3858 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003859{
3860 struct weston_output *output;
3861
3862 wl_list_for_each(output, &compositor->output_list, link)
3863 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003864 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003865}
3866
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003867/** Restores the compositor to active status
3868 *
3869 * \param compositor The compositor instance
3870 *
3871 * If the compositor was in a sleeping mode, all outputs are powered
3872 * back on via DPMS. Otherwise if the compositor was inactive
3873 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3874 * signal will fire.
3875 *
3876 * Restarts the idle timer.
3877 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003878WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003879weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003880{
Neil Roberts8b62e202013-09-30 13:14:47 +01003881 uint32_t old_state = compositor->state;
3882
3883 /* The state needs to be changed before emitting the wake
3884 * signal because that may try to schedule a repaint which
3885 * will not work if the compositor is still sleeping */
3886 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3887
3888 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003889 case WESTON_COMPOSITOR_SLEEPING:
3890 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3891 /* fall through */
3892 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003893 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003894 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003895 /* fall through */
3896 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003897 wl_event_source_timer_update(compositor->idle_source,
3898 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003899 }
3900}
3901
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003902/** Turns off rendering and frame events for the compositor.
3903 *
3904 * \param compositor The compositor instance
3905 *
3906 * This is used for example to prevent further rendering while the
3907 * compositor is shutting down.
3908 *
3909 * \note When offscreen state is entered, outputs will be powered
3910 * back on if they were sleeping (in DPMS off mode), even though
3911 * no rendering will be performed.
3912 *
3913 * Stops the idle timer.
3914 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003915WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003916weston_compositor_offscreen(struct weston_compositor *compositor)
3917{
3918 switch (compositor->state) {
3919 case WESTON_COMPOSITOR_OFFSCREEN:
3920 return;
3921 case WESTON_COMPOSITOR_SLEEPING:
3922 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3923 /* fall through */
3924 default:
3925 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3926 wl_event_source_timer_update(compositor->idle_source, 0);
3927 }
3928}
3929
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003930/** Powers down all attached output devices
3931 *
3932 * \param compositor The compositor instance
3933 *
3934 * Causes rendering to the outputs to cease, and no frame events to be
3935 * sent. Only powers down the outputs if the compositor is not already
3936 * in sleep mode.
3937 *
3938 * Stops the idle timer.
3939 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003940WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003941weston_compositor_sleep(struct weston_compositor *compositor)
3942{
3943 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3944 return;
3945
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003946 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003947 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3948 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3949}
3950
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003951/** Sets compositor to idle mode
3952 *
3953 * \param data The compositor instance
3954 *
3955 * This is called when the idle timer fires. Once the compositor is in
3956 * idle mode it requires a wake action (e.g. via
3957 * weston_compositor_wake()) to restore it. The compositor's
3958 * idle_signal will be triggered when the idle event occurs.
3959 *
3960 * Idleness can be inhibited by setting the compositor's idle_inhibit
3961 * property.
3962 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003963static int
3964idle_handler(void *data)
3965{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003966 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003967
3968 if (compositor->idle_inhibit)
3969 return 1;
3970
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003971 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003972 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003973
3974 return 1;
3975}
3976
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003977WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003978weston_plane_init(struct weston_plane *plane,
3979 struct weston_compositor *ec,
3980 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003981{
3982 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003983 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003984 plane->x = x;
3985 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003986 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003987
3988 /* Init the link so that the call to wl_list_remove() when releasing
3989 * the plane without ever stacking doesn't lead to a crash */
3990 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003991}
3992
3993WL_EXPORT void
3994weston_plane_release(struct weston_plane *plane)
3995{
Xiong Zhang97116532013-10-23 13:58:31 +08003996 struct weston_view *view;
3997
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003998 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003999 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004000
Xiong Zhang97116532013-10-23 13:58:31 +08004001 wl_list_for_each(view, &plane->compositor->view_list, link) {
4002 if (view->plane == plane)
4003 view->plane = NULL;
4004 }
4005
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004006 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004007}
4008
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004009WL_EXPORT void
4010weston_compositor_stack_plane(struct weston_compositor *ec,
4011 struct weston_plane *plane,
4012 struct weston_plane *above)
4013{
4014 if (above)
4015 wl_list_insert(above->link.prev, &plane->link);
4016 else
4017 wl_list_insert(&ec->plane_list, &plane->link);
4018}
4019
Casey Dahlin9074db52012-04-19 22:50:09 -04004020static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004021{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004022 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004023}
4024
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004025static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004026bind_output(struct wl_client *client,
4027 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004028{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004029 struct weston_output *output = data;
4030 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004031 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004032
Jason Ekstranda85118c2013-06-27 20:17:02 -05004033 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004034 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004035 if (resource == NULL) {
4036 wl_client_post_no_memory(client);
4037 return;
4038 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004039
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004040 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05004041 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004042
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004043 wl_output_send_geometry(resource,
4044 output->x,
4045 output->y,
4046 output->mm_width,
4047 output->mm_height,
4048 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004049 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004050 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004051 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004052 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004053 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004054
4055 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004056 wl_output_send_mode(resource,
4057 mode->flags,
4058 mode->width,
4059 mode->height,
4060 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004061 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004062
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004063 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004064 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004065}
4066
David Fort0de859e2016-05-27 23:22:57 +02004067/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004068static void
David Fort0de859e2016-05-27 23:22:57 +02004069weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4070 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004071{
4072 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004073 bool start_resizing = false;
4074
4075 if (!delta_width)
4076 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004077
4078 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004079 if (output == resized_output) {
4080 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004081 continue;
4082 }
4083
David Fort0de859e2016-05-27 23:22:57 +02004084 if (start_resizing) {
4085 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004086 output->dirty = 1;
4087 }
4088 }
4089}
4090
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004091WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004092weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004093{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004094 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004095 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004096
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004097 output->destroying = 1;
4098
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004099 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08004100 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004101 weston_view_assign_output(view);
4102 }
4103
Pekka Paalanen0513a952014-05-21 16:17:27 +03004104 wl_event_source_remove(output->repaint_timer);
4105
Pekka Paalanen133e4392014-09-23 22:08:46 -04004106 weston_presentation_feedback_discard_list(&output->feedback_list);
4107
David Fort0de859e2016-05-27 23:22:57 +02004108 weston_compositor_reflow_outputs(output->compositor, output, output->width);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004109 wl_list_remove(&output->link);
4110
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004111 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004112 wl_signal_emit(&output->destroy_signal, output);
4113
Richard Hughesafe690c2013-05-02 10:10:04 +01004114 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004115 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004116 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08004117 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004118
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004119 wl_resource_for_each(resource, &output->resource_list) {
4120 wl_resource_set_destructor(resource, NULL);
4121 }
4122
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004123 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004124}
4125
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004126WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004127weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004128{
Scott Moreau850ca422012-05-21 15:21:25 -06004129 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004130
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004131 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004132 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004133
Scott Moreauccbf29d2012-02-22 14:21:41 -07004134 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004135 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004136 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004137 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004138 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004139 weston_matrix_scale(&output->matrix, magnification,
4140 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004141 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004142
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004143 switch (output->transform) {
4144 case WL_OUTPUT_TRANSFORM_FLIPPED:
4145 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4146 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4147 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4148 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4149 weston_matrix_scale(&output->matrix, -1, 1, 1);
4150 break;
4151 }
4152
4153 switch (output->transform) {
4154 default:
4155 case WL_OUTPUT_TRANSFORM_NORMAL:
4156 case WL_OUTPUT_TRANSFORM_FLIPPED:
4157 break;
4158 case WL_OUTPUT_TRANSFORM_90:
4159 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4160 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4161 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4162 break;
4163 case WL_OUTPUT_TRANSFORM_180:
4164 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4165 weston_matrix_translate(&output->matrix,
4166 -output->width, -output->height, 0);
4167 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4168 break;
4169 case WL_OUTPUT_TRANSFORM_270:
4170 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4171 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4172 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4173 break;
4174 }
4175
4176 if (output->current_scale != 1)
4177 weston_matrix_scale(&output->matrix,
4178 output->current_scale,
4179 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004180
Scott Moreauccbf29d2012-02-22 14:21:41 -07004181 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004182
4183 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004184}
4185
Scott Moreau1bad5db2012-08-18 01:04:05 -06004186static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004187weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004188{
4189 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004190 output->native_scale = scale;
4191 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004192
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004193 convert_size_by_transform_scale(&output->width, &output->height,
4194 output->current_mode->width,
4195 output->current_mode->height,
4196 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004197}
4198
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004199static void
4200weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004201{
4202 output->x = x;
4203 output->y = y;
4204
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004205 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004206 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004207 output->width,
4208 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004209}
4210
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004211WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004212weston_output_move(struct weston_output *output, int x, int y)
4213{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004214 struct wl_resource *resource;
4215
4216 output->move_x = x - output->x;
4217 output->move_y = y - output->y;
4218
4219 if (output->move_x == 0 && output->move_y == 0)
4220 return;
4221
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004222 weston_output_init_geometry(output, x, y);
4223
4224 output->dirty = 1;
4225
4226 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004227 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004228
4229 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004230 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004231 wl_output_send_geometry(resource,
4232 output->x,
4233 output->y,
4234 output->mm_width,
4235 output->mm_height,
4236 output->subpixel,
4237 output->make,
4238 output->model,
4239 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004240
FORT David8a120692016-04-26 23:34:06 +02004241 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004242 wl_output_send_done(resource);
4243 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004244}
4245
Bryce Harrington3f650b82015-12-23 11:01:58 -08004246/** Initialize a weston_output object's parameters
4247 *
4248 * \param output The weston_output object to initialize
4249 * \param c The output's compositor
4250 * \param x x coordinate for the output in global coordinate space
4251 * \param y y coordinate for the output in global coordinate space
4252 * \param mm_width Physical width of the output as reported by the backend
4253 * \param mm_height Physical height of the output as reported by the backend
4254 * \param transform Rotation of the output
4255 * \param scale Native scaling factor for the output
4256 *
4257 * Sets up the transformation, zoom, and geometry of the output using
4258 * the input properties.
4259 *
4260 * Establishes a repaint timer for the output with the relevant display
4261 * object's event loop. See output_repaint_timer_handler().
4262 *
4263 * The output is assigned an ID. Weston can support up to 32 distinct
4264 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4265 * is referred to and used to find the first available ID number, and
4266 * then this ID is marked as used in output_id_pool.
4267 *
4268 * The output is also assigned a Wayland global with the wl_output
4269 * external interface.
4270 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004271WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004272weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004273 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004274 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004275{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004276 struct wl_event_loop *loop;
4277
Bryce Harrington18e45732015-12-23 20:53:53 -08004278 /* Verify we haven't reached the limit of 32 available output IDs */
4279 assert(ffs(~c->output_id_pool) > 0);
4280
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004281 output->compositor = c;
4282 output->x = x;
4283 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004284 output->mm_width = mm_width;
4285 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004286 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004287 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004288
Alexander Larsson0b135062013-05-28 16:23:36 +02004289 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004290 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004291
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004292 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004293 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004294
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004295 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004296 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004297 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004298 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004299 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004300 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004301
Pekka Paalanen0513a952014-05-21 16:17:27 +03004302 loop = wl_display_get_event_loop(c->wl_display);
4303 output->repaint_timer = wl_event_loop_add_timer(loop,
4304 output_repaint_timer_handler, output);
4305
Bryce Harrington3f650b82015-12-23 11:01:58 -08004306 /* Invert the output id pool and look for the lowest numbered
4307 * switch (the least significant bit). Take that bit's position
4308 * as our ID, and mark it used in the compositor's output_id_pool.
4309 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004310 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004311 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004312
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004313 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004314 wl_global_create(c->wl_display, &wl_output_interface, 2,
4315 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004316}
4317
4318/** Adds an output to the compositor's output list and
4319 * send the compositor's output_created signal.
4320 *
4321 * \param compositor The compositor instance.
4322 * \param output The output to be added.
4323 */
4324WL_EXPORT void
4325weston_compositor_add_output(struct weston_compositor *compositor,
4326 struct weston_output *output)
4327{
4328 wl_list_insert(compositor->output_list.prev, &output->link);
4329 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004330}
4331
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004332WL_EXPORT void
4333weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004334 double device_x, double device_y,
4335 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004336{
Derek Foreman0f679412014-10-02 13:41:17 -05004337 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004338 device_x,
4339 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004340 0.0,
4341 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004342
Derek Foreman67a18b92015-03-24 11:36:14 -05004343 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004344
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004345 *x = p.f[0] / p.f[3];
4346 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004347}
4348
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004349static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004350destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004351{
Jonny Lamb74130762013-11-26 18:19:46 +01004352 struct weston_surface *surface =
4353 wl_resource_get_user_data(resource);
4354
Pekka Paalanen4826f872016-04-22 14:14:38 +03004355 if (!surface)
4356 return;
4357
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004358 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004359 surface->pending.buffer_viewport.buffer.src_width =
4360 wl_fixed_from_int(-1);
4361 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004362 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004363}
4364
4365static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004366viewport_destroy(struct wl_client *client,
4367 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004368{
4369 wl_resource_destroy(resource);
4370}
4371
4372static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004373viewport_set_source(struct wl_client *client,
4374 struct wl_resource *resource,
4375 wl_fixed_t src_x,
4376 wl_fixed_t src_y,
4377 wl_fixed_t src_width,
4378 wl_fixed_t src_height)
4379{
4380 struct weston_surface *surface =
4381 wl_resource_get_user_data(resource);
4382
Pekka Paalanen4826f872016-04-22 14:14:38 +03004383 if (!surface) {
4384 wl_resource_post_error(resource,
4385 WP_VIEWPORT_ERROR_NO_SURFACE,
4386 "wl_surface for this viewport is no longer exists");
4387 return;
4388 }
4389
4390 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004391 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004392
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004393 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004394 src_height == wl_fixed_from_int(-1) &&
4395 src_x == wl_fixed_from_int(-1) &&
4396 src_y == wl_fixed_from_int(-1)) {
4397 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004398 surface->pending.buffer_viewport.buffer.src_width =
4399 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004400 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004401 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004402 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004403
Pekka Paalanen201769a2016-04-26 14:42:11 +03004404 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004405 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004406 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03004407 "wl_surface@%d viewport source "
4408 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4409 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004410 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03004411 wl_fixed_to_double(src_height),
4412 wl_fixed_to_double(src_x),
4413 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004414 return;
4415 }
4416
4417 surface->pending.buffer_viewport.buffer.src_x = src_x;
4418 surface->pending.buffer_viewport.buffer.src_y = src_y;
4419 surface->pending.buffer_viewport.buffer.src_width = src_width;
4420 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004421 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004422}
4423
4424static void
4425viewport_set_destination(struct wl_client *client,
4426 struct wl_resource *resource,
4427 int32_t dst_width,
4428 int32_t dst_height)
4429{
4430 struct weston_surface *surface =
4431 wl_resource_get_user_data(resource);
4432
Pekka Paalanen4826f872016-04-22 14:14:38 +03004433 if (!surface) {
4434 wl_resource_post_error(resource,
4435 WP_VIEWPORT_ERROR_NO_SURFACE,
4436 "wl_surface for this viewport no longer exists");
4437 return;
4438 }
4439
4440 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004441
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004442 if (dst_width == -1 && dst_height == -1) {
4443 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004444 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004445 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004446 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004447 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004448
4449 if (dst_width <= 0 || dst_height <= 0) {
4450 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004451 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004452 "destination size must be positive (%dx%d)",
4453 dst_width, dst_height);
4454 return;
4455 }
4456
4457 surface->pending.buffer_viewport.surface.width = dst_width;
4458 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004459 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004460}
4461
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004462static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004463 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004464 viewport_set_source,
4465 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004466};
4467
4468static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004469viewporter_destroy(struct wl_client *client,
4470 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004471{
4472 wl_resource_destroy(resource);
4473}
4474
4475static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004476viewporter_get_viewport(struct wl_client *client,
4477 struct wl_resource *viewporter,
4478 uint32_t id,
4479 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004480{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004481 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004482 struct weston_surface *surface =
4483 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004484 struct wl_resource *resource;
4485
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004486 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004487 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004488 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004489 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004490 return;
4491 }
4492
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004493 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004494 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004495 if (resource == NULL) {
4496 wl_client_post_no_memory(client);
4497 return;
4498 }
4499
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004500 wl_resource_set_implementation(resource, &viewport_interface,
4501 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004502
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004503 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004504}
4505
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004506static const struct wp_viewporter_interface viewporter_interface = {
4507 viewporter_destroy,
4508 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004509};
4510
4511static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004512bind_viewporter(struct wl_client *client,
4513 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004514{
4515 struct wl_resource *resource;
4516
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004517 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004518 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004519 if (resource == NULL) {
4520 wl_client_post_no_memory(client);
4521 return;
4522 }
4523
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004524 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004525 NULL, NULL);
4526}
4527
4528static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004529destroy_presentation_feedback(struct wl_resource *feedback_resource)
4530{
4531 struct weston_presentation_feedback *feedback;
4532
4533 feedback = wl_resource_get_user_data(feedback_resource);
4534
4535 wl_list_remove(&feedback->link);
4536 free(feedback);
4537}
4538
4539static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004540presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4541{
4542 wl_resource_destroy(resource);
4543}
4544
4545static void
4546presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004547 struct wl_resource *presentation_resource,
4548 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004549 uint32_t callback)
4550{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004551 struct weston_surface *surface;
4552 struct weston_presentation_feedback *feedback;
4553
4554 surface = wl_resource_get_user_data(surface_resource);
4555
Bryce Harringtonde16d892014-11-20 22:21:57 -08004556 feedback = zalloc(sizeof *feedback);
4557 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004558 goto err_calloc;
4559
4560 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004561 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004562 1, callback);
4563 if (!feedback->resource)
4564 goto err_create;
4565
4566 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4567 destroy_presentation_feedback);
4568
4569 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4570
4571 return;
4572
4573err_create:
4574 free(feedback);
4575
4576err_calloc:
4577 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004578}
4579
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004580static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004581 presentation_destroy,
4582 presentation_feedback
4583};
4584
4585static void
4586bind_presentation(struct wl_client *client,
4587 void *data, uint32_t version, uint32_t id)
4588{
4589 struct weston_compositor *compositor = data;
4590 struct wl_resource *resource;
4591
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004592 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004593 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004594 if (resource == NULL) {
4595 wl_client_post_no_memory(client);
4596 return;
4597 }
4598
4599 wl_resource_set_implementation(resource, &presentation_implementation,
4600 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004601 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004602}
4603
4604static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004605compositor_bind(struct wl_client *client,
4606 void *data, uint32_t version, uint32_t id)
4607{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004608 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004609 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004610
Jason Ekstranda85118c2013-06-27 20:17:02 -05004611 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004612 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004613 if (resource == NULL) {
4614 wl_client_post_no_memory(client);
4615 return;
4616 }
4617
4618 wl_resource_set_implementation(resource, &compositor_interface,
4619 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004620}
4621
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004622WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004623weston_environment_get_fd(const char *env)
4624{
4625 char *e, *end;
4626 int fd, flags;
4627
4628 e = getenv(env);
4629 if (!e)
4630 return -1;
Bryce Harrington375759e2016-07-12 16:51:27 -07004631 fd = strtol(e, &end, 10);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004632 if (*end != '\0')
4633 return -1;
4634
4635 flags = fcntl(fd, F_GETFD);
4636 if (flags == -1)
4637 return -1;
4638
4639 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4640 unsetenv(env);
4641
4642 return fd;
4643}
4644
Pekka Paalanenb5026542014-11-12 15:09:24 +02004645static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004646timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004647 uint32_t key, void *data)
4648{
4649 struct weston_compositor *compositor = data;
4650
4651 if (weston_timeline_enabled_)
4652 weston_timeline_close();
4653 else
4654 weston_timeline_open(compositor);
4655}
4656
Giulio Camuffo459137b2014-10-11 23:56:24 +03004657/** Create the compositor.
4658 *
4659 * This functions creates and initializes a compositor instance.
4660 *
4661 * \param display The Wayland display to be used.
4662 * \param user_data A pointer to an object that can later be retrieved
4663 * using the \ref weston_compositor_get_user_data function.
4664 * \return The compositor instance on success or NULL on failure.
4665 */
4666WL_EXPORT struct weston_compositor *
4667weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004668{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004669 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004670 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004671
Giulio Camuffo459137b2014-10-11 23:56:24 +03004672 ec = zalloc(sizeof *ec);
4673 if (!ec)
4674 return NULL;
4675
4676 ec->wl_display = display;
4677 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004678 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004679 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004680 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004681 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004682 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004683 wl_signal_init(&ec->idle_signal);
4684 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004685 wl_signal_init(&ec->show_input_panel_signal);
4686 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004687 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004688 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004689 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004690 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004691 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004692 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004693 wl_signal_init(&ec->session_signal);
4694 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004695
Casey Dahlin58ba1372012-04-19 22:50:08 -04004696 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004697 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004698
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02004699 ec->activate_serial = 1;
4700
Derek Foreman152254b2015-11-26 14:17:48 -06004701 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004702 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004703 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004704
Giulio Camuffo954f1832014-10-11 18:27:30 +03004705 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004706 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004707 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004708
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004709 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004710 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004711 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004712
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004713 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004714 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004715 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004716
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004717 if (weston_input_init(ec) != 0)
4718 goto fail;
4719
Jason Ekstranda7af7042013-10-12 22:38:11 -05004720 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004721 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004722 wl_list_init(&ec->layer_list);
4723 wl_list_init(&ec->seat_list);
4724 wl_list_init(&ec->output_list);
4725 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004726 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004727 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004728 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004729 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004730 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004731
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004732 wl_list_init(&ec->plugin_api_list);
4733
Xiong Zhang97116532013-10-23 13:58:31 +08004734 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004735 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004736
Giulio Camuffo459137b2014-10-11 23:56:24 +03004737 wl_data_device_manager_init(ec->wl_display);
4738
4739 wl_display_init_shm(ec->wl_display);
4740
4741 loop = wl_display_get_event_loop(ec->wl_display);
4742 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004743
Giulio Camuffo459137b2014-10-11 23:56:24 +03004744 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4745 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4746
4747 weston_compositor_add_debug_binding(ec, KEY_T,
4748 timeline_key_binding_handler, ec);
4749
Giulio Camuffo459137b2014-10-11 23:56:24 +03004750 return ec;
4751
4752fail:
4753 free(ec);
4754 return NULL;
4755}
4756
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004757WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004758weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004759{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004760 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004761
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004762 wl_event_source_remove(ec->idle_source);
4763
Matt Roper361d2ad2011-08-29 13:52:23 -07004764 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004765 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004766 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004767
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004768 if (ec->renderer)
4769 ec->renderer->destroy(ec);
4770
Daniel Stone325fc2d2012-05-30 16:31:58 +01004771 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004772 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004773 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004774 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004775 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004776 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004777
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004778 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004779}
4780
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004781WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004782weston_compositor_exit_with_code(struct weston_compositor *compositor,
4783 int exit_code)
4784{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004785 if (compositor->exit_code == EXIT_SUCCESS)
4786 compositor->exit_code = exit_code;
4787
Giulio Camuffo459137b2014-10-11 23:56:24 +03004788 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004789}
4790
4791WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004792weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4793 const struct weston_pointer_grab_interface *interface)
4794{
4795 struct weston_seat *seat;
4796
4797 ec->default_pointer_grab = interface;
4798 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004799 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4800
4801 if (pointer)
4802 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004803 }
4804}
4805
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004806WL_EXPORT int
4807weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4808 clockid_t clk_id)
4809{
4810 struct timespec ts;
4811
4812 if (clock_gettime(clk_id, &ts) < 0)
4813 return -1;
4814
4815 compositor->presentation_clock = clk_id;
4816
4817 return 0;
4818}
4819
4820/*
4821 * For choosing the software clock, when the display hardware or API
4822 * does not expose a compatible presentation timestamp.
4823 */
4824WL_EXPORT int
4825weston_compositor_set_presentation_clock_software(
4826 struct weston_compositor *compositor)
4827{
4828 /* In order of preference */
4829 static const clockid_t clocks[] = {
4830 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4831 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4832 CLOCK_MONOTONIC, /* no jumps, may crawl */
4833 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4834 CLOCK_REALTIME /* may jump and crawl */
4835 };
4836 unsigned i;
4837
4838 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4839 if (weston_compositor_set_presentation_clock(compositor,
4840 clocks[i]) == 0)
4841 return 0;
4842
4843 weston_log("Error: no suitable presentation clock available.\n");
4844
4845 return -1;
4846}
4847
Pekka Paalanen662f3842015-03-18 12:17:26 +02004848/** Read the current time from the Presentation clock
4849 *
4850 * \param compositor
4851 * \param ts[out] The current time.
4852 *
4853 * \note Reading the current time in user space is always imprecise to some
4854 * degree.
4855 *
4856 * This function is never meant to fail. If reading the clock does fail,
4857 * an error message is logged and a zero time is returned. Callers are not
4858 * supposed to detect or react to failures.
4859 */
4860WL_EXPORT void
4861weston_compositor_read_presentation_clock(
4862 const struct weston_compositor *compositor,
4863 struct timespec *ts)
4864{
4865 static bool warned;
4866 int ret;
4867
4868 ret = clock_gettime(compositor->presentation_clock, ts);
4869 if (ret < 0) {
4870 ts->tv_sec = 0;
4871 ts->tv_nsec = 0;
4872
4873 if (!warned)
4874 weston_log("Error: failure to read "
4875 "the presentation clock %#x: '%m' (%d)\n",
4876 compositor->presentation_clock, errno);
4877 warned = true;
4878 }
4879}
4880
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004881/** Import dmabuf buffer into current renderer
4882 *
4883 * \param compositor
4884 * \param buffer the dmabuf buffer to import
4885 * \return true on usable buffers, false otherwise
4886 *
4887 * This function tests that the linux_dmabuf_buffer is usable
4888 * for the current renderer. Returns false on unusable buffers. Usually
4889 * usability is tested by importing the dmabufs for composition.
4890 *
4891 * This hook is also used for detecting if the renderer supports
4892 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4893 * supported.
4894 * */
4895WL_EXPORT bool
4896weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4897 struct linux_dmabuf_buffer *buffer)
4898{
4899 struct weston_renderer *renderer;
4900
4901 renderer = compositor->renderer;
4902
4903 if (renderer->import_dmabuf == NULL)
4904 return false;
4905
4906 return renderer->import_dmabuf(compositor, buffer);
4907}
4908
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004909WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004910weston_version(int *major, int *minor, int *micro)
4911{
4912 *major = WESTON_VERSION_MAJOR;
4913 *minor = WESTON_VERSION_MINOR;
4914 *micro = WESTON_VERSION_MICRO;
4915}
4916
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004917WL_EXPORT void *
4918weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004919{
Derek Foreman3f86e502015-06-08 11:46:54 -05004920 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004921 char path[PATH_MAX];
4922 void *module, *init;
4923
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004924 if (name == NULL)
4925 return NULL;
4926
Derek Foreman3f86e502015-06-08 11:46:54 -05004927 if (name[0] != '/') {
4928 if (builddir)
4929 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4930 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03004931 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004932 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004933 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004934 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004935
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004936 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4937 if (module) {
4938 weston_log("Module '%s' already loaded\n", path);
4939 dlclose(module);
4940 return NULL;
4941 }
4942
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004943 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004944 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004945 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004946 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004947 return NULL;
4948 }
4949
4950 init = dlsym(module, entrypoint);
4951 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004952 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004953 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004954 return NULL;
4955 }
4956
4957 return init;
4958}
4959
Giulio Camuffo459137b2014-10-11 23:56:24 +03004960
4961/** Destroys the compositor.
4962 *
4963 * This function cleans up the compositor state and destroys it.
4964 *
4965 * \param compositor The compositor to be destroyed.
4966 */
4967WL_EXPORT void
4968weston_compositor_destroy(struct weston_compositor *compositor)
4969{
4970 /* prevent further rendering while shutting down */
4971 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4972
4973 wl_signal_emit(&compositor->destroy_signal, compositor);
4974
4975 weston_compositor_xkb_destroy(compositor);
4976
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004977 if (compositor->backend)
4978 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004979
4980 weston_plugin_api_destroy_list(compositor);
4981
Giulio Camuffo459137b2014-10-11 23:56:24 +03004982 free(compositor);
4983}
4984
4985/** Instruct the compositor to exit.
4986 *
4987 * This functions does not directly destroy the compositor object, it merely
4988 * command it to start the tear down process. It is not guaranteed that the
4989 * tear down will happen immediately.
4990 *
4991 * \param compositor The compositor to tear down.
4992 */
4993WL_EXPORT void
4994weston_compositor_exit(struct weston_compositor *compositor)
4995{
4996 compositor->exit(compositor);
4997}
4998
4999/** Return the user data stored in the compositor.
5000 *
5001 * This function returns the user data pointer set with user_data parameter
5002 * to the \ref weston_compositor_create function.
5003 */
5004WL_EXPORT void *
5005weston_compositor_get_user_data(struct weston_compositor *compositor)
5006{
5007 return compositor->user_data;
5008}
Pekka Paalanendd186732016-06-03 14:49:54 +03005009
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005010static const char * const backend_map[] = {
5011 [WESTON_BACKEND_DRM] = "drm-backend.so",
5012 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5013 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5014 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5015 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5016 [WESTON_BACKEND_X11] = "x11-backend.so",
5017};
5018
Pekka Paalanendd186732016-06-03 14:49:54 +03005019/** Load a backend into a weston_compositor
5020 *
5021 * A backend must be loaded to make a weston_compositor work. A backend
5022 * provides input and output capabilities, and determines the renderer to use.
5023 *
5024 * \param compositor A compositor that has not had a backend loaded yet.
5025 * \param backend Name of the backend file.
5026 * \param config_base A pointer to a backend-specific configuration
5027 * structure's 'base' member.
5028 *
5029 * \return 0 on success, or -1 on error.
5030 */
5031WL_EXPORT int
5032weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005033 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005034 struct weston_backend_config *config_base)
5035{
5036 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005037 struct weston_backend_config *config_base);
5038
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005039 if (backend < 0 || backend >= ARRAY_LENGTH(backend_map))
5040 return -1;
5041
5042 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005043 if (!backend_init)
5044 return -1;
5045
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005046 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005047}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005048
5049WL_EXPORT int
5050weston_compositor_load_xwayland(struct weston_compositor *compositor)
5051{
5052 int (*module_init)(struct weston_compositor *ec,
5053 int *argc, char *argv[]);
5054 int argc = 0;
5055
5056 module_init = weston_load_module("xwayland.so", "module_init");
5057 if (!module_init)
5058 return -1;
5059 if (module_init(compositor, &argc, NULL) < 0)
5060 return -1;
5061 return 0;
5062}