Debian Codegraph @

clutter/deprecated/clutter-texture.c

raw
1 /*
2 * Clutter.
3 *
4 * An OpenGL based 'interactive canvas' library.
5 *
6 * Authored By Matthew Allum <[email protected]>
7 *
8 * Copyright (C) 2006 OpenedHand
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 *
23 *
24 */
25
26 /**
27 * SECTION:clutter-texture
28 * @short_description: An actor for displaying and manipulating images.
29 *
30 * #ClutterTexture is a base class for displaying and manipulating pixel
31 * buffer type data.
32 *
33 * The clutter_texture_set_from_rgb_data() and
34 * clutter_texture_set_from_file() functions are used to copy image
35 * data into texture memory and subsequently realize the texture.
36 *
37 * Note: a ClutterTexture will scale its contents to fit the bounding
38 * box requested using clutter_actor_set_size(). To display an area of
39 * a texture without scaling, you should set the clip area using
40 * clutter_actor_set_clip().
41 *
42 * The #ClutterTexture API is deprecated since Clutter 1.12. It is strongly
43 * recommended to use #ClutterImage instead.
44 */
45
46 #ifdef HAVE_CONFIG_H
47 #include "config.h"
48 #endif
49
50 /* sadly, we are still using ClutterShader internally */
51 #define CLUTTER_DISABLE_DEPRECATION_WARNINGS
52
53 /* This file depends on the glib enum types which aren't exposed
54 * by cogl.h when COGL_ENABLE_EXPERIMENTAL_2_0_API is defined.
55 *
56 * Undefining COGL_ENABLE_EXPERIMENTAL_2_0_API will still expose
57 * us experimental api but will also expose Cogl 1.x api too...
58 */
59 #undef COGL_ENABLE_EXPERIMENTAL_2_0_API
60 #include <cogl/cogl.h>
61
62 #define CLUTTER_ENABLE_EXPERIMENTAL_API
63
64 #include "clutter-texture.h"
65
66 #include "clutter-actor-private.h"
67 #include "clutter-color.h"
68 #include "clutter-debug.h"
69 #include "clutter-enum-types.h"
70 #include "clutter-feature.h"
71 #include "clutter-main.h"
72 #include "clutter-marshal.h"
73 #include "clutter-private.h"
74 #include "clutter-scriptable.h"
75 #include "clutter-stage-private.h"
76
77 #include "deprecated/clutter-shader.h"
78 #include "deprecated/clutter-texture.h"
79 #include "deprecated/clutter-util.h"
80
81 typedef struct _ClutterTextureAsyncData ClutterTextureAsyncData;
82
83 struct _ClutterTexturePrivate
84 {
85 gint image_width;
86 gint image_height;
87
88 CoglPipeline *pipeline;
89
90 ClutterActor *fbo_source;
91 CoglHandle fbo_handle;
92
93 CoglPipeline *pick_pipeline;
94
95 gchar *filename;
96
97 ClutterTextureAsyncData *async_data;
98
99 guint no_slice : 1;
100 guint sync_actor_size : 1;
101 guint repeat_x : 1;
102 guint repeat_y : 1;
103 guint keep_aspect_ratio : 1;
104 guint load_size_async : 1;
105 guint load_data_async : 1;
106 guint load_async_set : 1; /* used to make load_async possible */
107 guint pick_with_alpha : 1;
108 guint pick_with_alpha_supported : 1;
109 guint seen_create_pick_pipeline_warning : 1;
110 };
111
112 #define ASYNC_STATE_LOCKED 1
113 #define ASYNC_STATE_CANCELLED 2
114 #define ASYNC_STATE_QUEUED 3
115
116 struct _ClutterTextureAsyncData
117 {
118 /* The texture for which the data is being loaded */
119 ClutterTexture *texture;
120
121 gchar *load_filename;
122 CoglHandle load_bitmap;
123
124 guint load_idle;
125
126 GError *load_error;
127
128 gint state;
129 };
130
131 static inline void
132 clutter_texture_async_data_lock (ClutterTextureAsyncData *data)
133 {
134 g_bit_lock (&data->state, 0);
135 }
136
137 static inline void
138 clutter_texture_async_data_unlock (ClutterTextureAsyncData *data)
139 {
140 g_bit_unlock (&data->state, 0);
141 }
142
143 enum
144 {
145 PROP_0,
146 PROP_NO_SLICE,
147 PROP_MAX_TILE_WASTE,
148 PROP_PIXEL_FORMAT,
149 PROP_SYNC_SIZE,
150 PROP_REPEAT_Y,
151 PROP_REPEAT_X,
152 PROP_FILTER_QUALITY,
153 PROP_COGL_TEXTURE,
154 PROP_COGL_MATERIAL,
155 PROP_FILENAME,
156 PROP_KEEP_ASPECT_RATIO,
157 PROP_LOAD_ASYNC,
158 PROP_LOAD_DATA_ASYNC,
159 PROP_PICK_WITH_ALPHA,
160
161 PROP_LAST
162 };
163
164 static GParamSpec *obj_props[PROP_LAST];
165
166 enum
167 {
168 SIZE_CHANGE,
169 PIXBUF_CHANGE,
170 LOAD_SUCCESS,
171 LOAD_FINISHED,
172 LAST_SIGNAL
173 };
174
175 static int texture_signals[LAST_SIGNAL] = { 0 };
176
177 static GThreadPool *async_thread_pool = NULL;
178 static guint repaint_upload_func = 0;
179 static GList *upload_list = NULL;
180 static GMutex upload_list_mutex;
181
182 static CoglPipeline *texture_template_pipeline = NULL;
183
184 static void
185 texture_fbo_free_resources (ClutterTexture *texture);
186
187 static void clutter_scriptable_iface_init (ClutterScriptableIface *iface);
188
189 G_DEFINE_TYPE_WITH_CODE (ClutterTexture,
190 clutter_texture,
191 CLUTTER_TYPE_ACTOR,
192 G_ADD_PRIVATE (ClutterTexture)
193 G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_SCRIPTABLE,
194 clutter_scriptable_iface_init));
195
196
197 GQuark
198 clutter_texture_error_quark (void)
199 {
200 return g_quark_from_static_string ("clutter-texture-error-quark");
201 }
202
203 static const struct
204 {
205 gint min_filter;
206 gint mag_filter;
207 }
208 clutter_texture_quality_filters[] =
209 {
210 /* CLUTTER_TEXTURE_QUALITY_LOW */
211 { COGL_PIPELINE_FILTER_NEAREST, COGL_PIPELINE_FILTER_NEAREST },
212
213 /* CLUTTER_TEXTURE_QUALITY_MEDIUM */
214 { COGL_PIPELINE_FILTER_LINEAR, COGL_PIPELINE_FILTER_LINEAR },
215
216 /* CLUTTER_TEXTURE_QUALITY_HIGH */
217 { COGL_PIPELINE_FILTER_LINEAR_MIPMAP_LINEAR, COGL_PIPELINE_FILTER_LINEAR }
218 };
219
220 static inline void
221 clutter_texture_quality_to_filters (ClutterTextureQuality quality,
222 gint *min_filter_p,
223 gint *mag_filter_p)
224 {
225 g_return_if_fail (quality < G_N_ELEMENTS (clutter_texture_quality_filters));
226
227 if (min_filter_p)
228 *min_filter_p = clutter_texture_quality_filters[quality].min_filter;
229
230 if (mag_filter_p)
231 *mag_filter_p = clutter_texture_quality_filters[quality].mag_filter;
232 }
233
234 static void
235 texture_free_gl_resources (ClutterTexture *texture)
236 {
237 ClutterTexturePrivate *priv = texture->priv;
238
239 if (priv->pipeline != NULL)
240 {
241 /* We want to keep the layer so that the filter settings will
242 remain but we want to free its resources so we clear the
243 texture handle */
244 cogl_pipeline_set_layer_texture (priv->pipeline, 0, NULL);
245 }
246 }
247
248 static void
249 clutter_texture_unrealize (ClutterActor *actor)
250 {
251 ClutterTexture *texture;
252 ClutterTexturePrivate *priv;
253
254 texture = CLUTTER_TEXTURE(actor);
255 priv = texture->priv;
256
257 if (priv->pipeline == NULL)
258 return;
259
260 if (priv->fbo_source != NULL)
261 {
262 /* Free up our fbo handle and texture resources, realize will recreate */
263 cogl_object_unref (priv->fbo_handle);
264 priv->fbo_handle = NULL;
265 texture_free_gl_resources (texture);
266 return;
267 }
268
269 CLUTTER_NOTE (TEXTURE, "Texture unrealized");
270 }
271
272 static void
273 clutter_texture_realize (ClutterActor *actor)
274 {
275 ClutterTexture *texture;
276 ClutterTexturePrivate *priv;
277
278 texture = CLUTTER_TEXTURE(actor);
279 priv = texture->priv;
280
281 if (priv->fbo_source)
282 {
283 CoglTextureFlags flags = COGL_TEXTURE_NONE;
284 CoglHandle tex;
285
286 /* Handle FBO's */
287
288 if (priv->no_slice)
289 flags |= COGL_TEXTURE_NO_SLICING;
290
291 tex = cogl_texture_new_with_size (priv->image_width,
292 priv->image_height,
293 flags,
294 COGL_PIXEL_FORMAT_RGBA_8888_PRE);
295
296 cogl_pipeline_set_layer_texture (priv->pipeline, 0, tex);
297
298 priv->fbo_handle = cogl_offscreen_new_to_texture (tex);
299
300 /* The pipeline now has a reference to the texture so it will
301 stick around */
302 cogl_object_unref (tex);
303
304 if (priv->fbo_handle == NULL)
305 {
306 g_warning ("%s: Offscreen texture creation failed", G_STRLOC);
307 CLUTTER_ACTOR_UNSET_FLAGS (actor, CLUTTER_ACTOR_REALIZED);
308 return;
309 }
310
311 clutter_actor_set_size (actor, priv->image_width, priv->image_height);
312
313 return;
314 }
315
316 /* If the texture is not a FBO, then realization is a no-op but we
317 * still want to be in REALIZED state to maintain invariants.
318 * ClutterTexture doesn't need to be realized to have a Cogl texture
319 * because Clutter assumes that a GL context is always current so
320 * there is no need to wait to realization time to create the
321 * texture. Although this is slightly odd it would be wasteful to
322 * redundantly store a copy of the texture data in local memory just
323 * so that we can make a texture during realize.
324 */
325
326 CLUTTER_NOTE (TEXTURE, "Texture realized");
327 }
328
329 static void
330 clutter_texture_get_preferred_width (ClutterActor *self,
331 gfloat for_height,
332 gfloat *min_width_p,
333 gfloat *natural_width_p)
334 {
335 ClutterTexture *texture = CLUTTER_TEXTURE (self);
336 ClutterTexturePrivate *priv = texture->priv;
337
338 /* Min request is always 0 since we can scale down or clip */
339 if (min_width_p)
340 *min_width_p = 0;
341
342 if (priv->sync_actor_size)
343 {
344 if (natural_width_p)
345 {
346 if (!priv->keep_aspect_ratio ||
347 for_height < 0 ||
348 priv->image_height <= 0)
349 {
350 *natural_width_p = priv->image_width;
351 }
352 else
353 {
354 /* Set the natural width so as to preserve the aspect ratio */
355 gfloat ratio = (gfloat) priv->image_width
356 / (gfloat) priv->image_height;
357
358 *natural_width_p = ratio * for_height;
359 }
360 }
361 }
362 else
363 {
364 if (natural_width_p)
365 *natural_width_p = 0;
366 }
367 }
368
369 static void
370 clutter_texture_get_preferred_height (ClutterActor *self,
371 gfloat for_width,
372 gfloat *min_height_p,
373 gfloat *natural_height_p)
374 {
375 ClutterTexture *texture = CLUTTER_TEXTURE (self);
376 ClutterTexturePrivate *priv = texture->priv;
377
378 /* Min request is always 0 since we can scale down or clip */
379 if (min_height_p)
380 *min_height_p = 0;
381
382 if (priv->sync_actor_size)
383 {
384 if (natural_height_p)
385 {
386 if (!priv->keep_aspect_ratio ||
387 for_width < 0 ||
388 priv->image_width <= 0)
389 {
390 *natural_height_p = priv->image_height;
391 }
392 else
393 {
394 /* Set the natural height so as to preserve the aspect ratio */
395 gfloat ratio = (gfloat) priv->image_height
396 / (gfloat) priv->image_width;
397
398 *natural_height_p = ratio * for_width;
399 }
400 }
401 }
402 else
403 {
404 if (natural_height_p)
405 *natural_height_p = 0;
406 }
407 }
408
409 static void
410 clutter_texture_allocate (ClutterActor *self,
411 const ClutterActorBox *box,
412 ClutterAllocationFlags flags)
413 {
414 ClutterTexturePrivate *priv = CLUTTER_TEXTURE (self)->priv;
415
416 /* chain up to set actor->allocation */
417 CLUTTER_ACTOR_CLASS (clutter_texture_parent_class)->allocate (self,
418 box,
419 flags);
420
421 /* If we adopted the source fbo then allocate that at its preferred
422 size */
423 if (priv->fbo_source && clutter_actor_get_parent (priv->fbo_source) == self)
424 clutter_actor_allocate_preferred_size (priv->fbo_source, flags);
425 }
426
427 static gboolean
428 clutter_texture_has_overlaps (ClutterActor *self)
429 {
430 /* Textures never need an offscreen redirect because there are never
431 any overlapping primitives */
432 return FALSE;
433 }
434
435 static void
436 set_viewport_with_buffer_under_fbo_source (ClutterActor *fbo_source,
437 int viewport_width,
438 int viewport_height)
439 {
440 ClutterActorBox box = { 0, };
441 float x_offset, y_offset;
442
443 if (clutter_actor_get_paint_box (fbo_source, &box))
444 clutter_actor_box_get_origin (&box, &x_offset, &y_offset);
445 else
446 {
447 /* As a fallback when the paint box can't be determined we use
448 * the transformed allocation to come up with an offset instead.
449 *
450 * FIXME: when we don't have a paint box we should instead be
451 * falling back to a stage sized fbo with an offset of (0,0)
452 */
453
454 ClutterVertex verts[4];
455 float x_min = G_MAXFLOAT, y_min = G_MAXFLOAT;
456 int i;
457
458 /* Get the actors allocation transformed into screen coordinates.
459 *
460 * XXX: Note: this may not be a bounding box for the actor, since an
461 * actor with depth may escape the box due to its perspective
462 * projection. */
463 clutter_actor_get_abs_allocation_vertices (fbo_source, verts);
464
465 for (i = 0; i < G_N_ELEMENTS (verts); ++i)
466 {
467 if (verts[i].x < x_min)
468 x_min = verts[i].x;
469 if (verts[i].y < y_min)
470 y_min = verts[i].y;
471 }
472
473 /* XXX: It's not good enough to round by simply truncating the fraction here
474 * via a cast, as it results in offscreen rendering being offset by 1 pixel
475 * in many cases... */
476 #define ROUND(x) ((x) >= 0 ? (long)((x) + 0.5) : (long)((x) - 0.5))
477
478 x_offset = ROUND (x_min);
479 y_offset = ROUND (y_min);
480
481 #undef ROUND
482 }
483
484 /* translate the viewport so that the source actor lands on the
485 * sub-region backed by the offscreen framebuffer... */
486 cogl_set_viewport (-x_offset, -y_offset, viewport_width, viewport_height);
487 }
488
489 static void
490 update_fbo (ClutterActor *self)
491 {
492 ClutterTexture *texture = CLUTTER_TEXTURE (self);
493 ClutterTexturePrivate *priv = texture->priv;
494 ClutterActor *head;
495 ClutterShader *shader = NULL;
496 ClutterActor *stage = NULL;
497 CoglMatrix projection;
498 CoglColor transparent_col;
499
500 head = _clutter_context_peek_shader_stack ();
501 if (head != NULL)
502 shader = clutter_actor_get_shader (head);
503
504 /* Temporarily turn off the shader on the top of the context's
505 * shader stack, to restore the GL pipeline to it's natural state.
506 */
507 if (shader != NULL)
508 clutter_shader_set_is_enabled (shader, FALSE);
509
510 /* Redirect drawing to the fbo */
511 cogl_push_framebuffer (priv->fbo_handle);
512
513 if ((stage = clutter_actor_get_stage (self)) != NULL)
514 {
515 gfloat stage_width, stage_height;
516 ClutterActor *source_parent;
517
518 /* We copy the projection and modelview matrices from the stage to
519 * the offscreen framebuffer and create a viewport larger than the
520 * offscreen framebuffer - the same size as the stage.
521 *
522 * The fbo source actor gets rendered into this stage size viewport at the
523 * same position it normally would after applying all it's usual parent
524 * transforms and it's own scale and rotate transforms etc.
525 *
526 * The viewport is offset such that the offscreen buffer will be positioned
527 * under the actor.
528 */
529
530 _clutter_stage_get_projection_matrix (CLUTTER_STAGE (stage), &projection);
531
532 /* Set the projection matrix modelview matrix as it is for the
533 * stage... */
534 cogl_set_projection_matrix (&projection);
535
536 clutter_actor_get_size (stage, &stage_width, &stage_height);
537
538 /* Set a negatively offset the viewport so that the offscreen
539 * framebuffer is position underneath the fbo_source actor... */
540 set_viewport_with_buffer_under_fbo_source (priv->fbo_source,
541 stage_width,
542 stage_height);
543
544 /* Apply the source's parent transformations to the modelview */
545 if ((source_parent = clutter_actor_get_parent (priv->fbo_source)))
546 {
547 CoglMatrix modelview;
548 cogl_matrix_init_identity (&modelview);
549 _clutter_actor_apply_relative_transformation_matrix (source_parent,
550 NULL,
551 &modelview);
552 cogl_set_modelview_matrix (&modelview);
553 }
554 }
555
556
557 /* cogl_clear is called to clear the buffers */
558 cogl_color_init_from_4ub (&transparent_col, 0, 0, 0, 0);
559 cogl_clear (&transparent_col,
560 COGL_BUFFER_BIT_COLOR |
561 COGL_BUFFER_BIT_DEPTH);
562
563 /* Render the actor to the fbo */
564 clutter_actor_paint (priv->fbo_source);
565
566 /* Restore drawing to the previous framebuffer */
567 cogl_pop_framebuffer ();
568
569 /* If there is a shader on top of the shader stack, turn it back on. */
570 if (shader != NULL)
571 clutter_shader_set_is_enabled (shader, TRUE);
572 }
573
574 static void
575 gen_texcoords_and_draw_cogl_rectangle (ClutterActor *self)
576 {
577 ClutterTexture *texture = CLUTTER_TEXTURE (self);
578 ClutterTexturePrivate *priv = texture->priv;
579 ClutterActorBox box;
580 float t_w, t_h;
581
582 clutter_actor_get_allocation_box (self, &box);
583
584 if (priv->repeat_x && priv->image_width > 0)
585 t_w = (box.x2 - box.x1) / (float) priv->image_width;
586 else
587 t_w = 1.0;
588
589 if (priv->repeat_y && priv->image_height > 0)
590 t_h = (box.y2 - box.y1) / (float) priv->image_height;
591 else
592 t_h = 1.0;
593
594 cogl_rectangle_with_texture_coords (0, 0,
595 box.x2 - box.x1,
596 box.y2 - box.y1,
597 0, 0, t_w, t_h);
598 }
599
600 static CoglPipeline *
601 create_pick_pipeline (ClutterActor *self)
602 {
603 ClutterTexture *texture = CLUTTER_TEXTURE (self);
604 ClutterTexturePrivate *priv = texture->priv;
605 CoglPipeline *pick_pipeline = cogl_pipeline_copy (texture_template_pipeline);
606 GError *error = NULL;
607
608 if (!cogl_pipeline_set_layer_combine (pick_pipeline, 0,
609 "RGBA = "
610 " MODULATE (CONSTANT, TEXTURE[A])",
611 &error))
612 {
613 if (!priv->seen_create_pick_pipeline_warning)
614 g_warning ("Error setting up texture combine for shaped "
615 "texture picking: %s", error->message);
616 priv->seen_create_pick_pipeline_warning = TRUE;
617 g_error_free (error);
618 cogl_object_unref (pick_pipeline);
619 return NULL;
620 }
621
622 cogl_pipeline_set_blend (pick_pipeline,
623 "RGBA = ADD (SRC_COLOR[RGBA], 0)",
624 NULL);
625
626 cogl_pipeline_set_alpha_test_function (pick_pipeline,
627 COGL_PIPELINE_ALPHA_FUNC_EQUAL,
628 1.0);
629
630 return pick_pipeline;
631 }
632
633 static void
634 clutter_texture_pick (ClutterActor *self,
635 const ClutterColor *color)
636 {
637 ClutterTexture *texture = CLUTTER_TEXTURE (self);
638 ClutterTexturePrivate *priv = texture->priv;
639
640 if (!clutter_actor_should_pick_paint (self))
641 return;
642
643 if (G_LIKELY (priv->pick_with_alpha_supported) && priv->pick_with_alpha)
644 {
645 CoglColor pick_color;
646
647 if (priv->pick_pipeline == NULL)
648 priv->pick_pipeline = create_pick_pipeline (self);
649
650 if (priv->pick_pipeline == NULL)
651 {
652 priv->pick_with_alpha_supported = FALSE;
653 CLUTTER_ACTOR_CLASS (clutter_texture_parent_class)->pick (self,
654 color);
655 return;
656 }
657
658 if (priv->fbo_handle != NULL)
659 update_fbo (self);
660
661 cogl_color_init_from_4ub (&pick_color,
662 color->red,
663 color->green,
664 color->blue,
665 0xff);
666 cogl_pipeline_set_layer_combine_constant (priv->pick_pipeline,
667 0, &pick_color);
668 cogl_pipeline_set_layer_texture (priv->pick_pipeline, 0,
669 clutter_texture_get_cogl_texture (texture));
670 cogl_set_source (priv->pick_pipeline);
671 gen_texcoords_and_draw_cogl_rectangle (self);
672 }
673 else
674 CLUTTER_ACTOR_CLASS (clutter_texture_parent_class)->pick (self, color);
675 }
676
677 static void
678 clutter_texture_paint (ClutterActor *self)
679 {
680 ClutterTexture *texture = CLUTTER_TEXTURE (self);
681 ClutterTexturePrivate *priv = texture->priv;
682 guint8 paint_opacity = clutter_actor_get_paint_opacity (self);
683
684 CLUTTER_NOTE (PAINT,
685 "painting texture '%s'",
686 clutter_actor_get_name (self) ? clutter_actor_get_name (self)
687 : "unknown");
688
689 if (priv->fbo_handle != NULL)
690 update_fbo (self);
691
692 cogl_pipeline_set_color4ub (priv->pipeline,
693 paint_opacity,
694 paint_opacity,
695 paint_opacity,
696 paint_opacity);
697 cogl_set_source (priv->pipeline);
698
699 gen_texcoords_and_draw_cogl_rectangle (self);
700 }
701
702 static gboolean
703 clutter_texture_get_paint_volume (ClutterActor *self,
704 ClutterPaintVolume *volume)
705 {
706 ClutterTexturePrivate *priv;
707
708 priv = CLUTTER_TEXTURE (self)->priv;
709
710 if (priv->pipeline == NULL)
711 return FALSE;
712
713 if (priv->image_width == 0 || priv->image_height == 0)
714 return FALSE;
715
716 return _clutter_actor_set_default_paint_volume (self,
717 CLUTTER_TYPE_TEXTURE,
718 volume);
719 }
720
721 static void
722 clutter_texture_async_data_free (ClutterTextureAsyncData *data)
723 {
724 /* This function should only be called either from the main thread
725 once it is known that the load thread has completed or from the
726 load thread/upload function itself if the abort flag is true (in
727 which case the main thread has disowned the data) */
728 g_free (data->load_filename);
729
730 if (data->load_bitmap != NULL)
731 cogl_object_unref (data->load_bitmap);
732
733 if (data->load_error != NULL)
734 g_error_free (data->load_error);
735
736 g_slice_free (ClutterTextureAsyncData, data);
737 }
738
739 /*
740 * clutter_texture_async_load_cancel:
741 * @texture: a #ClutterTexture
742 *
743 * Cancels an asynchronous loading operation, whether done
744 * with threads enabled or just using the main loop
745 */
746 static void
747 clutter_texture_async_load_cancel (ClutterTexture *texture)
748 {
749 ClutterTexturePrivate *priv = texture->priv;
750
751 if (priv->async_data != NULL)
752 {
753 ClutterTextureAsyncData *async_data = priv->async_data;
754
755 priv->async_data = NULL;
756
757 if (async_data->load_idle != 0)
758 {
759 g_source_remove (async_data->load_idle);
760 async_data->load_idle = 0;
761
762 clutter_texture_async_data_free (async_data);
763 }
764 else
765 {
766 clutter_texture_async_data_lock (async_data);
767
768 CLUTTER_NOTE (TEXTURE, "[async] cancelling operation for '%s'",
769 async_data->load_filename);
770
771 async_data->state |= ASYNC_STATE_CANCELLED;
772
773 clutter_texture_async_data_unlock (async_data);
774 }
775 }
776 }
777
778 static void
779 clutter_texture_dispose (GObject *object)
780 {
781 ClutterTexture *texture = CLUTTER_TEXTURE (object);
782 ClutterTexturePrivate *priv = texture->priv;
783
784 texture_free_gl_resources (texture);
785 texture_fbo_free_resources (texture);
786
787 clutter_texture_async_load_cancel (texture);
788
789 if (priv->pipeline != NULL)
790 {
791 cogl_object_unref (priv->pipeline);
792 priv->pipeline = NULL;
793 }
794
795 if (priv->pick_pipeline != NULL)
796 {
797 cogl_object_unref (priv->pick_pipeline);
798 priv->pick_pipeline = NULL;
799 }
800
801 G_OBJECT_CLASS (clutter_texture_parent_class)->dispose (object);
802 }
803
804 static void
805 clutter_texture_finalize (GObject *object)
806 {
807 ClutterTexturePrivate *priv = CLUTTER_TEXTURE (object)->priv;
808
809 g_free (priv->filename);
810
811 G_OBJECT_CLASS (clutter_texture_parent_class)->finalize (object);
812 }
813
814 static void
815 clutter_texture_set_property (GObject *object,
816 guint prop_id,
817 const GValue *value,
818 GParamSpec *pspec)
819 {
820 ClutterTexture *texture;
821 ClutterTexturePrivate *priv;
822
823 texture = CLUTTER_TEXTURE (object);
824 priv = texture->priv;
825
826 switch (prop_id)
827 {
828 case PROP_SYNC_SIZE:
829 clutter_texture_set_sync_size (texture, g_value_get_boolean (value));
830 break;
831
832 case PROP_REPEAT_X:
833 clutter_texture_set_repeat (texture,
834 g_value_get_boolean (value),
835 priv->repeat_y);
836 break;
837
838 case PROP_REPEAT_Y:
839 clutter_texture_set_repeat (texture,
840 priv->repeat_x,
841 g_value_get_boolean (value));
842 break;
843
844 case PROP_FILTER_QUALITY:
845 clutter_texture_set_filter_quality (texture,
846 g_value_get_enum (value));
847 break;
848
849 case PROP_COGL_TEXTURE:
850 {
851 CoglHandle hnd = g_value_get_boxed (value);
852
853 clutter_texture_set_cogl_texture (texture, hnd);
854 }
855 break;
856
857 case PROP_COGL_MATERIAL:
858 {
859 CoglHandle hnd = g_value_get_boxed (value);
860
861 clutter_texture_set_cogl_material (texture, hnd);
862 }
863 break;
864
865 case PROP_FILENAME:
866 clutter_texture_set_from_file (texture,
867 g_value_get_string (value),
868 NULL);
869 break;
870
871 case PROP_NO_SLICE:
872 priv->no_slice = g_value_get_boolean (value);
873 break;
874
875 case PROP_KEEP_ASPECT_RATIO:
876 clutter_texture_set_keep_aspect_ratio (texture,
877 g_value_get_boolean (value));
878 break;
879
880 case PROP_LOAD_DATA_ASYNC:
881 clutter_texture_set_load_data_async (texture,
882 g_value_get_boolean (value));
883 break;
884
885 case PROP_LOAD_ASYNC:
886 clutter_texture_set_load_async (texture, g_value_get_boolean (value));
887 break;
888
889 case PROP_PICK_WITH_ALPHA:
890 clutter_texture_set_pick_with_alpha (texture,
891 g_value_get_boolean (value));
892 break;
893
894 default:
895 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
896 break;
897 }
898 }
899
900 static void
901 clutter_texture_get_property (GObject *object,
902 guint prop_id,
903 GValue *value,
904 GParamSpec *pspec)
905 {
906 ClutterTexture *texture;
907 ClutterTexturePrivate *priv;
908
909 texture = CLUTTER_TEXTURE(object);
910 priv = texture->priv;
911
912 switch (prop_id)
913 {
914 case PROP_PIXEL_FORMAT:
915 g_value_set_enum (value, clutter_texture_get_pixel_format (texture));
916 break;
917
918 case PROP_MAX_TILE_WASTE:
919 g_value_set_int (value, clutter_texture_get_max_tile_waste (texture));
920 break;
921
922 case PROP_SYNC_SIZE:
923 g_value_set_boolean (value, priv->sync_actor_size);
924 break;
925
926 case PROP_REPEAT_X:
927 g_value_set_boolean (value, priv->repeat_x);
928 break;
929
930 case PROP_REPEAT_Y:
931 g_value_set_boolean (value, priv->repeat_y);
932 break;
933
934 case PROP_FILTER_QUALITY:
935 g_value_set_enum (value, clutter_texture_get_filter_quality (texture));
936 break;
937
938 case PROP_COGL_TEXTURE:
939 g_value_set_boxed (value, clutter_texture_get_cogl_texture (texture));
940 break;
941
942 case PROP_COGL_MATERIAL:
943 g_value_set_boxed (value, clutter_texture_get_cogl_material (texture));
944 break;
945
946 case PROP_NO_SLICE:
947 g_value_set_boolean (value, priv->no_slice);
948 break;
949
950 case PROP_KEEP_ASPECT_RATIO:
951 g_value_set_boolean (value, priv->keep_aspect_ratio);
952 break;
953
954 case PROP_PICK_WITH_ALPHA:
955 g_value_set_boolean (value, priv->pick_with_alpha);
956 break;
957
958 case PROP_FILENAME:
959 g_value_set_string (value, priv->filename);
960 break;
961
962 default:
963 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
964 break;
965 }
966 }
967
968 static void
969 clutter_texture_class_init (ClutterTextureClass *klass)
970 {
971 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
972 ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
973 GParamSpec *pspec;
974
975 actor_class->paint = clutter_texture_paint;
976 actor_class->pick = clutter_texture_pick;
977 actor_class->get_paint_volume = clutter_texture_get_paint_volume;
978 actor_class->realize = clutter_texture_realize;
979 actor_class->unrealize = clutter_texture_unrealize;
980 actor_class->has_overlaps = clutter_texture_has_overlaps;
981
982 actor_class->get_preferred_width = clutter_texture_get_preferred_width;
983 actor_class->get_preferred_height = clutter_texture_get_preferred_height;
984 actor_class->allocate = clutter_texture_allocate;
985
986 gobject_class->dispose = clutter_texture_dispose;
987 gobject_class->finalize = clutter_texture_finalize;
988 gobject_class->set_property = clutter_texture_set_property;
989 gobject_class->get_property = clutter_texture_get_property;
990
991 pspec = g_param_spec_boolean ("sync-size",
992 P_("Sync size of actor"),
993 P_("Auto sync size of actor to underlying pixbuf dimensions"),
994 TRUE,
995 CLUTTER_PARAM_READWRITE);
996 obj_props[PROP_SYNC_SIZE] = pspec;
997 g_object_class_install_property (gobject_class, PROP_SYNC_SIZE, pspec);
998
999 pspec = g_param_spec_boolean ("disable-slicing",
1000 P_("Disable Slicing"),
1001 P_("Forces the underlying texture to be singular and not made of smaller space saving "
1002 "individual textures"),
1003 FALSE,
1004 G_PARAM_CONSTRUCT_ONLY |
1005 CLUTTER_PARAM_READWRITE);
1006 obj_props[PROP_NO_SLICE] = pspec;
1007 g_object_class_install_property (gobject_class, PROP_NO_SLICE, pspec);
1008
1009 pspec = g_param_spec_int ("tile-waste",
1010 P_("Tile Waste"),
1011 P_("Maximum waste area of a sliced texture"),
1012 -1, G_MAXINT,
1013 COGL_TEXTURE_MAX_WASTE,
1014 CLUTTER_PARAM_READABLE);
1015 obj_props[PROP_MAX_TILE_WASTE] = pspec;
1016 g_object_class_install_property (gobject_class, PROP_MAX_TILE_WASTE, pspec);
1017
1018 pspec = g_param_spec_boolean ("repeat-x",
1019 P_("Horizontal repeat"),
1020 P_("Repeat the contents rather than scaling them horizontally"),
1021 FALSE,
1022 CLUTTER_PARAM_READWRITE);
1023 obj_props[PROP_REPEAT_X] = pspec;
1024 g_object_class_install_property (gobject_class, PROP_REPEAT_X, pspec);
1025
1026 pspec = g_param_spec_boolean ("repeat-y",
1027 P_("Vertical repeat"),
1028 P_("Repeat the contents rather than scaling them vertically"),
1029 FALSE,
1030 CLUTTER_PARAM_READWRITE);
1031 obj_props[PROP_REPEAT_Y] = pspec;
1032 g_object_class_install_property (gobject_class, PROP_REPEAT_Y, pspec);
1033
1034 pspec = g_param_spec_enum ("filter-quality",
1035 P_("Filter Quality"),
1036 P_("Rendering quality used when drawing the texture"),
1037 CLUTTER_TYPE_TEXTURE_QUALITY,
1038 CLUTTER_TEXTURE_QUALITY_MEDIUM,
1039 G_PARAM_CONSTRUCT | CLUTTER_PARAM_READWRITE);
1040 obj_props[PROP_FILTER_QUALITY] = pspec;
1041 g_object_class_install_property (gobject_class, PROP_FILTER_QUALITY, pspec);
1042
1043 pspec = g_param_spec_enum ("pixel-format",
1044 P_("Pixel Format"),
1045 P_("The Cogl pixel format to use"),
1046 COGL_TYPE_PIXEL_FORMAT,
1047 COGL_PIXEL_FORMAT_RGBA_8888,
1048 CLUTTER_PARAM_READABLE);
1049 obj_props[PROP_PIXEL_FORMAT] = pspec;
1050 g_object_class_install_property (gobject_class, PROP_PIXEL_FORMAT, pspec);
1051
1052 pspec = g_param_spec_boxed ("cogl-texture",
1053 P_("Cogl Texture"),
1054 P_("The underlying Cogl texture handle used to draw this actor"),
1055 COGL_TYPE_HANDLE,
1056 CLUTTER_PARAM_READWRITE);
1057 obj_props[PROP_COGL_TEXTURE] = pspec;
1058 g_object_class_install_property (gobject_class, PROP_COGL_TEXTURE, pspec);
1059
1060 pspec = g_param_spec_boxed ("cogl-material",
1061 P_("Cogl Material"),
1062 P_("The underlying Cogl material handle used to draw this actor"),
1063 COGL_TYPE_HANDLE,
1064 CLUTTER_PARAM_READWRITE);
1065 obj_props[PROP_COGL_MATERIAL] = pspec;
1066 g_object_class_install_property (gobject_class, PROP_COGL_MATERIAL, pspec);
1067
1068 /**
1069 * ClutterTexture:filename:
1070 *
1071 * The path of the file containing the image data to be displayed by
1072 * the texture.
1073 *
1074 * This property is unset when using the clutter_texture_set_from_*_data()
1075 * family of functions.
1076 *
1077 * Deprecated: 1.12: Use #ClutterImage and platform-specific image loading
1078 * API, like GdkPixbuf
1079 */
1080 pspec = g_param_spec_string ("filename",
1081 P_("Filename"),
1082 P_("The path of the file containing the image data"),
1083 NULL,
1084 CLUTTER_PARAM_READWRITE);
1085 obj_props[PROP_FILENAME] = pspec;
1086 g_object_class_install_property (gobject_class, PROP_FILENAME, pspec);
1087
1088 pspec = g_param_spec_boolean ("keep-aspect-ratio",
1089 P_("Keep Aspect Ratio"),
1090 P_("Keep the aspect ratio of the texture when requesting the preferred width or height"),
1091 FALSE,
1092 CLUTTER_PARAM_READWRITE);
1093 obj_props[PROP_KEEP_ASPECT_RATIO] = pspec;
1094 g_object_class_install_property (gobject_class, PROP_KEEP_ASPECT_RATIO, pspec);
1095
1096 /**
1097 * ClutterTexture:load-async:
1098 *
1099 * Tries to load a texture from a filename by using a local thread to perform
1100 * the read operations. The initially created texture has dimensions 0x0 when
1101 * the true size becomes available the #ClutterTexture::size-change signal is
1102 * emitted and when the image has completed loading the
1103 * #ClutterTexture::load-finished signal is emitted.
1104 *
1105 * Threading is only enabled if g_thread_init() has been called prior to
1106 * clutter_init(), otherwise #ClutterTexture will use the main loop to load
1107 * the image.
1108 *
1109 * The upload of the texture data on the GL pipeline is not asynchronous, as
1110 * it must be performed from within the same thread that called
1111 * clutter_main().
1112 *
1113 * Since: 1.0
1114 *
1115 * Deprecated: 1.12: Use platform-specific image loading API, like GdkPixbuf
1116 */
1117 pspec = g_param_spec_boolean ("load-async",
1118 P_("Load asynchronously"),
1119 P_("Load files inside a thread to avoid blocking when loading images from disk"),
1120 FALSE,
1121 CLUTTER_PARAM_WRITABLE);
1122 obj_props[PROP_LOAD_ASYNC] = pspec;
1123 g_object_class_install_property (gobject_class, PROP_LOAD_ASYNC, pspec);
1124
1125
1126 /**
1127 * ClutterTexture:load-data-async:
1128 *
1129 * Like #ClutterTexture:load-async but loads the width and height
1130 * synchronously causing some blocking.
1131 *
1132 * Since: 1.0
1133 *
1134 * Deprecated: 1.12: Use platform-specific image loading API, like GdkPixbuf
1135 */
1136 pspec = g_param_spec_boolean ("load-data-async",
1137 P_("Load data asynchronously"),
1138 P_("Decode image data files inside a thread to reduce blocking when loading images from disk"),
1139 FALSE,
1140 CLUTTER_PARAM_WRITABLE);
1141 obj_props[PROP_LOAD_DATA_ASYNC] = pspec;
1142 g_object_class_install_property (gobject_class, PROP_LOAD_DATA_ASYNC, pspec);
1143
1144 /**
1145 * ClutterTexture::pick-with-alpha:
1146 *
1147 * Determines whether a #ClutterTexture should have it's shape defined
1148 * by its alpha channel when picking.
1149 *
1150 * Be aware that this is a bit more costly than the default picking
1151 * due to the texture lookup, extra test against the alpha value and
1152 * the fact that it will also interrupt the batching of geometry
1153 * done internally.
1154 *
1155 * Also there is currently no control over the threshold used to
1156 * determine what value of alpha is considered pickable, and so
1157 * only fully opaque parts of the texture will react to picking.
1158 *
1159 * Since: 1.4
1160 *
1161 * Deprecated: 1.12: No replacement is available
1162 */
1163 pspec = g_param_spec_boolean ("pick-with-alpha",
1164 P_("Pick With Alpha"),
1165 P_("Shape actor with alpha channel when picking"),
1166 FALSE,
1167 CLUTTER_PARAM_READWRITE);
1168 obj_props[PROP_PICK_WITH_ALPHA] = pspec;
1169 g_object_class_install_property (gobject_class, PROP_PICK_WITH_ALPHA, pspec);
1170
1171 /**
1172 * ClutterTexture::size-change:
1173 * @texture: the texture which received the signal
1174 * @width: the width of the new texture
1175 * @height: the height of the new texture
1176 *
1177 * The ::size-change signal is emitted each time the size of the
1178 * pixbuf used by @texture changes. The new size is given as
1179 * argument to the callback.
1180 *
1181 * Deprecated: 1.12: No replacement is available
1182 */
1183 texture_signals[SIZE_CHANGE] =
1184 g_signal_new ("size-change",
1185 G_TYPE_FROM_CLASS (gobject_class),
1186 G_SIGNAL_RUN_LAST,
1187 G_STRUCT_OFFSET (ClutterTextureClass, size_change),
1188 NULL, NULL,
1189 _clutter_marshal_VOID__INT_INT,
1190 G_TYPE_NONE, 2,
1191 G_TYPE_INT,
1192 G_TYPE_INT);
1193 /**
1194 * ClutterTexture::pixbuf-change:
1195 * @texture: the texture which received the signal
1196 *
1197 * The ::pixbuf-change signal is emitted each time the pixbuf
1198 * used by @texture changes.
1199 *
1200 * Deprecated: 1.12: No replacement is available
1201 */
1202 texture_signals[PIXBUF_CHANGE] =
1203 g_signal_new ("pixbuf-change",
1204 G_TYPE_FROM_CLASS (gobject_class),
1205 G_SIGNAL_RUN_LAST,
1206 G_STRUCT_OFFSET (ClutterTextureClass, pixbuf_change),
1207 NULL, NULL,
1208 _clutter_marshal_VOID__VOID,
1209 G_TYPE_NONE,
1210 0);
1211 /**
1212 * ClutterTexture::load-finished:
1213 * @texture: the texture which received the signal
1214 * @error: A set error, or %NULL
1215 *
1216 * The ::load-finished signal is emitted when a texture load has
1217 * completed. If there was an error during loading, @error will
1218 * be set, otherwise it will be %NULL
1219 *
1220 * Since: 1.0
1221 *
1222 * Deprecated: 1.12: No replacement is available
1223 */
1224 texture_signals[LOAD_FINISHED] =
1225 g_signal_new (I_("load-finished"),
1226 G_TYPE_FROM_CLASS (gobject_class),
1227 G_SIGNAL_RUN_LAST,
1228 G_STRUCT_OFFSET (ClutterTextureClass, load_finished),
1229 NULL, NULL,
1230 _clutter_marshal_VOID__BOXED,
1231 G_TYPE_NONE,
1232 1,
1233 G_TYPE_ERROR);
1234 }
1235
1236 static ClutterScriptableIface *parent_scriptable_iface = NULL;
1237
1238 static void
1239 clutter_texture_set_custom_property (ClutterScriptable *scriptable,
1240 ClutterScript *script,
1241 const gchar *name,
1242 const GValue *value)
1243 {
1244 ClutterTexture *texture = CLUTTER_TEXTURE (scriptable);
1245
1246 if (strcmp ("filename", name) == 0)
1247 {
1248 const gchar *str = g_value_get_string (value);
1249 gchar *path;
1250 GError *error;
1251
1252 path = clutter_script_lookup_filename (script, str);
1253 if (G_UNLIKELY (!path))
1254 {
1255 g_warning ("Unable to find image %s", str);
1256 return;
1257 }
1258
1259 error = NULL;
1260 clutter_texture_set_from_file (texture, path, &error);
1261 if (error)
1262 {
1263 g_warning ("Unable to open image path at '%s': %s",
1264 path,
1265 error->message);
1266 g_error_free (error);
1267 }
1268
1269 g_free (path);
1270 }
1271 else
1272 {
1273 /* chain up */
1274 if (parent_scriptable_iface->set_custom_property)
1275 parent_scriptable_iface->set_custom_property (scriptable, script,
1276 name,
1277 value);
1278 }
1279 }
1280
1281 static void
1282 clutter_scriptable_iface_init (ClutterScriptableIface *iface)
1283 {
1284 parent_scriptable_iface = g_type_interface_peek_parent (iface);
1285
1286 if (!parent_scriptable_iface)
1287 parent_scriptable_iface = g_type_default_interface_peek
1288 (CLUTTER_TYPE_SCRIPTABLE);
1289
1290 iface->set_custom_property = clutter_texture_set_custom_property;
1291 }
1292
1293 static void
1294 clutter_texture_init (ClutterTexture *self)
1295 {
1296 ClutterTexturePrivate *priv;
1297
1298 self->priv = priv = clutter_texture_get_instance_private (self);
1299
1300 priv->repeat_x = FALSE;
1301 priv->repeat_y = FALSE;
1302 priv->sync_actor_size = TRUE;
1303 priv->fbo_handle = NULL;
1304 priv->pick_pipeline = NULL;
1305 priv->keep_aspect_ratio = FALSE;
1306 priv->pick_with_alpha = FALSE;
1307 priv->pick_with_alpha_supported = TRUE;
1308 priv->seen_create_pick_pipeline_warning = FALSE;
1309
1310 if (G_UNLIKELY (texture_template_pipeline == NULL))
1311 {
1312 CoglPipeline *pipeline;
1313 CoglContext *ctx =
1314 clutter_backend_get_cogl_context (clutter_get_default_backend ());
1315
1316 texture_template_pipeline = cogl_pipeline_new (ctx);
1317 pipeline = COGL_PIPELINE (texture_template_pipeline);
1318 cogl_pipeline_set_layer_null_texture (pipeline,
1319 0, /* layer_index */
1320 COGL_TEXTURE_TYPE_2D);
1321 }
1322
1323 g_assert (texture_template_pipeline != NULL);
1324 priv->pipeline = cogl_pipeline_copy (texture_template_pipeline);
1325 }
1326
1327 /**
1328 * clutter_texture_get_cogl_material:
1329 * @texture: A #ClutterTexture
1330 *
1331 * Returns a handle to the underlying COGL material used for drawing
1332 * the actor.
1333 *
1334 * Return value: (transfer none): a handle for a #CoglMaterial. The
1335 * material is owned by the #ClutterTexture and it should not be
1336 * unreferenced
1337 *
1338 * Since: 1.0
1339 *
1340 * Deprecated: 1.12: No replacement is available; it's not advisable
1341 * to modify the Cogl pipeline of an actor. Use a #ClutterContent
1342 * implementation and modify the pipeline during the paint sequence
1343 */
1344 CoglHandle
1345 clutter_texture_get_cogl_material (ClutterTexture *texture)
1346 {
1347 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), NULL);
1348
1349 return texture->priv->pipeline;
1350 }
1351
1352 /**
1353 * clutter_texture_set_cogl_material:
1354 * @texture: A #ClutterTexture
1355 * @cogl_material: A CoglHandle for a material
1356 *
1357 * Replaces the underlying Cogl material drawn by this actor with
1358 * @cogl_material. A reference to the material is taken so if the
1359 * handle is no longer needed it should be deref'd with
1360 * cogl_handle_unref. Texture data is attached to the material so
1361 * calling this function also replaces the Cogl
1362 * texture. #ClutterTexture requires that the material have a texture
1363 * layer so you should set one on the material before calling this
1364 * function.
1365 *
1366 * Since: 0.8
1367 *
1368 * Deprecated: 1.12: No replacement is available; it's not advisable
1369 * to modify the Cogl pipeline of an actor. Use a #ClutterContent
1370 * implementation and modify the pipeline during the paint sequence
1371 */
1372 void
1373 clutter_texture_set_cogl_material (ClutterTexture *texture,
1374 CoglHandle cogl_material)
1375 {
1376 CoglPipeline *cogl_pipeline = cogl_material;
1377 CoglHandle cogl_texture;
1378
1379 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
1380
1381 cogl_object_ref (cogl_pipeline);
1382
1383 if (texture->priv->pipeline)
1384 cogl_object_unref (texture->priv->pipeline);
1385
1386 texture->priv->pipeline = cogl_pipeline;
1387
1388 /* XXX: We are re-asserting the first layer of the new pipeline to ensure the
1389 * priv state is in sync with the contents of the pipeline. */
1390 cogl_texture = clutter_texture_get_cogl_texture (texture);
1391 clutter_texture_set_cogl_texture (texture, cogl_texture);
1392 /* XXX: If we add support for more pipeline layers, this will need
1393 * extending */
1394 }
1395
1396 typedef struct _GetLayerState
1397 {
1398 gboolean has_layer;
1399 int first_layer;
1400 } GetLayerState;
1401
1402 static gboolean
1403 layer_cb (CoglPipeline *pipeline, int layer, void *user_data)
1404 {
1405 GetLayerState *state = user_data;
1406
1407 state->has_layer = TRUE;
1408 state->first_layer = layer;
1409
1410 /* We only care about the first layer. */
1411 return FALSE;
1412 }
1413
1414 static gboolean
1415 get_first_layer_index (CoglPipeline *pipeline, int *layer_index)
1416 {
1417 GetLayerState state = { FALSE };
1418 cogl_pipeline_foreach_layer (pipeline,
1419 layer_cb,
1420 &state);
1421 if (state.has_layer)
1422 *layer_index = state.first_layer;
1423
1424 return state.has_layer;
1425 }
1426
1427 /**
1428 * clutter_texture_get_cogl_texture:
1429 * @texture: A #ClutterTexture
1430 *
1431 * Retrieves the handle to the underlying COGL texture used for drawing
1432 * the actor. No extra reference is taken so if you need to keep the
1433 * handle then you should call cogl_handle_ref() on it.
1434 *
1435 * The texture handle returned is the first layer of the material
1436 * handle used by the #ClutterTexture. If you need to access the other
1437 * layers you should use clutter_texture_get_cogl_material() instead
1438 * and use the #CoglMaterial API.
1439 *
1440 * Return value: (transfer none): a #CoglHandle for the texture. The returned
1441 * handle is owned by the #ClutterTexture and it should not be unreferenced
1442 *
1443 * Since: 0.8
1444 *
1445 * Deprecated: 1.12: No replacement available; it's not advisable to
1446 * modify the Cogl pipeline of an actor. Use a #ClutterContent
1447 * implementation and set up the pipeline during the paint sequence
1448 * instead.
1449 */
1450 CoglHandle
1451 clutter_texture_get_cogl_texture (ClutterTexture *texture)
1452 {
1453 ClutterTexturePrivate *priv;
1454 int layer_index;
1455
1456 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), NULL);
1457
1458 priv = texture->priv;
1459
1460 if (get_first_layer_index (priv->pipeline, &layer_index))
1461 return cogl_pipeline_get_layer_texture (priv->pipeline, layer_index);
1462 else
1463 return NULL;
1464 }
1465
1466 /**
1467 * clutter_texture_set_cogl_texture:
1468 * @texture: A #ClutterTexture
1469 * @cogl_tex: A CoglHandle for a texture
1470 *
1471 * Replaces the underlying COGL texture drawn by this actor with
1472 * @cogl_tex. A reference to the texture is taken so if the handle is
1473 * no longer needed it should be deref'd with cogl_handle_unref.
1474 *
1475 * Since: 0.8
1476 *
1477 * Deprecated: 1.12: No replacement available; it's not advisable to
1478 * modify the Cogl pipeline of an actor. Use a #ClutterContent
1479 * implementation and set up the pipeline during the paint sequence
1480 * instead.
1481 */
1482 void
1483 clutter_texture_set_cogl_texture (ClutterTexture *texture,
1484 CoglHandle cogl_tex)
1485 {
1486 ClutterTexturePrivate *priv;
1487 gboolean size_changed;
1488 guint width, height;
1489
1490 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
1491 g_return_if_fail (cogl_is_texture (cogl_tex));
1492
1493 /* This function can set the texture without the actor being
1494 realized. This is ok because Clutter requires that the GL context
1495 always be current so there is no point in waiting to realization
1496 to set the texture. */
1497
1498 priv = texture->priv;
1499
1500 width = cogl_texture_get_width (cogl_tex);
1501 height = cogl_texture_get_height (cogl_tex);
1502
1503 /* Reference the new texture now in case it is the same one we are
1504 already using */
1505 cogl_object_ref (cogl_tex);
1506
1507 /* Remove FBO if exisiting */
1508 if (priv->fbo_source)
1509 texture_fbo_free_resources (texture);
1510
1511 /* Remove old texture */
1512 texture_free_gl_resources (texture);
1513
1514 /* Use the new texture */
1515 if (priv->pipeline == NULL)
1516 priv->pipeline = cogl_pipeline_copy (texture_template_pipeline);
1517
1518 g_assert (priv->pipeline != NULL);
1519 cogl_pipeline_set_layer_texture (priv->pipeline, 0, cogl_tex);
1520
1521 /* The pipeline now holds a reference to the texture so we can
1522 safely release the reference we claimed above */
1523 cogl_object_unref (cogl_tex);
1524
1525 size_changed = (width != priv->image_width || height != priv->image_height);
1526 priv->image_width = width;
1527 priv->image_height = height;
1528
1529 CLUTTER_NOTE (TEXTURE, "set size (w:%d, h:%d)",
1530 priv->image_width,
1531 priv->image_height);
1532
1533 if (size_changed)
1534 {
1535 g_signal_emit (texture, texture_signals[SIZE_CHANGE], 0,
1536 priv->image_width,
1537 priv->image_height);
1538
1539 if (priv->sync_actor_size)
1540 {
1541 ClutterActor *actor = CLUTTER_ACTOR (texture);
1542
1543 /* we have been requested to keep the actor size in
1544 * sync with the texture data; if we also want to
1545 * maintain the aspect ratio we want to change the
1546 * requisition mode depending on the orientation of
1547 * the texture, so that the parent container can do
1548 * the right thing
1549 */
1550 if (priv->keep_aspect_ratio)
1551 {
1552 ClutterRequestMode request;
1553
1554 if (priv->image_width >= priv->image_height)
1555 request = CLUTTER_REQUEST_HEIGHT_FOR_WIDTH;
1556 else
1557 request = CLUTTER_REQUEST_WIDTH_FOR_HEIGHT;
1558
1559 clutter_actor_set_request_mode (actor, request);
1560 }
1561
1562 clutter_actor_queue_relayout (CLUTTER_ACTOR (texture));
1563 }
1564 }
1565
1566 /* rename signal */
1567 g_signal_emit (texture, texture_signals[PIXBUF_CHANGE], 0);
1568
1569 /* If resized actor may need resizing but paint() will do this */
1570 clutter_actor_queue_redraw (CLUTTER_ACTOR (texture));
1571
1572 g_object_notify_by_pspec (G_OBJECT (texture), obj_props[PROP_COGL_TEXTURE]);
1573 }
1574
1575 static gboolean
1576 clutter_texture_set_from_data (ClutterTexture *texture,
1577 const guchar *data,
1578 CoglPixelFormat source_format,
1579 gint width,
1580 gint height,
1581 gint rowstride,
1582 gint bpp,
1583 GError **error)
1584 {
1585 ClutterTexturePrivate *priv = texture->priv;
1586 CoglHandle new_texture = NULL;
1587 CoglTextureFlags flags = COGL_TEXTURE_NONE;
1588
1589 if (priv->no_slice)
1590 flags |= COGL_TEXTURE_NO_SLICING;
1591
1592 /* FIXME if we are not realized, we should store the data
1593 * for future use, instead of creating the texture.
1594 */
1595 new_texture = cogl_texture_new_from_data (width, height,
1596 flags,
1597 source_format,
1598 COGL_PIXEL_FORMAT_ANY,
1599 rowstride,
1600 data);
1601
1602 if (G_UNLIKELY (new_texture == NULL))
1603 {
1604 GError *inner_error = NULL;
1605
1606 g_set_error (&inner_error, CLUTTER_TEXTURE_ERROR,
1607 CLUTTER_TEXTURE_ERROR_BAD_FORMAT,
1608 _("Failed to load the image data"));
1609
1610 g_signal_emit (texture, texture_signals[LOAD_FINISHED], 0, inner_error);
1611
1612 if (error != NULL)
1613 g_propagate_error (error, inner_error);
1614 else
1615 g_error_free (inner_error);
1616
1617 return FALSE;
1618 }
1619
1620 g_free (priv->filename);
1621 priv->filename = NULL;
1622
1623 clutter_texture_set_cogl_texture (texture, new_texture);
1624
1625 cogl_object_unref (new_texture);
1626
1627 g_signal_emit (texture, texture_signals[LOAD_FINISHED], 0, NULL);
1628
1629 return TRUE;
1630 }
1631
1632 static inline gboolean
1633 get_pixel_format_from_texture_flags (gint bpp,
1634 gboolean has_alpha,
1635 ClutterTextureFlags flags,
1636 CoglPixelFormat *source_format)
1637 {
1638 /* Convert the flags to a CoglPixelFormat */
1639 if (has_alpha)
1640 {
1641 if (G_UNLIKELY (bpp != 4))
1642 {
1643 g_warning ("Unsupported bytes per pixel value '%d': "
1644 "Clutter supports only a value of 4 "
1645 "for RGBA data",
1646 bpp);
1647 return FALSE;
1648 }
1649
1650 *source_format = COGL_PIXEL_FORMAT_RGBA_8888;
1651 }
1652 else
1653 {
1654 if (G_UNLIKELY (bpp != 3))
1655 {
1656 g_warning ("Unsupported bytes per pixel value '%d': "
1657 "Clutter supports only a BPP value of 3 "
1658 "for RGB data",
1659 bpp);
1660 return FALSE;
1661 }
1662
1663 *source_format = COGL_PIXEL_FORMAT_RGB_888;
1664 }
1665
1666 if ((flags & CLUTTER_TEXTURE_RGB_FLAG_BGR))
1667 *source_format |= COGL_BGR_BIT;
1668
1669 if ((flags & CLUTTER_TEXTURE_RGB_FLAG_PREMULT))
1670 *source_format |= COGL_PREMULT_BIT;
1671
1672 return TRUE;
1673 }
1674
1675 /**
1676 * clutter_texture_set_from_rgb_data:
1677 * @texture: a #ClutterTexture
1678 * @data: (array): image data in RGBA type colorspace.
1679 * @has_alpha: set to %TRUE if image data has an alpha channel.
1680 * @width: width in pixels of image data.
1681 * @height: height in pixels of image data
1682 * @rowstride: distance in bytes between row starts.
1683 * @bpp: bytes per pixel (currently only 3 and 4 supported, depending
1684 * on the value of @has_alpha)
1685 * @flags: #ClutterTextureFlags
1686 * @error: return location for a #GError, or %NULL.
1687 *
1688 * Sets #ClutterTexture image data.
1689 *
1690 * Return value: %TRUE on success, %FALSE on failure.
1691 *
1692 * Since: 0.4
1693 *
1694 * Deprecated: 1.12: Use #ClutterImage and clutter_image_set_data() instead
1695 */
1696 gboolean
1697 clutter_texture_set_from_rgb_data (ClutterTexture *texture,
1698 const guchar *data,
1699 gboolean has_alpha,
1700 gint width,
1701 gint height,
1702 gint rowstride,
1703 gint bpp,
1704 ClutterTextureFlags flags,
1705 GError **error)
1706 {
1707 CoglPixelFormat source_format;
1708
1709 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), FALSE);
1710
1711 if (!get_pixel_format_from_texture_flags (bpp,
1712 has_alpha,
1713 flags,
1714 &source_format))
1715 {
1716 return FALSE;
1717 }
1718
1719 return clutter_texture_set_from_data (texture, data,
1720 source_format,
1721 width, height,
1722 rowstride, bpp,
1723 error);
1724 }
1725
1726 /**
1727 * clutter_texture_set_from_yuv_data:
1728 * @texture: A #ClutterTexture
1729 * @data: (array): Image data in YUV type colorspace.
1730 * @width: Width in pixels of image data.
1731 * @height: Height in pixels of image data
1732 * @flags: #ClutterTextureFlags
1733 * @error: Return location for a #GError, or %NULL.
1734 *
1735 * Sets a #ClutterTexture from YUV image data. If an error occurred,
1736 * %FALSE is returned and @error is set.
1737 *
1738 * The YUV support depends on the driver; the format supported by the
1739 * few drivers exposing this capability are not really useful.
1740 *
1741 * The proper way to convert image data in any YUV colorspace to any
1742 * RGB colorspace is to use a fragment shader associated with the
1743 * #ClutterTexture material.
1744 *
1745 * Return value: %TRUE if the texture was successfully updated
1746 *
1747 * Since: 0.4
1748 *
1749 * Deprecated: 1.10: Use a custom #ClutterContent implementation and
1750 * set up the Cogl pipeline using a #ClutterPipelineNode with a
1751 * fragment shader instead.
1752 */
1753 gboolean
1754 clutter_texture_set_from_yuv_data (ClutterTexture *texture,
1755 const guchar *data,
1756 gint width,
1757 gint height,
1758 ClutterTextureFlags flags,
1759 GError **error)
1760 {
1761 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), FALSE);
1762
1763 if (!clutter_feature_available (CLUTTER_FEATURE_TEXTURE_YUV))
1764 {
1765 g_set_error (error, CLUTTER_TEXTURE_ERROR,
1766 CLUTTER_TEXTURE_ERROR_NO_YUV,
1767 _("YUV textures are not supported"));
1768 return FALSE;
1769 }
1770
1771 /* Convert the flags to a CoglPixelFormat */
1772 if ((flags & CLUTTER_TEXTURE_YUV_FLAG_YUV2))
1773 {
1774 g_set_error (error, CLUTTER_TEXTURE_ERROR,
1775 CLUTTER_TEXTURE_ERROR_BAD_FORMAT,
1776 _("YUV2 textures are not supported"));
1777 return FALSE;
1778 }
1779
1780 return clutter_texture_set_from_data (texture, data,
1781 COGL_PIXEL_FORMAT_YUV,
1782 width, height,
1783 width * 3, 3,
1784 error);
1785 }
1786
1787 /*
1788 * clutter_texture_async_load_complete:
1789 * @self: a #ClutterTexture
1790 * @bitmap: a handle to a CoglBitmap
1791 * @error: load error
1792 *
1793 * If @error is %NULL, loads @bitmap into a #CoglTexture.
1794 *
1795 * This function emits the ::load-finished signal on @self.
1796 */
1797 static void
1798 clutter_texture_async_load_complete (ClutterTexture *self,
1799 CoglHandle bitmap,
1800 const GError *error)
1801 {
1802 ClutterTexturePrivate *priv = self->priv;
1803 CoglTextureFlags flags = COGL_TEXTURE_NONE;
1804 CoglHandle handle;
1805
1806 priv->async_data = NULL;
1807
1808 if (error == NULL)
1809 {
1810 if (priv->no_slice)
1811 flags |= COGL_TEXTURE_NO_SLICING;
1812
1813 handle = cogl_texture_new_from_bitmap (bitmap,
1814 flags,
1815 COGL_PIXEL_FORMAT_ANY);
1816 clutter_texture_set_cogl_texture (self, handle);
1817
1818 if (priv->load_size_async)
1819 {
1820 g_signal_emit (self, texture_signals[SIZE_CHANGE], 0,
1821 cogl_texture_get_width (handle),
1822 cogl_texture_get_height (handle));
1823 }
1824
1825 cogl_object_unref (handle);
1826 }
1827
1828 g_signal_emit (self, texture_signals[LOAD_FINISHED], 0, error);
1829
1830 clutter_actor_queue_relayout (CLUTTER_ACTOR (self));
1831 }
1832
1833 static gboolean
1834 texture_repaint_upload_func (gpointer user_data)
1835 {
1836 g_mutex_lock (&upload_list_mutex);
1837
1838 if (upload_list != NULL)
1839 {
1840 gint64 start_time = g_get_monotonic_time ();
1841
1842 /* continue uploading textures as long as we havent spent more
1843 * then 5ms doing so this stage redraw cycle.
1844 */
1845 do
1846 {
1847 ClutterTextureAsyncData *async_data = upload_list->data;
1848
1849 clutter_texture_async_data_lock (async_data);
1850
1851 if (async_data->state & ASYNC_STATE_QUEUED)
1852 {
1853 CLUTTER_NOTE (TEXTURE, "[async] operation complete for '%s'",
1854 async_data->load_filename);
1855
1856 clutter_texture_async_load_complete (async_data->texture,
1857 async_data->load_bitmap,
1858 async_data->load_error);
1859 }
1860 else
1861 CLUTTER_NOTE (TEXTURE, "[async] operation cancelled for '%s'",
1862 async_data->load_filename);
1863
1864 clutter_texture_async_data_unlock (async_data);
1865
1866 upload_list = g_list_remove (upload_list, async_data);
1867 clutter_texture_async_data_free (async_data);
1868 }
1869 while (upload_list != NULL &&
1870 g_get_monotonic_time () < start_time + 5 * 1000L);
1871 }
1872
1873 if (upload_list != NULL)
1874 {
1875 ClutterMasterClock *master_clock;
1876
1877 master_clock = _clutter_master_clock_get_default ();
1878 _clutter_master_clock_ensure_next_iteration (master_clock);
1879 }
1880
1881 g_mutex_unlock (&upload_list_mutex);
1882
1883 return TRUE;
1884 }
1885
1886 static void
1887 clutter_texture_thread_load (gpointer user_data,
1888 gpointer pool_data)
1889 {
1890 ClutterTextureAsyncData *async_data = user_data;
1891 ClutterMasterClock *master_clock = _clutter_master_clock_get_default ();
1892
1893 clutter_texture_async_data_lock (async_data);
1894
1895 if (~async_data->state & ASYNC_STATE_CANCELLED)
1896 {
1897 CLUTTER_NOTE (TEXTURE, "[async] loading bitmap from file '%s'",
1898 async_data->load_filename);
1899
1900 async_data->load_bitmap =
1901 cogl_bitmap_new_from_file (async_data->load_filename,
1902 &async_data->load_error);
1903
1904 g_mutex_lock (&upload_list_mutex);
1905
1906 if (repaint_upload_func == 0)
1907 {
1908 repaint_upload_func =
1909 clutter_threads_add_repaint_func (texture_repaint_upload_func,
1910 NULL, NULL);
1911 }
1912
1913 upload_list = g_list_append (upload_list, async_data);
1914 async_data->state |= ASYNC_STATE_QUEUED;
1915
1916 CLUTTER_NOTE (TEXTURE, "[async] operation queued");
1917
1918 g_mutex_unlock (&upload_list_mutex);
1919 }
1920 else
1921 {
1922 clutter_texture_async_data_unlock (async_data);
1923 clutter_texture_async_data_free (async_data);
1924
1925 return;
1926 }
1927
1928 clutter_texture_async_data_unlock (async_data);
1929
1930 _clutter_master_clock_ensure_next_iteration (master_clock);
1931 }
1932
1933 static gboolean
1934 clutter_texture_idle_load (gpointer data)
1935 {
1936 ClutterTextureAsyncData *async_data = data;
1937
1938 async_data->load_bitmap =
1939 cogl_bitmap_new_from_file (async_data->load_filename,
1940 &async_data->load_error);
1941
1942 clutter_texture_async_load_complete (async_data->texture,
1943 async_data->load_bitmap,
1944 async_data->load_error);
1945
1946 clutter_texture_async_data_free (async_data);
1947
1948 return FALSE;
1949 }
1950
1951 /*
1952 * clutter_texture_async_load:
1953 * @self: a #ClutterTExture
1954 * @filename: name of the file to load
1955 * @error: return location for a #GError
1956 *
1957 * Starts an asynchronous load of the file name stored inside
1958 * the load_filename member of @data.
1959 *
1960 * If threading is enabled we use a GThread to perform the actual
1961 * I/O; if threading is not enabled, we use an idle GSource.
1962 *
1963 * The I/O is the only bit done in a thread -- uploading the
1964 * texture data to the GL pipeline must be done from within the
1965 * same thread that called clutter_main(). Threaded upload should
1966 * be part of the GL implementation.
1967 *
1968 * This function will block until we get a size from the file
1969 * so that we can effectively get the size the texture actor after
1970 * clutter_texture_set_from_file().
1971 *
1972 * Return value: %TRUE if the asynchronous loading was successfully
1973 * initiated, %FALSE otherwise
1974 */
1975 static gboolean
1976 clutter_texture_async_load (ClutterTexture *self,
1977 const gchar *filename,
1978 GError **error)
1979 {
1980 ClutterTexturePrivate *priv = self->priv;
1981 ClutterTextureAsyncData *data;
1982 gint width, height;
1983 gboolean res;
1984
1985 /* ask the file for a size; if we cannot get the size then
1986 * there's no point in even continuing the asynchronous
1987 * loading, so we just stop there
1988 */
1989
1990 if (priv->load_size_async)
1991 {
1992 res = TRUE;
1993 width = 0;
1994 height = 0;
1995 }
1996 else
1997 res = cogl_bitmap_get_size_from_file (filename, &width, &height);
1998
1999 if (!res)
2000 {
2001 g_set_error (error, CLUTTER_TEXTURE_ERROR,
2002 CLUTTER_TEXTURE_ERROR_BAD_FORMAT,
2003 _("Failed to load the image data"));
2004 return FALSE;
2005 }
2006 else
2007 {
2008 priv->image_width = width;
2009 priv->image_height = height;
2010 }
2011
2012 clutter_texture_async_load_cancel (self);
2013
2014 data = g_slice_new0 (ClutterTextureAsyncData);
2015
2016 data->texture = self;
2017 data->load_filename = g_strdup (filename);
2018
2019 priv->async_data = data;
2020
2021 if (1)
2022 {
2023 if (G_UNLIKELY (async_thread_pool == NULL))
2024 {
2025 /* This apparently can't fail if exclusive == FALSE */
2026 async_thread_pool =
2027 g_thread_pool_new (clutter_texture_thread_load, NULL,
2028 1,
2029 FALSE,
2030 NULL);
2031 }
2032
2033 g_thread_pool_push (async_thread_pool, data, NULL);
2034 }
2035 else
2036 {
2037 data->load_idle =
2038 clutter_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
2039 clutter_texture_idle_load,
2040 data,
2041 NULL);
2042 }
2043
2044 return TRUE;
2045 }
2046
2047 /**
2048 * clutter_texture_set_from_file:
2049 * @texture: A #ClutterTexture
2050 * @filename: The filename of the image in GLib file name encoding
2051 * @error: Return location for a #GError, or %NULL
2052 *
2053 * Sets the #ClutterTexture image data from an image file. In case of
2054 * failure, %FALSE is returned and @error is set.
2055 *
2056 * If #ClutterTexture:load-async is set to %TRUE, this function
2057 * will return as soon as possible, and the actual image loading
2058 * from disk will be performed asynchronously. #ClutterTexture::size-change
2059 * will be emitten when the size of the texture is available and
2060 * #ClutterTexture::load-finished will be emitted when the image has been
2061 * loaded or if an error occurred.
2062 *
2063 * Return value: %TRUE if the image was successfully loaded and set
2064 *
2065 * Since: 0.8
2066 *
2067 * Deprecated: 1.12: Use #ClutterImage and platform-specific image
2068 * loading API, like GdkPixbuf, instead
2069 */
2070 gboolean
2071 clutter_texture_set_from_file (ClutterTexture *texture,
2072 const gchar *filename,
2073 GError **error)
2074 {
2075 ClutterTexturePrivate *priv;
2076 CoglHandle new_texture = NULL;
2077 GError *internal_error = NULL;
2078 CoglTextureFlags flags = COGL_TEXTURE_NONE;
2079
2080 priv = texture->priv;
2081
2082 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2083
2084 if (priv->load_data_async)
2085 return clutter_texture_async_load (texture, filename, error);
2086
2087 if (priv->no_slice)
2088 flags |= COGL_TEXTURE_NO_SLICING;
2089
2090 new_texture = cogl_texture_new_from_file (filename,
2091 flags,
2092 COGL_PIXEL_FORMAT_ANY,
2093 &internal_error);
2094
2095 /* If COGL didn't give an error then make one up */
2096 if (internal_error == NULL && new_texture == NULL)
2097 {
2098 g_set_error (&internal_error, CLUTTER_TEXTURE_ERROR,
2099 CLUTTER_TEXTURE_ERROR_BAD_FORMAT,
2100 _("Failed to load the image data"));
2101 }
2102
2103 if (internal_error != NULL)
2104 {
2105 g_signal_emit (texture, texture_signals[LOAD_FINISHED], 0,
2106 internal_error);
2107
2108 g_propagate_error (error, internal_error);
2109
2110 return FALSE;
2111 }
2112
2113 g_free (priv->filename);
2114 priv->filename = g_strdup (filename);
2115
2116 clutter_texture_set_cogl_texture (texture, new_texture);
2117
2118 cogl_object_unref (new_texture);
2119
2120 g_signal_emit (texture, texture_signals[LOAD_FINISHED], 0, NULL);
2121
2122 g_object_notify_by_pspec (G_OBJECT (texture), obj_props[PROP_FILENAME]);
2123
2124 return TRUE;
2125 }
2126
2127 /**
2128 * clutter_texture_set_filter_quality:
2129 * @texture: a #ClutterTexture
2130 * @filter_quality: new filter quality value
2131 *
2132 * Sets the filter quality when scaling a texture. The quality is an
2133 * enumeration currently the following values are supported:
2134 * %CLUTTER_TEXTURE_QUALITY_LOW which is fast but only uses nearest neighbour
2135 * interpolation. %CLUTTER_TEXTURE_QUALITY_MEDIUM which is computationally a
2136 * bit more expensive (bilinear interpolation), and
2137 * %CLUTTER_TEXTURE_QUALITY_HIGH which uses extra texture memory resources to
2138 * improve scaled down rendering as well (by using mipmaps). The default value
2139 * is %CLUTTER_TEXTURE_QUALITY_MEDIUM.
2140 *
2141 * Since: 0.8
2142 *
2143 * Deprecated: 1.12: Use #ClutterImage and clutter_actor_set_content_scaling_filters()
2144 * instead
2145 */
2146 void
2147 clutter_texture_set_filter_quality (ClutterTexture *texture,
2148 ClutterTextureQuality filter_quality)
2149 {
2150 ClutterTexturePrivate *priv;
2151 ClutterTextureQuality old_quality;
2152
2153 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
2154
2155 priv = texture->priv;
2156
2157 old_quality = clutter_texture_get_filter_quality (texture);
2158
2159 if (filter_quality != old_quality)
2160 {
2161 gint min_filter, mag_filter;
2162
2163 min_filter = mag_filter = COGL_PIPELINE_FILTER_LINEAR;
2164 clutter_texture_quality_to_filters (filter_quality,
2165 &min_filter,
2166 &mag_filter);
2167
2168 cogl_pipeline_set_layer_filters (priv->pipeline, 0,
2169 min_filter, mag_filter);
2170
2171 clutter_actor_queue_redraw (CLUTTER_ACTOR (texture));
2172
2173 g_object_notify_by_pspec (G_OBJECT (texture), obj_props[PROP_FILTER_QUALITY]);
2174 }
2175 }
2176
2177 /**
2178 * clutter_texture_get_filter_quality:
2179 * @texture: A #ClutterTexture
2180 *
2181 * Gets the filter quality used when scaling a texture.
2182 *
2183 * Return value: The filter quality value.
2184 *
2185 * Since: 0.8
2186 *
2187 * Deprecated: 1.12: Use #ClutterImage and clutter_actor_get_content_scaling_filters()
2188 * instead
2189 */
2190 ClutterTextureQuality
2191 clutter_texture_get_filter_quality (ClutterTexture *texture)
2192 {
2193 ClutterTexturePrivate *priv;
2194 int layer_index;
2195 CoglPipelineFilter min_filter, mag_filter;
2196 int i;
2197
2198 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), 0);
2199
2200 priv = texture->priv;
2201
2202 if (get_first_layer_index (priv->pipeline, &layer_index))
2203 {
2204 min_filter = cogl_pipeline_get_layer_min_filter (priv->pipeline,
2205 layer_index);
2206 mag_filter = cogl_pipeline_get_layer_mag_filter (priv->pipeline,
2207 layer_index);
2208 }
2209 else
2210 return CLUTTER_TEXTURE_QUALITY_MEDIUM;
2211
2212 for (i = 0; i < G_N_ELEMENTS (clutter_texture_quality_filters); i++)
2213 if (clutter_texture_quality_filters[i].min_filter == min_filter
2214 && clutter_texture_quality_filters[i].mag_filter == mag_filter)
2215 return i;
2216
2217 /* Unknown filter combination */
2218 return CLUTTER_TEXTURE_QUALITY_LOW;
2219 }
2220
2221 /**
2222 * clutter_texture_get_max_tile_waste:
2223 * @texture: A #ClutterTexture
2224 *
2225 * Gets the maximum waste that will be used when creating a texture or
2226 * -1 if slicing is disabled.
2227 *
2228 * Return value: The maximum waste or -1 if the texture waste is
2229 * unlimited.
2230 *
2231 * Since: 0.8
2232 *
2233 * Deprecated: 1.12: No replacement is available
2234 */
2235 gint
2236 clutter_texture_get_max_tile_waste (ClutterTexture *texture)
2237 {
2238 ClutterTexturePrivate *priv;
2239 CoglHandle cogl_texture;
2240
2241 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), 0);
2242
2243 priv = texture->priv;
2244
2245 cogl_texture = clutter_texture_get_cogl_texture (texture);
2246
2247 if (cogl_texture == NULL)
2248 return priv->no_slice ? -1 : COGL_TEXTURE_MAX_WASTE;
2249 else
2250 return cogl_texture_get_max_waste (cogl_texture);
2251 }
2252
2253 /**
2254 * clutter_texture_new_from_file:
2255 * @filename: The name of an image file to load.
2256 * @error: Return locatoin for an error.
2257 *
2258 * Creates a new ClutterTexture actor to display the image contained a
2259 * file. If the image failed to load then NULL is returned and @error
2260 * is set.
2261 *
2262 * Return value: A newly created #ClutterTexture object or NULL on
2263 * error.
2264 *
2265 * Since: 0.8
2266 *
2267 * Deprecated: 1.12: No direct replacement is available. Use #ClutterImage
2268 * and platform-specific image loading API, like GdkPixbuf, instead
2269 */
2270 ClutterActor*
2271 clutter_texture_new_from_file (const gchar *filename,
2272 GError **error)
2273 {
2274 ClutterActor *texture = clutter_texture_new ();
2275
2276 if (!clutter_texture_set_from_file (CLUTTER_TEXTURE (texture),
2277 filename, error))
2278 {
2279 g_object_ref_sink (texture);
2280 g_object_unref (texture);
2281
2282 return NULL;
2283 }
2284 else
2285 return texture;
2286 }
2287
2288 /**
2289 * clutter_texture_new:
2290 *
2291 * Creates a new empty #ClutterTexture object.
2292 *
2293 * Return value: A newly created #ClutterTexture object.
2294 *
2295 * Deprecated: 1.12: Use #ClutterImage instead
2296 */
2297 ClutterActor *
2298 clutter_texture_new (void)
2299 {
2300 return g_object_new (CLUTTER_TYPE_TEXTURE, NULL);
2301 }
2302
2303 /**
2304 * clutter_texture_get_base_size:
2305 * @texture: a #ClutterTexture
2306 * @width: (out): return location for the width, or %NULL
2307 * @height: (out): return location for the height, or %NULL
2308 *
2309 * Gets the size in pixels of the untransformed underlying image
2310 *
2311 * Deprecated: 1.12: Use #ClutterImage and clutter_content_get_preferred_size()
2312 * instead
2313 */
2314 void
2315 clutter_texture_get_base_size (ClutterTexture *texture,
2316 gint *width,
2317 gint *height)
2318 {
2319 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
2320
2321 if (width)
2322 *width = texture->priv->image_width;
2323
2324 if (height)
2325 *height = texture->priv->image_height;
2326 }
2327
2328 /**
2329 * clutter_texture_set_area_from_rgb_data:
2330 * @texture: A #ClutterTexture
2331 * @data: (array): Image data in RGB type colorspace.
2332 * @has_alpha: Set to TRUE if image data has an alpha channel.
2333 * @x: X coordinate of upper left corner of region to update.
2334 * @y: Y coordinate of upper left corner of region to update.
2335 * @width: Width in pixels of region to update.
2336 * @height: Height in pixels of region to update.
2337 * @rowstride: Distance in bytes between row starts on source buffer.
2338 * @bpp: bytes per pixel (Currently only 3 and 4 supported,
2339 * depending on @has_alpha)
2340 * @flags: #ClutterTextureFlags
2341 * @error: return location for a #GError, or %NULL
2342 *
2343 * Updates a sub-region of the pixel data in a #ClutterTexture.
2344 *
2345 * Return value: %TRUE on success, %FALSE on failure.
2346 *
2347 * Since: 0.6
2348 *
2349 * Deprecated: 1.12: Use #ClutterImage and clutter_image_set_area() instead
2350 */
2351 gboolean
2352 clutter_texture_set_area_from_rgb_data (ClutterTexture *texture,
2353 const guchar *data,
2354 gboolean has_alpha,
2355 gint x,
2356 gint y,
2357 gint width,
2358 gint height,
2359 gint rowstride,
2360 gint bpp,
2361 ClutterTextureFlags flags,
2362 GError **error)
2363 {
2364 CoglPixelFormat source_format;
2365 CoglHandle cogl_texture;
2366
2367 if (!get_pixel_format_from_texture_flags (bpp, has_alpha, flags,
2368 &source_format))
2369 {
2370 return FALSE;
2371 }
2372
2373 /* attempt to realize ... */
2374 if (!CLUTTER_ACTOR_IS_REALIZED (texture) &&
2375 clutter_actor_get_stage (CLUTTER_ACTOR (texture)) != NULL)
2376 {
2377 clutter_actor_realize (CLUTTER_ACTOR (texture));
2378 }
2379
2380 /* due to the fudging of clutter_texture_set_cogl_texture()
2381 * which allows setting a texture pre-realize, we may end
2382 * up having a texture even if we couldn't realize yet.
2383 */
2384 cogl_texture = clutter_texture_get_cogl_texture (texture);
2385 if (cogl_texture == NULL)
2386 {
2387 g_warning ("Failed to realize actor '%s'",
2388 _clutter_actor_get_debug_name (CLUTTER_ACTOR (texture)));
2389 return FALSE;
2390 }
2391
2392 if (!cogl_texture_set_region (cogl_texture,
2393 0, 0,
2394 x, y, width, height,
2395 width, height,
2396 source_format,
2397 rowstride,
2398 data))
2399 {
2400 g_set_error (error, CLUTTER_TEXTURE_ERROR,
2401 CLUTTER_TEXTURE_ERROR_BAD_FORMAT,
2402 _("Failed to load the image data"));
2403 return FALSE;
2404 }
2405
2406 g_free (texture->priv->filename);
2407 texture->priv->filename = NULL;
2408
2409 /* rename signal */
2410 g_signal_emit (texture, texture_signals[PIXBUF_CHANGE], 0);
2411
2412 clutter_actor_queue_redraw (CLUTTER_ACTOR (texture));
2413
2414 return TRUE;
2415 }
2416
2417 static void
2418 on_fbo_source_size_change (GObject *object,
2419 GParamSpec *param_spec,
2420 ClutterTexture *texture)
2421 {
2422 ClutterTexturePrivate *priv = texture->priv;
2423 gfloat w, h;
2424 ClutterActorBox box;
2425 gboolean status;
2426
2427 status = clutter_actor_get_paint_box (priv->fbo_source, &box);
2428 if (status)
2429 clutter_actor_box_get_size (&box, &w, &h);
2430
2431 /* In the end we will size the framebuffer according to the paint
2432 * box, but for code that does:
2433 * tex = clutter_texture_new_from_actor (src);
2434 * clutter_actor_get_size (tex, &width, &height);
2435 * it seems more helpfull to return the src actor size if it has a
2436 * degenerate paint box. The most likely reason it will have a
2437 * degenerate paint box is simply that the src currently has no
2438 * parent. */
2439 if (status == FALSE || w == 0 || h == 0)
2440 clutter_actor_get_size (priv->fbo_source, &w, &h);
2441
2442 /* We can't create a texture with a width or height of 0... */
2443 w = MAX (1, w);
2444 h = MAX (1, h);
2445
2446 if (w != priv->image_width || h != priv->image_height)
2447 {
2448 CoglTextureFlags flags = COGL_TEXTURE_NONE;
2449 CoglHandle tex;
2450
2451 /* tear down the FBO */
2452 if (priv->fbo_handle != NULL)
2453 cogl_object_unref (priv->fbo_handle);
2454
2455 texture_free_gl_resources (texture);
2456
2457 priv->image_width = w;
2458 priv->image_height = h;
2459
2460 flags |= COGL_TEXTURE_NO_SLICING;
2461
2462 tex = cogl_texture_new_with_size (MAX (priv->image_width, 1),
2463 MAX (priv->image_height, 1),
2464 flags,
2465 COGL_PIXEL_FORMAT_RGBA_8888_PRE);
2466
2467 cogl_pipeline_set_layer_texture (priv->pipeline, 0, tex);
2468
2469 priv->fbo_handle = cogl_offscreen_new_to_texture (tex);
2470
2471 /* The pipeline now has a reference to the texture so it will
2472 stick around */
2473 cogl_object_unref (tex);
2474
2475 if (priv->fbo_handle == NULL)
2476 {
2477 g_warning ("%s: Offscreen texture creation failed", G_STRLOC);
2478 return;
2479 }
2480
2481 clutter_actor_set_size (CLUTTER_ACTOR (texture), w, h);
2482 }
2483 }
2484
2485 static void
2486 on_fbo_parent_change (ClutterActor *actor,
2487 ClutterActor *old_parent,
2488 ClutterTexture *texture)
2489 {
2490 ClutterActor *parent = CLUTTER_ACTOR(texture);
2491
2492 while ((parent = clutter_actor_get_parent (parent)) != NULL)
2493 {
2494 if (parent == actor)
2495 {
2496 g_warning ("Offscreen texture is ancestor of source!");
2497 /* Desperate but will avoid infinite loops */
2498 clutter_actor_remove_child (parent, actor);
2499 }
2500 }
2501 }
2502
2503 static void
2504 fbo_source_queue_redraw_cb (ClutterActor *source,
2505 ClutterActor *origin,
2506 ClutterTexture *texture)
2507 {
2508 clutter_actor_queue_redraw (CLUTTER_ACTOR (texture));
2509 }
2510
2511 static void
2512 fbo_source_queue_relayout_cb (ClutterActor *source,
2513 ClutterTexture *texture)
2514 {
2515 clutter_actor_queue_relayout (CLUTTER_ACTOR (texture));
2516 }
2517
2518 /**
2519 * clutter_texture_new_from_actor:
2520 * @actor: A source #ClutterActor
2521 *
2522 * Creates a new #ClutterTexture object with its source a prexisting
2523 * actor (and associated children). The textures content will contain
2524 * 'live' redirected output of the actors scene.
2525 *
2526 * Note this function is intented as a utility call for uniformly applying
2527 * shaders to groups and other potential visual effects. It requires that
2528 * the %CLUTTER_FEATURE_OFFSCREEN feature is supported by the current backend
2529 * and the target system.
2530 *
2531 * Some tips on usage:
2532 *
2533 * - The source actor must be visible
2534 * - The source actor must have a parent in order for it to be
2535 * allocated a size from the layouting mechanism. If the source
2536 * actor does not have a parent when this function is called then
2537 * the ClutterTexture will adopt it and allocate it at its
2538 * preferred size. Using this you can clone an actor that is
2539 * otherwise not displayed. Because of this feature if you do
2540 * intend to display the source actor then you must make sure that
2541 * the actor is parented before calling
2542 * clutter_texture_new_from_actor() or that you unparent it before
2543 * adding it to a container.
2544 * - When getting the image for the clone texture, Clutter
2545 * will attempt to render the source actor exactly as it would
2546 * appear if it was rendered on screen. The source actor's parent
2547 * transformations are taken into account. Therefore if your
2548 * source actor is rotated along the X or Y axes so that it has
2549 * some depth, the texture will appear differently depending on
2550 * the on-screen location of the source actor. While painting the
2551 * source actor, Clutter will set up a temporary asymmetric
2552 * perspective matrix as the projection matrix so that the source
2553 * actor will be projected as if a small section of the screen was
2554 * being viewed. Before version 0.8.2, an orthogonal identity
2555 * projection was used which meant that the source actor would be
2556 * clipped if any part of it was not on the zero Z-plane.
2557 * - Avoid reparenting the source with the created texture.
2558 * - A group can be padded with a transparent rectangle as to
2559 * provide a border to contents for shader output (blurring text
2560 * for example).
2561 * - The texture will automatically resize to contain a further
2562 * transformed source. However, this involves overhead and can be
2563 * avoided by placing the source actor in a bounding group
2564 * sized large enough to contain any child tranformations.
2565 * - Uploading pixel data to the texture (e.g by using
2566 * clutter_texture_set_from_file()) will destroy the offscreen texture
2567 * data and end redirection.
2568 * - cogl_texture_get_data() with the handle returned by
2569 * clutter_texture_get_cogl_texture() can be used to read the
2570 * offscreen texture pixels into a pixbuf.
2571 *
2572 * Return value: A newly created #ClutterTexture object, or %NULL on failure.
2573 *
2574 * Since: 0.6
2575 *
2576 * Deprecated: 1.8: Use the #ClutterOffscreenEffect and #ClutterShaderEffect
2577 * directly on the intended #ClutterActor to replace the functionality of
2578 * this function.
2579 */
2580 ClutterActor *
2581 clutter_texture_new_from_actor (ClutterActor *actor)
2582 {
2583 ClutterTexture *texture;
2584 ClutterTexturePrivate *priv;
2585 gfloat w, h;
2586 ClutterActorBox box;
2587 gboolean status;
2588
2589 g_return_val_if_fail (CLUTTER_IS_ACTOR (actor), NULL);
2590
2591 if (clutter_feature_available (CLUTTER_FEATURE_OFFSCREEN) == FALSE)
2592 return NULL;
2593
2594 if (!CLUTTER_ACTOR_IS_REALIZED (actor))
2595 {
2596 clutter_actor_realize (actor);
2597
2598 if (!CLUTTER_ACTOR_IS_REALIZED (actor))
2599 return NULL;
2600 }
2601
2602 status = clutter_actor_get_paint_box (actor, &box);
2603 if (status)
2604 clutter_actor_box_get_size (&box, &w, &h);
2605
2606 /* In the end we will size the framebuffer according to the paint
2607 * box, but for code that does:
2608 * tex = clutter_texture_new_from_actor (src);
2609 * clutter_actor_get_size (tex, &width, &height);
2610 * it seems more helpfull to return the src actor size if it has a
2611 * degenerate paint box. The most likely reason it will have a
2612 * degenerate paint box is simply that the src currently has no
2613 * parent. */
2614 if (status == FALSE || w == 0 || h == 0)
2615 clutter_actor_get_size (actor, &w, &h);
2616
2617 /* We can't create a 0x0 fbo so always bump the size up to at least
2618 * 1 */
2619 w = MAX (1, w);
2620 h = MAX (1, h);
2621
2622 /* Hopefully now were good.. */
2623 texture = g_object_new (CLUTTER_TYPE_TEXTURE,
2624 "disable-slicing", TRUE,
2625 NULL);
2626
2627 priv = texture->priv;
2628
2629 priv->fbo_source = g_object_ref_sink (actor);
2630
2631 /* If the actor doesn't have a parent then claim it so that it will
2632 get a size allocation during layout */
2633 if (clutter_actor_get_parent (actor) == NULL)
2634 clutter_actor_add_child (CLUTTER_ACTOR (texture), actor);
2635
2636 /* Connect up any signals which could change our underlying size */
2637 g_signal_connect (actor,
2638 "notify::width",
2639 G_CALLBACK(on_fbo_source_size_change),
2640 texture);
2641 g_signal_connect (actor,
2642 "notify::height",
2643 G_CALLBACK(on_fbo_source_size_change),
2644 texture);
2645 g_signal_connect (actor,
2646 "notify::scale-x",
2647 G_CALLBACK(on_fbo_source_size_change),
2648 texture);
2649 g_signal_connect (actor,
2650 "notify::scale-y",
2651 G_CALLBACK(on_fbo_source_size_change),
2652 texture);
2653 g_signal_connect (actor,
2654 "notify::rotation-angle-x",
2655 G_CALLBACK(on_fbo_source_size_change),
2656 texture);
2657 g_signal_connect (actor,
2658 "notify::rotation-angle-y",
2659 G_CALLBACK(on_fbo_source_size_change),
2660 texture);
2661 g_signal_connect (actor,
2662 "notify::rotation-angle-z",
2663 G_CALLBACK(on_fbo_source_size_change),
2664 texture);
2665
2666 g_signal_connect (actor, "queue-relayout",
2667 G_CALLBACK (fbo_source_queue_relayout_cb), texture);
2668 g_signal_connect (actor, "queue-redraw",
2669 G_CALLBACK (fbo_source_queue_redraw_cb), texture);
2670
2671 /* And a warning if the source becomes a child of the texture */
2672 g_signal_connect (actor,
2673 "parent-set",
2674 G_CALLBACK(on_fbo_parent_change),
2675 texture);
2676
2677 priv->image_width = w;
2678 priv->image_height = h;
2679
2680 clutter_actor_set_size (CLUTTER_ACTOR (texture),
2681 priv->image_width,
2682 priv->image_height);
2683
2684 return CLUTTER_ACTOR (texture);
2685 }
2686
2687 static void
2688 texture_fbo_free_resources (ClutterTexture *texture)
2689 {
2690 ClutterTexturePrivate *priv;
2691
2692 priv = texture->priv;
2693
2694 if (priv->fbo_source != NULL)
2695 {
2696 ClutterActor *parent;
2697
2698 parent = clutter_actor_get_parent (priv->fbo_source);
2699
2700 /* If we parented the texture then unparent it again so that it
2701 will lose the reference */
2702 if (parent == CLUTTER_ACTOR (texture))
2703 clutter_actor_remove_child (parent, priv->fbo_source);
2704
2705 g_signal_handlers_disconnect_by_func
2706 (priv->fbo_source,
2707 G_CALLBACK(on_fbo_parent_change),
2708 texture);
2709
2710 g_signal_handlers_disconnect_by_func
2711 (priv->fbo_source,
2712 G_CALLBACK(on_fbo_source_size_change),
2713 texture);
2714
2715 g_signal_handlers_disconnect_by_func
2716 (priv->fbo_source,
2717 G_CALLBACK(fbo_source_queue_relayout_cb),
2718 texture);
2719
2720 g_signal_handlers_disconnect_by_func
2721 (priv->fbo_source,
2722 G_CALLBACK(fbo_source_queue_redraw_cb),
2723 texture);
2724
2725 g_object_unref (priv->fbo_source);
2726
2727 priv->fbo_source = NULL;
2728 }
2729
2730 if (priv->fbo_handle != NULL)
2731 {
2732 cogl_object_unref (priv->fbo_handle);
2733 priv->fbo_handle = NULL;
2734 }
2735 }
2736
2737 /**
2738 * clutter_texture_set_sync_size:
2739 * @texture: a #ClutterTexture
2740 * @sync_size: %TRUE if the texture should have the same size of the
2741 * underlying image data
2742 *
2743 * Sets whether @texture should have the same preferred size as the
2744 * underlying image data.
2745 *
2746 * Since: 1.0
2747 *
2748 * Deprecated: 1.12: No replacement is available. A #ClutterActor using
2749 * #ClutterImage with a %CLUTTER_REQUEST_CONTENT_SIZE request mode
2750 * will automatically bind the preferred size of the content to the
2751 * preferred size of the actor
2752 */
2753 void
2754 clutter_texture_set_sync_size (ClutterTexture *texture,
2755 gboolean sync_size)
2756 {
2757 ClutterTexturePrivate *priv;
2758
2759 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
2760
2761 priv = texture->priv;
2762
2763 if (priv->sync_actor_size != sync_size)
2764 {
2765 priv->sync_actor_size = sync_size;
2766
2767 clutter_actor_queue_relayout (CLUTTER_ACTOR (texture));
2768
2769 g_object_notify_by_pspec (G_OBJECT (texture), obj_props[PROP_SYNC_SIZE]);
2770 }
2771 }
2772
2773 /**
2774 * clutter_texture_get_sync_size:
2775 * @texture: a #ClutterTexture
2776 *
2777 * Retrieves the value set with clutter_texture_set_sync_size()
2778 *
2779 * Return value: %TRUE if the #ClutterTexture should have the same
2780 * preferred size of the underlying image data
2781 *
2782 * Since: 1.0
2783 *
2784 * Deprecated: 1.12: There is no direct replacement
2785 */
2786 gboolean
2787 clutter_texture_get_sync_size (ClutterTexture *texture)
2788 {
2789 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), FALSE);
2790
2791 return texture->priv->sync_actor_size;
2792 }
2793
2794 /**
2795 * clutter_texture_set_repeat:
2796 * @texture: a #ClutterTexture
2797 * @repeat_x: %TRUE if the texture should repeat horizontally
2798 * @repeat_y: %TRUE if the texture should repeat vertically
2799 *
2800 * Sets whether the @texture should repeat horizontally or
2801 * vertically when the actor size is bigger than the image size
2802 *
2803 * Since: 1.0
2804 *
2805 * Deprecated: 1.12: Use #ClutterImage and clutter_actor_set_content_repeat()
2806 * instead
2807 */
2808 void
2809 clutter_texture_set_repeat (ClutterTexture *texture,
2810 gboolean repeat_x,
2811 gboolean repeat_y)
2812 {
2813 ClutterTexturePrivate *priv;
2814 gboolean changed = FALSE;
2815
2816 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
2817
2818 priv = texture->priv;
2819
2820 g_object_freeze_notify (G_OBJECT (texture));
2821
2822 if (priv->repeat_x != repeat_x)
2823 {
2824 priv->repeat_x = repeat_x;
2825
2826 g_object_notify_by_pspec (G_OBJECT (texture), obj_props[PROP_REPEAT_X]);
2827
2828 changed = TRUE;
2829 }
2830
2831 if (priv->repeat_y != repeat_y)
2832 {
2833 priv->repeat_y = repeat_y;
2834
2835 g_object_notify_by_pspec (G_OBJECT (texture), obj_props[PROP_REPEAT_Y]);
2836
2837 changed = TRUE;
2838 }
2839
2840 if (changed)
2841 clutter_actor_queue_redraw (CLUTTER_ACTOR (texture));
2842
2843 g_object_thaw_notify (G_OBJECT (texture));
2844 }
2845
2846 /**
2847 * clutter_texture_get_repeat:
2848 * @texture: a #ClutterTexture
2849 * @repeat_x: (out): return location for the horizontal repeat
2850 * @repeat_y: (out): return location for the vertical repeat
2851 *
2852 * Retrieves the horizontal and vertical repeat values set
2853 * using clutter_texture_set_repeat()
2854 *
2855 * Since: 1.0
2856 *
2857 * Deprecated: 1.12: Use #ClutterImage and clutter_actor_get_content_repeat()
2858 * instead
2859 */
2860 void
2861 clutter_texture_get_repeat (ClutterTexture *texture,
2862 gboolean *repeat_x,
2863 gboolean *repeat_y)
2864 {
2865 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
2866
2867 if (repeat_x != NULL)
2868 *repeat_x = texture->priv->repeat_x;
2869
2870 if (repeat_y != NULL)
2871 *repeat_y = texture->priv->repeat_y;
2872 }
2873
2874 /**
2875 * clutter_texture_get_pixel_format:
2876 * @texture: a #ClutterTexture
2877 *
2878 * Retrieves the pixel format used by @texture. This is
2879 * equivalent to:
2880 *
2881 * |[
2882 * handle = clutter_texture_get_pixel_format (texture);
2883 *
2884 * if (handle != COGL_INVALID_HANDLE)
2885 * format = cogl_texture_get_format (handle);
2886 * ]|
2887 *
2888 * Return value: a #CoglPixelFormat value
2889 *
2890 * Since: 1.0
2891 *
2892 * Deprecated: 1.12: There is no direct replacement for this function
2893 */
2894 CoglPixelFormat
2895 clutter_texture_get_pixel_format (ClutterTexture *texture)
2896 {
2897 CoglHandle cogl_texture;
2898
2899 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), COGL_PIXEL_FORMAT_ANY);
2900
2901 cogl_texture = clutter_texture_get_cogl_texture (texture);
2902 if (cogl_texture == NULL)
2903 return COGL_PIXEL_FORMAT_ANY;
2904
2905 return cogl_texture_get_format (cogl_texture);
2906 }
2907
2908 /**
2909 * clutter_texture_set_keep_aspect_ratio:
2910 * @texture: a #ClutterTexture
2911 * @keep_aspect: %TRUE to maintain aspect ratio
2912 *
2913 * Sets whether @texture should have a preferred size maintaining
2914 * the aspect ratio of the underlying image
2915 *
2916 * Since: 1.0
2917 *
2918 * Deprecated: 1.12: Use #ClutterImage and clutter_actor_set_content_gravity()
2919 * with %CLUTTER_CONTENT_GRAVITY_RESIZE_ASPECT instead
2920 */
2921 void
2922 clutter_texture_set_keep_aspect_ratio (ClutterTexture *texture,
2923 gboolean keep_aspect)
2924 {
2925 ClutterTexturePrivate *priv;
2926
2927 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
2928
2929 priv = texture->priv;
2930
2931 if (priv->keep_aspect_ratio != keep_aspect)
2932 {
2933 priv->keep_aspect_ratio = keep_aspect;
2934
2935 clutter_actor_queue_relayout (CLUTTER_ACTOR (texture));
2936
2937 g_object_notify_by_pspec (G_OBJECT (texture), obj_props[PROP_KEEP_ASPECT_RATIO]);
2938 }
2939 }
2940
2941 /**
2942 * clutter_texture_get_keep_aspect_ratio:
2943 * @texture: a #ClutterTexture
2944 *
2945 * Retrieves the value set using clutter_texture_set_keep_aspect_ratio()
2946 *
2947 * Return value: %TRUE if the #ClutterTexture should maintain the
2948 * aspect ratio of the underlying image
2949 *
2950 * Since: 1.0
2951 *
2952 * Deprecated: 1.12: Use #ClutterImage and clutter_actor_get_content_gravity()
2953 * instead
2954 */
2955 gboolean
2956 clutter_texture_get_keep_aspect_ratio (ClutterTexture *texture)
2957 {
2958 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), FALSE);
2959
2960 return texture->priv->keep_aspect_ratio;
2961 }
2962
2963 /**
2964 * clutter_texture_set_load_async:
2965 * @texture: a #ClutterTexture
2966 * @load_async: %TRUE if the texture should asynchronously load data
2967 * from a filename
2968 *
2969 * Sets whether @texture should use a worker thread to load the data
2970 * from disk asynchronously. Setting @load_async to %TRUE will make
2971 * clutter_texture_set_from_file() return immediately.
2972 *
2973 * See the #ClutterTexture:load-async property documentation, and
2974 * clutter_texture_set_load_data_async().
2975 *
2976 * Since: 1.0
2977 *
2978 * Deprecated: 1.12: There is no direct replacement for this function.
2979 * Use #ClutterImage and platform-specific API for loading image data
2980 * asynchronously, like GdkPixbuf
2981 */
2982 void
2983 clutter_texture_set_load_async (ClutterTexture *texture,
2984 gboolean load_async)
2985 {
2986 ClutterTexturePrivate *priv;
2987
2988 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
2989
2990 priv = texture->priv;
2991
2992 load_async = !!load_async;
2993
2994 if (priv->load_async_set != load_async)
2995 {
2996 priv->load_data_async = load_async;
2997 priv->load_size_async = load_async;
2998
2999 priv->load_async_set = load_async;
3000
3001 g_object_notify_by_pspec (G_OBJECT (texture), obj_props[PROP_LOAD_ASYNC]);
3002 g_object_notify_by_pspec (G_OBJECT (texture), obj_props[PROP_LOAD_DATA_ASYNC]);
3003 }
3004 }
3005
3006 /**
3007 * clutter_texture_get_load_async:
3008 * @texture: a #ClutterTexture
3009 *
3010 * Retrieves the value set using clutter_texture_set_load_async()
3011 *
3012 * Return value: %TRUE if the #ClutterTexture should load the data from
3013 * disk asynchronously
3014 *
3015 * Since: 1.0
3016 *
3017 * Deprecated: 1.12: There is no direct replacement for this function
3018 */
3019 gboolean
3020 clutter_texture_get_load_async (ClutterTexture *texture)
3021 {
3022 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), FALSE);
3023
3024 return texture->priv->load_async_set;
3025 }
3026
3027 /**
3028 * clutter_texture_set_load_data_async:
3029 * @texture: a #ClutterTexture
3030 * @load_async: %TRUE if the texture should asynchronously load data
3031 * from a filename
3032 *
3033 * Sets whether @texture should use a worker thread to load the data
3034 * from disk asynchronously. Setting @load_async to %TRUE will make
3035 * clutter_texture_set_from_file() block until the #ClutterTexture has
3036 * determined the width and height of the image data.
3037 *
3038 * See the #ClutterTexture:load-async property documentation, and
3039 * clutter_texture_set_load_async().
3040 *
3041 * Since: 1.0
3042 *
3043 * Deprecated: 1.12: There is no direct replacement for this function.
3044 * Use #ClutterImage and platform-specific API for loading image data
3045 * asynchronously, like GdkPixbuf
3046 */
3047 void
3048 clutter_texture_set_load_data_async (ClutterTexture *texture,
3049 gboolean load_async)
3050 {
3051 ClutterTexturePrivate *priv;
3052
3053 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
3054
3055 priv = texture->priv;
3056
3057 if (priv->load_data_async != load_async)
3058 {
3059 /* load-data-async always unsets load-size-async */
3060 priv->load_data_async = load_async;
3061 priv->load_size_async = FALSE;
3062
3063 priv->load_async_set = load_async;
3064
3065 g_object_notify_by_pspec (G_OBJECT (texture), obj_props[PROP_LOAD_ASYNC]);
3066 g_object_notify_by_pspec (G_OBJECT (texture), obj_props[PROP_LOAD_DATA_ASYNC]);
3067 }
3068 }
3069
3070 /**
3071 * clutter_texture_get_load_data_async:
3072 * @texture: a #ClutterTexture
3073 *
3074 * Retrieves the value set by clutter_texture_set_load_data_async()
3075 *
3076 * Return value: %TRUE if the #ClutterTexture should load the image
3077 * data from a file asynchronously
3078 *
3079 * Since: 1.0
3080 *
3081 * Deprecated: 1.12: There is no direct replacement for this function
3082 */
3083 gboolean
3084 clutter_texture_get_load_data_async (ClutterTexture *texture)
3085 {
3086 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), FALSE);
3087
3088 return texture->priv->load_async_set &&
3089 texture->priv->load_data_async;
3090 }
3091
3092 /**
3093 * clutter_texture_set_pick_with_alpha:
3094 * @texture: a #ClutterTexture
3095 * @pick_with_alpha: %TRUE if the alpha channel should affect the
3096 * picking shape
3097 *
3098 * Sets whether @texture should have it's shape defined by the alpha
3099 * channel when picking.
3100 *
3101 * Be aware that this is a bit more costly than the default picking
3102 * due to the texture lookup, extra test against the alpha value and
3103 * the fact that it will also interrupt the batching of geometry done
3104 * internally.
3105 *
3106 * Also there is currently no control over the threshold used to
3107 * determine what value of alpha is considered pickable, and so only
3108 * fully opaque parts of the texture will react to picking.
3109 *
3110 * Since: 1.4
3111 *
3112 * Deprecated: 1.12: There is no direct replacement for this function
3113 */
3114 void
3115 clutter_texture_set_pick_with_alpha (ClutterTexture *texture,
3116 gboolean pick_with_alpha)
3117 {
3118 ClutterTexturePrivate *priv;
3119
3120 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
3121
3122 priv = texture->priv;
3123
3124 if (priv->pick_with_alpha == pick_with_alpha)
3125 return;
3126
3127 if (!pick_with_alpha && priv->pick_pipeline != NULL)
3128 {
3129 cogl_object_unref (priv->pick_pipeline);
3130 priv->pick_pipeline = NULL;
3131 }
3132
3133 /* NB: the pick pipeline is created lazily when we first pick */
3134 priv->pick_with_alpha = pick_with_alpha;
3135
3136 /* NB: actors are expected to call clutter_actor_queue_redraw when
3137 * ever some state changes that will affect painting *or picking...
3138 */
3139 clutter_actor_queue_redraw (CLUTTER_ACTOR (texture));
3140 }
3141
3142 /**
3143 * clutter_texture_get_pick_with_alpha:
3144 * @texture: a #ClutterTexture
3145 *
3146 * Retrieves the value set by clutter_texture_set_load_data_async()
3147 *
3148 * Return value: %TRUE if the #ClutterTexture should define its shape
3149 * using the alpha channel when picking.
3150 *
3151 * Since: 1.4
3152 *
3153 * Deprecated: 1.12: There is no direct replacement for this function
3154 */
3155 gboolean
3156 clutter_texture_get_pick_with_alpha (ClutterTexture *texture)
3157 {
3158 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), FALSE);
3159
3160 return texture->priv->pick_with_alpha ? TRUE : FALSE;
3161 }
3162