clutter renderer code cleanup
[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 /* for 3d texture */
31 #define COGL_ENABLE_EXPERIMENTAL_API
32
33 #include <clutter/clutter.h>
34
35 #include <clutter-gtk/clutter-gtk.h>
36
37
38
39 #define GQ_BUILD 1
40
41 #ifdef GQ_BUILD
42 #include "main.h"
43 #include "pixbuf_util.h"
44 #include "exif.h"
45 #else
46 typedef enum {
47         EXIF_ORIENTATION_UNKNOWN        = 0,
48         EXIF_ORIENTATION_TOP_LEFT       = 1,
49         EXIF_ORIENTATION_TOP_RIGHT      = 2,
50         EXIF_ORIENTATION_BOTTOM_RIGHT   = 3,
51         EXIF_ORIENTATION_BOTTOM_LEFT    = 4,
52         EXIF_ORIENTATION_LEFT_TOP       = 5,
53         EXIF_ORIENTATION_RIGHT_TOP      = 6,
54         EXIF_ORIENTATION_RIGHT_BOTTOM   = 7,
55         EXIF_ORIENTATION_LEFT_BOTTOM    = 8
56 } ExifOrientationType;
57 #endif
58
59 #define GET_RIGHT_PIXBUF_OFFSET(rc) \
60         (( (rc->stereo_mode & PR_STEREO_RIGHT) && !(rc->stereo_mode & PR_STEREO_SWAP)) || \
61          (!(rc->stereo_mode & PR_STEREO_RIGHT) &&  (rc->stereo_mode & PR_STEREO_SWAP)) ?  \
62           rc->pr->stereo_pixbuf_offset_right : rc->pr->stereo_pixbuf_offset_left )
63
64 #define GET_LEFT_PIXBUF_OFFSET(rc) \
65         ((!(rc->stereo_mode & PR_STEREO_RIGHT) && !(rc->stereo_mode & PR_STEREO_SWAP)) || \
66          ( (rc->stereo_mode & PR_STEREO_RIGHT) &&  (rc->stereo_mode & PR_STEREO_SWAP)) ?  \
67           rc->pr->stereo_pixbuf_offset_right : rc->pr->stereo_pixbuf_offset_left )
68
69
70 typedef struct _OverlayData OverlayData;
71 struct _OverlayData
72 {
73         gint id;
74
75         GdkPixbuf *pixbuf;
76         ClutterActor *actor;
77
78         gint x;
79         gint y;
80
81         OverlayRendererFlags flags;
82 };
83
84 typedef struct _RendererClutter RendererClutter;
85
86 struct _RendererClutter
87 {
88         RendererFuncs f;
89         PixbufRenderer *pr;
90
91         
92         gint stereo_mode;
93         gint stereo_off_x;
94         gint stereo_off_y;
95         
96         
97         GList *pending_updates;
98         gint idle_update;
99         
100         GList *overlay_list;
101         
102         GtkWidget *widget; /* widget and stage may be shared with other renderers */
103         ClutterActor *stage;
104         ClutterActor *texture;
105         ClutterActor *group;
106         
107         gboolean clut_updated;
108         gint64 last_pixbuf_change;
109 };
110
111 typedef struct _RendererClutterAreaParam RendererClutterAreaParam;
112 struct _RendererClutterAreaParam {
113         RendererClutter *rc;
114         gint x;
115         gint y;
116         gint w;
117         gint h;
118 };
119
120 #define CLUT_SIZE       32
121
122 static void rc_set_shader(CoglHandle material)
123 {
124   CoglHandle shader;
125   CoglHandle program;
126   gint uniform_no;
127   shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
128   cogl_shader_source (shader,
129   "vec3 checker(vec2 texc, vec3 color0, vec3 color1)                                            \n"
130   "{                                                                                            \n"
131   "  if (mod(int(floor(texc.x) + floor(texc.y)), 2) == 0)                                       \n"
132   "    return color0;                                                                           \n"
133   "  else                                                                                       \n"
134   "    return color1;                                                                           \n"
135   "}                                                                                            \n"
136   "                                                                                             \n"
137   "uniform sampler2D tex;                                                                       \n"
138   "uniform sampler3D clut;                                                                      \n"
139   "uniform float scale;                                                                         \n"
140   "uniform float offset;                                                                        \n"
141   "                                                                                             \n"
142   "void main(void)                                                                              \n"
143   "{                                                                                            \n"
144   "    vec3 bg = checker(gl_FragCoord.xy / 16, vec3(0.6, 0.6, 0.6), vec3(0.4, 0.4, 0.4));       \n"
145   "    vec4 img4 = texture2D(tex, gl_TexCoord[0].xy);                                           \n"
146   "    vec3 img3 = img4.rgb;                                                                    \n"
147   "    img3 = img3 * scale + offset;                                                            \n"
148   "    img3 = texture3D(clut, img3);                                                            \n"
149   "                                                                                             \n"
150   "    gl_FragColor = vec4(img3 * img4.a + bg * (1.0 - img4.a), 1.0);                           \n"
151   "}                                                                                            \n"
152   );
153   cogl_shader_compile(shader);
154   gchar *err = cogl_shader_get_info_log(shader);
155   DEBUG_0("%s\n",err);
156   g_free(err);
157
158   program = cogl_create_program ();
159   cogl_program_attach_shader (program, shader);
160   cogl_handle_unref (shader);
161   cogl_program_link (program);
162
163   uniform_no = cogl_program_get_uniform_location (program, "tex");
164   cogl_program_set_uniform_1i (program, uniform_no, 0);
165
166   uniform_no = cogl_program_get_uniform_location (program, "clut");
167   cogl_program_set_uniform_1i (program, uniform_no, 1);
168
169   uniform_no = cogl_program_get_uniform_location (program, "scale");
170   cogl_program_set_uniform_1f (program, uniform_no, (double) (CLUT_SIZE - 1) / CLUT_SIZE);
171
172   uniform_no = cogl_program_get_uniform_location (program, "offset");
173   cogl_program_set_uniform_1f (program, uniform_no, 1.0 / (2 * CLUT_SIZE));
174
175   cogl_material_set_user_program (material, program);
176   cogl_handle_unref (program);
177 }
178
179
180 static void rc_prepare_post_process_lut(RendererClutter *rc)
181 {
182         PixbufRenderer *pr = rc->pr;
183         static guchar clut[CLUT_SIZE * CLUT_SIZE * CLUT_SIZE * 3];
184         guint r, g, b;
185         GdkPixbuf *tmp_pixbuf;
186         CoglHandle material;
187         CoglHandle tex3d;
188         
189         DEBUG_0("%s clut start", get_exec_time());
190
191         for (r = 0; r < CLUT_SIZE; r++) 
192                 {
193                 for (g = 0; g < CLUT_SIZE; g++) 
194                         {
195                         for (b = 0; b < CLUT_SIZE; b++) 
196                                 {
197                                 guchar *ptr = clut + ((b * CLUT_SIZE + g) * CLUT_SIZE + r) * 3;
198                                 ptr[0] = floor ((double) r / (CLUT_SIZE - 1) * 255.0 + 0.5);
199                                 ptr[1] = floor ((double) g / (CLUT_SIZE - 1) * 255.0 + 0.5);
200                                 ptr[2] = floor ((double) b / (CLUT_SIZE - 1) * 255.0 + 0.5);
201                                 }
202                         }
203                 }
204         tmp_pixbuf = gdk_pixbuf_new_from_data(clut, GDK_COLORSPACE_RGB, FALSE, 8,
205                                               CLUT_SIZE * CLUT_SIZE,
206                                               CLUT_SIZE,
207                                               CLUT_SIZE * CLUT_SIZE * 3,
208                                               NULL, NULL);
209         if (pr->func_post_process)
210                 {
211                 pr->func_post_process(pr, &tmp_pixbuf, 0, 0, CLUT_SIZE * CLUT_SIZE, CLUT_SIZE, pr->post_process_user_data);
212                 }
213         g_object_unref(tmp_pixbuf);
214
215         DEBUG_0("%s clut upload start", get_exec_time());
216         
217         tex3d = cogl_texture_3d_new_from_data(CLUT_SIZE, CLUT_SIZE, CLUT_SIZE,
218                                               COGL_TEXTURE_NONE,
219                                               COGL_PIXEL_FORMAT_RGB_888,
220                                               COGL_PIXEL_FORMAT_RGB_888,
221                                               CLUT_SIZE * 3,
222                                               CLUT_SIZE * CLUT_SIZE * 3,
223                                               clut,
224                                               NULL);
225         material = clutter_texture_get_cogl_material(CLUTTER_TEXTURE(rc->texture));
226         cogl_material_set_layer(material, 1, tex3d);
227         cogl_handle_unref(tex3d);
228         DEBUG_0("%s clut end", get_exec_time());
229         rc->clut_updated = TRUE;
230 }
231
232
233
234 static void rc_sync_actor(RendererClutter *rc)
235 {
236         PixbufRenderer *pr = rc->pr;
237         gint anchor_x = 0;
238         gint anchor_y = 0;
239         
240         clutter_actor_set_anchor_point(CLUTTER_ACTOR(rc->texture), 0, 0);
241
242         DEBUG_0("scale %d %d", rc->pr->width, rc->pr->height);
243         DEBUG_0("pos   %d %d", rc->pr->x_offset, rc->pr->y_offset);
244         
245         clutter_actor_set_scale(CLUTTER_ACTOR(rc->texture), 
246                                 (gfloat)pr->width / pr->image_width,
247                                 (gfloat)pr->height / pr->image_height);
248                                 
249         switch (pr->orientation)
250                 {
251                 case EXIF_ORIENTATION_TOP_LEFT:
252                         /* normal  */
253                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
254                                                 CLUTTER_Z_AXIS,
255                                                 0, 0, 0, 0);
256                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
257                                                 CLUTTER_Y_AXIS,
258                                                 0, 0, 0, 0);
259                         anchor_x = 0;
260                         anchor_y = 0;
261                         break;
262                 case EXIF_ORIENTATION_TOP_RIGHT:
263                         /* mirrored */
264                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
265                                                 CLUTTER_Z_AXIS,
266                                                 0, 0, 0, 0);
267                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
268                                                 CLUTTER_Y_AXIS,
269                                                 180, 0, 0, 0);
270                         anchor_x = pr->width;
271                         anchor_y = 0;
272                         break;
273                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
274                         /* upside down */
275                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
276                                                 CLUTTER_Z_AXIS,
277                                                 180, 0, 0, 0);
278                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
279                                                 CLUTTER_Y_AXIS,
280                                                 0, 0, 0, 0);
281                         anchor_x = pr->width;
282                         anchor_y = pr->height;
283                         break;
284                 case EXIF_ORIENTATION_BOTTOM_LEFT:
285                         /* flipped */
286                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
287                                                 CLUTTER_Z_AXIS,
288                                                 180, 0, 0, 0);
289                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
290                                                 CLUTTER_Y_AXIS,
291                                                 180, 0, 0, 0);
292                         anchor_x = 0;
293                         anchor_y = pr->height;
294                         break;
295                 case EXIF_ORIENTATION_LEFT_TOP:
296                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
297                                                 CLUTTER_Z_AXIS,
298                                                 -90, 0, 0, 0);
299                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
300                                                 CLUTTER_Y_AXIS,
301                                                 180, 0, 0, 0);
302                         anchor_x = 0;
303                         anchor_y = 0;
304                         break;
305                 case EXIF_ORIENTATION_RIGHT_TOP:
306                         /* rotated -90 (270) */
307                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
308                                                 CLUTTER_Z_AXIS,
309                                                 -90, 0, 0, 0);
310                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
311                                                 CLUTTER_Y_AXIS,
312                                                 0, 0, 0, 0);
313                         anchor_x = 0;
314                         anchor_y = pr->height;
315                         break;
316                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
317                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
318                                                 CLUTTER_Z_AXIS,
319                                                 90, 0, 0, 0);
320                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
321                                                 CLUTTER_Y_AXIS,
322                                                 180, 0, 0, 0);
323                         anchor_x = pr->width;
324                         anchor_y = pr->height;
325                         break;
326                 case EXIF_ORIENTATION_LEFT_BOTTOM:
327                         /* rotated 90 */
328                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
329                                                 CLUTTER_Z_AXIS,
330                                                 90, 0, 0, 0);
331                         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
332                                                 CLUTTER_Y_AXIS,
333                                                 0, 0, 0, 0);
334                         anchor_x = pr->width;
335                         anchor_y = 0;
336                         break;
337                 default:
338                         /* The other values are out of range */
339                         break;
340                 }
341         
342         clutter_actor_set_position(CLUTTER_ACTOR(rc->texture), 
343                                 pr->x_offset - pr->x_scroll + anchor_x, 
344                                 pr->y_offset - pr->y_scroll + anchor_y);
345
346 }
347
348
349 static void rc_area_clip_add(RendererClutter *rc, gfloat x, gfloat y, gfloat w, gfloat h)
350 {
351         gfloat x2, y2;
352         gfloat clip_x, clip_y, clip_w, clip_h, clip_x2, clip_y2;
353         
354         x2 = x + w;
355         y2 = y + h;
356         
357         clutter_actor_get_clip(rc->texture, &clip_x, &clip_y, &clip_w, &clip_h);
358         
359         clip_x2 = clip_x + clip_w;
360         clip_y2 = clip_y + clip_h;
361         
362         if (clip_x > x) clip_x = x;
363         if (clip_x2 < x2) clip_x2 = x2;
364         if (clip_y > y) clip_y = y;
365         if (clip_y2 < y2) clip_y2 = y2;
366         
367         clip_w = clip_x2 - clip_x;
368         clip_h = clip_y2 - clip_y;
369         
370         clutter_actor_set_clip(rc->texture, clip_x, clip_y, clip_w, clip_h);
371 }
372
373 #define MAX_REGION_AREA (32768 * 1024)
374
375 static gboolean rc_area_changed_cb(gpointer data);
376
377 static void rc_schedule_texture_upload(RendererClutter *rc)
378 {
379         if (g_get_monotonic_time() - rc->last_pixbuf_change < 50000)
380                 {
381                 /* delay clutter redraw until the texture has some data 
382                    set priority between gtk redraw and clutter redraw */
383                 rc->idle_update = g_idle_add_full(CLUTTER_PRIORITY_REDRAW - 10, rc_area_changed_cb, rc, NULL);
384                 }
385         else
386                 {
387                 /* higher prio than histogram */
388                 rc->idle_update = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 5, rc_area_changed_cb, rc, NULL);
389                 }
390 }
391
392 static gboolean rc_area_changed_cb(gpointer data)
393 {
394         RendererClutter *rc = (RendererClutter *)data;
395         PixbufRenderer *pr = rc->pr;
396         
397         RendererClutterAreaParam *par = rc->pending_updates->data;
398         
399         gint h = MAX_REGION_AREA / par->w;
400         if (h == 0) h = 1;
401         if (h > par->h) h = par->h;
402         
403         
404         DEBUG_0("%s upload start", get_exec_time());
405         if (pr->pixbuf)
406                 {
407                 CoglHandle texture = clutter_texture_get_cogl_texture(CLUTTER_TEXTURE(rc->texture));
408                 
409                 cogl_texture_set_region(texture,
410                                         par->x + GET_RIGHT_PIXBUF_OFFSET(rc),
411                                         par->y,
412                                         par->x,
413                                         par->y,
414                                         par->w,
415                                         h,
416                                         par->w,
417                                         h,
418                                         gdk_pixbuf_get_has_alpha(pr->pixbuf) ? COGL_PIXEL_FORMAT_RGBA_8888 : COGL_PIXEL_FORMAT_RGB_888,
419                                         gdk_pixbuf_get_rowstride(pr->pixbuf),
420                                         gdk_pixbuf_get_pixels(pr->pixbuf));
421                 }
422         DEBUG_0("%s upload end", get_exec_time());
423         rc_area_clip_add(rc, par->x, par->y, par->w, h);
424
425                 
426         par->y += h;
427         par->h -= h;
428         
429         if (par->h == 0)
430                 {
431                 rc->pending_updates = g_list_remove(rc->pending_updates, par);
432                 g_free(par);
433                 }
434         if (!rc->pending_updates)
435                 {
436                 clutter_actor_queue_redraw(CLUTTER_ACTOR(rc->texture));
437                 rc->idle_update = 0;
438
439                 /* FIXME: find a better place for this */
440                 if (!rc->clut_updated) rc_prepare_post_process_lut(rc);
441
442                 return FALSE;
443                 }
444
445         rc_schedule_texture_upload(rc);
446
447         return FALSE; /* it was rescheduled, possibly with different prio */
448 }
449
450
451 static void rc_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
452 {
453         RendererClutter *rc = (RendererClutter *)renderer;
454         PixbufRenderer *pr = rc->pr;
455         RendererClutterAreaParam *par;
456
457         gint width = gdk_pixbuf_get_width(pr->pixbuf);
458         gint height = gdk_pixbuf_get_height(pr->pixbuf);
459                 
460         if (pr->stereo_data == STEREO_PIXBUF_SBS || pr->stereo_data == STEREO_PIXBUF_CROSS) 
461                         {
462                         width /= 2;
463                         }
464         
465         if (!pr_clip_region(src_x, src_y, src_w, src_h,
466                             GET_RIGHT_PIXBUF_OFFSET(rc), 0, width, height,
467                             &src_x, &src_y, &src_w, &src_h)) return;
468         
469         par = g_new0(RendererClutterAreaParam, 1);
470         par->rc = rc;
471         par->x = src_x - GET_RIGHT_PIXBUF_OFFSET(rc);
472         par->y = src_y;
473         par->w = src_w;
474         par->h = src_h;
475         rc->pending_updates = g_list_append(rc->pending_updates, par);
476         if (!rc->idle_update) 
477                 {
478                 rc_schedule_texture_upload(rc);
479                 }
480 }
481
482 static void rc_remove_pending_updates(RendererClutter *rc)
483 {
484         if (rc->idle_update) g_idle_remove_by_data(rc);
485         rc->idle_update = 0;
486         while (rc->pending_updates)
487                 {
488                 RendererClutterAreaParam *par = rc->pending_updates->data;
489                 rc->pending_updates = g_list_remove(rc->pending_updates, par);
490                 g_free(par);
491                 }
492 }
493
494 static void rc_update_pixbuf(void *renderer, gboolean lazy)
495 {
496         RendererClutter *rc = (RendererClutter *)renderer;
497         PixbufRenderer *pr = rc->pr;
498         
499         DEBUG_0("rc_update_pixbuf");
500
501         rc_remove_pending_updates(rc);
502         
503         if (pr->pixbuf)
504                 {
505                 gint width = gdk_pixbuf_get_width(pr->pixbuf);
506                 gint height = gdk_pixbuf_get_height(pr->pixbuf);
507                 
508                 gint prev_width, prev_height;
509                 
510                 if (pr->stereo_data == STEREO_PIXBUF_SBS || pr->stereo_data == STEREO_PIXBUF_CROSS) 
511                         {
512                         width /= 2;
513                         }
514
515                 
516                 clutter_texture_get_base_size(CLUTTER_TEXTURE(rc->texture), &prev_width, &prev_height);
517                 
518                 if (width != prev_width || height != prev_height)
519                         {
520                         /* FIXME use CoglMaterial with multiple textures for background, color management, anaglyph, ... */
521                         CoglHandle texture = cogl_texture_new_with_size(width,
522                                                                         height,
523                                                                         COGL_TEXTURE_NO_AUTO_MIPMAP,
524                                                                         gdk_pixbuf_get_has_alpha(pr->pixbuf) ? COGL_PIXEL_FORMAT_RGBA_8888 : COGL_PIXEL_FORMAT_RGB_888);
525
526                         if (texture != COGL_INVALID_HANDLE)
527                                 {
528                                 clutter_texture_set_cogl_texture(CLUTTER_TEXTURE(rc->texture), texture);
529                                 cogl_handle_unref(texture);
530                                 }
531                         }
532                 clutter_actor_set_clip(rc->texture, 0, 0, 0, 0); /* visible area is extended as area_changed events arrive */
533                 if (!lazy)
534                         {
535                         rc_area_changed(renderer, GET_RIGHT_PIXBUF_OFFSET(rc), 0, width, height);
536                         }
537                 }
538
539         rc->clut_updated = FALSE;
540         rc->last_pixbuf_change = g_get_monotonic_time();
541         rc_sync_actor(rc);
542 }
543
544
545
546 static void rc_update_zoom(void *renderer, gboolean lazy)
547 {
548         RendererClutter *rc = (RendererClutter *)renderer;
549
550         DEBUG_0("rc_update_zoom");
551         rc_sync_actor(rc);
552 }
553
554 static void rc_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
555 {
556 }
557
558 static OverlayData *rc_overlay_find(RendererClutter *rc, gint id)
559 {
560         GList *work;
561
562         work = rc->overlay_list;
563         while (work)
564                 {
565                 OverlayData *od = work->data;
566                 work = work->next;
567
568                 if (od->id == id) return od;
569                 }
570
571         return NULL;
572 }
573
574 static void rc_overlay_actor_destroy_cb(ClutterActor *actor, gpointer user_data)
575 {
576         OverlayData *od = user_data;
577         od->actor = NULL;
578 }
579
580 static void rc_overlay_free(RendererClutter *rc, OverlayData *od)
581 {
582         rc->overlay_list = g_list_remove(rc->overlay_list, od);
583
584         if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
585         if (od->actor) clutter_actor_destroy(od->actor);
586         g_free(od);
587 }
588
589 static void rc_overlay_update_position(RendererClutter *rc, OverlayData *od)
590 {
591         gint px, py, pw, ph;
592
593         pw = gdk_pixbuf_get_width(od->pixbuf);
594         ph = gdk_pixbuf_get_height(od->pixbuf);
595         px = od->x;
596         py = od->y;
597
598         if (od->flags & OVL_RELATIVE)
599                 {
600                 if (px < 0) px = rc->pr->viewport_width - pw + px;
601                 if (py < 0) py = rc->pr->viewport_height - ph + py;
602                 }
603         if (od->actor) clutter_actor_set_position(od->actor, px, py);
604 }
605
606 static void rc_overlay_update_positions(RendererClutter *rc)
607 {
608         GList *work;
609
610         work = rc->overlay_list;
611         while (work)
612                 {
613                 OverlayData *od = work->data;
614                 work = work->next;
615
616                 rc_overlay_update_position(rc, od);
617                 }
618 }
619
620 static void rc_overlay_free_all(RendererClutter *rc)
621 {
622         GList *work;
623
624         work = rc->overlay_list;
625         while (work)
626                 {
627                 OverlayData *od = work->data;
628                 work = work->next;
629
630                 rc_overlay_free(rc, od);
631                 }
632 }
633
634
635 static void rc_overlay_draw(void *renderer, gint x, gint y, gint w, gint h)
636 {
637 }
638
639 static gint rc_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y, OverlayRendererFlags flags)
640 {
641         RendererClutter *rc = (RendererClutter *)renderer;
642         PixbufRenderer *pr = rc->pr;
643         OverlayData *od;
644         gint id;
645
646         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
647         g_return_val_if_fail(pixbuf != NULL, -1);
648
649         id = 1;
650         while (rc_overlay_find(rc, id)) id++;
651
652         od = g_new0(OverlayData, 1);
653         od->id = id;
654         od->pixbuf = pixbuf;
655         g_object_ref(G_OBJECT(od->pixbuf));
656         od->x = x;
657         od->y = y;
658         od->flags = flags;
659         
660         od->actor = gtk_clutter_texture_new();
661         g_signal_connect (od->actor, "destroy", G_CALLBACK(rc_overlay_actor_destroy_cb), od);
662         
663         gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE (od->actor), pixbuf, NULL);
664         clutter_container_add_actor(CLUTTER_CONTAINER(rc->group), od->actor);
665
666         rc->overlay_list = g_list_append(rc->overlay_list, od);
667         rc_overlay_update_position(rc, od);
668
669         return od->id;
670 }
671
672 static void rc_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
673 {
674         RendererClutter *rc = (RendererClutter *)renderer;
675         PixbufRenderer *pr = rc->pr;
676         OverlayData *od;
677
678         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
679
680         od = rc_overlay_find(rc, id);
681         if (!od) return;
682
683         if (pixbuf)
684                 {
685                 g_object_ref(G_OBJECT(pixbuf));
686                 g_object_unref(G_OBJECT(od->pixbuf));
687                 od->pixbuf = pixbuf;
688
689                 od->x = x;
690                 od->y = y;
691
692                 if (od->actor) gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE(od->actor), pixbuf, NULL);
693                 rc_overlay_update_position(rc, od);
694                 }
695         else
696                 {
697                 rc_overlay_free(rc, od);
698                 }
699 }
700
701 static gboolean rc_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
702 {
703         RendererClutter *rc = (RendererClutter *)renderer;
704
705         PixbufRenderer *pr = rc->pr;
706         OverlayData *od;
707
708         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
709
710         od = rc_overlay_find(rc, id);
711         if (!od) return FALSE;
712
713         if (pixbuf) *pixbuf = od->pixbuf;
714         if (x) *x = od->x;
715         if (y) *y = od->y;
716
717         return TRUE;
718 }
719
720
721 static void rc_update_sizes(void *renderer)
722 {
723         RendererClutter *rc = (RendererClutter *)renderer;
724         ClutterColor stage_color = { 0x00, 0x00, 0x00, 0xff }; 
725
726         rc->stereo_off_x = 0;
727         rc->stereo_off_y = 0;
728         
729         if (rc->stereo_mode & PR_STEREO_RIGHT) 
730                 {
731                 if (rc->stereo_mode & PR_STEREO_HORIZ) 
732                         {
733                         rc->stereo_off_x = rc->pr->viewport_width;
734                         }
735                 else if (rc->stereo_mode & PR_STEREO_VERT) 
736                         {
737                         rc->stereo_off_y = rc->pr->viewport_height;
738                         }
739                 else if (rc->stereo_mode & PR_STEREO_FIXED) 
740                         {
741                         rc->stereo_off_x = rc->pr->stereo_fixed_x_right;
742                         rc->stereo_off_y = rc->pr->stereo_fixed_y_right;
743                         }
744                 }
745         else
746                 {
747                 if (rc->stereo_mode & PR_STEREO_FIXED) 
748                         {
749                         rc->stereo_off_x = rc->pr->stereo_fixed_x_left;
750                         rc->stereo_off_y = rc->pr->stereo_fixed_y_left;
751                         }
752                 }
753         DEBUG_0("rc_update_sizes  scale %d %d", rc->pr->width, rc->pr->height);
754
755         clutter_stage_set_color(CLUTTER_STAGE(rc->stage), &stage_color);
756
757
758         clutter_actor_set_size(rc->group, rc->pr->viewport_width, rc->pr->viewport_height);
759         clutter_actor_set_position(rc->group, rc->stereo_off_x, rc->stereo_off_y);
760         
761         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->group),
762                                                 CLUTTER_Y_AXIS,
763                                                 (rc->stereo_mode & PR_STEREO_MIRROR) ? 180 : 0, 
764                                                 rc->pr->viewport_width / 2.0, 0, 0);
765
766         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->group),
767                                                 CLUTTER_X_AXIS,
768                                                 (rc->stereo_mode & PR_STEREO_FLIP) ? 180 : 0,
769                                                 0, rc->pr->viewport_height / 2.0, 0);
770
771         rc_sync_actor(rc);
772         rc_overlay_update_positions(rc);
773 }
774
775 static void rc_scroll(void *renderer, gint x_off, gint y_off)
776 {
777         DEBUG_0("rc_scroll");
778         RendererClutter *rc = (RendererClutter *)renderer;
779
780         rc_sync_actor(rc);
781 }
782
783 static void rc_stereo_set(void *renderer, gint stereo_mode)
784 {
785         RendererClutter *rc = (RendererClutter *)renderer;
786
787         rc->stereo_mode = stereo_mode;
788 }
789
790 static void rc_free(void *renderer)
791 {
792         RendererClutter *rc = (RendererClutter *)renderer;
793         GtkWidget *widget = gtk_bin_get_child(GTK_BIN(rc->pr));
794
795         rc_remove_pending_updates(rc);
796
797         rc_overlay_free_all(rc);
798         
799         if (widget)
800                 {
801                 /* widget still exists */
802                 clutter_actor_destroy(rc->group);
803                 if (clutter_group_get_n_children(CLUTTER_GROUP(rc->stage)) == 0)
804                         {
805                         DEBUG_1("destroy %p", rc->widget);
806                         /* this was the last user */
807                         gtk_widget_destroy(rc->widget);
808                         }
809                 else
810                         {
811                         DEBUG_1("keep %p", rc->widget);
812                         g_object_unref(G_OBJECT(rc->widget));
813                         }
814                 }
815         g_free(rc);
816 }
817
818 RendererFuncs *renderer_clutter_new(PixbufRenderer *pr)
819 {
820         RendererClutter *rc = g_new0(RendererClutter, 1);
821         
822         rc->pr = pr;
823         
824         rc->f.area_changed = rc_area_changed;
825         rc->f.update_pixbuf = rc_update_pixbuf;
826         rc->f.free = rc_free;
827         rc->f.update_zoom = rc_update_zoom;
828         rc->f.invalidate_region = rc_invalidate_region;
829         rc->f.scroll = rc_scroll;
830         rc->f.update_sizes = rc_update_sizes;
831
832
833         rc->f.overlay_add = rc_overlay_add;
834         rc->f.overlay_set = rc_overlay_set;
835         rc->f.overlay_get = rc_overlay_get;
836         rc->f.overlay_draw = rc_overlay_draw;
837
838         rc->f.stereo_set = rc_stereo_set;
839         
840         
841         rc->stereo_mode = 0;
842         rc->stereo_off_x = 0;
843         rc->stereo_off_y = 0;
844
845         rc->idle_update = 0;
846         rc->pending_updates = NULL;
847
848         rc->widget = gtk_bin_get_child(GTK_BIN(rc->pr));
849         
850         if (rc->widget)
851                 {
852                 if (!GTK_CLUTTER_IS_EMBED(rc->widget))
853                         {
854                         g_free(rc);
855                         DEBUG_0("pixbuf renderer has a child of other type than gtk_clutter_embed");
856                         return NULL;
857                         }
858                 }
859         else 
860                 {
861                 rc->widget = gtk_clutter_embed_new();
862                 gtk_container_add(GTK_CONTAINER(rc->pr), rc->widget);
863                 }
864                 
865         gtk_event_box_set_above_child (GTK_EVENT_BOX(rc->pr), TRUE);
866         rc->stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (rc->widget));
867         
868         rc->group = clutter_group_new();
869         clutter_container_add_actor(CLUTTER_CONTAINER(rc->stage), rc->group);
870         clutter_actor_set_clip_to_allocation(CLUTTER_ACTOR(rc->group), TRUE);
871   
872         rc->texture = clutter_texture_new ();
873         clutter_container_add_actor(CLUTTER_CONTAINER(rc->group), rc->texture);
874         rc_set_shader(clutter_texture_get_cogl_material(CLUTTER_TEXTURE(rc->texture)));
875         g_object_ref(G_OBJECT(rc->widget));
876   
877         gtk_widget_show(rc->widget);
878         return (RendererFuncs *) rc;
879 }
880
881 #endif 
882 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */