~elementary-os/ubuntu-package-imports/mutter-bionic

« back to all changes in this revision

Viewing changes to clutter/tests/interactive/test-texture-async.c

  • Committer: RabbitBot
  • Date: 2018-04-11 14:49:36 UTC
  • Revision ID: rabbitbot@elementary.io-20180411144936-hgymqa9d8d1xfpbh
Initial import, version 3.28.0-2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
      clutter_actor_set_depth (image[i], -2500);
 
96
 
 
97
      clone[i] = clutter_clone_new (image[i]);
 
98
      g_signal_connect (image[i], "size-change",
 
99
                        G_CALLBACK (size_change_cb), clone[i]);
 
100
      clutter_container_add_actor (CLUTTER_CONTAINER (stage), clone[i]);
 
101
      clutter_actor_set_position (clone[i], 50 + i * 100, 150 + i * 50 + 100);
 
102
    }
 
103
 
 
104
  for (i = 0; i < 3; i++)
 
105
    {
 
106
      clutter_actor_save_easing_state (image[i]);
 
107
      clutter_actor_set_easing_duration (image[i], 5000);
 
108
      clutter_actor_set_depth (image[i], 0);
 
109
      clutter_actor_restore_easing_state (image[i]);
 
110
    }
 
111
 
 
112
  return FALSE;
 
113
}
 
114
 
 
115
static void
 
116
cleanup_task (gpointer data)
 
117
{
 
118
}
 
119
 
 
120
G_MODULE_EXPORT gint
 
121
test_texture_async_main (int argc, char *argv[])
 
122
{
 
123
  gchar *path;
 
124
 
 
125
  if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
 
126
    return 1;
 
127
 
 
128
  stage = clutter_stage_new ();
 
129
  clutter_stage_set_title (CLUTTER_STAGE (stage), "Asynchronous Texture Loading");
 
130
  clutter_actor_set_background_color (stage, CLUTTER_COLOR_LightSkyBlue);
 
131
  g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
 
132
 
 
133
  clutter_actor_show (stage);
 
134
 
 
135
  path = (argc > 1)
 
136
       ? g_strdup (argv[1])
 
137
       : g_build_filename (TESTS_DATADIR, "redhand.png", NULL);
 
138
 
 
139
  clutter_threads_add_timeout_full (G_PRIORITY_DEFAULT, 500,
 
140
                                    task, path,
 
141
                                    cleanup_task);
 
142
 
 
143
  clutter_threads_enter ();
 
144
  clutter_main ();
 
145
  clutter_threads_leave ();
 
146
 
 
147
  g_free (path);
 
148
 
 
149
  return EXIT_SUCCESS;
 
150
}
 
151
 
 
152
G_MODULE_EXPORT const char *
 
153
test_texture_async_describe (void)
 
154
{
 
155
  return "Texture asynchronous loading using threads";
 
156
}