| 1 |
#include <glib.h> |
| 2 |
#include <gmodule.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <clutter/clutter.h> |
| 5 |
#include <cogl/cogl.h> |
| 6 |
|
| 7 |
/* Coglbox declaration |
| 8 |
*--------------------------------------------------*/ |
| 9 |
|
| 10 |
G_BEGIN_DECLS |
| 11 |
|
| 12 |
#define TEST_TYPE_COGLBOX test_coglbox_get_type() |
| 13 |
|
| 14 |
#define TEST_COGLBOX(obj) \ |
| 15 |
(G_TYPE_CHECK_INSTANCE_CAST ((obj), \ |
| 16 |
TEST_TYPE_COGLBOX, TestCoglboxClass)) |
| 17 |
|
| 18 |
#define TEST_COGLBOX_CLASS(klass) \ |
| 19 |
(G_TYPE_CHECK_CLASS_CAST ((klass), \ |
| 20 |
TEST_TYPE_COGLBOX, TestCoglboxClass)) |
| 21 |
|
| 22 |
#define TEST_IS_COGLBOX(obj) \ |
| 23 |
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), \ |
| 24 |
TEST_TYPE_COGLBOX)) |
| 25 |
|
| 26 |
#define TEST_IS_COGLBOX_CLASS(klass) \ |
| 27 |
(G_TYPE_CHECK_CLASS_TYPE ((klass), \ |
| 28 |
TEST_TYPE_COGLBOX)) |
| 29 |
|
| 30 |
#define TEST_COGLBOX_GET_CLASS(obj) \ |
| 31 |
(G_TYPE_INSTANCE_GET_CLASS ((obj), \ |
| 32 |
TEST_TYPE_COGLBOX, TestCoglboxClass)) |
| 33 |
|
| 34 |
typedef struct _TestCoglbox TestCoglbox; |
| 35 |
typedef struct _TestCoglboxClass TestCoglboxClass; |
| 36 |
typedef struct _TestCoglboxPrivate TestCoglboxPrivate; |
| 37 |
|
| 38 |
struct _TestCoglbox |
| 39 |
{ |
| 40 |
ClutterActor parent; |
| 41 |
|
| 42 |
/*< private >*/ |
| 43 |
TestCoglboxPrivate *priv; |
| 44 |
}; |
| 45 |
|
| 46 |
struct _TestCoglboxClass |
| 47 |
{ |
| 48 |
ClutterActorClass parent_class; |
| 49 |
|
| 50 |
/* padding for future expansion */ |
| 51 |
void (*_test_coglbox1) (void); |
| 52 |
void (*_test_coglbox2) (void); |
| 53 |
void (*_test_coglbox3) (void); |
| 54 |
void (*_test_coglbox4) (void); |
| 55 |
}; |
| 56 |
|
| 57 |
static GType test_coglbox_get_type (void) G_GNUC_CONST; |
| 58 |
|
| 59 |
G_END_DECLS |
| 60 |
|
| 61 |
/* Coglbox private declaration |
| 62 |
*--------------------------------------------------*/ |
| 63 |
|
| 64 |
G_DEFINE_TYPE (TestCoglbox, test_coglbox, CLUTTER_TYPE_ACTOR); |
| 65 |
|
| 66 |
#define TEST_COGLBOX_GET_PRIVATE(obj) \ |
| 67 |
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), TEST_TYPE_COGLBOX, TestCoglboxPrivate)) |
| 68 |
|
| 69 |
struct _TestCoglboxPrivate |
| 70 |
{ |
| 71 |
CoglHandle cogl_tex_id[4]; |
| 72 |
}; |
| 73 |
|
| 74 |
/* Coglbox implementation |
| 75 |
*--------------------------------------------------*/ |
| 76 |
|
| 77 |
static void |
| 78 |
test_coglbox_paint(ClutterActor *self) |
| 79 |
{ |
| 80 |
TestCoglboxPrivate *priv = TEST_COGLBOX_GET_PRIVATE (self); |
| 81 |
gfloat texcoords[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; |
| 82 |
|
| 83 |
cogl_set_source_color4ub (0x66, 0x66, 0xdd, 0xff); |
| 84 |
cogl_rectangle (0, 0, 400, 400); |
| 85 |
|
| 86 |
cogl_push_matrix (); |
| 87 |
|
| 88 |
cogl_translate (100, 100, 0); |
| 89 |
cogl_set_source_texture (priv->cogl_tex_id[1]); |
| 90 |
cogl_rectangle_with_texture_coords (0, 0, 200, 213, |
| 91 |
texcoords[0], texcoords[1], |
| 92 |
texcoords[2], texcoords[3]); |
| 93 |
|
| 94 |
cogl_pop_matrix(); |
| 95 |
} |
| 96 |
|
| 97 |
static void |
| 98 |
test_coglbox_finalize (GObject *object) |
| 99 |
{ |
| 100 |
G_OBJECT_CLASS (test_coglbox_parent_class)->finalize (object); |
| 101 |
} |
| 102 |
|
| 103 |
static void |
| 104 |
test_coglbox_dispose (GObject *object) |
| 105 |
{ |
| 106 |
TestCoglboxPrivate *priv; |
| 107 |
|
| 108 |
priv = TEST_COGLBOX_GET_PRIVATE (object); |
| 109 |
cogl_handle_unref (priv->cogl_tex_id); |
| 110 |
|
| 111 |
G_OBJECT_CLASS (test_coglbox_parent_class)->dispose (object); |
| 112 |
} |
| 113 |
|
| 114 |
static void |
| 115 |
test_coglbox_init (TestCoglbox *self) |
| 116 |
{ |
| 117 |
TestCoglboxPrivate *priv; |
| 118 |
guint width; |
| 119 |
guint height; |
| 120 |
guint rowstride; |
| 121 |
CoglPixelFormat format; |
| 122 |
gint size; |
| 123 |
guchar *data; |
| 124 |
gint x,y,t; |
| 125 |
guchar *pixel; |
| 126 |
gchar *file; |
| 127 |
|
| 128 |
self->priv = priv = TEST_COGLBOX_GET_PRIVATE(self); |
| 129 |
|
| 130 |
/* Load image from file */ |
| 131 |
file = g_build_filename (TESTS_DATADIR, "redhand.png", NULL); |
| 132 |
priv->cogl_tex_id[0] = |
| 133 |
cogl_texture_new_from_file (file, |
| 134 |
COGL_TEXTURE_NONE, |
| 135 |
COGL_PIXEL_FORMAT_ANY, |
| 136 |
NULL); |
| 137 |
|
| 138 |
if (priv->cogl_tex_id[0] == COGL_INVALID_HANDLE) |
| 139 |
{ |
| 140 |
printf ("Failed loading redhand.png image!\n"); |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
g_free (file); |
| 145 |
|
| 146 |
printf("Texture loaded from file.\n"); |
| 147 |
|
| 148 |
/* Obtain pixel data */ |
| 149 |
|
| 150 |
format = cogl_texture_get_format (priv->cogl_tex_id[0]); |
| 151 |
g_assert(format == COGL_PIXEL_FORMAT_RGBA_8888_PRE || |
| 152 |
format == COGL_PIXEL_FORMAT_ARGB_8888_PRE); |
| 153 |
|
| 154 |
width = cogl_texture_get_width (priv->cogl_tex_id[0]); |
| 155 |
height = cogl_texture_get_height (priv->cogl_tex_id[0]); |
| 156 |
size = cogl_texture_get_data (priv->cogl_tex_id[0], |
| 157 |
format, 0, NULL); |
| 158 |
|
| 159 |
printf("size: %dx%d\n", width, height); |
| 160 |
printf("format: 0x%x\n", format); |
| 161 |
printf("bytesize: %d\n", size); |
| 162 |
|
| 163 |
data = (guchar*) g_malloc (sizeof(guchar) * size); |
| 164 |
|
| 165 |
cogl_texture_get_data (priv->cogl_tex_id[0], |
| 166 |
format, 0, data); |
| 167 |
rowstride = cogl_texture_get_rowstride (priv->cogl_tex_id[0]); |
| 168 |
|
| 169 |
/* Create new texture from modified data */ |
| 170 |
|
| 171 |
priv->cogl_tex_id[1] = |
| 172 |
cogl_texture_new_from_data (width, height, |
| 173 |
COGL_TEXTURE_NONE, |
| 174 |
format, format, |
| 175 |
rowstride, data); |
| 176 |
|
| 177 |
if (priv->cogl_tex_id[1] == COGL_INVALID_HANDLE) |
| 178 |
{ |
| 179 |
printf ("Failed creating image from data!\n"); |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
printf ("Texture created from data.\n"); |
| 184 |
|
| 185 |
/* Modify data (swap red and green) */ |
| 186 |
|
| 187 |
for (y=0; y<height; ++y) |
| 188 |
{ |
| 189 |
for (x=0; x<width; ++x) |
| 190 |
{ |
| 191 |
pixel = data + y * rowstride + x * 4; |
| 192 |
if (format == COGL_PIXEL_FORMAT_RGBA_8888_PRE) |
| 193 |
{ |
| 194 |
t = pixel[0]; |
| 195 |
pixel[0] = pixel[1]; |
| 196 |
pixel[1] = t; |
| 197 |
} |
| 198 |
else |
| 199 |
{ |
| 200 |
t = pixel[1]; |
| 201 |
pixel[1] = pixel[2]; |
| 202 |
pixel[2] = t; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
cogl_texture_set_region (priv->cogl_tex_id[1], |
| 209 |
0, 0, 0, 0, |
| 210 |
100, 100, width, height, |
| 211 |
format, 0, data); |
| 212 |
|
| 213 |
cogl_texture_set_region (priv->cogl_tex_id[1], |
| 214 |
100, 100, 100, 100, |
| 215 |
100, 100, width, height, |
| 216 |
format, 0, data); |
| 217 |
|
| 218 |
printf ("Subregion data updated.\n"); |
| 219 |
} |
| 220 |
|
| 221 |
static void |
| 222 |
test_coglbox_class_init (TestCoglboxClass *klass) |
| 223 |
{ |
| 224 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 225 |
ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass); |
| 226 |
|
| 227 |
gobject_class->finalize = test_coglbox_finalize; |
| 228 |
gobject_class->dispose = test_coglbox_dispose; |
| 229 |
actor_class->paint = test_coglbox_paint; |
| 230 |
|
| 231 |
g_type_class_add_private (gobject_class, sizeof (TestCoglboxPrivate)); |
| 232 |
} |
| 233 |
|
| 234 |
static ClutterActor* |
| 235 |
test_coglbox_new (void) |
| 236 |
{ |
| 237 |
return g_object_new (TEST_TYPE_COGLBOX, NULL); |
| 238 |
} |
| 239 |
|
| 240 |
G_MODULE_EXPORT int |
| 241 |
test_cogl_tex_getset_main (int argc, char *argv[]) |
| 242 |
{ |
| 243 |
ClutterActor *stage; |
| 244 |
ClutterActor *coglbox; |
| 245 |
|
| 246 |
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS) |
| 247 |
return 1; |
| 248 |
|
| 249 |
/* Stage */ |
| 250 |
stage = clutter_stage_new (); |
| 251 |
clutter_actor_set_size (stage, 400, 400); |
| 252 |
clutter_stage_set_title (CLUTTER_STAGE (stage), "Cogl Texture Readback"); |
| 253 |
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL); |
| 254 |
|
| 255 |
/* Cogl Box */ |
| 256 |
coglbox = test_coglbox_new (); |
| 257 |
clutter_container_add_actor (CLUTTER_CONTAINER (stage), coglbox); |
| 258 |
|
| 259 |
clutter_actor_show_all (stage); |
| 260 |
|
| 261 |
clutter_main (); |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
G_MODULE_EXPORT const char * |
| 267 |
test_cogl_tex_getset_describe (void) |
| 268 |
{ |
| 269 |
return "Texture region readback and update in Cogl."; |
| 270 |
} |