blob: 6fbda9908d7318aa02d2764db946fee42b01a1cc [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;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001855
1856 if (surface->viewport_resource)
1857 wl_resource_set_user_data(surface->viewport_resource, NULL);
1858
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001859 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001860}
1861
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001862static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001863weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1864{
1865 struct weston_buffer *buffer =
1866 container_of(listener, struct weston_buffer, destroy_listener);
1867
1868 wl_signal_emit(&buffer->destroy_signal, buffer);
1869 free(buffer);
1870}
1871
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001872WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001873weston_buffer_from_resource(struct wl_resource *resource)
1874{
1875 struct weston_buffer *buffer;
1876 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001877
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001878 listener = wl_resource_get_destroy_listener(resource,
1879 weston_buffer_destroy_handler);
1880
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001881 if (listener)
1882 return container_of(listener, struct weston_buffer,
1883 destroy_listener);
1884
1885 buffer = zalloc(sizeof *buffer);
1886 if (buffer == NULL)
1887 return NULL;
1888
1889 buffer->resource = resource;
1890 wl_signal_init(&buffer->destroy_signal);
1891 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001892 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001893 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001894
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001895 return buffer;
1896}
1897
1898static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001899weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1900 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001901{
Pekka Paalanende685b82012-12-04 15:58:12 +02001902 struct weston_buffer_reference *ref =
1903 container_of(listener, struct weston_buffer_reference,
1904 destroy_listener);
1905
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001906 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001907 ref->buffer = NULL;
1908}
1909
1910WL_EXPORT void
1911weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001912 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001913{
1914 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001915 ref->buffer->busy_count--;
1916 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001917 assert(wl_resource_get_client(ref->buffer->resource));
1918 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001919 WL_BUFFER_RELEASE);
1920 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001921 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001922 }
1923
Pekka Paalanende685b82012-12-04 15:58:12 +02001924 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001925 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001926 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001927 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001928 }
1929
Pekka Paalanende685b82012-12-04 15:58:12 +02001930 ref->buffer = buffer;
1931 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1932}
1933
1934static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001935weston_surface_attach(struct weston_surface *surface,
1936 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001937{
1938 weston_buffer_reference(&surface->buffer_ref, buffer);
1939
Pekka Paalanena6421c42012-12-04 15:58:10 +02001940 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001941 if (weston_surface_is_mapped(surface))
1942 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001943 }
1944
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001945 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001946
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001947 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001948 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001949}
1950
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001951WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001952weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001953{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001954 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001955
1956 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001957 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001958}
1959
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001960WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001961weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001962{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001963 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001964
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001965 pixman_region32_union(&compositor->primary_plane.damage,
1966 &compositor->primary_plane.damage,
1967 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001968 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001969}
1970
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001971static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001972surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001973{
Pekka Paalanende685b82012-12-04 15:58:12 +02001974 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001975 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001976 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001977
Pekka Paalanenb5026542014-11-12 15:09:24 +02001978 if (weston_timeline_enabled_ &&
1979 pixman_region32_not_empty(&surface->damage))
1980 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1981 TLP_OUTPUT(surface->output), TLP_END);
1982
Jason Ekstrandef540082014-06-26 10:37:36 -07001983 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001984}
1985
1986static void
1987view_accumulate_damage(struct weston_view *view,
1988 pixman_region32_t *opaque)
1989{
1990 pixman_region32_t damage;
1991
1992 pixman_region32_init(&damage);
1993 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001994 pixman_box32_t *extents;
1995
Jason Ekstranda7af7042013-10-12 22:38:11 -05001996 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001997 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001998 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001999 pixman_region32_copy(&damage, &view->surface->damage);
2000 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002001 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002002 }
2003
Pekka Paalanen380adf52015-02-16 14:39:11 +02002004 pixman_region32_intersect(&damage, &damage,
2005 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002006 pixman_region32_subtract(&damage, &damage, opaque);
2007 pixman_region32_union(&view->plane->damage,
2008 &view->plane->damage, &damage);
2009 pixman_region32_fini(&damage);
2010 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002011 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002012}
2013
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002014static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002015compositor_accumulate_damage(struct weston_compositor *ec)
2016{
2017 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002019 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002020
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002021 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002022
2023 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002024 pixman_region32_copy(&plane->clip, &clip);
2025
2026 pixman_region32_init(&opaque);
2027
Jason Ekstranda7af7042013-10-12 22:38:11 -05002028 wl_list_for_each(ev, &ec->view_list, link) {
2029 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002030 continue;
2031
Jason Ekstranda7af7042013-10-12 22:38:11 -05002032 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002033 }
2034
2035 pixman_region32_union(&clip, &clip, &opaque);
2036 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002037 }
2038
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002039 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002040
Jason Ekstranda7af7042013-10-12 22:38:11 -05002041 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002042 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002043
2044 wl_list_for_each(ev, &ec->view_list, link) {
2045 if (ev->surface->touched)
2046 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002047 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002048
2049 surface_flush_damage(ev->surface);
2050
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002051 /* Both the renderer and the backend have seen the buffer
2052 * by now. If renderer needs the buffer, it has its own
2053 * reference set. If the backend wants to keep the buffer
2054 * around for migrating the surface into a non-primary plane
2055 * later, keep_buffer is true. Otherwise, drop the core
2056 * reference now, and allow early buffer release. This enables
2057 * clients to use single-buffering.
2058 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002059 if (!ev->surface->keep_buffer)
2060 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002061 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002062}
2063
2064static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002065surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002066{
2067 struct weston_subsurface *sub;
2068
Pekka Paalanene67858b2013-04-25 13:57:42 +03002069 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002071 continue;
2072
Jason Ekstranda7af7042013-10-12 22:38:11 -05002073 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2074 wl_list_init(&sub->surface->views);
2075
2076 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002077 }
2078}
2079
2080static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002081surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002082{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083 struct weston_subsurface *sub;
2084 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002085
Jason Ekstranda7af7042013-10-12 22:38:11 -05002086 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2087 if (sub->surface == surface)
2088 continue;
2089
George Kiagiadakised04d382014-06-13 18:10:26 +02002090 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2091 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002092 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002093 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002094
2095 surface_free_unused_subsurface_views(sub->surface);
2096 }
2097}
2098
2099static void
2100view_list_add_subsurface_view(struct weston_compositor *compositor,
2101 struct weston_subsurface *sub,
2102 struct weston_view *parent)
2103{
2104 struct weston_subsurface *child;
2105 struct weston_view *view = NULL, *iv;
2106
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002107 if (!weston_surface_is_mapped(sub->surface))
2108 return;
2109
Jason Ekstranda7af7042013-10-12 22:38:11 -05002110 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2111 if (iv->geometry.parent == parent) {
2112 view = iv;
2113 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002114 }
2115 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002116
2117 if (view) {
2118 /* Put it back in the surface's list of views */
2119 wl_list_remove(&view->surface_link);
2120 wl_list_insert(&sub->surface->views, &view->surface_link);
2121 } else {
2122 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002123 weston_view_set_position(view,
2124 sub->position.x,
2125 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002126 weston_view_set_transform_parent(view, parent);
2127 }
2128
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002129 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002130 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131
Pekka Paalanenb188e912013-11-19 14:03:35 +02002132 if (wl_list_empty(&sub->surface->subsurface_list)) {
2133 wl_list_insert(compositor->view_list.prev, &view->link);
2134 return;
2135 }
2136
2137 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2138 if (child->surface == sub->surface)
2139 wl_list_insert(compositor->view_list.prev, &view->link);
2140 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002141 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002142 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143}
2144
2145static void
2146view_list_add(struct weston_compositor *compositor,
2147 struct weston_view *view)
2148{
2149 struct weston_subsurface *sub;
2150
2151 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002152
Pekka Paalanenb188e912013-11-19 14:03:35 +02002153 if (wl_list_empty(&view->surface->subsurface_list)) {
2154 wl_list_insert(compositor->view_list.prev, &view->link);
2155 return;
2156 }
2157
2158 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2159 if (sub->surface == view->surface)
2160 wl_list_insert(compositor->view_list.prev, &view->link);
2161 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002162 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002163 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002164}
2165
2166static void
2167weston_compositor_build_view_list(struct weston_compositor *compositor)
2168{
2169 struct weston_view *view;
2170 struct weston_layer *layer;
2171
2172 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002173 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002174 surface_stash_subsurface_views(view->surface);
2175
2176 wl_list_init(&compositor->view_list);
2177 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002178 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179 view_list_add(compositor, view);
2180 }
2181 }
2182
2183 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002184 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002185 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002186}
2187
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002188static void
2189weston_output_take_feedback_list(struct weston_output *output,
2190 struct weston_surface *surface)
2191{
2192 struct weston_view *view;
2193 struct weston_presentation_feedback *feedback;
2194 uint32_t flags = 0xffffffff;
2195
2196 if (wl_list_empty(&surface->feedback_list))
2197 return;
2198
2199 /* All views must have the flag for the flag to survive. */
2200 wl_list_for_each(view, &surface->views, surface_link) {
2201 /* ignore views that are not on this output at all */
2202 if (view->output_mask & (1u << output->id))
2203 flags &= view->psf_flags;
2204 }
2205
2206 wl_list_for_each(feedback, &surface->feedback_list, link)
2207 feedback->psf_flags = flags;
2208
2209 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2210 wl_list_init(&surface->feedback_list);
2211}
2212
David Herrmann1edf44c2013-10-22 17:11:26 +02002213static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002214weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002215{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002216 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002217 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002218 struct weston_animation *animation, *next;
2219 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002220 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002221 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002222 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002223
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002224 if (output->destroying)
2225 return 0;
2226
Pekka Paalanenb5026542014-11-12 15:09:24 +02002227 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2228
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002229 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002231
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002232 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002233 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002234 } else {
2235 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002236 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002237 ev->psf_flags = 0;
2238 }
2239 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002240
Pekka Paalanene67858b2013-04-25 13:57:42 +03002241 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002242 wl_list_for_each(ev, &ec->view_list, link) {
2243 /* Note: This operation is safe to do multiple times on the
2244 * same surface.
2245 */
2246 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002247 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002248 &ev->surface->frame_callback_list);
2249 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002250
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002251 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002252 }
2253 }
2254
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002255 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002256
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002257 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002258 pixman_region32_intersect(&output_damage,
2259 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002260 pixman_region32_subtract(&output_damage,
2261 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002262
Scott Moreauccbf29d2012-02-22 14:21:41 -07002263 if (output->dirty)
2264 weston_output_update_matrix(output);
2265
David Herrmann1edf44c2013-10-22 17:11:26 +02002266 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002267
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002268 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002269
Kristian Høgsbergef044142011-06-21 15:02:12 -04002270 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002271
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002272 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002273
Jonas Ådahldb773762012-06-13 00:01:21 +02002274 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002275 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002276 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002277 }
2278
Scott Moreaud64cf212012-06-08 19:40:54 -06002279 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002280 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002281 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002282 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002283
Pekka Paalanenb5026542014-11-12 15:09:24 +02002284 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2285
David Herrmann1edf44c2013-10-22 17:11:26 +02002286 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002287}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002288
Pekka Paalanen82919792014-05-21 13:51:49 +03002289static void
2290weston_output_schedule_repaint_reset(struct weston_output *output)
2291{
Pekka Paalanen82919792014-05-21 13:51:49 +03002292 output->repaint_scheduled = 0;
2293 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002294}
2295
Pekka Paalanen0513a952014-05-21 16:17:27 +03002296static int
2297output_repaint_timer_handler(void *data)
2298{
2299 struct weston_output *output = data;
2300 struct weston_compositor *compositor = output->compositor;
2301
2302 if (output->repaint_needed &&
2303 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2304 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2305 weston_output_repaint(output) == 0)
2306 return 0;
2307
2308 weston_output_schedule_repaint_reset(output);
2309
2310 return 0;
2311}
2312
Kristian Høgsbergef044142011-06-21 15:02:12 -04002313WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002314weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002315 const struct timespec *stamp,
2316 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002317{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002318 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002319 int32_t refresh_nsec;
2320 struct timespec now;
2321 struct timespec gone;
2322 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002323
Pekka Paalanenb5026542014-11-12 15:09:24 +02002324 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2325 TLP_VBLANK(stamp), TLP_END);
2326
Pekka Paalanend7894d02015-07-03 15:08:53 +03002327 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002328 weston_presentation_feedback_present_list(&output->feedback_list,
2329 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002330 output->msc,
2331 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002332
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002333 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002334
Pekka Paalanen0513a952014-05-21 16:17:27 +03002335 weston_compositor_read_presentation_clock(compositor, &now);
2336 timespec_sub(&gone, &now, stamp);
2337 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2338 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002339
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002340 if (msec < -1000 || msec > 1000) {
2341 static bool warned;
2342
2343 if (!warned)
2344 weston_log("Warning: computed repaint delay is "
2345 "insane: %d msec\n", msec);
2346 warned = true;
2347
2348 msec = 0;
2349 }
2350
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002351 /* Called from restart_repaint_loop and restart happens already after
2352 * the deadline given by repaint_msec? In that case we delay until
2353 * the deadline of the next frame, to give clients a more predictable
2354 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002355 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002356 msec += refresh_nsec / 1000000;
2357
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002358 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002359 output_repaint_timer_handler(output);
2360 else
2361 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002362}
2363
2364static void
2365idle_repaint(void *data)
2366{
2367 struct weston_output *output = data;
2368
Jonas Ådahle5a12252013-04-05 23:07:11 +02002369 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002370}
2371
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002372WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002373weston_layer_entry_insert(struct weston_layer_entry *list,
2374 struct weston_layer_entry *entry)
2375{
2376 wl_list_insert(&list->link, &entry->link);
2377 entry->layer = list->layer;
2378}
2379
2380WL_EXPORT void
2381weston_layer_entry_remove(struct weston_layer_entry *entry)
2382{
2383 wl_list_remove(&entry->link);
2384 wl_list_init(&entry->link);
2385 entry->layer = NULL;
2386}
2387
2388WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002389weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2390{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002391 wl_list_init(&layer->view_list.link);
2392 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002393 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002394 if (below != NULL)
2395 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002396}
2397
2398WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002399weston_layer_set_mask(struct weston_layer *layer,
2400 int x, int y, int width, int height)
2401{
2402 struct weston_view *view;
2403
2404 layer->mask.x1 = x;
2405 layer->mask.x2 = x + width;
2406 layer->mask.y1 = y;
2407 layer->mask.y2 = y + height;
2408
2409 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2410 weston_view_geometry_dirty(view);
2411 }
2412}
2413
2414WL_EXPORT void
2415weston_layer_set_mask_infinite(struct weston_layer *layer)
2416{
2417 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2418 UINT32_MAX, UINT32_MAX);
2419}
2420
2421WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002422weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002423{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002424 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002425 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002426
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002427 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2428 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002429 return;
2430
Pekka Paalanenb5026542014-11-12 15:09:24 +02002431 if (!output->repaint_needed)
2432 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2433
Kristian Høgsbergef044142011-06-21 15:02:12 -04002434 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002435 output->repaint_needed = 1;
2436 if (output->repaint_scheduled)
2437 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002438
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002439 wl_event_loop_add_idle(loop, idle_repaint, output);
2440 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002441 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002442}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002443
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002444WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002445weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2446{
2447 struct weston_output *output;
2448
2449 wl_list_for_each(output, &compositor->output_list, link)
2450 weston_output_schedule_repaint(output);
2451}
2452
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002453static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002454surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002455{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002456 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002457}
2458
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002459static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002460surface_attach(struct wl_client *client,
2461 struct wl_resource *resource,
2462 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2463{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002464 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002465 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002466
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002467 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002468 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002469 if (buffer == NULL) {
2470 wl_client_post_no_memory(client);
2471 return;
2472 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002473 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002474
Pekka Paalanende685b82012-12-04 15:58:12 +02002475 /* Attach, attach, without commit in between does not send
2476 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002477 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002478
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002479 surface->pending.sx = sx;
2480 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002481 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002482}
2483
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002484static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002485surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002486 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002487 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002488{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002489 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002490
Derek Foreman57e92ed2015-11-17 14:11:35 -06002491 if (width <= 0 || height <= 0)
2492 return;
2493
Derek Foreman152254b2015-11-26 14:17:48 -06002494 pixman_region32_union_rect(&surface->pending.damage_surface,
2495 &surface->pending.damage_surface,
2496 x, y, width, height);
2497}
2498
2499static void
2500surface_damage_buffer(struct wl_client *client,
2501 struct wl_resource *resource,
2502 int32_t x, int32_t y, int32_t width, int32_t height)
2503{
2504 struct weston_surface *surface = wl_resource_get_user_data(resource);
2505
2506 if (width <= 0 || height <= 0)
2507 return;
2508
2509 pixman_region32_union_rect(&surface->pending.damage_buffer,
2510 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002511 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002512}
2513
Kristian Høgsberg33418202011-08-16 23:01:28 -04002514static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002515destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002516{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002517 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002518
2519 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002520 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002521}
2522
2523static void
2524surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002525 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002526{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002527 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002528 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002529
2530 cb = malloc(sizeof *cb);
2531 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002532 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002533 return;
2534 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002535
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002536 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2537 callback);
2538 if (cb->resource == NULL) {
2539 free(cb);
2540 wl_resource_post_no_memory(resource);
2541 return;
2542 }
2543
Jason Ekstranda85118c2013-06-27 20:17:02 -05002544 wl_resource_set_implementation(cb->resource, NULL, cb,
2545 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002546
Pekka Paalanenbc106382012-10-10 12:49:31 +03002547 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002548}
2549
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002550static void
2551surface_set_opaque_region(struct wl_client *client,
2552 struct wl_resource *resource,
2553 struct wl_resource *region_resource)
2554{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002555 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002556 struct weston_region *region;
2557
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002558 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002559 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002560 pixman_region32_copy(&surface->pending.opaque,
2561 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002562 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002563 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002564 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002565}
2566
2567static void
2568surface_set_input_region(struct wl_client *client,
2569 struct wl_resource *resource,
2570 struct wl_resource *region_resource)
2571{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002572 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002573 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002574
2575 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002576 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002577 pixman_region32_copy(&surface->pending.input,
2578 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002579 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002580 pixman_region32_fini(&surface->pending.input);
2581 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002582 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002583}
2584
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002585static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002586weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002587{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002588 struct weston_subsurface *sub;
2589
2590 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2591 parent_link_pending) {
2592 wl_list_remove(&sub->parent_link);
2593 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2594 }
2595}
2596
2597static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002598weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002599 struct weston_matrix *matrix)
2600{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002601 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002602 double src_width, src_height, dest_width, dest_height;
2603
2604 weston_matrix_init(matrix);
2605
2606 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2607 src_width = surface->width_from_buffer;
2608 src_height = surface->height_from_buffer;
2609 } else {
2610 src_width = wl_fixed_to_double(vp->buffer.src_width);
2611 src_height = wl_fixed_to_double(vp->buffer.src_height);
2612 }
2613
2614 if (vp->surface.width == -1) {
2615 dest_width = src_width;
2616 dest_height = src_height;
2617 } else {
2618 dest_width = vp->surface.width;
2619 dest_height = vp->surface.height;
2620 }
2621
2622 if (src_width != dest_width || src_height != dest_height)
2623 weston_matrix_scale(matrix,
2624 src_width / dest_width,
2625 src_height / dest_height, 1);
2626
2627 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2628 weston_matrix_translate(matrix,
2629 wl_fixed_to_double(vp->buffer.src_x),
2630 wl_fixed_to_double(vp->buffer.src_y),
2631 0);
2632
2633 switch (vp->buffer.transform) {
2634 case WL_OUTPUT_TRANSFORM_FLIPPED:
2635 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2636 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2637 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2638 weston_matrix_scale(matrix, -1, 1, 1);
2639 weston_matrix_translate(matrix,
2640 surface->width_from_buffer, 0, 0);
2641 break;
2642 }
2643
2644 switch (vp->buffer.transform) {
2645 default:
2646 case WL_OUTPUT_TRANSFORM_NORMAL:
2647 case WL_OUTPUT_TRANSFORM_FLIPPED:
2648 break;
2649 case WL_OUTPUT_TRANSFORM_90:
2650 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2651 weston_matrix_rotate_xy(matrix, 0, 1);
2652 weston_matrix_translate(matrix,
2653 surface->height_from_buffer, 0, 0);
2654 break;
2655 case WL_OUTPUT_TRANSFORM_180:
2656 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2657 weston_matrix_rotate_xy(matrix, -1, 0);
2658 weston_matrix_translate(matrix,
2659 surface->width_from_buffer,
2660 surface->height_from_buffer, 0);
2661 break;
2662 case WL_OUTPUT_TRANSFORM_270:
2663 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2664 weston_matrix_rotate_xy(matrix, 0, -1);
2665 weston_matrix_translate(matrix,
2666 0, surface->width_from_buffer, 0);
2667 break;
2668 }
2669
2670 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2671}
2672
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002673/**
2674 * Compute a + b > c while being safe to overflows.
2675 */
2676static bool
2677fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2678{
2679 return (int64_t)a + (int64_t)b > (int64_t)c;
2680}
2681
2682static bool
2683weston_surface_is_pending_viewport_source_valid(
2684 const struct weston_surface *surface)
2685{
2686 const struct weston_surface_state *pend = &surface->pending;
2687 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2688 int width_from_buffer = 0;
2689 int height_from_buffer = 0;
2690 wl_fixed_t w;
2691 wl_fixed_t h;
2692
2693 /* If viewport source rect is not set, it is always ok. */
2694 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2695 return true;
2696
2697 if (pend->newly_attached) {
2698 if (pend->buffer) {
2699 convert_size_by_transform_scale(&width_from_buffer,
2700 &height_from_buffer,
2701 pend->buffer->width,
2702 pend->buffer->height,
2703 vp->buffer.transform,
2704 vp->buffer.scale);
2705 } else {
2706 /* No buffer: viewport is irrelevant. */
2707 return true;
2708 }
2709 } else {
2710 width_from_buffer = surface->width_from_buffer;
2711 height_from_buffer = surface->height_from_buffer;
2712 }
2713
2714 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2715 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2716
2717 /* No buffer: viewport is irrelevant. */
2718 if (width_from_buffer == 0 || height_from_buffer == 0)
2719 return true;
2720
2721 /* overflow checks for wl_fixed_from_int() */
2722 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2723 return false;
2724 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2725 return false;
2726
2727 w = wl_fixed_from_int(width_from_buffer);
2728 h = wl_fixed_from_int(height_from_buffer);
2729
2730 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2731 return false;
2732 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2733 return false;
2734
2735 return true;
2736}
2737
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002738static bool
2739fixed_is_integer(wl_fixed_t v)
2740{
2741 return (v & 0xff) == 0;
2742}
2743
2744static bool
2745weston_surface_is_pending_viewport_dst_size_int(
2746 const struct weston_surface *surface)
2747{
2748 const struct weston_buffer_viewport *vp =
2749 &surface->pending.buffer_viewport;
2750
2751 if (vp->surface.width != -1) {
2752 assert(vp->surface.width > 0 && vp->surface.height > 0);
2753 return true;
2754 }
2755
2756 return fixed_is_integer(vp->buffer.src_width) &&
2757 fixed_is_integer(vp->buffer.src_height);
2758}
2759
Derek Foreman152254b2015-11-26 14:17:48 -06002760/* Translate pending damage in buffer co-ordinates to surface
2761 * co-ordinates and union it with a pixman_region32_t.
2762 * This should only be called after the buffer is attached.
2763 */
2764static void
2765apply_damage_buffer(pixman_region32_t *dest,
2766 struct weston_surface *surface,
2767 struct weston_surface_state *state)
2768{
2769 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2770
2771 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2772 * translated into surface co-ordinates and unioned with
2773 * any other surface damage.
2774 * None of this makes sense if there is no buffer though.
2775 */
2776 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2777 pixman_region32_t buffer_damage;
2778
2779 pixman_region32_intersect_rect(&state->damage_buffer,
2780 &state->damage_buffer,
2781 0, 0, buffer->width,
2782 buffer->height);
2783 pixman_region32_init(&buffer_damage);
2784 weston_matrix_transform_region(&buffer_damage,
2785 &surface->buffer_to_surface_matrix,
2786 &state->damage_buffer);
2787 pixman_region32_union(dest, dest, &buffer_damage);
2788 pixman_region32_fini(&buffer_damage);
2789 }
2790 /* We should clear this on commit even if there was no buffer */
2791 pixman_region32_clear(&state->damage_buffer);
2792}
2793
Jason Ekstrand1e059042014-10-16 10:55:19 -05002794static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002795weston_surface_commit_state(struct weston_surface *surface,
2796 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002797{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002798 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002799 pixman_region32_t opaque;
2800
Alexander Larsson4ea95522013-05-22 14:41:37 +02002801 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002802 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002803 /* wp_viewport.set_source */
2804 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002805 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002806
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002807 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002808 if (state->newly_attached)
2809 weston_surface_attach(surface, state->buffer);
2810 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002811
Jason Ekstrand1e059042014-10-16 10:55:19 -05002812 weston_surface_build_buffer_matrix(surface,
2813 &surface->surface_to_buffer_matrix);
2814 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2815 &surface->surface_to_buffer_matrix);
2816
Jason Ekstrand7b982072014-05-20 14:33:03 -05002817 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002818 weston_surface_update_size(surface);
2819 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002820 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002821 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002822
Jason Ekstrand7b982072014-05-20 14:33:03 -05002823 state->sx = 0;
2824 state->sy = 0;
2825 state->newly_attached = 0;
2826 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002827
Derek Foreman152254b2015-11-26 14:17:48 -06002828 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002829 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002830 (pixman_region32_not_empty(&state->damage_surface) ||
2831 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002832 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002833
Pekka Paalanen8e159182012-10-10 12:49:25 +03002834 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002835 &state->damage_surface);
2836
2837 apply_damage_buffer(&surface->damage, surface, state);
2838
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002839 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002840 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002841 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002842
2843 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002844 pixman_region32_init(&opaque);
2845 pixman_region32_intersect_rect(&opaque, &state->opaque,
2846 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002847
2848 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2849 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002850 wl_list_for_each(view, &surface->views, surface_link)
2851 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002852 }
2853
2854 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002855
2856 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002857 pixman_region32_intersect_rect(&surface->input, &state->input,
2858 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002859
Pekka Paalanenbc106382012-10-10 12:49:31 +03002860 /* wl_surface.frame */
2861 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002862 &state->frame_callback_list);
2863 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002864
2865 /* XXX:
2866 * What should happen with a feedback request, if there
2867 * is no wl_buffer attached for this commit?
2868 */
2869
2870 /* presentation.feedback */
2871 wl_list_insert_list(&surface->feedback_list,
2872 &state->feedback_list);
2873 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002874}
2875
2876static void
2877weston_surface_commit(struct weston_surface *surface)
2878{
2879 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002880
Pekka Paalanene67858b2013-04-25 13:57:42 +03002881 weston_surface_commit_subsurface_order(surface);
2882
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002883 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002884}
2885
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002886static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002887weston_subsurface_commit(struct weston_subsurface *sub);
2888
2889static void
2890weston_subsurface_parent_commit(struct weston_subsurface *sub,
2891 int parent_is_synchronized);
2892
2893static void
2894surface_commit(struct wl_client *client, struct wl_resource *resource)
2895{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002896 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002897 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2898
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002899 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
2900 assert(surface->viewport_resource);
2901
2902 wl_resource_post_error(surface->viewport_resource,
2903 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
2904 "wl_surface@%d has viewport source outside buffer",
2905 wl_resource_get_id(resource));
2906 return;
2907 }
2908
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002909 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
2910 assert(surface->viewport_resource);
2911
2912 wl_resource_post_error(surface->viewport_resource,
2913 WP_VIEWPORT_ERROR_BAD_SIZE,
2914 "wl_surface@%d viewport dst size not integer",
2915 wl_resource_get_id(resource));
2916 return;
2917 }
2918
Pekka Paalanene67858b2013-04-25 13:57:42 +03002919 if (sub) {
2920 weston_subsurface_commit(sub);
2921 return;
2922 }
2923
2924 weston_surface_commit(surface);
2925
2926 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2927 if (sub->surface != surface)
2928 weston_subsurface_parent_commit(sub, 0);
2929 }
2930}
2931
2932static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002933surface_set_buffer_transform(struct wl_client *client,
2934 struct wl_resource *resource, int transform)
2935{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002936 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002937
Jonny Lamba55f1392014-05-30 12:07:15 +02002938 /* if wl_output.transform grows more members this will need to be updated. */
2939 if (transform < 0 ||
2940 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2941 wl_resource_post_error(resource,
2942 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2943 "buffer transform must be a valid transform "
2944 "('%d' specified)", transform);
2945 return;
2946 }
2947
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002948 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002949 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002950}
2951
Alexander Larsson4ea95522013-05-22 14:41:37 +02002952static void
2953surface_set_buffer_scale(struct wl_client *client,
2954 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002955 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002956{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002957 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002958
Jonny Lamba55f1392014-05-30 12:07:15 +02002959 if (scale < 1) {
2960 wl_resource_post_error(resource,
2961 WL_SURFACE_ERROR_INVALID_SCALE,
2962 "buffer scale must be at least one "
2963 "('%d' specified)", scale);
2964 return;
2965 }
2966
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002967 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002968 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002969}
2970
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002971static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002972 surface_destroy,
2973 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002974 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002975 surface_frame,
2976 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002977 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002978 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002979 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06002980 surface_set_buffer_scale,
2981 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002982};
2983
2984static void
2985compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002986 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002987{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002988 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002989 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002990
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002991 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002992 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002993 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002994 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002995 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002996
Jason Ekstranda85118c2013-06-27 20:17:02 -05002997 surface->resource =
2998 wl_resource_create(client, &wl_surface_interface,
2999 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003000 if (surface->resource == NULL) {
3001 weston_surface_destroy(surface);
3002 wl_resource_post_no_memory(resource);
3003 return;
3004 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003005 wl_resource_set_implementation(surface->resource, &surface_interface,
3006 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003007
3008 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003009}
3010
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003011static void
3012destroy_region(struct wl_resource *resource)
3013{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003014 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003015
3016 pixman_region32_fini(&region->region);
3017 free(region);
3018}
3019
3020static void
3021region_destroy(struct wl_client *client, struct wl_resource *resource)
3022{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003023 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003024}
3025
3026static void
3027region_add(struct wl_client *client, struct wl_resource *resource,
3028 int32_t x, int32_t y, int32_t width, int32_t height)
3029{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003030 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003031
3032 pixman_region32_union_rect(&region->region, &region->region,
3033 x, y, width, height);
3034}
3035
3036static void
3037region_subtract(struct wl_client *client, struct wl_resource *resource,
3038 int32_t x, int32_t y, int32_t width, int32_t height)
3039{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003040 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003041 pixman_region32_t rect;
3042
3043 pixman_region32_init_rect(&rect, x, y, width, height);
3044 pixman_region32_subtract(&region->region, &region->region, &rect);
3045 pixman_region32_fini(&rect);
3046}
3047
3048static const struct wl_region_interface region_interface = {
3049 region_destroy,
3050 region_add,
3051 region_subtract
3052};
3053
3054static void
3055compositor_create_region(struct wl_client *client,
3056 struct wl_resource *resource, uint32_t id)
3057{
3058 struct weston_region *region;
3059
3060 region = malloc(sizeof *region);
3061 if (region == NULL) {
3062 wl_resource_post_no_memory(resource);
3063 return;
3064 }
3065
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003066 pixman_region32_init(&region->region);
3067
Jason Ekstranda85118c2013-06-27 20:17:02 -05003068 region->resource =
3069 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003070 if (region->resource == NULL) {
3071 free(region);
3072 wl_resource_post_no_memory(resource);
3073 return;
3074 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003075 wl_resource_set_implementation(region->resource, &region_interface,
3076 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003077}
3078
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003079static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003080 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003081 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003082};
3083
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003084static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003085weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3086{
3087 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003088
Jason Ekstrand7b982072014-05-20 14:33:03 -05003089 weston_surface_commit_state(surface, &sub->cached);
3090 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003091
3092 weston_surface_commit_subsurface_order(surface);
3093
3094 weston_surface_schedule_repaint(surface);
3095
Jason Ekstrand7b982072014-05-20 14:33:03 -05003096 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003097}
3098
3099static void
3100weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3101{
3102 struct weston_surface *surface = sub->surface;
3103
3104 /*
3105 * If this commit would cause the surface to move by the
3106 * attach(dx, dy) parameters, the old damage region must be
3107 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003108 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003109 */
Derek Foreman152254b2015-11-26 14:17:48 -06003110 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003111 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003112 pixman_region32_union(&sub->cached.damage_surface,
3113 &sub->cached.damage_surface,
3114 &surface->pending.damage_surface);
3115 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003116
3117 if (surface->pending.newly_attached) {
3118 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003119 weston_surface_state_set_buffer(&sub->cached,
3120 surface->pending.buffer);
3121 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003122 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003123 weston_presentation_feedback_discard_list(
3124 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003125 }
3126 sub->cached.sx += surface->pending.sx;
3127 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003128
Derek Foreman152254b2015-11-26 14:17:48 -06003129 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3130
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003131 sub->cached.buffer_viewport.changed |=
3132 surface->pending.buffer_viewport.changed;
3133 sub->cached.buffer_viewport.buffer =
3134 surface->pending.buffer_viewport.buffer;
3135 sub->cached.buffer_viewport.surface =
3136 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003137
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003138 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003139
3140 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3141
3142 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3143
3144 wl_list_insert_list(&sub->cached.frame_callback_list,
3145 &surface->pending.frame_callback_list);
3146 wl_list_init(&surface->pending.frame_callback_list);
3147
Pekka Paalanen133e4392014-09-23 22:08:46 -04003148 wl_list_insert_list(&sub->cached.feedback_list,
3149 &surface->pending.feedback_list);
3150 wl_list_init(&surface->pending.feedback_list);
3151
Jason Ekstrand7b982072014-05-20 14:33:03 -05003152 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003153}
3154
Derek Foreman280e7dd2014-10-03 13:13:42 -05003155static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003156weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3157{
3158 while (sub) {
3159 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003160 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003161
3162 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003163 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003164
3165 sub = weston_surface_to_subsurface(sub->parent);
3166 }
3167
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003168 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003169}
3170
3171static void
3172weston_subsurface_commit(struct weston_subsurface *sub)
3173{
3174 struct weston_surface *surface = sub->surface;
3175 struct weston_subsurface *tmp;
3176
3177 /* Recursive check for effectively synchronized. */
3178 if (weston_subsurface_is_synchronized(sub)) {
3179 weston_subsurface_commit_to_cache(sub);
3180 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003181 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003182 /* flush accumulated state from cache */
3183 weston_subsurface_commit_to_cache(sub);
3184 weston_subsurface_commit_from_cache(sub);
3185 } else {
3186 weston_surface_commit(surface);
3187 }
3188
3189 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3190 if (tmp->surface != surface)
3191 weston_subsurface_parent_commit(tmp, 0);
3192 }
3193 }
3194}
3195
3196static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003197weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003198{
3199 struct weston_surface *surface = sub->surface;
3200 struct weston_subsurface *tmp;
3201
Pekka Paalanene67858b2013-04-25 13:57:42 +03003202 /* From now on, commit_from_cache the whole sub-tree, regardless of
3203 * the synchronized mode of each child. This sub-surface or some
3204 * of its ancestors were synchronized, so we are synchronized
3205 * all the way down.
3206 */
3207
Jason Ekstrand7b982072014-05-20 14:33:03 -05003208 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003209 weston_subsurface_commit_from_cache(sub);
3210
3211 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3212 if (tmp->surface != surface)
3213 weston_subsurface_parent_commit(tmp, 1);
3214 }
3215}
3216
3217static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003218weston_subsurface_parent_commit(struct weston_subsurface *sub,
3219 int parent_is_synchronized)
3220{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003221 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003222 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003223 wl_list_for_each(view, &sub->surface->views, surface_link)
3224 weston_view_set_position(view,
3225 sub->position.x,
3226 sub->position.y);
3227
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003228 sub->position.set = 0;
3229 }
3230
3231 if (parent_is_synchronized || sub->synchronized)
3232 weston_subsurface_synchronized_commit(sub);
3233}
3234
Pekka Paalanen8274d902014-08-06 19:36:51 +03003235static int
3236subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3237{
3238 return snprintf(buf, len, "sub-surface");
3239}
3240
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003241static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003242subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003243{
3244 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003245 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003246
Jason Ekstranda7af7042013-10-12 22:38:11 -05003247 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003248 weston_view_set_position(view,
3249 view->geometry.x + dx,
3250 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003251
3252 /* No need to check parent mappedness, because if parent is not
3253 * mapped, parent is not in a visible layer, so this sub-surface
3254 * will not be drawn either.
3255 */
3256 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003257 struct weston_output *output;
3258
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003259 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003260 * because that would call it also for the parent surface,
3261 * which might not be mapped yet. That would lead to
3262 * inconsistent state, where the window could never be
3263 * mapped.
3264 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003265 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003266 * weston_surface_is_mapped() return true, so that when the
3267 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003268 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003269 */
3270 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003271 output = container_of(compositor->output_list.next,
3272 struct weston_output, link);
3273
3274 surface->output = output;
Bryce Harrington89324ce2015-12-23 18:38:07 -08003275 weston_surface_update_output_mask(surface, 1u << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003276 }
3277}
3278
3279static struct weston_subsurface *
3280weston_surface_to_subsurface(struct weston_surface *surface)
3281{
3282 if (surface->configure == subsurface_configure)
3283 return surface->configure_private;
3284
3285 return NULL;
3286}
3287
Pekka Paalanen01388e22013-04-25 13:57:44 +03003288WL_EXPORT struct weston_surface *
3289weston_surface_get_main_surface(struct weston_surface *surface)
3290{
3291 struct weston_subsurface *sub;
3292
3293 while (surface && (sub = weston_surface_to_subsurface(surface)))
3294 surface = sub->parent;
3295
3296 return surface;
3297}
3298
Pekka Paalanen50b67472014-10-01 15:02:41 +03003299WL_EXPORT int
3300weston_surface_set_role(struct weston_surface *surface,
3301 const char *role_name,
3302 struct wl_resource *error_resource,
3303 uint32_t error_code)
3304{
3305 assert(role_name);
3306
3307 if (surface->role_name == NULL ||
3308 surface->role_name == role_name ||
3309 strcmp(surface->role_name, role_name) == 0) {
3310 surface->role_name = role_name;
3311
3312 return 0;
3313 }
3314
3315 wl_resource_post_error(error_resource, error_code,
3316 "Cannot assign role %s to wl_surface@%d,"
3317 " already has role %s\n",
3318 role_name,
3319 wl_resource_get_id(surface->resource),
3320 surface->role_name);
3321 return -1;
3322}
3323
Pekka Paalanen8274d902014-08-06 19:36:51 +03003324WL_EXPORT void
3325weston_surface_set_label_func(struct weston_surface *surface,
3326 int (*desc)(struct weston_surface *,
3327 char *, size_t))
3328{
3329 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003330 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003331}
3332
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003333/** Get the size of surface contents
3334 *
3335 * \param surface The surface to query.
3336 * \param width Returns the width of raw contents.
3337 * \param height Returns the height of raw contents.
3338 *
3339 * Retrieves the raw surface content size in pixels for the given surface.
3340 * This is the whole content size in buffer pixels. If the surface
3341 * has no content or the renderer does not implement this feature,
3342 * zeroes are returned.
3343 *
3344 * This function is used to determine the buffer size needed for
3345 * a weston_surface_copy_content() call.
3346 */
3347WL_EXPORT void
3348weston_surface_get_content_size(struct weston_surface *surface,
3349 int *width, int *height)
3350{
3351 struct weston_renderer *rer = surface->compositor->renderer;
3352
3353 if (!rer->surface_get_content_size) {
3354 *width = 0;
3355 *height = 0;
3356 return;
3357 }
3358
3359 rer->surface_get_content_size(surface, width, height);
3360}
3361
3362/** Copy surface contents to system memory.
3363 *
3364 * \param surface The surface to copy from.
3365 * \param target Pointer to the target memory buffer.
3366 * \param size Size of the target buffer in bytes.
3367 * \param src_x X location on contents to copy from.
3368 * \param src_y Y location on contents to copy from.
3369 * \param width Width in pixels of the area to copy.
3370 * \param height Height in pixels of the area to copy.
3371 * \return 0 for success, -1 for failure.
3372 *
3373 * Surface contents are maintained by the renderer. They can be in a
3374 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3375 * else.
3376 *
3377 * Surface contents are copied into memory pointed to by target,
3378 * which has size bytes of space available. The target memory
3379 * may be larger than needed, but being smaller returns an error.
3380 * The extra bytes in target may or may not be written; their content is
3381 * unspecified. Size must be large enough to hold the image.
3382 *
3383 * The image in the target memory will be arranged in rows from
3384 * top to bottom, and pixels on a row from left to right. The pixel
3385 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3386 * width * 4.
3387 *
3388 * Parameters src_x and src_y define the upper-left corner in buffer
3389 * coordinates (pixels) to copy from. Parameters width and height
3390 * define the size of the area to copy in pixels.
3391 *
3392 * The rectangle defined by src_x, src_y, width, height must fit in
3393 * the surface contents. Otherwise an error is returned.
3394 *
3395 * Use surface_get_data_size to determine the content size; the
3396 * needed target buffer size and rectangle limits.
3397 *
3398 * CURRENT IMPLEMENTATION RESTRICTIONS:
3399 * - the machine must be little-endian due to Pixman formats.
3400 *
3401 * NOTE: Pixman formats are premultiplied.
3402 */
3403WL_EXPORT int
3404weston_surface_copy_content(struct weston_surface *surface,
3405 void *target, size_t size,
3406 int src_x, int src_y,
3407 int width, int height)
3408{
3409 struct weston_renderer *rer = surface->compositor->renderer;
3410 int cw, ch;
3411 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3412
3413 if (!rer->surface_copy_content)
3414 return -1;
3415
3416 weston_surface_get_content_size(surface, &cw, &ch);
3417
3418 if (src_x < 0 || src_y < 0)
3419 return -1;
3420
3421 if (width <= 0 || height <= 0)
3422 return -1;
3423
3424 if (src_x + width > cw || src_y + height > ch)
3425 return -1;
3426
3427 if (width * bytespp * height > size)
3428 return -1;
3429
3430 return rer->surface_copy_content(surface, target, size,
3431 src_x, src_y, width, height);
3432}
3433
Pekka Paalanene67858b2013-04-25 13:57:42 +03003434static void
3435subsurface_set_position(struct wl_client *client,
3436 struct wl_resource *resource, int32_t x, int32_t y)
3437{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003438 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003439
3440 if (!sub)
3441 return;
3442
3443 sub->position.x = x;
3444 sub->position.y = y;
3445 sub->position.set = 1;
3446}
3447
3448static struct weston_subsurface *
3449subsurface_from_surface(struct weston_surface *surface)
3450{
3451 struct weston_subsurface *sub;
3452
3453 sub = weston_surface_to_subsurface(surface);
3454 if (sub)
3455 return sub;
3456
3457 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3458 if (sub->surface == surface)
3459 return sub;
3460
3461 return NULL;
3462}
3463
3464static struct weston_subsurface *
3465subsurface_sibling_check(struct weston_subsurface *sub,
3466 struct weston_surface *surface,
3467 const char *request)
3468{
3469 struct weston_subsurface *sibling;
3470
3471 sibling = subsurface_from_surface(surface);
3472
3473 if (!sibling) {
3474 wl_resource_post_error(sub->resource,
3475 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3476 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003477 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003478 return NULL;
3479 }
3480
3481 if (sibling->parent != sub->parent) {
3482 wl_resource_post_error(sub->resource,
3483 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3484 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003485 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003486 return NULL;
3487 }
3488
3489 return sibling;
3490}
3491
3492static void
3493subsurface_place_above(struct wl_client *client,
3494 struct wl_resource *resource,
3495 struct wl_resource *sibling_resource)
3496{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003497 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003498 struct weston_surface *surface =
3499 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003500 struct weston_subsurface *sibling;
3501
3502 if (!sub)
3503 return;
3504
3505 sibling = subsurface_sibling_check(sub, surface, "place_above");
3506 if (!sibling)
3507 return;
3508
3509 wl_list_remove(&sub->parent_link_pending);
3510 wl_list_insert(sibling->parent_link_pending.prev,
3511 &sub->parent_link_pending);
3512}
3513
3514static void
3515subsurface_place_below(struct wl_client *client,
3516 struct wl_resource *resource,
3517 struct wl_resource *sibling_resource)
3518{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003519 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003520 struct weston_surface *surface =
3521 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003522 struct weston_subsurface *sibling;
3523
3524 if (!sub)
3525 return;
3526
3527 sibling = subsurface_sibling_check(sub, surface, "place_below");
3528 if (!sibling)
3529 return;
3530
3531 wl_list_remove(&sub->parent_link_pending);
3532 wl_list_insert(&sibling->parent_link_pending,
3533 &sub->parent_link_pending);
3534}
3535
3536static void
3537subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3538{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003539 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003540
3541 if (sub)
3542 sub->synchronized = 1;
3543}
3544
3545static void
3546subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3547{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003548 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003549
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003550 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003552
3553 /* If sub became effectively desynchronized, flush. */
3554 if (!weston_subsurface_is_synchronized(sub))
3555 weston_subsurface_synchronized_commit(sub);
3556 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003557}
3558
3559static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003560weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3561{
3562 wl_list_remove(&sub->parent_link);
3563 wl_list_remove(&sub->parent_link_pending);
3564 wl_list_remove(&sub->parent_destroy_listener.link);
3565 sub->parent = NULL;
3566}
3567
3568static void
3569weston_subsurface_destroy(struct weston_subsurface *sub);
3570
3571static void
3572subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3573{
3574 struct weston_subsurface *sub =
3575 container_of(listener, struct weston_subsurface,
3576 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003577 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003578
3579 /* The protocol object (wl_resource) is left inert. */
3580 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003581 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003582
3583 weston_subsurface_destroy(sub);
3584}
3585
3586static void
3587subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3588{
3589 struct weston_subsurface *sub =
3590 container_of(listener, struct weston_subsurface,
3591 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003592 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003593 assert(sub->surface != sub->parent);
3594
3595 if (weston_surface_is_mapped(sub->surface))
3596 weston_surface_unmap(sub->surface);
3597
3598 weston_subsurface_unlink_parent(sub);
3599}
3600
3601static void
3602subsurface_resource_destroy(struct wl_resource *resource)
3603{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003604 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003605
3606 if (sub)
3607 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003608}
3609
3610static void
3611subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3612{
3613 wl_resource_destroy(resource);
3614}
3615
3616static void
3617weston_subsurface_link_parent(struct weston_subsurface *sub,
3618 struct weston_surface *parent)
3619{
3620 sub->parent = parent;
3621 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003622 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003623 &sub->parent_destroy_listener);
3624
3625 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3626 wl_list_insert(&parent->subsurface_list_pending,
3627 &sub->parent_link_pending);
3628}
3629
3630static void
3631weston_subsurface_link_surface(struct weston_subsurface *sub,
3632 struct weston_surface *surface)
3633{
3634 sub->surface = surface;
3635 sub->surface_destroy_listener.notify =
3636 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003637 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003638 &sub->surface_destroy_listener);
3639}
3640
3641static void
3642weston_subsurface_destroy(struct weston_subsurface *sub)
3643{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003644 struct weston_view *view, *next;
3645
Pekka Paalanene67858b2013-04-25 13:57:42 +03003646 assert(sub->surface);
3647
3648 if (sub->resource) {
3649 assert(weston_surface_to_subsurface(sub->surface) == sub);
3650 assert(sub->parent_destroy_listener.notify ==
3651 subsurface_handle_parent_destroy);
3652
George Kiagiadakised04d382014-06-13 18:10:26 +02003653 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3654 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003655 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003656 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003657
Pekka Paalanene67858b2013-04-25 13:57:42 +03003658 if (sub->parent)
3659 weston_subsurface_unlink_parent(sub);
3660
Jason Ekstrand7b982072014-05-20 14:33:03 -05003661 weston_surface_state_fini(&sub->cached);
3662 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003663
3664 sub->surface->configure = NULL;
3665 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003666 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003667 } else {
3668 /* the dummy weston_subsurface for the parent itself */
3669 assert(sub->parent_destroy_listener.notify == NULL);
3670 wl_list_remove(&sub->parent_link);
3671 wl_list_remove(&sub->parent_link_pending);
3672 }
3673
3674 wl_list_remove(&sub->surface_destroy_listener.link);
3675 free(sub);
3676}
3677
3678static const struct wl_subsurface_interface subsurface_implementation = {
3679 subsurface_destroy,
3680 subsurface_set_position,
3681 subsurface_place_above,
3682 subsurface_place_below,
3683 subsurface_set_sync,
3684 subsurface_set_desync
3685};
3686
3687static struct weston_subsurface *
3688weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3689 struct weston_surface *parent)
3690{
3691 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003692 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003693
Bryce Harringtonde16d892014-11-20 22:21:57 -08003694 sub = zalloc(sizeof *sub);
3695 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003696 return NULL;
3697
Jason Ekstranda7af7042013-10-12 22:38:11 -05003698 wl_list_init(&sub->unused_views);
3699
Jason Ekstranda85118c2013-06-27 20:17:02 -05003700 sub->resource =
3701 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003702 if (!sub->resource) {
3703 free(sub);
3704 return NULL;
3705 }
3706
Jason Ekstranda85118c2013-06-27 20:17:02 -05003707 wl_resource_set_implementation(sub->resource,
3708 &subsurface_implementation,
3709 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003710 weston_subsurface_link_surface(sub, surface);
3711 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003712 weston_surface_state_init(&sub->cached);
3713 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003714 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003715
3716 return sub;
3717}
3718
3719/* Create a dummy subsurface for having the parent itself in its
3720 * sub-surface lists. Makes stacking order manipulation easy.
3721 */
3722static struct weston_subsurface *
3723weston_subsurface_create_for_parent(struct weston_surface *parent)
3724{
3725 struct weston_subsurface *sub;
3726
Bryce Harringtonde16d892014-11-20 22:21:57 -08003727 sub = zalloc(sizeof *sub);
3728 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003729 return NULL;
3730
3731 weston_subsurface_link_surface(sub, parent);
3732 sub->parent = parent;
3733 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3734 wl_list_insert(&parent->subsurface_list_pending,
3735 &sub->parent_link_pending);
3736
3737 return sub;
3738}
3739
3740static void
3741subcompositor_get_subsurface(struct wl_client *client,
3742 struct wl_resource *resource,
3743 uint32_t id,
3744 struct wl_resource *surface_resource,
3745 struct wl_resource *parent_resource)
3746{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003747 struct weston_surface *surface =
3748 wl_resource_get_user_data(surface_resource);
3749 struct weston_surface *parent =
3750 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003751 struct weston_subsurface *sub;
3752 static const char where[] = "get_subsurface: wl_subsurface@";
3753
3754 if (surface == parent) {
3755 wl_resource_post_error(resource,
3756 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3757 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003758 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003759 return;
3760 }
3761
3762 if (weston_surface_to_subsurface(surface)) {
3763 wl_resource_post_error(resource,
3764 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3765 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003766 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003767 return;
3768 }
3769
Pekka Paalanen50b67472014-10-01 15:02:41 +03003770 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3771 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003772 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003773
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003774 if (weston_surface_get_main_surface(parent) == surface) {
3775 wl_resource_post_error(resource,
3776 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3777 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003778 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003779 return;
3780 }
3781
Pekka Paalanene67858b2013-04-25 13:57:42 +03003782 /* make sure the parent is in its own list */
3783 if (wl_list_empty(&parent->subsurface_list)) {
3784 if (!weston_subsurface_create_for_parent(parent)) {
3785 wl_resource_post_no_memory(resource);
3786 return;
3787 }
3788 }
3789
3790 sub = weston_subsurface_create(id, surface, parent);
3791 if (!sub) {
3792 wl_resource_post_no_memory(resource);
3793 return;
3794 }
3795
3796 surface->configure = subsurface_configure;
3797 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003798 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003799}
3800
3801static void
3802subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3803{
3804 wl_resource_destroy(resource);
3805}
3806
3807static const struct wl_subcompositor_interface subcompositor_interface = {
3808 subcompositor_destroy,
3809 subcompositor_get_subsurface
3810};
3811
3812static void
3813bind_subcompositor(struct wl_client *client,
3814 void *data, uint32_t version, uint32_t id)
3815{
3816 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003817 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003818
Jason Ekstranda85118c2013-06-27 20:17:02 -05003819 resource =
3820 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003821 if (resource == NULL) {
3822 wl_client_post_no_memory(client);
3823 return;
3824 }
3825 wl_resource_set_implementation(resource, &subcompositor_interface,
3826 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003827}
3828
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003829/** Set a DPMS mode on all of the compositor's outputs
3830 *
3831 * \param compositor The compositor instance
3832 * \param state The DPMS state the outputs will be set to
3833 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003834static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003835weston_compositor_dpms(struct weston_compositor *compositor,
3836 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003837{
3838 struct weston_output *output;
3839
3840 wl_list_for_each(output, &compositor->output_list, link)
3841 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003842 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003843}
3844
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003845/** Restores the compositor to active status
3846 *
3847 * \param compositor The compositor instance
3848 *
3849 * If the compositor was in a sleeping mode, all outputs are powered
3850 * back on via DPMS. Otherwise if the compositor was inactive
3851 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3852 * signal will fire.
3853 *
3854 * Restarts the idle timer.
3855 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003856WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003857weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003858{
Neil Roberts8b62e202013-09-30 13:14:47 +01003859 uint32_t old_state = compositor->state;
3860
3861 /* The state needs to be changed before emitting the wake
3862 * signal because that may try to schedule a repaint which
3863 * will not work if the compositor is still sleeping */
3864 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3865
3866 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003867 case WESTON_COMPOSITOR_SLEEPING:
3868 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3869 /* fall through */
3870 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003871 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003872 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003873 /* fall through */
3874 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003875 wl_event_source_timer_update(compositor->idle_source,
3876 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003877 }
3878}
3879
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003880/** Turns off rendering and frame events for the compositor.
3881 *
3882 * \param compositor The compositor instance
3883 *
3884 * This is used for example to prevent further rendering while the
3885 * compositor is shutting down.
3886 *
3887 * \note When offscreen state is entered, outputs will be powered
3888 * back on if they were sleeping (in DPMS off mode), even though
3889 * no rendering will be performed.
3890 *
3891 * Stops the idle timer.
3892 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003893WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003894weston_compositor_offscreen(struct weston_compositor *compositor)
3895{
3896 switch (compositor->state) {
3897 case WESTON_COMPOSITOR_OFFSCREEN:
3898 return;
3899 case WESTON_COMPOSITOR_SLEEPING:
3900 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3901 /* fall through */
3902 default:
3903 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3904 wl_event_source_timer_update(compositor->idle_source, 0);
3905 }
3906}
3907
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003908/** Powers down all attached output devices
3909 *
3910 * \param compositor The compositor instance
3911 *
3912 * Causes rendering to the outputs to cease, and no frame events to be
3913 * sent. Only powers down the outputs if the compositor is not already
3914 * in sleep mode.
3915 *
3916 * Stops the idle timer.
3917 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003918WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003919weston_compositor_sleep(struct weston_compositor *compositor)
3920{
3921 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3922 return;
3923
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003924 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003925 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3926 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3927}
3928
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003929/** Sets compositor to idle mode
3930 *
3931 * \param data The compositor instance
3932 *
3933 * This is called when the idle timer fires. Once the compositor is in
3934 * idle mode it requires a wake action (e.g. via
3935 * weston_compositor_wake()) to restore it. The compositor's
3936 * idle_signal will be triggered when the idle event occurs.
3937 *
3938 * Idleness can be inhibited by setting the compositor's idle_inhibit
3939 * property.
3940 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003941static int
3942idle_handler(void *data)
3943{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003944 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003945
3946 if (compositor->idle_inhibit)
3947 return 1;
3948
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003949 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003950 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003951
3952 return 1;
3953}
3954
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003955WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003956weston_plane_init(struct weston_plane *plane,
3957 struct weston_compositor *ec,
3958 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003959{
3960 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003961 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003962 plane->x = x;
3963 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003964 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003965
3966 /* Init the link so that the call to wl_list_remove() when releasing
3967 * the plane without ever stacking doesn't lead to a crash */
3968 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003969}
3970
3971WL_EXPORT void
3972weston_plane_release(struct weston_plane *plane)
3973{
Xiong Zhang97116532013-10-23 13:58:31 +08003974 struct weston_view *view;
3975
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003976 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003977 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003978
Xiong Zhang97116532013-10-23 13:58:31 +08003979 wl_list_for_each(view, &plane->compositor->view_list, link) {
3980 if (view->plane == plane)
3981 view->plane = NULL;
3982 }
3983
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003984 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003985}
3986
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003987WL_EXPORT void
3988weston_compositor_stack_plane(struct weston_compositor *ec,
3989 struct weston_plane *plane,
3990 struct weston_plane *above)
3991{
3992 if (above)
3993 wl_list_insert(above->link.prev, &plane->link);
3994 else
3995 wl_list_insert(&ec->plane_list, &plane->link);
3996}
3997
Casey Dahlin9074db52012-04-19 22:50:09 -04003998static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003999{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004000 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004001}
4002
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004003static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004004bind_output(struct wl_client *client,
4005 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004006{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004007 struct weston_output *output = data;
4008 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004009 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004010
Jason Ekstranda85118c2013-06-27 20:17:02 -05004011 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004012 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004013 if (resource == NULL) {
4014 wl_client_post_no_memory(client);
4015 return;
4016 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004017
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004018 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05004019 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004020
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004021 wl_output_send_geometry(resource,
4022 output->x,
4023 output->y,
4024 output->mm_width,
4025 output->mm_height,
4026 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004027 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004028 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004029 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004030 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004031 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004032
4033 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004034 wl_output_send_mode(resource,
4035 mode->flags,
4036 mode->width,
4037 mode->height,
4038 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004039 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004040
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004041 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004042 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004043}
4044
David Fort0de859e2016-05-27 23:22:57 +02004045/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004046static void
David Fort0de859e2016-05-27 23:22:57 +02004047weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4048 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004049{
4050 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004051 bool start_resizing = false;
4052
4053 if (!delta_width)
4054 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004055
4056 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004057 if (output == resized_output) {
4058 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004059 continue;
4060 }
4061
David Fort0de859e2016-05-27 23:22:57 +02004062 if (start_resizing) {
4063 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004064 output->dirty = 1;
4065 }
4066 }
4067}
4068
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004069WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004070weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004071{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004072 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004073 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004074
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004075 output->destroying = 1;
4076
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004077 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08004078 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004079 weston_view_assign_output(view);
4080 }
4081
Pekka Paalanen0513a952014-05-21 16:17:27 +03004082 wl_event_source_remove(output->repaint_timer);
4083
Pekka Paalanen133e4392014-09-23 22:08:46 -04004084 weston_presentation_feedback_discard_list(&output->feedback_list);
4085
David Fort0de859e2016-05-27 23:22:57 +02004086 weston_compositor_reflow_outputs(output->compositor, output, output->width);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004087 wl_list_remove(&output->link);
4088
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004089 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004090 wl_signal_emit(&output->destroy_signal, output);
4091
Richard Hughesafe690c2013-05-02 10:10:04 +01004092 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004093 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004094 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08004095 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004096
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004097 wl_resource_for_each(resource, &output->resource_list) {
4098 wl_resource_set_destructor(resource, NULL);
4099 }
4100
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004101 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004102}
4103
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004104WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004105weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004106{
Scott Moreau850ca422012-05-21 15:21:25 -06004107 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004108
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004109 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004110 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004111
Scott Moreauccbf29d2012-02-22 14:21:41 -07004112 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004113 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004114 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004115 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004116 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004117 weston_matrix_scale(&output->matrix, magnification,
4118 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004119 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004120
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004121 switch (output->transform) {
4122 case WL_OUTPUT_TRANSFORM_FLIPPED:
4123 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4124 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4125 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4126 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4127 weston_matrix_scale(&output->matrix, -1, 1, 1);
4128 break;
4129 }
4130
4131 switch (output->transform) {
4132 default:
4133 case WL_OUTPUT_TRANSFORM_NORMAL:
4134 case WL_OUTPUT_TRANSFORM_FLIPPED:
4135 break;
4136 case WL_OUTPUT_TRANSFORM_90:
4137 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4138 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4139 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4140 break;
4141 case WL_OUTPUT_TRANSFORM_180:
4142 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4143 weston_matrix_translate(&output->matrix,
4144 -output->width, -output->height, 0);
4145 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4146 break;
4147 case WL_OUTPUT_TRANSFORM_270:
4148 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4149 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4150 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4151 break;
4152 }
4153
4154 if (output->current_scale != 1)
4155 weston_matrix_scale(&output->matrix,
4156 output->current_scale,
4157 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004158
Scott Moreauccbf29d2012-02-22 14:21:41 -07004159 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004160
4161 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004162}
4163
Scott Moreau1bad5db2012-08-18 01:04:05 -06004164static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004165weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004166{
4167 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004168 output->native_scale = scale;
4169 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004170
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004171 convert_size_by_transform_scale(&output->width, &output->height,
4172 output->current_mode->width,
4173 output->current_mode->height,
4174 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004175}
4176
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004177static void
4178weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004179{
4180 output->x = x;
4181 output->y = y;
4182
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004183 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004184 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004185 output->width,
4186 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004187}
4188
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004189WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004190weston_output_move(struct weston_output *output, int x, int y)
4191{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004192 struct wl_resource *resource;
4193
4194 output->move_x = x - output->x;
4195 output->move_y = y - output->y;
4196
4197 if (output->move_x == 0 && output->move_y == 0)
4198 return;
4199
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004200 weston_output_init_geometry(output, x, y);
4201
4202 output->dirty = 1;
4203
4204 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004205 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004206
4207 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004208 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004209 wl_output_send_geometry(resource,
4210 output->x,
4211 output->y,
4212 output->mm_width,
4213 output->mm_height,
4214 output->subpixel,
4215 output->make,
4216 output->model,
4217 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004218
FORT David8a120692016-04-26 23:34:06 +02004219 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004220 wl_output_send_done(resource);
4221 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004222}
4223
Bryce Harrington3f650b82015-12-23 11:01:58 -08004224/** Initialize a weston_output object's parameters
4225 *
4226 * \param output The weston_output object to initialize
4227 * \param c The output's compositor
4228 * \param x x coordinate for the output in global coordinate space
4229 * \param y y coordinate for the output in global coordinate space
4230 * \param mm_width Physical width of the output as reported by the backend
4231 * \param mm_height Physical height of the output as reported by the backend
4232 * \param transform Rotation of the output
4233 * \param scale Native scaling factor for the output
4234 *
4235 * Sets up the transformation, zoom, and geometry of the output using
4236 * the input properties.
4237 *
4238 * Establishes a repaint timer for the output with the relevant display
4239 * object's event loop. See output_repaint_timer_handler().
4240 *
4241 * The output is assigned an ID. Weston can support up to 32 distinct
4242 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4243 * is referred to and used to find the first available ID number, and
4244 * then this ID is marked as used in output_id_pool.
4245 *
4246 * The output is also assigned a Wayland global with the wl_output
4247 * external interface.
4248 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004249WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004250weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004251 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004252 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004253{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004254 struct wl_event_loop *loop;
4255
Bryce Harrington18e45732015-12-23 20:53:53 -08004256 /* Verify we haven't reached the limit of 32 available output IDs */
4257 assert(ffs(~c->output_id_pool) > 0);
4258
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004259 output->compositor = c;
4260 output->x = x;
4261 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004262 output->mm_width = mm_width;
4263 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004264 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004265 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004266
Alexander Larsson0b135062013-05-28 16:23:36 +02004267 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004268 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004269
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004270 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004271 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004272
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004273 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004274 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004275 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004276 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004277 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004278 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004279
Pekka Paalanen0513a952014-05-21 16:17:27 +03004280 loop = wl_display_get_event_loop(c->wl_display);
4281 output->repaint_timer = wl_event_loop_add_timer(loop,
4282 output_repaint_timer_handler, output);
4283
Bryce Harrington3f650b82015-12-23 11:01:58 -08004284 /* Invert the output id pool and look for the lowest numbered
4285 * switch (the least significant bit). Take that bit's position
4286 * as our ID, and mark it used in the compositor's output_id_pool.
4287 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004288 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004289 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004290
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004291 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004292 wl_global_create(c->wl_display, &wl_output_interface, 2,
4293 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004294}
4295
4296/** Adds an output to the compositor's output list and
4297 * send the compositor's output_created signal.
4298 *
4299 * \param compositor The compositor instance.
4300 * \param output The output to be added.
4301 */
4302WL_EXPORT void
4303weston_compositor_add_output(struct weston_compositor *compositor,
4304 struct weston_output *output)
4305{
4306 wl_list_insert(compositor->output_list.prev, &output->link);
4307 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004308}
4309
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004310WL_EXPORT void
4311weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004312 double device_x, double device_y,
4313 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004314{
Derek Foreman0f679412014-10-02 13:41:17 -05004315 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004316 device_x,
4317 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004318 0.0,
4319 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004320
Derek Foreman67a18b92015-03-24 11:36:14 -05004321 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004322
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004323 *x = p.f[0] / p.f[3];
4324 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004325}
4326
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004327static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004328destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004329{
Jonny Lamb74130762013-11-26 18:19:46 +01004330 struct weston_surface *surface =
4331 wl_resource_get_user_data(resource);
4332
Pekka Paalanen4826f872016-04-22 14:14:38 +03004333 if (!surface)
4334 return;
4335
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004336 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004337 surface->pending.buffer_viewport.buffer.src_width =
4338 wl_fixed_from_int(-1);
4339 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004340 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004341}
4342
4343static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004344viewport_destroy(struct wl_client *client,
4345 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004346{
4347 wl_resource_destroy(resource);
4348}
4349
4350static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004351viewport_set_source(struct wl_client *client,
4352 struct wl_resource *resource,
4353 wl_fixed_t src_x,
4354 wl_fixed_t src_y,
4355 wl_fixed_t src_width,
4356 wl_fixed_t src_height)
4357{
4358 struct weston_surface *surface =
4359 wl_resource_get_user_data(resource);
4360
Pekka Paalanen4826f872016-04-22 14:14:38 +03004361 if (!surface) {
4362 wl_resource_post_error(resource,
4363 WP_VIEWPORT_ERROR_NO_SURFACE,
4364 "wl_surface for this viewport is no longer exists");
4365 return;
4366 }
4367
4368 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004369
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004370 if (src_width == wl_fixed_from_int(-1) &&
4371 src_height == wl_fixed_from_int(-1)) {
4372 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004373 surface->pending.buffer_viewport.buffer.src_width =
4374 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004375 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004376 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004377 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004378
4379 if (src_width <= 0 || src_height <= 0) {
4380 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004381 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004382 "source size must be positive (%fx%f)",
4383 wl_fixed_to_double(src_width),
4384 wl_fixed_to_double(src_height));
4385 return;
4386 }
4387
4388 surface->pending.buffer_viewport.buffer.src_x = src_x;
4389 surface->pending.buffer_viewport.buffer.src_y = src_y;
4390 surface->pending.buffer_viewport.buffer.src_width = src_width;
4391 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004392 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004393}
4394
4395static void
4396viewport_set_destination(struct wl_client *client,
4397 struct wl_resource *resource,
4398 int32_t dst_width,
4399 int32_t dst_height)
4400{
4401 struct weston_surface *surface =
4402 wl_resource_get_user_data(resource);
4403
Pekka Paalanen4826f872016-04-22 14:14:38 +03004404 if (!surface) {
4405 wl_resource_post_error(resource,
4406 WP_VIEWPORT_ERROR_NO_SURFACE,
4407 "wl_surface for this viewport no longer exists");
4408 return;
4409 }
4410
4411 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004412
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004413 if (dst_width == -1 && dst_height == -1) {
4414 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004415 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004416 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004417 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004418 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004419
4420 if (dst_width <= 0 || dst_height <= 0) {
4421 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004422 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004423 "destination size must be positive (%dx%d)",
4424 dst_width, dst_height);
4425 return;
4426 }
4427
4428 surface->pending.buffer_viewport.surface.width = dst_width;
4429 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004430 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004431}
4432
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004433static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004434 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004435 viewport_set_source,
4436 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004437};
4438
4439static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004440viewporter_destroy(struct wl_client *client,
4441 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004442{
4443 wl_resource_destroy(resource);
4444}
4445
4446static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004447viewporter_get_viewport(struct wl_client *client,
4448 struct wl_resource *viewporter,
4449 uint32_t id,
4450 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004451{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004452 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004453 struct weston_surface *surface =
4454 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004455 struct wl_resource *resource;
4456
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004457 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004458 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004459 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004460 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004461 return;
4462 }
4463
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004464 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004465 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004466 if (resource == NULL) {
4467 wl_client_post_no_memory(client);
4468 return;
4469 }
4470
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004471 wl_resource_set_implementation(resource, &viewport_interface,
4472 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004473
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004474 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004475}
4476
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004477static const struct wp_viewporter_interface viewporter_interface = {
4478 viewporter_destroy,
4479 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004480};
4481
4482static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004483bind_viewporter(struct wl_client *client,
4484 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004485{
4486 struct wl_resource *resource;
4487
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004488 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004489 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004490 if (resource == NULL) {
4491 wl_client_post_no_memory(client);
4492 return;
4493 }
4494
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004495 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004496 NULL, NULL);
4497}
4498
4499static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004500destroy_presentation_feedback(struct wl_resource *feedback_resource)
4501{
4502 struct weston_presentation_feedback *feedback;
4503
4504 feedback = wl_resource_get_user_data(feedback_resource);
4505
4506 wl_list_remove(&feedback->link);
4507 free(feedback);
4508}
4509
4510static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004511presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4512{
4513 wl_resource_destroy(resource);
4514}
4515
4516static void
4517presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004518 struct wl_resource *presentation_resource,
4519 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004520 uint32_t callback)
4521{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004522 struct weston_surface *surface;
4523 struct weston_presentation_feedback *feedback;
4524
4525 surface = wl_resource_get_user_data(surface_resource);
4526
Bryce Harringtonde16d892014-11-20 22:21:57 -08004527 feedback = zalloc(sizeof *feedback);
4528 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004529 goto err_calloc;
4530
4531 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004532 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004533 1, callback);
4534 if (!feedback->resource)
4535 goto err_create;
4536
4537 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4538 destroy_presentation_feedback);
4539
4540 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4541
4542 return;
4543
4544err_create:
4545 free(feedback);
4546
4547err_calloc:
4548 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004549}
4550
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004551static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004552 presentation_destroy,
4553 presentation_feedback
4554};
4555
4556static void
4557bind_presentation(struct wl_client *client,
4558 void *data, uint32_t version, uint32_t id)
4559{
4560 struct weston_compositor *compositor = data;
4561 struct wl_resource *resource;
4562
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004563 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004564 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004565 if (resource == NULL) {
4566 wl_client_post_no_memory(client);
4567 return;
4568 }
4569
4570 wl_resource_set_implementation(resource, &presentation_implementation,
4571 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004572 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004573}
4574
4575static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004576compositor_bind(struct wl_client *client,
4577 void *data, uint32_t version, uint32_t id)
4578{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004579 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004580 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004581
Jason Ekstranda85118c2013-06-27 20:17:02 -05004582 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004583 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004584 if (resource == NULL) {
4585 wl_client_post_no_memory(client);
4586 return;
4587 }
4588
4589 wl_resource_set_implementation(resource, &compositor_interface,
4590 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004591}
4592
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004593WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004594weston_environment_get_fd(const char *env)
4595{
4596 char *e, *end;
4597 int fd, flags;
4598
4599 e = getenv(env);
4600 if (!e)
4601 return -1;
4602 fd = strtol(e, &end, 0);
4603 if (*end != '\0')
4604 return -1;
4605
4606 flags = fcntl(fd, F_GETFD);
4607 if (flags == -1)
4608 return -1;
4609
4610 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4611 unsetenv(env);
4612
4613 return fd;
4614}
4615
Pekka Paalanenb5026542014-11-12 15:09:24 +02004616static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004617timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004618 uint32_t key, void *data)
4619{
4620 struct weston_compositor *compositor = data;
4621
4622 if (weston_timeline_enabled_)
4623 weston_timeline_close();
4624 else
4625 weston_timeline_open(compositor);
4626}
4627
Giulio Camuffo459137b2014-10-11 23:56:24 +03004628/** Create the compositor.
4629 *
4630 * This functions creates and initializes a compositor instance.
4631 *
4632 * \param display The Wayland display to be used.
4633 * \param user_data A pointer to an object that can later be retrieved
4634 * using the \ref weston_compositor_get_user_data function.
4635 * \return The compositor instance on success or NULL on failure.
4636 */
4637WL_EXPORT struct weston_compositor *
4638weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004639{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004640 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004641 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004642
Giulio Camuffo459137b2014-10-11 23:56:24 +03004643 ec = zalloc(sizeof *ec);
4644 if (!ec)
4645 return NULL;
4646
4647 ec->wl_display = display;
4648 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004649 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004650 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004651 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004652 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004653 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004654 wl_signal_init(&ec->idle_signal);
4655 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004656 wl_signal_init(&ec->show_input_panel_signal);
4657 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004658 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004659 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004660 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004661 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004662 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004663 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004664 wl_signal_init(&ec->session_signal);
4665 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004666
Casey Dahlin58ba1372012-04-19 22:50:08 -04004667 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004668 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004669
Derek Foreman152254b2015-11-26 14:17:48 -06004670 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004671 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004672 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004673
Giulio Camuffo954f1832014-10-11 18:27:30 +03004674 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004675 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004676 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004677
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004678 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004679 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004680 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004681
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004682 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004683 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004684 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004685
Jason Ekstranda7af7042013-10-12 22:38:11 -05004686 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004687 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004688 wl_list_init(&ec->layer_list);
4689 wl_list_init(&ec->seat_list);
4690 wl_list_init(&ec->output_list);
4691 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004692 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004693 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004694 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004695 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004696 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004697
Xiong Zhang97116532013-10-23 13:58:31 +08004698 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004699 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004700
Giulio Camuffo459137b2014-10-11 23:56:24 +03004701 wl_data_device_manager_init(ec->wl_display);
4702
4703 wl_display_init_shm(ec->wl_display);
4704
4705 loop = wl_display_get_event_loop(ec->wl_display);
4706 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004707
Giulio Camuffo459137b2014-10-11 23:56:24 +03004708 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4709 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4710
4711 weston_compositor_add_debug_binding(ec, KEY_T,
4712 timeline_key_binding_handler, ec);
4713
Giulio Camuffo459137b2014-10-11 23:56:24 +03004714 return ec;
4715
4716fail:
4717 free(ec);
4718 return NULL;
4719}
4720
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004721WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004722weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004723{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004724 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004725
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004726 wl_event_source_remove(ec->idle_source);
4727
Matt Roper361d2ad2011-08-29 13:52:23 -07004728 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004729 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004730 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004731
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004732 if (ec->renderer)
4733 ec->renderer->destroy(ec);
4734
Daniel Stone325fc2d2012-05-30 16:31:58 +01004735 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004736 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004737 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004738 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004739 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004740 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004741
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004742 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004743}
4744
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004745WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004746weston_compositor_exit_with_code(struct weston_compositor *compositor,
4747 int exit_code)
4748{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004749 if (compositor->exit_code == EXIT_SUCCESS)
4750 compositor->exit_code = exit_code;
4751
Giulio Camuffo459137b2014-10-11 23:56:24 +03004752 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004753}
4754
4755WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004756weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4757 const struct weston_pointer_grab_interface *interface)
4758{
4759 struct weston_seat *seat;
4760
4761 ec->default_pointer_grab = interface;
4762 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004763 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4764
4765 if (pointer)
4766 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004767 }
4768}
4769
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004770WL_EXPORT int
4771weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4772 clockid_t clk_id)
4773{
4774 struct timespec ts;
4775
4776 if (clock_gettime(clk_id, &ts) < 0)
4777 return -1;
4778
4779 compositor->presentation_clock = clk_id;
4780
4781 return 0;
4782}
4783
4784/*
4785 * For choosing the software clock, when the display hardware or API
4786 * does not expose a compatible presentation timestamp.
4787 */
4788WL_EXPORT int
4789weston_compositor_set_presentation_clock_software(
4790 struct weston_compositor *compositor)
4791{
4792 /* In order of preference */
4793 static const clockid_t clocks[] = {
4794 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4795 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4796 CLOCK_MONOTONIC, /* no jumps, may crawl */
4797 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4798 CLOCK_REALTIME /* may jump and crawl */
4799 };
4800 unsigned i;
4801
4802 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4803 if (weston_compositor_set_presentation_clock(compositor,
4804 clocks[i]) == 0)
4805 return 0;
4806
4807 weston_log("Error: no suitable presentation clock available.\n");
4808
4809 return -1;
4810}
4811
Pekka Paalanen662f3842015-03-18 12:17:26 +02004812/** Read the current time from the Presentation clock
4813 *
4814 * \param compositor
4815 * \param ts[out] The current time.
4816 *
4817 * \note Reading the current time in user space is always imprecise to some
4818 * degree.
4819 *
4820 * This function is never meant to fail. If reading the clock does fail,
4821 * an error message is logged and a zero time is returned. Callers are not
4822 * supposed to detect or react to failures.
4823 */
4824WL_EXPORT void
4825weston_compositor_read_presentation_clock(
4826 const struct weston_compositor *compositor,
4827 struct timespec *ts)
4828{
4829 static bool warned;
4830 int ret;
4831
4832 ret = clock_gettime(compositor->presentation_clock, ts);
4833 if (ret < 0) {
4834 ts->tv_sec = 0;
4835 ts->tv_nsec = 0;
4836
4837 if (!warned)
4838 weston_log("Error: failure to read "
4839 "the presentation clock %#x: '%m' (%d)\n",
4840 compositor->presentation_clock, errno);
4841 warned = true;
4842 }
4843}
4844
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004845/** Import dmabuf buffer into current renderer
4846 *
4847 * \param compositor
4848 * \param buffer the dmabuf buffer to import
4849 * \return true on usable buffers, false otherwise
4850 *
4851 * This function tests that the linux_dmabuf_buffer is usable
4852 * for the current renderer. Returns false on unusable buffers. Usually
4853 * usability is tested by importing the dmabufs for composition.
4854 *
4855 * This hook is also used for detecting if the renderer supports
4856 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4857 * supported.
4858 * */
4859WL_EXPORT bool
4860weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4861 struct linux_dmabuf_buffer *buffer)
4862{
4863 struct weston_renderer *renderer;
4864
4865 renderer = compositor->renderer;
4866
4867 if (renderer->import_dmabuf == NULL)
4868 return false;
4869
4870 return renderer->import_dmabuf(compositor, buffer);
4871}
4872
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004873WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004874weston_version(int *major, int *minor, int *micro)
4875{
4876 *major = WESTON_VERSION_MAJOR;
4877 *minor = WESTON_VERSION_MINOR;
4878 *micro = WESTON_VERSION_MICRO;
4879}
4880
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004881WL_EXPORT void *
4882weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004883{
Derek Foreman3f86e502015-06-08 11:46:54 -05004884 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004885 char path[PATH_MAX];
4886 void *module, *init;
4887
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004888 if (name == NULL)
4889 return NULL;
4890
Derek Foreman3f86e502015-06-08 11:46:54 -05004891 if (name[0] != '/') {
4892 if (builddir)
4893 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4894 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03004895 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004896 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004897 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004898 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004899
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004900 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4901 if (module) {
4902 weston_log("Module '%s' already loaded\n", path);
4903 dlclose(module);
4904 return NULL;
4905 }
4906
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004907 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004908 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004909 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004910 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004911 return NULL;
4912 }
4913
4914 init = dlsym(module, entrypoint);
4915 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004916 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004917 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004918 return NULL;
4919 }
4920
4921 return init;
4922}
4923
Giulio Camuffo459137b2014-10-11 23:56:24 +03004924
4925/** Destroys the compositor.
4926 *
4927 * This function cleans up the compositor state and destroys it.
4928 *
4929 * \param compositor The compositor to be destroyed.
4930 */
4931WL_EXPORT void
4932weston_compositor_destroy(struct weston_compositor *compositor)
4933{
4934 /* prevent further rendering while shutting down */
4935 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4936
4937 wl_signal_emit(&compositor->destroy_signal, compositor);
4938
4939 weston_compositor_xkb_destroy(compositor);
4940
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004941 if (compositor->backend)
4942 compositor->backend->destroy(compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004943 free(compositor);
4944}
4945
4946/** Instruct the compositor to exit.
4947 *
4948 * This functions does not directly destroy the compositor object, it merely
4949 * command it to start the tear down process. It is not guaranteed that the
4950 * tear down will happen immediately.
4951 *
4952 * \param compositor The compositor to tear down.
4953 */
4954WL_EXPORT void
4955weston_compositor_exit(struct weston_compositor *compositor)
4956{
4957 compositor->exit(compositor);
4958}
4959
4960/** Return the user data stored in the compositor.
4961 *
4962 * This function returns the user data pointer set with user_data parameter
4963 * to the \ref weston_compositor_create function.
4964 */
4965WL_EXPORT void *
4966weston_compositor_get_user_data(struct weston_compositor *compositor)
4967{
4968 return compositor->user_data;
4969}
Pekka Paalanendd186732016-06-03 14:49:54 +03004970
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004971static const char * const backend_map[] = {
4972 [WESTON_BACKEND_DRM] = "drm-backend.so",
4973 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
4974 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
4975 [WESTON_BACKEND_RDP] = "rdp-backend.so",
4976 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
4977 [WESTON_BACKEND_X11] = "x11-backend.so",
4978};
4979
Pekka Paalanendd186732016-06-03 14:49:54 +03004980/** Load a backend into a weston_compositor
4981 *
4982 * A backend must be loaded to make a weston_compositor work. A backend
4983 * provides input and output capabilities, and determines the renderer to use.
4984 *
4985 * \param compositor A compositor that has not had a backend loaded yet.
4986 * \param backend Name of the backend file.
4987 * \param config_base A pointer to a backend-specific configuration
4988 * structure's 'base' member.
4989 *
4990 * \return 0 on success, or -1 on error.
4991 */
4992WL_EXPORT int
4993weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004994 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03004995 struct weston_backend_config *config_base)
4996{
4997 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03004998 struct weston_backend_config *config_base);
4999
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005000 if (backend < 0 || backend >= ARRAY_LENGTH(backend_map))
5001 return -1;
5002
5003 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005004 if (!backend_init)
5005 return -1;
5006
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005007 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005008}