blob: 870cd47febcd43658eba48a6552aa8680b823c85 [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"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050064
Pekka Paalanen0513a952014-05-21 16:17:27 +030065#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
66
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020067static void
Alexander Larsson0b135062013-05-28 16:23:36 +020068weston_output_transform_scale_init(struct weston_output *output,
69 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020070
Rob Bradford27b17932013-06-26 18:08:46 +010071static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050072weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010073
Derek Foreman6ae7bc92014-11-04 10:47:33 -060074static void weston_mode_switch_finish(struct weston_output *output,
75 int mode_changed,
76 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080077{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020078 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020079 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020080 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060081 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020082
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020083 pixman_region32_init(&old_output_region);
84 pixman_region32_copy(&old_output_region, &output->region);
85
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020086 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020087 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088
89 pixman_region32_init(&output->previous_damage);
90 pixman_region32_init_rect(&output->region, output->x, output->y,
91 output->width, output->height);
92
93 weston_output_update_matrix(output);
94
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020095 /* If a pointer falls outside the outputs new geometry, move it to its
96 * lower-right corner */
97 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -050098 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020099 int32_t x, y;
100
101 if (!pointer)
102 continue;
103
104 x = wl_fixed_to_int(pointer->x);
105 y = wl_fixed_to_int(pointer->y);
106
107 if (!pixman_region32_contains_point(&old_output_region,
108 x, y, NULL) ||
109 pixman_region32_contains_point(&output->region,
110 x, y, NULL))
111 continue;
112
113 if (x >= output->x + output->width)
114 x = output->x + output->width - 1;
115 if (y >= output->y + output->height)
116 y = output->y + output->height - 1;
117
118 pointer->x = wl_fixed_from_int(x);
119 pointer->y = wl_fixed_from_int(y);
120 }
121
122 pixman_region32_fini(&old_output_region);
123
Derek Foremandd4cd332014-11-10 10:29:59 -0600124 if (!mode_changed && !scale_changed)
125 return;
126
Hardening57388e42013-09-18 23:56:36 +0200127 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600128 wl_resource_for_each(resource, &output->resource_list) {
129 if (mode_changed) {
130 wl_output_send_mode(resource,
131 output->current_mode->flags,
132 output->current_mode->width,
133 output->current_mode->height,
134 output->current_mode->refresh);
135 }
Hardening57388e42013-09-18 23:56:36 +0200136
Derek Foreman41bdc272014-11-05 13:26:57 -0600137 version = wl_resource_get_version(resource);
138 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600139 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200140
Derek Foreman41bdc272014-11-05 13:26:57 -0600141 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
142 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600143 }
144}
145
David Fort0de859e2016-05-27 23:22:57 +0200146
147static void
148weston_compositor_reflow_outputs(struct weston_compositor *compositor,
149 struct weston_output *resized_output, int delta_width);
150
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600151WL_EXPORT int
152weston_output_mode_set_native(struct weston_output *output,
153 struct weston_mode *mode,
154 int32_t scale)
155{
156 int ret;
157 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200158 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600159
160 if (!output->switch_mode)
161 return -1;
162
163 if (!output->original_mode) {
164 mode_changed = 1;
165 ret = output->switch_mode(output, mode);
166 if (ret < 0)
167 return ret;
168 if (output->current_scale != scale) {
169 scale_changed = 1;
170 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200171 }
172 }
173
David Fort0de859e2016-05-27 23:22:57 +0200174 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600175 output->native_mode = mode;
176 output->native_scale = scale;
177
178 weston_mode_switch_finish(output, mode_changed, scale_changed);
179
David Fort0de859e2016-05-27 23:22:57 +0200180 if (mode_changed || scale_changed) {
181 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
182
183 wl_signal_emit(&output->compositor->output_resized_signal, output);
184 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600185 return 0;
186}
187
188WL_EXPORT int
189weston_output_mode_switch_to_native(struct weston_output *output)
190{
191 int ret;
192 int mode_changed = 0, scale_changed = 0;
193
194 if (!output->switch_mode)
195 return -1;
196
197 if (!output->original_mode) {
198 weston_log("already in the native mode\n");
199 return -1;
200 }
201 /* the non fullscreen clients haven't seen a mode set since we
202 * switched into a temporary, so we need to notify them if the
203 * mode at that time is different from the native mode now.
204 */
205 mode_changed = (output->original_mode != output->native_mode);
206 scale_changed = (output->original_scale != output->native_scale);
207
208 ret = output->switch_mode(output, output->native_mode);
209 if (ret < 0)
210 return ret;
211
212 output->current_scale = output->native_scale;
213
214 output->original_mode = NULL;
215 output->original_scale = 0;
216
217 weston_mode_switch_finish(output, mode_changed, scale_changed);
218
219 return 0;
220}
221
222WL_EXPORT int
223weston_output_mode_switch_to_temporary(struct weston_output *output,
224 struct weston_mode *mode,
225 int32_t scale)
226{
227 int ret;
228
229 if (!output->switch_mode)
230 return -1;
231
232 /* original_mode is the last mode non full screen clients have seen,
233 * so we shouldn't change it if we already have one set.
234 */
235 if (!output->original_mode) {
236 output->original_mode = output->native_mode;
237 output->original_scale = output->native_scale;
238 }
239 ret = output->switch_mode(output, mode);
240 if (ret < 0)
241 return ret;
242
243 output->current_scale = scale;
244
245 weston_mode_switch_finish(output, 0, 0);
246
247 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800248}
249
Benjamin Franzke06286262011-05-06 19:12:33 +0200250static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300251region_init_infinite(pixman_region32_t *region)
252{
253 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
254 UINT32_MAX, UINT32_MAX);
255}
256
Pekka Paalanene67858b2013-04-25 13:57:42 +0300257static struct weston_subsurface *
258weston_surface_to_subsurface(struct weston_surface *surface);
259
Jason Ekstranda7af7042013-10-12 22:38:11 -0500260WL_EXPORT struct weston_view *
261weston_view_create(struct weston_surface *surface)
262{
263 struct weston_view *view;
264
Bryce Harringtonde16d892014-11-20 22:21:57 -0800265 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500266 if (view == NULL)
267 return NULL;
268
269 view->surface = surface;
270
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 /* Assign to surface */
272 wl_list_insert(&surface->views, &view->surface_link);
273
274 wl_signal_init(&view->destroy_signal);
275 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300276 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500277
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 pixman_region32_init(&view->clip);
279
280 view->alpha = 1.0;
281 pixman_region32_init(&view->transform.opaque);
282
283 wl_list_init(&view->geometry.transformation_list);
284 wl_list_insert(&view->geometry.transformation_list,
285 &view->transform.position.link);
286 weston_matrix_init(&view->transform.position.matrix);
287 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200288 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289 pixman_region32_init(&view->transform.boundingbox);
290 view->transform.dirty = 1;
291
Jason Ekstranda7af7042013-10-12 22:38:11 -0500292 return view;
293}
294
Jason Ekstrand108865d2014-06-26 10:04:49 -0700295struct weston_frame_callback {
296 struct wl_resource *resource;
297 struct wl_list link;
298};
299
Pekka Paalanen133e4392014-09-23 22:08:46 -0400300struct weston_presentation_feedback {
301 struct wl_resource *resource;
302
303 /* XXX: could use just wl_resource_get_link() instead */
304 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200305
306 /* The per-surface feedback flags */
307 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400308};
309
310static void
311weston_presentation_feedback_discard(
312 struct weston_presentation_feedback *feedback)
313{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200314 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400315 wl_resource_destroy(feedback->resource);
316}
317
318static void
319weston_presentation_feedback_discard_list(struct wl_list *list)
320{
321 struct weston_presentation_feedback *feedback, *tmp;
322
323 wl_list_for_each_safe(feedback, tmp, list, link)
324 weston_presentation_feedback_discard(feedback);
325}
326
327static void
328weston_presentation_feedback_present(
329 struct weston_presentation_feedback *feedback,
330 struct weston_output *output,
331 uint32_t refresh_nsec,
332 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200333 uint64_t seq,
334 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400335{
336 struct wl_client *client = wl_resource_get_client(feedback->resource);
337 struct wl_resource *o;
338 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400339
340 wl_resource_for_each(o, &output->resource_list) {
341 if (wl_resource_get_client(o) != client)
342 continue;
343
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200344 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400345 }
346
347 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200348 wp_presentation_feedback_send_presented(feedback->resource,
349 secs >> 32, secs & 0xffffffff,
350 ts->tv_nsec,
351 refresh_nsec,
352 seq >> 32, seq & 0xffffffff,
353 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400354 wl_resource_destroy(feedback->resource);
355}
356
357static void
358weston_presentation_feedback_present_list(struct wl_list *list,
359 struct weston_output *output,
360 uint32_t refresh_nsec,
361 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200362 uint64_t seq,
363 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400364{
365 struct weston_presentation_feedback *feedback, *tmp;
366
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200367 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200368 wl_list_empty(list));
369
Pekka Paalanen133e4392014-09-23 22:08:46 -0400370 wl_list_for_each_safe(feedback, tmp, list, link)
371 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200372 refresh_nsec, ts, seq,
373 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400374}
375
Jason Ekstrand7b982072014-05-20 14:33:03 -0500376static void
377surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
378{
379 struct weston_surface_state *state =
380 container_of(listener, struct weston_surface_state,
381 buffer_destroy_listener);
382
383 state->buffer = NULL;
384}
385
386static void
387weston_surface_state_init(struct weston_surface_state *state)
388{
389 state->newly_attached = 0;
390 state->buffer = NULL;
391 state->buffer_destroy_listener.notify =
392 surface_state_handle_buffer_destroy;
393 state->sx = 0;
394 state->sy = 0;
395
Derek Foreman152254b2015-11-26 14:17:48 -0600396 pixman_region32_init(&state->damage_surface);
397 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500398 pixman_region32_init(&state->opaque);
399 region_init_infinite(&state->input);
400
401 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400402 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500403
404 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
405 state->buffer_viewport.buffer.scale = 1;
406 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
407 state->buffer_viewport.surface.width = -1;
408 state->buffer_viewport.changed = 0;
409}
410
411static void
412weston_surface_state_fini(struct weston_surface_state *state)
413{
414 struct weston_frame_callback *cb, *next;
415
416 wl_list_for_each_safe(cb, next,
417 &state->frame_callback_list, link)
418 wl_resource_destroy(cb->resource);
419
Pekka Paalanen133e4392014-09-23 22:08:46 -0400420 weston_presentation_feedback_discard_list(&state->feedback_list);
421
Jason Ekstrand7b982072014-05-20 14:33:03 -0500422 pixman_region32_fini(&state->input);
423 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600424 pixman_region32_fini(&state->damage_surface);
425 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500426
427 if (state->buffer)
428 wl_list_remove(&state->buffer_destroy_listener.link);
429 state->buffer = NULL;
430}
431
432static void
433weston_surface_state_set_buffer(struct weston_surface_state *state,
434 struct weston_buffer *buffer)
435{
436 if (state->buffer == buffer)
437 return;
438
439 if (state->buffer)
440 wl_list_remove(&state->buffer_destroy_listener.link);
441 state->buffer = buffer;
442 if (state->buffer)
443 wl_signal_add(&state->buffer->destroy_signal,
444 &state->buffer_destroy_listener);
445}
446
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500447WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500448weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500449{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500450 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400451
Bryce Harringtonde16d892014-11-20 22:21:57 -0800452 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400453 if (surface == NULL)
454 return NULL;
455
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500456 wl_signal_init(&surface->destroy_signal);
457
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500458 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200459 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400460
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200461 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
462 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200463 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
464 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500465
466 weston_surface_state_init(&surface->pending);
467
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400468 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500469 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300470 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400471
Jason Ekstranda7af7042013-10-12 22:38:11 -0500472 wl_list_init(&surface->views);
473
474 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400475 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500476
Pekka Paalanene67858b2013-04-25 13:57:42 +0300477 wl_list_init(&surface->subsurface_list);
478 wl_list_init(&surface->subsurface_list_pending);
479
Jason Ekstrand1e059042014-10-16 10:55:19 -0500480 weston_matrix_init(&surface->buffer_to_surface_matrix);
481 weston_matrix_init(&surface->surface_to_buffer_matrix);
482
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400483 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500484}
485
Alex Wu8811bf92012-02-28 18:07:54 +0800486WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500487weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200488 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500489{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100490 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500491}
492
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400493WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500494weston_view_to_global_float(struct weston_view *view,
495 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200496{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500497 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200498 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
499
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200501
502 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200503 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700504 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200505 v.f[3]);
506 *x = 0;
507 *y = 0;
508 return;
509 }
510
511 *x = v.f[0] / v.f[3];
512 *y = v.f[1] / v.f[3];
513 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500514 *x = sx + view->geometry.x;
515 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200516 }
517}
518
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500519WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200520weston_transformed_coord(int width, int height,
521 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200522 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200523 float sx, float sy, float *bx, float *by)
524{
525 switch (transform) {
526 case WL_OUTPUT_TRANSFORM_NORMAL:
527 default:
528 *bx = sx;
529 *by = sy;
530 break;
531 case WL_OUTPUT_TRANSFORM_FLIPPED:
532 *bx = width - sx;
533 *by = sy;
534 break;
535 case WL_OUTPUT_TRANSFORM_90:
536 *bx = height - sy;
537 *by = sx;
538 break;
539 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
540 *bx = height - sy;
541 *by = width - sx;
542 break;
543 case WL_OUTPUT_TRANSFORM_180:
544 *bx = width - sx;
545 *by = height - sy;
546 break;
547 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
548 *bx = sx;
549 *by = height - sy;
550 break;
551 case WL_OUTPUT_TRANSFORM_270:
552 *bx = sy;
553 *by = width - sx;
554 break;
555 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
556 *bx = sy;
557 *by = sx;
558 break;
559 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200560
561 *bx *= scale;
562 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200563}
564
565WL_EXPORT pixman_box32_t
566weston_transformed_rect(int width, int height,
567 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200568 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200569 pixman_box32_t rect)
570{
571 float x1, x2, y1, y2;
572
573 pixman_box32_t ret;
574
Alexander Larsson4ea95522013-05-22 14:41:37 +0200575 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200576 rect.x1, rect.y1, &x1, &y1);
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.x2, rect.y2, &x2, &y2);
579
580 if (x1 <= x2) {
581 ret.x1 = x1;
582 ret.x2 = x2;
583 } else {
584 ret.x1 = x2;
585 ret.x2 = x1;
586 }
587
588 if (y1 <= y2) {
589 ret.y1 = y1;
590 ret.y2 = y2;
591 } else {
592 ret.y1 = y2;
593 ret.y2 = y1;
594 }
595
596 return ret;
597}
598
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600599/** Transform a region by a matrix, restricted to axis-aligned transformations
600 *
601 * Warning: This function does not work for projective, affine, or matrices
602 * that encode arbitrary rotations. Only 90-degree step rotations are
603 * supported.
604 */
605WL_EXPORT void
606weston_matrix_transform_region(pixman_region32_t *dest,
607 struct weston_matrix *matrix,
608 pixman_region32_t *src)
609{
610 pixman_box32_t *src_rects, *dest_rects;
611 int nrects, i;
612
613 src_rects = pixman_region32_rectangles(src, &nrects);
614 dest_rects = malloc(nrects * sizeof(*dest_rects));
615 if (!dest_rects)
616 return;
617
618 for (i = 0; i < nrects; i++) {
619 struct weston_vector vec1 = {{
620 src_rects[i].x1, src_rects[i].y1, 0, 1
621 }};
622 weston_matrix_transform(matrix, &vec1);
623 vec1.f[0] /= vec1.f[3];
624 vec1.f[1] /= vec1.f[3];
625
626 struct weston_vector vec2 = {{
627 src_rects[i].x2, src_rects[i].y2, 0, 1
628 }};
629 weston_matrix_transform(matrix, &vec2);
630 vec2.f[0] /= vec2.f[3];
631 vec2.f[1] /= vec2.f[3];
632
633 if (vec1.f[0] < vec2.f[0]) {
634 dest_rects[i].x1 = floor(vec1.f[0]);
635 dest_rects[i].x2 = ceil(vec2.f[0]);
636 } else {
637 dest_rects[i].x1 = floor(vec2.f[0]);
638 dest_rects[i].x2 = ceil(vec1.f[0]);
639 }
640
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600641 if (vec1.f[1] < vec2.f[1]) {
642 dest_rects[i].y1 = floor(vec1.f[1]);
643 dest_rects[i].y2 = ceil(vec2.f[1]);
644 } else {
645 dest_rects[i].y1 = floor(vec2.f[1]);
646 dest_rects[i].y2 = ceil(vec1.f[1]);
647 }
648 }
649
650 pixman_region32_clear(dest);
651 pixman_region32_init_rects(dest, dest_rects, nrects);
652 free(dest_rects);
653}
654
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200655WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500656weston_transformed_region(int width, int height,
657 enum wl_output_transform transform,
658 int32_t scale,
659 pixman_region32_t *src, pixman_region32_t *dest)
660{
661 pixman_box32_t *src_rects, *dest_rects;
662 int nrects, i;
663
664 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
665 if (src != dest)
666 pixman_region32_copy(dest, src);
667 return;
668 }
669
670 src_rects = pixman_region32_rectangles(src, &nrects);
671 dest_rects = malloc(nrects * sizeof(*dest_rects));
672 if (!dest_rects)
673 return;
674
675 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
676 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
677 } else {
678 for (i = 0; i < nrects; i++) {
679 switch (transform) {
680 default:
681 case WL_OUTPUT_TRANSFORM_NORMAL:
682 dest_rects[i].x1 = src_rects[i].x1;
683 dest_rects[i].y1 = src_rects[i].y1;
684 dest_rects[i].x2 = src_rects[i].x2;
685 dest_rects[i].y2 = src_rects[i].y2;
686 break;
687 case WL_OUTPUT_TRANSFORM_90:
688 dest_rects[i].x1 = height - src_rects[i].y2;
689 dest_rects[i].y1 = src_rects[i].x1;
690 dest_rects[i].x2 = height - src_rects[i].y1;
691 dest_rects[i].y2 = src_rects[i].x2;
692 break;
693 case WL_OUTPUT_TRANSFORM_180:
694 dest_rects[i].x1 = width - src_rects[i].x2;
695 dest_rects[i].y1 = height - src_rects[i].y2;
696 dest_rects[i].x2 = width - src_rects[i].x1;
697 dest_rects[i].y2 = height - src_rects[i].y1;
698 break;
699 case WL_OUTPUT_TRANSFORM_270:
700 dest_rects[i].x1 = src_rects[i].y1;
701 dest_rects[i].y1 = width - src_rects[i].x2;
702 dest_rects[i].x2 = src_rects[i].y2;
703 dest_rects[i].y2 = width - src_rects[i].x1;
704 break;
705 case WL_OUTPUT_TRANSFORM_FLIPPED:
706 dest_rects[i].x1 = width - src_rects[i].x2;
707 dest_rects[i].y1 = src_rects[i].y1;
708 dest_rects[i].x2 = width - src_rects[i].x1;
709 dest_rects[i].y2 = src_rects[i].y2;
710 break;
711 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
712 dest_rects[i].x1 = height - src_rects[i].y2;
713 dest_rects[i].y1 = width - src_rects[i].x2;
714 dest_rects[i].x2 = height - src_rects[i].y1;
715 dest_rects[i].y2 = width - src_rects[i].x1;
716 break;
717 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
718 dest_rects[i].x1 = src_rects[i].x1;
719 dest_rects[i].y1 = height - src_rects[i].y2;
720 dest_rects[i].x2 = src_rects[i].x2;
721 dest_rects[i].y2 = height - src_rects[i].y1;
722 break;
723 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
724 dest_rects[i].x1 = src_rects[i].y1;
725 dest_rects[i].y1 = src_rects[i].x1;
726 dest_rects[i].x2 = src_rects[i].y2;
727 dest_rects[i].y2 = src_rects[i].x2;
728 break;
729 }
730 }
731 }
732
733 if (scale != 1) {
734 for (i = 0; i < nrects; i++) {
735 dest_rects[i].x1 *= scale;
736 dest_rects[i].x2 *= scale;
737 dest_rects[i].y1 *= scale;
738 dest_rects[i].y2 *= scale;
739 }
740 }
741
742 pixman_region32_clear(dest);
743 pixman_region32_init_rects(dest, dest_rects, nrects);
744 free(dest_rects);
745}
746
Jonny Lamb74130762013-11-26 18:19:46 +0100747static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300748viewport_surface_to_buffer(struct weston_surface *surface,
749 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100750{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200751 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200752 double src_width, src_height;
753 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200754
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200755 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
756 if (vp->surface.width == -1) {
757 *bx = sx;
758 *by = sy;
759 return;
760 }
Jonny Lamb74130762013-11-26 18:19:46 +0100761
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200762 src_x = 0.0;
763 src_y = 0.0;
764 src_width = surface->width_from_buffer;
765 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100766 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200767 src_x = wl_fixed_to_double(vp->buffer.src_x);
768 src_y = wl_fixed_to_double(vp->buffer.src_y);
769 src_width = wl_fixed_to_double(vp->buffer.src_width);
770 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100771 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200772
773 *bx = sx * src_width / surface->width + src_x;
774 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100775}
776
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500777WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200778weston_surface_to_buffer_float(struct weston_surface *surface,
779 float sx, float sy, float *bx, float *by)
780{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200781 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
782
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300783 /* first transform coordinates if the viewport is set */
784 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100785
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500786 weston_transformed_coord(surface->width_from_buffer,
787 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200788 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100789 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200790}
791
Derek Foreman99739672015-12-03 16:38:11 -0600792/** Transform a rectangle from surface coordinates to buffer coordinates
793 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300794 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600795 * from.
796 * \param rect The rectangle to transform.
797 * \return The transformed rectangle.
798 *
799 * Viewport and buffer transformations can only do translation, scaling,
800 * and rotations in 90-degree steps. Therefore the only loss in the
801 * conversion is coordinate rounding.
802 *
803 * However, some coordinate rounding takes place as an intermediate
804 * step before the buffer scale factor is applied, so the rectangle
805 * boundary may not be exactly as expected.
806 *
807 * This is OK for damage tracking since a little extra coverage is
808 * not a problem.
809 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200810WL_EXPORT pixman_box32_t
811weston_surface_to_buffer_rect(struct weston_surface *surface,
812 pixman_box32_t rect)
813{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200814 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100815 float xf, yf;
816
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300817 /* first transform box coordinates if the viewport is set */
818 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100819 rect.x1 = floorf(xf);
820 rect.y1 = floorf(yf);
821
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300822 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600823 rect.x2 = ceilf(xf);
824 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100825
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500826 return weston_transformed_rect(surface->width_from_buffer,
827 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200828 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200829 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200830}
831
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200832/** Transform a region from surface coordinates to buffer coordinates
833 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300834 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200835 * from.
836 * \param surface_region[in] The region in surface coordinates.
837 * \param buffer_region[out] The region converted to buffer coordinates.
838 *
839 * Buffer_region must be init'd, but will be completely overwritten.
840 *
841 * Viewport and buffer transformations can only do translation, scaling,
842 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600843 * conversion is from the coordinate rounding that takes place in
844 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200845 */
846WL_EXPORT void
847weston_surface_to_buffer_region(struct weston_surface *surface,
848 pixman_region32_t *surface_region,
849 pixman_region32_t *buffer_region)
850{
851 pixman_box32_t *src_rects, *dest_rects;
852 int nrects, i;
853
854 src_rects = pixman_region32_rectangles(surface_region, &nrects);
855 dest_rects = malloc(nrects * sizeof(*dest_rects));
856 if (!dest_rects)
857 return;
858
859 for (i = 0; i < nrects; i++) {
860 dest_rects[i] = weston_surface_to_buffer_rect(surface,
861 src_rects[i]);
862 }
863
864 pixman_region32_fini(buffer_region);
865 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
866 free(dest_rects);
867}
868
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200869WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500870weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400871 struct weston_plane *plane)
872{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500873 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400874 return;
875
Jason Ekstranda7af7042013-10-12 22:38:11 -0500876 weston_view_damage_below(view);
877 view->plane = plane;
878 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400879}
880
Pekka Paalanen51723d52015-02-17 13:10:01 +0200881/** Inflict damage on the plane where the view is visible.
882 *
883 * \param view The view that causes the damage.
884 *
885 * If the view is currently on a plane (including the primary plane),
886 * take the view's boundingbox, subtract all the opaque views that cover it,
887 * and add the remaining region as damage to the plane. This corresponds
888 * to the damage inflicted to the plane if this view disappeared.
889 *
890 * A repaint is scheduled for this view.
891 *
892 * The region of all opaque views covering this view is stored in
893 * weston_view::clip and updated by view_accumulate_damage() during
894 * weston_output_repaint(). Specifically, that region matches the
895 * scenegraph as it was last painted.
896 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400897WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500898weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200899{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500900 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200901
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500902 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200903 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500904 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800905 if (view->plane)
906 pixman_region32_union(&view->plane->damage,
907 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500908 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800909 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200910}
911
Bryce Harrington3f650b82015-12-23 11:01:58 -0800912/**
913 * \param es The surface
914 * \param mask The new set of outputs for the surface
915 *
916 * Sets the surface's set of outputs to the ones specified by
917 * the new output mask provided. Identifies the outputs that
918 * have changed, the posts enter and leave events for these
919 * outputs as appropriate.
920 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400921static void
922weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
923{
924 uint32_t different = es->output_mask ^ mask;
925 uint32_t entered = mask & different;
926 uint32_t left = es->output_mask & different;
927 struct weston_output *output;
928 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500929 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400930
931 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500932 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400933 return;
934 if (different == 0)
935 return;
936
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500937 client = wl_resource_get_client(es->resource);
938
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400939 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800940 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400941 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500942 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400943 client);
944 if (resource == NULL)
945 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800946 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500947 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800948 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500949 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400950 }
951}
952
Bryce Harrington3f650b82015-12-23 11:01:58 -0800953/** Recalculate which output(s) the surface has views displayed on
954 *
955 * \param es The surface to remap to outputs
956 *
957 * Finds the output that is showing the largest amount of one
958 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300959 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800960 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300961 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800962 * in the output_mask for the surface.
963 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400964static void
965weston_surface_assign_output(struct weston_surface *es)
966{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 struct weston_output *new_output;
968 struct weston_view *view;
969 pixman_region32_t region;
970 uint32_t max, area, mask;
971 pixman_box32_t *e;
972
973 new_output = NULL;
974 max = 0;
975 mask = 0;
976 pixman_region32_init(&region);
977 wl_list_for_each(view, &es->views, surface_link) {
978 if (!view->output)
979 continue;
980
981 pixman_region32_intersect(&region, &view->transform.boundingbox,
982 &view->output->region);
983
984 e = pixman_region32_extents(&region);
985 area = (e->x2 - e->x1) * (e->y2 - e->y1);
986
987 mask |= view->output_mask;
988
989 if (area >= max) {
990 new_output = view->output;
991 max = area;
992 }
993 }
994 pixman_region32_fini(&region);
995
996 es->output = new_output;
997 weston_surface_update_output_mask(es, mask);
998}
999
Bryce Harrington3f650b82015-12-23 11:01:58 -08001000/** Recalculate which output(s) the view is displayed on
1001 *
1002 * \param ev The view to remap to outputs
1003 *
1004 * Identifies the set of outputs that the view is visible on,
1005 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001006 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001007 *
1008 * Also does the same for the view's surface. See
1009 * weston_surface_assign_output().
1010 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011static void
1012weston_view_assign_output(struct weston_view *ev)
1013{
1014 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001015 struct weston_output *output, *new_output;
1016 pixman_region32_t region;
1017 uint32_t max, area, mask;
1018 pixman_box32_t *e;
1019
1020 new_output = NULL;
1021 max = 0;
1022 mask = 0;
1023 pixman_region32_init(&region);
1024 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001025 if (output->destroying)
1026 continue;
1027
Jason Ekstranda7af7042013-10-12 22:38:11 -05001028 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001029 &output->region);
1030
1031 e = pixman_region32_extents(&region);
1032 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1033
1034 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001035 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001036
1037 if (area >= max) {
1038 new_output = output;
1039 max = area;
1040 }
1041 }
1042 pixman_region32_fini(&region);
1043
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044 ev->output = new_output;
1045 ev->output_mask = mask;
1046
1047 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001048}
1049
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001050static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001051weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1052 int from_x, int from_y, int *to_x, int *to_y)
1053{
1054 float x, y;
1055
1056 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1057 weston_view_from_global_float(to, x, y, &x, &y);
1058
1059 *to_x = round(x);
1060 *to_y = round(y);
1061}
1062
1063static void
1064weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1065{
1066 pixman_box32_t *a;
1067 pixman_box32_t b;
1068
1069 a = pixman_region32_extents(&from->geometry.scissor);
1070
1071 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1072 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1073
1074 pixman_region32_fini(&to->geometry.scissor);
1075 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1076}
1077
1078static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001079view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001081{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001082 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1083 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001084 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001085 { inbox->x1, inbox->y1 },
1086 { inbox->x1, inbox->y2 },
1087 { inbox->x2, inbox->y1 },
1088 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001089 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001090 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001091 int i;
1092
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001093 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001094 /* avoid rounding empty bbox to 1x1 */
1095 pixman_region32_init(bbox);
1096 return;
1097 }
1098
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001099 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001100 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001101 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001102 if (x < min_x)
1103 min_x = x;
1104 if (x > max_x)
1105 max_x = x;
1106 if (y < min_y)
1107 min_y = y;
1108 if (y > max_y)
1109 max_y = y;
1110 }
1111
Pekka Paalanen219b9822012-02-08 15:38:37 +02001112 int_x = floorf(min_x);
1113 int_y = floorf(min_y);
1114 pixman_region32_init_rect(bbox, int_x, int_y,
1115 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001116}
1117
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001118static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001120{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001122
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001123 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 view->geometry.x = roundf(view->geometry.x);
1125 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001126
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001127 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1129 view->transform.position.matrix.d[12] = view->geometry.x;
1130 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001131
Jason Ekstranda7af7042013-10-12 22:38:11 -05001132 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001133
Jason Ekstranda7af7042013-10-12 22:38:11 -05001134 view->transform.inverse = view->transform.position.matrix;
1135 view->transform.inverse.d[12] = -view->geometry.x;
1136 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001137
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001139 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001140 view->surface->width,
1141 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001142 if (view->geometry.scissor_enabled)
1143 pixman_region32_intersect(&view->transform.boundingbox,
1144 &view->transform.boundingbox,
1145 &view->geometry.scissor);
1146
1147 pixman_region32_translate(&view->transform.boundingbox,
1148 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001149
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 if (view->alpha == 1.0) {
1151 pixman_region32_copy(&view->transform.opaque,
1152 &view->surface->opaque);
1153 pixman_region32_translate(&view->transform.opaque,
1154 view->geometry.x,
1155 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001156 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001157}
1158
1159static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001161{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162 struct weston_view *parent = view->geometry.parent;
1163 struct weston_matrix *matrix = &view->transform.matrix;
1164 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001165 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001166 pixman_region32_t surfregion;
1167 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001168
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001170
1171 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1173 view->transform.position.matrix.d[12] = view->geometry.x;
1174 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001175
1176 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001177 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001178 weston_matrix_multiply(matrix, &tform->matrix);
1179
Pekka Paalanen483243f2013-03-08 14:56:50 +02001180 if (parent)
1181 weston_matrix_multiply(matrix, &parent->transform.matrix);
1182
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001183 if (weston_matrix_invert(inverse, matrix) < 0) {
1184 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001185 weston_log("error: weston_view %p"
1186 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001187 return -1;
1188 }
1189
Pekka Paalanen380adf52015-02-16 14:39:11 +02001190 pixman_region32_init_rect(&surfregion, 0, 0,
1191 view->surface->width, view->surface->height);
1192 if (view->geometry.scissor_enabled)
1193 pixman_region32_intersect(&surfregion, &surfregion,
1194 &view->geometry.scissor);
1195 surfbox = pixman_region32_extents(&surfregion);
1196
1197 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1198 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001199
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001200 return 0;
1201}
1202
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001203static struct weston_layer *
1204get_view_layer(struct weston_view *view)
1205{
1206 if (view->parent_view)
1207 return get_view_layer(view->parent_view);
1208 return view->layer_link.layer;
1209}
1210
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001211WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001213{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001215 struct weston_layer *layer;
1216 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001217
Jason Ekstranda7af7042013-10-12 22:38:11 -05001218 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001219 return;
1220
Pekka Paalanen483243f2013-03-08 14:56:50 +02001221 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001223
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001225
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 pixman_region32_fini(&view->transform.boundingbox);
1229 pixman_region32_fini(&view->transform.opaque);
1230 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001231
Pekka Paalanencd403622012-01-25 13:37:39 +02001232 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 if (view->geometry.transformation_list.next ==
1234 &view->transform.position.link &&
1235 view->geometry.transformation_list.prev ==
1236 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001237 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001238 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001239 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 if (weston_view_update_transform_enable(view) < 0)
1241 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001242 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001243
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001244 layer = get_view_layer(view);
1245 if (layer) {
1246 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001247 pixman_region32_intersect(&view->transform.boundingbox,
1248 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001249 pixman_region32_intersect(&view->transform.opaque,
1250 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001251 pixman_region32_fini(&mask);
1252 }
1253
Pekka Paalanen380adf52015-02-16 14:39:11 +02001254 if (parent) {
1255 if (parent->geometry.scissor_enabled) {
1256 view->geometry.scissor_enabled = true;
1257 weston_view_transfer_scissor(parent, view);
1258 } else {
1259 view->geometry.scissor_enabled = false;
1260 }
1261 }
1262
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001264
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001266
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 wl_signal_emit(&view->surface->compositor->transform_signal,
1268 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001269}
1270
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001271WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001273{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001275
1276 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 * The invariant: if view->geometry.dirty, then all views
1278 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001279 * Corollary: if not parent->geometry.dirty, then all ancestors
1280 * are not dirty.
1281 */
1282
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001284 return;
1285
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001287
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001289 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001291}
1292
1293WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294weston_view_to_global_fixed(struct weston_view *view,
1295 wl_fixed_t vx, wl_fixed_t vy,
1296 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001297{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001298 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001299
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300 weston_view_to_global_float(view,
1301 wl_fixed_to_double(vx),
1302 wl_fixed_to_double(vy),
1303 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001304 *x = wl_fixed_from_double(xf);
1305 *y = wl_fixed_from_double(yf);
1306}
1307
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001308WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309weston_view_from_global_float(struct weston_view *view,
1310 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001311{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001313 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1314
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001316
1317 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001318 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001320 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 *vx = 0;
1322 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001323 return;
1324 }
1325
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326 *vx = v.f[0] / v.f[3];
1327 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001328 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329 *vx = x - view->geometry.x;
1330 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001331 }
1332}
1333
1334WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335weston_view_from_global_fixed(struct weston_view *view,
1336 wl_fixed_t x, wl_fixed_t y,
1337 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001338{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001340
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 weston_view_from_global_float(view,
1342 wl_fixed_to_double(x),
1343 wl_fixed_to_double(y),
1344 &vxf, &vyf);
1345 *vx = wl_fixed_from_double(vxf);
1346 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001347}
1348
1349WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350weston_view_from_global(struct weston_view *view,
1351 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001352{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001354
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1356 *vx = floorf(vxf);
1357 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001358}
1359
Bryce Harrington3f650b82015-12-23 11:01:58 -08001360/**
1361 * \param surface The surface to be repainted
1362 *
1363 * Marks the output(s) that the surface is shown on as needing to be
1364 * repainted. See weston_output_schedule_repaint().
1365 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001366WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001367weston_surface_schedule_repaint(struct weston_surface *surface)
1368{
1369 struct weston_output *output;
1370
1371 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001372 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001373 weston_output_schedule_repaint(output);
1374}
1375
Bryce Harrington3f650b82015-12-23 11:01:58 -08001376/**
1377 * \param view The view to be repainted
1378 *
1379 * Marks the output(s) that the view is shown on as needing to be
1380 * repainted. See weston_output_schedule_repaint().
1381 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001382WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383weston_view_schedule_repaint(struct weston_view *view)
1384{
1385 struct weston_output *output;
1386
1387 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001388 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 weston_output_schedule_repaint(output);
1390}
1391
Pekka Paalanene508ce62015-02-19 13:59:55 +02001392/**
1393 * XXX: This function does it the wrong way.
1394 * surface->damage is the damage from the client, and causes
1395 * surface_flush_damage() to copy pixels. No window management action can
1396 * cause damage to the client-provided content, warranting re-upload!
1397 *
1398 * Instead of surface->damage, this function should record the damage
1399 * with all the views for this surface to avoid extraneous texture
1400 * uploads.
1401 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001403weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001404{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001405 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 0, 0, surface->width,
1407 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001408
Kristian Høgsberg98238702012-08-03 16:29:12 -04001409 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001410}
1411
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001412WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001414{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001415 if (view->geometry.x == x && view->geometry.y == y)
1416 return;
1417
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 view->geometry.x = x;
1419 view->geometry.y = y;
1420 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001421}
1422
Pekka Paalanen483243f2013-03-08 14:56:50 +02001423static void
1424transform_parent_handle_parent_destroy(struct wl_listener *listener,
1425 void *data)
1426{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001427 struct weston_view *view =
1428 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001429 geometry.parent_destroy_listener);
1430
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001432}
1433
1434WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001436 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001437{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 if (view->geometry.parent) {
1439 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1440 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001441
1442 if (!parent)
1443 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001444 }
1445
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001447
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001449 transform_parent_handle_parent_destroy;
1450 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001451 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001453 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001455 }
1456
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 weston_view_geometry_dirty(view);
1458}
1459
Pekka Paalanen380adf52015-02-16 14:39:11 +02001460/** Set a clip mask rectangle on a view
1461 *
1462 * \param view The view to set the clip mask on.
1463 * \param x Top-left corner X coordinate of the clip rectangle.
1464 * \param y Top-left corner Y coordinate of the clip rectangle.
1465 * \param width Width of the clip rectangle, non-negative.
1466 * \param height Height of the clip rectangle, non-negative.
1467 *
1468 * A shell may set a clip mask rectangle on a view. Everything outside
1469 * the rectangle is cut away for input and output purposes: it is
1470 * not drawn and cannot be hit by hit-test based input like pointer
1471 * motion or touch-downs. Everything inside the rectangle will behave
1472 * normally. Clients are unaware of clipping.
1473 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001474 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001475 * mask rectangle does not affect the view position, the view is positioned
1476 * as it would be without a clip. The clip also does not change
1477 * weston_surface::width,height.
1478 *
1479 * The clip mask rectangle is part of transformation inheritance
1480 * (weston_view_set_transform_parent()). A clip set in the root of the
1481 * transformation inheritance tree will affect all views in the tree.
1482 * A clip can be set only on the root view. Attempting to set a clip
1483 * on view that has a transformation parent will fail. Assigning a parent
1484 * to a view that has a clip set will cause the clip to be forgotten.
1485 *
1486 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1487 * on the additional transformations in the child views. These transformations
1488 * may not rotate the coordinate axes, i.e., only translation and scaling
1489 * are allowed. Violating this restriction causes the clipping to malfunction.
1490 * Furthermore, using scaling may cause rounding errors in child clipping.
1491 *
1492 * The clip mask rectangle is not automatically adjusted based on
1493 * wl_surface.attach dx and dy arguments.
1494 *
1495 * A clip mask rectangle can be set only if the compositor capability
1496 * WESTON_CAP_VIEW_CLIP_MASK is present.
1497 *
1498 * This function sets the clip mask rectangle and schedules a repaint for
1499 * the view.
1500 */
1501WL_EXPORT void
1502weston_view_set_mask(struct weston_view *view,
1503 int x, int y, int width, int height)
1504{
1505 struct weston_compositor *compositor = view->surface->compositor;
1506
1507 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1508 weston_log("%s not allowed without capability!\n", __func__);
1509 return;
1510 }
1511
1512 if (view->geometry.parent) {
1513 weston_log("view %p has a parent, clip forbidden!\n", view);
1514 return;
1515 }
1516
1517 if (width < 0 || height < 0) {
1518 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1519 x, y, width, height);
1520 return;
1521 }
1522
1523 pixman_region32_fini(&view->geometry.scissor);
1524 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1525 view->geometry.scissor_enabled = true;
1526 weston_view_geometry_dirty(view);
1527 weston_view_schedule_repaint(view);
1528}
1529
1530/** Remove the clip mask from a view
1531 *
1532 * \param view The view to remove the clip mask from.
1533 *
1534 * Removed the clip mask rectangle and schedules a repaint.
1535 *
1536 * \sa weston_view_set_mask
1537 */
1538WL_EXPORT void
1539weston_view_set_mask_infinite(struct weston_view *view)
1540{
1541 view->geometry.scissor_enabled = false;
1542 weston_view_geometry_dirty(view);
1543 weston_view_schedule_repaint(view);
1544}
1545
Derek Foreman280e7dd2014-10-03 13:13:42 -05001546WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547weston_view_is_mapped(struct weston_view *view)
1548{
1549 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001550 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001552 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001553}
1554
Derek Foreman280e7dd2014-10-03 13:13:42 -05001555WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001556weston_surface_is_mapped(struct weston_surface *surface)
1557{
1558 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001559 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001560 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001561 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001562}
1563
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001564static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001565surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001566{
1567 struct weston_view *view;
1568
1569 if (surface->width == width && surface->height == height)
1570 return;
1571
1572 surface->width = width;
1573 surface->height = height;
1574
1575 wl_list_for_each(view, &surface->views, surface_link)
1576 weston_view_geometry_dirty(view);
1577}
1578
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001579WL_EXPORT void
1580weston_surface_set_size(struct weston_surface *surface,
1581 int32_t width, int32_t height)
1582{
1583 assert(!surface->resource);
1584 surface_set_size(surface, width, height);
1585}
1586
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001587static int
1588fixed_round_up_to_int(wl_fixed_t f)
1589{
1590 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1591}
1592
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001593static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001594convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1595 int32_t width, int32_t height,
1596 uint32_t transform,
1597 int32_t scale)
1598{
1599 assert(scale > 0);
1600
1601 switch (transform) {
1602 case WL_OUTPUT_TRANSFORM_NORMAL:
1603 case WL_OUTPUT_TRANSFORM_180:
1604 case WL_OUTPUT_TRANSFORM_FLIPPED:
1605 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1606 *width_out = width / scale;
1607 *height_out = height / scale;
1608 break;
1609 case WL_OUTPUT_TRANSFORM_90:
1610 case WL_OUTPUT_TRANSFORM_270:
1611 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1612 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1613 *width_out = height / scale;
1614 *height_out = width / scale;
1615 break;
1616 default:
1617 assert(0 && "invalid transform");
1618 }
1619}
1620
1621static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001622weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001623{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001624 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001625
1626 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001627 surface->width_from_buffer = 0;
1628 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001629 return;
1630 }
1631
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001632 convert_size_by_transform_scale(&surface->width_from_buffer,
1633 &surface->height_from_buffer,
1634 surface->buffer_ref.buffer->width,
1635 surface->buffer_ref.buffer->height,
1636 vp->buffer.transform,
1637 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001638}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001639
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001640static void
1641weston_surface_update_size(struct weston_surface *surface)
1642{
1643 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1644 int32_t width, height;
1645
1646 width = surface->width_from_buffer;
1647 height = surface->height_from_buffer;
1648
1649 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001650 surface_set_size(surface,
1651 vp->surface.width, vp->surface.height);
1652 return;
1653 }
1654
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001655 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001656 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1657 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1658
1659 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001660 return;
1661 }
1662
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001663 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001664}
1665
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001666WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001667weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001668{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001669 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001670
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001671 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001672
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001673 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001674}
1675
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676WL_EXPORT struct weston_view *
1677weston_compositor_pick_view(struct weston_compositor *compositor,
1678 wl_fixed_t x, wl_fixed_t y,
1679 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001680{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001682 wl_fixed_t view_x, view_y;
1683 int view_ix, view_iy;
1684 int ix = wl_fixed_to_int(x);
1685 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001686
Jason Ekstranda7af7042013-10-12 22:38:11 -05001687 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001688 if (!pixman_region32_contains_point(
1689 &view->transform.boundingbox, ix, iy, NULL))
1690 continue;
1691
1692 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1693 view_ix = wl_fixed_to_int(view_x);
1694 view_iy = wl_fixed_to_int(view_y);
1695
1696 if (!pixman_region32_contains_point(&view->surface->input,
1697 view_ix, view_iy, NULL))
1698 continue;
1699
Pekka Paalanen380adf52015-02-16 14:39:11 +02001700 if (view->geometry.scissor_enabled &&
1701 !pixman_region32_contains_point(&view->geometry.scissor,
1702 view_ix, view_iy, NULL))
1703 continue;
1704
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001705 *vx = view_x;
1706 *vy = view_y;
1707 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001708 }
1709
Derek Foremanf9318d12015-05-11 15:40:11 -05001710 *vx = wl_fixed_from_int(-1000000);
1711 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001712 return NULL;
1713}
1714
1715static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001716weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001717{
Daniel Stone37816df2012-05-16 18:45:18 +01001718 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001719
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001720 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001721 return;
1722
Daniel Stone37816df2012-05-16 18:45:18 +01001723 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001724 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001725}
1726
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001727WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001728weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001729{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001730 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001731
Jason Ekstranda7af7042013-10-12 22:38:11 -05001732 if (!weston_view_is_mapped(view))
1733 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001734
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735 weston_view_damage_below(view);
1736 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001737 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001738 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001739 wl_list_remove(&view->link);
1740 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 view->output_mask = 0;
1742 weston_surface_assign_output(view->surface);
1743
1744 if (weston_surface_is_mapped(view->surface))
1745 return;
1746
1747 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001748 struct weston_touch *touch = weston_seat_get_touch(seat);
1749 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1750 struct weston_keyboard *keyboard =
1751 weston_seat_get_keyboard(seat);
1752
1753 if (keyboard && keyboard->focus == view->surface)
1754 weston_keyboard_set_focus(keyboard, NULL);
1755 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001756 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001757 if (touch && touch->focus == view)
1758 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001759 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001760}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001761
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762WL_EXPORT void
1763weston_surface_unmap(struct weston_surface *surface)
1764{
1765 struct weston_view *view;
1766
1767 wl_list_for_each(view, &surface->views, surface_link)
1768 weston_view_unmap(view);
1769 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001770}
1771
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001772static void
1773weston_surface_reset_pending_buffer(struct weston_surface *surface)
1774{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001775 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001776 surface->pending.sx = 0;
1777 surface->pending.sy = 0;
1778 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001779 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001780}
1781
Jason Ekstranda7af7042013-10-12 22:38:11 -05001782WL_EXPORT void
1783weston_view_destroy(struct weston_view *view)
1784{
1785 wl_signal_emit(&view->destroy_signal, view);
1786
1787 assert(wl_list_empty(&view->geometry.child_list));
1788
1789 if (weston_view_is_mapped(view)) {
1790 weston_view_unmap(view);
1791 weston_compositor_build_view_list(view->surface->compositor);
1792 }
1793
1794 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001795 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796
1797 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001798 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001800 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801
1802 weston_view_set_transform_parent(view, NULL);
1803
Jason Ekstranda7af7042013-10-12 22:38:11 -05001804 wl_list_remove(&view->surface_link);
1805
1806 free(view);
1807}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001808
1809WL_EXPORT void
1810weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001811{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001812 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001814
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001815 if (--surface->ref_count > 0)
1816 return;
1817
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001818 assert(surface->resource == NULL);
1819
Pekka Paalanenca790762015-04-17 14:23:38 +03001820 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001821
Pekka Paalanene67858b2013-04-25 13:57:42 +03001822 assert(wl_list_empty(&surface->subsurface_list_pending));
1823 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001824
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1826 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001827
Jason Ekstrand7b982072014-05-20 14:33:03 -05001828 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001829
Pekka Paalanende685b82012-12-04 15:58:12 +02001830 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001831
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001832 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001833 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001834 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001835
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001836 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001837 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001838
Pekka Paalanen133e4392014-09-23 22:08:46 -04001839 weston_presentation_feedback_discard_list(&surface->feedback_list);
1840
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001841 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001842}
1843
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001844static void
1845destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001846{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001847 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001848
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001849 assert(surface);
1850
Giulio Camuffo0d379742013-11-15 22:06:15 +01001851 /* Set the resource to NULL, since we don't want to leave a
1852 * dangling pointer if the surface was refcounted and survives
1853 * the weston_surface_destroy() call. */
1854 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001855 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001856}
1857
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001858static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001859weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1860{
1861 struct weston_buffer *buffer =
1862 container_of(listener, struct weston_buffer, destroy_listener);
1863
1864 wl_signal_emit(&buffer->destroy_signal, buffer);
1865 free(buffer);
1866}
1867
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001868WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001869weston_buffer_from_resource(struct wl_resource *resource)
1870{
1871 struct weston_buffer *buffer;
1872 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001873
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001874 listener = wl_resource_get_destroy_listener(resource,
1875 weston_buffer_destroy_handler);
1876
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001877 if (listener)
1878 return container_of(listener, struct weston_buffer,
1879 destroy_listener);
1880
1881 buffer = zalloc(sizeof *buffer);
1882 if (buffer == NULL)
1883 return NULL;
1884
1885 buffer->resource = resource;
1886 wl_signal_init(&buffer->destroy_signal);
1887 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001888 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001889 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001890
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001891 return buffer;
1892}
1893
1894static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001895weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1896 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001897{
Pekka Paalanende685b82012-12-04 15:58:12 +02001898 struct weston_buffer_reference *ref =
1899 container_of(listener, struct weston_buffer_reference,
1900 destroy_listener);
1901
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001902 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001903 ref->buffer = NULL;
1904}
1905
1906WL_EXPORT void
1907weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001908 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001909{
1910 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001911 ref->buffer->busy_count--;
1912 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001913 assert(wl_resource_get_client(ref->buffer->resource));
1914 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001915 WL_BUFFER_RELEASE);
1916 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001917 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001918 }
1919
Pekka Paalanende685b82012-12-04 15:58:12 +02001920 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001921 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001922 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001923 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001924 }
1925
Pekka Paalanende685b82012-12-04 15:58:12 +02001926 ref->buffer = buffer;
1927 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1928}
1929
1930static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001931weston_surface_attach(struct weston_surface *surface,
1932 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001933{
1934 weston_buffer_reference(&surface->buffer_ref, buffer);
1935
Pekka Paalanena6421c42012-12-04 15:58:10 +02001936 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001937 if (weston_surface_is_mapped(surface))
1938 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001939 }
1940
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001941 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001942
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001943 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001944 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001945}
1946
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001947WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001948weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001949{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001950 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001951
1952 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001953 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001954}
1955
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001956WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001957weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001958{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001959 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001960
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001961 pixman_region32_union(&compositor->primary_plane.damage,
1962 &compositor->primary_plane.damage,
1963 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001964 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001965}
1966
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001967static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001968surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001969{
Pekka Paalanende685b82012-12-04 15:58:12 +02001970 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001971 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001972 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001973
Pekka Paalanenb5026542014-11-12 15:09:24 +02001974 if (weston_timeline_enabled_ &&
1975 pixman_region32_not_empty(&surface->damage))
1976 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1977 TLP_OUTPUT(surface->output), TLP_END);
1978
Jason Ekstrandef540082014-06-26 10:37:36 -07001979 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001980}
1981
1982static void
1983view_accumulate_damage(struct weston_view *view,
1984 pixman_region32_t *opaque)
1985{
1986 pixman_region32_t damage;
1987
1988 pixman_region32_init(&damage);
1989 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001990 pixman_box32_t *extents;
1991
Jason Ekstranda7af7042013-10-12 22:38:11 -05001992 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001993 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001994 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001995 pixman_region32_copy(&damage, &view->surface->damage);
1996 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02001997 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001998 }
1999
Pekka Paalanen380adf52015-02-16 14:39:11 +02002000 pixman_region32_intersect(&damage, &damage,
2001 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002002 pixman_region32_subtract(&damage, &damage, opaque);
2003 pixman_region32_union(&view->plane->damage,
2004 &view->plane->damage, &damage);
2005 pixman_region32_fini(&damage);
2006 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002007 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002008}
2009
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002010static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002011compositor_accumulate_damage(struct weston_compositor *ec)
2012{
2013 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002014 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002015 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002016
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002017 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002018
2019 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002020 pixman_region32_copy(&plane->clip, &clip);
2021
2022 pixman_region32_init(&opaque);
2023
Jason Ekstranda7af7042013-10-12 22:38:11 -05002024 wl_list_for_each(ev, &ec->view_list, link) {
2025 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002026 continue;
2027
Jason Ekstranda7af7042013-10-12 22:38:11 -05002028 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002029 }
2030
2031 pixman_region32_union(&clip, &clip, &opaque);
2032 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002033 }
2034
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002035 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002036
Jason Ekstranda7af7042013-10-12 22:38:11 -05002037 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002038 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002039
2040 wl_list_for_each(ev, &ec->view_list, link) {
2041 if (ev->surface->touched)
2042 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002043 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002044
2045 surface_flush_damage(ev->surface);
2046
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002047 /* Both the renderer and the backend have seen the buffer
2048 * by now. If renderer needs the buffer, it has its own
2049 * reference set. If the backend wants to keep the buffer
2050 * around for migrating the surface into a non-primary plane
2051 * later, keep_buffer is true. Otherwise, drop the core
2052 * reference now, and allow early buffer release. This enables
2053 * clients to use single-buffering.
2054 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002055 if (!ev->surface->keep_buffer)
2056 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002057 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002058}
2059
2060static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002061surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002062{
2063 struct weston_subsurface *sub;
2064
Pekka Paalanene67858b2013-04-25 13:57:42 +03002065 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002066 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002067 continue;
2068
Jason Ekstranda7af7042013-10-12 22:38:11 -05002069 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2070 wl_list_init(&sub->surface->views);
2071
2072 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002073 }
2074}
2075
2076static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002077surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002078{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079 struct weston_subsurface *sub;
2080 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002081
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2083 if (sub->surface == surface)
2084 continue;
2085
George Kiagiadakised04d382014-06-13 18:10:26 +02002086 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2087 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002088 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002089 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002090
2091 surface_free_unused_subsurface_views(sub->surface);
2092 }
2093}
2094
2095static void
2096view_list_add_subsurface_view(struct weston_compositor *compositor,
2097 struct weston_subsurface *sub,
2098 struct weston_view *parent)
2099{
2100 struct weston_subsurface *child;
2101 struct weston_view *view = NULL, *iv;
2102
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002103 if (!weston_surface_is_mapped(sub->surface))
2104 return;
2105
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2107 if (iv->geometry.parent == parent) {
2108 view = iv;
2109 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002110 }
2111 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112
2113 if (view) {
2114 /* Put it back in the surface's list of views */
2115 wl_list_remove(&view->surface_link);
2116 wl_list_insert(&sub->surface->views, &view->surface_link);
2117 } else {
2118 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002119 weston_view_set_position(view,
2120 sub->position.x,
2121 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002122 weston_view_set_transform_parent(view, parent);
2123 }
2124
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002125 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002126 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127
Pekka Paalanenb188e912013-11-19 14:03:35 +02002128 if (wl_list_empty(&sub->surface->subsurface_list)) {
2129 wl_list_insert(compositor->view_list.prev, &view->link);
2130 return;
2131 }
2132
2133 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2134 if (child->surface == sub->surface)
2135 wl_list_insert(compositor->view_list.prev, &view->link);
2136 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002137 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002138 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002139}
2140
2141static void
2142view_list_add(struct weston_compositor *compositor,
2143 struct weston_view *view)
2144{
2145 struct weston_subsurface *sub;
2146
2147 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002148
Pekka Paalanenb188e912013-11-19 14:03:35 +02002149 if (wl_list_empty(&view->surface->subsurface_list)) {
2150 wl_list_insert(compositor->view_list.prev, &view->link);
2151 return;
2152 }
2153
2154 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2155 if (sub->surface == view->surface)
2156 wl_list_insert(compositor->view_list.prev, &view->link);
2157 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002158 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002159 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002160}
2161
2162static void
2163weston_compositor_build_view_list(struct weston_compositor *compositor)
2164{
2165 struct weston_view *view;
2166 struct weston_layer *layer;
2167
2168 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002169 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002170 surface_stash_subsurface_views(view->surface);
2171
2172 wl_list_init(&compositor->view_list);
2173 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002174 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002175 view_list_add(compositor, view);
2176 }
2177 }
2178
2179 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002180 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002182}
2183
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002184static void
2185weston_output_take_feedback_list(struct weston_output *output,
2186 struct weston_surface *surface)
2187{
2188 struct weston_view *view;
2189 struct weston_presentation_feedback *feedback;
2190 uint32_t flags = 0xffffffff;
2191
2192 if (wl_list_empty(&surface->feedback_list))
2193 return;
2194
2195 /* All views must have the flag for the flag to survive. */
2196 wl_list_for_each(view, &surface->views, surface_link) {
2197 /* ignore views that are not on this output at all */
2198 if (view->output_mask & (1u << output->id))
2199 flags &= view->psf_flags;
2200 }
2201
2202 wl_list_for_each(feedback, &surface->feedback_list, link)
2203 feedback->psf_flags = flags;
2204
2205 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2206 wl_list_init(&surface->feedback_list);
2207}
2208
David Herrmann1edf44c2013-10-22 17:11:26 +02002209static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002210weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002211{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002212 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002213 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002214 struct weston_animation *animation, *next;
2215 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002216 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002217 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002218 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002219
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002220 if (output->destroying)
2221 return 0;
2222
Pekka Paalanenb5026542014-11-12 15:09:24 +02002223 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2224
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002225 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002227
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002228 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002229 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002230 } else {
2231 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002232 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002233 ev->psf_flags = 0;
2234 }
2235 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002236
Pekka Paalanene67858b2013-04-25 13:57:42 +03002237 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002238 wl_list_for_each(ev, &ec->view_list, link) {
2239 /* Note: This operation is safe to do multiple times on the
2240 * same surface.
2241 */
2242 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002243 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002244 &ev->surface->frame_callback_list);
2245 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002246
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002247 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002248 }
2249 }
2250
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002251 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002252
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002253 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002254 pixman_region32_intersect(&output_damage,
2255 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002256 pixman_region32_subtract(&output_damage,
2257 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002258
Scott Moreauccbf29d2012-02-22 14:21:41 -07002259 if (output->dirty)
2260 weston_output_update_matrix(output);
2261
David Herrmann1edf44c2013-10-22 17:11:26 +02002262 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002263
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002264 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002265
Kristian Høgsbergef044142011-06-21 15:02:12 -04002266 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002267
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002268 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002269
Jonas Ådahldb773762012-06-13 00:01:21 +02002270 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002271 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002272 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002273 }
2274
Scott Moreaud64cf212012-06-08 19:40:54 -06002275 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002276 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002277 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002278 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002279
Pekka Paalanenb5026542014-11-12 15:09:24 +02002280 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2281
David Herrmann1edf44c2013-10-22 17:11:26 +02002282 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002283}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002284
Pekka Paalanen82919792014-05-21 13:51:49 +03002285static void
2286weston_output_schedule_repaint_reset(struct weston_output *output)
2287{
Pekka Paalanen82919792014-05-21 13:51:49 +03002288 output->repaint_scheduled = 0;
2289 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002290}
2291
Pekka Paalanen0513a952014-05-21 16:17:27 +03002292static int
2293output_repaint_timer_handler(void *data)
2294{
2295 struct weston_output *output = data;
2296 struct weston_compositor *compositor = output->compositor;
2297
2298 if (output->repaint_needed &&
2299 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2300 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2301 weston_output_repaint(output) == 0)
2302 return 0;
2303
2304 weston_output_schedule_repaint_reset(output);
2305
2306 return 0;
2307}
2308
Kristian Høgsbergef044142011-06-21 15:02:12 -04002309WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002310weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002311 const struct timespec *stamp,
2312 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002313{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002314 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002315 int32_t refresh_nsec;
2316 struct timespec now;
2317 struct timespec gone;
2318 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002319
Pekka Paalanenb5026542014-11-12 15:09:24 +02002320 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2321 TLP_VBLANK(stamp), TLP_END);
2322
Pekka Paalanend7894d02015-07-03 15:08:53 +03002323 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002324 weston_presentation_feedback_present_list(&output->feedback_list,
2325 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002326 output->msc,
2327 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002328
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002329 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002330
Pekka Paalanen0513a952014-05-21 16:17:27 +03002331 weston_compositor_read_presentation_clock(compositor, &now);
2332 timespec_sub(&gone, &now, stamp);
2333 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2334 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002335
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002336 if (msec < -1000 || msec > 1000) {
2337 static bool warned;
2338
2339 if (!warned)
2340 weston_log("Warning: computed repaint delay is "
2341 "insane: %d msec\n", msec);
2342 warned = true;
2343
2344 msec = 0;
2345 }
2346
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002347 /* Called from restart_repaint_loop and restart happens already after
2348 * the deadline given by repaint_msec? In that case we delay until
2349 * the deadline of the next frame, to give clients a more predictable
2350 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002351 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002352 msec += refresh_nsec / 1000000;
2353
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002354 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002355 output_repaint_timer_handler(output);
2356 else
2357 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002358}
2359
2360static void
2361idle_repaint(void *data)
2362{
2363 struct weston_output *output = data;
2364
Jonas Ådahle5a12252013-04-05 23:07:11 +02002365 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002366}
2367
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002368WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002369weston_layer_entry_insert(struct weston_layer_entry *list,
2370 struct weston_layer_entry *entry)
2371{
2372 wl_list_insert(&list->link, &entry->link);
2373 entry->layer = list->layer;
2374}
2375
2376WL_EXPORT void
2377weston_layer_entry_remove(struct weston_layer_entry *entry)
2378{
2379 wl_list_remove(&entry->link);
2380 wl_list_init(&entry->link);
2381 entry->layer = NULL;
2382}
2383
2384WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002385weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2386{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002387 wl_list_init(&layer->view_list.link);
2388 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002389 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002390 if (below != NULL)
2391 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002392}
2393
2394WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002395weston_layer_set_mask(struct weston_layer *layer,
2396 int x, int y, int width, int height)
2397{
2398 struct weston_view *view;
2399
2400 layer->mask.x1 = x;
2401 layer->mask.x2 = x + width;
2402 layer->mask.y1 = y;
2403 layer->mask.y2 = y + height;
2404
2405 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2406 weston_view_geometry_dirty(view);
2407 }
2408}
2409
2410WL_EXPORT void
2411weston_layer_set_mask_infinite(struct weston_layer *layer)
2412{
2413 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2414 UINT32_MAX, UINT32_MAX);
2415}
2416
2417WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002418weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002419{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002420 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002421 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002422
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002423 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2424 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002425 return;
2426
Pekka Paalanenb5026542014-11-12 15:09:24 +02002427 if (!output->repaint_needed)
2428 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2429
Kristian Høgsbergef044142011-06-21 15:02:12 -04002430 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002431 output->repaint_needed = 1;
2432 if (output->repaint_scheduled)
2433 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002434
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002435 wl_event_loop_add_idle(loop, idle_repaint, output);
2436 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002437 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002438}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002439
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002440WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002441weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2442{
2443 struct weston_output *output;
2444
2445 wl_list_for_each(output, &compositor->output_list, link)
2446 weston_output_schedule_repaint(output);
2447}
2448
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002449static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002450surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002451{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002452 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002453}
2454
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002455static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002456surface_attach(struct wl_client *client,
2457 struct wl_resource *resource,
2458 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2459{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002460 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002461 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002462
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002463 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002464 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002465 if (buffer == NULL) {
2466 wl_client_post_no_memory(client);
2467 return;
2468 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002469 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002470
Pekka Paalanende685b82012-12-04 15:58:12 +02002471 /* Attach, attach, without commit in between does not send
2472 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002473 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002474
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002475 surface->pending.sx = sx;
2476 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002477 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002478}
2479
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002480static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002481surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002482 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002483 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002484{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002485 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002486
Derek Foreman57e92ed2015-11-17 14:11:35 -06002487 if (width <= 0 || height <= 0)
2488 return;
2489
Derek Foreman152254b2015-11-26 14:17:48 -06002490 pixman_region32_union_rect(&surface->pending.damage_surface,
2491 &surface->pending.damage_surface,
2492 x, y, width, height);
2493}
2494
2495static void
2496surface_damage_buffer(struct wl_client *client,
2497 struct wl_resource *resource,
2498 int32_t x, int32_t y, int32_t width, int32_t height)
2499{
2500 struct weston_surface *surface = wl_resource_get_user_data(resource);
2501
2502 if (width <= 0 || height <= 0)
2503 return;
2504
2505 pixman_region32_union_rect(&surface->pending.damage_buffer,
2506 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002507 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002508}
2509
Kristian Høgsberg33418202011-08-16 23:01:28 -04002510static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002511destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002512{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002513 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002514
2515 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002516 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002517}
2518
2519static void
2520surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002521 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002522{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002523 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002524 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002525
2526 cb = malloc(sizeof *cb);
2527 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002528 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002529 return;
2530 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002531
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002532 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2533 callback);
2534 if (cb->resource == NULL) {
2535 free(cb);
2536 wl_resource_post_no_memory(resource);
2537 return;
2538 }
2539
Jason Ekstranda85118c2013-06-27 20:17:02 -05002540 wl_resource_set_implementation(cb->resource, NULL, cb,
2541 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002542
Pekka Paalanenbc106382012-10-10 12:49:31 +03002543 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002544}
2545
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002546static void
2547surface_set_opaque_region(struct wl_client *client,
2548 struct wl_resource *resource,
2549 struct wl_resource *region_resource)
2550{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002551 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002552 struct weston_region *region;
2553
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002554 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002555 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002556 pixman_region32_copy(&surface->pending.opaque,
2557 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002558 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002559 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002560 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002561}
2562
2563static void
2564surface_set_input_region(struct wl_client *client,
2565 struct wl_resource *resource,
2566 struct wl_resource *region_resource)
2567{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002568 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002569 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002570
2571 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002572 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002573 pixman_region32_copy(&surface->pending.input,
2574 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002575 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002576 pixman_region32_fini(&surface->pending.input);
2577 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002578 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002579}
2580
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002581static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002582weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002583{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002584 struct weston_subsurface *sub;
2585
2586 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2587 parent_link_pending) {
2588 wl_list_remove(&sub->parent_link);
2589 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2590 }
2591}
2592
2593static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002594weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002595 struct weston_matrix *matrix)
2596{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002597 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002598 double src_width, src_height, dest_width, dest_height;
2599
2600 weston_matrix_init(matrix);
2601
2602 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2603 src_width = surface->width_from_buffer;
2604 src_height = surface->height_from_buffer;
2605 } else {
2606 src_width = wl_fixed_to_double(vp->buffer.src_width);
2607 src_height = wl_fixed_to_double(vp->buffer.src_height);
2608 }
2609
2610 if (vp->surface.width == -1) {
2611 dest_width = src_width;
2612 dest_height = src_height;
2613 } else {
2614 dest_width = vp->surface.width;
2615 dest_height = vp->surface.height;
2616 }
2617
2618 if (src_width != dest_width || src_height != dest_height)
2619 weston_matrix_scale(matrix,
2620 src_width / dest_width,
2621 src_height / dest_height, 1);
2622
2623 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2624 weston_matrix_translate(matrix,
2625 wl_fixed_to_double(vp->buffer.src_x),
2626 wl_fixed_to_double(vp->buffer.src_y),
2627 0);
2628
2629 switch (vp->buffer.transform) {
2630 case WL_OUTPUT_TRANSFORM_FLIPPED:
2631 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2632 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2633 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2634 weston_matrix_scale(matrix, -1, 1, 1);
2635 weston_matrix_translate(matrix,
2636 surface->width_from_buffer, 0, 0);
2637 break;
2638 }
2639
2640 switch (vp->buffer.transform) {
2641 default:
2642 case WL_OUTPUT_TRANSFORM_NORMAL:
2643 case WL_OUTPUT_TRANSFORM_FLIPPED:
2644 break;
2645 case WL_OUTPUT_TRANSFORM_90:
2646 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2647 weston_matrix_rotate_xy(matrix, 0, 1);
2648 weston_matrix_translate(matrix,
2649 surface->height_from_buffer, 0, 0);
2650 break;
2651 case WL_OUTPUT_TRANSFORM_180:
2652 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2653 weston_matrix_rotate_xy(matrix, -1, 0);
2654 weston_matrix_translate(matrix,
2655 surface->width_from_buffer,
2656 surface->height_from_buffer, 0);
2657 break;
2658 case WL_OUTPUT_TRANSFORM_270:
2659 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2660 weston_matrix_rotate_xy(matrix, 0, -1);
2661 weston_matrix_translate(matrix,
2662 0, surface->width_from_buffer, 0);
2663 break;
2664 }
2665
2666 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2667}
2668
Derek Foreman152254b2015-11-26 14:17:48 -06002669/* Translate pending damage in buffer co-ordinates to surface
2670 * co-ordinates and union it with a pixman_region32_t.
2671 * This should only be called after the buffer is attached.
2672 */
2673static void
2674apply_damage_buffer(pixman_region32_t *dest,
2675 struct weston_surface *surface,
2676 struct weston_surface_state *state)
2677{
2678 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2679
2680 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2681 * translated into surface co-ordinates and unioned with
2682 * any other surface damage.
2683 * None of this makes sense if there is no buffer though.
2684 */
2685 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2686 pixman_region32_t buffer_damage;
2687
2688 pixman_region32_intersect_rect(&state->damage_buffer,
2689 &state->damage_buffer,
2690 0, 0, buffer->width,
2691 buffer->height);
2692 pixman_region32_init(&buffer_damage);
2693 weston_matrix_transform_region(&buffer_damage,
2694 &surface->buffer_to_surface_matrix,
2695 &state->damage_buffer);
2696 pixman_region32_union(dest, dest, &buffer_damage);
2697 pixman_region32_fini(&buffer_damage);
2698 }
2699 /* We should clear this on commit even if there was no buffer */
2700 pixman_region32_clear(&state->damage_buffer);
2701}
2702
Jason Ekstrand1e059042014-10-16 10:55:19 -05002703static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002704weston_surface_commit_state(struct weston_surface *surface,
2705 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002706{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002707 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002708 pixman_region32_t opaque;
2709
Alexander Larsson4ea95522013-05-22 14:41:37 +02002710 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002711 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002712 /* wp_viewport.set_source */
2713 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002714 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002715
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002716 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002717 if (state->newly_attached)
2718 weston_surface_attach(surface, state->buffer);
2719 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002720
Jason Ekstrand1e059042014-10-16 10:55:19 -05002721 weston_surface_build_buffer_matrix(surface,
2722 &surface->surface_to_buffer_matrix);
2723 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2724 &surface->surface_to_buffer_matrix);
2725
Jason Ekstrand7b982072014-05-20 14:33:03 -05002726 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002727 weston_surface_update_size(surface);
2728 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002729 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002730 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002731
Jason Ekstrand7b982072014-05-20 14:33:03 -05002732 state->sx = 0;
2733 state->sy = 0;
2734 state->newly_attached = 0;
2735 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002736
Derek Foreman152254b2015-11-26 14:17:48 -06002737 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002738 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002739 (pixman_region32_not_empty(&state->damage_surface) ||
2740 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002741 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002742
Pekka Paalanen8e159182012-10-10 12:49:25 +03002743 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002744 &state->damage_surface);
2745
2746 apply_damage_buffer(&surface->damage, surface, state);
2747
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002748 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002749 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002750 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002751
2752 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002753 pixman_region32_init(&opaque);
2754 pixman_region32_intersect_rect(&opaque, &state->opaque,
2755 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002756
2757 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2758 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002759 wl_list_for_each(view, &surface->views, surface_link)
2760 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002761 }
2762
2763 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002764
2765 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002766 pixman_region32_intersect_rect(&surface->input, &state->input,
2767 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002768
Pekka Paalanenbc106382012-10-10 12:49:31 +03002769 /* wl_surface.frame */
2770 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002771 &state->frame_callback_list);
2772 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002773
2774 /* XXX:
2775 * What should happen with a feedback request, if there
2776 * is no wl_buffer attached for this commit?
2777 */
2778
2779 /* presentation.feedback */
2780 wl_list_insert_list(&surface->feedback_list,
2781 &state->feedback_list);
2782 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002783}
2784
2785static void
2786weston_surface_commit(struct weston_surface *surface)
2787{
2788 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002789
Pekka Paalanene67858b2013-04-25 13:57:42 +03002790 weston_surface_commit_subsurface_order(surface);
2791
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002792 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002793}
2794
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002795static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002796weston_subsurface_commit(struct weston_subsurface *sub);
2797
2798static void
2799weston_subsurface_parent_commit(struct weston_subsurface *sub,
2800 int parent_is_synchronized);
2801
2802static void
2803surface_commit(struct wl_client *client, struct wl_resource *resource)
2804{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002805 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002806 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2807
2808 if (sub) {
2809 weston_subsurface_commit(sub);
2810 return;
2811 }
2812
2813 weston_surface_commit(surface);
2814
2815 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2816 if (sub->surface != surface)
2817 weston_subsurface_parent_commit(sub, 0);
2818 }
2819}
2820
2821static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002822surface_set_buffer_transform(struct wl_client *client,
2823 struct wl_resource *resource, int transform)
2824{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002825 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002826
Jonny Lamba55f1392014-05-30 12:07:15 +02002827 /* if wl_output.transform grows more members this will need to be updated. */
2828 if (transform < 0 ||
2829 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2830 wl_resource_post_error(resource,
2831 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2832 "buffer transform must be a valid transform "
2833 "('%d' specified)", transform);
2834 return;
2835 }
2836
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002837 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002838 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002839}
2840
Alexander Larsson4ea95522013-05-22 14:41:37 +02002841static void
2842surface_set_buffer_scale(struct wl_client *client,
2843 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002844 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002845{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002846 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002847
Jonny Lamba55f1392014-05-30 12:07:15 +02002848 if (scale < 1) {
2849 wl_resource_post_error(resource,
2850 WL_SURFACE_ERROR_INVALID_SCALE,
2851 "buffer scale must be at least one "
2852 "('%d' specified)", scale);
2853 return;
2854 }
2855
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002856 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002857 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002858}
2859
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002860static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002861 surface_destroy,
2862 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002863 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002864 surface_frame,
2865 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002866 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002867 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002868 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06002869 surface_set_buffer_scale,
2870 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002871};
2872
2873static void
2874compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002875 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002876{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002877 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002878 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002879
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002880 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002881 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002882 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002883 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002884 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002885
Jason Ekstranda85118c2013-06-27 20:17:02 -05002886 surface->resource =
2887 wl_resource_create(client, &wl_surface_interface,
2888 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002889 if (surface->resource == NULL) {
2890 weston_surface_destroy(surface);
2891 wl_resource_post_no_memory(resource);
2892 return;
2893 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002894 wl_resource_set_implementation(surface->resource, &surface_interface,
2895 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002896
2897 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002898}
2899
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002900static void
2901destroy_region(struct wl_resource *resource)
2902{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002903 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002904
2905 pixman_region32_fini(&region->region);
2906 free(region);
2907}
2908
2909static void
2910region_destroy(struct wl_client *client, struct wl_resource *resource)
2911{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002912 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002913}
2914
2915static void
2916region_add(struct wl_client *client, struct wl_resource *resource,
2917 int32_t x, int32_t y, int32_t width, int32_t height)
2918{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002919 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002920
2921 pixman_region32_union_rect(&region->region, &region->region,
2922 x, y, width, height);
2923}
2924
2925static void
2926region_subtract(struct wl_client *client, struct wl_resource *resource,
2927 int32_t x, int32_t y, int32_t width, int32_t height)
2928{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002929 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002930 pixman_region32_t rect;
2931
2932 pixman_region32_init_rect(&rect, x, y, width, height);
2933 pixman_region32_subtract(&region->region, &region->region, &rect);
2934 pixman_region32_fini(&rect);
2935}
2936
2937static const struct wl_region_interface region_interface = {
2938 region_destroy,
2939 region_add,
2940 region_subtract
2941};
2942
2943static void
2944compositor_create_region(struct wl_client *client,
2945 struct wl_resource *resource, uint32_t id)
2946{
2947 struct weston_region *region;
2948
2949 region = malloc(sizeof *region);
2950 if (region == NULL) {
2951 wl_resource_post_no_memory(resource);
2952 return;
2953 }
2954
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002955 pixman_region32_init(&region->region);
2956
Jason Ekstranda85118c2013-06-27 20:17:02 -05002957 region->resource =
2958 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002959 if (region->resource == NULL) {
2960 free(region);
2961 wl_resource_post_no_memory(resource);
2962 return;
2963 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002964 wl_resource_set_implementation(region->resource, &region_interface,
2965 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002966}
2967
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002968static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002969 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002970 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002971};
2972
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002973static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002974weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2975{
2976 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002977
Jason Ekstrand7b982072014-05-20 14:33:03 -05002978 weston_surface_commit_state(surface, &sub->cached);
2979 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002980
2981 weston_surface_commit_subsurface_order(surface);
2982
2983 weston_surface_schedule_repaint(surface);
2984
Jason Ekstrand7b982072014-05-20 14:33:03 -05002985 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002986}
2987
2988static void
2989weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2990{
2991 struct weston_surface *surface = sub->surface;
2992
2993 /*
2994 * If this commit would cause the surface to move by the
2995 * attach(dx, dy) parameters, the old damage region must be
2996 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05002997 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002998 */
Derek Foreman152254b2015-11-26 14:17:48 -06002999 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003000 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003001 pixman_region32_union(&sub->cached.damage_surface,
3002 &sub->cached.damage_surface,
3003 &surface->pending.damage_surface);
3004 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003005
3006 if (surface->pending.newly_attached) {
3007 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003008 weston_surface_state_set_buffer(&sub->cached,
3009 surface->pending.buffer);
3010 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003011 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003012 weston_presentation_feedback_discard_list(
3013 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003014 }
3015 sub->cached.sx += surface->pending.sx;
3016 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003017
Derek Foreman152254b2015-11-26 14:17:48 -06003018 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3019
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003020 sub->cached.buffer_viewport.changed |=
3021 surface->pending.buffer_viewport.changed;
3022 sub->cached.buffer_viewport.buffer =
3023 surface->pending.buffer_viewport.buffer;
3024 sub->cached.buffer_viewport.surface =
3025 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003026
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003027 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003028
3029 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3030
3031 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3032
3033 wl_list_insert_list(&sub->cached.frame_callback_list,
3034 &surface->pending.frame_callback_list);
3035 wl_list_init(&surface->pending.frame_callback_list);
3036
Pekka Paalanen133e4392014-09-23 22:08:46 -04003037 wl_list_insert_list(&sub->cached.feedback_list,
3038 &surface->pending.feedback_list);
3039 wl_list_init(&surface->pending.feedback_list);
3040
Jason Ekstrand7b982072014-05-20 14:33:03 -05003041 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003042}
3043
Derek Foreman280e7dd2014-10-03 13:13:42 -05003044static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003045weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3046{
3047 while (sub) {
3048 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003049 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003050
3051 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003052 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003053
3054 sub = weston_surface_to_subsurface(sub->parent);
3055 }
3056
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003057 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003058}
3059
3060static void
3061weston_subsurface_commit(struct weston_subsurface *sub)
3062{
3063 struct weston_surface *surface = sub->surface;
3064 struct weston_subsurface *tmp;
3065
3066 /* Recursive check for effectively synchronized. */
3067 if (weston_subsurface_is_synchronized(sub)) {
3068 weston_subsurface_commit_to_cache(sub);
3069 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003070 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003071 /* flush accumulated state from cache */
3072 weston_subsurface_commit_to_cache(sub);
3073 weston_subsurface_commit_from_cache(sub);
3074 } else {
3075 weston_surface_commit(surface);
3076 }
3077
3078 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3079 if (tmp->surface != surface)
3080 weston_subsurface_parent_commit(tmp, 0);
3081 }
3082 }
3083}
3084
3085static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003086weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003087{
3088 struct weston_surface *surface = sub->surface;
3089 struct weston_subsurface *tmp;
3090
Pekka Paalanene67858b2013-04-25 13:57:42 +03003091 /* From now on, commit_from_cache the whole sub-tree, regardless of
3092 * the synchronized mode of each child. This sub-surface or some
3093 * of its ancestors were synchronized, so we are synchronized
3094 * all the way down.
3095 */
3096
Jason Ekstrand7b982072014-05-20 14:33:03 -05003097 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003098 weston_subsurface_commit_from_cache(sub);
3099
3100 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3101 if (tmp->surface != surface)
3102 weston_subsurface_parent_commit(tmp, 1);
3103 }
3104}
3105
3106static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003107weston_subsurface_parent_commit(struct weston_subsurface *sub,
3108 int parent_is_synchronized)
3109{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003110 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003111 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003112 wl_list_for_each(view, &sub->surface->views, surface_link)
3113 weston_view_set_position(view,
3114 sub->position.x,
3115 sub->position.y);
3116
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003117 sub->position.set = 0;
3118 }
3119
3120 if (parent_is_synchronized || sub->synchronized)
3121 weston_subsurface_synchronized_commit(sub);
3122}
3123
Pekka Paalanen8274d902014-08-06 19:36:51 +03003124static int
3125subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3126{
3127 return snprintf(buf, len, "sub-surface");
3128}
3129
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003130static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003131subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003132{
3133 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003134 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003135
Jason Ekstranda7af7042013-10-12 22:38:11 -05003136 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003137 weston_view_set_position(view,
3138 view->geometry.x + dx,
3139 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003140
3141 /* No need to check parent mappedness, because if parent is not
3142 * mapped, parent is not in a visible layer, so this sub-surface
3143 * will not be drawn either.
3144 */
3145 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003146 struct weston_output *output;
3147
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003148 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003149 * because that would call it also for the parent surface,
3150 * which might not be mapped yet. That would lead to
3151 * inconsistent state, where the window could never be
3152 * mapped.
3153 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003154 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003155 * weston_surface_is_mapped() return true, so that when the
3156 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003157 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003158 */
3159 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003160 output = container_of(compositor->output_list.next,
3161 struct weston_output, link);
3162
3163 surface->output = output;
Bryce Harrington89324ce2015-12-23 18:38:07 -08003164 weston_surface_update_output_mask(surface, 1u << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003165 }
3166}
3167
3168static struct weston_subsurface *
3169weston_surface_to_subsurface(struct weston_surface *surface)
3170{
3171 if (surface->configure == subsurface_configure)
3172 return surface->configure_private;
3173
3174 return NULL;
3175}
3176
Pekka Paalanen01388e22013-04-25 13:57:44 +03003177WL_EXPORT struct weston_surface *
3178weston_surface_get_main_surface(struct weston_surface *surface)
3179{
3180 struct weston_subsurface *sub;
3181
3182 while (surface && (sub = weston_surface_to_subsurface(surface)))
3183 surface = sub->parent;
3184
3185 return surface;
3186}
3187
Pekka Paalanen50b67472014-10-01 15:02:41 +03003188WL_EXPORT int
3189weston_surface_set_role(struct weston_surface *surface,
3190 const char *role_name,
3191 struct wl_resource *error_resource,
3192 uint32_t error_code)
3193{
3194 assert(role_name);
3195
3196 if (surface->role_name == NULL ||
3197 surface->role_name == role_name ||
3198 strcmp(surface->role_name, role_name) == 0) {
3199 surface->role_name = role_name;
3200
3201 return 0;
3202 }
3203
3204 wl_resource_post_error(error_resource, error_code,
3205 "Cannot assign role %s to wl_surface@%d,"
3206 " already has role %s\n",
3207 role_name,
3208 wl_resource_get_id(surface->resource),
3209 surface->role_name);
3210 return -1;
3211}
3212
Pekka Paalanen8274d902014-08-06 19:36:51 +03003213WL_EXPORT void
3214weston_surface_set_label_func(struct weston_surface *surface,
3215 int (*desc)(struct weston_surface *,
3216 char *, size_t))
3217{
3218 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003219 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003220}
3221
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003222/** Get the size of surface contents
3223 *
3224 * \param surface The surface to query.
3225 * \param width Returns the width of raw contents.
3226 * \param height Returns the height of raw contents.
3227 *
3228 * Retrieves the raw surface content size in pixels for the given surface.
3229 * This is the whole content size in buffer pixels. If the surface
3230 * has no content or the renderer does not implement this feature,
3231 * zeroes are returned.
3232 *
3233 * This function is used to determine the buffer size needed for
3234 * a weston_surface_copy_content() call.
3235 */
3236WL_EXPORT void
3237weston_surface_get_content_size(struct weston_surface *surface,
3238 int *width, int *height)
3239{
3240 struct weston_renderer *rer = surface->compositor->renderer;
3241
3242 if (!rer->surface_get_content_size) {
3243 *width = 0;
3244 *height = 0;
3245 return;
3246 }
3247
3248 rer->surface_get_content_size(surface, width, height);
3249}
3250
3251/** Copy surface contents to system memory.
3252 *
3253 * \param surface The surface to copy from.
3254 * \param target Pointer to the target memory buffer.
3255 * \param size Size of the target buffer in bytes.
3256 * \param src_x X location on contents to copy from.
3257 * \param src_y Y location on contents to copy from.
3258 * \param width Width in pixels of the area to copy.
3259 * \param height Height in pixels of the area to copy.
3260 * \return 0 for success, -1 for failure.
3261 *
3262 * Surface contents are maintained by the renderer. They can be in a
3263 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3264 * else.
3265 *
3266 * Surface contents are copied into memory pointed to by target,
3267 * which has size bytes of space available. The target memory
3268 * may be larger than needed, but being smaller returns an error.
3269 * The extra bytes in target may or may not be written; their content is
3270 * unspecified. Size must be large enough to hold the image.
3271 *
3272 * The image in the target memory will be arranged in rows from
3273 * top to bottom, and pixels on a row from left to right. The pixel
3274 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3275 * width * 4.
3276 *
3277 * Parameters src_x and src_y define the upper-left corner in buffer
3278 * coordinates (pixels) to copy from. Parameters width and height
3279 * define the size of the area to copy in pixels.
3280 *
3281 * The rectangle defined by src_x, src_y, width, height must fit in
3282 * the surface contents. Otherwise an error is returned.
3283 *
3284 * Use surface_get_data_size to determine the content size; the
3285 * needed target buffer size and rectangle limits.
3286 *
3287 * CURRENT IMPLEMENTATION RESTRICTIONS:
3288 * - the machine must be little-endian due to Pixman formats.
3289 *
3290 * NOTE: Pixman formats are premultiplied.
3291 */
3292WL_EXPORT int
3293weston_surface_copy_content(struct weston_surface *surface,
3294 void *target, size_t size,
3295 int src_x, int src_y,
3296 int width, int height)
3297{
3298 struct weston_renderer *rer = surface->compositor->renderer;
3299 int cw, ch;
3300 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3301
3302 if (!rer->surface_copy_content)
3303 return -1;
3304
3305 weston_surface_get_content_size(surface, &cw, &ch);
3306
3307 if (src_x < 0 || src_y < 0)
3308 return -1;
3309
3310 if (width <= 0 || height <= 0)
3311 return -1;
3312
3313 if (src_x + width > cw || src_y + height > ch)
3314 return -1;
3315
3316 if (width * bytespp * height > size)
3317 return -1;
3318
3319 return rer->surface_copy_content(surface, target, size,
3320 src_x, src_y, width, height);
3321}
3322
Pekka Paalanene67858b2013-04-25 13:57:42 +03003323static void
3324subsurface_set_position(struct wl_client *client,
3325 struct wl_resource *resource, int32_t x, int32_t y)
3326{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003327 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003328
3329 if (!sub)
3330 return;
3331
3332 sub->position.x = x;
3333 sub->position.y = y;
3334 sub->position.set = 1;
3335}
3336
3337static struct weston_subsurface *
3338subsurface_from_surface(struct weston_surface *surface)
3339{
3340 struct weston_subsurface *sub;
3341
3342 sub = weston_surface_to_subsurface(surface);
3343 if (sub)
3344 return sub;
3345
3346 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3347 if (sub->surface == surface)
3348 return sub;
3349
3350 return NULL;
3351}
3352
3353static struct weston_subsurface *
3354subsurface_sibling_check(struct weston_subsurface *sub,
3355 struct weston_surface *surface,
3356 const char *request)
3357{
3358 struct weston_subsurface *sibling;
3359
3360 sibling = subsurface_from_surface(surface);
3361
3362 if (!sibling) {
3363 wl_resource_post_error(sub->resource,
3364 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3365 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003366 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003367 return NULL;
3368 }
3369
3370 if (sibling->parent != sub->parent) {
3371 wl_resource_post_error(sub->resource,
3372 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3373 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003374 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003375 return NULL;
3376 }
3377
3378 return sibling;
3379}
3380
3381static void
3382subsurface_place_above(struct wl_client *client,
3383 struct wl_resource *resource,
3384 struct wl_resource *sibling_resource)
3385{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003386 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003387 struct weston_surface *surface =
3388 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003389 struct weston_subsurface *sibling;
3390
3391 if (!sub)
3392 return;
3393
3394 sibling = subsurface_sibling_check(sub, surface, "place_above");
3395 if (!sibling)
3396 return;
3397
3398 wl_list_remove(&sub->parent_link_pending);
3399 wl_list_insert(sibling->parent_link_pending.prev,
3400 &sub->parent_link_pending);
3401}
3402
3403static void
3404subsurface_place_below(struct wl_client *client,
3405 struct wl_resource *resource,
3406 struct wl_resource *sibling_resource)
3407{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003408 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003409 struct weston_surface *surface =
3410 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003411 struct weston_subsurface *sibling;
3412
3413 if (!sub)
3414 return;
3415
3416 sibling = subsurface_sibling_check(sub, surface, "place_below");
3417 if (!sibling)
3418 return;
3419
3420 wl_list_remove(&sub->parent_link_pending);
3421 wl_list_insert(&sibling->parent_link_pending,
3422 &sub->parent_link_pending);
3423}
3424
3425static void
3426subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3427{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003428 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003429
3430 if (sub)
3431 sub->synchronized = 1;
3432}
3433
3434static void
3435subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3436{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003437 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003438
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003439 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003440 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003441
3442 /* If sub became effectively desynchronized, flush. */
3443 if (!weston_subsurface_is_synchronized(sub))
3444 weston_subsurface_synchronized_commit(sub);
3445 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003446}
3447
3448static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003449weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3450{
3451 wl_list_remove(&sub->parent_link);
3452 wl_list_remove(&sub->parent_link_pending);
3453 wl_list_remove(&sub->parent_destroy_listener.link);
3454 sub->parent = NULL;
3455}
3456
3457static void
3458weston_subsurface_destroy(struct weston_subsurface *sub);
3459
3460static void
3461subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3462{
3463 struct weston_subsurface *sub =
3464 container_of(listener, struct weston_subsurface,
3465 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003466 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003467
3468 /* The protocol object (wl_resource) is left inert. */
3469 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003470 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003471
3472 weston_subsurface_destroy(sub);
3473}
3474
3475static void
3476subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3477{
3478 struct weston_subsurface *sub =
3479 container_of(listener, struct weston_subsurface,
3480 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003481 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003482 assert(sub->surface != sub->parent);
3483
3484 if (weston_surface_is_mapped(sub->surface))
3485 weston_surface_unmap(sub->surface);
3486
3487 weston_subsurface_unlink_parent(sub);
3488}
3489
3490static void
3491subsurface_resource_destroy(struct wl_resource *resource)
3492{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003493 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003494
3495 if (sub)
3496 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003497}
3498
3499static void
3500subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3501{
3502 wl_resource_destroy(resource);
3503}
3504
3505static void
3506weston_subsurface_link_parent(struct weston_subsurface *sub,
3507 struct weston_surface *parent)
3508{
3509 sub->parent = parent;
3510 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003511 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003512 &sub->parent_destroy_listener);
3513
3514 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3515 wl_list_insert(&parent->subsurface_list_pending,
3516 &sub->parent_link_pending);
3517}
3518
3519static void
3520weston_subsurface_link_surface(struct weston_subsurface *sub,
3521 struct weston_surface *surface)
3522{
3523 sub->surface = surface;
3524 sub->surface_destroy_listener.notify =
3525 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003526 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003527 &sub->surface_destroy_listener);
3528}
3529
3530static void
3531weston_subsurface_destroy(struct weston_subsurface *sub)
3532{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003533 struct weston_view *view, *next;
3534
Pekka Paalanene67858b2013-04-25 13:57:42 +03003535 assert(sub->surface);
3536
3537 if (sub->resource) {
3538 assert(weston_surface_to_subsurface(sub->surface) == sub);
3539 assert(sub->parent_destroy_listener.notify ==
3540 subsurface_handle_parent_destroy);
3541
George Kiagiadakised04d382014-06-13 18:10:26 +02003542 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3543 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003544 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003545 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003546
Pekka Paalanene67858b2013-04-25 13:57:42 +03003547 if (sub->parent)
3548 weston_subsurface_unlink_parent(sub);
3549
Jason Ekstrand7b982072014-05-20 14:33:03 -05003550 weston_surface_state_fini(&sub->cached);
3551 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003552
3553 sub->surface->configure = NULL;
3554 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003555 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003556 } else {
3557 /* the dummy weston_subsurface for the parent itself */
3558 assert(sub->parent_destroy_listener.notify == NULL);
3559 wl_list_remove(&sub->parent_link);
3560 wl_list_remove(&sub->parent_link_pending);
3561 }
3562
3563 wl_list_remove(&sub->surface_destroy_listener.link);
3564 free(sub);
3565}
3566
3567static const struct wl_subsurface_interface subsurface_implementation = {
3568 subsurface_destroy,
3569 subsurface_set_position,
3570 subsurface_place_above,
3571 subsurface_place_below,
3572 subsurface_set_sync,
3573 subsurface_set_desync
3574};
3575
3576static struct weston_subsurface *
3577weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3578 struct weston_surface *parent)
3579{
3580 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003581 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003582
Bryce Harringtonde16d892014-11-20 22:21:57 -08003583 sub = zalloc(sizeof *sub);
3584 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003585 return NULL;
3586
Jason Ekstranda7af7042013-10-12 22:38:11 -05003587 wl_list_init(&sub->unused_views);
3588
Jason Ekstranda85118c2013-06-27 20:17:02 -05003589 sub->resource =
3590 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003591 if (!sub->resource) {
3592 free(sub);
3593 return NULL;
3594 }
3595
Jason Ekstranda85118c2013-06-27 20:17:02 -05003596 wl_resource_set_implementation(sub->resource,
3597 &subsurface_implementation,
3598 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003599 weston_subsurface_link_surface(sub, surface);
3600 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003601 weston_surface_state_init(&sub->cached);
3602 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003603 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003604
3605 return sub;
3606}
3607
3608/* Create a dummy subsurface for having the parent itself in its
3609 * sub-surface lists. Makes stacking order manipulation easy.
3610 */
3611static struct weston_subsurface *
3612weston_subsurface_create_for_parent(struct weston_surface *parent)
3613{
3614 struct weston_subsurface *sub;
3615
Bryce Harringtonde16d892014-11-20 22:21:57 -08003616 sub = zalloc(sizeof *sub);
3617 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003618 return NULL;
3619
3620 weston_subsurface_link_surface(sub, parent);
3621 sub->parent = parent;
3622 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3623 wl_list_insert(&parent->subsurface_list_pending,
3624 &sub->parent_link_pending);
3625
3626 return sub;
3627}
3628
3629static void
3630subcompositor_get_subsurface(struct wl_client *client,
3631 struct wl_resource *resource,
3632 uint32_t id,
3633 struct wl_resource *surface_resource,
3634 struct wl_resource *parent_resource)
3635{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003636 struct weston_surface *surface =
3637 wl_resource_get_user_data(surface_resource);
3638 struct weston_surface *parent =
3639 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003640 struct weston_subsurface *sub;
3641 static const char where[] = "get_subsurface: wl_subsurface@";
3642
3643 if (surface == parent) {
3644 wl_resource_post_error(resource,
3645 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3646 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003647 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003648 return;
3649 }
3650
3651 if (weston_surface_to_subsurface(surface)) {
3652 wl_resource_post_error(resource,
3653 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3654 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003655 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003656 return;
3657 }
3658
Pekka Paalanen50b67472014-10-01 15:02:41 +03003659 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3660 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003661 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003662
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003663 if (weston_surface_get_main_surface(parent) == surface) {
3664 wl_resource_post_error(resource,
3665 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3666 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003667 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003668 return;
3669 }
3670
Pekka Paalanene67858b2013-04-25 13:57:42 +03003671 /* make sure the parent is in its own list */
3672 if (wl_list_empty(&parent->subsurface_list)) {
3673 if (!weston_subsurface_create_for_parent(parent)) {
3674 wl_resource_post_no_memory(resource);
3675 return;
3676 }
3677 }
3678
3679 sub = weston_subsurface_create(id, surface, parent);
3680 if (!sub) {
3681 wl_resource_post_no_memory(resource);
3682 return;
3683 }
3684
3685 surface->configure = subsurface_configure;
3686 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003687 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003688}
3689
3690static void
3691subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3692{
3693 wl_resource_destroy(resource);
3694}
3695
3696static const struct wl_subcompositor_interface subcompositor_interface = {
3697 subcompositor_destroy,
3698 subcompositor_get_subsurface
3699};
3700
3701static void
3702bind_subcompositor(struct wl_client *client,
3703 void *data, uint32_t version, uint32_t id)
3704{
3705 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003706 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003707
Jason Ekstranda85118c2013-06-27 20:17:02 -05003708 resource =
3709 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003710 if (resource == NULL) {
3711 wl_client_post_no_memory(client);
3712 return;
3713 }
3714 wl_resource_set_implementation(resource, &subcompositor_interface,
3715 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003716}
3717
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003718/** Set a DPMS mode on all of the compositor's outputs
3719 *
3720 * \param compositor The compositor instance
3721 * \param state The DPMS state the outputs will be set to
3722 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003723static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003724weston_compositor_dpms(struct weston_compositor *compositor,
3725 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003726{
3727 struct weston_output *output;
3728
3729 wl_list_for_each(output, &compositor->output_list, link)
3730 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003731 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003732}
3733
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003734/** Restores the compositor to active status
3735 *
3736 * \param compositor The compositor instance
3737 *
3738 * If the compositor was in a sleeping mode, all outputs are powered
3739 * back on via DPMS. Otherwise if the compositor was inactive
3740 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3741 * signal will fire.
3742 *
3743 * Restarts the idle timer.
3744 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003745WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003746weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003747{
Neil Roberts8b62e202013-09-30 13:14:47 +01003748 uint32_t old_state = compositor->state;
3749
3750 /* The state needs to be changed before emitting the wake
3751 * signal because that may try to schedule a repaint which
3752 * will not work if the compositor is still sleeping */
3753 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3754
3755 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003756 case WESTON_COMPOSITOR_SLEEPING:
3757 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3758 /* fall through */
3759 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003760 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003761 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003762 /* fall through */
3763 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003764 wl_event_source_timer_update(compositor->idle_source,
3765 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003766 }
3767}
3768
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003769/** Turns off rendering and frame events for the compositor.
3770 *
3771 * \param compositor The compositor instance
3772 *
3773 * This is used for example to prevent further rendering while the
3774 * compositor is shutting down.
3775 *
3776 * \note When offscreen state is entered, outputs will be powered
3777 * back on if they were sleeping (in DPMS off mode), even though
3778 * no rendering will be performed.
3779 *
3780 * Stops the idle timer.
3781 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003782WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003783weston_compositor_offscreen(struct weston_compositor *compositor)
3784{
3785 switch (compositor->state) {
3786 case WESTON_COMPOSITOR_OFFSCREEN:
3787 return;
3788 case WESTON_COMPOSITOR_SLEEPING:
3789 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3790 /* fall through */
3791 default:
3792 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3793 wl_event_source_timer_update(compositor->idle_source, 0);
3794 }
3795}
3796
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003797/** Powers down all attached output devices
3798 *
3799 * \param compositor The compositor instance
3800 *
3801 * Causes rendering to the outputs to cease, and no frame events to be
3802 * sent. Only powers down the outputs if the compositor is not already
3803 * in sleep mode.
3804 *
3805 * Stops the idle timer.
3806 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003807WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003808weston_compositor_sleep(struct weston_compositor *compositor)
3809{
3810 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3811 return;
3812
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003813 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003814 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3815 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3816}
3817
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003818/** Sets compositor to idle mode
3819 *
3820 * \param data The compositor instance
3821 *
3822 * This is called when the idle timer fires. Once the compositor is in
3823 * idle mode it requires a wake action (e.g. via
3824 * weston_compositor_wake()) to restore it. The compositor's
3825 * idle_signal will be triggered when the idle event occurs.
3826 *
3827 * Idleness can be inhibited by setting the compositor's idle_inhibit
3828 * property.
3829 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003830static int
3831idle_handler(void *data)
3832{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003833 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003834
3835 if (compositor->idle_inhibit)
3836 return 1;
3837
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003838 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003839 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003840
3841 return 1;
3842}
3843
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003844WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003845weston_plane_init(struct weston_plane *plane,
3846 struct weston_compositor *ec,
3847 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003848{
3849 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003850 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003851 plane->x = x;
3852 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003853 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003854
3855 /* Init the link so that the call to wl_list_remove() when releasing
3856 * the plane without ever stacking doesn't lead to a crash */
3857 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003858}
3859
3860WL_EXPORT void
3861weston_plane_release(struct weston_plane *plane)
3862{
Xiong Zhang97116532013-10-23 13:58:31 +08003863 struct weston_view *view;
3864
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003865 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003866 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003867
Xiong Zhang97116532013-10-23 13:58:31 +08003868 wl_list_for_each(view, &plane->compositor->view_list, link) {
3869 if (view->plane == plane)
3870 view->plane = NULL;
3871 }
3872
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003873 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003874}
3875
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003876WL_EXPORT void
3877weston_compositor_stack_plane(struct weston_compositor *ec,
3878 struct weston_plane *plane,
3879 struct weston_plane *above)
3880{
3881 if (above)
3882 wl_list_insert(above->link.prev, &plane->link);
3883 else
3884 wl_list_insert(&ec->plane_list, &plane->link);
3885}
3886
Casey Dahlin9074db52012-04-19 22:50:09 -04003887static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003888{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003889 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003890}
3891
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003892static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003893bind_output(struct wl_client *client,
3894 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003895{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003896 struct weston_output *output = data;
3897 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003898 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003899
Jason Ekstranda85118c2013-06-27 20:17:02 -05003900 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06003901 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003902 if (resource == NULL) {
3903 wl_client_post_no_memory(client);
3904 return;
3905 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003906
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003907 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003908 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003909
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003910 wl_output_send_geometry(resource,
3911 output->x,
3912 output->y,
3913 output->mm_width,
3914 output->mm_height,
3915 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003916 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003917 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003918 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003919 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003920 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003921
3922 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003923 wl_output_send_mode(resource,
3924 mode->flags,
3925 mode->width,
3926 mode->height,
3927 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003928 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003929
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003930 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003931 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003932}
3933
David Fort0de859e2016-05-27 23:22:57 +02003934/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003935static void
David Fort0de859e2016-05-27 23:22:57 +02003936weston_compositor_reflow_outputs(struct weston_compositor *compositor,
3937 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003938{
3939 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02003940 bool start_resizing = false;
3941
3942 if (!delta_width)
3943 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003944
3945 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02003946 if (output == resized_output) {
3947 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003948 continue;
3949 }
3950
David Fort0de859e2016-05-27 23:22:57 +02003951 if (start_resizing) {
3952 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003953 output->dirty = 1;
3954 }
3955 }
3956}
3957
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003958WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003959weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003960{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003961 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003962 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003963
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003964 output->destroying = 1;
3965
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003966 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08003967 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003968 weston_view_assign_output(view);
3969 }
3970
Pekka Paalanen0513a952014-05-21 16:17:27 +03003971 wl_event_source_remove(output->repaint_timer);
3972
Pekka Paalanen133e4392014-09-23 22:08:46 -04003973 weston_presentation_feedback_discard_list(&output->feedback_list);
3974
David Fort0de859e2016-05-27 23:22:57 +02003975 weston_compositor_reflow_outputs(output->compositor, output, output->width);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003976 wl_list_remove(&output->link);
3977
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003978 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003979 wl_signal_emit(&output->destroy_signal, output);
3980
Richard Hughesafe690c2013-05-02 10:10:04 +01003981 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003982 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003983 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08003984 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003985
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003986 wl_resource_for_each(resource, &output->resource_list) {
3987 wl_resource_set_destructor(resource, NULL);
3988 }
3989
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003990 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003991}
3992
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003993WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003994weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003995{
Scott Moreau850ca422012-05-21 15:21:25 -06003996 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003997
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003998 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003999 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004000
Scott Moreauccbf29d2012-02-22 14:21:41 -07004001 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004002 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004003 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004004 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004005 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004006 weston_matrix_scale(&output->matrix, magnification,
4007 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004008 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004009
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004010 switch (output->transform) {
4011 case WL_OUTPUT_TRANSFORM_FLIPPED:
4012 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4013 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4014 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4015 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4016 weston_matrix_scale(&output->matrix, -1, 1, 1);
4017 break;
4018 }
4019
4020 switch (output->transform) {
4021 default:
4022 case WL_OUTPUT_TRANSFORM_NORMAL:
4023 case WL_OUTPUT_TRANSFORM_FLIPPED:
4024 break;
4025 case WL_OUTPUT_TRANSFORM_90:
4026 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4027 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4028 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4029 break;
4030 case WL_OUTPUT_TRANSFORM_180:
4031 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4032 weston_matrix_translate(&output->matrix,
4033 -output->width, -output->height, 0);
4034 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4035 break;
4036 case WL_OUTPUT_TRANSFORM_270:
4037 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4038 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4039 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4040 break;
4041 }
4042
4043 if (output->current_scale != 1)
4044 weston_matrix_scale(&output->matrix,
4045 output->current_scale,
4046 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004047
Scott Moreauccbf29d2012-02-22 14:21:41 -07004048 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004049
4050 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004051}
4052
Scott Moreau1bad5db2012-08-18 01:04:05 -06004053static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004054weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004055{
4056 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004057 output->native_scale = scale;
4058 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004059
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004060 convert_size_by_transform_scale(&output->width, &output->height,
4061 output->current_mode->width,
4062 output->current_mode->height,
4063 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004064}
4065
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004066static void
4067weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004068{
4069 output->x = x;
4070 output->y = y;
4071
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004072 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004073 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004074 output->width,
4075 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004076}
4077
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004078WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004079weston_output_move(struct weston_output *output, int x, int y)
4080{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004081 struct wl_resource *resource;
4082
4083 output->move_x = x - output->x;
4084 output->move_y = y - output->y;
4085
4086 if (output->move_x == 0 && output->move_y == 0)
4087 return;
4088
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004089 weston_output_init_geometry(output, x, y);
4090
4091 output->dirty = 1;
4092
4093 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004094 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004095
4096 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004097 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004098 wl_output_send_geometry(resource,
4099 output->x,
4100 output->y,
4101 output->mm_width,
4102 output->mm_height,
4103 output->subpixel,
4104 output->make,
4105 output->model,
4106 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004107
FORT David8a120692016-04-26 23:34:06 +02004108 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004109 wl_output_send_done(resource);
4110 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004111}
4112
Bryce Harrington3f650b82015-12-23 11:01:58 -08004113/** Initialize a weston_output object's parameters
4114 *
4115 * \param output The weston_output object to initialize
4116 * \param c The output's compositor
4117 * \param x x coordinate for the output in global coordinate space
4118 * \param y y coordinate for the output in global coordinate space
4119 * \param mm_width Physical width of the output as reported by the backend
4120 * \param mm_height Physical height of the output as reported by the backend
4121 * \param transform Rotation of the output
4122 * \param scale Native scaling factor for the output
4123 *
4124 * Sets up the transformation, zoom, and geometry of the output using
4125 * the input properties.
4126 *
4127 * Establishes a repaint timer for the output with the relevant display
4128 * object's event loop. See output_repaint_timer_handler().
4129 *
4130 * The output is assigned an ID. Weston can support up to 32 distinct
4131 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4132 * is referred to and used to find the first available ID number, and
4133 * then this ID is marked as used in output_id_pool.
4134 *
4135 * The output is also assigned a Wayland global with the wl_output
4136 * external interface.
4137 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004138WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004139weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004140 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004141 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004142{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004143 struct wl_event_loop *loop;
4144
Bryce Harrington18e45732015-12-23 20:53:53 -08004145 /* Verify we haven't reached the limit of 32 available output IDs */
4146 assert(ffs(~c->output_id_pool) > 0);
4147
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004148 output->compositor = c;
4149 output->x = x;
4150 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004151 output->mm_width = mm_width;
4152 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004153 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004154 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004155
Alexander Larsson0b135062013-05-28 16:23:36 +02004156 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004157 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004158
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004159 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004160 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004161
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004162 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004163 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004164 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004165 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004166 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004167 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004168
Pekka Paalanen0513a952014-05-21 16:17:27 +03004169 loop = wl_display_get_event_loop(c->wl_display);
4170 output->repaint_timer = wl_event_loop_add_timer(loop,
4171 output_repaint_timer_handler, output);
4172
Bryce Harrington3f650b82015-12-23 11:01:58 -08004173 /* Invert the output id pool and look for the lowest numbered
4174 * switch (the least significant bit). Take that bit's position
4175 * as our ID, and mark it used in the compositor's output_id_pool.
4176 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004177 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004178 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004179
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004180 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004181 wl_global_create(c->wl_display, &wl_output_interface, 2,
4182 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004183}
4184
4185/** Adds an output to the compositor's output list and
4186 * send the compositor's output_created signal.
4187 *
4188 * \param compositor The compositor instance.
4189 * \param output The output to be added.
4190 */
4191WL_EXPORT void
4192weston_compositor_add_output(struct weston_compositor *compositor,
4193 struct weston_output *output)
4194{
4195 wl_list_insert(compositor->output_list.prev, &output->link);
4196 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004197}
4198
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004199WL_EXPORT void
4200weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004201 double device_x, double device_y,
4202 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004203{
Derek Foreman0f679412014-10-02 13:41:17 -05004204 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004205 device_x,
4206 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004207 0.0,
4208 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004209
Derek Foreman67a18b92015-03-24 11:36:14 -05004210 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004211
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004212 *x = p.f[0] / p.f[3];
4213 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004214}
4215
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004216static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004217destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004218{
Jonny Lamb74130762013-11-26 18:19:46 +01004219 struct weston_surface *surface =
4220 wl_resource_get_user_data(resource);
4221
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004222 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004223 surface->pending.buffer_viewport.buffer.src_width =
4224 wl_fixed_from_int(-1);
4225 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004226 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004227}
4228
4229static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004230viewport_destroy(struct wl_client *client,
4231 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004232{
4233 wl_resource_destroy(resource);
4234}
4235
4236static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004237viewport_set_source(struct wl_client *client,
4238 struct wl_resource *resource,
4239 wl_fixed_t src_x,
4240 wl_fixed_t src_y,
4241 wl_fixed_t src_width,
4242 wl_fixed_t src_height)
4243{
4244 struct weston_surface *surface =
4245 wl_resource_get_user_data(resource);
4246
4247 assert(surface->viewport_resource != NULL);
4248
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004249 if (src_width == wl_fixed_from_int(-1) &&
4250 src_height == wl_fixed_from_int(-1)) {
4251 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004252 surface->pending.buffer_viewport.buffer.src_width =
4253 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004254 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004255 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004256 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004257
4258 if (src_width <= 0 || src_height <= 0) {
4259 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004260 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004261 "source size must be positive (%fx%f)",
4262 wl_fixed_to_double(src_width),
4263 wl_fixed_to_double(src_height));
4264 return;
4265 }
4266
4267 surface->pending.buffer_viewport.buffer.src_x = src_x;
4268 surface->pending.buffer_viewport.buffer.src_y = src_y;
4269 surface->pending.buffer_viewport.buffer.src_width = src_width;
4270 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004271 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004272}
4273
4274static void
4275viewport_set_destination(struct wl_client *client,
4276 struct wl_resource *resource,
4277 int32_t dst_width,
4278 int32_t dst_height)
4279{
4280 struct weston_surface *surface =
4281 wl_resource_get_user_data(resource);
4282
4283 assert(surface->viewport_resource != NULL);
4284
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004285 if (dst_width == -1 && dst_height == -1) {
4286 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004287 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004288 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004289 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004290 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004291
4292 if (dst_width <= 0 || dst_height <= 0) {
4293 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004294 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004295 "destination size must be positive (%dx%d)",
4296 dst_width, dst_height);
4297 return;
4298 }
4299
4300 surface->pending.buffer_viewport.surface.width = dst_width;
4301 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004302 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004303}
4304
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004305static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004306 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004307 viewport_set_source,
4308 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004309};
4310
4311static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004312viewporter_destroy(struct wl_client *client,
4313 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004314{
4315 wl_resource_destroy(resource);
4316}
4317
4318static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004319viewporter_get_viewport(struct wl_client *client,
4320 struct wl_resource *viewporter,
4321 uint32_t id,
4322 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004323{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004324 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004325 struct weston_surface *surface =
4326 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004327 struct wl_resource *resource;
4328
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004329 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004330 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004331 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004332 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004333 return;
4334 }
4335
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004336 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004337 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004338 if (resource == NULL) {
4339 wl_client_post_no_memory(client);
4340 return;
4341 }
4342
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004343 wl_resource_set_implementation(resource, &viewport_interface,
4344 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004345
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004346 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004347}
4348
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004349static const struct wp_viewporter_interface viewporter_interface = {
4350 viewporter_destroy,
4351 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004352};
4353
4354static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004355bind_viewporter(struct wl_client *client,
4356 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004357{
4358 struct wl_resource *resource;
4359
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004360 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004361 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004362 if (resource == NULL) {
4363 wl_client_post_no_memory(client);
4364 return;
4365 }
4366
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004367 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004368 NULL, NULL);
4369}
4370
4371static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004372destroy_presentation_feedback(struct wl_resource *feedback_resource)
4373{
4374 struct weston_presentation_feedback *feedback;
4375
4376 feedback = wl_resource_get_user_data(feedback_resource);
4377
4378 wl_list_remove(&feedback->link);
4379 free(feedback);
4380}
4381
4382static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004383presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4384{
4385 wl_resource_destroy(resource);
4386}
4387
4388static void
4389presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004390 struct wl_resource *presentation_resource,
4391 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004392 uint32_t callback)
4393{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004394 struct weston_surface *surface;
4395 struct weston_presentation_feedback *feedback;
4396
4397 surface = wl_resource_get_user_data(surface_resource);
4398
Bryce Harringtonde16d892014-11-20 22:21:57 -08004399 feedback = zalloc(sizeof *feedback);
4400 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004401 goto err_calloc;
4402
4403 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004404 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004405 1, callback);
4406 if (!feedback->resource)
4407 goto err_create;
4408
4409 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4410 destroy_presentation_feedback);
4411
4412 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4413
4414 return;
4415
4416err_create:
4417 free(feedback);
4418
4419err_calloc:
4420 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004421}
4422
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004423static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004424 presentation_destroy,
4425 presentation_feedback
4426};
4427
4428static void
4429bind_presentation(struct wl_client *client,
4430 void *data, uint32_t version, uint32_t id)
4431{
4432 struct weston_compositor *compositor = data;
4433 struct wl_resource *resource;
4434
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004435 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004436 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004437 if (resource == NULL) {
4438 wl_client_post_no_memory(client);
4439 return;
4440 }
4441
4442 wl_resource_set_implementation(resource, &presentation_implementation,
4443 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004444 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004445}
4446
4447static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004448compositor_bind(struct wl_client *client,
4449 void *data, uint32_t version, uint32_t id)
4450{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004451 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004452 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004453
Jason Ekstranda85118c2013-06-27 20:17:02 -05004454 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004455 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004456 if (resource == NULL) {
4457 wl_client_post_no_memory(client);
4458 return;
4459 }
4460
4461 wl_resource_set_implementation(resource, &compositor_interface,
4462 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004463}
4464
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004465WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004466weston_environment_get_fd(const char *env)
4467{
4468 char *e, *end;
4469 int fd, flags;
4470
4471 e = getenv(env);
4472 if (!e)
4473 return -1;
4474 fd = strtol(e, &end, 0);
4475 if (*end != '\0')
4476 return -1;
4477
4478 flags = fcntl(fd, F_GETFD);
4479 if (flags == -1)
4480 return -1;
4481
4482 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4483 unsetenv(env);
4484
4485 return fd;
4486}
4487
Pekka Paalanenb5026542014-11-12 15:09:24 +02004488static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004489timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004490 uint32_t key, void *data)
4491{
4492 struct weston_compositor *compositor = data;
4493
4494 if (weston_timeline_enabled_)
4495 weston_timeline_close();
4496 else
4497 weston_timeline_open(compositor);
4498}
4499
Giulio Camuffo459137b2014-10-11 23:56:24 +03004500/** Create the compositor.
4501 *
4502 * This functions creates and initializes a compositor instance.
4503 *
4504 * \param display The Wayland display to be used.
4505 * \param user_data A pointer to an object that can later be retrieved
4506 * using the \ref weston_compositor_get_user_data function.
4507 * \return The compositor instance on success or NULL on failure.
4508 */
4509WL_EXPORT struct weston_compositor *
4510weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004511{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004512 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004513 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004514
Giulio Camuffo459137b2014-10-11 23:56:24 +03004515 ec = zalloc(sizeof *ec);
4516 if (!ec)
4517 return NULL;
4518
4519 ec->wl_display = display;
4520 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004521 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004522 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004523 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004524 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004525 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004526 wl_signal_init(&ec->idle_signal);
4527 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004528 wl_signal_init(&ec->show_input_panel_signal);
4529 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004530 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004531 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004532 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004533 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004534 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004535 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004536 wl_signal_init(&ec->session_signal);
4537 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004538
Casey Dahlin58ba1372012-04-19 22:50:08 -04004539 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004540 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004541
Derek Foreman152254b2015-11-26 14:17:48 -06004542 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004543 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004544 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004545
Giulio Camuffo954f1832014-10-11 18:27:30 +03004546 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004547 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004548 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004549
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004550 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004551 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004552 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004553
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004554 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004555 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004556 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004557
Jason Ekstranda7af7042013-10-12 22:38:11 -05004558 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004559 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004560 wl_list_init(&ec->layer_list);
4561 wl_list_init(&ec->seat_list);
4562 wl_list_init(&ec->output_list);
4563 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004564 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004565 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004566 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004567 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004568 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004569
Xiong Zhang97116532013-10-23 13:58:31 +08004570 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004571 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004572
Giulio Camuffo459137b2014-10-11 23:56:24 +03004573 wl_data_device_manager_init(ec->wl_display);
4574
4575 wl_display_init_shm(ec->wl_display);
4576
4577 loop = wl_display_get_event_loop(ec->wl_display);
4578 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004579
Giulio Camuffo459137b2014-10-11 23:56:24 +03004580 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4581 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4582
4583 weston_compositor_add_debug_binding(ec, KEY_T,
4584 timeline_key_binding_handler, ec);
4585
Giulio Camuffo459137b2014-10-11 23:56:24 +03004586 return ec;
4587
4588fail:
4589 free(ec);
4590 return NULL;
4591}
4592
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004593WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004594weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004595{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004596 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004597
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004598 wl_event_source_remove(ec->idle_source);
4599
Matt Roper361d2ad2011-08-29 13:52:23 -07004600 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004601 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004602 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004603
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004604 if (ec->renderer)
4605 ec->renderer->destroy(ec);
4606
Daniel Stone325fc2d2012-05-30 16:31:58 +01004607 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004608 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004609 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004610 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004611 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004612 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004613
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004614 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004615}
4616
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004617WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004618weston_compositor_exit_with_code(struct weston_compositor *compositor,
4619 int exit_code)
4620{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004621 if (compositor->exit_code == EXIT_SUCCESS)
4622 compositor->exit_code = exit_code;
4623
Giulio Camuffo459137b2014-10-11 23:56:24 +03004624 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004625}
4626
4627WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004628weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4629 const struct weston_pointer_grab_interface *interface)
4630{
4631 struct weston_seat *seat;
4632
4633 ec->default_pointer_grab = interface;
4634 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004635 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4636
4637 if (pointer)
4638 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004639 }
4640}
4641
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004642WL_EXPORT int
4643weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4644 clockid_t clk_id)
4645{
4646 struct timespec ts;
4647
4648 if (clock_gettime(clk_id, &ts) < 0)
4649 return -1;
4650
4651 compositor->presentation_clock = clk_id;
4652
4653 return 0;
4654}
4655
4656/*
4657 * For choosing the software clock, when the display hardware or API
4658 * does not expose a compatible presentation timestamp.
4659 */
4660WL_EXPORT int
4661weston_compositor_set_presentation_clock_software(
4662 struct weston_compositor *compositor)
4663{
4664 /* In order of preference */
4665 static const clockid_t clocks[] = {
4666 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4667 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4668 CLOCK_MONOTONIC, /* no jumps, may crawl */
4669 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4670 CLOCK_REALTIME /* may jump and crawl */
4671 };
4672 unsigned i;
4673
4674 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4675 if (weston_compositor_set_presentation_clock(compositor,
4676 clocks[i]) == 0)
4677 return 0;
4678
4679 weston_log("Error: no suitable presentation clock available.\n");
4680
4681 return -1;
4682}
4683
Pekka Paalanen662f3842015-03-18 12:17:26 +02004684/** Read the current time from the Presentation clock
4685 *
4686 * \param compositor
4687 * \param ts[out] The current time.
4688 *
4689 * \note Reading the current time in user space is always imprecise to some
4690 * degree.
4691 *
4692 * This function is never meant to fail. If reading the clock does fail,
4693 * an error message is logged and a zero time is returned. Callers are not
4694 * supposed to detect or react to failures.
4695 */
4696WL_EXPORT void
4697weston_compositor_read_presentation_clock(
4698 const struct weston_compositor *compositor,
4699 struct timespec *ts)
4700{
4701 static bool warned;
4702 int ret;
4703
4704 ret = clock_gettime(compositor->presentation_clock, ts);
4705 if (ret < 0) {
4706 ts->tv_sec = 0;
4707 ts->tv_nsec = 0;
4708
4709 if (!warned)
4710 weston_log("Error: failure to read "
4711 "the presentation clock %#x: '%m' (%d)\n",
4712 compositor->presentation_clock, errno);
4713 warned = true;
4714 }
4715}
4716
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004717/** Import dmabuf buffer into current renderer
4718 *
4719 * \param compositor
4720 * \param buffer the dmabuf buffer to import
4721 * \return true on usable buffers, false otherwise
4722 *
4723 * This function tests that the linux_dmabuf_buffer is usable
4724 * for the current renderer. Returns false on unusable buffers. Usually
4725 * usability is tested by importing the dmabufs for composition.
4726 *
4727 * This hook is also used for detecting if the renderer supports
4728 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4729 * supported.
4730 * */
4731WL_EXPORT bool
4732weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4733 struct linux_dmabuf_buffer *buffer)
4734{
4735 struct weston_renderer *renderer;
4736
4737 renderer = compositor->renderer;
4738
4739 if (renderer->import_dmabuf == NULL)
4740 return false;
4741
4742 return renderer->import_dmabuf(compositor, buffer);
4743}
4744
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004745WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004746weston_version(int *major, int *minor, int *micro)
4747{
4748 *major = WESTON_VERSION_MAJOR;
4749 *minor = WESTON_VERSION_MINOR;
4750 *micro = WESTON_VERSION_MICRO;
4751}
4752
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004753WL_EXPORT void *
4754weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004755{
Derek Foreman3f86e502015-06-08 11:46:54 -05004756 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004757 char path[PATH_MAX];
4758 void *module, *init;
4759
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004760 if (name == NULL)
4761 return NULL;
4762
Derek Foreman3f86e502015-06-08 11:46:54 -05004763 if (name[0] != '/') {
4764 if (builddir)
4765 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4766 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03004767 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004768 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004769 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004770 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004771
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004772 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4773 if (module) {
4774 weston_log("Module '%s' already loaded\n", path);
4775 dlclose(module);
4776 return NULL;
4777 }
4778
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004779 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004780 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004781 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004782 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004783 return NULL;
4784 }
4785
4786 init = dlsym(module, entrypoint);
4787 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004788 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004789 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004790 return NULL;
4791 }
4792
4793 return init;
4794}
4795
Giulio Camuffo459137b2014-10-11 23:56:24 +03004796
4797/** Destroys the compositor.
4798 *
4799 * This function cleans up the compositor state and destroys it.
4800 *
4801 * \param compositor The compositor to be destroyed.
4802 */
4803WL_EXPORT void
4804weston_compositor_destroy(struct weston_compositor *compositor)
4805{
4806 /* prevent further rendering while shutting down */
4807 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4808
4809 wl_signal_emit(&compositor->destroy_signal, compositor);
4810
4811 weston_compositor_xkb_destroy(compositor);
4812
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004813 if (compositor->backend)
4814 compositor->backend->destroy(compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004815 free(compositor);
4816}
4817
4818/** Instruct the compositor to exit.
4819 *
4820 * This functions does not directly destroy the compositor object, it merely
4821 * command it to start the tear down process. It is not guaranteed that the
4822 * tear down will happen immediately.
4823 *
4824 * \param compositor The compositor to tear down.
4825 */
4826WL_EXPORT void
4827weston_compositor_exit(struct weston_compositor *compositor)
4828{
4829 compositor->exit(compositor);
4830}
4831
4832/** Return the user data stored in the compositor.
4833 *
4834 * This function returns the user data pointer set with user_data parameter
4835 * to the \ref weston_compositor_create function.
4836 */
4837WL_EXPORT void *
4838weston_compositor_get_user_data(struct weston_compositor *compositor)
4839{
4840 return compositor->user_data;
4841}
Pekka Paalanendd186732016-06-03 14:49:54 +03004842
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004843static const char * const backend_map[] = {
4844 [WESTON_BACKEND_DRM] = "drm-backend.so",
4845 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
4846 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
4847 [WESTON_BACKEND_RDP] = "rdp-backend.so",
4848 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
4849 [WESTON_BACKEND_X11] = "x11-backend.so",
4850};
4851
Pekka Paalanendd186732016-06-03 14:49:54 +03004852/** Load a backend into a weston_compositor
4853 *
4854 * A backend must be loaded to make a weston_compositor work. A backend
4855 * provides input and output capabilities, and determines the renderer to use.
4856 *
4857 * \param compositor A compositor that has not had a backend loaded yet.
4858 * \param backend Name of the backend file.
4859 * \param config_base A pointer to a backend-specific configuration
4860 * structure's 'base' member.
4861 *
4862 * \return 0 on success, or -1 on error.
4863 */
4864WL_EXPORT int
4865weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004866 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03004867 struct weston_backend_config *config_base)
4868{
4869 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03004870 struct weston_backend_config *config_base);
4871
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004872 if (backend < 0 || backend >= ARRAY_LENGTH(backend_map))
4873 return -1;
4874
4875 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03004876 if (!backend_init)
4877 return -1;
4878
Pekka Paalanena256c5e2016-06-03 14:56:18 +03004879 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03004880}