blob: 8123543be31a069245dfe6ddabd82c79f4c58aaf [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
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400485 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500486}
487
Alex Wu8811bf92012-02-28 18:07:54 +0800488WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500489weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200490 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500491{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100492 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500493}
494
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400495WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500496weston_view_to_global_float(struct weston_view *view,
497 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200498{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500499 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200500 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
501
Jason Ekstranda7af7042013-10-12 22:38:11 -0500502 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200503
504 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200505 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700506 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200507 v.f[3]);
508 *x = 0;
509 *y = 0;
510 return;
511 }
512
513 *x = v.f[0] / v.f[3];
514 *y = v.f[1] / v.f[3];
515 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500516 *x = sx + view->geometry.x;
517 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200518 }
519}
520
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500521WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200522weston_transformed_coord(int width, int height,
523 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200524 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200525 float sx, float sy, float *bx, float *by)
526{
527 switch (transform) {
528 case WL_OUTPUT_TRANSFORM_NORMAL:
529 default:
530 *bx = sx;
531 *by = sy;
532 break;
533 case WL_OUTPUT_TRANSFORM_FLIPPED:
534 *bx = width - sx;
535 *by = sy;
536 break;
537 case WL_OUTPUT_TRANSFORM_90:
538 *bx = height - sy;
539 *by = sx;
540 break;
541 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
542 *bx = height - sy;
543 *by = width - sx;
544 break;
545 case WL_OUTPUT_TRANSFORM_180:
546 *bx = width - sx;
547 *by = height - sy;
548 break;
549 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
550 *bx = sx;
551 *by = height - sy;
552 break;
553 case WL_OUTPUT_TRANSFORM_270:
554 *bx = sy;
555 *by = width - sx;
556 break;
557 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
558 *bx = sy;
559 *by = sx;
560 break;
561 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200562
563 *bx *= scale;
564 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200565}
566
567WL_EXPORT pixman_box32_t
568weston_transformed_rect(int width, int height,
569 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200570 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200571 pixman_box32_t rect)
572{
573 float x1, x2, y1, y2;
574
575 pixman_box32_t ret;
576
Alexander Larsson4ea95522013-05-22 14:41:37 +0200577 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200578 rect.x1, rect.y1, &x1, &y1);
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.x2, rect.y2, &x2, &y2);
581
582 if (x1 <= x2) {
583 ret.x1 = x1;
584 ret.x2 = x2;
585 } else {
586 ret.x1 = x2;
587 ret.x2 = x1;
588 }
589
590 if (y1 <= y2) {
591 ret.y1 = y1;
592 ret.y2 = y2;
593 } else {
594 ret.y1 = y2;
595 ret.y2 = y1;
596 }
597
598 return ret;
599}
600
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600601/** Transform a region by a matrix, restricted to axis-aligned transformations
602 *
603 * Warning: This function does not work for projective, affine, or matrices
604 * that encode arbitrary rotations. Only 90-degree step rotations are
605 * supported.
606 */
607WL_EXPORT void
608weston_matrix_transform_region(pixman_region32_t *dest,
609 struct weston_matrix *matrix,
610 pixman_region32_t *src)
611{
612 pixman_box32_t *src_rects, *dest_rects;
613 int nrects, i;
614
615 src_rects = pixman_region32_rectangles(src, &nrects);
616 dest_rects = malloc(nrects * sizeof(*dest_rects));
617 if (!dest_rects)
618 return;
619
620 for (i = 0; i < nrects; i++) {
621 struct weston_vector vec1 = {{
622 src_rects[i].x1, src_rects[i].y1, 0, 1
623 }};
624 weston_matrix_transform(matrix, &vec1);
625 vec1.f[0] /= vec1.f[3];
626 vec1.f[1] /= vec1.f[3];
627
628 struct weston_vector vec2 = {{
629 src_rects[i].x2, src_rects[i].y2, 0, 1
630 }};
631 weston_matrix_transform(matrix, &vec2);
632 vec2.f[0] /= vec2.f[3];
633 vec2.f[1] /= vec2.f[3];
634
635 if (vec1.f[0] < vec2.f[0]) {
636 dest_rects[i].x1 = floor(vec1.f[0]);
637 dest_rects[i].x2 = ceil(vec2.f[0]);
638 } else {
639 dest_rects[i].x1 = floor(vec2.f[0]);
640 dest_rects[i].x2 = ceil(vec1.f[0]);
641 }
642
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600643 if (vec1.f[1] < vec2.f[1]) {
644 dest_rects[i].y1 = floor(vec1.f[1]);
645 dest_rects[i].y2 = ceil(vec2.f[1]);
646 } else {
647 dest_rects[i].y1 = floor(vec2.f[1]);
648 dest_rects[i].y2 = ceil(vec1.f[1]);
649 }
650 }
651
652 pixman_region32_clear(dest);
653 pixman_region32_init_rects(dest, dest_rects, nrects);
654 free(dest_rects);
655}
656
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200657WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500658weston_transformed_region(int width, int height,
659 enum wl_output_transform transform,
660 int32_t scale,
661 pixman_region32_t *src, pixman_region32_t *dest)
662{
663 pixman_box32_t *src_rects, *dest_rects;
664 int nrects, i;
665
666 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
667 if (src != dest)
668 pixman_region32_copy(dest, src);
669 return;
670 }
671
672 src_rects = pixman_region32_rectangles(src, &nrects);
673 dest_rects = malloc(nrects * sizeof(*dest_rects));
674 if (!dest_rects)
675 return;
676
677 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
678 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
679 } else {
680 for (i = 0; i < nrects; i++) {
681 switch (transform) {
682 default:
683 case WL_OUTPUT_TRANSFORM_NORMAL:
684 dest_rects[i].x1 = src_rects[i].x1;
685 dest_rects[i].y1 = src_rects[i].y1;
686 dest_rects[i].x2 = src_rects[i].x2;
687 dest_rects[i].y2 = src_rects[i].y2;
688 break;
689 case WL_OUTPUT_TRANSFORM_90:
690 dest_rects[i].x1 = height - src_rects[i].y2;
691 dest_rects[i].y1 = src_rects[i].x1;
692 dest_rects[i].x2 = height - src_rects[i].y1;
693 dest_rects[i].y2 = src_rects[i].x2;
694 break;
695 case WL_OUTPUT_TRANSFORM_180:
696 dest_rects[i].x1 = width - src_rects[i].x2;
697 dest_rects[i].y1 = height - src_rects[i].y2;
698 dest_rects[i].x2 = width - src_rects[i].x1;
699 dest_rects[i].y2 = height - src_rects[i].y1;
700 break;
701 case WL_OUTPUT_TRANSFORM_270:
702 dest_rects[i].x1 = src_rects[i].y1;
703 dest_rects[i].y1 = width - src_rects[i].x2;
704 dest_rects[i].x2 = src_rects[i].y2;
705 dest_rects[i].y2 = width - src_rects[i].x1;
706 break;
707 case WL_OUTPUT_TRANSFORM_FLIPPED:
708 dest_rects[i].x1 = width - src_rects[i].x2;
709 dest_rects[i].y1 = src_rects[i].y1;
710 dest_rects[i].x2 = width - src_rects[i].x1;
711 dest_rects[i].y2 = src_rects[i].y2;
712 break;
713 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
714 dest_rects[i].x1 = height - src_rects[i].y2;
715 dest_rects[i].y1 = width - src_rects[i].x2;
716 dest_rects[i].x2 = height - src_rects[i].y1;
717 dest_rects[i].y2 = width - src_rects[i].x1;
718 break;
719 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
720 dest_rects[i].x1 = src_rects[i].x1;
721 dest_rects[i].y1 = height - src_rects[i].y2;
722 dest_rects[i].x2 = src_rects[i].x2;
723 dest_rects[i].y2 = height - src_rects[i].y1;
724 break;
725 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
726 dest_rects[i].x1 = src_rects[i].y1;
727 dest_rects[i].y1 = src_rects[i].x1;
728 dest_rects[i].x2 = src_rects[i].y2;
729 dest_rects[i].y2 = src_rects[i].x2;
730 break;
731 }
732 }
733 }
734
735 if (scale != 1) {
736 for (i = 0; i < nrects; i++) {
737 dest_rects[i].x1 *= scale;
738 dest_rects[i].x2 *= scale;
739 dest_rects[i].y1 *= scale;
740 dest_rects[i].y2 *= scale;
741 }
742 }
743
744 pixman_region32_clear(dest);
745 pixman_region32_init_rects(dest, dest_rects, nrects);
746 free(dest_rects);
747}
748
Jonny Lamb74130762013-11-26 18:19:46 +0100749static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300750viewport_surface_to_buffer(struct weston_surface *surface,
751 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100752{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200753 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200754 double src_width, src_height;
755 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200756
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200757 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
758 if (vp->surface.width == -1) {
759 *bx = sx;
760 *by = sy;
761 return;
762 }
Jonny Lamb74130762013-11-26 18:19:46 +0100763
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200764 src_x = 0.0;
765 src_y = 0.0;
766 src_width = surface->width_from_buffer;
767 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100768 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200769 src_x = wl_fixed_to_double(vp->buffer.src_x);
770 src_y = wl_fixed_to_double(vp->buffer.src_y);
771 src_width = wl_fixed_to_double(vp->buffer.src_width);
772 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100773 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200774
775 *bx = sx * src_width / surface->width + src_x;
776 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100777}
778
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500779WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200780weston_surface_to_buffer_float(struct weston_surface *surface,
781 float sx, float sy, float *bx, float *by)
782{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200783 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
784
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300785 /* first transform coordinates if the viewport is set */
786 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100787
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500788 weston_transformed_coord(surface->width_from_buffer,
789 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200790 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100791 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200792}
793
Derek Foreman99739672015-12-03 16:38:11 -0600794/** Transform a rectangle from surface coordinates to buffer coordinates
795 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300796 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600797 * from.
798 * \param rect The rectangle to transform.
799 * \return The transformed rectangle.
800 *
801 * Viewport and buffer transformations can only do translation, scaling,
802 * and rotations in 90-degree steps. Therefore the only loss in the
803 * conversion is coordinate rounding.
804 *
805 * However, some coordinate rounding takes place as an intermediate
806 * step before the buffer scale factor is applied, so the rectangle
807 * boundary may not be exactly as expected.
808 *
809 * This is OK for damage tracking since a little extra coverage is
810 * not a problem.
811 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200812WL_EXPORT pixman_box32_t
813weston_surface_to_buffer_rect(struct weston_surface *surface,
814 pixman_box32_t rect)
815{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200816 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100817 float xf, yf;
818
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300819 /* first transform box coordinates if the viewport is set */
820 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100821 rect.x1 = floorf(xf);
822 rect.y1 = floorf(yf);
823
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300824 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600825 rect.x2 = ceilf(xf);
826 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100827
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500828 return weston_transformed_rect(surface->width_from_buffer,
829 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200830 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200831 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200832}
833
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200834/** Transform a region from surface coordinates to buffer coordinates
835 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300836 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200837 * from.
838 * \param surface_region[in] The region in surface coordinates.
839 * \param buffer_region[out] The region converted to buffer coordinates.
840 *
841 * Buffer_region must be init'd, but will be completely overwritten.
842 *
843 * Viewport and buffer transformations can only do translation, scaling,
844 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600845 * conversion is from the coordinate rounding that takes place in
846 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200847 */
848WL_EXPORT void
849weston_surface_to_buffer_region(struct weston_surface *surface,
850 pixman_region32_t *surface_region,
851 pixman_region32_t *buffer_region)
852{
853 pixman_box32_t *src_rects, *dest_rects;
854 int nrects, i;
855
856 src_rects = pixman_region32_rectangles(surface_region, &nrects);
857 dest_rects = malloc(nrects * sizeof(*dest_rects));
858 if (!dest_rects)
859 return;
860
861 for (i = 0; i < nrects; i++) {
862 dest_rects[i] = weston_surface_to_buffer_rect(surface,
863 src_rects[i]);
864 }
865
866 pixman_region32_fini(buffer_region);
867 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
868 free(dest_rects);
869}
870
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200871WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500872weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400873 struct weston_plane *plane)
874{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500875 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400876 return;
877
Jason Ekstranda7af7042013-10-12 22:38:11 -0500878 weston_view_damage_below(view);
879 view->plane = plane;
880 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400881}
882
Pekka Paalanen51723d52015-02-17 13:10:01 +0200883/** Inflict damage on the plane where the view is visible.
884 *
885 * \param view The view that causes the damage.
886 *
887 * If the view is currently on a plane (including the primary plane),
888 * take the view's boundingbox, subtract all the opaque views that cover it,
889 * and add the remaining region as damage to the plane. This corresponds
890 * to the damage inflicted to the plane if this view disappeared.
891 *
892 * A repaint is scheduled for this view.
893 *
894 * The region of all opaque views covering this view is stored in
895 * weston_view::clip and updated by view_accumulate_damage() during
896 * weston_output_repaint(). Specifically, that region matches the
897 * scenegraph as it was last painted.
898 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400899WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500900weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200901{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500902 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200903
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500904 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200905 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500906 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800907 if (view->plane)
908 pixman_region32_union(&view->plane->damage,
909 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500910 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800911 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200912}
913
Bryce Harrington3f650b82015-12-23 11:01:58 -0800914/**
915 * \param es The surface
916 * \param mask The new set of outputs for the surface
917 *
918 * Sets the surface's set of outputs to the ones specified by
919 * the new output mask provided. Identifies the outputs that
920 * have changed, the posts enter and leave events for these
921 * outputs as appropriate.
922 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400923static void
924weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
925{
926 uint32_t different = es->output_mask ^ mask;
927 uint32_t entered = mask & different;
928 uint32_t left = es->output_mask & different;
929 struct weston_output *output;
930 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500931 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400932
933 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500934 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400935 return;
936 if (different == 0)
937 return;
938
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500939 client = wl_resource_get_client(es->resource);
940
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400941 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800942 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400943 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500944 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400945 client);
946 if (resource == NULL)
947 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800948 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500949 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800950 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500951 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400952 }
953}
954
Bryce Harrington3f650b82015-12-23 11:01:58 -0800955/** Recalculate which output(s) the surface has views displayed on
956 *
957 * \param es The surface to remap to outputs
958 *
959 * Finds the output that is showing the largest amount of one
960 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300961 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800962 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300963 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800964 * in the output_mask for the surface.
965 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400966static void
967weston_surface_assign_output(struct weston_surface *es)
968{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500969 struct weston_output *new_output;
970 struct weston_view *view;
971 pixman_region32_t region;
972 uint32_t max, area, mask;
973 pixman_box32_t *e;
974
975 new_output = NULL;
976 max = 0;
977 mask = 0;
978 pixman_region32_init(&region);
979 wl_list_for_each(view, &es->views, surface_link) {
980 if (!view->output)
981 continue;
982
983 pixman_region32_intersect(&region, &view->transform.boundingbox,
984 &view->output->region);
985
986 e = pixman_region32_extents(&region);
987 area = (e->x2 - e->x1) * (e->y2 - e->y1);
988
989 mask |= view->output_mask;
990
991 if (area >= max) {
992 new_output = view->output;
993 max = area;
994 }
995 }
996 pixman_region32_fini(&region);
997
998 es->output = new_output;
999 weston_surface_update_output_mask(es, mask);
1000}
1001
Bryce Harrington3f650b82015-12-23 11:01:58 -08001002/** Recalculate which output(s) the view is displayed on
1003 *
1004 * \param ev The view to remap to outputs
1005 *
1006 * Identifies the set of outputs that the view is visible on,
1007 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001008 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001009 *
1010 * Also does the same for the view's surface. See
1011 * weston_surface_assign_output().
1012 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013static void
1014weston_view_assign_output(struct weston_view *ev)
1015{
1016 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001017 struct weston_output *output, *new_output;
1018 pixman_region32_t region;
1019 uint32_t max, area, mask;
1020 pixman_box32_t *e;
1021
1022 new_output = NULL;
1023 max = 0;
1024 mask = 0;
1025 pixman_region32_init(&region);
1026 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001027 if (output->destroying)
1028 continue;
1029
Jason Ekstranda7af7042013-10-12 22:38:11 -05001030 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001031 &output->region);
1032
1033 e = pixman_region32_extents(&region);
1034 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1035
1036 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001037 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001038
1039 if (area >= max) {
1040 new_output = output;
1041 max = area;
1042 }
1043 }
1044 pixman_region32_fini(&region);
1045
Jason Ekstranda7af7042013-10-12 22:38:11 -05001046 ev->output = new_output;
1047 ev->output_mask = mask;
1048
1049 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001050}
1051
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001052static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001053weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1054 int from_x, int from_y, int *to_x, int *to_y)
1055{
1056 float x, y;
1057
1058 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1059 weston_view_from_global_float(to, x, y, &x, &y);
1060
1061 *to_x = round(x);
1062 *to_y = round(y);
1063}
1064
1065static void
1066weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1067{
1068 pixman_box32_t *a;
1069 pixman_box32_t b;
1070
1071 a = pixman_region32_extents(&from->geometry.scissor);
1072
1073 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1074 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1075
1076 pixman_region32_fini(&to->geometry.scissor);
1077 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1078}
1079
1080static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001081view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001083{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001084 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1085 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001086 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001087 { inbox->x1, inbox->y1 },
1088 { inbox->x1, inbox->y2 },
1089 { inbox->x2, inbox->y1 },
1090 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001091 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001092 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001093 int i;
1094
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001095 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001096 /* avoid rounding empty bbox to 1x1 */
1097 pixman_region32_init(bbox);
1098 return;
1099 }
1100
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001101 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001102 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001103 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001104 if (x < min_x)
1105 min_x = x;
1106 if (x > max_x)
1107 max_x = x;
1108 if (y < min_y)
1109 min_y = y;
1110 if (y > max_y)
1111 max_y = y;
1112 }
1113
Pekka Paalanen219b9822012-02-08 15:38:37 +02001114 int_x = floorf(min_x);
1115 int_y = floorf(min_y);
1116 pixman_region32_init_rect(bbox, int_x, int_y,
1117 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001118}
1119
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001120static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001122{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001124
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001125 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001126 view->geometry.x = roundf(view->geometry.x);
1127 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001128
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001129 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1131 view->transform.position.matrix.d[12] = view->geometry.x;
1132 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001133
Jason Ekstranda7af7042013-10-12 22:38:11 -05001134 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001135
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136 view->transform.inverse = view->transform.position.matrix;
1137 view->transform.inverse.d[12] = -view->geometry.x;
1138 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001139
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001141 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001142 view->surface->width,
1143 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001144 if (view->geometry.scissor_enabled)
1145 pixman_region32_intersect(&view->transform.boundingbox,
1146 &view->transform.boundingbox,
1147 &view->geometry.scissor);
1148
1149 pixman_region32_translate(&view->transform.boundingbox,
1150 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001151
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 if (view->alpha == 1.0) {
1153 pixman_region32_copy(&view->transform.opaque,
1154 &view->surface->opaque);
1155 pixman_region32_translate(&view->transform.opaque,
1156 view->geometry.x,
1157 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001158 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001159}
1160
1161static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001163{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 struct weston_view *parent = view->geometry.parent;
1165 struct weston_matrix *matrix = &view->transform.matrix;
1166 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001167 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001168 pixman_region32_t surfregion;
1169 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001170
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001172
1173 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1175 view->transform.position.matrix.d[12] = view->geometry.x;
1176 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001177
1178 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001179 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001180 weston_matrix_multiply(matrix, &tform->matrix);
1181
Pekka Paalanen483243f2013-03-08 14:56:50 +02001182 if (parent)
1183 weston_matrix_multiply(matrix, &parent->transform.matrix);
1184
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001185 if (weston_matrix_invert(inverse, matrix) < 0) {
1186 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 weston_log("error: weston_view %p"
1188 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001189 return -1;
1190 }
1191
Pekka Paalanen380adf52015-02-16 14:39:11 +02001192 pixman_region32_init_rect(&surfregion, 0, 0,
1193 view->surface->width, view->surface->height);
1194 if (view->geometry.scissor_enabled)
1195 pixman_region32_intersect(&surfregion, &surfregion,
1196 &view->geometry.scissor);
1197 surfbox = pixman_region32_extents(&surfregion);
1198
1199 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1200 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001201
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001202 return 0;
1203}
1204
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001205static struct weston_layer *
1206get_view_layer(struct weston_view *view)
1207{
1208 if (view->parent_view)
1209 return get_view_layer(view->parent_view);
1210 return view->layer_link.layer;
1211}
1212
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001213WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001215{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001217 struct weston_layer *layer;
1218 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001219
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001221 return;
1222
Pekka Paalanen483243f2013-03-08 14:56:50 +02001223 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001225
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001229
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 pixman_region32_fini(&view->transform.boundingbox);
1231 pixman_region32_fini(&view->transform.opaque);
1232 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001233
Pekka Paalanencd403622012-01-25 13:37:39 +02001234 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 if (view->geometry.transformation_list.next ==
1236 &view->transform.position.link &&
1237 view->geometry.transformation_list.prev ==
1238 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001239 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001241 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 if (weston_view_update_transform_enable(view) < 0)
1243 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001244 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001245
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001246 layer = get_view_layer(view);
1247 if (layer) {
1248 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001249 pixman_region32_intersect(&view->transform.boundingbox,
1250 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001251 pixman_region32_intersect(&view->transform.opaque,
1252 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001253 pixman_region32_fini(&mask);
1254 }
1255
Pekka Paalanen380adf52015-02-16 14:39:11 +02001256 if (parent) {
1257 if (parent->geometry.scissor_enabled) {
1258 view->geometry.scissor_enabled = true;
1259 weston_view_transfer_scissor(parent, view);
1260 } else {
1261 view->geometry.scissor_enabled = false;
1262 }
1263 }
1264
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001266
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001268
Jason Ekstranda7af7042013-10-12 22:38:11 -05001269 wl_signal_emit(&view->surface->compositor->transform_signal,
1270 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001271}
1272
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001273WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001275{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001277
1278 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 * The invariant: if view->geometry.dirty, then all views
1280 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001281 * Corollary: if not parent->geometry.dirty, then all ancestors
1282 * are not dirty.
1283 */
1284
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001286 return;
1287
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001289
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001291 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001293}
1294
1295WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296weston_view_to_global_fixed(struct weston_view *view,
1297 wl_fixed_t vx, wl_fixed_t vy,
1298 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001299{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001300 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001301
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 weston_view_to_global_float(view,
1303 wl_fixed_to_double(vx),
1304 wl_fixed_to_double(vy),
1305 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001306 *x = wl_fixed_from_double(xf);
1307 *y = wl_fixed_from_double(yf);
1308}
1309
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001310WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311weston_view_from_global_float(struct weston_view *view,
1312 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001313{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001315 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1316
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001318
1319 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001320 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001322 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323 *vx = 0;
1324 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001325 return;
1326 }
1327
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 *vx = v.f[0] / v.f[3];
1329 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001330 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001331 *vx = x - view->geometry.x;
1332 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001333 }
1334}
1335
1336WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337weston_view_from_global_fixed(struct weston_view *view,
1338 wl_fixed_t x, wl_fixed_t y,
1339 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001340{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001342
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 weston_view_from_global_float(view,
1344 wl_fixed_to_double(x),
1345 wl_fixed_to_double(y),
1346 &vxf, &vyf);
1347 *vx = wl_fixed_from_double(vxf);
1348 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001349}
1350
1351WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001352weston_view_from_global(struct weston_view *view,
1353 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001354{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001356
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1358 *vx = floorf(vxf);
1359 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001360}
1361
Bryce Harrington3f650b82015-12-23 11:01:58 -08001362/**
1363 * \param surface The surface to be repainted
1364 *
1365 * Marks the output(s) that the surface is shown on as needing to be
1366 * repainted. See weston_output_schedule_repaint().
1367 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001368WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001369weston_surface_schedule_repaint(struct weston_surface *surface)
1370{
1371 struct weston_output *output;
1372
1373 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001374 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001375 weston_output_schedule_repaint(output);
1376}
1377
Bryce Harrington3f650b82015-12-23 11:01:58 -08001378/**
1379 * \param view The view to be repainted
1380 *
1381 * Marks the output(s) that the view is shown on as needing to be
1382 * repainted. See weston_output_schedule_repaint().
1383 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001384WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385weston_view_schedule_repaint(struct weston_view *view)
1386{
1387 struct weston_output *output;
1388
1389 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001390 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 weston_output_schedule_repaint(output);
1392}
1393
Pekka Paalanene508ce62015-02-19 13:59:55 +02001394/**
1395 * XXX: This function does it the wrong way.
1396 * surface->damage is the damage from the client, and causes
1397 * surface_flush_damage() to copy pixels. No window management action can
1398 * cause damage to the client-provided content, warranting re-upload!
1399 *
1400 * Instead of surface->damage, this function should record the damage
1401 * with all the views for this surface to avoid extraneous texture
1402 * uploads.
1403 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001405weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001406{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001407 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408 0, 0, surface->width,
1409 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001410
Kristian Høgsberg98238702012-08-03 16:29:12 -04001411 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001412}
1413
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001414WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001416{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001417 if (view->geometry.x == x && view->geometry.y == y)
1418 return;
1419
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 view->geometry.x = x;
1421 view->geometry.y = y;
1422 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001423}
1424
Pekka Paalanen483243f2013-03-08 14:56:50 +02001425static void
1426transform_parent_handle_parent_destroy(struct wl_listener *listener,
1427 void *data)
1428{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429 struct weston_view *view =
1430 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001431 geometry.parent_destroy_listener);
1432
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001434}
1435
1436WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001438 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001439{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 if (view->geometry.parent) {
1441 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1442 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001443
1444 if (!parent)
1445 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001446 }
1447
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001451 transform_parent_handle_parent_destroy;
1452 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001453 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001455 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001457 }
1458
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459 weston_view_geometry_dirty(view);
1460}
1461
Pekka Paalanen380adf52015-02-16 14:39:11 +02001462/** Set a clip mask rectangle on a view
1463 *
1464 * \param view The view to set the clip mask on.
1465 * \param x Top-left corner X coordinate of the clip rectangle.
1466 * \param y Top-left corner Y coordinate of the clip rectangle.
1467 * \param width Width of the clip rectangle, non-negative.
1468 * \param height Height of the clip rectangle, non-negative.
1469 *
1470 * A shell may set a clip mask rectangle on a view. Everything outside
1471 * the rectangle is cut away for input and output purposes: it is
1472 * not drawn and cannot be hit by hit-test based input like pointer
1473 * motion or touch-downs. Everything inside the rectangle will behave
1474 * normally. Clients are unaware of clipping.
1475 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001476 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001477 * mask rectangle does not affect the view position, the view is positioned
1478 * as it would be without a clip. The clip also does not change
1479 * weston_surface::width,height.
1480 *
1481 * The clip mask rectangle is part of transformation inheritance
1482 * (weston_view_set_transform_parent()). A clip set in the root of the
1483 * transformation inheritance tree will affect all views in the tree.
1484 * A clip can be set only on the root view. Attempting to set a clip
1485 * on view that has a transformation parent will fail. Assigning a parent
1486 * to a view that has a clip set will cause the clip to be forgotten.
1487 *
1488 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1489 * on the additional transformations in the child views. These transformations
1490 * may not rotate the coordinate axes, i.e., only translation and scaling
1491 * are allowed. Violating this restriction causes the clipping to malfunction.
1492 * Furthermore, using scaling may cause rounding errors in child clipping.
1493 *
1494 * The clip mask rectangle is not automatically adjusted based on
1495 * wl_surface.attach dx and dy arguments.
1496 *
1497 * A clip mask rectangle can be set only if the compositor capability
1498 * WESTON_CAP_VIEW_CLIP_MASK is present.
1499 *
1500 * This function sets the clip mask rectangle and schedules a repaint for
1501 * the view.
1502 */
1503WL_EXPORT void
1504weston_view_set_mask(struct weston_view *view,
1505 int x, int y, int width, int height)
1506{
1507 struct weston_compositor *compositor = view->surface->compositor;
1508
1509 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1510 weston_log("%s not allowed without capability!\n", __func__);
1511 return;
1512 }
1513
1514 if (view->geometry.parent) {
1515 weston_log("view %p has a parent, clip forbidden!\n", view);
1516 return;
1517 }
1518
1519 if (width < 0 || height < 0) {
1520 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1521 x, y, width, height);
1522 return;
1523 }
1524
1525 pixman_region32_fini(&view->geometry.scissor);
1526 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1527 view->geometry.scissor_enabled = true;
1528 weston_view_geometry_dirty(view);
1529 weston_view_schedule_repaint(view);
1530}
1531
1532/** Remove the clip mask from a view
1533 *
1534 * \param view The view to remove the clip mask from.
1535 *
1536 * Removed the clip mask rectangle and schedules a repaint.
1537 *
1538 * \sa weston_view_set_mask
1539 */
1540WL_EXPORT void
1541weston_view_set_mask_infinite(struct weston_view *view)
1542{
1543 view->geometry.scissor_enabled = false;
1544 weston_view_geometry_dirty(view);
1545 weston_view_schedule_repaint(view);
1546}
1547
Armin Krezović0da12b82016-06-30 06:04:33 +02001548/* Check if view should be displayed
1549 *
1550 * The indicator is set manually when assigning
1551 * a view to a surface.
1552 *
1553 * This needs reworking. See the thread starting at:
1554 *
1555 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1556 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001557WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001558weston_view_is_mapped(struct weston_view *view)
1559{
Armin Krezović0da12b82016-06-30 06:04:33 +02001560 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001561}
1562
Armin Krezović0da12b82016-06-30 06:04:33 +02001563/* Check if a surface has a view assigned to it
1564 *
1565 * The indicator is set manually when mapping
1566 * a surface and creating a view for it.
1567 *
1568 * This needs to go. See the thread starting at:
1569 *
1570 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1571 *
1572 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001573WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001574weston_surface_is_mapped(struct weston_surface *surface)
1575{
Armin Krezović0da12b82016-06-30 06:04:33 +02001576 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001577}
1578
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001579static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001580surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001581{
1582 struct weston_view *view;
1583
1584 if (surface->width == width && surface->height == height)
1585 return;
1586
1587 surface->width = width;
1588 surface->height = height;
1589
1590 wl_list_for_each(view, &surface->views, surface_link)
1591 weston_view_geometry_dirty(view);
1592}
1593
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001594WL_EXPORT void
1595weston_surface_set_size(struct weston_surface *surface,
1596 int32_t width, int32_t height)
1597{
1598 assert(!surface->resource);
1599 surface_set_size(surface, width, height);
1600}
1601
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001602static int
1603fixed_round_up_to_int(wl_fixed_t f)
1604{
1605 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1606}
1607
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001608static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001609convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1610 int32_t width, int32_t height,
1611 uint32_t transform,
1612 int32_t scale)
1613{
1614 assert(scale > 0);
1615
1616 switch (transform) {
1617 case WL_OUTPUT_TRANSFORM_NORMAL:
1618 case WL_OUTPUT_TRANSFORM_180:
1619 case WL_OUTPUT_TRANSFORM_FLIPPED:
1620 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1621 *width_out = width / scale;
1622 *height_out = height / scale;
1623 break;
1624 case WL_OUTPUT_TRANSFORM_90:
1625 case WL_OUTPUT_TRANSFORM_270:
1626 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1627 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1628 *width_out = height / scale;
1629 *height_out = width / scale;
1630 break;
1631 default:
1632 assert(0 && "invalid transform");
1633 }
1634}
1635
1636static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001637weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001638{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001639 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001640
1641 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001642 surface->width_from_buffer = 0;
1643 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001644 return;
1645 }
1646
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001647 convert_size_by_transform_scale(&surface->width_from_buffer,
1648 &surface->height_from_buffer,
1649 surface->buffer_ref.buffer->width,
1650 surface->buffer_ref.buffer->height,
1651 vp->buffer.transform,
1652 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001653}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001654
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001655static void
1656weston_surface_update_size(struct weston_surface *surface)
1657{
1658 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1659 int32_t width, height;
1660
1661 width = surface->width_from_buffer;
1662 height = surface->height_from_buffer;
1663
1664 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001665 surface_set_size(surface,
1666 vp->surface.width, vp->surface.height);
1667 return;
1668 }
1669
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001670 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001671 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1672 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1673
1674 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001675 return;
1676 }
1677
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001678 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001679}
1680
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001681WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001682weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001683{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001684 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001685
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001686 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001687
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001688 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001689}
1690
Jason Ekstranda7af7042013-10-12 22:38:11 -05001691WL_EXPORT struct weston_view *
1692weston_compositor_pick_view(struct weston_compositor *compositor,
1693 wl_fixed_t x, wl_fixed_t y,
1694 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001695{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001696 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001697 wl_fixed_t view_x, view_y;
1698 int view_ix, view_iy;
1699 int ix = wl_fixed_to_int(x);
1700 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001701
Jason Ekstranda7af7042013-10-12 22:38:11 -05001702 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001703 if (!pixman_region32_contains_point(
1704 &view->transform.boundingbox, ix, iy, NULL))
1705 continue;
1706
1707 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1708 view_ix = wl_fixed_to_int(view_x);
1709 view_iy = wl_fixed_to_int(view_y);
1710
1711 if (!pixman_region32_contains_point(&view->surface->input,
1712 view_ix, view_iy, NULL))
1713 continue;
1714
Pekka Paalanen380adf52015-02-16 14:39:11 +02001715 if (view->geometry.scissor_enabled &&
1716 !pixman_region32_contains_point(&view->geometry.scissor,
1717 view_ix, view_iy, NULL))
1718 continue;
1719
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001720 *vx = view_x;
1721 *vy = view_y;
1722 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001723 }
1724
Derek Foremanf9318d12015-05-11 15:40:11 -05001725 *vx = wl_fixed_from_int(-1000000);
1726 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001727 return NULL;
1728}
1729
1730static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001731weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001732{
Daniel Stone37816df2012-05-16 18:45:18 +01001733 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001734
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001735 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001736 return;
1737
Daniel Stone37816df2012-05-16 18:45:18 +01001738 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001739 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001740}
1741
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001742WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001743weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001744{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001745 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001746
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747 if (!weston_view_is_mapped(view))
1748 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001749
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 weston_view_damage_below(view);
1751 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001752 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001753 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001754 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001755 wl_list_remove(&view->link);
1756 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001757 view->output_mask = 0;
1758 weston_surface_assign_output(view->surface);
1759
1760 if (weston_surface_is_mapped(view->surface))
1761 return;
1762
1763 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001764 struct weston_touch *touch = weston_seat_get_touch(seat);
1765 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1766 struct weston_keyboard *keyboard =
1767 weston_seat_get_keyboard(seat);
1768
1769 if (keyboard && keyboard->focus == view->surface)
1770 weston_keyboard_set_focus(keyboard, NULL);
1771 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001772 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001773 if (touch && touch->focus == view)
1774 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001775 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001776}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001777
Jason Ekstranda7af7042013-10-12 22:38:11 -05001778WL_EXPORT void
1779weston_surface_unmap(struct weston_surface *surface)
1780{
1781 struct weston_view *view;
1782
Armin Krezovićf8486c32016-06-30 06:04:28 +02001783 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001784 wl_list_for_each(view, &surface->views, surface_link)
1785 weston_view_unmap(view);
1786 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001787}
1788
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001789static void
1790weston_surface_reset_pending_buffer(struct weston_surface *surface)
1791{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001792 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001793 surface->pending.sx = 0;
1794 surface->pending.sy = 0;
1795 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001796 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001797}
1798
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799WL_EXPORT void
1800weston_view_destroy(struct weston_view *view)
1801{
1802 wl_signal_emit(&view->destroy_signal, view);
1803
1804 assert(wl_list_empty(&view->geometry.child_list));
1805
1806 if (weston_view_is_mapped(view)) {
1807 weston_view_unmap(view);
1808 weston_compositor_build_view_list(view->surface->compositor);
1809 }
1810
1811 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001812 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813
1814 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001815 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001817 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818
1819 weston_view_set_transform_parent(view, NULL);
1820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 wl_list_remove(&view->surface_link);
1822
1823 free(view);
1824}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001825
1826WL_EXPORT void
1827weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001828{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001829 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001830 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001831
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001832 if (--surface->ref_count > 0)
1833 return;
1834
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001835 assert(surface->resource == NULL);
1836
Pekka Paalanenca790762015-04-17 14:23:38 +03001837 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001838
Pekka Paalanene67858b2013-04-25 13:57:42 +03001839 assert(wl_list_empty(&surface->subsurface_list_pending));
1840 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001841
Jason Ekstranda7af7042013-10-12 22:38:11 -05001842 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1843 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001844
Jason Ekstrand7b982072014-05-20 14:33:03 -05001845 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001846
Pekka Paalanende685b82012-12-04 15:58:12 +02001847 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001848
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001849 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001850 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001851 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001852
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001853 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001854 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001855
Pekka Paalanen133e4392014-09-23 22:08:46 -04001856 weston_presentation_feedback_discard_list(&surface->feedback_list);
1857
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001858 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001859}
1860
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001861static void
1862destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001863{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001864 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001865
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001866 assert(surface);
1867
Giulio Camuffo0d379742013-11-15 22:06:15 +01001868 /* Set the resource to NULL, since we don't want to leave a
1869 * dangling pointer if the surface was refcounted and survives
1870 * the weston_surface_destroy() call. */
1871 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001872
1873 if (surface->viewport_resource)
1874 wl_resource_set_user_data(surface->viewport_resource, NULL);
1875
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001876 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001877}
1878
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001879static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001880weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1881{
1882 struct weston_buffer *buffer =
1883 container_of(listener, struct weston_buffer, destroy_listener);
1884
1885 wl_signal_emit(&buffer->destroy_signal, buffer);
1886 free(buffer);
1887}
1888
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001889WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001890weston_buffer_from_resource(struct wl_resource *resource)
1891{
1892 struct weston_buffer *buffer;
1893 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001894
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001895 listener = wl_resource_get_destroy_listener(resource,
1896 weston_buffer_destroy_handler);
1897
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001898 if (listener)
1899 return container_of(listener, struct weston_buffer,
1900 destroy_listener);
1901
1902 buffer = zalloc(sizeof *buffer);
1903 if (buffer == NULL)
1904 return NULL;
1905
1906 buffer->resource = resource;
1907 wl_signal_init(&buffer->destroy_signal);
1908 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001909 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001910 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001911
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001912 return buffer;
1913}
1914
1915static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001916weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1917 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001918{
Pekka Paalanende685b82012-12-04 15:58:12 +02001919 struct weston_buffer_reference *ref =
1920 container_of(listener, struct weston_buffer_reference,
1921 destroy_listener);
1922
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001923 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001924 ref->buffer = NULL;
1925}
1926
1927WL_EXPORT void
1928weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001929 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001930{
1931 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001932 ref->buffer->busy_count--;
1933 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001934 assert(wl_resource_get_client(ref->buffer->resource));
1935 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001936 WL_BUFFER_RELEASE);
1937 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001938 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001939 }
1940
Pekka Paalanende685b82012-12-04 15:58:12 +02001941 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001942 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001943 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001944 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001945 }
1946
Pekka Paalanende685b82012-12-04 15:58:12 +02001947 ref->buffer = buffer;
1948 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1949}
1950
1951static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001952weston_surface_attach(struct weston_surface *surface,
1953 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001954{
1955 weston_buffer_reference(&surface->buffer_ref, buffer);
1956
Pekka Paalanena6421c42012-12-04 15:58:10 +02001957 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001958 if (weston_surface_is_mapped(surface))
1959 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001960 }
1961
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001962 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001963
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001964 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001965 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001966}
1967
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001968WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001969weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001970{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001971 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001972
1973 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001974 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001975}
1976
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001977WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001978weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001979{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001980 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001981
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001982 pixman_region32_union(&compositor->primary_plane.damage,
1983 &compositor->primary_plane.damage,
1984 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001985 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001986}
1987
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001988static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001989surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001990{
Pekka Paalanende685b82012-12-04 15:58:12 +02001991 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001992 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001993 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001994
Pekka Paalanenb5026542014-11-12 15:09:24 +02001995 if (weston_timeline_enabled_ &&
1996 pixman_region32_not_empty(&surface->damage))
1997 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1998 TLP_OUTPUT(surface->output), TLP_END);
1999
Jason Ekstrandef540082014-06-26 10:37:36 -07002000 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002001}
2002
2003static void
2004view_accumulate_damage(struct weston_view *view,
2005 pixman_region32_t *opaque)
2006{
2007 pixman_region32_t damage;
2008
2009 pixman_region32_init(&damage);
2010 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002011 pixman_box32_t *extents;
2012
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002014 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002015 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002016 pixman_region32_copy(&damage, &view->surface->damage);
2017 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002018 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002019 }
2020
Pekka Paalanen380adf52015-02-16 14:39:11 +02002021 pixman_region32_intersect(&damage, &damage,
2022 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023 pixman_region32_subtract(&damage, &damage, opaque);
2024 pixman_region32_union(&view->plane->damage,
2025 &view->plane->damage, &damage);
2026 pixman_region32_fini(&damage);
2027 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002028 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002029}
2030
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002031static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002032compositor_accumulate_damage(struct weston_compositor *ec)
2033{
2034 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002036 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002037
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002038 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002039
2040 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002041 pixman_region32_copy(&plane->clip, &clip);
2042
2043 pixman_region32_init(&opaque);
2044
Jason Ekstranda7af7042013-10-12 22:38:11 -05002045 wl_list_for_each(ev, &ec->view_list, link) {
2046 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002047 continue;
2048
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002050 }
2051
2052 pixman_region32_union(&clip, &clip, &opaque);
2053 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002054 }
2055
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002056 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002057
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002059 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060
2061 wl_list_for_each(ev, &ec->view_list, link) {
2062 if (ev->surface->touched)
2063 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002064 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002065
2066 surface_flush_damage(ev->surface);
2067
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002068 /* Both the renderer and the backend have seen the buffer
2069 * by now. If renderer needs the buffer, it has its own
2070 * reference set. If the backend wants to keep the buffer
2071 * around for migrating the surface into a non-primary plane
2072 * later, keep_buffer is true. Otherwise, drop the core
2073 * reference now, and allow early buffer release. This enables
2074 * clients to use single-buffering.
2075 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 if (!ev->surface->keep_buffer)
2077 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002078 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002079}
2080
2081static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002083{
2084 struct weston_subsurface *sub;
2085
Pekka Paalanene67858b2013-04-25 13:57:42 +03002086 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002088 continue;
2089
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2091 wl_list_init(&sub->surface->views);
2092
2093 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002094 }
2095}
2096
2097static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002098surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002099{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002100 struct weston_subsurface *sub;
2101 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002102
Jason Ekstranda7af7042013-10-12 22:38:11 -05002103 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2104 if (sub->surface == surface)
2105 continue;
2106
George Kiagiadakised04d382014-06-13 18:10:26 +02002107 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2108 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002110 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002111
2112 surface_free_unused_subsurface_views(sub->surface);
2113 }
2114}
2115
2116static void
2117view_list_add_subsurface_view(struct weston_compositor *compositor,
2118 struct weston_subsurface *sub,
2119 struct weston_view *parent)
2120{
2121 struct weston_subsurface *child;
2122 struct weston_view *view = NULL, *iv;
2123
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002124 if (!weston_surface_is_mapped(sub->surface))
2125 return;
2126
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2128 if (iv->geometry.parent == parent) {
2129 view = iv;
2130 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002131 }
2132 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002133
2134 if (view) {
2135 /* Put it back in the surface's list of views */
2136 wl_list_remove(&view->surface_link);
2137 wl_list_insert(&sub->surface->views, &view->surface_link);
2138 } else {
2139 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002140 weston_view_set_position(view,
2141 sub->position.x,
2142 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143 weston_view_set_transform_parent(view, parent);
2144 }
2145
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002146 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002147 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002148 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002149
Pekka Paalanenb188e912013-11-19 14:03:35 +02002150 if (wl_list_empty(&sub->surface->subsurface_list)) {
2151 wl_list_insert(compositor->view_list.prev, &view->link);
2152 return;
2153 }
2154
2155 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2156 if (child->surface == sub->surface)
2157 wl_list_insert(compositor->view_list.prev, &view->link);
2158 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002159 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002160 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161}
2162
2163static void
2164view_list_add(struct weston_compositor *compositor,
2165 struct weston_view *view)
2166{
2167 struct weston_subsurface *sub;
2168
2169 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002170
Pekka Paalanenb188e912013-11-19 14:03:35 +02002171 if (wl_list_empty(&view->surface->subsurface_list)) {
2172 wl_list_insert(compositor->view_list.prev, &view->link);
2173 return;
2174 }
2175
2176 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2177 if (sub->surface == view->surface)
2178 wl_list_insert(compositor->view_list.prev, &view->link);
2179 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002181 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002182}
2183
2184static void
2185weston_compositor_build_view_list(struct weston_compositor *compositor)
2186{
2187 struct weston_view *view;
2188 struct weston_layer *layer;
2189
2190 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002191 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002192 surface_stash_subsurface_views(view->surface);
2193
2194 wl_list_init(&compositor->view_list);
2195 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002196 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002197 view_list_add(compositor, view);
2198 }
2199 }
2200
2201 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002202 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002204}
2205
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002206static void
2207weston_output_take_feedback_list(struct weston_output *output,
2208 struct weston_surface *surface)
2209{
2210 struct weston_view *view;
2211 struct weston_presentation_feedback *feedback;
2212 uint32_t flags = 0xffffffff;
2213
2214 if (wl_list_empty(&surface->feedback_list))
2215 return;
2216
2217 /* All views must have the flag for the flag to survive. */
2218 wl_list_for_each(view, &surface->views, surface_link) {
2219 /* ignore views that are not on this output at all */
2220 if (view->output_mask & (1u << output->id))
2221 flags &= view->psf_flags;
2222 }
2223
2224 wl_list_for_each(feedback, &surface->feedback_list, link)
2225 feedback->psf_flags = flags;
2226
2227 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2228 wl_list_init(&surface->feedback_list);
2229}
2230
David Herrmann1edf44c2013-10-22 17:11:26 +02002231static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002232weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002233{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002234 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002235 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002236 struct weston_animation *animation, *next;
2237 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002238 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002239 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002240 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002241
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002242 if (output->destroying)
2243 return 0;
2244
Pekka Paalanenb5026542014-11-12 15:09:24 +02002245 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2246
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002247 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002248 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002249
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002250 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002251 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002252 } else {
2253 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002254 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002255 ev->psf_flags = 0;
2256 }
2257 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002258
Pekka Paalanene67858b2013-04-25 13:57:42 +03002259 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002260 wl_list_for_each(ev, &ec->view_list, link) {
2261 /* Note: This operation is safe to do multiple times on the
2262 * same surface.
2263 */
2264 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002265 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002266 &ev->surface->frame_callback_list);
2267 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002268
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002269 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002270 }
2271 }
2272
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002273 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002274
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002275 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002276 pixman_region32_intersect(&output_damage,
2277 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002278 pixman_region32_subtract(&output_damage,
2279 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002280
Scott Moreauccbf29d2012-02-22 14:21:41 -07002281 if (output->dirty)
2282 weston_output_update_matrix(output);
2283
David Herrmann1edf44c2013-10-22 17:11:26 +02002284 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002285
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002286 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002287
Kristian Høgsbergef044142011-06-21 15:02:12 -04002288 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002289
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002290 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002291
Jonas Ådahldb773762012-06-13 00:01:21 +02002292 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002293 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002294 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002295 }
2296
Scott Moreaud64cf212012-06-08 19:40:54 -06002297 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002298 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002299 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002300 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002301
Pekka Paalanenb5026542014-11-12 15:09:24 +02002302 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2303
David Herrmann1edf44c2013-10-22 17:11:26 +02002304 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002305}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002306
Pekka Paalanen82919792014-05-21 13:51:49 +03002307static void
2308weston_output_schedule_repaint_reset(struct weston_output *output)
2309{
Pekka Paalanen82919792014-05-21 13:51:49 +03002310 output->repaint_scheduled = 0;
2311 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002312}
2313
Pekka Paalanen0513a952014-05-21 16:17:27 +03002314static int
2315output_repaint_timer_handler(void *data)
2316{
2317 struct weston_output *output = data;
2318 struct weston_compositor *compositor = output->compositor;
2319
2320 if (output->repaint_needed &&
2321 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2322 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2323 weston_output_repaint(output) == 0)
2324 return 0;
2325
2326 weston_output_schedule_repaint_reset(output);
2327
2328 return 0;
2329}
2330
Kristian Høgsbergef044142011-06-21 15:02:12 -04002331WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002332weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002333 const struct timespec *stamp,
2334 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002335{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002336 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002337 int32_t refresh_nsec;
2338 struct timespec now;
2339 struct timespec gone;
2340 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002341
Pekka Paalanenb5026542014-11-12 15:09:24 +02002342 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2343 TLP_VBLANK(stamp), TLP_END);
2344
Pekka Paalanend7894d02015-07-03 15:08:53 +03002345 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002346 weston_presentation_feedback_present_list(&output->feedback_list,
2347 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002348 output->msc,
2349 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002350
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002351 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002352
Pekka Paalanen0513a952014-05-21 16:17:27 +03002353 weston_compositor_read_presentation_clock(compositor, &now);
2354 timespec_sub(&gone, &now, stamp);
2355 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2356 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002357
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002358 if (msec < -1000 || msec > 1000) {
2359 static bool warned;
2360
2361 if (!warned)
2362 weston_log("Warning: computed repaint delay is "
2363 "insane: %d msec\n", msec);
2364 warned = true;
2365
2366 msec = 0;
2367 }
2368
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002369 /* Called from restart_repaint_loop and restart happens already after
2370 * the deadline given by repaint_msec? In that case we delay until
2371 * the deadline of the next frame, to give clients a more predictable
2372 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002373 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002374 msec += refresh_nsec / 1000000;
2375
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002376 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002377 output_repaint_timer_handler(output);
2378 else
2379 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002380}
2381
2382static void
2383idle_repaint(void *data)
2384{
2385 struct weston_output *output = data;
2386
Jonas Ådahle5a12252013-04-05 23:07:11 +02002387 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002388}
2389
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002390WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002391weston_layer_entry_insert(struct weston_layer_entry *list,
2392 struct weston_layer_entry *entry)
2393{
2394 wl_list_insert(&list->link, &entry->link);
2395 entry->layer = list->layer;
2396}
2397
2398WL_EXPORT void
2399weston_layer_entry_remove(struct weston_layer_entry *entry)
2400{
2401 wl_list_remove(&entry->link);
2402 wl_list_init(&entry->link);
2403 entry->layer = NULL;
2404}
2405
2406WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002407weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2408{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002409 wl_list_init(&layer->view_list.link);
2410 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002411 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002412 if (below != NULL)
2413 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002414}
2415
2416WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002417weston_layer_set_mask(struct weston_layer *layer,
2418 int x, int y, int width, int height)
2419{
2420 struct weston_view *view;
2421
2422 layer->mask.x1 = x;
2423 layer->mask.x2 = x + width;
2424 layer->mask.y1 = y;
2425 layer->mask.y2 = y + height;
2426
2427 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2428 weston_view_geometry_dirty(view);
2429 }
2430}
2431
2432WL_EXPORT void
2433weston_layer_set_mask_infinite(struct weston_layer *layer)
2434{
2435 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2436 UINT32_MAX, UINT32_MAX);
2437}
2438
2439WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002440weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002441{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002442 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002443 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002444
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002445 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2446 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002447 return;
2448
Pekka Paalanenb5026542014-11-12 15:09:24 +02002449 if (!output->repaint_needed)
2450 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2451
Kristian Høgsbergef044142011-06-21 15:02:12 -04002452 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002453 output->repaint_needed = 1;
2454 if (output->repaint_scheduled)
2455 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002456
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002457 wl_event_loop_add_idle(loop, idle_repaint, output);
2458 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002459 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002460}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002461
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002462WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002463weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2464{
2465 struct weston_output *output;
2466
2467 wl_list_for_each(output, &compositor->output_list, link)
2468 weston_output_schedule_repaint(output);
2469}
2470
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002471static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002472surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002473{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002474 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002475}
2476
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002477static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002478surface_attach(struct wl_client *client,
2479 struct wl_resource *resource,
2480 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2481{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002482 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002483 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002484
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002485 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002486 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002487 if (buffer == NULL) {
2488 wl_client_post_no_memory(client);
2489 return;
2490 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002491 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002492
Pekka Paalanende685b82012-12-04 15:58:12 +02002493 /* Attach, attach, without commit in between does not send
2494 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002495 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002496
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002497 surface->pending.sx = sx;
2498 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002499 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002500}
2501
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002502static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002503surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002504 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002505 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002506{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002507 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002508
Derek Foreman57e92ed2015-11-17 14:11:35 -06002509 if (width <= 0 || height <= 0)
2510 return;
2511
Derek Foreman152254b2015-11-26 14:17:48 -06002512 pixman_region32_union_rect(&surface->pending.damage_surface,
2513 &surface->pending.damage_surface,
2514 x, y, width, height);
2515}
2516
2517static void
2518surface_damage_buffer(struct wl_client *client,
2519 struct wl_resource *resource,
2520 int32_t x, int32_t y, int32_t width, int32_t height)
2521{
2522 struct weston_surface *surface = wl_resource_get_user_data(resource);
2523
2524 if (width <= 0 || height <= 0)
2525 return;
2526
2527 pixman_region32_union_rect(&surface->pending.damage_buffer,
2528 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002529 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002530}
2531
Kristian Høgsberg33418202011-08-16 23:01:28 -04002532static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002533destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002534{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002535 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002536
2537 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002538 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002539}
2540
2541static void
2542surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002543 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002544{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002545 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002546 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002547
2548 cb = malloc(sizeof *cb);
2549 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002550 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002551 return;
2552 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002553
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002554 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2555 callback);
2556 if (cb->resource == NULL) {
2557 free(cb);
2558 wl_resource_post_no_memory(resource);
2559 return;
2560 }
2561
Jason Ekstranda85118c2013-06-27 20:17:02 -05002562 wl_resource_set_implementation(cb->resource, NULL, cb,
2563 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002564
Pekka Paalanenbc106382012-10-10 12:49:31 +03002565 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002566}
2567
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002568static void
2569surface_set_opaque_region(struct wl_client *client,
2570 struct wl_resource *resource,
2571 struct wl_resource *region_resource)
2572{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002573 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002574 struct weston_region *region;
2575
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002576 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002577 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002578 pixman_region32_copy(&surface->pending.opaque,
2579 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002580 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002581 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002582 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002583}
2584
2585static void
2586surface_set_input_region(struct wl_client *client,
2587 struct wl_resource *resource,
2588 struct wl_resource *region_resource)
2589{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002590 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002591 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002592
2593 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002594 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002595 pixman_region32_copy(&surface->pending.input,
2596 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002597 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002598 pixman_region32_fini(&surface->pending.input);
2599 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002600 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002601}
2602
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002603static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002604weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002605{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002606 struct weston_subsurface *sub;
2607
2608 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2609 parent_link_pending) {
2610 wl_list_remove(&sub->parent_link);
2611 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2612 }
2613}
2614
2615static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002616weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002617 struct weston_matrix *matrix)
2618{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002619 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002620 double src_width, src_height, dest_width, dest_height;
2621
2622 weston_matrix_init(matrix);
2623
2624 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2625 src_width = surface->width_from_buffer;
2626 src_height = surface->height_from_buffer;
2627 } else {
2628 src_width = wl_fixed_to_double(vp->buffer.src_width);
2629 src_height = wl_fixed_to_double(vp->buffer.src_height);
2630 }
2631
2632 if (vp->surface.width == -1) {
2633 dest_width = src_width;
2634 dest_height = src_height;
2635 } else {
2636 dest_width = vp->surface.width;
2637 dest_height = vp->surface.height;
2638 }
2639
2640 if (src_width != dest_width || src_height != dest_height)
2641 weston_matrix_scale(matrix,
2642 src_width / dest_width,
2643 src_height / dest_height, 1);
2644
2645 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2646 weston_matrix_translate(matrix,
2647 wl_fixed_to_double(vp->buffer.src_x),
2648 wl_fixed_to_double(vp->buffer.src_y),
2649 0);
2650
2651 switch (vp->buffer.transform) {
2652 case WL_OUTPUT_TRANSFORM_FLIPPED:
2653 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2654 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2655 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2656 weston_matrix_scale(matrix, -1, 1, 1);
2657 weston_matrix_translate(matrix,
2658 surface->width_from_buffer, 0, 0);
2659 break;
2660 }
2661
2662 switch (vp->buffer.transform) {
2663 default:
2664 case WL_OUTPUT_TRANSFORM_NORMAL:
2665 case WL_OUTPUT_TRANSFORM_FLIPPED:
2666 break;
2667 case WL_OUTPUT_TRANSFORM_90:
2668 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2669 weston_matrix_rotate_xy(matrix, 0, 1);
2670 weston_matrix_translate(matrix,
2671 surface->height_from_buffer, 0, 0);
2672 break;
2673 case WL_OUTPUT_TRANSFORM_180:
2674 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2675 weston_matrix_rotate_xy(matrix, -1, 0);
2676 weston_matrix_translate(matrix,
2677 surface->width_from_buffer,
2678 surface->height_from_buffer, 0);
2679 break;
2680 case WL_OUTPUT_TRANSFORM_270:
2681 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2682 weston_matrix_rotate_xy(matrix, 0, -1);
2683 weston_matrix_translate(matrix,
2684 0, surface->width_from_buffer, 0);
2685 break;
2686 }
2687
2688 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2689}
2690
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002691/**
2692 * Compute a + b > c while being safe to overflows.
2693 */
2694static bool
2695fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2696{
2697 return (int64_t)a + (int64_t)b > (int64_t)c;
2698}
2699
2700static bool
2701weston_surface_is_pending_viewport_source_valid(
2702 const struct weston_surface *surface)
2703{
2704 const struct weston_surface_state *pend = &surface->pending;
2705 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2706 int width_from_buffer = 0;
2707 int height_from_buffer = 0;
2708 wl_fixed_t w;
2709 wl_fixed_t h;
2710
2711 /* If viewport source rect is not set, it is always ok. */
2712 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2713 return true;
2714
2715 if (pend->newly_attached) {
2716 if (pend->buffer) {
2717 convert_size_by_transform_scale(&width_from_buffer,
2718 &height_from_buffer,
2719 pend->buffer->width,
2720 pend->buffer->height,
2721 vp->buffer.transform,
2722 vp->buffer.scale);
2723 } else {
2724 /* No buffer: viewport is irrelevant. */
2725 return true;
2726 }
2727 } else {
2728 width_from_buffer = surface->width_from_buffer;
2729 height_from_buffer = surface->height_from_buffer;
2730 }
2731
2732 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2733 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2734
2735 /* No buffer: viewport is irrelevant. */
2736 if (width_from_buffer == 0 || height_from_buffer == 0)
2737 return true;
2738
2739 /* overflow checks for wl_fixed_from_int() */
2740 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2741 return false;
2742 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2743 return false;
2744
2745 w = wl_fixed_from_int(width_from_buffer);
2746 h = wl_fixed_from_int(height_from_buffer);
2747
2748 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2749 return false;
2750 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2751 return false;
2752
2753 return true;
2754}
2755
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002756static bool
2757fixed_is_integer(wl_fixed_t v)
2758{
2759 return (v & 0xff) == 0;
2760}
2761
2762static bool
2763weston_surface_is_pending_viewport_dst_size_int(
2764 const struct weston_surface *surface)
2765{
2766 const struct weston_buffer_viewport *vp =
2767 &surface->pending.buffer_viewport;
2768
2769 if (vp->surface.width != -1) {
2770 assert(vp->surface.width > 0 && vp->surface.height > 0);
2771 return true;
2772 }
2773
2774 return fixed_is_integer(vp->buffer.src_width) &&
2775 fixed_is_integer(vp->buffer.src_height);
2776}
2777
Derek Foreman152254b2015-11-26 14:17:48 -06002778/* Translate pending damage in buffer co-ordinates to surface
2779 * co-ordinates and union it with a pixman_region32_t.
2780 * This should only be called after the buffer is attached.
2781 */
2782static void
2783apply_damage_buffer(pixman_region32_t *dest,
2784 struct weston_surface *surface,
2785 struct weston_surface_state *state)
2786{
2787 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2788
2789 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2790 * translated into surface co-ordinates and unioned with
2791 * any other surface damage.
2792 * None of this makes sense if there is no buffer though.
2793 */
2794 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2795 pixman_region32_t buffer_damage;
2796
2797 pixman_region32_intersect_rect(&state->damage_buffer,
2798 &state->damage_buffer,
2799 0, 0, buffer->width,
2800 buffer->height);
2801 pixman_region32_init(&buffer_damage);
2802 weston_matrix_transform_region(&buffer_damage,
2803 &surface->buffer_to_surface_matrix,
2804 &state->damage_buffer);
2805 pixman_region32_union(dest, dest, &buffer_damage);
2806 pixman_region32_fini(&buffer_damage);
2807 }
2808 /* We should clear this on commit even if there was no buffer */
2809 pixman_region32_clear(&state->damage_buffer);
2810}
2811
Jason Ekstrand1e059042014-10-16 10:55:19 -05002812static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002813weston_surface_commit_state(struct weston_surface *surface,
2814 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002815{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002816 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002817 pixman_region32_t opaque;
2818
Alexander Larsson4ea95522013-05-22 14:41:37 +02002819 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002820 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002821 /* wp_viewport.set_source */
2822 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002823 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002824
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002825 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002826 if (state->newly_attached)
2827 weston_surface_attach(surface, state->buffer);
2828 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002829
Jason Ekstrand1e059042014-10-16 10:55:19 -05002830 weston_surface_build_buffer_matrix(surface,
2831 &surface->surface_to_buffer_matrix);
2832 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2833 &surface->surface_to_buffer_matrix);
2834
Jason Ekstrand7b982072014-05-20 14:33:03 -05002835 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002836 weston_surface_update_size(surface);
2837 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002838 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002839 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002840
Jason Ekstrand7b982072014-05-20 14:33:03 -05002841 state->sx = 0;
2842 state->sy = 0;
2843 state->newly_attached = 0;
2844 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002845
Derek Foreman152254b2015-11-26 14:17:48 -06002846 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002847 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002848 (pixman_region32_not_empty(&state->damage_surface) ||
2849 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002850 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002851
Pekka Paalanen8e159182012-10-10 12:49:25 +03002852 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002853 &state->damage_surface);
2854
2855 apply_damage_buffer(&surface->damage, surface, state);
2856
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002857 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002858 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002859 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002860
2861 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002862 pixman_region32_init(&opaque);
2863 pixman_region32_intersect_rect(&opaque, &state->opaque,
2864 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002865
2866 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2867 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002868 wl_list_for_each(view, &surface->views, surface_link)
2869 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002870 }
2871
2872 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002873
2874 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002875 pixman_region32_intersect_rect(&surface->input, &state->input,
2876 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002877
Pekka Paalanenbc106382012-10-10 12:49:31 +03002878 /* wl_surface.frame */
2879 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002880 &state->frame_callback_list);
2881 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002882
2883 /* XXX:
2884 * What should happen with a feedback request, if there
2885 * is no wl_buffer attached for this commit?
2886 */
2887
2888 /* presentation.feedback */
2889 wl_list_insert_list(&surface->feedback_list,
2890 &state->feedback_list);
2891 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08002892
2893 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002894}
2895
2896static void
2897weston_surface_commit(struct weston_surface *surface)
2898{
2899 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002900
Pekka Paalanene67858b2013-04-25 13:57:42 +03002901 weston_surface_commit_subsurface_order(surface);
2902
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002903 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002904}
2905
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002906static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002907weston_subsurface_commit(struct weston_subsurface *sub);
2908
2909static void
2910weston_subsurface_parent_commit(struct weston_subsurface *sub,
2911 int parent_is_synchronized);
2912
2913static void
2914surface_commit(struct wl_client *client, struct wl_resource *resource)
2915{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002916 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002917 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2918
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002919 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
2920 assert(surface->viewport_resource);
2921
2922 wl_resource_post_error(surface->viewport_resource,
2923 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
2924 "wl_surface@%d has viewport source outside buffer",
2925 wl_resource_get_id(resource));
2926 return;
2927 }
2928
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002929 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
2930 assert(surface->viewport_resource);
2931
2932 wl_resource_post_error(surface->viewport_resource,
2933 WP_VIEWPORT_ERROR_BAD_SIZE,
2934 "wl_surface@%d viewport dst size not integer",
2935 wl_resource_get_id(resource));
2936 return;
2937 }
2938
Pekka Paalanene67858b2013-04-25 13:57:42 +03002939 if (sub) {
2940 weston_subsurface_commit(sub);
2941 return;
2942 }
2943
2944 weston_surface_commit(surface);
2945
2946 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2947 if (sub->surface != surface)
2948 weston_subsurface_parent_commit(sub, 0);
2949 }
2950}
2951
2952static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002953surface_set_buffer_transform(struct wl_client *client,
2954 struct wl_resource *resource, int transform)
2955{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002956 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002957
Jonny Lamba55f1392014-05-30 12:07:15 +02002958 /* if wl_output.transform grows more members this will need to be updated. */
2959 if (transform < 0 ||
2960 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2961 wl_resource_post_error(resource,
2962 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2963 "buffer transform must be a valid transform "
2964 "('%d' specified)", transform);
2965 return;
2966 }
2967
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002968 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002969 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002970}
2971
Alexander Larsson4ea95522013-05-22 14:41:37 +02002972static void
2973surface_set_buffer_scale(struct wl_client *client,
2974 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002975 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002976{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002977 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002978
Jonny Lamba55f1392014-05-30 12:07:15 +02002979 if (scale < 1) {
2980 wl_resource_post_error(resource,
2981 WL_SURFACE_ERROR_INVALID_SCALE,
2982 "buffer scale must be at least one "
2983 "('%d' specified)", scale);
2984 return;
2985 }
2986
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002987 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002988 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002989}
2990
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002991static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002992 surface_destroy,
2993 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002994 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002995 surface_frame,
2996 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002997 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002998 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002999 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003000 surface_set_buffer_scale,
3001 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003002};
3003
3004static void
3005compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003006 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003007{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003008 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003009 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003010
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003011 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003012 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003013 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003014 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003015 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003016
Jason Ekstranda85118c2013-06-27 20:17:02 -05003017 surface->resource =
3018 wl_resource_create(client, &wl_surface_interface,
3019 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003020 if (surface->resource == NULL) {
3021 weston_surface_destroy(surface);
3022 wl_resource_post_no_memory(resource);
3023 return;
3024 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003025 wl_resource_set_implementation(surface->resource, &surface_interface,
3026 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003027
3028 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003029}
3030
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003031static void
3032destroy_region(struct wl_resource *resource)
3033{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003034 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003035
3036 pixman_region32_fini(&region->region);
3037 free(region);
3038}
3039
3040static void
3041region_destroy(struct wl_client *client, struct wl_resource *resource)
3042{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003043 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003044}
3045
3046static void
3047region_add(struct wl_client *client, struct wl_resource *resource,
3048 int32_t x, int32_t y, int32_t width, int32_t height)
3049{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003050 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003051
3052 pixman_region32_union_rect(&region->region, &region->region,
3053 x, y, width, height);
3054}
3055
3056static void
3057region_subtract(struct wl_client *client, struct wl_resource *resource,
3058 int32_t x, int32_t y, int32_t width, int32_t height)
3059{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003060 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003061 pixman_region32_t rect;
3062
3063 pixman_region32_init_rect(&rect, x, y, width, height);
3064 pixman_region32_subtract(&region->region, &region->region, &rect);
3065 pixman_region32_fini(&rect);
3066}
3067
3068static const struct wl_region_interface region_interface = {
3069 region_destroy,
3070 region_add,
3071 region_subtract
3072};
3073
3074static void
3075compositor_create_region(struct wl_client *client,
3076 struct wl_resource *resource, uint32_t id)
3077{
3078 struct weston_region *region;
3079
3080 region = malloc(sizeof *region);
3081 if (region == NULL) {
3082 wl_resource_post_no_memory(resource);
3083 return;
3084 }
3085
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003086 pixman_region32_init(&region->region);
3087
Jason Ekstranda85118c2013-06-27 20:17:02 -05003088 region->resource =
3089 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003090 if (region->resource == NULL) {
3091 free(region);
3092 wl_resource_post_no_memory(resource);
3093 return;
3094 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003095 wl_resource_set_implementation(region->resource, &region_interface,
3096 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003097}
3098
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003099static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003100 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003101 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003102};
3103
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003104static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003105weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3106{
3107 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003108
Jason Ekstrand7b982072014-05-20 14:33:03 -05003109 weston_surface_commit_state(surface, &sub->cached);
3110 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003111
3112 weston_surface_commit_subsurface_order(surface);
3113
3114 weston_surface_schedule_repaint(surface);
3115
Jason Ekstrand7b982072014-05-20 14:33:03 -05003116 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003117}
3118
3119static void
3120weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3121{
3122 struct weston_surface *surface = sub->surface;
3123
3124 /*
3125 * If this commit would cause the surface to move by the
3126 * attach(dx, dy) parameters, the old damage region must be
3127 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003128 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003129 */
Derek Foreman152254b2015-11-26 14:17:48 -06003130 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003131 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003132 pixman_region32_union(&sub->cached.damage_surface,
3133 &sub->cached.damage_surface,
3134 &surface->pending.damage_surface);
3135 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003136
3137 if (surface->pending.newly_attached) {
3138 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003139 weston_surface_state_set_buffer(&sub->cached,
3140 surface->pending.buffer);
3141 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003142 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003143 weston_presentation_feedback_discard_list(
3144 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003145 }
3146 sub->cached.sx += surface->pending.sx;
3147 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003148
Derek Foreman152254b2015-11-26 14:17:48 -06003149 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3150
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003151 sub->cached.buffer_viewport.changed |=
3152 surface->pending.buffer_viewport.changed;
3153 sub->cached.buffer_viewport.buffer =
3154 surface->pending.buffer_viewport.buffer;
3155 sub->cached.buffer_viewport.surface =
3156 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003157
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003158 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003159
3160 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3161
3162 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3163
3164 wl_list_insert_list(&sub->cached.frame_callback_list,
3165 &surface->pending.frame_callback_list);
3166 wl_list_init(&surface->pending.frame_callback_list);
3167
Pekka Paalanen133e4392014-09-23 22:08:46 -04003168 wl_list_insert_list(&sub->cached.feedback_list,
3169 &surface->pending.feedback_list);
3170 wl_list_init(&surface->pending.feedback_list);
3171
Jason Ekstrand7b982072014-05-20 14:33:03 -05003172 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003173}
3174
Derek Foreman280e7dd2014-10-03 13:13:42 -05003175static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003176weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3177{
3178 while (sub) {
3179 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003180 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003181
3182 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003183 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003184
3185 sub = weston_surface_to_subsurface(sub->parent);
3186 }
3187
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003188 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189}
3190
3191static void
3192weston_subsurface_commit(struct weston_subsurface *sub)
3193{
3194 struct weston_surface *surface = sub->surface;
3195 struct weston_subsurface *tmp;
3196
3197 /* Recursive check for effectively synchronized. */
3198 if (weston_subsurface_is_synchronized(sub)) {
3199 weston_subsurface_commit_to_cache(sub);
3200 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003201 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003202 /* flush accumulated state from cache */
3203 weston_subsurface_commit_to_cache(sub);
3204 weston_subsurface_commit_from_cache(sub);
3205 } else {
3206 weston_surface_commit(surface);
3207 }
3208
3209 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3210 if (tmp->surface != surface)
3211 weston_subsurface_parent_commit(tmp, 0);
3212 }
3213 }
3214}
3215
3216static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003217weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003218{
3219 struct weston_surface *surface = sub->surface;
3220 struct weston_subsurface *tmp;
3221
Pekka Paalanene67858b2013-04-25 13:57:42 +03003222 /* From now on, commit_from_cache the whole sub-tree, regardless of
3223 * the synchronized mode of each child. This sub-surface or some
3224 * of its ancestors were synchronized, so we are synchronized
3225 * all the way down.
3226 */
3227
Jason Ekstrand7b982072014-05-20 14:33:03 -05003228 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003229 weston_subsurface_commit_from_cache(sub);
3230
3231 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3232 if (tmp->surface != surface)
3233 weston_subsurface_parent_commit(tmp, 1);
3234 }
3235}
3236
3237static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003238weston_subsurface_parent_commit(struct weston_subsurface *sub,
3239 int parent_is_synchronized)
3240{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003241 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003242 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003243 wl_list_for_each(view, &sub->surface->views, surface_link)
3244 weston_view_set_position(view,
3245 sub->position.x,
3246 sub->position.y);
3247
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003248 sub->position.set = 0;
3249 }
3250
3251 if (parent_is_synchronized || sub->synchronized)
3252 weston_subsurface_synchronized_commit(sub);
3253}
3254
Pekka Paalanen8274d902014-08-06 19:36:51 +03003255static int
3256subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3257{
3258 return snprintf(buf, len, "sub-surface");
3259}
3260
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003261static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003262subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003263{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003264 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003265
Jason Ekstranda7af7042013-10-12 22:38:11 -05003266 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003267 weston_view_set_position(view,
3268 view->geometry.x + dx,
3269 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003270
3271 /* No need to check parent mappedness, because if parent is not
3272 * mapped, parent is not in a visible layer, so this sub-surface
3273 * will not be drawn either.
3274 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003275
Pekka Paalanene67858b2013-04-25 13:57:42 +03003276 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003277 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003278
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003279 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003280 * because that would call it also for the parent surface,
3281 * which might not be mapped yet. That would lead to
3282 * inconsistent state, where the window could never be
3283 * mapped.
3284 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003285 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003286 * weston_surface_is_mapped() return true, so that when the
3287 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003288 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003289 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003290 }
3291}
3292
3293static struct weston_subsurface *
3294weston_surface_to_subsurface(struct weston_surface *surface)
3295{
3296 if (surface->configure == subsurface_configure)
3297 return surface->configure_private;
3298
3299 return NULL;
3300}
3301
Pekka Paalanen01388e22013-04-25 13:57:44 +03003302WL_EXPORT struct weston_surface *
3303weston_surface_get_main_surface(struct weston_surface *surface)
3304{
3305 struct weston_subsurface *sub;
3306
3307 while (surface && (sub = weston_surface_to_subsurface(surface)))
3308 surface = sub->parent;
3309
3310 return surface;
3311}
3312
Pekka Paalanen50b67472014-10-01 15:02:41 +03003313WL_EXPORT int
3314weston_surface_set_role(struct weston_surface *surface,
3315 const char *role_name,
3316 struct wl_resource *error_resource,
3317 uint32_t error_code)
3318{
3319 assert(role_name);
3320
3321 if (surface->role_name == NULL ||
3322 surface->role_name == role_name ||
3323 strcmp(surface->role_name, role_name) == 0) {
3324 surface->role_name = role_name;
3325
3326 return 0;
3327 }
3328
3329 wl_resource_post_error(error_resource, error_code,
3330 "Cannot assign role %s to wl_surface@%d,"
3331 " already has role %s\n",
3332 role_name,
3333 wl_resource_get_id(surface->resource),
3334 surface->role_name);
3335 return -1;
3336}
3337
Pekka Paalanen8274d902014-08-06 19:36:51 +03003338WL_EXPORT void
3339weston_surface_set_label_func(struct weston_surface *surface,
3340 int (*desc)(struct weston_surface *,
3341 char *, size_t))
3342{
3343 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003344 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003345}
3346
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003347/** Get the size of surface contents
3348 *
3349 * \param surface The surface to query.
3350 * \param width Returns the width of raw contents.
3351 * \param height Returns the height of raw contents.
3352 *
3353 * Retrieves the raw surface content size in pixels for the given surface.
3354 * This is the whole content size in buffer pixels. If the surface
3355 * has no content or the renderer does not implement this feature,
3356 * zeroes are returned.
3357 *
3358 * This function is used to determine the buffer size needed for
3359 * a weston_surface_copy_content() call.
3360 */
3361WL_EXPORT void
3362weston_surface_get_content_size(struct weston_surface *surface,
3363 int *width, int *height)
3364{
3365 struct weston_renderer *rer = surface->compositor->renderer;
3366
3367 if (!rer->surface_get_content_size) {
3368 *width = 0;
3369 *height = 0;
3370 return;
3371 }
3372
3373 rer->surface_get_content_size(surface, width, height);
3374}
3375
3376/** Copy surface contents to system memory.
3377 *
3378 * \param surface The surface to copy from.
3379 * \param target Pointer to the target memory buffer.
3380 * \param size Size of the target buffer in bytes.
3381 * \param src_x X location on contents to copy from.
3382 * \param src_y Y location on contents to copy from.
3383 * \param width Width in pixels of the area to copy.
3384 * \param height Height in pixels of the area to copy.
3385 * \return 0 for success, -1 for failure.
3386 *
3387 * Surface contents are maintained by the renderer. They can be in a
3388 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3389 * else.
3390 *
3391 * Surface contents are copied into memory pointed to by target,
3392 * which has size bytes of space available. The target memory
3393 * may be larger than needed, but being smaller returns an error.
3394 * The extra bytes in target may or may not be written; their content is
3395 * unspecified. Size must be large enough to hold the image.
3396 *
3397 * The image in the target memory will be arranged in rows from
3398 * top to bottom, and pixels on a row from left to right. The pixel
3399 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3400 * width * 4.
3401 *
3402 * Parameters src_x and src_y define the upper-left corner in buffer
3403 * coordinates (pixels) to copy from. Parameters width and height
3404 * define the size of the area to copy in pixels.
3405 *
3406 * The rectangle defined by src_x, src_y, width, height must fit in
3407 * the surface contents. Otherwise an error is returned.
3408 *
3409 * Use surface_get_data_size to determine the content size; the
3410 * needed target buffer size and rectangle limits.
3411 *
3412 * CURRENT IMPLEMENTATION RESTRICTIONS:
3413 * - the machine must be little-endian due to Pixman formats.
3414 *
3415 * NOTE: Pixman formats are premultiplied.
3416 */
3417WL_EXPORT int
3418weston_surface_copy_content(struct weston_surface *surface,
3419 void *target, size_t size,
3420 int src_x, int src_y,
3421 int width, int height)
3422{
3423 struct weston_renderer *rer = surface->compositor->renderer;
3424 int cw, ch;
3425 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3426
3427 if (!rer->surface_copy_content)
3428 return -1;
3429
3430 weston_surface_get_content_size(surface, &cw, &ch);
3431
3432 if (src_x < 0 || src_y < 0)
3433 return -1;
3434
3435 if (width <= 0 || height <= 0)
3436 return -1;
3437
3438 if (src_x + width > cw || src_y + height > ch)
3439 return -1;
3440
3441 if (width * bytespp * height > size)
3442 return -1;
3443
3444 return rer->surface_copy_content(surface, target, size,
3445 src_x, src_y, width, height);
3446}
3447
Pekka Paalanene67858b2013-04-25 13:57:42 +03003448static void
3449subsurface_set_position(struct wl_client *client,
3450 struct wl_resource *resource, int32_t x, int32_t y)
3451{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003452 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003453
3454 if (!sub)
3455 return;
3456
3457 sub->position.x = x;
3458 sub->position.y = y;
3459 sub->position.set = 1;
3460}
3461
3462static struct weston_subsurface *
3463subsurface_from_surface(struct weston_surface *surface)
3464{
3465 struct weston_subsurface *sub;
3466
3467 sub = weston_surface_to_subsurface(surface);
3468 if (sub)
3469 return sub;
3470
3471 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3472 if (sub->surface == surface)
3473 return sub;
3474
3475 return NULL;
3476}
3477
3478static struct weston_subsurface *
3479subsurface_sibling_check(struct weston_subsurface *sub,
3480 struct weston_surface *surface,
3481 const char *request)
3482{
3483 struct weston_subsurface *sibling;
3484
3485 sibling = subsurface_from_surface(surface);
3486
3487 if (!sibling) {
3488 wl_resource_post_error(sub->resource,
3489 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3490 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003491 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003492 return NULL;
3493 }
3494
3495 if (sibling->parent != sub->parent) {
3496 wl_resource_post_error(sub->resource,
3497 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3498 "%s: wl_surface@%d has a different parent",
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 return sibling;
3504}
3505
3506static void
3507subsurface_place_above(struct wl_client *client,
3508 struct wl_resource *resource,
3509 struct wl_resource *sibling_resource)
3510{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003511 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003512 struct weston_surface *surface =
3513 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003514 struct weston_subsurface *sibling;
3515
3516 if (!sub)
3517 return;
3518
3519 sibling = subsurface_sibling_check(sub, surface, "place_above");
3520 if (!sibling)
3521 return;
3522
3523 wl_list_remove(&sub->parent_link_pending);
3524 wl_list_insert(sibling->parent_link_pending.prev,
3525 &sub->parent_link_pending);
3526}
3527
3528static void
3529subsurface_place_below(struct wl_client *client,
3530 struct wl_resource *resource,
3531 struct wl_resource *sibling_resource)
3532{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003533 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003534 struct weston_surface *surface =
3535 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003536 struct weston_subsurface *sibling;
3537
3538 if (!sub)
3539 return;
3540
3541 sibling = subsurface_sibling_check(sub, surface, "place_below");
3542 if (!sibling)
3543 return;
3544
3545 wl_list_remove(&sub->parent_link_pending);
3546 wl_list_insert(&sibling->parent_link_pending,
3547 &sub->parent_link_pending);
3548}
3549
3550static void
3551subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3552{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003553 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003554
3555 if (sub)
3556 sub->synchronized = 1;
3557}
3558
3559static void
3560subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3561{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003562 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003563
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003564 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003565 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003566
3567 /* If sub became effectively desynchronized, flush. */
3568 if (!weston_subsurface_is_synchronized(sub))
3569 weston_subsurface_synchronized_commit(sub);
3570 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003571}
3572
3573static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003574weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3575{
3576 wl_list_remove(&sub->parent_link);
3577 wl_list_remove(&sub->parent_link_pending);
3578 wl_list_remove(&sub->parent_destroy_listener.link);
3579 sub->parent = NULL;
3580}
3581
3582static void
3583weston_subsurface_destroy(struct weston_subsurface *sub);
3584
3585static void
3586subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3587{
3588 struct weston_subsurface *sub =
3589 container_of(listener, struct weston_subsurface,
3590 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003591 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003592
3593 /* The protocol object (wl_resource) is left inert. */
3594 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003595 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003596
3597 weston_subsurface_destroy(sub);
3598}
3599
3600static void
3601subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3602{
3603 struct weston_subsurface *sub =
3604 container_of(listener, struct weston_subsurface,
3605 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003606 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003607 assert(sub->surface != sub->parent);
3608
3609 if (weston_surface_is_mapped(sub->surface))
3610 weston_surface_unmap(sub->surface);
3611
3612 weston_subsurface_unlink_parent(sub);
3613}
3614
3615static void
3616subsurface_resource_destroy(struct wl_resource *resource)
3617{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003618 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003619
3620 if (sub)
3621 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003622}
3623
3624static void
3625subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3626{
3627 wl_resource_destroy(resource);
3628}
3629
3630static void
3631weston_subsurface_link_parent(struct weston_subsurface *sub,
3632 struct weston_surface *parent)
3633{
3634 sub->parent = parent;
3635 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003636 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003637 &sub->parent_destroy_listener);
3638
3639 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3640 wl_list_insert(&parent->subsurface_list_pending,
3641 &sub->parent_link_pending);
3642}
3643
3644static void
3645weston_subsurface_link_surface(struct weston_subsurface *sub,
3646 struct weston_surface *surface)
3647{
3648 sub->surface = surface;
3649 sub->surface_destroy_listener.notify =
3650 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003651 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003652 &sub->surface_destroy_listener);
3653}
3654
3655static void
3656weston_subsurface_destroy(struct weston_subsurface *sub)
3657{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003658 struct weston_view *view, *next;
3659
Pekka Paalanene67858b2013-04-25 13:57:42 +03003660 assert(sub->surface);
3661
3662 if (sub->resource) {
3663 assert(weston_surface_to_subsurface(sub->surface) == sub);
3664 assert(sub->parent_destroy_listener.notify ==
3665 subsurface_handle_parent_destroy);
3666
George Kiagiadakised04d382014-06-13 18:10:26 +02003667 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3668 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003669 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003670 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003671
Pekka Paalanene67858b2013-04-25 13:57:42 +03003672 if (sub->parent)
3673 weston_subsurface_unlink_parent(sub);
3674
Jason Ekstrand7b982072014-05-20 14:33:03 -05003675 weston_surface_state_fini(&sub->cached);
3676 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003677
3678 sub->surface->configure = NULL;
3679 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003680 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003681 } else {
3682 /* the dummy weston_subsurface for the parent itself */
3683 assert(sub->parent_destroy_listener.notify == NULL);
3684 wl_list_remove(&sub->parent_link);
3685 wl_list_remove(&sub->parent_link_pending);
3686 }
3687
3688 wl_list_remove(&sub->surface_destroy_listener.link);
3689 free(sub);
3690}
3691
3692static const struct wl_subsurface_interface subsurface_implementation = {
3693 subsurface_destroy,
3694 subsurface_set_position,
3695 subsurface_place_above,
3696 subsurface_place_below,
3697 subsurface_set_sync,
3698 subsurface_set_desync
3699};
3700
3701static struct weston_subsurface *
3702weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3703 struct weston_surface *parent)
3704{
3705 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003706 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003707
Bryce Harringtonde16d892014-11-20 22:21:57 -08003708 sub = zalloc(sizeof *sub);
3709 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003710 return NULL;
3711
Jason Ekstranda7af7042013-10-12 22:38:11 -05003712 wl_list_init(&sub->unused_views);
3713
Jason Ekstranda85118c2013-06-27 20:17:02 -05003714 sub->resource =
3715 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003716 if (!sub->resource) {
3717 free(sub);
3718 return NULL;
3719 }
3720
Jason Ekstranda85118c2013-06-27 20:17:02 -05003721 wl_resource_set_implementation(sub->resource,
3722 &subsurface_implementation,
3723 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003724 weston_subsurface_link_surface(sub, surface);
3725 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003726 weston_surface_state_init(&sub->cached);
3727 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003728 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003729
3730 return sub;
3731}
3732
3733/* Create a dummy subsurface for having the parent itself in its
3734 * sub-surface lists. Makes stacking order manipulation easy.
3735 */
3736static struct weston_subsurface *
3737weston_subsurface_create_for_parent(struct weston_surface *parent)
3738{
3739 struct weston_subsurface *sub;
3740
Bryce Harringtonde16d892014-11-20 22:21:57 -08003741 sub = zalloc(sizeof *sub);
3742 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003743 return NULL;
3744
3745 weston_subsurface_link_surface(sub, parent);
3746 sub->parent = parent;
3747 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3748 wl_list_insert(&parent->subsurface_list_pending,
3749 &sub->parent_link_pending);
3750
3751 return sub;
3752}
3753
3754static void
3755subcompositor_get_subsurface(struct wl_client *client,
3756 struct wl_resource *resource,
3757 uint32_t id,
3758 struct wl_resource *surface_resource,
3759 struct wl_resource *parent_resource)
3760{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003761 struct weston_surface *surface =
3762 wl_resource_get_user_data(surface_resource);
3763 struct weston_surface *parent =
3764 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003765 struct weston_subsurface *sub;
3766 static const char where[] = "get_subsurface: wl_subsurface@";
3767
3768 if (surface == parent) {
3769 wl_resource_post_error(resource,
3770 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3771 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003772 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003773 return;
3774 }
3775
3776 if (weston_surface_to_subsurface(surface)) {
3777 wl_resource_post_error(resource,
3778 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3779 "%s%d: wl_surface@%d is already a sub-surface",
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
Pekka Paalanen50b67472014-10-01 15:02:41 +03003784 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3785 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003786 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003787
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003788 if (weston_surface_get_main_surface(parent) == surface) {
3789 wl_resource_post_error(resource,
3790 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3791 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003792 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003793 return;
3794 }
3795
Pekka Paalanene67858b2013-04-25 13:57:42 +03003796 /* make sure the parent is in its own list */
3797 if (wl_list_empty(&parent->subsurface_list)) {
3798 if (!weston_subsurface_create_for_parent(parent)) {
3799 wl_resource_post_no_memory(resource);
3800 return;
3801 }
3802 }
3803
3804 sub = weston_subsurface_create(id, surface, parent);
3805 if (!sub) {
3806 wl_resource_post_no_memory(resource);
3807 return;
3808 }
3809
3810 surface->configure = subsurface_configure;
3811 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003812 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003813}
3814
3815static void
3816subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3817{
3818 wl_resource_destroy(resource);
3819}
3820
3821static const struct wl_subcompositor_interface subcompositor_interface = {
3822 subcompositor_destroy,
3823 subcompositor_get_subsurface
3824};
3825
3826static void
3827bind_subcompositor(struct wl_client *client,
3828 void *data, uint32_t version, uint32_t id)
3829{
3830 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003831 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003832
Jason Ekstranda85118c2013-06-27 20:17:02 -05003833 resource =
3834 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003835 if (resource == NULL) {
3836 wl_client_post_no_memory(client);
3837 return;
3838 }
3839 wl_resource_set_implementation(resource, &subcompositor_interface,
3840 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003841}
3842
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003843/** Set a DPMS mode on all of the compositor's outputs
3844 *
3845 * \param compositor The compositor instance
3846 * \param state The DPMS state the outputs will be set to
3847 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003848static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003849weston_compositor_dpms(struct weston_compositor *compositor,
3850 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003851{
3852 struct weston_output *output;
3853
3854 wl_list_for_each(output, &compositor->output_list, link)
3855 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003856 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003857}
3858
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003859/** Restores the compositor to active status
3860 *
3861 * \param compositor The compositor instance
3862 *
3863 * If the compositor was in a sleeping mode, all outputs are powered
3864 * back on via DPMS. Otherwise if the compositor was inactive
3865 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3866 * signal will fire.
3867 *
3868 * Restarts the idle timer.
3869 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003870WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003871weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003872{
Neil Roberts8b62e202013-09-30 13:14:47 +01003873 uint32_t old_state = compositor->state;
3874
3875 /* The state needs to be changed before emitting the wake
3876 * signal because that may try to schedule a repaint which
3877 * will not work if the compositor is still sleeping */
3878 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3879
3880 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003881 case WESTON_COMPOSITOR_SLEEPING:
3882 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3883 /* fall through */
3884 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003885 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003886 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003887 /* fall through */
3888 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003889 wl_event_source_timer_update(compositor->idle_source,
3890 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003891 }
3892}
3893
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003894/** Turns off rendering and frame events for the compositor.
3895 *
3896 * \param compositor The compositor instance
3897 *
3898 * This is used for example to prevent further rendering while the
3899 * compositor is shutting down.
3900 *
3901 * \note When offscreen state is entered, outputs will be powered
3902 * back on if they were sleeping (in DPMS off mode), even though
3903 * no rendering will be performed.
3904 *
3905 * Stops the idle timer.
3906 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003907WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003908weston_compositor_offscreen(struct weston_compositor *compositor)
3909{
3910 switch (compositor->state) {
3911 case WESTON_COMPOSITOR_OFFSCREEN:
3912 return;
3913 case WESTON_COMPOSITOR_SLEEPING:
3914 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3915 /* fall through */
3916 default:
3917 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3918 wl_event_source_timer_update(compositor->idle_source, 0);
3919 }
3920}
3921
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003922/** Powers down all attached output devices
3923 *
3924 * \param compositor The compositor instance
3925 *
3926 * Causes rendering to the outputs to cease, and no frame events to be
3927 * sent. Only powers down the outputs if the compositor is not already
3928 * in sleep mode.
3929 *
3930 * Stops the idle timer.
3931 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003932WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003933weston_compositor_sleep(struct weston_compositor *compositor)
3934{
3935 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3936 return;
3937
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003938 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003939 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3940 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3941}
3942
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003943/** Sets compositor to idle mode
3944 *
3945 * \param data The compositor instance
3946 *
3947 * This is called when the idle timer fires. Once the compositor is in
3948 * idle mode it requires a wake action (e.g. via
3949 * weston_compositor_wake()) to restore it. The compositor's
3950 * idle_signal will be triggered when the idle event occurs.
3951 *
3952 * Idleness can be inhibited by setting the compositor's idle_inhibit
3953 * property.
3954 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003955static int
3956idle_handler(void *data)
3957{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003958 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003959
3960 if (compositor->idle_inhibit)
3961 return 1;
3962
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003963 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003964 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003965
3966 return 1;
3967}
3968
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003969WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003970weston_plane_init(struct weston_plane *plane,
3971 struct weston_compositor *ec,
3972 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003973{
3974 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003975 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003976 plane->x = x;
3977 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003978 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003979
3980 /* Init the link so that the call to wl_list_remove() when releasing
3981 * the plane without ever stacking doesn't lead to a crash */
3982 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003983}
3984
3985WL_EXPORT void
3986weston_plane_release(struct weston_plane *plane)
3987{
Xiong Zhang97116532013-10-23 13:58:31 +08003988 struct weston_view *view;
3989
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003990 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003991 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003992
Xiong Zhang97116532013-10-23 13:58:31 +08003993 wl_list_for_each(view, &plane->compositor->view_list, link) {
3994 if (view->plane == plane)
3995 view->plane = NULL;
3996 }
3997
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003998 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003999}
4000
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004001WL_EXPORT void
4002weston_compositor_stack_plane(struct weston_compositor *ec,
4003 struct weston_plane *plane,
4004 struct weston_plane *above)
4005{
4006 if (above)
4007 wl_list_insert(above->link.prev, &plane->link);
4008 else
4009 wl_list_insert(&ec->plane_list, &plane->link);
4010}
4011
Casey Dahlin9074db52012-04-19 22:50:09 -04004012static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004013{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004014 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004015}
4016
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004017static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004018bind_output(struct wl_client *client,
4019 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004020{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004021 struct weston_output *output = data;
4022 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004023 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004024
Jason Ekstranda85118c2013-06-27 20:17:02 -05004025 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004026 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004027 if (resource == NULL) {
4028 wl_client_post_no_memory(client);
4029 return;
4030 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004031
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004032 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05004033 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004034
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004035 wl_output_send_geometry(resource,
4036 output->x,
4037 output->y,
4038 output->mm_width,
4039 output->mm_height,
4040 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004041 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004042 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004043 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004044 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004045 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004046
4047 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004048 wl_output_send_mode(resource,
4049 mode->flags,
4050 mode->width,
4051 mode->height,
4052 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004053 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004054
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004055 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004056 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004057}
4058
David Fort0de859e2016-05-27 23:22:57 +02004059/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004060static void
David Fort0de859e2016-05-27 23:22:57 +02004061weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4062 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004063{
4064 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004065 bool start_resizing = false;
4066
4067 if (!delta_width)
4068 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004069
4070 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004071 if (output == resized_output) {
4072 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004073 continue;
4074 }
4075
David Fort0de859e2016-05-27 23:22:57 +02004076 if (start_resizing) {
4077 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004078 output->dirty = 1;
4079 }
4080 }
4081}
4082
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004083WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004084weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004085{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004086 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004087 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004088
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004089 output->destroying = 1;
4090
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004091 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08004092 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004093 weston_view_assign_output(view);
4094 }
4095
Pekka Paalanen0513a952014-05-21 16:17:27 +03004096 wl_event_source_remove(output->repaint_timer);
4097
Pekka Paalanen133e4392014-09-23 22:08:46 -04004098 weston_presentation_feedback_discard_list(&output->feedback_list);
4099
David Fort0de859e2016-05-27 23:22:57 +02004100 weston_compositor_reflow_outputs(output->compositor, output, output->width);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004101 wl_list_remove(&output->link);
4102
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004103 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004104 wl_signal_emit(&output->destroy_signal, output);
4105
Richard Hughesafe690c2013-05-02 10:10:04 +01004106 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004107 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004108 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08004109 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004110
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004111 wl_resource_for_each(resource, &output->resource_list) {
4112 wl_resource_set_destructor(resource, NULL);
4113 }
4114
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004115 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004116}
4117
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004118WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004119weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004120{
Scott Moreau850ca422012-05-21 15:21:25 -06004121 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004122
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004123 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004124 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004125
Scott Moreauccbf29d2012-02-22 14:21:41 -07004126 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004127 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004128 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004129 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004130 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004131 weston_matrix_scale(&output->matrix, magnification,
4132 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004133 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004134
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004135 switch (output->transform) {
4136 case WL_OUTPUT_TRANSFORM_FLIPPED:
4137 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4138 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4139 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4140 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4141 weston_matrix_scale(&output->matrix, -1, 1, 1);
4142 break;
4143 }
4144
4145 switch (output->transform) {
4146 default:
4147 case WL_OUTPUT_TRANSFORM_NORMAL:
4148 case WL_OUTPUT_TRANSFORM_FLIPPED:
4149 break;
4150 case WL_OUTPUT_TRANSFORM_90:
4151 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4152 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4153 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4154 break;
4155 case WL_OUTPUT_TRANSFORM_180:
4156 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4157 weston_matrix_translate(&output->matrix,
4158 -output->width, -output->height, 0);
4159 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4160 break;
4161 case WL_OUTPUT_TRANSFORM_270:
4162 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4163 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4164 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4165 break;
4166 }
4167
4168 if (output->current_scale != 1)
4169 weston_matrix_scale(&output->matrix,
4170 output->current_scale,
4171 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004172
Scott Moreauccbf29d2012-02-22 14:21:41 -07004173 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004174
4175 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004176}
4177
Scott Moreau1bad5db2012-08-18 01:04:05 -06004178static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004179weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004180{
4181 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004182 output->native_scale = scale;
4183 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004184
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004185 convert_size_by_transform_scale(&output->width, &output->height,
4186 output->current_mode->width,
4187 output->current_mode->height,
4188 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004189}
4190
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004191static void
4192weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004193{
4194 output->x = x;
4195 output->y = y;
4196
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004197 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004198 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004199 output->width,
4200 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004201}
4202
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004203WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004204weston_output_move(struct weston_output *output, int x, int y)
4205{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004206 struct wl_resource *resource;
4207
4208 output->move_x = x - output->x;
4209 output->move_y = y - output->y;
4210
4211 if (output->move_x == 0 && output->move_y == 0)
4212 return;
4213
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004214 weston_output_init_geometry(output, x, y);
4215
4216 output->dirty = 1;
4217
4218 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004219 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004220
4221 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004222 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004223 wl_output_send_geometry(resource,
4224 output->x,
4225 output->y,
4226 output->mm_width,
4227 output->mm_height,
4228 output->subpixel,
4229 output->make,
4230 output->model,
4231 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004232
FORT David8a120692016-04-26 23:34:06 +02004233 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004234 wl_output_send_done(resource);
4235 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004236}
4237
Bryce Harrington3f650b82015-12-23 11:01:58 -08004238/** Initialize a weston_output object's parameters
4239 *
4240 * \param output The weston_output object to initialize
4241 * \param c The output's compositor
4242 * \param x x coordinate for the output in global coordinate space
4243 * \param y y coordinate for the output in global coordinate space
4244 * \param mm_width Physical width of the output as reported by the backend
4245 * \param mm_height Physical height of the output as reported by the backend
4246 * \param transform Rotation of the output
4247 * \param scale Native scaling factor for the output
4248 *
4249 * Sets up the transformation, zoom, and geometry of the output using
4250 * the input properties.
4251 *
4252 * Establishes a repaint timer for the output with the relevant display
4253 * object's event loop. See output_repaint_timer_handler().
4254 *
4255 * The output is assigned an ID. Weston can support up to 32 distinct
4256 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4257 * is referred to and used to find the first available ID number, and
4258 * then this ID is marked as used in output_id_pool.
4259 *
4260 * The output is also assigned a Wayland global with the wl_output
4261 * external interface.
4262 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004263WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004264weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004265 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004266 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004267{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004268 struct wl_event_loop *loop;
4269
Bryce Harrington18e45732015-12-23 20:53:53 -08004270 /* Verify we haven't reached the limit of 32 available output IDs */
4271 assert(ffs(~c->output_id_pool) > 0);
4272
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004273 output->compositor = c;
4274 output->x = x;
4275 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004276 output->mm_width = mm_width;
4277 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004278 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004279 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004280
Alexander Larsson0b135062013-05-28 16:23:36 +02004281 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004282 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004283
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004284 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004285 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004286
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004287 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004288 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004289 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004290 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004291 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004292 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004293
Pekka Paalanen0513a952014-05-21 16:17:27 +03004294 loop = wl_display_get_event_loop(c->wl_display);
4295 output->repaint_timer = wl_event_loop_add_timer(loop,
4296 output_repaint_timer_handler, output);
4297
Bryce Harrington3f650b82015-12-23 11:01:58 -08004298 /* Invert the output id pool and look for the lowest numbered
4299 * switch (the least significant bit). Take that bit's position
4300 * as our ID, and mark it used in the compositor's output_id_pool.
4301 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004302 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004303 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004304
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004305 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004306 wl_global_create(c->wl_display, &wl_output_interface, 2,
4307 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004308}
4309
4310/** Adds an output to the compositor's output list and
4311 * send the compositor's output_created signal.
4312 *
4313 * \param compositor The compositor instance.
4314 * \param output The output to be added.
4315 */
4316WL_EXPORT void
4317weston_compositor_add_output(struct weston_compositor *compositor,
4318 struct weston_output *output)
4319{
4320 wl_list_insert(compositor->output_list.prev, &output->link);
4321 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004322}
4323
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004324WL_EXPORT void
4325weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004326 double device_x, double device_y,
4327 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004328{
Derek Foreman0f679412014-10-02 13:41:17 -05004329 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004330 device_x,
4331 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004332 0.0,
4333 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004334
Derek Foreman67a18b92015-03-24 11:36:14 -05004335 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004336
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004337 *x = p.f[0] / p.f[3];
4338 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004339}
4340
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004341static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004342destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004343{
Jonny Lamb74130762013-11-26 18:19:46 +01004344 struct weston_surface *surface =
4345 wl_resource_get_user_data(resource);
4346
Pekka Paalanen4826f872016-04-22 14:14:38 +03004347 if (!surface)
4348 return;
4349
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004350 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004351 surface->pending.buffer_viewport.buffer.src_width =
4352 wl_fixed_from_int(-1);
4353 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004354 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004355}
4356
4357static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004358viewport_destroy(struct wl_client *client,
4359 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004360{
4361 wl_resource_destroy(resource);
4362}
4363
4364static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004365viewport_set_source(struct wl_client *client,
4366 struct wl_resource *resource,
4367 wl_fixed_t src_x,
4368 wl_fixed_t src_y,
4369 wl_fixed_t src_width,
4370 wl_fixed_t src_height)
4371{
4372 struct weston_surface *surface =
4373 wl_resource_get_user_data(resource);
4374
Pekka Paalanen4826f872016-04-22 14:14:38 +03004375 if (!surface) {
4376 wl_resource_post_error(resource,
4377 WP_VIEWPORT_ERROR_NO_SURFACE,
4378 "wl_surface for this viewport is no longer exists");
4379 return;
4380 }
4381
4382 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004383 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004384
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004385 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004386 src_height == wl_fixed_from_int(-1) &&
4387 src_x == wl_fixed_from_int(-1) &&
4388 src_y == wl_fixed_from_int(-1)) {
4389 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004390 surface->pending.buffer_viewport.buffer.src_width =
4391 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004392 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004393 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004394 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004395
Pekka Paalanen201769a2016-04-26 14:42:11 +03004396 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004397 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004398 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03004399 "wl_surface@%d viewport source "
4400 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4401 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004402 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03004403 wl_fixed_to_double(src_height),
4404 wl_fixed_to_double(src_x),
4405 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004406 return;
4407 }
4408
4409 surface->pending.buffer_viewport.buffer.src_x = src_x;
4410 surface->pending.buffer_viewport.buffer.src_y = src_y;
4411 surface->pending.buffer_viewport.buffer.src_width = src_width;
4412 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004413 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004414}
4415
4416static void
4417viewport_set_destination(struct wl_client *client,
4418 struct wl_resource *resource,
4419 int32_t dst_width,
4420 int32_t dst_height)
4421{
4422 struct weston_surface *surface =
4423 wl_resource_get_user_data(resource);
4424
Pekka Paalanen4826f872016-04-22 14:14:38 +03004425 if (!surface) {
4426 wl_resource_post_error(resource,
4427 WP_VIEWPORT_ERROR_NO_SURFACE,
4428 "wl_surface for this viewport no longer exists");
4429 return;
4430 }
4431
4432 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004433
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004434 if (dst_width == -1 && dst_height == -1) {
4435 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004436 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004437 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004438 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004439 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004440
4441 if (dst_width <= 0 || dst_height <= 0) {
4442 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004443 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004444 "destination size must be positive (%dx%d)",
4445 dst_width, dst_height);
4446 return;
4447 }
4448
4449 surface->pending.buffer_viewport.surface.width = dst_width;
4450 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004451 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004452}
4453
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004454static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004455 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004456 viewport_set_source,
4457 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004458};
4459
4460static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004461viewporter_destroy(struct wl_client *client,
4462 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004463{
4464 wl_resource_destroy(resource);
4465}
4466
4467static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004468viewporter_get_viewport(struct wl_client *client,
4469 struct wl_resource *viewporter,
4470 uint32_t id,
4471 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004472{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004473 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004474 struct weston_surface *surface =
4475 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004476 struct wl_resource *resource;
4477
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004478 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004479 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004480 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004481 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004482 return;
4483 }
4484
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004485 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004486 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004487 if (resource == NULL) {
4488 wl_client_post_no_memory(client);
4489 return;
4490 }
4491
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004492 wl_resource_set_implementation(resource, &viewport_interface,
4493 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004494
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004495 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004496}
4497
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004498static const struct wp_viewporter_interface viewporter_interface = {
4499 viewporter_destroy,
4500 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004501};
4502
4503static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004504bind_viewporter(struct wl_client *client,
4505 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004506{
4507 struct wl_resource *resource;
4508
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004509 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004510 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004511 if (resource == NULL) {
4512 wl_client_post_no_memory(client);
4513 return;
4514 }
4515
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004516 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004517 NULL, NULL);
4518}
4519
4520static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004521destroy_presentation_feedback(struct wl_resource *feedback_resource)
4522{
4523 struct weston_presentation_feedback *feedback;
4524
4525 feedback = wl_resource_get_user_data(feedback_resource);
4526
4527 wl_list_remove(&feedback->link);
4528 free(feedback);
4529}
4530
4531static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004532presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4533{
4534 wl_resource_destroy(resource);
4535}
4536
4537static void
4538presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004539 struct wl_resource *presentation_resource,
4540 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004541 uint32_t callback)
4542{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004543 struct weston_surface *surface;
4544 struct weston_presentation_feedback *feedback;
4545
4546 surface = wl_resource_get_user_data(surface_resource);
4547
Bryce Harringtonde16d892014-11-20 22:21:57 -08004548 feedback = zalloc(sizeof *feedback);
4549 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004550 goto err_calloc;
4551
4552 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004553 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004554 1, callback);
4555 if (!feedback->resource)
4556 goto err_create;
4557
4558 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4559 destroy_presentation_feedback);
4560
4561 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4562
4563 return;
4564
4565err_create:
4566 free(feedback);
4567
4568err_calloc:
4569 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004570}
4571
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004572static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004573 presentation_destroy,
4574 presentation_feedback
4575};
4576
4577static void
4578bind_presentation(struct wl_client *client,
4579 void *data, uint32_t version, uint32_t id)
4580{
4581 struct weston_compositor *compositor = data;
4582 struct wl_resource *resource;
4583
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004584 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004585 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004586 if (resource == NULL) {
4587 wl_client_post_no_memory(client);
4588 return;
4589 }
4590
4591 wl_resource_set_implementation(resource, &presentation_implementation,
4592 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004593 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004594}
4595
4596static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004597compositor_bind(struct wl_client *client,
4598 void *data, uint32_t version, uint32_t id)
4599{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004600 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004601 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004602
Jason Ekstranda85118c2013-06-27 20:17:02 -05004603 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004604 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004605 if (resource == NULL) {
4606 wl_client_post_no_memory(client);
4607 return;
4608 }
4609
4610 wl_resource_set_implementation(resource, &compositor_interface,
4611 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004612}
4613
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004614WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004615weston_environment_get_fd(const char *env)
4616{
4617 char *e, *end;
4618 int fd, flags;
4619
4620 e = getenv(env);
4621 if (!e)
4622 return -1;
Bryce Harrington375759e2016-07-12 16:51:27 -07004623 fd = strtol(e, &end, 10);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004624 if (*end != '\0')
4625 return -1;
4626
4627 flags = fcntl(fd, F_GETFD);
4628 if (flags == -1)
4629 return -1;
4630
4631 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4632 unsetenv(env);
4633
4634 return fd;
4635}
4636
Pekka Paalanenb5026542014-11-12 15:09:24 +02004637static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004638timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004639 uint32_t key, void *data)
4640{
4641 struct weston_compositor *compositor = data;
4642
4643 if (weston_timeline_enabled_)
4644 weston_timeline_close();
4645 else
4646 weston_timeline_open(compositor);
4647}
4648
Giulio Camuffo459137b2014-10-11 23:56:24 +03004649/** Create the compositor.
4650 *
4651 * This functions creates and initializes a compositor instance.
4652 *
4653 * \param display The Wayland display to be used.
4654 * \param user_data A pointer to an object that can later be retrieved
4655 * using the \ref weston_compositor_get_user_data function.
4656 * \return The compositor instance on success or NULL on failure.
4657 */
4658WL_EXPORT struct weston_compositor *
4659weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004660{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004661 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004662 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004663
Giulio Camuffo459137b2014-10-11 23:56:24 +03004664 ec = zalloc(sizeof *ec);
4665 if (!ec)
4666 return NULL;
4667
4668 ec->wl_display = display;
4669 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004670 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004671 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004672 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004673 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004674 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004675 wl_signal_init(&ec->idle_signal);
4676 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004677 wl_signal_init(&ec->show_input_panel_signal);
4678 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004679 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004680 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004681 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004682 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004683 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004684 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004685 wl_signal_init(&ec->session_signal);
4686 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004687
Casey Dahlin58ba1372012-04-19 22:50:08 -04004688 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004689 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004690
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02004691 ec->activate_serial = 1;
4692
Derek Foreman152254b2015-11-26 14:17:48 -06004693 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004694 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004695 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004696
Giulio Camuffo954f1832014-10-11 18:27:30 +03004697 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004698 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004699 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004700
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004701 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004702 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004703 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004704
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004705 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004706 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004707 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004708
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004709 if (weston_input_init(ec) != 0)
4710 goto fail;
4711
Jason Ekstranda7af7042013-10-12 22:38:11 -05004712 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004713 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004714 wl_list_init(&ec->layer_list);
4715 wl_list_init(&ec->seat_list);
4716 wl_list_init(&ec->output_list);
4717 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004718 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004719 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004720 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004721 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004722 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004723
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004724 wl_list_init(&ec->plugin_api_list);
4725
Xiong Zhang97116532013-10-23 13:58:31 +08004726 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004727 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004728
Giulio Camuffo459137b2014-10-11 23:56:24 +03004729 wl_data_device_manager_init(ec->wl_display);
4730
4731 wl_display_init_shm(ec->wl_display);
4732
4733 loop = wl_display_get_event_loop(ec->wl_display);
4734 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004735
Giulio Camuffo459137b2014-10-11 23:56:24 +03004736 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4737 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4738
4739 weston_compositor_add_debug_binding(ec, KEY_T,
4740 timeline_key_binding_handler, ec);
4741
Giulio Camuffo459137b2014-10-11 23:56:24 +03004742 return ec;
4743
4744fail:
4745 free(ec);
4746 return NULL;
4747}
4748
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004749WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004750weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004751{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004752 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004753
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004754 wl_event_source_remove(ec->idle_source);
4755
Matt Roper361d2ad2011-08-29 13:52:23 -07004756 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004757 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004758 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004759
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004760 if (ec->renderer)
4761 ec->renderer->destroy(ec);
4762
Daniel Stone325fc2d2012-05-30 16:31:58 +01004763 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004764 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004765 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004766 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004767 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004768 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004769
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004770 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004771}
4772
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004773WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004774weston_compositor_exit_with_code(struct weston_compositor *compositor,
4775 int exit_code)
4776{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004777 if (compositor->exit_code == EXIT_SUCCESS)
4778 compositor->exit_code = exit_code;
4779
Giulio Camuffo459137b2014-10-11 23:56:24 +03004780 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004781}
4782
4783WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004784weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4785 const struct weston_pointer_grab_interface *interface)
4786{
4787 struct weston_seat *seat;
4788
4789 ec->default_pointer_grab = interface;
4790 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004791 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4792
4793 if (pointer)
4794 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004795 }
4796}
4797
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004798WL_EXPORT int
4799weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4800 clockid_t clk_id)
4801{
4802 struct timespec ts;
4803
4804 if (clock_gettime(clk_id, &ts) < 0)
4805 return -1;
4806
4807 compositor->presentation_clock = clk_id;
4808
4809 return 0;
4810}
4811
4812/*
4813 * For choosing the software clock, when the display hardware or API
4814 * does not expose a compatible presentation timestamp.
4815 */
4816WL_EXPORT int
4817weston_compositor_set_presentation_clock_software(
4818 struct weston_compositor *compositor)
4819{
4820 /* In order of preference */
4821 static const clockid_t clocks[] = {
4822 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4823 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4824 CLOCK_MONOTONIC, /* no jumps, may crawl */
4825 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4826 CLOCK_REALTIME /* may jump and crawl */
4827 };
4828 unsigned i;
4829
4830 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4831 if (weston_compositor_set_presentation_clock(compositor,
4832 clocks[i]) == 0)
4833 return 0;
4834
4835 weston_log("Error: no suitable presentation clock available.\n");
4836
4837 return -1;
4838}
4839
Pekka Paalanen662f3842015-03-18 12:17:26 +02004840/** Read the current time from the Presentation clock
4841 *
4842 * \param compositor
4843 * \param ts[out] The current time.
4844 *
4845 * \note Reading the current time in user space is always imprecise to some
4846 * degree.
4847 *
4848 * This function is never meant to fail. If reading the clock does fail,
4849 * an error message is logged and a zero time is returned. Callers are not
4850 * supposed to detect or react to failures.
4851 */
4852WL_EXPORT void
4853weston_compositor_read_presentation_clock(
4854 const struct weston_compositor *compositor,
4855 struct timespec *ts)
4856{
4857 static bool warned;
4858 int ret;
4859
4860 ret = clock_gettime(compositor->presentation_clock, ts);
4861 if (ret < 0) {
4862 ts->tv_sec = 0;
4863 ts->tv_nsec = 0;
4864
4865 if (!warned)
4866 weston_log("Error: failure to read "
4867 "the presentation clock %#x: '%m' (%d)\n",
4868 compositor->presentation_clock, errno);
4869 warned = true;
4870 }
4871}
4872
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004873/** Import dmabuf buffer into current renderer
4874 *
4875 * \param compositor
4876 * \param buffer the dmabuf buffer to import
4877 * \return true on usable buffers, false otherwise
4878 *
4879 * This function tests that the linux_dmabuf_buffer is usable
4880 * for the current renderer. Returns false on unusable buffers. Usually
4881 * usability is tested by importing the dmabufs for composition.
4882 *
4883 * This hook is also used for detecting if the renderer supports
4884 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4885 * supported.
4886 * */
4887WL_EXPORT bool
4888weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4889 struct linux_dmabuf_buffer *buffer)
4890{
4891 struct weston_renderer *renderer;
4892
4893 renderer = compositor->renderer;
4894
4895 if (renderer->import_dmabuf == NULL)
4896 return false;
4897
4898 return renderer->import_dmabuf(compositor, buffer);
4899}
4900
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004901WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004902weston_version(int *major, int *minor, int *micro)
4903{
4904 *major = WESTON_VERSION_MAJOR;
4905 *minor = WESTON_VERSION_MINOR;
4906 *micro = WESTON_VERSION_MICRO;
4907}
4908
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004909WL_EXPORT void *
4910weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004911{
Derek Foreman3f86e502015-06-08 11:46:54 -05004912 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004913 char path[PATH_MAX];
4914 void *module, *init;
4915
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004916 if (name == NULL)
4917 return NULL;
4918
Derek Foreman3f86e502015-06-08 11:46:54 -05004919 if (name[0] != '/') {
4920 if (builddir)
4921 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4922 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03004923 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004924 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004925 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004926 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004927
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004928 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4929 if (module) {
4930 weston_log("Module '%s' already loaded\n", path);
4931 dlclose(module);
4932 return NULL;
4933 }
4934
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004935 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004936 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004937 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004938 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004939 return NULL;
4940 }
4941
4942 init = dlsym(module, entrypoint);
4943 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004944 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004945 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004946 return NULL;
4947 }
4948
4949 return init;
4950}
4951
Giulio Camuffo459137b2014-10-11 23:56:24 +03004952
4953/** Destroys the compositor.
4954 *
4955 * This function cleans up the compositor state and destroys it.
4956 *
4957 * \param compositor The compositor to be destroyed.
4958 */
4959WL_EXPORT void
4960weston_compositor_destroy(struct weston_compositor *compositor)
4961{
4962 /* prevent further rendering while shutting down */
4963 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4964
4965 wl_signal_emit(&compositor->destroy_signal, compositor);
4966
4967 weston_compositor_xkb_destroy(compositor);
4968
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004969 if (compositor->backend)
4970 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004971
4972 weston_plugin_api_destroy_list(compositor);
4973
Giulio Camuffo459137b2014-10-11 23:56:24 +03004974 free(compositor);
4975}
4976
4977/** Instruct the compositor to exit.
4978 *
4979 * This functions does not directly destroy the compositor object, it merely
4980 * command it to start the tear down process. It is not guaranteed that the
4981 * tear down will happen immediately.
4982 *
4983 * \param compositor The compositor to tear down.
4984 */
4985WL_EXPORT void
4986weston_compositor_exit(struct weston_compositor *compositor)
4987{
4988 compositor->exit(compositor);
4989}
4990
4991/** Return the user data stored in the compositor.
4992 *
4993 * This function returns the user data pointer set with user_data parameter
4994 * to the \ref weston_compositor_create function.
4995 */
4996WL_EXPORT void *
4997weston_compositor_get_user_data(struct weston_compositor *compositor)
4998{
4999 return compositor->user_data;
5000}
Pekka Paalanendd186732016-06-03 14:49:54 +03005001
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005002static const char * const backend_map[] = {
5003 [WESTON_BACKEND_DRM] = "drm-backend.so",
5004 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5005 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5006 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5007 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5008 [WESTON_BACKEND_X11] = "x11-backend.so",
5009};
5010
Pekka Paalanendd186732016-06-03 14:49:54 +03005011/** Load a backend into a weston_compositor
5012 *
5013 * A backend must be loaded to make a weston_compositor work. A backend
5014 * provides input and output capabilities, and determines the renderer to use.
5015 *
5016 * \param compositor A compositor that has not had a backend loaded yet.
5017 * \param backend Name of the backend file.
5018 * \param config_base A pointer to a backend-specific configuration
5019 * structure's 'base' member.
5020 *
5021 * \return 0 on success, or -1 on error.
5022 */
5023WL_EXPORT int
5024weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005025 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005026 struct weston_backend_config *config_base)
5027{
5028 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005029 struct weston_backend_config *config_base);
5030
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005031 if (backend < 0 || backend >= ARRAY_LENGTH(backend_map))
5032 return -1;
5033
5034 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005035 if (!backend_init)
5036 return -1;
5037
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005038 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005039}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005040
5041WL_EXPORT int
5042weston_compositor_load_xwayland(struct weston_compositor *compositor)
5043{
5044 int (*module_init)(struct weston_compositor *ec,
5045 int *argc, char *argv[]);
5046 int argc = 0;
5047
5048 module_init = weston_load_module("xwayland.so", "module_init");
5049 if (!module_init)
5050 return -1;
5051 if (module_init(compositor, &argc, NULL) < 0)
5052 return -1;
5053 return 0;
5054}