Debian Codegraph @

tests/interactive/test-cogl-tex-foreign.c

raw
1 #include <glib.h>
2 #include <gmodule.h>
3 #include <stdlib.h>
4 #include <clutter/clutter.h>
5 #include <cogl/cogl.h>
6
7 #ifndef GL_UNPACK_ALIGNMENT
8 #define GL_UNPACK_ALIGNMENT 0x0CF5
9 #endif
10 #ifndef GL_TEXTURE_BINDING_2D
11 #define GL_TEXTURE_BINDING_2D 0x8069
12 #endif
13 #ifndef GL_TEXTURE_2D
14 #define GL_TEXTURE_2D 0x0DE1
15 #endif
16 #ifndef GL_RGB
17 #define GL_RGB 0x1907
18 #endif
19 #ifndef GL_UNSIGNED_BYTE
20 #define GL_UNSIGNED_BYTE 0x1401
21 #endif
22 #ifndef GL_TEXTURE_MAG_FILTER
23 #define GL_TEXTURE_MAG_FILTER 0x2800
24 #endif
25 #ifndef GL_LINEAR
26 #define GL_LINEAR 0x1208
27 #endif
28 #ifndef GL_TEXTURE_MIN_FILTER
29 #define GL_TEXTURE_MIN_FILTER 0x2801
30 #endif
31
32 /* Coglbox declaration
33 *--------------------------------------------------*/
34
35 G_BEGIN_DECLS
36
37 #define TEST_TYPE_COGLBOX test_coglbox_get_type()
38
39 #define TEST_COGLBOX(obj) \
40 (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
41 TEST_TYPE_COGLBOX, TestCoglboxClass))
42
43 #define TEST_COGLBOX_CLASS(klass) \
44 (G_TYPE_CHECK_CLASS_CAST ((klass), \
45 TEST_TYPE_COGLBOX, TestCoglboxClass))
46
47 #define TEST_IS_COGLBOX(obj) \
48 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
49 TEST_TYPE_COGLBOX))
50
51 #define TEST_IS_COGLBOX_CLASS(klass) \
52 (G_TYPE_CHECK_CLASS_TYPE ((klass), \
53 TEST_TYPE_COGLBOX))
54
55 #define TEST_COGLBOX_GET_CLASS(obj) \
56 (G_TYPE_INSTANCE_GET_CLASS ((obj), \
57 TEST_TYPE_COGLBOX, TestCoglboxClass))
58
59 typedef struct _TestCoglbox TestCoglbox;
60 typedef struct _TestCoglboxClass TestCoglboxClass;
61 typedef struct _TestCoglboxPrivate TestCoglboxPrivate;
62
63 struct _TestCoglbox
64 {
65 ClutterActor parent;
66
67 /*< private >*/
68 TestCoglboxPrivate *priv;
69 };
70
71 struct _TestCoglboxClass
72 {
73 ClutterActorClass parent_class;
74
75 /* padding for future expansion */
76 void (*_test_coglbox1) (void);
77 void (*_test_coglbox2) (void);
78 void (*_test_coglbox3) (void);
79 void (*_test_coglbox4) (void);
80 };
81
82 static GType test_coglbox_get_type (void) G_GNUC_CONST;
83
84 G_END_DECLS
85
86 /* Coglbox private declaration
87 *--------------------------------------------------*/
88
89 G_DEFINE_TYPE (TestCoglbox, test_coglbox, CLUTTER_TYPE_ACTOR);
90
91 #define TEST_COGLBOX_GET_PRIVATE(obj) \
92 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TEST_TYPE_COGLBOX, TestCoglboxPrivate))
93
94 struct _TestCoglboxPrivate
95 {
96 guint gl_handle;
97 CoglHandle cogl_handle;
98
99 void
100 (* glGetIntegerv) (guint pname, int *params);
101 void
102 (* glPixelStorei) (guint pname, int param);
103 void
104 (* glTexParameteri) (guint target, guint pname, int param);
105 void
106 (* glTexImage2D) (guint target, int level,
107 int internalFormat,
108 int width, int height,
109 int border, guint format, guint type,
110 const void *pixels);
111 void
112 (* glGenTextures) (int n, guint *textures);
113 void
114 (* glDeleteTextures) (int n, const guint *textures);
115 void
116 (* glBindTexture) (guint target, guint texture);
117 };
118
119 /* Coglbox implementation
120 *--------------------------------------------------*/
121
122 static void
123 test_coglbox_paint(ClutterActor *self)
124 {
125 TestCoglboxPrivate *priv = TEST_COGLBOX_GET_PRIVATE (self);
126 gfloat texcoords[4] = { 0.3f, 0.3f, 0.7f, 0.7f };
127
128 cogl_set_source_color4ub (0x66, 0x66, 0xdd, 0xff);
129 cogl_rectangle (0,0,400,400);
130
131 cogl_push_matrix ();
132
133 cogl_translate (100,100,0);
134 cogl_set_source_texture (priv->cogl_handle);
135 cogl_rectangle_with_texture_coords (0, 0, 200, 200,
136 texcoords[0], texcoords[1],
137 texcoords[2], texcoords[3]);
138
139 cogl_pop_matrix();
140 }
141
142 static void
143 test_coglbox_finalize (GObject *object)
144 {
145 G_OBJECT_CLASS (test_coglbox_parent_class)->finalize (object);
146 }
147
148 static void
149 test_coglbox_dispose (GObject *object)
150 {
151 TestCoglboxPrivate *priv;
152
153 priv = TEST_COGLBOX_GET_PRIVATE (object);
154
155 cogl_handle_unref (priv->cogl_handle);
156 priv->glDeleteTextures (1, &priv->gl_handle);
157
158 G_OBJECT_CLASS (test_coglbox_parent_class)->dispose (object);
159 }
160
161 static void
162 test_coglbox_init (TestCoglbox *self)
163 {
164 TestCoglboxPrivate *priv;
165 guchar data[12];
166 int prev_unpack_alignment;
167 int prev_2d_texture_binding;
168
169 self->priv = priv = TEST_COGLBOX_GET_PRIVATE(self);
170
171 /* Prepare a 2x2 pixels texture */
172
173 data[0] = 255; data[1] = 0; data[2] = 0;
174 data[3] = 0; data[4] = 255; data[5] = 0;
175 data[6] = 0; data[7] = 0; data[8] = 255;
176 data[9] = 0; data[10] = 0; data[11] = 0;
177
178 priv->glGetIntegerv = (void *) cogl_get_proc_address ("glGetIntegerv");
179 priv->glPixelStorei = (void *) cogl_get_proc_address ("glPixelStorei");
180 priv->glTexParameteri = (void *) cogl_get_proc_address ("glTexParameteri");
181 priv->glTexImage2D = (void *) cogl_get_proc_address ("glTexImage2D");
182 priv->glGenTextures = (void *) cogl_get_proc_address ("glGenTextures");
183 priv->glDeleteTextures = (void *) cogl_get_proc_address ("glDeleteTextures");
184 priv->glBindTexture = (void *) cogl_get_proc_address ("glBindTexture");
185
186 /* We are about to use OpenGL directly to create a TEXTURE_2D
187 * texture so we need to save the state that we modify so we can
188 * restore it afterwards and be sure not to interfere with any state
189 * caching that Cogl may do internally.
190 */
191 priv->glGetIntegerv (GL_UNPACK_ALIGNMENT, &prev_unpack_alignment);
192 priv->glGetIntegerv (GL_TEXTURE_BINDING_2D, &prev_2d_texture_binding);
193
194 priv->glGenTextures (1, &priv->gl_handle);
195 priv->glBindTexture (GL_TEXTURE_2D, priv->gl_handle);
196
197 priv->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
198 priv->glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB,
199 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
200
201 /* Now restore the original GL state as Cogl had left it */
202 priv->glPixelStorei (GL_UNPACK_ALIGNMENT, prev_unpack_alignment);
203 priv->glBindTexture (GL_TEXTURE_2D, prev_2d_texture_binding);
204
205 priv->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
206 priv->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
207
208 /* Create texture from foreign */
209
210 priv->cogl_handle =
211 cogl_texture_new_from_foreign (priv->gl_handle,
212 GL_TEXTURE_2D,
213 2, 2, 0, 0,
214 COGL_PIXEL_FORMAT_RGB_888);
215
216 if (priv->cogl_handle == COGL_INVALID_HANDLE)
217 {
218 printf ("Failed creating texture from foreign!\n");
219 return;
220 }
221 }
222
223 static void
224 test_coglbox_class_init (TestCoglboxClass *klass)
225 {
226 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
227 ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
228
229 gobject_class->finalize = test_coglbox_finalize;
230 gobject_class->dispose = test_coglbox_dispose;
231 actor_class->paint = test_coglbox_paint;
232
233 g_type_class_add_private (gobject_class, sizeof (TestCoglboxPrivate));
234 }
235
236 static ClutterActor*
237 test_coglbox_new (void)
238 {
239 return g_object_new (TEST_TYPE_COGLBOX, NULL);
240 }
241
242 G_MODULE_EXPORT int
243 test_cogl_tex_foreign_main (int argc, char *argv[])
244 {
245 ClutterActor *stage;
246 ClutterActor *coglbox;
247
248 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
249 return 1;
250
251 /* Stage */
252 stage = clutter_stage_new ();
253 clutter_actor_set_size (stage, 400, 400);
254 clutter_stage_set_title (CLUTTER_STAGE (stage), "Cogl Foreign Textures");
255 g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
256
257 /* Cogl Box */
258 coglbox = test_coglbox_new ();
259 clutter_container_add_actor (CLUTTER_CONTAINER (stage), coglbox);
260
261 clutter_actor_show_all (stage);
262
263 clutter_main ();
264
265 return 0;
266 }
267
268 G_MODULE_EXPORT const char *
269 test_cogl_tex_foreign_describe (void)
270 {
271 return "Foreign textures support in Cogl.";
272 }