blob: 44126ddb29b134913f865decd7e7ca54a47eb778 [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"
Jonny Lamb8ae35902013-11-26 18:19:45 +010057#include "scaler-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
146WL_EXPORT int
147weston_output_mode_set_native(struct weston_output *output,
148 struct weston_mode *mode,
149 int32_t scale)
150{
151 int ret;
152 int mode_changed = 0, scale_changed = 0;
153
154 if (!output->switch_mode)
155 return -1;
156
157 if (!output->original_mode) {
158 mode_changed = 1;
159 ret = output->switch_mode(output, mode);
160 if (ret < 0)
161 return ret;
162 if (output->current_scale != scale) {
163 scale_changed = 1;
164 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200165 }
166 }
167
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600168 output->native_mode = mode;
169 output->native_scale = scale;
170
171 weston_mode_switch_finish(output, mode_changed, scale_changed);
172
173 return 0;
174}
175
176WL_EXPORT int
177weston_output_mode_switch_to_native(struct weston_output *output)
178{
179 int ret;
180 int mode_changed = 0, scale_changed = 0;
181
182 if (!output->switch_mode)
183 return -1;
184
185 if (!output->original_mode) {
186 weston_log("already in the native mode\n");
187 return -1;
188 }
189 /* the non fullscreen clients haven't seen a mode set since we
190 * switched into a temporary, so we need to notify them if the
191 * mode at that time is different from the native mode now.
192 */
193 mode_changed = (output->original_mode != output->native_mode);
194 scale_changed = (output->original_scale != output->native_scale);
195
196 ret = output->switch_mode(output, output->native_mode);
197 if (ret < 0)
198 return ret;
199
200 output->current_scale = output->native_scale;
201
202 output->original_mode = NULL;
203 output->original_scale = 0;
204
205 weston_mode_switch_finish(output, mode_changed, scale_changed);
206
207 return 0;
208}
209
210WL_EXPORT int
211weston_output_mode_switch_to_temporary(struct weston_output *output,
212 struct weston_mode *mode,
213 int32_t scale)
214{
215 int ret;
216
217 if (!output->switch_mode)
218 return -1;
219
220 /* original_mode is the last mode non full screen clients have seen,
221 * so we shouldn't change it if we already have one set.
222 */
223 if (!output->original_mode) {
224 output->original_mode = output->native_mode;
225 output->original_scale = output->native_scale;
226 }
227 ret = output->switch_mode(output, mode);
228 if (ret < 0)
229 return ret;
230
231 output->current_scale = scale;
232
233 weston_mode_switch_finish(output, 0, 0);
234
235 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800236}
237
Benjamin Franzke06286262011-05-06 19:12:33 +0200238static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300239region_init_infinite(pixman_region32_t *region)
240{
241 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
242 UINT32_MAX, UINT32_MAX);
243}
244
Pekka Paalanene67858b2013-04-25 13:57:42 +0300245static struct weston_subsurface *
246weston_surface_to_subsurface(struct weston_surface *surface);
247
Jason Ekstranda7af7042013-10-12 22:38:11 -0500248WL_EXPORT struct weston_view *
249weston_view_create(struct weston_surface *surface)
250{
251 struct weston_view *view;
252
Bryce Harringtonde16d892014-11-20 22:21:57 -0800253 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500254 if (view == NULL)
255 return NULL;
256
257 view->surface = surface;
258
Jason Ekstranda7af7042013-10-12 22:38:11 -0500259 /* Assign to surface */
260 wl_list_insert(&surface->views, &view->surface_link);
261
262 wl_signal_init(&view->destroy_signal);
263 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300264 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500265
Jason Ekstranda7af7042013-10-12 22:38:11 -0500266 pixman_region32_init(&view->clip);
267
268 view->alpha = 1.0;
269 pixman_region32_init(&view->transform.opaque);
270
271 wl_list_init(&view->geometry.transformation_list);
272 wl_list_insert(&view->geometry.transformation_list,
273 &view->transform.position.link);
274 weston_matrix_init(&view->transform.position.matrix);
275 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200276 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500277 pixman_region32_init(&view->transform.boundingbox);
278 view->transform.dirty = 1;
279
Jason Ekstranda7af7042013-10-12 22:38:11 -0500280 return view;
281}
282
Jason Ekstrand108865d2014-06-26 10:04:49 -0700283struct weston_frame_callback {
284 struct wl_resource *resource;
285 struct wl_list link;
286};
287
Pekka Paalanen133e4392014-09-23 22:08:46 -0400288struct weston_presentation_feedback {
289 struct wl_resource *resource;
290
291 /* XXX: could use just wl_resource_get_link() instead */
292 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200293
294 /* The per-surface feedback flags */
295 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400296};
297
298static void
299weston_presentation_feedback_discard(
300 struct weston_presentation_feedback *feedback)
301{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200302 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400303 wl_resource_destroy(feedback->resource);
304}
305
306static void
307weston_presentation_feedback_discard_list(struct wl_list *list)
308{
309 struct weston_presentation_feedback *feedback, *tmp;
310
311 wl_list_for_each_safe(feedback, tmp, list, link)
312 weston_presentation_feedback_discard(feedback);
313}
314
315static void
316weston_presentation_feedback_present(
317 struct weston_presentation_feedback *feedback,
318 struct weston_output *output,
319 uint32_t refresh_nsec,
320 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200321 uint64_t seq,
322 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400323{
324 struct wl_client *client = wl_resource_get_client(feedback->resource);
325 struct wl_resource *o;
326 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400327
328 wl_resource_for_each(o, &output->resource_list) {
329 if (wl_resource_get_client(o) != client)
330 continue;
331
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200332 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400333 }
334
335 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200336 wp_presentation_feedback_send_presented(feedback->resource,
337 secs >> 32, secs & 0xffffffff,
338 ts->tv_nsec,
339 refresh_nsec,
340 seq >> 32, seq & 0xffffffff,
341 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400342 wl_resource_destroy(feedback->resource);
343}
344
345static void
346weston_presentation_feedback_present_list(struct wl_list *list,
347 struct weston_output *output,
348 uint32_t refresh_nsec,
349 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200350 uint64_t seq,
351 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400352{
353 struct weston_presentation_feedback *feedback, *tmp;
354
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200355 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200356 wl_list_empty(list));
357
Pekka Paalanen133e4392014-09-23 22:08:46 -0400358 wl_list_for_each_safe(feedback, tmp, list, link)
359 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200360 refresh_nsec, ts, seq,
361 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400362}
363
Jason Ekstrand7b982072014-05-20 14:33:03 -0500364static void
365surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
366{
367 struct weston_surface_state *state =
368 container_of(listener, struct weston_surface_state,
369 buffer_destroy_listener);
370
371 state->buffer = NULL;
372}
373
374static void
375weston_surface_state_init(struct weston_surface_state *state)
376{
377 state->newly_attached = 0;
378 state->buffer = NULL;
379 state->buffer_destroy_listener.notify =
380 surface_state_handle_buffer_destroy;
381 state->sx = 0;
382 state->sy = 0;
383
Derek Foreman152254b2015-11-26 14:17:48 -0600384 pixman_region32_init(&state->damage_surface);
385 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500386 pixman_region32_init(&state->opaque);
387 region_init_infinite(&state->input);
388
389 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400390 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500391
392 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
393 state->buffer_viewport.buffer.scale = 1;
394 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
395 state->buffer_viewport.surface.width = -1;
396 state->buffer_viewport.changed = 0;
397}
398
399static void
400weston_surface_state_fini(struct weston_surface_state *state)
401{
402 struct weston_frame_callback *cb, *next;
403
404 wl_list_for_each_safe(cb, next,
405 &state->frame_callback_list, link)
406 wl_resource_destroy(cb->resource);
407
Pekka Paalanen133e4392014-09-23 22:08:46 -0400408 weston_presentation_feedback_discard_list(&state->feedback_list);
409
Jason Ekstrand7b982072014-05-20 14:33:03 -0500410 pixman_region32_fini(&state->input);
411 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600412 pixman_region32_fini(&state->damage_surface);
413 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500414
415 if (state->buffer)
416 wl_list_remove(&state->buffer_destroy_listener.link);
417 state->buffer = NULL;
418}
419
420static void
421weston_surface_state_set_buffer(struct weston_surface_state *state,
422 struct weston_buffer *buffer)
423{
424 if (state->buffer == buffer)
425 return;
426
427 if (state->buffer)
428 wl_list_remove(&state->buffer_destroy_listener.link);
429 state->buffer = buffer;
430 if (state->buffer)
431 wl_signal_add(&state->buffer->destroy_signal,
432 &state->buffer_destroy_listener);
433}
434
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500435WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500436weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500437{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500438 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400439
Bryce Harringtonde16d892014-11-20 22:21:57 -0800440 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400441 if (surface == NULL)
442 return NULL;
443
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500444 wl_signal_init(&surface->destroy_signal);
445
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500446 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200447 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400448
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200449 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
450 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200451 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
452 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500453
454 weston_surface_state_init(&surface->pending);
455
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400456 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500457 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300458 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400459
Jason Ekstranda7af7042013-10-12 22:38:11 -0500460 wl_list_init(&surface->views);
461
462 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400463 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500464
Pekka Paalanene67858b2013-04-25 13:57:42 +0300465 wl_list_init(&surface->subsurface_list);
466 wl_list_init(&surface->subsurface_list_pending);
467
Jason Ekstrand1e059042014-10-16 10:55:19 -0500468 weston_matrix_init(&surface->buffer_to_surface_matrix);
469 weston_matrix_init(&surface->surface_to_buffer_matrix);
470
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400471 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500472}
473
Alex Wu8811bf92012-02-28 18:07:54 +0800474WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500475weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200476 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500477{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100478 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500479}
480
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400481WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500482weston_view_to_global_float(struct weston_view *view,
483 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200484{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500485 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200486 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
487
Jason Ekstranda7af7042013-10-12 22:38:11 -0500488 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200489
490 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200491 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700492 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200493 v.f[3]);
494 *x = 0;
495 *y = 0;
496 return;
497 }
498
499 *x = v.f[0] / v.f[3];
500 *y = v.f[1] / v.f[3];
501 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500502 *x = sx + view->geometry.x;
503 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200504 }
505}
506
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500507WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200508weston_transformed_coord(int width, int height,
509 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200510 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200511 float sx, float sy, float *bx, float *by)
512{
513 switch (transform) {
514 case WL_OUTPUT_TRANSFORM_NORMAL:
515 default:
516 *bx = sx;
517 *by = sy;
518 break;
519 case WL_OUTPUT_TRANSFORM_FLIPPED:
520 *bx = width - sx;
521 *by = sy;
522 break;
523 case WL_OUTPUT_TRANSFORM_90:
524 *bx = height - sy;
525 *by = sx;
526 break;
527 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
528 *bx = height - sy;
529 *by = width - sx;
530 break;
531 case WL_OUTPUT_TRANSFORM_180:
532 *bx = width - sx;
533 *by = height - sy;
534 break;
535 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
536 *bx = sx;
537 *by = height - sy;
538 break;
539 case WL_OUTPUT_TRANSFORM_270:
540 *bx = sy;
541 *by = width - sx;
542 break;
543 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
544 *bx = sy;
545 *by = sx;
546 break;
547 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200548
549 *bx *= scale;
550 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200551}
552
553WL_EXPORT pixman_box32_t
554weston_transformed_rect(int width, int height,
555 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200556 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200557 pixman_box32_t rect)
558{
559 float x1, x2, y1, y2;
560
561 pixman_box32_t ret;
562
Alexander Larsson4ea95522013-05-22 14:41:37 +0200563 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200564 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200565 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200566 rect.x2, rect.y2, &x2, &y2);
567
568 if (x1 <= x2) {
569 ret.x1 = x1;
570 ret.x2 = x2;
571 } else {
572 ret.x1 = x2;
573 ret.x2 = x1;
574 }
575
576 if (y1 <= y2) {
577 ret.y1 = y1;
578 ret.y2 = y2;
579 } else {
580 ret.y1 = y2;
581 ret.y2 = y1;
582 }
583
584 return ret;
585}
586
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600587/** Transform a region by a matrix, restricted to axis-aligned transformations
588 *
589 * Warning: This function does not work for projective, affine, or matrices
590 * that encode arbitrary rotations. Only 90-degree step rotations are
591 * supported.
592 */
593WL_EXPORT void
594weston_matrix_transform_region(pixman_region32_t *dest,
595 struct weston_matrix *matrix,
596 pixman_region32_t *src)
597{
598 pixman_box32_t *src_rects, *dest_rects;
599 int nrects, i;
600
601 src_rects = pixman_region32_rectangles(src, &nrects);
602 dest_rects = malloc(nrects * sizeof(*dest_rects));
603 if (!dest_rects)
604 return;
605
606 for (i = 0; i < nrects; i++) {
607 struct weston_vector vec1 = {{
608 src_rects[i].x1, src_rects[i].y1, 0, 1
609 }};
610 weston_matrix_transform(matrix, &vec1);
611 vec1.f[0] /= vec1.f[3];
612 vec1.f[1] /= vec1.f[3];
613
614 struct weston_vector vec2 = {{
615 src_rects[i].x2, src_rects[i].y2, 0, 1
616 }};
617 weston_matrix_transform(matrix, &vec2);
618 vec2.f[0] /= vec2.f[3];
619 vec2.f[1] /= vec2.f[3];
620
621 if (vec1.f[0] < vec2.f[0]) {
622 dest_rects[i].x1 = floor(vec1.f[0]);
623 dest_rects[i].x2 = ceil(vec2.f[0]);
624 } else {
625 dest_rects[i].x1 = floor(vec2.f[0]);
626 dest_rects[i].x2 = ceil(vec1.f[0]);
627 }
628
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600629 if (vec1.f[1] < vec2.f[1]) {
630 dest_rects[i].y1 = floor(vec1.f[1]);
631 dest_rects[i].y2 = ceil(vec2.f[1]);
632 } else {
633 dest_rects[i].y1 = floor(vec2.f[1]);
634 dest_rects[i].y2 = ceil(vec1.f[1]);
635 }
636 }
637
638 pixman_region32_clear(dest);
639 pixman_region32_init_rects(dest, dest_rects, nrects);
640 free(dest_rects);
641}
642
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200643WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500644weston_transformed_region(int width, int height,
645 enum wl_output_transform transform,
646 int32_t scale,
647 pixman_region32_t *src, pixman_region32_t *dest)
648{
649 pixman_box32_t *src_rects, *dest_rects;
650 int nrects, i;
651
652 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
653 if (src != dest)
654 pixman_region32_copy(dest, src);
655 return;
656 }
657
658 src_rects = pixman_region32_rectangles(src, &nrects);
659 dest_rects = malloc(nrects * sizeof(*dest_rects));
660 if (!dest_rects)
661 return;
662
663 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
664 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
665 } else {
666 for (i = 0; i < nrects; i++) {
667 switch (transform) {
668 default:
669 case WL_OUTPUT_TRANSFORM_NORMAL:
670 dest_rects[i].x1 = src_rects[i].x1;
671 dest_rects[i].y1 = src_rects[i].y1;
672 dest_rects[i].x2 = src_rects[i].x2;
673 dest_rects[i].y2 = src_rects[i].y2;
674 break;
675 case WL_OUTPUT_TRANSFORM_90:
676 dest_rects[i].x1 = height - src_rects[i].y2;
677 dest_rects[i].y1 = src_rects[i].x1;
678 dest_rects[i].x2 = height - src_rects[i].y1;
679 dest_rects[i].y2 = src_rects[i].x2;
680 break;
681 case WL_OUTPUT_TRANSFORM_180:
682 dest_rects[i].x1 = width - src_rects[i].x2;
683 dest_rects[i].y1 = height - src_rects[i].y2;
684 dest_rects[i].x2 = width - src_rects[i].x1;
685 dest_rects[i].y2 = height - src_rects[i].y1;
686 break;
687 case WL_OUTPUT_TRANSFORM_270:
688 dest_rects[i].x1 = src_rects[i].y1;
689 dest_rects[i].y1 = width - src_rects[i].x2;
690 dest_rects[i].x2 = src_rects[i].y2;
691 dest_rects[i].y2 = width - src_rects[i].x1;
692 break;
693 case WL_OUTPUT_TRANSFORM_FLIPPED:
694 dest_rects[i].x1 = width - src_rects[i].x2;
695 dest_rects[i].y1 = src_rects[i].y1;
696 dest_rects[i].x2 = width - src_rects[i].x1;
697 dest_rects[i].y2 = src_rects[i].y2;
698 break;
699 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
700 dest_rects[i].x1 = height - src_rects[i].y2;
701 dest_rects[i].y1 = width - src_rects[i].x2;
702 dest_rects[i].x2 = height - src_rects[i].y1;
703 dest_rects[i].y2 = width - src_rects[i].x1;
704 break;
705 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
706 dest_rects[i].x1 = src_rects[i].x1;
707 dest_rects[i].y1 = height - src_rects[i].y2;
708 dest_rects[i].x2 = src_rects[i].x2;
709 dest_rects[i].y2 = height - src_rects[i].y1;
710 break;
711 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
712 dest_rects[i].x1 = src_rects[i].y1;
713 dest_rects[i].y1 = src_rects[i].x1;
714 dest_rects[i].x2 = src_rects[i].y2;
715 dest_rects[i].y2 = src_rects[i].x2;
716 break;
717 }
718 }
719 }
720
721 if (scale != 1) {
722 for (i = 0; i < nrects; i++) {
723 dest_rects[i].x1 *= scale;
724 dest_rects[i].x2 *= scale;
725 dest_rects[i].y1 *= scale;
726 dest_rects[i].y2 *= scale;
727 }
728 }
729
730 pixman_region32_clear(dest);
731 pixman_region32_init_rects(dest, dest_rects, nrects);
732 free(dest_rects);
733}
734
Jonny Lamb74130762013-11-26 18:19:46 +0100735static void
736scaler_surface_to_buffer(struct weston_surface *surface,
737 float sx, float sy, float *bx, float *by)
738{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200739 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200740 double src_width, src_height;
741 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200742
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200743 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
744 if (vp->surface.width == -1) {
745 *bx = sx;
746 *by = sy;
747 return;
748 }
Jonny Lamb74130762013-11-26 18:19:46 +0100749
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200750 src_x = 0.0;
751 src_y = 0.0;
752 src_width = surface->width_from_buffer;
753 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100754 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200755 src_x = wl_fixed_to_double(vp->buffer.src_x);
756 src_y = wl_fixed_to_double(vp->buffer.src_y);
757 src_width = wl_fixed_to_double(vp->buffer.src_width);
758 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100759 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200760
761 *bx = sx * src_width / surface->width + src_x;
762 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100763}
764
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500765WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200766weston_surface_to_buffer_float(struct weston_surface *surface,
767 float sx, float sy, float *bx, float *by)
768{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200769 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
770
Jonny Lamb74130762013-11-26 18:19:46 +0100771 /* first transform coordinates if the scaler is set */
772 scaler_surface_to_buffer(surface, sx, sy, bx, by);
773
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500774 weston_transformed_coord(surface->width_from_buffer,
775 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200776 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100777 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200778}
779
Derek Foreman99739672015-12-03 16:38:11 -0600780/** Transform a rectangle from surface coordinates to buffer coordinates
781 *
782 * \param surface The surface to fetch wl_viewport and buffer transformation
783 * from.
784 * \param rect The rectangle to transform.
785 * \return The transformed rectangle.
786 *
787 * Viewport and buffer transformations can only do translation, scaling,
788 * and rotations in 90-degree steps. Therefore the only loss in the
789 * conversion is coordinate rounding.
790 *
791 * However, some coordinate rounding takes place as an intermediate
792 * step before the buffer scale factor is applied, so the rectangle
793 * boundary may not be exactly as expected.
794 *
795 * This is OK for damage tracking since a little extra coverage is
796 * not a problem.
797 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200798WL_EXPORT pixman_box32_t
799weston_surface_to_buffer_rect(struct weston_surface *surface,
800 pixman_box32_t rect)
801{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200802 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100803 float xf, yf;
804
805 /* first transform box coordinates if the scaler is set */
806 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
807 rect.x1 = floorf(xf);
808 rect.y1 = floorf(yf);
809
810 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600811 rect.x2 = ceilf(xf);
812 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100813
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500814 return weston_transformed_rect(surface->width_from_buffer,
815 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200816 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200817 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200818}
819
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200820/** Transform a region from surface coordinates to buffer coordinates
821 *
822 * \param surface The surface to fetch wl_viewport and buffer transformation
823 * from.
824 * \param surface_region[in] The region in surface coordinates.
825 * \param buffer_region[out] The region converted to buffer coordinates.
826 *
827 * Buffer_region must be init'd, but will be completely overwritten.
828 *
829 * Viewport and buffer transformations can only do translation, scaling,
830 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600831 * conversion is from the coordinate rounding that takes place in
832 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200833 */
834WL_EXPORT void
835weston_surface_to_buffer_region(struct weston_surface *surface,
836 pixman_region32_t *surface_region,
837 pixman_region32_t *buffer_region)
838{
839 pixman_box32_t *src_rects, *dest_rects;
840 int nrects, i;
841
842 src_rects = pixman_region32_rectangles(surface_region, &nrects);
843 dest_rects = malloc(nrects * sizeof(*dest_rects));
844 if (!dest_rects)
845 return;
846
847 for (i = 0; i < nrects; i++) {
848 dest_rects[i] = weston_surface_to_buffer_rect(surface,
849 src_rects[i]);
850 }
851
852 pixman_region32_fini(buffer_region);
853 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
854 free(dest_rects);
855}
856
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200857WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500858weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400859 struct weston_plane *plane)
860{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500861 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400862 return;
863
Jason Ekstranda7af7042013-10-12 22:38:11 -0500864 weston_view_damage_below(view);
865 view->plane = plane;
866 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400867}
868
Pekka Paalanen51723d52015-02-17 13:10:01 +0200869/** Inflict damage on the plane where the view is visible.
870 *
871 * \param view The view that causes the damage.
872 *
873 * If the view is currently on a plane (including the primary plane),
874 * take the view's boundingbox, subtract all the opaque views that cover it,
875 * and add the remaining region as damage to the plane. This corresponds
876 * to the damage inflicted to the plane if this view disappeared.
877 *
878 * A repaint is scheduled for this view.
879 *
880 * The region of all opaque views covering this view is stored in
881 * weston_view::clip and updated by view_accumulate_damage() during
882 * weston_output_repaint(). Specifically, that region matches the
883 * scenegraph as it was last painted.
884 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400885WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500886weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200887{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500888 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200889
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500890 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200891 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500892 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800893 if (view->plane)
894 pixman_region32_union(&view->plane->damage,
895 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500896 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800897 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200898}
899
Bryce Harrington3f650b82015-12-23 11:01:58 -0800900/**
901 * \param es The surface
902 * \param mask The new set of outputs for the surface
903 *
904 * Sets the surface's set of outputs to the ones specified by
905 * the new output mask provided. Identifies the outputs that
906 * have changed, the posts enter and leave events for these
907 * outputs as appropriate.
908 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400909static void
910weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
911{
912 uint32_t different = es->output_mask ^ mask;
913 uint32_t entered = mask & different;
914 uint32_t left = es->output_mask & different;
915 struct weston_output *output;
916 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500917 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400918
919 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500920 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400921 return;
922 if (different == 0)
923 return;
924
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500925 client = wl_resource_get_client(es->resource);
926
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400927 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800928 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400929 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500930 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400931 client);
932 if (resource == NULL)
933 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800934 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500935 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800936 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500937 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400938 }
939}
940
Bryce Harrington3f650b82015-12-23 11:01:58 -0800941/** Recalculate which output(s) the surface has views displayed on
942 *
943 * \param es The surface to remap to outputs
944 *
945 * Finds the output that is showing the largest amount of one
946 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300947 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800948 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300949 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800950 * in the output_mask for the surface.
951 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400952static void
953weston_surface_assign_output(struct weston_surface *es)
954{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955 struct weston_output *new_output;
956 struct weston_view *view;
957 pixman_region32_t region;
958 uint32_t max, area, mask;
959 pixman_box32_t *e;
960
961 new_output = NULL;
962 max = 0;
963 mask = 0;
964 pixman_region32_init(&region);
965 wl_list_for_each(view, &es->views, surface_link) {
966 if (!view->output)
967 continue;
968
969 pixman_region32_intersect(&region, &view->transform.boundingbox,
970 &view->output->region);
971
972 e = pixman_region32_extents(&region);
973 area = (e->x2 - e->x1) * (e->y2 - e->y1);
974
975 mask |= view->output_mask;
976
977 if (area >= max) {
978 new_output = view->output;
979 max = area;
980 }
981 }
982 pixman_region32_fini(&region);
983
984 es->output = new_output;
985 weston_surface_update_output_mask(es, mask);
986}
987
Bryce Harrington3f650b82015-12-23 11:01:58 -0800988/** Recalculate which output(s) the view is displayed on
989 *
990 * \param ev The view to remap to outputs
991 *
992 * Identifies the set of outputs that the view is visible on,
993 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300994 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800995 *
996 * Also does the same for the view's surface. See
997 * weston_surface_assign_output().
998 */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500999static void
1000weston_view_assign_output(struct weston_view *ev)
1001{
1002 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001003 struct weston_output *output, *new_output;
1004 pixman_region32_t region;
1005 uint32_t max, area, mask;
1006 pixman_box32_t *e;
1007
1008 new_output = NULL;
1009 max = 0;
1010 mask = 0;
1011 pixman_region32_init(&region);
1012 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001013 if (output->destroying)
1014 continue;
1015
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001017 &output->region);
1018
1019 e = pixman_region32_extents(&region);
1020 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1021
1022 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001023 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001024
1025 if (area >= max) {
1026 new_output = output;
1027 max = area;
1028 }
1029 }
1030 pixman_region32_fini(&region);
1031
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 ev->output = new_output;
1033 ev->output_mask = mask;
1034
1035 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001036}
1037
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001038static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001039weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1040 int from_x, int from_y, int *to_x, int *to_y)
1041{
1042 float x, y;
1043
1044 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1045 weston_view_from_global_float(to, x, y, &x, &y);
1046
1047 *to_x = round(x);
1048 *to_y = round(y);
1049}
1050
1051static void
1052weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1053{
1054 pixman_box32_t *a;
1055 pixman_box32_t b;
1056
1057 a = pixman_region32_extents(&from->geometry.scissor);
1058
1059 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1060 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1061
1062 pixman_region32_fini(&to->geometry.scissor);
1063 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1064}
1065
1066static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001067view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001069{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001070 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1071 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001072 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001073 { inbox->x1, inbox->y1 },
1074 { inbox->x1, inbox->y2 },
1075 { inbox->x2, inbox->y1 },
1076 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001077 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001078 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001079 int i;
1080
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001081 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001082 /* avoid rounding empty bbox to 1x1 */
1083 pixman_region32_init(bbox);
1084 return;
1085 }
1086
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001087 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001088 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001090 if (x < min_x)
1091 min_x = x;
1092 if (x > max_x)
1093 max_x = x;
1094 if (y < min_y)
1095 min_y = y;
1096 if (y > max_y)
1097 max_y = y;
1098 }
1099
Pekka Paalanen219b9822012-02-08 15:38:37 +02001100 int_x = floorf(min_x);
1101 int_y = floorf(min_y);
1102 pixman_region32_init_rect(bbox, int_x, int_y,
1103 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001104}
1105
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001106static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001107weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001108{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001109 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001110
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001111 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001112 view->geometry.x = roundf(view->geometry.x);
1113 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001114
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001115 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1117 view->transform.position.matrix.d[12] = view->geometry.x;
1118 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001119
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001121
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 view->transform.inverse = view->transform.position.matrix;
1123 view->transform.inverse.d[12] = -view->geometry.x;
1124 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001125
Jason Ekstranda7af7042013-10-12 22:38:11 -05001126 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001127 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001128 view->surface->width,
1129 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001130 if (view->geometry.scissor_enabled)
1131 pixman_region32_intersect(&view->transform.boundingbox,
1132 &view->transform.boundingbox,
1133 &view->geometry.scissor);
1134
1135 pixman_region32_translate(&view->transform.boundingbox,
1136 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001137
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 if (view->alpha == 1.0) {
1139 pixman_region32_copy(&view->transform.opaque,
1140 &view->surface->opaque);
1141 pixman_region32_translate(&view->transform.opaque,
1142 view->geometry.x,
1143 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001144 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001145}
1146
1147static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001149{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 struct weston_view *parent = view->geometry.parent;
1151 struct weston_matrix *matrix = &view->transform.matrix;
1152 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001153 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001154 pixman_region32_t surfregion;
1155 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001156
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001158
1159 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1161 view->transform.position.matrix.d[12] = view->geometry.x;
1162 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001163
1164 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001166 weston_matrix_multiply(matrix, &tform->matrix);
1167
Pekka Paalanen483243f2013-03-08 14:56:50 +02001168 if (parent)
1169 weston_matrix_multiply(matrix, &parent->transform.matrix);
1170
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001171 if (weston_matrix_invert(inverse, matrix) < 0) {
1172 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173 weston_log("error: weston_view %p"
1174 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001175 return -1;
1176 }
1177
Pekka Paalanen380adf52015-02-16 14:39:11 +02001178 pixman_region32_init_rect(&surfregion, 0, 0,
1179 view->surface->width, view->surface->height);
1180 if (view->geometry.scissor_enabled)
1181 pixman_region32_intersect(&surfregion, &surfregion,
1182 &view->geometry.scissor);
1183 surfbox = pixman_region32_extents(&surfregion);
1184
1185 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1186 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001187
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001188 return 0;
1189}
1190
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001191static struct weston_layer *
1192get_view_layer(struct weston_view *view)
1193{
1194 if (view->parent_view)
1195 return get_view_layer(view->parent_view);
1196 return view->layer_link.layer;
1197}
1198
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001199WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001201{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001203 struct weston_layer *layer;
1204 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001205
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001207 return;
1208
Pekka Paalanen483243f2013-03-08 14:56:50 +02001209 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001211
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001213
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001215
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 pixman_region32_fini(&view->transform.boundingbox);
1217 pixman_region32_fini(&view->transform.opaque);
1218 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001219
Pekka Paalanencd403622012-01-25 13:37:39 +02001220 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 if (view->geometry.transformation_list.next ==
1222 &view->transform.position.link &&
1223 view->geometry.transformation_list.prev ==
1224 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001225 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001227 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 if (weston_view_update_transform_enable(view) < 0)
1229 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001230 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001231
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001232 layer = get_view_layer(view);
1233 if (layer) {
1234 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001235 pixman_region32_intersect(&view->transform.boundingbox,
1236 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001237 pixman_region32_intersect(&view->transform.opaque,
1238 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001239 pixman_region32_fini(&mask);
1240 }
1241
Pekka Paalanen380adf52015-02-16 14:39:11 +02001242 if (parent) {
1243 if (parent->geometry.scissor_enabled) {
1244 view->geometry.scissor_enabled = true;
1245 weston_view_transfer_scissor(parent, view);
1246 } else {
1247 view->geometry.scissor_enabled = false;
1248 }
1249 }
1250
Jason Ekstranda7af7042013-10-12 22:38:11 -05001251 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001252
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001254
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 wl_signal_emit(&view->surface->compositor->transform_signal,
1256 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001257}
1258
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001259WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001261{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001263
1264 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 * The invariant: if view->geometry.dirty, then all views
1266 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001267 * Corollary: if not parent->geometry.dirty, then all ancestors
1268 * are not dirty.
1269 */
1270
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001272 return;
1273
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001275
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001277 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001279}
1280
1281WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282weston_view_to_global_fixed(struct weston_view *view,
1283 wl_fixed_t vx, wl_fixed_t vy,
1284 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001285{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001286 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001287
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 weston_view_to_global_float(view,
1289 wl_fixed_to_double(vx),
1290 wl_fixed_to_double(vy),
1291 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001292 *x = wl_fixed_from_double(xf);
1293 *y = wl_fixed_from_double(yf);
1294}
1295
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001296WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297weston_view_from_global_float(struct weston_view *view,
1298 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001299{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001301 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1302
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001304
1305 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001306 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001308 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 *vx = 0;
1310 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001311 return;
1312 }
1313
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 *vx = v.f[0] / v.f[3];
1315 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001316 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317 *vx = x - view->geometry.x;
1318 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001319 }
1320}
1321
1322WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323weston_view_from_global_fixed(struct weston_view *view,
1324 wl_fixed_t x, wl_fixed_t y,
1325 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001326{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001328
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329 weston_view_from_global_float(view,
1330 wl_fixed_to_double(x),
1331 wl_fixed_to_double(y),
1332 &vxf, &vyf);
1333 *vx = wl_fixed_from_double(vxf);
1334 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001335}
1336
1337WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338weston_view_from_global(struct weston_view *view,
1339 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001340{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001342
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1344 *vx = floorf(vxf);
1345 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001346}
1347
Bryce Harrington3f650b82015-12-23 11:01:58 -08001348/**
1349 * \param surface The surface to be repainted
1350 *
1351 * Marks the output(s) that the surface is shown on as needing to be
1352 * repainted. See weston_output_schedule_repaint().
1353 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001354WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001355weston_surface_schedule_repaint(struct weston_surface *surface)
1356{
1357 struct weston_output *output;
1358
1359 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001360 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001361 weston_output_schedule_repaint(output);
1362}
1363
Bryce Harrington3f650b82015-12-23 11:01:58 -08001364/**
1365 * \param view The view to be repainted
1366 *
1367 * Marks the output(s) that the view is shown on as needing to be
1368 * repainted. See weston_output_schedule_repaint().
1369 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001370WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371weston_view_schedule_repaint(struct weston_view *view)
1372{
1373 struct weston_output *output;
1374
1375 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001376 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 weston_output_schedule_repaint(output);
1378}
1379
Pekka Paalanene508ce62015-02-19 13:59:55 +02001380/**
1381 * XXX: This function does it the wrong way.
1382 * surface->damage is the damage from the client, and causes
1383 * surface_flush_damage() to copy pixels. No window management action can
1384 * cause damage to the client-provided content, warranting re-upload!
1385 *
1386 * Instead of surface->damage, this function should record the damage
1387 * with all the views for this surface to avoid extraneous texture
1388 * uploads.
1389 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001391weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001392{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001393 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001394 0, 0, surface->width,
1395 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001396
Kristian Høgsberg98238702012-08-03 16:29:12 -04001397 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001398}
1399
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001400WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001402{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001403 if (view->geometry.x == x && view->geometry.y == y)
1404 return;
1405
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 view->geometry.x = x;
1407 view->geometry.y = y;
1408 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001409}
1410
Pekka Paalanen483243f2013-03-08 14:56:50 +02001411static void
1412transform_parent_handle_parent_destroy(struct wl_listener *listener,
1413 void *data)
1414{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415 struct weston_view *view =
1416 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001417 geometry.parent_destroy_listener);
1418
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001420}
1421
1422WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001424 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001425{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001426 if (view->geometry.parent) {
1427 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1428 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001429
1430 if (!parent)
1431 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001432 }
1433
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001435
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001437 transform_parent_handle_parent_destroy;
1438 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001439 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001441 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001443 }
1444
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 weston_view_geometry_dirty(view);
1446}
1447
Pekka Paalanen380adf52015-02-16 14:39:11 +02001448/** Set a clip mask rectangle on a view
1449 *
1450 * \param view The view to set the clip mask on.
1451 * \param x Top-left corner X coordinate of the clip rectangle.
1452 * \param y Top-left corner Y coordinate of the clip rectangle.
1453 * \param width Width of the clip rectangle, non-negative.
1454 * \param height Height of the clip rectangle, non-negative.
1455 *
1456 * A shell may set a clip mask rectangle on a view. Everything outside
1457 * the rectangle is cut away for input and output purposes: it is
1458 * not drawn and cannot be hit by hit-test based input like pointer
1459 * motion or touch-downs. Everything inside the rectangle will behave
1460 * normally. Clients are unaware of clipping.
1461 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001462 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001463 * mask rectangle does not affect the view position, the view is positioned
1464 * as it would be without a clip. The clip also does not change
1465 * weston_surface::width,height.
1466 *
1467 * The clip mask rectangle is part of transformation inheritance
1468 * (weston_view_set_transform_parent()). A clip set in the root of the
1469 * transformation inheritance tree will affect all views in the tree.
1470 * A clip can be set only on the root view. Attempting to set a clip
1471 * on view that has a transformation parent will fail. Assigning a parent
1472 * to a view that has a clip set will cause the clip to be forgotten.
1473 *
1474 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1475 * on the additional transformations in the child views. These transformations
1476 * may not rotate the coordinate axes, i.e., only translation and scaling
1477 * are allowed. Violating this restriction causes the clipping to malfunction.
1478 * Furthermore, using scaling may cause rounding errors in child clipping.
1479 *
1480 * The clip mask rectangle is not automatically adjusted based on
1481 * wl_surface.attach dx and dy arguments.
1482 *
1483 * A clip mask rectangle can be set only if the compositor capability
1484 * WESTON_CAP_VIEW_CLIP_MASK is present.
1485 *
1486 * This function sets the clip mask rectangle and schedules a repaint for
1487 * the view.
1488 */
1489WL_EXPORT void
1490weston_view_set_mask(struct weston_view *view,
1491 int x, int y, int width, int height)
1492{
1493 struct weston_compositor *compositor = view->surface->compositor;
1494
1495 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1496 weston_log("%s not allowed without capability!\n", __func__);
1497 return;
1498 }
1499
1500 if (view->geometry.parent) {
1501 weston_log("view %p has a parent, clip forbidden!\n", view);
1502 return;
1503 }
1504
1505 if (width < 0 || height < 0) {
1506 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1507 x, y, width, height);
1508 return;
1509 }
1510
1511 pixman_region32_fini(&view->geometry.scissor);
1512 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1513 view->geometry.scissor_enabled = true;
1514 weston_view_geometry_dirty(view);
1515 weston_view_schedule_repaint(view);
1516}
1517
1518/** Remove the clip mask from a view
1519 *
1520 * \param view The view to remove the clip mask from.
1521 *
1522 * Removed the clip mask rectangle and schedules a repaint.
1523 *
1524 * \sa weston_view_set_mask
1525 */
1526WL_EXPORT void
1527weston_view_set_mask_infinite(struct weston_view *view)
1528{
1529 view->geometry.scissor_enabled = false;
1530 weston_view_geometry_dirty(view);
1531 weston_view_schedule_repaint(view);
1532}
1533
Derek Foreman280e7dd2014-10-03 13:13:42 -05001534WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535weston_view_is_mapped(struct weston_view *view)
1536{
1537 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001538 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001539 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001540 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001541}
1542
Derek Foreman280e7dd2014-10-03 13:13:42 -05001543WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001544weston_surface_is_mapped(struct weston_surface *surface)
1545{
1546 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001547 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001548 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001549 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001550}
1551
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001552static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001553surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001554{
1555 struct weston_view *view;
1556
1557 if (surface->width == width && surface->height == height)
1558 return;
1559
1560 surface->width = width;
1561 surface->height = height;
1562
1563 wl_list_for_each(view, &surface->views, surface_link)
1564 weston_view_geometry_dirty(view);
1565}
1566
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001567WL_EXPORT void
1568weston_surface_set_size(struct weston_surface *surface,
1569 int32_t width, int32_t height)
1570{
1571 assert(!surface->resource);
1572 surface_set_size(surface, width, height);
1573}
1574
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001575static int
1576fixed_round_up_to_int(wl_fixed_t f)
1577{
1578 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1579}
1580
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001581static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001582convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1583 int32_t width, int32_t height,
1584 uint32_t transform,
1585 int32_t scale)
1586{
1587 assert(scale > 0);
1588
1589 switch (transform) {
1590 case WL_OUTPUT_TRANSFORM_NORMAL:
1591 case WL_OUTPUT_TRANSFORM_180:
1592 case WL_OUTPUT_TRANSFORM_FLIPPED:
1593 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1594 *width_out = width / scale;
1595 *height_out = height / scale;
1596 break;
1597 case WL_OUTPUT_TRANSFORM_90:
1598 case WL_OUTPUT_TRANSFORM_270:
1599 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1600 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1601 *width_out = height / scale;
1602 *height_out = width / scale;
1603 break;
1604 default:
1605 assert(0 && "invalid transform");
1606 }
1607}
1608
1609static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001610weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001611{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001612 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001613
1614 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001615 surface->width_from_buffer = 0;
1616 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001617 return;
1618 }
1619
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001620 convert_size_by_transform_scale(&surface->width_from_buffer,
1621 &surface->height_from_buffer,
1622 surface->buffer_ref.buffer->width,
1623 surface->buffer_ref.buffer->height,
1624 vp->buffer.transform,
1625 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001626}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001627
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001628static void
1629weston_surface_update_size(struct weston_surface *surface)
1630{
1631 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1632 int32_t width, height;
1633
1634 width = surface->width_from_buffer;
1635 height = surface->height_from_buffer;
1636
1637 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001638 surface_set_size(surface,
1639 vp->surface.width, vp->surface.height);
1640 return;
1641 }
1642
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001643 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001644 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1645 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1646
1647 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001648 return;
1649 }
1650
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001651 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001652}
1653
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001654WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001655weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001656{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001657 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001658
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001659 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001660
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001661 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001662}
1663
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664WL_EXPORT struct weston_view *
1665weston_compositor_pick_view(struct weston_compositor *compositor,
1666 wl_fixed_t x, wl_fixed_t y,
1667 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001668{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001670 wl_fixed_t view_x, view_y;
1671 int view_ix, view_iy;
1672 int ix = wl_fixed_to_int(x);
1673 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001674
Jason Ekstranda7af7042013-10-12 22:38:11 -05001675 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001676 if (!pixman_region32_contains_point(
1677 &view->transform.boundingbox, ix, iy, NULL))
1678 continue;
1679
1680 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1681 view_ix = wl_fixed_to_int(view_x);
1682 view_iy = wl_fixed_to_int(view_y);
1683
1684 if (!pixman_region32_contains_point(&view->surface->input,
1685 view_ix, view_iy, NULL))
1686 continue;
1687
Pekka Paalanen380adf52015-02-16 14:39:11 +02001688 if (view->geometry.scissor_enabled &&
1689 !pixman_region32_contains_point(&view->geometry.scissor,
1690 view_ix, view_iy, NULL))
1691 continue;
1692
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001693 *vx = view_x;
1694 *vy = view_y;
1695 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001696 }
1697
Derek Foremanf9318d12015-05-11 15:40:11 -05001698 *vx = wl_fixed_from_int(-1000000);
1699 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001700 return NULL;
1701}
1702
1703static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001704weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001705{
Daniel Stone37816df2012-05-16 18:45:18 +01001706 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001707
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001708 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001709 return;
1710
Daniel Stone37816df2012-05-16 18:45:18 +01001711 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001712 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001713}
1714
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001715WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001716weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001717{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001718 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001719
Jason Ekstranda7af7042013-10-12 22:38:11 -05001720 if (!weston_view_is_mapped(view))
1721 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001722
Jason Ekstranda7af7042013-10-12 22:38:11 -05001723 weston_view_damage_below(view);
1724 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001725 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001726 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001727 wl_list_remove(&view->link);
1728 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001729 view->output_mask = 0;
1730 weston_surface_assign_output(view->surface);
1731
1732 if (weston_surface_is_mapped(view->surface))
1733 return;
1734
1735 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001736 struct weston_touch *touch = weston_seat_get_touch(seat);
1737 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1738 struct weston_keyboard *keyboard =
1739 weston_seat_get_keyboard(seat);
1740
1741 if (keyboard && keyboard->focus == view->surface)
1742 weston_keyboard_set_focus(keyboard, NULL);
1743 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001744 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001745 if (touch && touch->focus == view)
1746 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001747 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001748}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001749
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750WL_EXPORT void
1751weston_surface_unmap(struct weston_surface *surface)
1752{
1753 struct weston_view *view;
1754
1755 wl_list_for_each(view, &surface->views, surface_link)
1756 weston_view_unmap(view);
1757 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001758}
1759
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001760static void
1761weston_surface_reset_pending_buffer(struct weston_surface *surface)
1762{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001763 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001764 surface->pending.sx = 0;
1765 surface->pending.sy = 0;
1766 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001767 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001768}
1769
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770WL_EXPORT void
1771weston_view_destroy(struct weston_view *view)
1772{
1773 wl_signal_emit(&view->destroy_signal, view);
1774
1775 assert(wl_list_empty(&view->geometry.child_list));
1776
1777 if (weston_view_is_mapped(view)) {
1778 weston_view_unmap(view);
1779 weston_compositor_build_view_list(view->surface->compositor);
1780 }
1781
1782 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001783 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001784
1785 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001786 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001787 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001788 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789
1790 weston_view_set_transform_parent(view, NULL);
1791
Jason Ekstranda7af7042013-10-12 22:38:11 -05001792 wl_list_remove(&view->surface_link);
1793
1794 free(view);
1795}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001796
1797WL_EXPORT void
1798weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001799{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001800 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001802
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001803 if (--surface->ref_count > 0)
1804 return;
1805
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001806 assert(surface->resource == NULL);
1807
Pekka Paalanenca790762015-04-17 14:23:38 +03001808 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001809
Pekka Paalanene67858b2013-04-25 13:57:42 +03001810 assert(wl_list_empty(&surface->subsurface_list_pending));
1811 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001812
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1814 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001815
Jason Ekstrand7b982072014-05-20 14:33:03 -05001816 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001817
Pekka Paalanende685b82012-12-04 15:58:12 +02001818 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001819
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001820 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001821 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001822 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001823
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001824 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001825 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001826
Pekka Paalanen133e4392014-09-23 22:08:46 -04001827 weston_presentation_feedback_discard_list(&surface->feedback_list);
1828
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001829 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001830}
1831
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001832static void
1833destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001834{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001835 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001836
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001837 assert(surface);
1838
Giulio Camuffo0d379742013-11-15 22:06:15 +01001839 /* Set the resource to NULL, since we don't want to leave a
1840 * dangling pointer if the surface was refcounted and survives
1841 * the weston_surface_destroy() call. */
1842 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001843 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001844}
1845
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001846static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001847weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1848{
1849 struct weston_buffer *buffer =
1850 container_of(listener, struct weston_buffer, destroy_listener);
1851
1852 wl_signal_emit(&buffer->destroy_signal, buffer);
1853 free(buffer);
1854}
1855
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001856WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001857weston_buffer_from_resource(struct wl_resource *resource)
1858{
1859 struct weston_buffer *buffer;
1860 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001861
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001862 listener = wl_resource_get_destroy_listener(resource,
1863 weston_buffer_destroy_handler);
1864
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001865 if (listener)
1866 return container_of(listener, struct weston_buffer,
1867 destroy_listener);
1868
1869 buffer = zalloc(sizeof *buffer);
1870 if (buffer == NULL)
1871 return NULL;
1872
1873 buffer->resource = resource;
1874 wl_signal_init(&buffer->destroy_signal);
1875 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001876 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001877 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001878
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001879 return buffer;
1880}
1881
1882static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001883weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1884 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001885{
Pekka Paalanende685b82012-12-04 15:58:12 +02001886 struct weston_buffer_reference *ref =
1887 container_of(listener, struct weston_buffer_reference,
1888 destroy_listener);
1889
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001890 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001891 ref->buffer = NULL;
1892}
1893
1894WL_EXPORT void
1895weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001896 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001897{
1898 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001899 ref->buffer->busy_count--;
1900 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001901 assert(wl_resource_get_client(ref->buffer->resource));
1902 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001903 WL_BUFFER_RELEASE);
1904 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001905 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001906 }
1907
Pekka Paalanende685b82012-12-04 15:58:12 +02001908 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001909 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001910 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001911 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001912 }
1913
Pekka Paalanende685b82012-12-04 15:58:12 +02001914 ref->buffer = buffer;
1915 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1916}
1917
1918static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001919weston_surface_attach(struct weston_surface *surface,
1920 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001921{
1922 weston_buffer_reference(&surface->buffer_ref, buffer);
1923
Pekka Paalanena6421c42012-12-04 15:58:10 +02001924 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001925 if (weston_surface_is_mapped(surface))
1926 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001927 }
1928
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001929 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001930
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001931 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001932 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001933}
1934
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001935WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001936weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001937{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001938 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001939
1940 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001941 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001942}
1943
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001944WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001945weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001946{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001947 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001948
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001949 pixman_region32_union(&compositor->primary_plane.damage,
1950 &compositor->primary_plane.damage,
1951 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001952 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001953}
1954
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001955static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001956surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001957{
Pekka Paalanende685b82012-12-04 15:58:12 +02001958 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001959 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001960 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001961
Pekka Paalanenb5026542014-11-12 15:09:24 +02001962 if (weston_timeline_enabled_ &&
1963 pixman_region32_not_empty(&surface->damage))
1964 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1965 TLP_OUTPUT(surface->output), TLP_END);
1966
Jason Ekstrandef540082014-06-26 10:37:36 -07001967 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001968}
1969
1970static void
1971view_accumulate_damage(struct weston_view *view,
1972 pixman_region32_t *opaque)
1973{
1974 pixman_region32_t damage;
1975
1976 pixman_region32_init(&damage);
1977 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001978 pixman_box32_t *extents;
1979
Jason Ekstranda7af7042013-10-12 22:38:11 -05001980 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001981 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001982 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001983 pixman_region32_copy(&damage, &view->surface->damage);
1984 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02001985 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001986 }
1987
Pekka Paalanen380adf52015-02-16 14:39:11 +02001988 pixman_region32_intersect(&damage, &damage,
1989 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001990 pixman_region32_subtract(&damage, &damage, opaque);
1991 pixman_region32_union(&view->plane->damage,
1992 &view->plane->damage, &damage);
1993 pixman_region32_fini(&damage);
1994 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001995 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001996}
1997
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001998static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001999compositor_accumulate_damage(struct weston_compositor *ec)
2000{
2001 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002002 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002003 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002004
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002005 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002006
2007 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002008 pixman_region32_copy(&plane->clip, &clip);
2009
2010 pixman_region32_init(&opaque);
2011
Jason Ekstranda7af7042013-10-12 22:38:11 -05002012 wl_list_for_each(ev, &ec->view_list, link) {
2013 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002014 continue;
2015
Jason Ekstranda7af7042013-10-12 22:38:11 -05002016 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002017 }
2018
2019 pixman_region32_union(&clip, &clip, &opaque);
2020 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002021 }
2022
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002023 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002024
Jason Ekstranda7af7042013-10-12 22:38:11 -05002025 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002026 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002027
2028 wl_list_for_each(ev, &ec->view_list, link) {
2029 if (ev->surface->touched)
2030 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002031 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002032
2033 surface_flush_damage(ev->surface);
2034
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002035 /* Both the renderer and the backend have seen the buffer
2036 * by now. If renderer needs the buffer, it has its own
2037 * reference set. If the backend wants to keep the buffer
2038 * around for migrating the surface into a non-primary plane
2039 * later, keep_buffer is true. Otherwise, drop the core
2040 * reference now, and allow early buffer release. This enables
2041 * clients to use single-buffering.
2042 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002043 if (!ev->surface->keep_buffer)
2044 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002045 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002046}
2047
2048static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002050{
2051 struct weston_subsurface *sub;
2052
Pekka Paalanene67858b2013-04-25 13:57:42 +03002053 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002054 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002055 continue;
2056
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2058 wl_list_init(&sub->surface->views);
2059
2060 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002061 }
2062}
2063
2064static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002065surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002066{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002067 struct weston_subsurface *sub;
2068 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002069
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2071 if (sub->surface == surface)
2072 continue;
2073
George Kiagiadakised04d382014-06-13 18:10:26 +02002074 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2075 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002077 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002078
2079 surface_free_unused_subsurface_views(sub->surface);
2080 }
2081}
2082
2083static void
2084view_list_add_subsurface_view(struct weston_compositor *compositor,
2085 struct weston_subsurface *sub,
2086 struct weston_view *parent)
2087{
2088 struct weston_subsurface *child;
2089 struct weston_view *view = NULL, *iv;
2090
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002091 if (!weston_surface_is_mapped(sub->surface))
2092 return;
2093
Jason Ekstranda7af7042013-10-12 22:38:11 -05002094 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2095 if (iv->geometry.parent == parent) {
2096 view = iv;
2097 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002098 }
2099 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002100
2101 if (view) {
2102 /* Put it back in the surface's list of views */
2103 wl_list_remove(&view->surface_link);
2104 wl_list_insert(&sub->surface->views, &view->surface_link);
2105 } else {
2106 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002107 weston_view_set_position(view,
2108 sub->position.x,
2109 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002110 weston_view_set_transform_parent(view, parent);
2111 }
2112
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002113 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002114 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002115
Pekka Paalanenb188e912013-11-19 14:03:35 +02002116 if (wl_list_empty(&sub->surface->subsurface_list)) {
2117 wl_list_insert(compositor->view_list.prev, &view->link);
2118 return;
2119 }
2120
2121 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2122 if (child->surface == sub->surface)
2123 wl_list_insert(compositor->view_list.prev, &view->link);
2124 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002125 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002126 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127}
2128
2129static void
2130view_list_add(struct weston_compositor *compositor,
2131 struct weston_view *view)
2132{
2133 struct weston_subsurface *sub;
2134
2135 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136
Pekka Paalanenb188e912013-11-19 14:03:35 +02002137 if (wl_list_empty(&view->surface->subsurface_list)) {
2138 wl_list_insert(compositor->view_list.prev, &view->link);
2139 return;
2140 }
2141
2142 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2143 if (sub->surface == view->surface)
2144 wl_list_insert(compositor->view_list.prev, &view->link);
2145 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002146 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002147 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002148}
2149
2150static void
2151weston_compositor_build_view_list(struct weston_compositor *compositor)
2152{
2153 struct weston_view *view;
2154 struct weston_layer *layer;
2155
2156 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002157 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002158 surface_stash_subsurface_views(view->surface);
2159
2160 wl_list_init(&compositor->view_list);
2161 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002162 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002163 view_list_add(compositor, view);
2164 }
2165 }
2166
2167 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002168 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002170}
2171
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002172static void
2173weston_output_take_feedback_list(struct weston_output *output,
2174 struct weston_surface *surface)
2175{
2176 struct weston_view *view;
2177 struct weston_presentation_feedback *feedback;
2178 uint32_t flags = 0xffffffff;
2179
2180 if (wl_list_empty(&surface->feedback_list))
2181 return;
2182
2183 /* All views must have the flag for the flag to survive. */
2184 wl_list_for_each(view, &surface->views, surface_link) {
2185 /* ignore views that are not on this output at all */
2186 if (view->output_mask & (1u << output->id))
2187 flags &= view->psf_flags;
2188 }
2189
2190 wl_list_for_each(feedback, &surface->feedback_list, link)
2191 feedback->psf_flags = flags;
2192
2193 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2194 wl_list_init(&surface->feedback_list);
2195}
2196
David Herrmann1edf44c2013-10-22 17:11:26 +02002197static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002198weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002199{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002200 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002201 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002202 struct weston_animation *animation, *next;
2203 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002204 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002205 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002206 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002207
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002208 if (output->destroying)
2209 return 0;
2210
Pekka Paalanenb5026542014-11-12 15:09:24 +02002211 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2212
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002213 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002214 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002215
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002216 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002217 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002218 } else {
2219 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002220 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002221 ev->psf_flags = 0;
2222 }
2223 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002224
Pekka Paalanene67858b2013-04-25 13:57:42 +03002225 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226 wl_list_for_each(ev, &ec->view_list, link) {
2227 /* Note: This operation is safe to do multiple times on the
2228 * same surface.
2229 */
2230 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002231 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002232 &ev->surface->frame_callback_list);
2233 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002234
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002235 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002236 }
2237 }
2238
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002239 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002240
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002241 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002242 pixman_region32_intersect(&output_damage,
2243 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002244 pixman_region32_subtract(&output_damage,
2245 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002246
Scott Moreauccbf29d2012-02-22 14:21:41 -07002247 if (output->dirty)
2248 weston_output_update_matrix(output);
2249
David Herrmann1edf44c2013-10-22 17:11:26 +02002250 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002251
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002252 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002253
Kristian Høgsbergef044142011-06-21 15:02:12 -04002254 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002255
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002256 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002257
Jonas Ådahldb773762012-06-13 00:01:21 +02002258 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002259 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002260 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002261 }
2262
Scott Moreaud64cf212012-06-08 19:40:54 -06002263 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002264 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002265 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002266 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002267
Pekka Paalanenb5026542014-11-12 15:09:24 +02002268 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2269
David Herrmann1edf44c2013-10-22 17:11:26 +02002270 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002271}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002272
Pekka Paalanen82919792014-05-21 13:51:49 +03002273static void
2274weston_output_schedule_repaint_reset(struct weston_output *output)
2275{
Pekka Paalanen82919792014-05-21 13:51:49 +03002276 output->repaint_scheduled = 0;
2277 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002278}
2279
Pekka Paalanen0513a952014-05-21 16:17:27 +03002280static int
2281output_repaint_timer_handler(void *data)
2282{
2283 struct weston_output *output = data;
2284 struct weston_compositor *compositor = output->compositor;
2285
2286 if (output->repaint_needed &&
2287 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2288 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2289 weston_output_repaint(output) == 0)
2290 return 0;
2291
2292 weston_output_schedule_repaint_reset(output);
2293
2294 return 0;
2295}
2296
Kristian Høgsbergef044142011-06-21 15:02:12 -04002297WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002298weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002299 const struct timespec *stamp,
2300 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002301{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002302 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002303 int32_t refresh_nsec;
2304 struct timespec now;
2305 struct timespec gone;
2306 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002307
Pekka Paalanenb5026542014-11-12 15:09:24 +02002308 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2309 TLP_VBLANK(stamp), TLP_END);
2310
Pekka Paalanend7894d02015-07-03 15:08:53 +03002311 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002312 weston_presentation_feedback_present_list(&output->feedback_list,
2313 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002314 output->msc,
2315 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002316
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002317 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002318
Pekka Paalanen0513a952014-05-21 16:17:27 +03002319 weston_compositor_read_presentation_clock(compositor, &now);
2320 timespec_sub(&gone, &now, stamp);
2321 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2322 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002323
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002324 if (msec < -1000 || msec > 1000) {
2325 static bool warned;
2326
2327 if (!warned)
2328 weston_log("Warning: computed repaint delay is "
2329 "insane: %d msec\n", msec);
2330 warned = true;
2331
2332 msec = 0;
2333 }
2334
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002335 /* Called from restart_repaint_loop and restart happens already after
2336 * the deadline given by repaint_msec? In that case we delay until
2337 * the deadline of the next frame, to give clients a more predictable
2338 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002339 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002340 msec += refresh_nsec / 1000000;
2341
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002342 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002343 output_repaint_timer_handler(output);
2344 else
2345 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002346}
2347
2348static void
2349idle_repaint(void *data)
2350{
2351 struct weston_output *output = data;
2352
Jonas Ådahle5a12252013-04-05 23:07:11 +02002353 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002354}
2355
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002356WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002357weston_layer_entry_insert(struct weston_layer_entry *list,
2358 struct weston_layer_entry *entry)
2359{
2360 wl_list_insert(&list->link, &entry->link);
2361 entry->layer = list->layer;
2362}
2363
2364WL_EXPORT void
2365weston_layer_entry_remove(struct weston_layer_entry *entry)
2366{
2367 wl_list_remove(&entry->link);
2368 wl_list_init(&entry->link);
2369 entry->layer = NULL;
2370}
2371
2372WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002373weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2374{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002375 wl_list_init(&layer->view_list.link);
2376 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002377 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002378 if (below != NULL)
2379 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002380}
2381
2382WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002383weston_layer_set_mask(struct weston_layer *layer,
2384 int x, int y, int width, int height)
2385{
2386 struct weston_view *view;
2387
2388 layer->mask.x1 = x;
2389 layer->mask.x2 = x + width;
2390 layer->mask.y1 = y;
2391 layer->mask.y2 = y + height;
2392
2393 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2394 weston_view_geometry_dirty(view);
2395 }
2396}
2397
2398WL_EXPORT void
2399weston_layer_set_mask_infinite(struct weston_layer *layer)
2400{
2401 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2402 UINT32_MAX, UINT32_MAX);
2403}
2404
2405WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002406weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002407{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002408 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002409 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002410
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002411 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2412 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002413 return;
2414
Pekka Paalanenb5026542014-11-12 15:09:24 +02002415 if (!output->repaint_needed)
2416 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2417
Kristian Høgsbergef044142011-06-21 15:02:12 -04002418 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002419 output->repaint_needed = 1;
2420 if (output->repaint_scheduled)
2421 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002422
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002423 wl_event_loop_add_idle(loop, idle_repaint, output);
2424 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002425 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002426}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002427
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002428WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002429weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2430{
2431 struct weston_output *output;
2432
2433 wl_list_for_each(output, &compositor->output_list, link)
2434 weston_output_schedule_repaint(output);
2435}
2436
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002437static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002438surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002439{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002440 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002441}
2442
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002443static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002444surface_attach(struct wl_client *client,
2445 struct wl_resource *resource,
2446 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2447{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002448 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002449 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002450
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002451 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002452 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002453 if (buffer == NULL) {
2454 wl_client_post_no_memory(client);
2455 return;
2456 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002457 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002458
Pekka Paalanende685b82012-12-04 15:58:12 +02002459 /* Attach, attach, without commit in between does not send
2460 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002461 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002462
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002463 surface->pending.sx = sx;
2464 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002465 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002466}
2467
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002468static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002469surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002470 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002471 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002472{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002473 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002474
Derek Foreman57e92ed2015-11-17 14:11:35 -06002475 if (width <= 0 || height <= 0)
2476 return;
2477
Derek Foreman152254b2015-11-26 14:17:48 -06002478 pixman_region32_union_rect(&surface->pending.damage_surface,
2479 &surface->pending.damage_surface,
2480 x, y, width, height);
2481}
2482
2483static void
2484surface_damage_buffer(struct wl_client *client,
2485 struct wl_resource *resource,
2486 int32_t x, int32_t y, int32_t width, int32_t height)
2487{
2488 struct weston_surface *surface = wl_resource_get_user_data(resource);
2489
2490 if (width <= 0 || height <= 0)
2491 return;
2492
2493 pixman_region32_union_rect(&surface->pending.damage_buffer,
2494 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002495 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002496}
2497
Kristian Høgsberg33418202011-08-16 23:01:28 -04002498static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002499destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002500{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002501 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002502
2503 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002504 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002505}
2506
2507static void
2508surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002509 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002510{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002511 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002512 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002513
2514 cb = malloc(sizeof *cb);
2515 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002516 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002517 return;
2518 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002519
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002520 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2521 callback);
2522 if (cb->resource == NULL) {
2523 free(cb);
2524 wl_resource_post_no_memory(resource);
2525 return;
2526 }
2527
Jason Ekstranda85118c2013-06-27 20:17:02 -05002528 wl_resource_set_implementation(cb->resource, NULL, cb,
2529 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002530
Pekka Paalanenbc106382012-10-10 12:49:31 +03002531 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002532}
2533
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002534static void
2535surface_set_opaque_region(struct wl_client *client,
2536 struct wl_resource *resource,
2537 struct wl_resource *region_resource)
2538{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002539 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002540 struct weston_region *region;
2541
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002542 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002543 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002544 pixman_region32_copy(&surface->pending.opaque,
2545 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002546 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002547 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002548 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002549}
2550
2551static void
2552surface_set_input_region(struct wl_client *client,
2553 struct wl_resource *resource,
2554 struct wl_resource *region_resource)
2555{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002556 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002557 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002558
2559 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002560 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002561 pixman_region32_copy(&surface->pending.input,
2562 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002563 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002564 pixman_region32_fini(&surface->pending.input);
2565 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002566 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002567}
2568
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002569static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002570weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002571{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002572 struct weston_subsurface *sub;
2573
2574 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2575 parent_link_pending) {
2576 wl_list_remove(&sub->parent_link);
2577 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2578 }
2579}
2580
2581static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002582weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002583 struct weston_matrix *matrix)
2584{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002585 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002586 double src_width, src_height, dest_width, dest_height;
2587
2588 weston_matrix_init(matrix);
2589
2590 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2591 src_width = surface->width_from_buffer;
2592 src_height = surface->height_from_buffer;
2593 } else {
2594 src_width = wl_fixed_to_double(vp->buffer.src_width);
2595 src_height = wl_fixed_to_double(vp->buffer.src_height);
2596 }
2597
2598 if (vp->surface.width == -1) {
2599 dest_width = src_width;
2600 dest_height = src_height;
2601 } else {
2602 dest_width = vp->surface.width;
2603 dest_height = vp->surface.height;
2604 }
2605
2606 if (src_width != dest_width || src_height != dest_height)
2607 weston_matrix_scale(matrix,
2608 src_width / dest_width,
2609 src_height / dest_height, 1);
2610
2611 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2612 weston_matrix_translate(matrix,
2613 wl_fixed_to_double(vp->buffer.src_x),
2614 wl_fixed_to_double(vp->buffer.src_y),
2615 0);
2616
2617 switch (vp->buffer.transform) {
2618 case WL_OUTPUT_TRANSFORM_FLIPPED:
2619 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2620 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2621 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2622 weston_matrix_scale(matrix, -1, 1, 1);
2623 weston_matrix_translate(matrix,
2624 surface->width_from_buffer, 0, 0);
2625 break;
2626 }
2627
2628 switch (vp->buffer.transform) {
2629 default:
2630 case WL_OUTPUT_TRANSFORM_NORMAL:
2631 case WL_OUTPUT_TRANSFORM_FLIPPED:
2632 break;
2633 case WL_OUTPUT_TRANSFORM_90:
2634 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2635 weston_matrix_rotate_xy(matrix, 0, 1);
2636 weston_matrix_translate(matrix,
2637 surface->height_from_buffer, 0, 0);
2638 break;
2639 case WL_OUTPUT_TRANSFORM_180:
2640 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2641 weston_matrix_rotate_xy(matrix, -1, 0);
2642 weston_matrix_translate(matrix,
2643 surface->width_from_buffer,
2644 surface->height_from_buffer, 0);
2645 break;
2646 case WL_OUTPUT_TRANSFORM_270:
2647 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2648 weston_matrix_rotate_xy(matrix, 0, -1);
2649 weston_matrix_translate(matrix,
2650 0, surface->width_from_buffer, 0);
2651 break;
2652 }
2653
2654 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2655}
2656
Derek Foreman152254b2015-11-26 14:17:48 -06002657/* Translate pending damage in buffer co-ordinates to surface
2658 * co-ordinates and union it with a pixman_region32_t.
2659 * This should only be called after the buffer is attached.
2660 */
2661static void
2662apply_damage_buffer(pixman_region32_t *dest,
2663 struct weston_surface *surface,
2664 struct weston_surface_state *state)
2665{
2666 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2667
2668 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2669 * translated into surface co-ordinates and unioned with
2670 * any other surface damage.
2671 * None of this makes sense if there is no buffer though.
2672 */
2673 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2674 pixman_region32_t buffer_damage;
2675
2676 pixman_region32_intersect_rect(&state->damage_buffer,
2677 &state->damage_buffer,
2678 0, 0, buffer->width,
2679 buffer->height);
2680 pixman_region32_init(&buffer_damage);
2681 weston_matrix_transform_region(&buffer_damage,
2682 &surface->buffer_to_surface_matrix,
2683 &state->damage_buffer);
2684 pixman_region32_union(dest, dest, &buffer_damage);
2685 pixman_region32_fini(&buffer_damage);
2686 }
2687 /* We should clear this on commit even if there was no buffer */
2688 pixman_region32_clear(&state->damage_buffer);
2689}
2690
Jason Ekstrand1e059042014-10-16 10:55:19 -05002691static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002692weston_surface_commit_state(struct weston_surface *surface,
2693 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002694{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002695 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002696 pixman_region32_t opaque;
2697
Alexander Larsson4ea95522013-05-22 14:41:37 +02002698 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002699 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002700 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002701 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002702
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002703 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002704 if (state->newly_attached)
2705 weston_surface_attach(surface, state->buffer);
2706 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002707
Jason Ekstrand1e059042014-10-16 10:55:19 -05002708 weston_surface_build_buffer_matrix(surface,
2709 &surface->surface_to_buffer_matrix);
2710 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2711 &surface->surface_to_buffer_matrix);
2712
Jason Ekstrand7b982072014-05-20 14:33:03 -05002713 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002714 weston_surface_update_size(surface);
2715 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002716 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002717 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002718
Jason Ekstrand7b982072014-05-20 14:33:03 -05002719 state->sx = 0;
2720 state->sy = 0;
2721 state->newly_attached = 0;
2722 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002723
Derek Foreman152254b2015-11-26 14:17:48 -06002724 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002725 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002726 (pixman_region32_not_empty(&state->damage_surface) ||
2727 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002728 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002729
Pekka Paalanen8e159182012-10-10 12:49:25 +03002730 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002731 &state->damage_surface);
2732
2733 apply_damage_buffer(&surface->damage, surface, state);
2734
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002735 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002736 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002737 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002738
2739 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002740 pixman_region32_init(&opaque);
2741 pixman_region32_intersect_rect(&opaque, &state->opaque,
2742 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002743
2744 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2745 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002746 wl_list_for_each(view, &surface->views, surface_link)
2747 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002748 }
2749
2750 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002751
2752 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002753 pixman_region32_intersect_rect(&surface->input, &state->input,
2754 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002755
Pekka Paalanenbc106382012-10-10 12:49:31 +03002756 /* wl_surface.frame */
2757 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002758 &state->frame_callback_list);
2759 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002760
2761 /* XXX:
2762 * What should happen with a feedback request, if there
2763 * is no wl_buffer attached for this commit?
2764 */
2765
2766 /* presentation.feedback */
2767 wl_list_insert_list(&surface->feedback_list,
2768 &state->feedback_list);
2769 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002770}
2771
2772static void
2773weston_surface_commit(struct weston_surface *surface)
2774{
2775 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002776
Pekka Paalanene67858b2013-04-25 13:57:42 +03002777 weston_surface_commit_subsurface_order(surface);
2778
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002779 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002780}
2781
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002782static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002783weston_subsurface_commit(struct weston_subsurface *sub);
2784
2785static void
2786weston_subsurface_parent_commit(struct weston_subsurface *sub,
2787 int parent_is_synchronized);
2788
2789static void
2790surface_commit(struct wl_client *client, struct wl_resource *resource)
2791{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002792 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002793 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2794
2795 if (sub) {
2796 weston_subsurface_commit(sub);
2797 return;
2798 }
2799
2800 weston_surface_commit(surface);
2801
2802 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2803 if (sub->surface != surface)
2804 weston_subsurface_parent_commit(sub, 0);
2805 }
2806}
2807
2808static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002809surface_set_buffer_transform(struct wl_client *client,
2810 struct wl_resource *resource, int transform)
2811{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002812 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002813
Jonny Lamba55f1392014-05-30 12:07:15 +02002814 /* if wl_output.transform grows more members this will need to be updated. */
2815 if (transform < 0 ||
2816 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2817 wl_resource_post_error(resource,
2818 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2819 "buffer transform must be a valid transform "
2820 "('%d' specified)", transform);
2821 return;
2822 }
2823
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002824 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002825 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002826}
2827
Alexander Larsson4ea95522013-05-22 14:41:37 +02002828static void
2829surface_set_buffer_scale(struct wl_client *client,
2830 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002831 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002832{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002833 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002834
Jonny Lamba55f1392014-05-30 12:07:15 +02002835 if (scale < 1) {
2836 wl_resource_post_error(resource,
2837 WL_SURFACE_ERROR_INVALID_SCALE,
2838 "buffer scale must be at least one "
2839 "('%d' specified)", scale);
2840 return;
2841 }
2842
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002843 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002844 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002845}
2846
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002847static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002848 surface_destroy,
2849 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002850 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002851 surface_frame,
2852 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002853 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002854 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002855 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06002856 surface_set_buffer_scale,
2857 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002858};
2859
2860static void
2861compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002862 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002863{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002864 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002865 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002866
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002867 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002868 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002869 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002870 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002871 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002872
Jason Ekstranda85118c2013-06-27 20:17:02 -05002873 surface->resource =
2874 wl_resource_create(client, &wl_surface_interface,
2875 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002876 if (surface->resource == NULL) {
2877 weston_surface_destroy(surface);
2878 wl_resource_post_no_memory(resource);
2879 return;
2880 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002881 wl_resource_set_implementation(surface->resource, &surface_interface,
2882 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002883
2884 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002885}
2886
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002887static void
2888destroy_region(struct wl_resource *resource)
2889{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002890 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002891
2892 pixman_region32_fini(&region->region);
2893 free(region);
2894}
2895
2896static void
2897region_destroy(struct wl_client *client, struct wl_resource *resource)
2898{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002899 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002900}
2901
2902static void
2903region_add(struct wl_client *client, struct wl_resource *resource,
2904 int32_t x, int32_t y, int32_t width, int32_t height)
2905{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002906 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002907
2908 pixman_region32_union_rect(&region->region, &region->region,
2909 x, y, width, height);
2910}
2911
2912static void
2913region_subtract(struct wl_client *client, struct wl_resource *resource,
2914 int32_t x, int32_t y, int32_t width, int32_t height)
2915{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002916 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002917 pixman_region32_t rect;
2918
2919 pixman_region32_init_rect(&rect, x, y, width, height);
2920 pixman_region32_subtract(&region->region, &region->region, &rect);
2921 pixman_region32_fini(&rect);
2922}
2923
2924static const struct wl_region_interface region_interface = {
2925 region_destroy,
2926 region_add,
2927 region_subtract
2928};
2929
2930static void
2931compositor_create_region(struct wl_client *client,
2932 struct wl_resource *resource, uint32_t id)
2933{
2934 struct weston_region *region;
2935
2936 region = malloc(sizeof *region);
2937 if (region == NULL) {
2938 wl_resource_post_no_memory(resource);
2939 return;
2940 }
2941
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002942 pixman_region32_init(&region->region);
2943
Jason Ekstranda85118c2013-06-27 20:17:02 -05002944 region->resource =
2945 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002946 if (region->resource == NULL) {
2947 free(region);
2948 wl_resource_post_no_memory(resource);
2949 return;
2950 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002951 wl_resource_set_implementation(region->resource, &region_interface,
2952 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002953}
2954
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002955static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002956 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002957 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002958};
2959
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002960static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002961weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2962{
2963 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002964
Jason Ekstrand7b982072014-05-20 14:33:03 -05002965 weston_surface_commit_state(surface, &sub->cached);
2966 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002967
2968 weston_surface_commit_subsurface_order(surface);
2969
2970 weston_surface_schedule_repaint(surface);
2971
Jason Ekstrand7b982072014-05-20 14:33:03 -05002972 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002973}
2974
2975static void
2976weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2977{
2978 struct weston_surface *surface = sub->surface;
2979
2980 /*
2981 * If this commit would cause the surface to move by the
2982 * attach(dx, dy) parameters, the old damage region must be
2983 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05002984 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002985 */
Derek Foreman152254b2015-11-26 14:17:48 -06002986 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002987 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06002988 pixman_region32_union(&sub->cached.damage_surface,
2989 &sub->cached.damage_surface,
2990 &surface->pending.damage_surface);
2991 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002992
2993 if (surface->pending.newly_attached) {
2994 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002995 weston_surface_state_set_buffer(&sub->cached,
2996 surface->pending.buffer);
2997 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002998 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002999 weston_presentation_feedback_discard_list(
3000 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003001 }
3002 sub->cached.sx += surface->pending.sx;
3003 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003004
Derek Foreman152254b2015-11-26 14:17:48 -06003005 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3006
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003007 sub->cached.buffer_viewport.changed |=
3008 surface->pending.buffer_viewport.changed;
3009 sub->cached.buffer_viewport.buffer =
3010 surface->pending.buffer_viewport.buffer;
3011 sub->cached.buffer_viewport.surface =
3012 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003013
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003014 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003015
3016 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3017
3018 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3019
3020 wl_list_insert_list(&sub->cached.frame_callback_list,
3021 &surface->pending.frame_callback_list);
3022 wl_list_init(&surface->pending.frame_callback_list);
3023
Pekka Paalanen133e4392014-09-23 22:08:46 -04003024 wl_list_insert_list(&sub->cached.feedback_list,
3025 &surface->pending.feedback_list);
3026 wl_list_init(&surface->pending.feedback_list);
3027
Jason Ekstrand7b982072014-05-20 14:33:03 -05003028 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003029}
3030
Derek Foreman280e7dd2014-10-03 13:13:42 -05003031static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003032weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3033{
3034 while (sub) {
3035 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003036 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003037
3038 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003039 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003040
3041 sub = weston_surface_to_subsurface(sub->parent);
3042 }
3043
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003044 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003045}
3046
3047static void
3048weston_subsurface_commit(struct weston_subsurface *sub)
3049{
3050 struct weston_surface *surface = sub->surface;
3051 struct weston_subsurface *tmp;
3052
3053 /* Recursive check for effectively synchronized. */
3054 if (weston_subsurface_is_synchronized(sub)) {
3055 weston_subsurface_commit_to_cache(sub);
3056 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003057 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003058 /* flush accumulated state from cache */
3059 weston_subsurface_commit_to_cache(sub);
3060 weston_subsurface_commit_from_cache(sub);
3061 } else {
3062 weston_surface_commit(surface);
3063 }
3064
3065 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3066 if (tmp->surface != surface)
3067 weston_subsurface_parent_commit(tmp, 0);
3068 }
3069 }
3070}
3071
3072static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003073weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003074{
3075 struct weston_surface *surface = sub->surface;
3076 struct weston_subsurface *tmp;
3077
Pekka Paalanene67858b2013-04-25 13:57:42 +03003078 /* From now on, commit_from_cache the whole sub-tree, regardless of
3079 * the synchronized mode of each child. This sub-surface or some
3080 * of its ancestors were synchronized, so we are synchronized
3081 * all the way down.
3082 */
3083
Jason Ekstrand7b982072014-05-20 14:33:03 -05003084 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003085 weston_subsurface_commit_from_cache(sub);
3086
3087 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3088 if (tmp->surface != surface)
3089 weston_subsurface_parent_commit(tmp, 1);
3090 }
3091}
3092
3093static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003094weston_subsurface_parent_commit(struct weston_subsurface *sub,
3095 int parent_is_synchronized)
3096{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003097 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003098 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003099 wl_list_for_each(view, &sub->surface->views, surface_link)
3100 weston_view_set_position(view,
3101 sub->position.x,
3102 sub->position.y);
3103
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003104 sub->position.set = 0;
3105 }
3106
3107 if (parent_is_synchronized || sub->synchronized)
3108 weston_subsurface_synchronized_commit(sub);
3109}
3110
Pekka Paalanen8274d902014-08-06 19:36:51 +03003111static int
3112subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3113{
3114 return snprintf(buf, len, "sub-surface");
3115}
3116
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003117static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003118subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003119{
3120 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003121 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003122
Jason Ekstranda7af7042013-10-12 22:38:11 -05003123 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003124 weston_view_set_position(view,
3125 view->geometry.x + dx,
3126 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003127
3128 /* No need to check parent mappedness, because if parent is not
3129 * mapped, parent is not in a visible layer, so this sub-surface
3130 * will not be drawn either.
3131 */
3132 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003133 struct weston_output *output;
3134
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003135 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003136 * because that would call it also for the parent surface,
3137 * which might not be mapped yet. That would lead to
3138 * inconsistent state, where the window could never be
3139 * mapped.
3140 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003141 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003142 * weston_surface_is_mapped() return true, so that when the
3143 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003144 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003145 */
3146 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003147 output = container_of(compositor->output_list.next,
3148 struct weston_output, link);
3149
3150 surface->output = output;
Bryce Harrington89324ce2015-12-23 18:38:07 -08003151 weston_surface_update_output_mask(surface, 1u << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003152 }
3153}
3154
3155static struct weston_subsurface *
3156weston_surface_to_subsurface(struct weston_surface *surface)
3157{
3158 if (surface->configure == subsurface_configure)
3159 return surface->configure_private;
3160
3161 return NULL;
3162}
3163
Pekka Paalanen01388e22013-04-25 13:57:44 +03003164WL_EXPORT struct weston_surface *
3165weston_surface_get_main_surface(struct weston_surface *surface)
3166{
3167 struct weston_subsurface *sub;
3168
3169 while (surface && (sub = weston_surface_to_subsurface(surface)))
3170 surface = sub->parent;
3171
3172 return surface;
3173}
3174
Pekka Paalanen50b67472014-10-01 15:02:41 +03003175WL_EXPORT int
3176weston_surface_set_role(struct weston_surface *surface,
3177 const char *role_name,
3178 struct wl_resource *error_resource,
3179 uint32_t error_code)
3180{
3181 assert(role_name);
3182
3183 if (surface->role_name == NULL ||
3184 surface->role_name == role_name ||
3185 strcmp(surface->role_name, role_name) == 0) {
3186 surface->role_name = role_name;
3187
3188 return 0;
3189 }
3190
3191 wl_resource_post_error(error_resource, error_code,
3192 "Cannot assign role %s to wl_surface@%d,"
3193 " already has role %s\n",
3194 role_name,
3195 wl_resource_get_id(surface->resource),
3196 surface->role_name);
3197 return -1;
3198}
3199
Pekka Paalanen8274d902014-08-06 19:36:51 +03003200WL_EXPORT void
3201weston_surface_set_label_func(struct weston_surface *surface,
3202 int (*desc)(struct weston_surface *,
3203 char *, size_t))
3204{
3205 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003206 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003207}
3208
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003209/** Get the size of surface contents
3210 *
3211 * \param surface The surface to query.
3212 * \param width Returns the width of raw contents.
3213 * \param height Returns the height of raw contents.
3214 *
3215 * Retrieves the raw surface content size in pixels for the given surface.
3216 * This is the whole content size in buffer pixels. If the surface
3217 * has no content or the renderer does not implement this feature,
3218 * zeroes are returned.
3219 *
3220 * This function is used to determine the buffer size needed for
3221 * a weston_surface_copy_content() call.
3222 */
3223WL_EXPORT void
3224weston_surface_get_content_size(struct weston_surface *surface,
3225 int *width, int *height)
3226{
3227 struct weston_renderer *rer = surface->compositor->renderer;
3228
3229 if (!rer->surface_get_content_size) {
3230 *width = 0;
3231 *height = 0;
3232 return;
3233 }
3234
3235 rer->surface_get_content_size(surface, width, height);
3236}
3237
3238/** Copy surface contents to system memory.
3239 *
3240 * \param surface The surface to copy from.
3241 * \param target Pointer to the target memory buffer.
3242 * \param size Size of the target buffer in bytes.
3243 * \param src_x X location on contents to copy from.
3244 * \param src_y Y location on contents to copy from.
3245 * \param width Width in pixels of the area to copy.
3246 * \param height Height in pixels of the area to copy.
3247 * \return 0 for success, -1 for failure.
3248 *
3249 * Surface contents are maintained by the renderer. They can be in a
3250 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3251 * else.
3252 *
3253 * Surface contents are copied into memory pointed to by target,
3254 * which has size bytes of space available. The target memory
3255 * may be larger than needed, but being smaller returns an error.
3256 * The extra bytes in target may or may not be written; their content is
3257 * unspecified. Size must be large enough to hold the image.
3258 *
3259 * The image in the target memory will be arranged in rows from
3260 * top to bottom, and pixels on a row from left to right. The pixel
3261 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3262 * width * 4.
3263 *
3264 * Parameters src_x and src_y define the upper-left corner in buffer
3265 * coordinates (pixels) to copy from. Parameters width and height
3266 * define the size of the area to copy in pixels.
3267 *
3268 * The rectangle defined by src_x, src_y, width, height must fit in
3269 * the surface contents. Otherwise an error is returned.
3270 *
3271 * Use surface_get_data_size to determine the content size; the
3272 * needed target buffer size and rectangle limits.
3273 *
3274 * CURRENT IMPLEMENTATION RESTRICTIONS:
3275 * - the machine must be little-endian due to Pixman formats.
3276 *
3277 * NOTE: Pixman formats are premultiplied.
3278 */
3279WL_EXPORT int
3280weston_surface_copy_content(struct weston_surface *surface,
3281 void *target, size_t size,
3282 int src_x, int src_y,
3283 int width, int height)
3284{
3285 struct weston_renderer *rer = surface->compositor->renderer;
3286 int cw, ch;
3287 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3288
3289 if (!rer->surface_copy_content)
3290 return -1;
3291
3292 weston_surface_get_content_size(surface, &cw, &ch);
3293
3294 if (src_x < 0 || src_y < 0)
3295 return -1;
3296
3297 if (width <= 0 || height <= 0)
3298 return -1;
3299
3300 if (src_x + width > cw || src_y + height > ch)
3301 return -1;
3302
3303 if (width * bytespp * height > size)
3304 return -1;
3305
3306 return rer->surface_copy_content(surface, target, size,
3307 src_x, src_y, width, height);
3308}
3309
Pekka Paalanene67858b2013-04-25 13:57:42 +03003310static void
3311subsurface_set_position(struct wl_client *client,
3312 struct wl_resource *resource, int32_t x, int32_t y)
3313{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003314 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003315
3316 if (!sub)
3317 return;
3318
3319 sub->position.x = x;
3320 sub->position.y = y;
3321 sub->position.set = 1;
3322}
3323
3324static struct weston_subsurface *
3325subsurface_from_surface(struct weston_surface *surface)
3326{
3327 struct weston_subsurface *sub;
3328
3329 sub = weston_surface_to_subsurface(surface);
3330 if (sub)
3331 return sub;
3332
3333 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3334 if (sub->surface == surface)
3335 return sub;
3336
3337 return NULL;
3338}
3339
3340static struct weston_subsurface *
3341subsurface_sibling_check(struct weston_subsurface *sub,
3342 struct weston_surface *surface,
3343 const char *request)
3344{
3345 struct weston_subsurface *sibling;
3346
3347 sibling = subsurface_from_surface(surface);
3348
3349 if (!sibling) {
3350 wl_resource_post_error(sub->resource,
3351 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3352 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003353 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003354 return NULL;
3355 }
3356
3357 if (sibling->parent != sub->parent) {
3358 wl_resource_post_error(sub->resource,
3359 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3360 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003361 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003362 return NULL;
3363 }
3364
3365 return sibling;
3366}
3367
3368static void
3369subsurface_place_above(struct wl_client *client,
3370 struct wl_resource *resource,
3371 struct wl_resource *sibling_resource)
3372{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003373 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003374 struct weston_surface *surface =
3375 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003376 struct weston_subsurface *sibling;
3377
3378 if (!sub)
3379 return;
3380
3381 sibling = subsurface_sibling_check(sub, surface, "place_above");
3382 if (!sibling)
3383 return;
3384
3385 wl_list_remove(&sub->parent_link_pending);
3386 wl_list_insert(sibling->parent_link_pending.prev,
3387 &sub->parent_link_pending);
3388}
3389
3390static void
3391subsurface_place_below(struct wl_client *client,
3392 struct wl_resource *resource,
3393 struct wl_resource *sibling_resource)
3394{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003395 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003396 struct weston_surface *surface =
3397 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003398 struct weston_subsurface *sibling;
3399
3400 if (!sub)
3401 return;
3402
3403 sibling = subsurface_sibling_check(sub, surface, "place_below");
3404 if (!sibling)
3405 return;
3406
3407 wl_list_remove(&sub->parent_link_pending);
3408 wl_list_insert(&sibling->parent_link_pending,
3409 &sub->parent_link_pending);
3410}
3411
3412static void
3413subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3414{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003415 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003416
3417 if (sub)
3418 sub->synchronized = 1;
3419}
3420
3421static void
3422subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3423{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003424 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003425
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003426 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003427 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003428
3429 /* If sub became effectively desynchronized, flush. */
3430 if (!weston_subsurface_is_synchronized(sub))
3431 weston_subsurface_synchronized_commit(sub);
3432 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003433}
3434
3435static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003436weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3437{
3438 wl_list_remove(&sub->parent_link);
3439 wl_list_remove(&sub->parent_link_pending);
3440 wl_list_remove(&sub->parent_destroy_listener.link);
3441 sub->parent = NULL;
3442}
3443
3444static void
3445weston_subsurface_destroy(struct weston_subsurface *sub);
3446
3447static void
3448subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3449{
3450 struct weston_subsurface *sub =
3451 container_of(listener, struct weston_subsurface,
3452 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003453 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003454
3455 /* The protocol object (wl_resource) is left inert. */
3456 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003457 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003458
3459 weston_subsurface_destroy(sub);
3460}
3461
3462static void
3463subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3464{
3465 struct weston_subsurface *sub =
3466 container_of(listener, struct weston_subsurface,
3467 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003468 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003469 assert(sub->surface != sub->parent);
3470
3471 if (weston_surface_is_mapped(sub->surface))
3472 weston_surface_unmap(sub->surface);
3473
3474 weston_subsurface_unlink_parent(sub);
3475}
3476
3477static void
3478subsurface_resource_destroy(struct wl_resource *resource)
3479{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003480 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003481
3482 if (sub)
3483 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003484}
3485
3486static void
3487subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3488{
3489 wl_resource_destroy(resource);
3490}
3491
3492static void
3493weston_subsurface_link_parent(struct weston_subsurface *sub,
3494 struct weston_surface *parent)
3495{
3496 sub->parent = parent;
3497 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003498 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003499 &sub->parent_destroy_listener);
3500
3501 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3502 wl_list_insert(&parent->subsurface_list_pending,
3503 &sub->parent_link_pending);
3504}
3505
3506static void
3507weston_subsurface_link_surface(struct weston_subsurface *sub,
3508 struct weston_surface *surface)
3509{
3510 sub->surface = surface;
3511 sub->surface_destroy_listener.notify =
3512 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003513 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003514 &sub->surface_destroy_listener);
3515}
3516
3517static void
3518weston_subsurface_destroy(struct weston_subsurface *sub)
3519{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003520 struct weston_view *view, *next;
3521
Pekka Paalanene67858b2013-04-25 13:57:42 +03003522 assert(sub->surface);
3523
3524 if (sub->resource) {
3525 assert(weston_surface_to_subsurface(sub->surface) == sub);
3526 assert(sub->parent_destroy_listener.notify ==
3527 subsurface_handle_parent_destroy);
3528
George Kiagiadakised04d382014-06-13 18:10:26 +02003529 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3530 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003531 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003532 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003533
Pekka Paalanene67858b2013-04-25 13:57:42 +03003534 if (sub->parent)
3535 weston_subsurface_unlink_parent(sub);
3536
Jason Ekstrand7b982072014-05-20 14:33:03 -05003537 weston_surface_state_fini(&sub->cached);
3538 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003539
3540 sub->surface->configure = NULL;
3541 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003542 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003543 } else {
3544 /* the dummy weston_subsurface for the parent itself */
3545 assert(sub->parent_destroy_listener.notify == NULL);
3546 wl_list_remove(&sub->parent_link);
3547 wl_list_remove(&sub->parent_link_pending);
3548 }
3549
3550 wl_list_remove(&sub->surface_destroy_listener.link);
3551 free(sub);
3552}
3553
3554static const struct wl_subsurface_interface subsurface_implementation = {
3555 subsurface_destroy,
3556 subsurface_set_position,
3557 subsurface_place_above,
3558 subsurface_place_below,
3559 subsurface_set_sync,
3560 subsurface_set_desync
3561};
3562
3563static struct weston_subsurface *
3564weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3565 struct weston_surface *parent)
3566{
3567 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003568 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003569
Bryce Harringtonde16d892014-11-20 22:21:57 -08003570 sub = zalloc(sizeof *sub);
3571 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003572 return NULL;
3573
Jason Ekstranda7af7042013-10-12 22:38:11 -05003574 wl_list_init(&sub->unused_views);
3575
Jason Ekstranda85118c2013-06-27 20:17:02 -05003576 sub->resource =
3577 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003578 if (!sub->resource) {
3579 free(sub);
3580 return NULL;
3581 }
3582
Jason Ekstranda85118c2013-06-27 20:17:02 -05003583 wl_resource_set_implementation(sub->resource,
3584 &subsurface_implementation,
3585 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003586 weston_subsurface_link_surface(sub, surface);
3587 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003588 weston_surface_state_init(&sub->cached);
3589 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003590 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003591
3592 return sub;
3593}
3594
3595/* Create a dummy subsurface for having the parent itself in its
3596 * sub-surface lists. Makes stacking order manipulation easy.
3597 */
3598static struct weston_subsurface *
3599weston_subsurface_create_for_parent(struct weston_surface *parent)
3600{
3601 struct weston_subsurface *sub;
3602
Bryce Harringtonde16d892014-11-20 22:21:57 -08003603 sub = zalloc(sizeof *sub);
3604 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003605 return NULL;
3606
3607 weston_subsurface_link_surface(sub, parent);
3608 sub->parent = parent;
3609 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3610 wl_list_insert(&parent->subsurface_list_pending,
3611 &sub->parent_link_pending);
3612
3613 return sub;
3614}
3615
3616static void
3617subcompositor_get_subsurface(struct wl_client *client,
3618 struct wl_resource *resource,
3619 uint32_t id,
3620 struct wl_resource *surface_resource,
3621 struct wl_resource *parent_resource)
3622{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003623 struct weston_surface *surface =
3624 wl_resource_get_user_data(surface_resource);
3625 struct weston_surface *parent =
3626 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627 struct weston_subsurface *sub;
3628 static const char where[] = "get_subsurface: wl_subsurface@";
3629
3630 if (surface == parent) {
3631 wl_resource_post_error(resource,
3632 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3633 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003634 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003635 return;
3636 }
3637
3638 if (weston_surface_to_subsurface(surface)) {
3639 wl_resource_post_error(resource,
3640 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3641 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003642 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003643 return;
3644 }
3645
Pekka Paalanen50b67472014-10-01 15:02:41 +03003646 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3647 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003648 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003649
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003650 if (weston_surface_get_main_surface(parent) == surface) {
3651 wl_resource_post_error(resource,
3652 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3653 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003654 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003655 return;
3656 }
3657
Pekka Paalanene67858b2013-04-25 13:57:42 +03003658 /* make sure the parent is in its own list */
3659 if (wl_list_empty(&parent->subsurface_list)) {
3660 if (!weston_subsurface_create_for_parent(parent)) {
3661 wl_resource_post_no_memory(resource);
3662 return;
3663 }
3664 }
3665
3666 sub = weston_subsurface_create(id, surface, parent);
3667 if (!sub) {
3668 wl_resource_post_no_memory(resource);
3669 return;
3670 }
3671
3672 surface->configure = subsurface_configure;
3673 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003674 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003675}
3676
3677static void
3678subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3679{
3680 wl_resource_destroy(resource);
3681}
3682
3683static const struct wl_subcompositor_interface subcompositor_interface = {
3684 subcompositor_destroy,
3685 subcompositor_get_subsurface
3686};
3687
3688static void
3689bind_subcompositor(struct wl_client *client,
3690 void *data, uint32_t version, uint32_t id)
3691{
3692 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003693 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003694
Jason Ekstranda85118c2013-06-27 20:17:02 -05003695 resource =
3696 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003697 if (resource == NULL) {
3698 wl_client_post_no_memory(client);
3699 return;
3700 }
3701 wl_resource_set_implementation(resource, &subcompositor_interface,
3702 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003703}
3704
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003705/** Set a DPMS mode on all of the compositor's outputs
3706 *
3707 * \param compositor The compositor instance
3708 * \param state The DPMS state the outputs will be set to
3709 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003710static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003711weston_compositor_dpms(struct weston_compositor *compositor,
3712 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003713{
3714 struct weston_output *output;
3715
3716 wl_list_for_each(output, &compositor->output_list, link)
3717 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003718 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003719}
3720
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003721/** Restores the compositor to active status
3722 *
3723 * \param compositor The compositor instance
3724 *
3725 * If the compositor was in a sleeping mode, all outputs are powered
3726 * back on via DPMS. Otherwise if the compositor was inactive
3727 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3728 * signal will fire.
3729 *
3730 * Restarts the idle timer.
3731 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003732WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003733weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003734{
Neil Roberts8b62e202013-09-30 13:14:47 +01003735 uint32_t old_state = compositor->state;
3736
3737 /* The state needs to be changed before emitting the wake
3738 * signal because that may try to schedule a repaint which
3739 * will not work if the compositor is still sleeping */
3740 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3741
3742 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003743 case WESTON_COMPOSITOR_SLEEPING:
3744 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3745 /* fall through */
3746 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003747 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003748 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003749 /* fall through */
3750 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003751 wl_event_source_timer_update(compositor->idle_source,
3752 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003753 }
3754}
3755
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003756/** Turns off rendering and frame events for the compositor.
3757 *
3758 * \param compositor The compositor instance
3759 *
3760 * This is used for example to prevent further rendering while the
3761 * compositor is shutting down.
3762 *
3763 * \note When offscreen state is entered, outputs will be powered
3764 * back on if they were sleeping (in DPMS off mode), even though
3765 * no rendering will be performed.
3766 *
3767 * Stops the idle timer.
3768 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003769WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003770weston_compositor_offscreen(struct weston_compositor *compositor)
3771{
3772 switch (compositor->state) {
3773 case WESTON_COMPOSITOR_OFFSCREEN:
3774 return;
3775 case WESTON_COMPOSITOR_SLEEPING:
3776 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3777 /* fall through */
3778 default:
3779 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3780 wl_event_source_timer_update(compositor->idle_source, 0);
3781 }
3782}
3783
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003784/** Powers down all attached output devices
3785 *
3786 * \param compositor The compositor instance
3787 *
3788 * Causes rendering to the outputs to cease, and no frame events to be
3789 * sent. Only powers down the outputs if the compositor is not already
3790 * in sleep mode.
3791 *
3792 * Stops the idle timer.
3793 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003794WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003795weston_compositor_sleep(struct weston_compositor *compositor)
3796{
3797 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3798 return;
3799
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003800 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003801 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3802 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3803}
3804
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003805/** Sets compositor to idle mode
3806 *
3807 * \param data The compositor instance
3808 *
3809 * This is called when the idle timer fires. Once the compositor is in
3810 * idle mode it requires a wake action (e.g. via
3811 * weston_compositor_wake()) to restore it. The compositor's
3812 * idle_signal will be triggered when the idle event occurs.
3813 *
3814 * Idleness can be inhibited by setting the compositor's idle_inhibit
3815 * property.
3816 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003817static int
3818idle_handler(void *data)
3819{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003820 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003821
3822 if (compositor->idle_inhibit)
3823 return 1;
3824
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003825 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003826 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003827
3828 return 1;
3829}
3830
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003831WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003832weston_plane_init(struct weston_plane *plane,
3833 struct weston_compositor *ec,
3834 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003835{
3836 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003837 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003838 plane->x = x;
3839 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003840 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003841
3842 /* Init the link so that the call to wl_list_remove() when releasing
3843 * the plane without ever stacking doesn't lead to a crash */
3844 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003845}
3846
3847WL_EXPORT void
3848weston_plane_release(struct weston_plane *plane)
3849{
Xiong Zhang97116532013-10-23 13:58:31 +08003850 struct weston_view *view;
3851
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003852 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003853 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003854
Xiong Zhang97116532013-10-23 13:58:31 +08003855 wl_list_for_each(view, &plane->compositor->view_list, link) {
3856 if (view->plane == plane)
3857 view->plane = NULL;
3858 }
3859
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003860 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003861}
3862
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003863WL_EXPORT void
3864weston_compositor_stack_plane(struct weston_compositor *ec,
3865 struct weston_plane *plane,
3866 struct weston_plane *above)
3867{
3868 if (above)
3869 wl_list_insert(above->link.prev, &plane->link);
3870 else
3871 wl_list_insert(&ec->plane_list, &plane->link);
3872}
3873
Casey Dahlin9074db52012-04-19 22:50:09 -04003874static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003875{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003876 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003877}
3878
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003879static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003880bind_output(struct wl_client *client,
3881 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003882{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003883 struct weston_output *output = data;
3884 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003885 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003886
Jason Ekstranda85118c2013-06-27 20:17:02 -05003887 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06003888 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003889 if (resource == NULL) {
3890 wl_client_post_no_memory(client);
3891 return;
3892 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003893
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003894 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003895 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003896
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003897 wl_output_send_geometry(resource,
3898 output->x,
3899 output->y,
3900 output->mm_width,
3901 output->mm_height,
3902 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003903 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003904 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003905 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003906 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003907 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003908
3909 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003910 wl_output_send_mode(resource,
3911 mode->flags,
3912 mode->width,
3913 mode->height,
3914 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003915 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003916
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003917 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003918 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003919}
3920
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003921/* Move other outputs when one is removed so the space remains contiguos. */
3922static void
3923weston_compositor_remove_output(struct weston_compositor *compositor,
3924 struct weston_output *remove_output)
3925{
3926 struct weston_output *output;
3927 int offset = 0;
3928
3929 wl_list_for_each(output, &compositor->output_list, link) {
3930 if (output == remove_output) {
3931 offset = output->width;
3932 continue;
3933 }
3934
3935 if (offset > 0) {
3936 weston_output_move(output,
3937 output->x - offset, output->y);
3938 output->dirty = 1;
3939 }
3940 }
3941}
3942
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003943WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003944weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003945{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003946 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003947 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003948
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003949 output->destroying = 1;
3950
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003951 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08003952 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003953 weston_view_assign_output(view);
3954 }
3955
Pekka Paalanen0513a952014-05-21 16:17:27 +03003956 wl_event_source_remove(output->repaint_timer);
3957
Pekka Paalanen133e4392014-09-23 22:08:46 -04003958 weston_presentation_feedback_discard_list(&output->feedback_list);
3959
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003960 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003961 wl_list_remove(&output->link);
3962
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003963 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003964 wl_signal_emit(&output->destroy_signal, output);
3965
Richard Hughesafe690c2013-05-02 10:10:04 +01003966 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003967 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003968 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08003969 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003970
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003971 wl_resource_for_each(resource, &output->resource_list) {
3972 wl_resource_set_destructor(resource, NULL);
3973 }
3974
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003975 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003976}
3977
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003978WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003979weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003980{
Scott Moreau850ca422012-05-21 15:21:25 -06003981 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003982
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003983 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003984 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003985
Scott Moreauccbf29d2012-02-22 14:21:41 -07003986 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003987 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003988 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003989 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003990 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003991 weston_matrix_scale(&output->matrix, magnification,
3992 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003993 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003994
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003995 switch (output->transform) {
3996 case WL_OUTPUT_TRANSFORM_FLIPPED:
3997 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3998 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3999 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4000 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4001 weston_matrix_scale(&output->matrix, -1, 1, 1);
4002 break;
4003 }
4004
4005 switch (output->transform) {
4006 default:
4007 case WL_OUTPUT_TRANSFORM_NORMAL:
4008 case WL_OUTPUT_TRANSFORM_FLIPPED:
4009 break;
4010 case WL_OUTPUT_TRANSFORM_90:
4011 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4012 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4013 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4014 break;
4015 case WL_OUTPUT_TRANSFORM_180:
4016 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4017 weston_matrix_translate(&output->matrix,
4018 -output->width, -output->height, 0);
4019 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4020 break;
4021 case WL_OUTPUT_TRANSFORM_270:
4022 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4023 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4024 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4025 break;
4026 }
4027
4028 if (output->current_scale != 1)
4029 weston_matrix_scale(&output->matrix,
4030 output->current_scale,
4031 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004032
Scott Moreauccbf29d2012-02-22 14:21:41 -07004033 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004034
4035 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004036}
4037
Scott Moreau1bad5db2012-08-18 01:04:05 -06004038static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004039weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004040{
4041 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004042 output->native_scale = scale;
4043 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004044
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004045 convert_size_by_transform_scale(&output->width, &output->height,
4046 output->current_mode->width,
4047 output->current_mode->height,
4048 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004049}
4050
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004051static void
4052weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004053{
4054 output->x = x;
4055 output->y = y;
4056
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004057 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004058 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004059 output->width,
4060 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004061}
4062
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004063WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004064weston_output_move(struct weston_output *output, int x, int y)
4065{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004066 struct wl_resource *resource;
4067
4068 output->move_x = x - output->x;
4069 output->move_y = y - output->y;
4070
4071 if (output->move_x == 0 && output->move_y == 0)
4072 return;
4073
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004074 weston_output_init_geometry(output, x, y);
4075
4076 output->dirty = 1;
4077
4078 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004079 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004080
4081 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004082 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004083 wl_output_send_geometry(resource,
4084 output->x,
4085 output->y,
4086 output->mm_width,
4087 output->mm_height,
4088 output->subpixel,
4089 output->make,
4090 output->model,
4091 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004092
FORT David8a120692016-04-26 23:34:06 +02004093 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004094 wl_output_send_done(resource);
4095 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004096}
4097
Bryce Harrington3f650b82015-12-23 11:01:58 -08004098/** Initialize a weston_output object's parameters
4099 *
4100 * \param output The weston_output object to initialize
4101 * \param c The output's compositor
4102 * \param x x coordinate for the output in global coordinate space
4103 * \param y y coordinate for the output in global coordinate space
4104 * \param mm_width Physical width of the output as reported by the backend
4105 * \param mm_height Physical height of the output as reported by the backend
4106 * \param transform Rotation of the output
4107 * \param scale Native scaling factor for the output
4108 *
4109 * Sets up the transformation, zoom, and geometry of the output using
4110 * the input properties.
4111 *
4112 * Establishes a repaint timer for the output with the relevant display
4113 * object's event loop. See output_repaint_timer_handler().
4114 *
4115 * The output is assigned an ID. Weston can support up to 32 distinct
4116 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4117 * is referred to and used to find the first available ID number, and
4118 * then this ID is marked as used in output_id_pool.
4119 *
4120 * The output is also assigned a Wayland global with the wl_output
4121 * external interface.
4122 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004123WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004124weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004125 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004126 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004127{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004128 struct wl_event_loop *loop;
4129
Bryce Harrington18e45732015-12-23 20:53:53 -08004130 /* Verify we haven't reached the limit of 32 available output IDs */
4131 assert(ffs(~c->output_id_pool) > 0);
4132
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004133 output->compositor = c;
4134 output->x = x;
4135 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004136 output->mm_width = mm_width;
4137 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004138 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004139 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004140
Alexander Larsson0b135062013-05-28 16:23:36 +02004141 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004142 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004143
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004144 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004145 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004146
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004147 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004148 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004149 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004150 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004151 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004152 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004153
Pekka Paalanen0513a952014-05-21 16:17:27 +03004154 loop = wl_display_get_event_loop(c->wl_display);
4155 output->repaint_timer = wl_event_loop_add_timer(loop,
4156 output_repaint_timer_handler, output);
4157
Bryce Harrington3f650b82015-12-23 11:01:58 -08004158 /* Invert the output id pool and look for the lowest numbered
4159 * switch (the least significant bit). Take that bit's position
4160 * as our ID, and mark it used in the compositor's output_id_pool.
4161 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004162 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004163 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004164
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004165 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004166 wl_global_create(c->wl_display, &wl_output_interface, 2,
4167 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004168}
4169
4170/** Adds an output to the compositor's output list and
4171 * send the compositor's output_created signal.
4172 *
4173 * \param compositor The compositor instance.
4174 * \param output The output to be added.
4175 */
4176WL_EXPORT void
4177weston_compositor_add_output(struct weston_compositor *compositor,
4178 struct weston_output *output)
4179{
4180 wl_list_insert(compositor->output_list.prev, &output->link);
4181 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004182}
4183
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004184WL_EXPORT void
4185weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004186 double device_x, double device_y,
4187 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004188{
Derek Foreman0f679412014-10-02 13:41:17 -05004189 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004190 device_x,
4191 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004192 0.0,
4193 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004194
Derek Foreman67a18b92015-03-24 11:36:14 -05004195 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004196
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004197 *x = p.f[0] / p.f[3];
4198 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004199}
4200
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004201static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004202destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004203{
Jonny Lamb74130762013-11-26 18:19:46 +01004204 struct weston_surface *surface =
4205 wl_resource_get_user_data(resource);
4206
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004207 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004208 surface->pending.buffer_viewport.buffer.src_width =
4209 wl_fixed_from_int(-1);
4210 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004211 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004212}
4213
4214static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004215viewport_destroy(struct wl_client *client,
4216 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004217{
4218 wl_resource_destroy(resource);
4219}
4220
4221static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004222viewport_set(struct wl_client *client,
4223 struct wl_resource *resource,
4224 wl_fixed_t src_x,
4225 wl_fixed_t src_y,
4226 wl_fixed_t src_width,
4227 wl_fixed_t src_height,
4228 int32_t dst_width,
4229 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004230{
Jonny Lamb74130762013-11-26 18:19:46 +01004231 struct weston_surface *surface =
4232 wl_resource_get_user_data(resource);
4233
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004234 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004235
Jonny Lamb8ae35902013-11-26 18:19:45 +01004236 if (wl_fixed_to_double(src_width) < 0 ||
4237 wl_fixed_to_double(src_height) < 0) {
4238 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004239 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004240 "source dimensions must be non-negative (%fx%f)",
4241 wl_fixed_to_double(src_width),
4242 wl_fixed_to_double(src_height));
4243 return;
4244 }
4245
4246 if (dst_width <= 0 || dst_height <= 0) {
4247 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004248 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004249 "destination dimensions must be positive (%dx%d)",
4250 dst_width, dst_height);
4251 return;
4252 }
4253
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004254 surface->pending.buffer_viewport.buffer.src_x = src_x;
4255 surface->pending.buffer_viewport.buffer.src_y = src_y;
4256 surface->pending.buffer_viewport.buffer.src_width = src_width;
4257 surface->pending.buffer_viewport.buffer.src_height = src_height;
4258 surface->pending.buffer_viewport.surface.width = dst_width;
4259 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004260 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004261}
4262
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004263static void
4264viewport_set_source(struct wl_client *client,
4265 struct wl_resource *resource,
4266 wl_fixed_t src_x,
4267 wl_fixed_t src_y,
4268 wl_fixed_t src_width,
4269 wl_fixed_t src_height)
4270{
4271 struct weston_surface *surface =
4272 wl_resource_get_user_data(resource);
4273
4274 assert(surface->viewport_resource != NULL);
4275
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004276 if (src_width == wl_fixed_from_int(-1) &&
4277 src_height == wl_fixed_from_int(-1)) {
4278 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004279 surface->pending.buffer_viewport.buffer.src_width =
4280 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004281 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004282 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004283 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004284
4285 if (src_width <= 0 || src_height <= 0) {
4286 wl_resource_post_error(resource,
4287 WL_VIEWPORT_ERROR_BAD_VALUE,
4288 "source size must be positive (%fx%f)",
4289 wl_fixed_to_double(src_width),
4290 wl_fixed_to_double(src_height));
4291 return;
4292 }
4293
4294 surface->pending.buffer_viewport.buffer.src_x = src_x;
4295 surface->pending.buffer_viewport.buffer.src_y = src_y;
4296 surface->pending.buffer_viewport.buffer.src_width = src_width;
4297 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004298 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004299}
4300
4301static void
4302viewport_set_destination(struct wl_client *client,
4303 struct wl_resource *resource,
4304 int32_t dst_width,
4305 int32_t dst_height)
4306{
4307 struct weston_surface *surface =
4308 wl_resource_get_user_data(resource);
4309
4310 assert(surface->viewport_resource != NULL);
4311
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004312 if (dst_width == -1 && dst_height == -1) {
4313 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004314 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004315 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004316 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004317 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004318
4319 if (dst_width <= 0 || dst_height <= 0) {
4320 wl_resource_post_error(resource,
4321 WL_VIEWPORT_ERROR_BAD_VALUE,
4322 "destination size must be positive (%dx%d)",
4323 dst_width, dst_height);
4324 return;
4325 }
4326
4327 surface->pending.buffer_viewport.surface.width = dst_width;
4328 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004329 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004330}
4331
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004332static const struct wl_viewport_interface viewport_interface = {
4333 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004334 viewport_set,
4335 viewport_set_source,
4336 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004337};
4338
4339static void
4340scaler_destroy(struct wl_client *client,
4341 struct wl_resource *resource)
4342{
4343 wl_resource_destroy(resource);
4344}
4345
4346static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004347scaler_get_viewport(struct wl_client *client,
4348 struct wl_resource *scaler,
4349 uint32_t id,
4350 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004351{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004352 int version = wl_resource_get_version(scaler);
4353 struct weston_surface *surface =
4354 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004355 struct wl_resource *resource;
4356
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004357 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004358 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004359 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4360 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004361 return;
4362 }
4363
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004364 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004365 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004366 if (resource == NULL) {
4367 wl_client_post_no_memory(client);
4368 return;
4369 }
4370
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004371 wl_resource_set_implementation(resource, &viewport_interface,
4372 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004373
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004374 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004375}
4376
4377static const struct wl_scaler_interface scaler_interface = {
4378 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004379 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004380};
4381
4382static void
4383bind_scaler(struct wl_client *client,
4384 void *data, uint32_t version, uint32_t id)
4385{
4386 struct wl_resource *resource;
4387
4388 resource = wl_resource_create(client, &wl_scaler_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004389 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004390 if (resource == NULL) {
4391 wl_client_post_no_memory(client);
4392 return;
4393 }
4394
4395 wl_resource_set_implementation(resource, &scaler_interface,
4396 NULL, NULL);
4397}
4398
4399static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004400destroy_presentation_feedback(struct wl_resource *feedback_resource)
4401{
4402 struct weston_presentation_feedback *feedback;
4403
4404 feedback = wl_resource_get_user_data(feedback_resource);
4405
4406 wl_list_remove(&feedback->link);
4407 free(feedback);
4408}
4409
4410static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004411presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4412{
4413 wl_resource_destroy(resource);
4414}
4415
4416static void
4417presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004418 struct wl_resource *presentation_resource,
4419 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004420 uint32_t callback)
4421{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004422 struct weston_surface *surface;
4423 struct weston_presentation_feedback *feedback;
4424
4425 surface = wl_resource_get_user_data(surface_resource);
4426
Bryce Harringtonde16d892014-11-20 22:21:57 -08004427 feedback = zalloc(sizeof *feedback);
4428 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004429 goto err_calloc;
4430
4431 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004432 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004433 1, callback);
4434 if (!feedback->resource)
4435 goto err_create;
4436
4437 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4438 destroy_presentation_feedback);
4439
4440 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4441
4442 return;
4443
4444err_create:
4445 free(feedback);
4446
4447err_calloc:
4448 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004449}
4450
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004451static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004452 presentation_destroy,
4453 presentation_feedback
4454};
4455
4456static void
4457bind_presentation(struct wl_client *client,
4458 void *data, uint32_t version, uint32_t id)
4459{
4460 struct weston_compositor *compositor = data;
4461 struct wl_resource *resource;
4462
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004463 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004464 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004465 if (resource == NULL) {
4466 wl_client_post_no_memory(client);
4467 return;
4468 }
4469
4470 wl_resource_set_implementation(resource, &presentation_implementation,
4471 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004472 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004473}
4474
4475static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004476compositor_bind(struct wl_client *client,
4477 void *data, uint32_t version, uint32_t id)
4478{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004479 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004480 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004481
Jason Ekstranda85118c2013-06-27 20:17:02 -05004482 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004483 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004484 if (resource == NULL) {
4485 wl_client_post_no_memory(client);
4486 return;
4487 }
4488
4489 wl_resource_set_implementation(resource, &compositor_interface,
4490 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004491}
4492
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004493WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004494weston_environment_get_fd(const char *env)
4495{
4496 char *e, *end;
4497 int fd, flags;
4498
4499 e = getenv(env);
4500 if (!e)
4501 return -1;
4502 fd = strtol(e, &end, 0);
4503 if (*end != '\0')
4504 return -1;
4505
4506 flags = fcntl(fd, F_GETFD);
4507 if (flags == -1)
4508 return -1;
4509
4510 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4511 unsetenv(env);
4512
4513 return fd;
4514}
4515
Pekka Paalanenb5026542014-11-12 15:09:24 +02004516static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004517timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004518 uint32_t key, void *data)
4519{
4520 struct weston_compositor *compositor = data;
4521
4522 if (weston_timeline_enabled_)
4523 weston_timeline_close();
4524 else
4525 weston_timeline_open(compositor);
4526}
4527
Giulio Camuffo459137b2014-10-11 23:56:24 +03004528/** Create the compositor.
4529 *
4530 * This functions creates and initializes a compositor instance.
4531 *
4532 * \param display The Wayland display to be used.
4533 * \param user_data A pointer to an object that can later be retrieved
4534 * using the \ref weston_compositor_get_user_data function.
4535 * \return The compositor instance on success or NULL on failure.
4536 */
4537WL_EXPORT struct weston_compositor *
4538weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004539{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004540 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004541 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004542
Giulio Camuffo459137b2014-10-11 23:56:24 +03004543 ec = zalloc(sizeof *ec);
4544 if (!ec)
4545 return NULL;
4546
4547 ec->wl_display = display;
4548 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004549 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004550 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004551 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004552 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004553 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004554 wl_signal_init(&ec->idle_signal);
4555 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004556 wl_signal_init(&ec->show_input_panel_signal);
4557 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004558 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004559 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004560 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004561 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004562 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004563 wl_signal_init(&ec->session_signal);
4564 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004565
Casey Dahlin58ba1372012-04-19 22:50:08 -04004566 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004567 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004568
Derek Foreman152254b2015-11-26 14:17:48 -06004569 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004570 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004571 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004572
Giulio Camuffo954f1832014-10-11 18:27:30 +03004573 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004574 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004575 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004576
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004577 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004578 ec, bind_scaler))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004579 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004580
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004581 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004582 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004583 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004584
Jason Ekstranda7af7042013-10-12 22:38:11 -05004585 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004586 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004587 wl_list_init(&ec->layer_list);
4588 wl_list_init(&ec->seat_list);
4589 wl_list_init(&ec->output_list);
4590 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004591 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004592 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004593 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004594 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004595 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004596
Xiong Zhang97116532013-10-23 13:58:31 +08004597 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004598 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004599
Giulio Camuffo459137b2014-10-11 23:56:24 +03004600 wl_data_device_manager_init(ec->wl_display);
4601
4602 wl_display_init_shm(ec->wl_display);
4603
4604 loop = wl_display_get_event_loop(ec->wl_display);
4605 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004606
Giulio Camuffo459137b2014-10-11 23:56:24 +03004607 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4608 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4609
4610 weston_compositor_add_debug_binding(ec, KEY_T,
4611 timeline_key_binding_handler, ec);
4612
Giulio Camuffo459137b2014-10-11 23:56:24 +03004613 return ec;
4614
4615fail:
4616 free(ec);
4617 return NULL;
4618}
4619
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004620WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004621weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004622{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004623 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004624
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004625 wl_event_source_remove(ec->idle_source);
4626
Matt Roper361d2ad2011-08-29 13:52:23 -07004627 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004628 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004629 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004630
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004631 if (ec->renderer)
4632 ec->renderer->destroy(ec);
4633
Daniel Stone325fc2d2012-05-30 16:31:58 +01004634 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004635 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004636 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004637 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004638 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004639 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004640
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004641 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004642}
4643
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004644WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004645weston_compositor_exit_with_code(struct weston_compositor *compositor,
4646 int exit_code)
4647{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004648 if (compositor->exit_code == EXIT_SUCCESS)
4649 compositor->exit_code = exit_code;
4650
Giulio Camuffo459137b2014-10-11 23:56:24 +03004651 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004652}
4653
4654WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004655weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4656 const struct weston_pointer_grab_interface *interface)
4657{
4658 struct weston_seat *seat;
4659
4660 ec->default_pointer_grab = interface;
4661 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004662 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4663
4664 if (pointer)
4665 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004666 }
4667}
4668
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004669WL_EXPORT int
4670weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4671 clockid_t clk_id)
4672{
4673 struct timespec ts;
4674
4675 if (clock_gettime(clk_id, &ts) < 0)
4676 return -1;
4677
4678 compositor->presentation_clock = clk_id;
4679
4680 return 0;
4681}
4682
4683/*
4684 * For choosing the software clock, when the display hardware or API
4685 * does not expose a compatible presentation timestamp.
4686 */
4687WL_EXPORT int
4688weston_compositor_set_presentation_clock_software(
4689 struct weston_compositor *compositor)
4690{
4691 /* In order of preference */
4692 static const clockid_t clocks[] = {
4693 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4694 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4695 CLOCK_MONOTONIC, /* no jumps, may crawl */
4696 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4697 CLOCK_REALTIME /* may jump and crawl */
4698 };
4699 unsigned i;
4700
4701 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4702 if (weston_compositor_set_presentation_clock(compositor,
4703 clocks[i]) == 0)
4704 return 0;
4705
4706 weston_log("Error: no suitable presentation clock available.\n");
4707
4708 return -1;
4709}
4710
Pekka Paalanen662f3842015-03-18 12:17:26 +02004711/** Read the current time from the Presentation clock
4712 *
4713 * \param compositor
4714 * \param ts[out] The current time.
4715 *
4716 * \note Reading the current time in user space is always imprecise to some
4717 * degree.
4718 *
4719 * This function is never meant to fail. If reading the clock does fail,
4720 * an error message is logged and a zero time is returned. Callers are not
4721 * supposed to detect or react to failures.
4722 */
4723WL_EXPORT void
4724weston_compositor_read_presentation_clock(
4725 const struct weston_compositor *compositor,
4726 struct timespec *ts)
4727{
4728 static bool warned;
4729 int ret;
4730
4731 ret = clock_gettime(compositor->presentation_clock, ts);
4732 if (ret < 0) {
4733 ts->tv_sec = 0;
4734 ts->tv_nsec = 0;
4735
4736 if (!warned)
4737 weston_log("Error: failure to read "
4738 "the presentation clock %#x: '%m' (%d)\n",
4739 compositor->presentation_clock, errno);
4740 warned = true;
4741 }
4742}
4743
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004744/** Import dmabuf buffer into current renderer
4745 *
4746 * \param compositor
4747 * \param buffer the dmabuf buffer to import
4748 * \return true on usable buffers, false otherwise
4749 *
4750 * This function tests that the linux_dmabuf_buffer is usable
4751 * for the current renderer. Returns false on unusable buffers. Usually
4752 * usability is tested by importing the dmabufs for composition.
4753 *
4754 * This hook is also used for detecting if the renderer supports
4755 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4756 * supported.
4757 * */
4758WL_EXPORT bool
4759weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4760 struct linux_dmabuf_buffer *buffer)
4761{
4762 struct weston_renderer *renderer;
4763
4764 renderer = compositor->renderer;
4765
4766 if (renderer->import_dmabuf == NULL)
4767 return false;
4768
4769 return renderer->import_dmabuf(compositor, buffer);
4770}
4771
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004772WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004773weston_version(int *major, int *minor, int *micro)
4774{
4775 *major = WESTON_VERSION_MAJOR;
4776 *minor = WESTON_VERSION_MINOR;
4777 *micro = WESTON_VERSION_MICRO;
4778}
4779
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004780WL_EXPORT void *
4781weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004782{
Derek Foreman3f86e502015-06-08 11:46:54 -05004783 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004784 char path[PATH_MAX];
4785 void *module, *init;
4786
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004787 if (name == NULL)
4788 return NULL;
4789
Derek Foreman3f86e502015-06-08 11:46:54 -05004790 if (name[0] != '/') {
4791 if (builddir)
4792 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4793 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03004794 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004795 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004796 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004797 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004798
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004799 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4800 if (module) {
4801 weston_log("Module '%s' already loaded\n", path);
4802 dlclose(module);
4803 return NULL;
4804 }
4805
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004806 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004807 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004808 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004809 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004810 return NULL;
4811 }
4812
4813 init = dlsym(module, entrypoint);
4814 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004815 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004816 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004817 return NULL;
4818 }
4819
4820 return init;
4821}
4822
Giulio Camuffo459137b2014-10-11 23:56:24 +03004823
4824/** Destroys the compositor.
4825 *
4826 * This function cleans up the compositor state and destroys it.
4827 *
4828 * \param compositor The compositor to be destroyed.
4829 */
4830WL_EXPORT void
4831weston_compositor_destroy(struct weston_compositor *compositor)
4832{
4833 /* prevent further rendering while shutting down */
4834 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4835
4836 wl_signal_emit(&compositor->destroy_signal, compositor);
4837
4838 weston_compositor_xkb_destroy(compositor);
4839
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004840 if (compositor->backend)
4841 compositor->backend->destroy(compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004842 free(compositor);
4843}
4844
4845/** Instruct the compositor to exit.
4846 *
4847 * This functions does not directly destroy the compositor object, it merely
4848 * command it to start the tear down process. It is not guaranteed that the
4849 * tear down will happen immediately.
4850 *
4851 * \param compositor The compositor to tear down.
4852 */
4853WL_EXPORT void
4854weston_compositor_exit(struct weston_compositor *compositor)
4855{
4856 compositor->exit(compositor);
4857}
4858
4859/** Return the user data stored in the compositor.
4860 *
4861 * This function returns the user data pointer set with user_data parameter
4862 * to the \ref weston_compositor_create function.
4863 */
4864WL_EXPORT void *
4865weston_compositor_get_user_data(struct weston_compositor *compositor)
4866{
4867 return compositor->user_data;
4868}
Pekka Paalanendd186732016-06-03 14:49:54 +03004869
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004870static const char * const backend_map[] = {
4871 [WESTON_BACKEND_DRM] = "drm-backend.so",
4872 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
4873 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
4874 [WESTON_BACKEND_RDP] = "rdp-backend.so",
4875 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
4876 [WESTON_BACKEND_X11] = "x11-backend.so",
4877};
4878
Pekka Paalanendd186732016-06-03 14:49:54 +03004879/** Load a backend into a weston_compositor
4880 *
4881 * A backend must be loaded to make a weston_compositor work. A backend
4882 * provides input and output capabilities, and determines the renderer to use.
4883 *
4884 * \param compositor A compositor that has not had a backend loaded yet.
4885 * \param backend Name of the backend file.
4886 * \param config_base A pointer to a backend-specific configuration
4887 * structure's 'base' member.
4888 *
4889 * \return 0 on success, or -1 on error.
4890 */
4891WL_EXPORT int
4892weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004893 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03004894 struct weston_backend_config *config_base)
4895{
4896 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03004897 struct weston_backend_config *config_base);
4898
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004899 if (backend < 0 || backend >= ARRAY_LENGTH(backend_map))
4900 return -1;
4901
4902 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03004903 if (!backend_init)
4904 return -1;
4905
Pekka Paalanena256c5e2016-06-03 14:56:18 +03004906 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03004907}