Debian Codegraph @

clutter/wayland/clutter-wayland-surface.c

raw
1 /*
2 * Clutter.
3 *
4 * An OpenGL based 'interactive canvas' library.
5 *
6 * Copyright (C) 2011 Intel Corporation.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see
20 * <http://www.gnu.org/licenses/>.
21 *
22 * Authors:
23 * Robert Bragg <[email protected]>
24 */
25
26 /**
27 * SECTION:clutter-wayland-surface
28 * @Title: ClutterWaylandSurface
29 * @short_description: An actor which displays the content of a client surface
30 *
31 * #ClutterWaylandSurface is an actor for displaying the contents of a client
32 * surface. It is intended to support developers implementing Clutter based
33 * wayland compositors.
34 */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #define CLUTTER_ENABLE_EXPERIMENTAL_API
41
42 #include "clutter-wayland-surface.h"
43
44 #include "clutter-actor-private.h"
45 #include "clutter-marshal.h"
46 #include "clutter-paint-volume-private.h"
47 #include "clutter-private.h"
48 #include "clutter-backend.h"
49
50 #include <cogl/cogl.h>
51 #include <cogl/cogl-wayland-server.h>
52
53 enum
54 {
55 PROP_SURFACE = 1,
56 PROP_SURFACE_WIDTH,
57 PROP_SURFACE_HEIGHT,
58 PROP_COGL_TEXTURE,
59 PROP_LAST
60 };
61
62 static GParamSpec *obj_props[PROP_LAST];
63
64 enum
65 {
66 QUEUE_DAMAGE_REDRAW,
67
68 LAST_SIGNAL
69 };
70
71 static guint signals[LAST_SIGNAL] = { 0, };
72
73 struct _ClutterWaylandSurfacePrivate
74 {
75 struct wl_surface *surface;
76 CoglTexture2D *buffer;
77 int width, height;
78 CoglPipeline *pipeline;
79 };
80
81 G_DEFINE_TYPE_WITH_PRIVATE (ClutterWaylandSurface,
82 clutter_wayland_surface,
83 CLUTTER_TYPE_ACTOR)
84
85 static gboolean
86 clutter_wayland_surface_get_paint_volume (ClutterActor *self,
87 ClutterPaintVolume *volume)
88 {
89 return clutter_paint_volume_set_from_allocation (volume, self);
90 }
91
92 static void
93 clutter_wayland_surface_queue_damage_redraw (ClutterWaylandSurface *texture,
94 gint x,
95 gint y,
96 gint width,
97 gint height)
98 {
99 ClutterWaylandSurfacePrivate *priv = texture->priv;
100 ClutterActor *self = CLUTTER_ACTOR (texture);
101 ClutterActorBox allocation;
102 float scale_x;
103 float scale_y;
104 cairo_rectangle_int_t clip;
105
106 /* NB: clutter_actor_queue_redraw_with_clip expects a box in the actor's
107 * coordinate space so we need to convert from surface coordinates to
108 * actor coordinates...
109 */
110
111 /* Calling clutter_actor_get_allocation_box() is enormously expensive
112 * if the actor has an out-of-date allocation, since it triggers
113 * a full redraw. clutter_actor_queue_redraw_with_clip() would redraw
114 * the whole stage anyways in that case, so just go ahead and do
115 * it here.
116 */
117 if (!clutter_actor_has_allocation (self))
118 {
119 clutter_actor_queue_redraw (self);
120 return;
121 }
122
123 if (priv->width == 0 || priv->height == 0)
124 return;
125
126 clutter_actor_get_allocation_box (self, &allocation);
127
128 scale_x = (allocation.x2 - allocation.x1) / priv->width;
129 scale_y = (allocation.y2 - allocation.y1) / priv->height;
130
131 clip.x = x * scale_x;
132 clip.y = y * scale_y;
133 clip.width = width * scale_x;
134 clip.height = height * scale_y;
135 clutter_actor_queue_redraw_with_clip (self, &clip);
136 }
137
138 static void
139 free_pipeline (ClutterWaylandSurface *self)
140 {
141 ClutterWaylandSurfacePrivate *priv = self->priv;
142
143 if (priv->pipeline)
144 {
145 cogl_object_unref (priv->pipeline);
146 priv->pipeline = NULL;
147 }
148 }
149
150 static void
151 opacity_change_cb (ClutterWaylandSurface *self)
152 {
153 free_pipeline (self);
154 }
155
156 static void
157 clutter_wayland_surface_init (ClutterWaylandSurface *self)
158 {
159 ClutterWaylandSurfacePrivate *priv;
160
161 priv = clutter_wayland_surface_get_instance_private (self);
162 priv->surface = NULL;
163 priv->width = 0;
164 priv->height = 0;
165
166 self->priv = priv;
167
168 g_signal_connect (self, "notify::opacity", G_CALLBACK (opacity_change_cb), NULL);
169 }
170
171 static void
172 free_surface_buffers (ClutterWaylandSurface *self)
173 {
174 ClutterWaylandSurfacePrivate *priv = self->priv;
175
176 if (priv->buffer)
177 {
178 cogl_object_unref (priv->buffer);
179 priv->buffer = NULL;
180 free_pipeline (self);
181 }
182 }
183
184 static void
185 clutter_wayland_surface_dispose (GObject *object)
186 {
187 ClutterWaylandSurface *self = CLUTTER_WAYLAND_SURFACE (object);
188 ClutterWaylandSurfacePrivate *priv = self->priv;
189
190 free_pipeline (self);
191 free_surface_buffers (self);
192 priv->surface = NULL;
193
194 G_OBJECT_CLASS (clutter_wayland_surface_parent_class)->dispose (object);
195 }
196
197 static void
198 set_size (ClutterWaylandSurface *self,
199 int width,
200 int height)
201 {
202 ClutterWaylandSurfacePrivate *priv = self->priv;
203
204 if (priv->width != width)
205 {
206 priv->width = width;
207 g_object_notify_by_pspec (G_OBJECT (self), obj_props[PROP_SURFACE_WIDTH]);
208 }
209 if (priv->height != height)
210 {
211 priv->height = height;
212 g_object_notify_by_pspec (G_OBJECT (self), obj_props[PROP_SURFACE_HEIGHT]);
213 }
214
215 clutter_actor_set_size (CLUTTER_ACTOR (self), priv->width, priv->height);
216 }
217
218 /**
219 * clutter_wayland_surface_get_surface:
220 * @self: a #ClutterWaylandSurface
221 *
222 * Retrieves a point to the Wayland surface used by the actor.
223 *
224 * Return value: (transfer none): a wl_surface pointer, or %NULL
225 *
226 * Since: 1.10
227 */
228 struct wl_surface *
229 clutter_wayland_surface_get_surface (ClutterWaylandSurface *self)
230 {
231 ClutterWaylandSurfacePrivate *priv = self->priv;
232 return priv->surface;
233 }
234
235 /**
236 * clutter_wayland_surface_set_surface:
237 * @self: a #ClutterWaylandSurface
238 * @surface: a Wayland wl_surface pointer
239 *
240 * Sets the Wayland surface to be used by the actor.
241 *
242 * Since: 1.10
243 */
244 void
245 clutter_wayland_surface_set_surface (ClutterWaylandSurface *self,
246 struct wl_surface *surface)
247 {
248 ClutterWaylandSurfacePrivate *priv;
249
250 g_return_if_fail (CLUTTER_WAYLAND_IS_SURFACE (self));
251
252 priv = self->priv;
253
254 if (priv->surface == surface)
255 return;
256
257 if (priv->surface)
258 {
259 free_pipeline (self);
260 free_surface_buffers (self);
261 g_signal_emit (self, signals[QUEUE_DAMAGE_REDRAW],
262 0,
263 0, 0, priv->width, priv->height);
264 }
265
266 priv->surface = surface;
267
268 /* XXX: should we freeze/thaw notifications? */
269
270 g_object_notify_by_pspec (G_OBJECT (self), obj_props[PROP_SURFACE]);
271
272 /* We have to wait until the next attach event to find out the surface
273 * geometry... */
274 set_size (self, 0, 0);
275 }
276
277 static void
278 clutter_wayland_surface_set_property (GObject *object,
279 guint prop_id,
280 const GValue *value,
281 GParamSpec *pspec)
282 {
283 ClutterWaylandSurface *self = CLUTTER_WAYLAND_SURFACE (object);
284
285 switch (prop_id)
286 {
287 case PROP_SURFACE:
288 clutter_wayland_surface_set_surface (self, g_value_get_pointer (value));
289 break;
290 default:
291 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292 break;
293 }
294 }
295
296 static void
297 clutter_wayland_surface_get_property (GObject *object,
298 guint prop_id,
299 GValue *value,
300 GParamSpec *pspec)
301 {
302 ClutterWaylandSurface *self = CLUTTER_WAYLAND_SURFACE (object);
303 ClutterWaylandSurfacePrivate *priv = self->priv;
304
305 switch (prop_id)
306 {
307 case PROP_SURFACE:
308 g_value_set_pointer (value, priv->surface);
309 break;
310 case PROP_SURFACE_WIDTH:
311 g_value_set_uint (value, priv->width);
312 break;
313 case PROP_SURFACE_HEIGHT:
314 g_value_set_uint (value, priv->height);
315 break;
316 default:
317 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
318 break;
319 }
320 }
321
322 static void
323 clutter_wayland_surface_paint (ClutterActor *self)
324 {
325 ClutterWaylandSurfacePrivate *priv;
326 ClutterActorBox box;
327
328 g_return_if_fail (CLUTTER_WAYLAND_IS_SURFACE (self));
329
330 priv = CLUTTER_WAYLAND_SURFACE (self)->priv;
331
332 if (G_UNLIKELY (priv->pipeline == NULL))
333 {
334 CoglContext *ctx =
335 clutter_backend_get_cogl_context (clutter_get_default_backend ());
336 guint8 paint_opacity = clutter_actor_get_paint_opacity (self);
337
338 priv->pipeline = cogl_pipeline_new (ctx);
339 cogl_pipeline_set_color4ub (priv->pipeline,
340 paint_opacity,
341 paint_opacity,
342 paint_opacity,
343 paint_opacity);
344 cogl_pipeline_set_layer_texture (priv->pipeline, 0,
345 COGL_TEXTURE (priv->buffer));
346 }
347
348 cogl_set_source (priv->pipeline);
349 clutter_actor_get_allocation_box (self, &box);
350 cogl_rectangle (0, 0, box.x2 - box.x1, box.y2 - box.y1);
351 }
352
353 static void
354 clutter_wayland_surface_get_preferred_width (ClutterActor *self,
355 gfloat for_height,
356 gfloat *min_width_p,
357 gfloat *natural_width_p)
358 {
359 ClutterWaylandSurfacePrivate *priv;
360
361 g_return_if_fail (CLUTTER_WAYLAND_IS_SURFACE (self));
362
363 priv = CLUTTER_WAYLAND_SURFACE (self)->priv;
364
365 if (min_width_p)
366 *min_width_p = 0;
367
368 if (natural_width_p)
369 *natural_width_p = priv->width;
370 }
371
372 static void
373 clutter_wayland_surface_get_preferred_height (ClutterActor *self,
374 gfloat for_width,
375 gfloat *min_height_p,
376 gfloat *natural_height_p)
377 {
378 ClutterWaylandSurfacePrivate *priv;
379
380 g_return_if_fail (CLUTTER_WAYLAND_IS_SURFACE (self));
381
382 priv = CLUTTER_WAYLAND_SURFACE (self)->priv;
383
384 if (min_height_p)
385 *min_height_p = 0;
386
387 if (natural_height_p)
388 *natural_height_p = priv->height;
389 }
390
391 static gboolean
392 clutter_wayland_surface_has_overlaps (ClutterActor *self)
393 {
394 /* Rectangles never need an offscreen redirect because there are
395 never any overlapping primitives */
396 return FALSE;
397 }
398
399 static void
400 clutter_wayland_surface_class_init (ClutterWaylandSurfaceClass *klass)
401 {
402 GObjectClass *object_class = G_OBJECT_CLASS (klass);
403 ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
404 GParamSpec *pspec;
405
406 actor_class->get_paint_volume = clutter_wayland_surface_get_paint_volume;
407 actor_class->paint = clutter_wayland_surface_paint;
408 actor_class->get_preferred_width =
409 clutter_wayland_surface_get_preferred_width;
410 actor_class->get_preferred_height =
411 clutter_wayland_surface_get_preferred_height;
412 actor_class->has_overlaps = clutter_wayland_surface_has_overlaps;
413
414 object_class->dispose = clutter_wayland_surface_dispose;
415 object_class->set_property = clutter_wayland_surface_set_property;
416 object_class->get_property = clutter_wayland_surface_get_property;
417
418 pspec = g_param_spec_pointer ("surface",
419 P_("Surface"),
420 P_("The underlying wayland surface"),
421 CLUTTER_PARAM_READWRITE|
422 G_PARAM_CONSTRUCT_ONLY);
423 obj_props[PROP_SURFACE] = pspec;
424 g_object_class_install_property (object_class, PROP_SURFACE, pspec);
425
426 pspec = g_param_spec_uint ("surface-width",
427 P_("Surface width"),
428 P_("The width of the underlying wayland surface"),
429 0, G_MAXUINT,
430 0,
431 G_PARAM_READABLE);
432 obj_props[PROP_SURFACE_WIDTH] = pspec;
433 g_object_class_install_property (object_class, PROP_SURFACE_WIDTH, pspec);
434
435 pspec = g_param_spec_uint ("surface-height",
436 P_("Surface height"),
437 P_("The height of the underlying wayland surface"),
438 0, G_MAXUINT,
439 0,
440 G_PARAM_READABLE);
441 obj_props[PROP_SURFACE_HEIGHT] = pspec;
442 g_object_class_install_property (object_class, PROP_SURFACE_HEIGHT, pspec);
443
444 pspec = g_param_spec_boxed ("cogl-texture",
445 P_("Cogl Texture"),
446 P_("The underlying Cogl texture handle used to draw this actor"),
447 COGL_TYPE_HANDLE,
448 CLUTTER_PARAM_READWRITE);
449 obj_props[PROP_COGL_TEXTURE] = pspec;
450 g_object_class_install_property (object_class, PROP_COGL_TEXTURE, pspec);
451
452 /**
453 * ClutterWaylandSurface::queue-damage-redraw:
454 * @texture: the object which received the signal
455 * @x: The top left x position of the damage region
456 * @y: The top left y position of the damage region
457 * @width: The width of the damage region
458 * @height: The height of the damage region
459 *
460 * ::queue-damage-redraw is emitted to notify that some sub-region
461 * of the texture has been changed. This usually means a redraw
462 * needs to be queued for the actor.
463 *
464 * The default handler will queue a clipped redraw in response to
465 * the damage, using the assumption that the pixmap is being painted
466 * to a rectangle covering the transformed allocation of the actor.
467 * If you sub-class and change the paint method so this isn't true
468 * then you must also provide your own damage signal handler to
469 * queue a redraw that blocks this default behaviour.
470 *
471 * Since: 1.10
472 */
473 signals[QUEUE_DAMAGE_REDRAW] =
474 g_signal_new (g_intern_static_string ("queue-damage-redraw"),
475 G_TYPE_FROM_CLASS (object_class),
476 G_SIGNAL_RUN_FIRST,
477 G_STRUCT_OFFSET (ClutterWaylandSurfaceClass, queue_damage_redraw),
478 NULL, NULL,
479 _clutter_marshal_VOID__INT_INT_INT_INT,
480 G_TYPE_NONE, 4,
481 G_TYPE_INT,
482 G_TYPE_INT,
483 G_TYPE_INT,
484 G_TYPE_INT);
485 klass->queue_damage_redraw = clutter_wayland_surface_queue_damage_redraw;
486 }
487
488 /**
489 * clutter_wayland_surface_new:
490 * @surface: the Wayland surface this actor should represent
491 *
492 * Creates a new #ClutterWaylandSurface for @surface
493 *
494 * Return value: A new #ClutterWaylandSurface representing @surface
495 *
496 * Since: 1.8
497 * Stability: unstable
498 */
499 ClutterActor *
500 clutter_wayland_surface_new (struct wl_surface *surface)
501 {
502 ClutterActor *actor;
503
504 actor = g_object_new (CLUTTER_WAYLAND_TYPE_SURFACE,
505 "surface", surface,
506 NULL);
507
508 return actor;
509 }
510
511 /**
512 * clutter_wayland_surface_attach_buffer:
513 * @self: A #ClutterWaylandSurface actor
514 * @buffer: A compositor side resource representing a wl_buffer
515 * @error: A #GError
516 *
517 * This associates a client's buffer with the #ClutterWaylandSurface
518 * actor @self. This will automatically result in @self being re-drawn
519 * with the new buffer contents.
520 *
521 * Since: 1.8
522 * Stability: unstable
523 */
524 gboolean
525 clutter_wayland_surface_attach_buffer (ClutterWaylandSurface *self,
526 struct wl_resource *buffer,
527 GError **error)
528 {
529 ClutterWaylandSurfacePrivate *priv;
530 ClutterBackend *backend = clutter_get_default_backend ();
531 CoglContext *context = clutter_backend_get_cogl_context (backend);
532
533 g_return_val_if_fail (CLUTTER_WAYLAND_IS_SURFACE (self), TRUE);
534
535 priv = self->priv;
536
537 free_surface_buffers (self);
538
539 priv->buffer =
540 cogl_wayland_texture_2d_new_from_buffer (context, buffer, error);
541
542 g_object_notify_by_pspec (G_OBJECT (self), obj_props[PROP_COGL_TEXTURE]);
543
544 /* NB: We don't queue a redraw of the actor here because we don't
545 * know how much of the buffer has changed with respect to the
546 * previous buffer. We only ever queue a redraw in response to
547 * surface damage. */
548
549 if (!priv->buffer)
550 return FALSE;
551
552 set_size (self,
553 cogl_texture_get_width (COGL_TEXTURE (priv->buffer)),
554 cogl_texture_get_height (COGL_TEXTURE (priv->buffer)));
555
556 return TRUE;
557 }
558
559 /**
560 * clutter_wayland_surface_damage_buffer:
561 * @self: A #ClutterWaylandSurface actor
562 * @buffer: A wayland resource for a buffer
563 * @x: The x coordinate of the damaged rectangle
564 * @y: The y coordinate of the damaged rectangle
565 * @width: The width of the damaged rectangle
566 * @height: The height of the damaged rectangle
567 *
568 * This marks a region of the given @buffer has having been changed by
569 * the client. This will automatically result in the corresponding damaged
570 * region of the actor @self being redrawn.
571 *
572 * If multiple regions are changed then this should be called multiple
573 * times with different damage rectangles.
574 *
575 * Since: 1.8
576 * Stability: unstable
577 */
578 void
579 clutter_wayland_surface_damage_buffer (ClutterWaylandSurface *self,
580 struct wl_resource *buffer,
581 gint32 x,
582 gint32 y,
583 gint32 width,
584 gint32 height)
585 {
586 ClutterWaylandSurfacePrivate *priv;
587 struct wl_shm_buffer *shm_buffer;
588
589 g_return_if_fail (CLUTTER_WAYLAND_IS_SURFACE (self));
590
591 priv = self->priv;
592
593 shm_buffer = wl_shm_buffer_get (buffer);
594
595 if (priv->buffer && shm_buffer)
596 {
597 CoglPixelFormat format;
598
599 switch (wl_shm_buffer_get_format (shm_buffer))
600 {
601 #if G_BYTE_ORDER == G_BIG_ENDIAN
602 case WL_SHM_FORMAT_ARGB8888:
603 format = COGL_PIXEL_FORMAT_ARGB_8888_PRE;
604 break;
605 case WL_SHM_FORMAT_XRGB8888:
606 format = COGL_PIXEL_FORMAT_ARGB_8888;
607 break;
608 #elif G_BYTE_ORDER == G_LITTLE_ENDIAN
609 case WL_SHM_FORMAT_ARGB8888:
610 format = COGL_PIXEL_FORMAT_BGRA_8888_PRE;
611 break;
612 case WL_SHM_FORMAT_XRGB8888:
613 format = COGL_PIXEL_FORMAT_BGRA_8888;
614 break;
615 #endif
616 default:
617 g_warn_if_reached ();
618 format = COGL_PIXEL_FORMAT_ARGB_8888;
619 }
620
621 cogl_texture_set_region (COGL_TEXTURE (priv->buffer),
622 x, y,
623 x, y,
624 width, height,
625 width, height,
626 format,
627 wl_shm_buffer_get_stride (shm_buffer),
628 wl_shm_buffer_get_data (shm_buffer));
629 }
630
631 g_signal_emit (self, signals[QUEUE_DAMAGE_REDRAW],
632 0,
633 x, y, width, height);
634 }
635
636 /**
637 * clutter_wayland_surface_get_cogl_texture:
638 * @self: a #ClutterWaylandSurface
639 *
640 * Retrieves the Cogl texture with the contents of the Wayland surface.
641 *
642 * Return value: (transfer none): a Cogl texture, or %NULL
643 *
644 * Since: 1.10
645 */
646 CoglTexture *
647 clutter_wayland_surface_get_cogl_texture (ClutterWaylandSurface *self)
648 {
649 g_return_val_if_fail (CLUTTER_WAYLAND_IS_SURFACE (self), NULL);
650
651 return COGL_TEXTURE (self->priv->buffer);
652 }