clutter drawing improvement
[geeqie.git] / src / renderer-clutter.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 - 2012 The Geeqie Team
5  *
6  * Author: John Ellis
7  * Author: Vladimir Nadvornik
8  *
9  * This software is released under the GNU General Public License (GNU GPL).
10  * Please read the included file COPYING for more information.
11  * This software comes with no warranty of any kind, use at your own risk!
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <math.h>
18
19 #include "main.h"
20 #include "pixbuf-renderer.h"
21 #include "renderer-clutter.h"
22
23 #include "intl.h"
24 #include "layout.h"
25
26 #include <gtk/gtk.h>
27
28 #ifdef HAVE_CLUTTER
29
30 #include <clutter/clutter.h>
31
32 #include <clutter-gtk/clutter-gtk.h>
33
34
35
36 #define GQ_BUILD 1
37
38 #ifdef GQ_BUILD
39 #include "main.h"
40 #include "pixbuf_util.h"
41 #include "exif.h"
42 #else
43 typedef enum {
44         EXIF_ORIENTATION_UNKNOWN        = 0,
45         EXIF_ORIENTATION_TOP_LEFT       = 1,
46         EXIF_ORIENTATION_TOP_RIGHT      = 2,
47         EXIF_ORIENTATION_BOTTOM_RIGHT   = 3,
48         EXIF_ORIENTATION_BOTTOM_LEFT    = 4,
49         EXIF_ORIENTATION_LEFT_TOP       = 5,
50         EXIF_ORIENTATION_RIGHT_TOP      = 6,
51         EXIF_ORIENTATION_RIGHT_BOTTOM   = 7,
52         EXIF_ORIENTATION_LEFT_BOTTOM    = 8
53 } ExifOrientationType;
54 #endif
55
56 #define GET_RIGHT_PIXBUF_OFFSET(rc) \
57         (( (rc->stereo_mode & PR_STEREO_RIGHT) && !(rc->stereo_mode & PR_STEREO_SWAP)) || \
58          (!(rc->stereo_mode & PR_STEREO_RIGHT) &&  (rc->stereo_mode & PR_STEREO_SWAP)) ?  \
59           rc->pr->stereo_pixbuf_offset_right : rc->pr->stereo_pixbuf_offset_left )
60
61 #define GET_LEFT_PIXBUF_OFFSET(rc) \
62         ((!(rc->stereo_mode & PR_STEREO_RIGHT) && !(rc->stereo_mode & PR_STEREO_SWAP)) || \
63          ( (rc->stereo_mode & PR_STEREO_RIGHT) &&  (rc->stereo_mode & PR_STEREO_SWAP)) ?  \
64           rc->pr->stereo_pixbuf_offset_right : rc->pr->stereo_pixbuf_offset_left )
65
66
67 typedef struct _OverlayData OverlayData;
68 struct _OverlayData
69 {
70         gint id;
71
72         GdkPixbuf *pixbuf;
73         ClutterActor *actor;
74
75         gint x;
76         gint y;
77
78         OverlayRendererFlags flags;
79 };
80
81 typedef struct _RendererClutter RendererClutter;
82
83 struct _RendererClutter
84 {
85         RendererFuncs f;
86         PixbufRenderer *pr;
87
88         
89         gint stereo_mode;
90         gint stereo_off_x;
91         gint stereo_off_y;
92         
93         
94         GList *pending_updates;
95         gint idle_update;
96         
97         GList *overlay_list;
98         
99         GtkWidget *widget; /* widget and stage may be shared with other renderers */
100         ClutterActor *stage;
101         ClutterActor *texture;
102         ClutterActor *group;
103 };
104
105 typedef struct _RendererClutterAreaParam RendererClutterAreaParam;
106 struct _RendererClutterAreaParam {
107         RendererClutter *rc;
108         gint x;
109         gint y;
110         gint w;
111         gint h;
112 };
113
114
115 static void rc_sync_actor(RendererClutter *rc)
116 {
117         PixbufRenderer *pr = rc->pr;
118         gint anchor_x = 0;
119         gint anchor_y = 0;
120         
121         clutter_actor_set_anchor_point(CLUTTER_ACTOR(rc->texture), 0, 0);
122
123         printf("scale %d %d\n", rc->pr->width, rc->pr->height);
124         printf("pos   %d %d\n", rc->pr->x_offset, rc->pr->y_offset);
125         
126         clutter_actor_set_scale(CLUTTER_ACTOR(rc->texture), 
127                                 (gfloat)pr->width / pr->image_width,
128                                 (gfloat)pr->height / pr->image_height);
129                                 
130         switch (pr->orientation)
131                 {
132                 case EXIF_ORIENTATION_TOP_LEFT:
133                         /* normal  */
134                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
135                                                 CLUTTER_Z_AXIS,
136                                                 0, 0, 0, 0);
137                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
138                                                 CLUTTER_Y_AXIS,
139                                                 0, 0, 0, 0);
140                         anchor_x = 0;
141                         anchor_y = 0;
142                         break;
143                 case EXIF_ORIENTATION_TOP_RIGHT:
144                         /* mirrored */
145                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
146                                                 CLUTTER_Z_AXIS,
147                                                 0, 0, 0, 0);
148                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
149                                                 CLUTTER_Y_AXIS,
150                                                 180, 0, 0, 0);
151                         anchor_x = pr->width;
152                         anchor_y = 0;
153                         break;
154                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
155                         /* upside down */
156                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
157                                                 CLUTTER_Z_AXIS,
158                                                 180, 0, 0, 0);
159                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
160                                                 CLUTTER_Y_AXIS,
161                                                 0, 0, 0, 0);
162                         anchor_x = pr->width;
163                         anchor_y = pr->height;
164                         break;
165                 case EXIF_ORIENTATION_BOTTOM_LEFT:
166                         /* flipped */
167                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
168                                                 CLUTTER_Z_AXIS,
169                                                 180, 0, 0, 0);
170                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
171                                                 CLUTTER_Y_AXIS,
172                                                 180, 0, 0, 0);
173                         anchor_x = 0;
174                         anchor_y = pr->height;
175                         break;
176                 case EXIF_ORIENTATION_LEFT_TOP:
177                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
178                                                 CLUTTER_Z_AXIS,
179                                                 -90, 0, 0, 0);
180                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
181                                                 CLUTTER_Y_AXIS,
182                                                 180, 0, 0, 0);
183                         anchor_x = 0;
184                         anchor_y = 0;
185                         break;
186                 case EXIF_ORIENTATION_RIGHT_TOP:
187                         /* rotated -90 (270) */
188                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
189                                                 CLUTTER_Z_AXIS,
190                                                 -90, 0, 0, 0);
191                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
192                                                 CLUTTER_Y_AXIS,
193                                                 0, 0, 0, 0);
194                         anchor_x = 0;
195                         anchor_y = pr->height;
196                         break;
197                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
198                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
199                                                 CLUTTER_Z_AXIS,
200                                                 90, 0, 0, 0);
201                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
202                                                 CLUTTER_Y_AXIS,
203                                                 180, 0, 0, 0);
204                         anchor_x = pr->width;
205                         anchor_y = pr->height;
206                         break;
207                 case EXIF_ORIENTATION_LEFT_BOTTOM:
208                         /* rotated 90 */
209                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
210                                                 CLUTTER_Z_AXIS,
211                                                 90, 0, 0, 0);
212                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
213                                                 CLUTTER_Y_AXIS,
214                                                 0, 0, 0, 0);
215                         anchor_x = pr->width;
216                         anchor_y = 0;
217                         break;
218                 default:
219                         /* The other values are out of range */
220                         break;
221                 }
222         
223         clutter_actor_set_position(CLUTTER_ACTOR(rc->texture), 
224                                 pr->x_offset - pr->x_scroll + anchor_x, 
225                                 pr->y_offset - pr->y_scroll + anchor_y);
226
227 }
228
229
230 static void renderer_area_clip_add(RendererClutter *rc, gfloat x, gfloat y, gfloat w, gfloat h)
231 {
232         PixbufRenderer *pr = rc->pr;
233         gfloat x2, y2;
234         gfloat clip_x, clip_y, clip_w, clip_h, clip_x2, clip_y2;
235         
236         x2 = x + w;
237         y2 = y + h;
238         
239         clutter_actor_get_clip(rc->texture, &clip_x, &clip_y, &clip_w, &clip_h);
240         
241         clip_x2 = clip_x + clip_w;
242         clip_y2 = clip_y + clip_h;
243         
244         if (clip_x > x) clip_x = x;
245         if (clip_x2 < x2) clip_x2 = x2;
246         if (clip_y > y) clip_y = y;
247         if (clip_y2 < y2) clip_y2 = y2;
248         
249         clip_w = clip_x2 - clip_x;
250         clip_h = clip_y2 - clip_y;
251         
252         printf("clip %f %f %f %f\n", clip_x, clip_y, clip_w, clip_h);
253         clutter_actor_set_clip(rc->texture, clip_x, clip_y, clip_w, clip_h);
254 }
255
256 #define MAX_REGION_AREA (32768 * 1024)
257
258 static gboolean renderer_area_changed_cb(gpointer data)
259 {
260         RendererClutter *rc = (RendererClutter *)data;
261         PixbufRenderer *pr = rc->pr;
262         
263         RendererClutterAreaParam *par = rc->pending_updates->data;
264         
265         gint h = MAX_REGION_AREA / par->w;
266         if (h == 0) h = 1;
267         if (h > par->h) h = par->h;
268         
269         
270         printf("renderer_area_changed_cb %d %d %d %d  (%d)\n", par->x, par->y, par->w, h, par->h);
271         if (pr->pixbuf)
272                 {
273                 CoglHandle texture = clutter_texture_get_cogl_texture(CLUTTER_TEXTURE(rc->texture));
274                 
275                 cogl_texture_set_region(texture,
276                                         par->x + GET_RIGHT_PIXBUF_OFFSET(rc),
277                                         par->y,
278                                         par->x,
279                                         par->y,
280                                         par->w,
281                                         h,
282                                         par->w,
283                                         h,
284                                         gdk_pixbuf_get_has_alpha(pr->pixbuf) ? COGL_PIXEL_FORMAT_RGBA_8888 : COGL_PIXEL_FORMAT_RGB_888,
285                                         gdk_pixbuf_get_rowstride(pr->pixbuf),
286                                         gdk_pixbuf_get_pixels(pr->pixbuf));
287                 }
288         renderer_area_clip_add(rc, par->x, par->y, par->w, h);
289
290                 
291         par->y += h;
292         par->h -= h;
293         
294         if (par->h == 0)
295                 {
296                 rc->pending_updates = g_list_remove(rc->pending_updates, par);
297                 g_free(par);
298                 }
299         if (!rc->pending_updates)
300                 {
301                 clutter_actor_queue_redraw(CLUTTER_ACTOR(rc->texture));
302                 rc->idle_update = 0;
303                 return FALSE;
304                 }
305         return TRUE;
306 }
307
308
309 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
310 {
311         RendererClutter *rc = (RendererClutter *)renderer;
312         PixbufRenderer *pr = rc->pr;
313         RendererClutterAreaParam *par;
314
315         gint width = gdk_pixbuf_get_width(pr->pixbuf);
316         gint height = gdk_pixbuf_get_height(pr->pixbuf);
317                 
318         if (pr->stereo_data == STEREO_PIXBUF_SBS || pr->stereo_data == STEREO_PIXBUF_CROSS) 
319                         {
320                         width /= 2;
321                         }
322         
323         if (!pr_clip_region(src_x, src_y, src_w, src_h,
324                             GET_RIGHT_PIXBUF_OFFSET(rc), 0, width, height,
325                             &src_x, &src_y, &src_w, &src_h)) return;
326         
327         par = g_new0(RendererClutterAreaParam, 1);
328         par->rc = rc;
329         par->x = src_x - GET_RIGHT_PIXBUF_OFFSET(rc);
330         par->y = src_y;
331         par->w = src_w;
332         par->h = src_h;
333         rc->pending_updates = g_list_append(rc->pending_updates, par);
334         if (!rc->idle_update) 
335                 {
336                 rc->idle_update = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, renderer_area_changed_cb, rc, NULL);
337                 }
338 }
339
340 static void renderer_remove_pending_updates(RendererClutter *rc)
341 {
342         if (rc->idle_update) g_idle_remove_by_data(rc);
343         rc->idle_update = 0;
344         while (rc->pending_updates)
345                 {
346                 RendererClutterAreaParam *par = rc->pending_updates->data;
347                 rc->pending_updates = g_list_remove(rc->pending_updates, par);
348                 g_free(par);
349                 }
350 }
351
352 static void renderer_update_pixbuf(void *renderer, gboolean lazy)
353 {
354         RendererClutter *rc = (RendererClutter *)renderer;
355         PixbufRenderer *pr = rc->pr;
356         
357         renderer_remove_pending_updates(rc);
358         
359         if (pr->pixbuf)
360                 {
361                 gint width = gdk_pixbuf_get_width(pr->pixbuf);
362                 gint height = gdk_pixbuf_get_height(pr->pixbuf);
363                 
364                 gint prev_width, prev_height;
365                 
366                 if (pr->stereo_data == STEREO_PIXBUF_SBS || pr->stereo_data == STEREO_PIXBUF_CROSS) 
367                         {
368                         width /= 2;
369                         }
370
371                 
372                 printf("renderer_update_pixbuf\n");
373                 clutter_texture_get_base_size(CLUTTER_TEXTURE(rc->texture), &prev_width, &prev_height);
374                 printf("change from %d %d to %d %d\n", prev_width, prev_height, width, height);
375                 
376                 if (width != prev_width || height != prev_height)
377                         {
378                         /* FIXME use CoglMaterial with multiple textures for background, color management, anaglyph, ... */
379                         CoglHandle texture = cogl_texture_new_with_size(width,
380                                                                         height,
381                                                                         COGL_TEXTURE_NONE,
382                                                                         gdk_pixbuf_get_has_alpha(pr->pixbuf) ? COGL_PIXEL_FORMAT_RGBA_8888 : COGL_PIXEL_FORMAT_RGB_888);
383
384                         if (texture != COGL_INVALID_HANDLE)
385                                 {
386                                 clutter_texture_set_cogl_texture(CLUTTER_TEXTURE(rc->texture), texture);
387                                 cogl_handle_unref(texture);
388                                 }
389                         }
390                 clutter_actor_set_clip(rc->texture, 0, 0, 0, 0); /* visible area is extended as area_changed events arrive */
391                 if (!lazy)
392                         {
393                         renderer_area_changed(renderer, GET_RIGHT_PIXBUF_OFFSET(rc), 0, width, height);
394                         }
395                 }
396
397
398         printf("renderer_update_pixbuf\n");
399         rc_sync_actor(rc);
400 }
401
402
403
404 static void renderer_update_zoom(void *renderer, gboolean lazy)
405 {
406         RendererClutter *rc = (RendererClutter *)renderer;
407         PixbufRenderer *pr = rc->pr;
408
409         printf("renderer_update_zoom\n");
410         rc_sync_actor(rc);
411 }
412
413 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
414 {
415 }
416
417 static OverlayData *rc_overlay_find(RendererClutter *rc, gint id)
418 {
419         GList *work;
420
421         work = rc->overlay_list;
422         while (work)
423                 {
424                 OverlayData *od = work->data;
425                 work = work->next;
426
427                 if (od->id == id) return od;
428                 }
429
430         return NULL;
431 }
432
433 static void rc_overlay_actor_destroy_cb(ClutterActor *actor, gpointer user_data)
434 {
435         OverlayData *od = user_data;
436         od->actor = NULL;
437 }
438
439 static void rc_overlay_free(RendererClutter *rc, OverlayData *od)
440 {
441         rc->overlay_list = g_list_remove(rc->overlay_list, od);
442
443         if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
444         if (od->actor) clutter_actor_destroy(od->actor);
445         g_free(od);
446 }
447
448 static void rc_overlay_update_position(RendererClutter *rc, OverlayData *od)
449 {
450         gint px, py, pw, ph;
451
452         pw = gdk_pixbuf_get_width(od->pixbuf);
453         ph = gdk_pixbuf_get_height(od->pixbuf);
454         px = od->x;
455         py = od->y;
456
457         if (od->flags & OVL_RELATIVE)
458                 {
459                 if (px < 0) px = rc->pr->viewport_width - pw + px;
460                 if (py < 0) py = rc->pr->viewport_height - ph + py;
461                 }
462         if (od->actor) clutter_actor_set_position(od->actor, px, py);
463 }
464
465 static void rc_overlay_update_positions(RendererClutter *rc)
466 {
467         GList *work;
468
469         work = rc->overlay_list;
470         while (work)
471                 {
472                 OverlayData *od = work->data;
473                 work = work->next;
474
475                 rc_overlay_update_position(rc, od);
476                 }
477 }
478
479 static void rc_overlay_free_all(RendererClutter *rc)
480 {
481         GList *work;
482
483         work = rc->overlay_list;
484         while (work)
485                 {
486                 OverlayData *od = work->data;
487                 work = work->next;
488
489                 rc_overlay_free(rc, od);
490                 }
491 }
492
493
494 static void renderer_overlay_draw(void *renderer, gint x, gint y, gint w, gint h)
495 {
496 }
497
498 static gint renderer_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y, OverlayRendererFlags flags)
499 {
500         RendererClutter *rc = (RendererClutter *)renderer;
501         PixbufRenderer *pr = rc->pr;
502         OverlayData *od;
503         gint id;
504
505         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
506         g_return_val_if_fail(pixbuf != NULL, -1);
507
508         id = 1;
509         while (rc_overlay_find(rc, id)) id++;
510
511         od = g_new0(OverlayData, 1);
512         od->id = id;
513         od->pixbuf = pixbuf;
514         g_object_ref(G_OBJECT(od->pixbuf));
515         od->x = x;
516         od->y = y;
517         od->flags = flags;
518         
519         od->actor = gtk_clutter_texture_new();
520         g_signal_connect (od->actor, "destroy", G_CALLBACK(rc_overlay_actor_destroy_cb), od);
521         
522         gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE (od->actor), pixbuf, NULL);
523         clutter_container_add_actor(CLUTTER_CONTAINER(rc->group), od->actor);
524
525         rc->overlay_list = g_list_append(rc->overlay_list, od);
526         rc_overlay_update_position(rc, od);
527
528         return od->id;
529 }
530
531 static void renderer_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
532 {
533         RendererClutter *rc = (RendererClutter *)renderer;
534         PixbufRenderer *pr = rc->pr;
535         OverlayData *od;
536
537         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
538
539         od = rc_overlay_find(rc, id);
540         if (!od) return;
541
542         if (pixbuf)
543                 {
544                 g_object_ref(G_OBJECT(pixbuf));
545                 g_object_unref(G_OBJECT(od->pixbuf));
546                 od->pixbuf = pixbuf;
547
548                 od->x = x;
549                 od->y = y;
550
551                 if (od->actor) gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE(od->actor), pixbuf, NULL);
552                 rc_overlay_update_position(rc, od);
553                 }
554         else
555                 {
556                 rc_overlay_free(rc, od);
557                 }
558 }
559
560 static gboolean renderer_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
561 {
562         RendererClutter *rc = (RendererClutter *)renderer;
563
564         PixbufRenderer *pr = rc->pr;
565         OverlayData *od;
566
567         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
568
569         od = rc_overlay_find(rc, id);
570         if (!od) return FALSE;
571
572         if (pixbuf) *pixbuf = od->pixbuf;
573         if (x) *x = od->x;
574         if (y) *y = od->y;
575
576         return TRUE;
577 }
578
579
580 static void renderer_update_sizes(void *renderer)
581 {
582         RendererClutter *rc = (RendererClutter *)renderer;
583         ClutterColor stage_color = { 0x00, 0x00, 0x00, 0xff }; 
584
585         rc->stereo_off_x = 0;
586         rc->stereo_off_y = 0;
587         
588         if (rc->stereo_mode & PR_STEREO_RIGHT) 
589                 {
590                 if (rc->stereo_mode & PR_STEREO_HORIZ) 
591                         {
592                         rc->stereo_off_x = rc->pr->viewport_width;
593                         }
594                 else if (rc->stereo_mode & PR_STEREO_VERT) 
595                         {
596                         rc->stereo_off_y = rc->pr->viewport_height;
597                         }
598                 else if (rc->stereo_mode & PR_STEREO_FIXED) 
599                         {
600                         rc->stereo_off_x = rc->pr->stereo_fixed_x_right;
601                         rc->stereo_off_y = rc->pr->stereo_fixed_y_right;
602                         }
603                 }
604         else
605                 {
606                 if (rc->stereo_mode & PR_STEREO_FIXED) 
607                         {
608                         rc->stereo_off_x = rc->pr->stereo_fixed_x_left;
609                         rc->stereo_off_y = rc->pr->stereo_fixed_y_left;
610                         }
611                 }
612         DEBUG_1("update size: %p  %d %d   %d %d", rc, rc->stereo_off_x, rc->stereo_off_y, rc->pr->viewport_width, rc->pr->viewport_height);
613
614         printf("renderer_update_sizes  scale %d %d\n", rc->pr->width, rc->pr->height);
615
616         clutter_stage_set_color(CLUTTER_STAGE(rc->stage), &stage_color);
617
618
619         clutter_actor_set_size(rc->group, rc->pr->viewport_width, rc->pr->viewport_height);
620         clutter_actor_set_position(rc->group, rc->stereo_off_x, rc->stereo_off_y);
621         
622         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->group),
623                                                 CLUTTER_Y_AXIS,
624                                                 (rc->stereo_mode & PR_STEREO_MIRROR) ? 180 : 0, 
625                                                 rc->pr->viewport_width / 2.0, 0, 0);
626
627         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->group),
628                                                 CLUTTER_X_AXIS,
629                                                 (rc->stereo_mode & PR_STEREO_FLIP) ? 180 : 0,
630                                                 0, rc->pr->viewport_height / 2.0, 0);
631
632         rc_sync_actor(rc);
633         rc_overlay_update_positions(rc);
634 }
635
636 static void renderer_scroll(void *renderer, gint x_off, gint y_off)
637 {
638         printf("renderer_scroll\n");
639         RendererClutter *rc = (RendererClutter *)renderer;
640         PixbufRenderer *pr = rc->pr;
641
642         rc_sync_actor(rc);
643 }
644
645 static void renderer_stereo_set(void *renderer, gint stereo_mode)
646 {
647         RendererClutter *rc = (RendererClutter *)renderer;
648
649         rc->stereo_mode = stereo_mode;
650 }
651
652 static void renderer_free(void *renderer)
653 {
654         RendererClutter *rc = (RendererClutter *)renderer;
655         GtkWidget *widget = gtk_bin_get_child(GTK_BIN(rc->pr));
656
657         renderer_remove_pending_updates(rc);
658
659         rc_overlay_free_all(rc);
660         
661         if (widget)
662                 {
663                 /* widget still exists */
664                 clutter_actor_destroy(rc->group);
665                 if (clutter_group_get_n_children(CLUTTER_GROUP(rc->stage)) == 0)
666                         {
667                         printf("destroy %p\n", rc->widget);
668                         /* this was the last user */
669                         gtk_widget_destroy(rc->widget);
670                         }
671                 else
672                         {
673                         printf("keep %p\n", rc->widget);
674                         g_object_unref(G_OBJECT(rc->widget));
675                         }
676                 }
677         g_free(rc);
678 }
679
680 RendererFuncs *renderer_clutter_new(PixbufRenderer *pr)
681 {
682         RendererClutter *rc = g_new0(RendererClutter, 1);
683         
684         rc->pr = pr;
685         
686         rc->f.area_changed = renderer_area_changed;
687         rc->f.update_pixbuf = renderer_update_pixbuf;
688         rc->f.free = renderer_free;
689         rc->f.update_zoom = renderer_update_zoom;
690         rc->f.invalidate_region = renderer_invalidate_region;
691         rc->f.scroll = renderer_scroll;
692         rc->f.update_sizes = renderer_update_sizes;
693
694
695         rc->f.overlay_add = renderer_overlay_add;
696         rc->f.overlay_set = renderer_overlay_set;
697         rc->f.overlay_get = renderer_overlay_get;
698         rc->f.overlay_draw = renderer_overlay_draw;
699
700         rc->f.stereo_set = renderer_stereo_set;
701         
702         
703         rc->stereo_mode = 0;
704         rc->stereo_off_x = 0;
705         rc->stereo_off_y = 0;
706
707         rc->idle_update = 0;
708         rc->pending_updates = NULL;
709
710         rc->widget = gtk_bin_get_child(GTK_BIN(rc->pr));
711         
712         if (rc->widget)
713                 {
714                 if (!GTK_CLUTTER_IS_EMBED(rc->widget))
715                         {
716                         g_free(rc);
717                         DEBUG_0("pixbuf renderer has a child of other type than gtk_clutter_embed");
718                         return NULL;
719                         }
720                 }
721         else 
722                 {
723                 rc->widget = gtk_clutter_embed_new();
724                 gtk_container_add(GTK_CONTAINER(rc->pr), rc->widget);
725                 }
726                 
727         gtk_event_box_set_above_child (GTK_EVENT_BOX(rc->pr), TRUE);
728         rc->stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (rc->widget));
729         
730         rc->group = clutter_group_new();
731         clutter_container_add_actor(CLUTTER_CONTAINER(rc->stage), rc->group);
732         clutter_actor_set_clip_to_allocation(CLUTTER_ACTOR(rc->group), TRUE);
733   
734         rc->texture = clutter_texture_new ();
735         clutter_container_add_actor(CLUTTER_CONTAINER(rc->group), rc->texture);
736         g_object_ref(G_OBJECT(rc->widget));
737   
738         gtk_widget_show(rc->widget);
739         return (RendererFuncs *) rc;
740 }
741
742 #endif 
743 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */