[clutter/wip/actor-content: 5/24] image: Implement synchronous loading



commit b450390c3922dedb496fac321af352806911ec41
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Mon Dec 6 18:10:52 2010 +0000

    image: Implement synchronous loading
    
    Use the GFile and the ImageLoader synchronous API.

 clutter/clutter-image.c           |   63 +++++++++++++++++++++++++++++++++++++
 tests/conform/Makefile.am         |    1 +
 tests/conform/test-conform-main.c |    2 +
 tests/conform/test-image.c        |   34 ++++++++++++++++++++
 4 files changed, 100 insertions(+), 0 deletions(-)
---
diff --git a/clutter/clutter-image.c b/clutter/clutter-image.c
index b8ccd1d..7ca97ed 100644
--- a/clutter/clutter-image.c
+++ b/clutter/clutter-image.c
@@ -7,11 +7,14 @@
 #include "clutter-image.h"
 
 #include "clutter-debug.h"
+#include "clutter-image-loader.h"
 #include "clutter-marshal.h"
 #include "clutter-private.h"
 
 struct _ClutterImagePrivate
 {
+  ClutterImageLoader *loader;
+
   gint image_width;
   gint image_height;
 
@@ -132,6 +135,8 @@ clutter_image_class_init (ClutterImageClass *klass)
   gobject_class->get_property = clutter_image_get_property;
   g_object_class_install_properties (gobject_class, LAST_PROP, image_props);
 
+  gobject_class->dispose = clutter_image_dispose;
+
   image_signals[SIZE_CHANGED] =
     g_signal_new (I_("size-changed"),
                   G_TYPE_FROM_CLASS (klass),
@@ -221,11 +226,69 @@ clutter_image_load (ClutterImage  *image,
                     gint          *height,
                     GError       **error)
 {
+  ClutterImagePrivate *priv;
+  GFileInputStream *stream;
+  CoglHandle texture;
+  gboolean res;
+
   g_return_val_if_fail (CLUTTER_IS_IMAGE (image), FALSE);
   g_return_val_if_fail (G_IS_FILE (gfile), FALSE);
   g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable),
                         FALSE);
 
+  priv = image->priv;
+
+  if (priv->loader == NULL)
+    {
+      priv->loader = _clutter_image_loader_new ();
+      if (priv->loader == NULL)
+        return FALSE;
+
+      CLUTTER_NOTE (MISC, "Image loader type: %s",
+                    G_OBJECT_TYPE_NAME (priv->loader));
+    }
+
+  stream = g_file_read (gfile, cancellable, error);
+  if (stream == NULL)
+    return FALSE;
+
+  res = _clutter_image_loader_load_stream (priv->loader,
+                                           G_INPUT_STREAM (stream),
+                                           cancellable,
+                                           error);
+  if (!res)
+    {
+      g_object_unref (stream);
+      return FALSE;
+    }
+
+  _clutter_image_loader_get_image_size (priv->loader,
+                                        &priv->image_width,
+                                        &priv->image_height);
+
+  texture = _clutter_image_loader_get_texture_handle (priv->loader);
+  if (texture == COGL_INVALID_HANDLE)
+    {
+      g_object_unref (stream);
+      return FALSE;
+    }
+
+  cogl_material_set_layer (priv->material, 0, texture);
+
+  g_signal_emit (image, image_signals[SIZE_CHANGED], 0,
+                 priv->image_width,
+                 priv->image_height);
+
+  if (width)
+    *width = priv->image_width;
+
+  if (height)
+    *height = priv->image_height;
+
+  g_object_unref (priv->loader);
+  priv->loader = NULL;
+
+  g_object_unref (stream);
 
   return TRUE;
 }
diff --git a/tests/conform/Makefile.am b/tests/conform/Makefile.am
index fa00dc8..0b99048 100644
--- a/tests/conform/Makefile.am
+++ b/tests/conform/Makefile.am
@@ -82,6 +82,7 @@ units_sources += \
 	test-color.c			\
 	test-model.c			\
 	test-script-parser.c		\
+	test-image.c			\
         $(NULL)
 
 test_conformance_SOURCES = $(common_sources) $(units_sources)
diff --git a/tests/conform/test-conform-main.c b/tests/conform/test-conform-main.c
index 7fa1e8d..ba44c3d 100644
--- a/tests/conform/test-conform-main.c
+++ b/tests/conform/test-conform-main.c
@@ -209,6 +209,8 @@ main (int argc, char **argv)
 
   TEST_CONFORM_SIMPLE ("/behaviours", test_behaviours);
 
+  TEST_CONFORM_SIMPLE ("/image", image_sync_loading);
+
   TEST_CONFORM_SIMPLE ("/cogl", test_cogl_object);
   TEST_CONFORM_SIMPLE ("/cogl", test_cogl_fixed);
   TEST_CONFORM_SIMPLE ("/cogl", test_cogl_backface_culling);
diff --git a/tests/conform/test-image.c b/tests/conform/test-image.c
new file mode 100644
index 0000000..efccd24
--- /dev/null
+++ b/tests/conform/test-image.c
@@ -0,0 +1,34 @@
+#include <clutter/clutter.h>
+#include "test-conform-common.h"
+
+void
+image_sync_loading (void)
+{
+  ClutterImage *image = clutter_image_new ();
+  GError *error;
+  GFile *gfile;
+  gint width, height;
+  gboolean res;
+
+  g_assert (CLUTTER_IS_IMAGE (image));
+
+  gfile = g_file_new_for_path (TESTS_DATADIR "/redhand.png");
+
+  width = height = 0;
+  error = NULL;
+
+  res = clutter_image_load (image, gfile, NULL,
+                            &width,
+                            &height,
+                            &error);
+
+  if (g_test_verbose() && error != NULL)
+    g_print ("Unexpected error: %s\n", error->message);
+
+  g_assert (error == NULL);
+  g_assert (res);
+  g_assert (width > 0 && height > 0);
+
+  g_object_unref (gfile);
+  g_object_unref (image);
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]