20d802b24ed86efca4c4261cede8c6501c0d19c1
[profile/ivi/clutter.git] / tests / interactive / test-texture-async.c
1 #include <stdlib.h>
2 #include <gmodule.h>
3 #include <clutter/clutter.h>
4
5 enum
6 {
7   LOAD_SYNC,
8   LOAD_DATA_ASYNC,
9   LOAD_ASYNC
10 };
11
12 static ClutterActor *stage = NULL;
13
14 static void
15 on_load_finished (ClutterTexture *texture,
16                   const GError   *error,
17                   gpointer        user_data)
18 {
19   gint load_type = GPOINTER_TO_INT (user_data);
20   const gchar *load_str = NULL;
21
22   switch (load_type)
23     {
24     case LOAD_SYNC:
25       load_str = "synchronous loading";
26       break;
27
28     case LOAD_DATA_ASYNC:
29       load_str = "asynchronous data loading";
30       break;
31
32     case LOAD_ASYNC:
33       load_str = "asynchronous loading";
34       break;
35     }
36
37   if (error != NULL)
38     g_print ("%s failed: %s\n", load_str, error->message);
39   else
40     g_print ("%s successful\n", load_str);
41 }
42
43 static void
44 size_change_cb (ClutterTexture *texture,
45                 gint            width,
46                 gint            height,
47                 gpointer        user_data)
48 {
49   clutter_actor_set_size (user_data, width, height);
50 }
51
52 static
53 gboolean task (gpointer user_data)
54 {
55   const gchar *path = user_data;
56   ClutterActor *image[3];
57   ClutterActor *clone[3];
58   gint i;
59
60   image[0] = g_object_new (CLUTTER_TYPE_TEXTURE, NULL);
61   g_signal_connect (image[0], "load-finished",
62                     G_CALLBACK (on_load_finished),
63                     GINT_TO_POINTER (LOAD_SYNC));
64
65   image[1] = g_object_new (CLUTTER_TYPE_TEXTURE,
66                            "load-data-async", TRUE,
67                            NULL);
68   g_signal_connect (image[1], "load-finished",
69                     G_CALLBACK (on_load_finished),
70                     GINT_TO_POINTER (LOAD_DATA_ASYNC));
71   image[2] = g_object_new (CLUTTER_TYPE_TEXTURE,
72                            "load-async", TRUE,
73                            NULL);
74   g_signal_connect (image[2], "load-finished",
75                     G_CALLBACK (on_load_finished),
76                     GINT_TO_POINTER (LOAD_ASYNC));
77
78   for (i = 0; i < 3; i++)
79     {
80       GError *error = NULL;
81
82       clutter_texture_set_from_file (CLUTTER_TEXTURE (image[i]), path, &error);
83       if (error != NULL)
84         g_error ("Unable to load image at '%s': %s",
85                  path != NULL ? path : "<unknown>",
86                  error->message);
87     }
88
89   for (i = 0; i < 3; i++)
90     clutter_container_add_actor (CLUTTER_CONTAINER (stage), image[i]);
91
92   for (i = 0; i < 3; i++)
93     {
94       clutter_actor_set_position (image[i], 50 + i * 100, 0 + i * 50);
95       clone[i] = clutter_clone_new (image[i]);
96       g_signal_connect (image[i], "size-change",
97                         G_CALLBACK (size_change_cb), clone[i]);
98       clutter_container_add_actor (CLUTTER_CONTAINER (stage), clone[i]);
99       clutter_actor_set_position (clone[i], 50 + i * 100, 150 + i * 50 + 100);
100     }
101
102   for (i = 0; i < 3; i++)
103     {
104       clutter_actor_set_depth (image[i], -2500);
105       clutter_actor_animate (image[i], CLUTTER_LINEAR, 5000,
106                              "depth", 0.0,
107                              NULL);
108     }
109
110   return FALSE;
111 }
112
113 static void
114 cleanup_task (gpointer data)
115 {
116 }
117
118 G_MODULE_EXPORT gint
119 test_texture_async_main (int argc, char *argv[])
120 {
121   gchar *path;
122
123   if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
124     return 1;
125
126   stage = clutter_stage_new ();
127   clutter_stage_set_title (CLUTTER_STAGE (stage), "Asynchronous Texture Loading");
128   clutter_stage_set_color (CLUTTER_STAGE (stage), CLUTTER_COLOR_LightSkyBlue);
129   g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
130
131   clutter_actor_show (stage);
132
133   path = (argc > 1)
134        ? g_strdup (argv[1])
135        : g_build_filename (TESTS_DATADIR, "redhand.png", NULL);
136  
137   clutter_threads_add_timeout_full (G_PRIORITY_DEFAULT, 500,
138                                     task, path,
139                                     cleanup_task);
140
141   clutter_threads_enter ();
142   clutter_main ();
143   clutter_threads_leave ();
144
145   g_free (path);
146
147   return EXIT_SUCCESS;
148 }
149
150 G_MODULE_EXPORT const char *
151 test_texture_async_describe (void)
152 {
153   return "Texture asynchronous loading using threads";
154 }