speed improvements
[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.bgr;                                                                    \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                 DEBUG_0("%s tex upload high prio", get_exec_time());
384                 rc->idle_update = g_idle_add_full(CLUTTER_PRIORITY_REDRAW - 10, rc_area_changed_cb, rc, NULL);
385                 }
386         else
387                 {
388                 /* higher prio than histogram */
389                 DEBUG_0("%s tex upload low prio", get_exec_time());
390
391                 rc->idle_update = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 5, rc_area_changed_cb, rc, NULL);
392                 }
393 }
394
395 static gboolean rc_area_changed_cb(gpointer data)
396 {
397         RendererClutter *rc = (RendererClutter *)data;
398         PixbufRenderer *pr = rc->pr;
399         
400         RendererClutterAreaParam *par = rc->pending_updates->data;
401         
402         gint h = MAX_REGION_AREA / par->w;
403         if (h == 0) h = 1;
404         if (h > par->h) h = par->h;
405         
406         
407         DEBUG_0("%s upload start", get_exec_time());
408         if (pr->pixbuf)
409                 {
410                 CoglHandle texture = clutter_texture_get_cogl_texture(CLUTTER_TEXTURE(rc->texture));
411                 
412                 cogl_texture_set_region(texture,
413                                         par->x + GET_RIGHT_PIXBUF_OFFSET(rc),
414                                         par->y,
415                                         par->x,
416                                         par->y,
417                                         par->w,
418                                         h,
419                                         par->w,
420                                         h,
421                                         gdk_pixbuf_get_has_alpha(pr->pixbuf) ? COGL_PIXEL_FORMAT_BGRA_8888 : COGL_PIXEL_FORMAT_BGR_888,
422                                         gdk_pixbuf_get_rowstride(pr->pixbuf),
423                                         gdk_pixbuf_get_pixels(pr->pixbuf));
424                 }
425         DEBUG_0("%s upload end", get_exec_time());
426         rc_area_clip_add(rc, par->x, par->y, par->w, h);
427
428                 
429         par->y += h;
430         par->h -= h;
431         
432         if (par->h == 0)
433                 {
434                 rc->pending_updates = g_list_remove(rc->pending_updates, par);
435                 g_free(par);
436                 }
437         if (!rc->pending_updates)
438                 {
439                 clutter_actor_queue_redraw(CLUTTER_ACTOR(rc->texture));
440                 rc->idle_update = 0;
441
442                 /* FIXME: find a better place for this */
443                 if (!rc->clut_updated) rc_prepare_post_process_lut(rc);
444
445                 return FALSE;
446                 }
447
448         rc_schedule_texture_upload(rc);
449
450         return FALSE; /* it was rescheduled, possibly with different prio */
451 }
452
453
454 static void rc_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
455 {
456         RendererClutter *rc = (RendererClutter *)renderer;
457         PixbufRenderer *pr = rc->pr;
458         RendererClutterAreaParam *par;
459
460         gint width = gdk_pixbuf_get_width(pr->pixbuf);
461         gint height = gdk_pixbuf_get_height(pr->pixbuf);
462                 
463         if (pr->stereo_data == STEREO_PIXBUF_SBS || pr->stereo_data == STEREO_PIXBUF_CROSS) 
464                         {
465                         width /= 2;
466                         }
467         
468         if (!pr_clip_region(src_x, src_y, src_w, src_h,
469                             GET_RIGHT_PIXBUF_OFFSET(rc), 0, width, height,
470                             &src_x, &src_y, &src_w, &src_h)) return;
471         
472         par = g_new0(RendererClutterAreaParam, 1);
473         par->rc = rc;
474         par->x = src_x - GET_RIGHT_PIXBUF_OFFSET(rc);
475         par->y = src_y;
476         par->w = src_w;
477         par->h = src_h;
478         rc->pending_updates = g_list_append(rc->pending_updates, par);
479         if (!rc->idle_update) 
480                 {
481                 rc_schedule_texture_upload(rc);
482                 }
483 }
484
485 static void rc_remove_pending_updates(RendererClutter *rc)
486 {
487         if (rc->idle_update) g_idle_remove_by_data(rc);
488         rc->idle_update = 0;
489         while (rc->pending_updates)
490                 {
491                 RendererClutterAreaParam *par = rc->pending_updates->data;
492                 rc->pending_updates = g_list_remove(rc->pending_updates, par);
493                 g_free(par);
494                 }
495 }
496
497 static void rc_update_pixbuf(void *renderer, gboolean lazy)
498 {
499         RendererClutter *rc = (RendererClutter *)renderer;
500         PixbufRenderer *pr = rc->pr;
501         
502         DEBUG_0("rc_update_pixbuf");
503
504         rc_remove_pending_updates(rc);
505
506         rc->last_pixbuf_change = g_get_monotonic_time();
507         DEBUG_0("%s change time reset", get_exec_time());
508         
509         if (pr->pixbuf)
510                 {
511                 gint width = gdk_pixbuf_get_width(pr->pixbuf);
512                 gint height = gdk_pixbuf_get_height(pr->pixbuf);
513
514                 DEBUG_0("pixbuf size %d x %d (%d)", width, height, gdk_pixbuf_get_has_alpha(pr->pixbuf) ? 32 : 24);
515                 
516                 gint prev_width, prev_height;
517                 
518                 if (pr->stereo_data == STEREO_PIXBUF_SBS || pr->stereo_data == STEREO_PIXBUF_CROSS) 
519                         {
520                         width /= 2;
521                         }
522
523                 
524                 clutter_texture_get_base_size(CLUTTER_TEXTURE(rc->texture), &prev_width, &prev_height);
525                 
526                 if (width != prev_width || height != prev_height)
527                         {
528                         /* FIXME use CoglMaterial with multiple textures for background, color management, anaglyph, ... */
529                         CoglHandle texture = cogl_texture_new_with_size(width,
530                                                                         height,
531                                                                         COGL_TEXTURE_NO_AUTO_MIPMAP,
532                                                                         gdk_pixbuf_get_has_alpha(pr->pixbuf) ? COGL_PIXEL_FORMAT_BGRA_8888 : COGL_PIXEL_FORMAT_BGR_888);
533
534                         if (texture != COGL_INVALID_HANDLE)
535                                 {
536                                 clutter_texture_set_cogl_texture(CLUTTER_TEXTURE(rc->texture), texture);
537                                 cogl_handle_unref(texture);
538                                 }
539                         }
540                 clutter_actor_set_clip(rc->texture, 0, 0, 0, 0); /* visible area is extended as area_changed events arrive */
541                 if (!lazy)
542                         {
543                         rc_area_changed(renderer, GET_RIGHT_PIXBUF_OFFSET(rc), 0, width, height);
544                         }
545                 }
546
547         rc->clut_updated = FALSE;
548 }
549
550
551
552 static void rc_update_zoom(void *renderer, gboolean lazy)
553 {
554         RendererClutter *rc = (RendererClutter *)renderer;
555
556         DEBUG_0("rc_update_zoom");
557         rc_sync_actor(rc);
558 }
559
560 static void rc_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
561 {
562 }
563
564 static OverlayData *rc_overlay_find(RendererClutter *rc, gint id)
565 {
566         GList *work;
567
568         work = rc->overlay_list;
569         while (work)
570                 {
571                 OverlayData *od = work->data;
572                 work = work->next;
573
574                 if (od->id == id) return od;
575                 }
576
577         return NULL;
578 }
579
580 static void rc_overlay_actor_destroy_cb(ClutterActor *actor, gpointer user_data)
581 {
582         OverlayData *od = user_data;
583         od->actor = NULL;
584 }
585
586 static void rc_overlay_free(RendererClutter *rc, OverlayData *od)
587 {
588         rc->overlay_list = g_list_remove(rc->overlay_list, od);
589
590         if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
591         if (od->actor) clutter_actor_destroy(od->actor);
592         g_free(od);
593 }
594
595 static void rc_overlay_update_position(RendererClutter *rc, OverlayData *od)
596 {
597         gint px, py, pw, ph;
598
599         pw = gdk_pixbuf_get_width(od->pixbuf);
600         ph = gdk_pixbuf_get_height(od->pixbuf);
601         px = od->x;
602         py = od->y;
603
604         if (od->flags & OVL_RELATIVE)
605                 {
606                 if (px < 0) px = rc->pr->viewport_width - pw + px;
607                 if (py < 0) py = rc->pr->viewport_height - ph + py;
608                 }
609         if (od->actor) clutter_actor_set_position(od->actor, px, py);
610 }
611
612 static void rc_overlay_update_positions(RendererClutter *rc)
613 {
614         GList *work;
615
616         work = rc->overlay_list;
617         while (work)
618                 {
619                 OverlayData *od = work->data;
620                 work = work->next;
621
622                 rc_overlay_update_position(rc, od);
623                 }
624 }
625
626 static void rc_overlay_free_all(RendererClutter *rc)
627 {
628         GList *work;
629
630         work = rc->overlay_list;
631         while (work)
632                 {
633                 OverlayData *od = work->data;
634                 work = work->next;
635
636                 rc_overlay_free(rc, od);
637                 }
638 }
639
640 static gint rc_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y, OverlayRendererFlags flags)
641 {
642         RendererClutter *rc = (RendererClutter *)renderer;
643         PixbufRenderer *pr = rc->pr;
644         OverlayData *od;
645         gint id;
646
647         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
648         g_return_val_if_fail(pixbuf != NULL, -1);
649
650         id = 1;
651         while (rc_overlay_find(rc, id)) id++;
652
653         od = g_new0(OverlayData, 1);
654         od->id = id;
655         od->pixbuf = pixbuf;
656         g_object_ref(G_OBJECT(od->pixbuf));
657         od->x = x;
658         od->y = y;
659         od->flags = flags;
660         
661         od->actor = gtk_clutter_texture_new();
662         g_signal_connect (od->actor, "destroy", G_CALLBACK(rc_overlay_actor_destroy_cb), od);
663         
664         gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE (od->actor), pixbuf, NULL);
665         clutter_container_add_actor(CLUTTER_CONTAINER(rc->group), od->actor);
666
667         rc->overlay_list = g_list_append(rc->overlay_list, od);
668         rc_overlay_update_position(rc, od);
669
670         return od->id;
671 }
672
673 static void rc_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
674 {
675         RendererClutter *rc = (RendererClutter *)renderer;
676         PixbufRenderer *pr = rc->pr;
677         OverlayData *od;
678
679         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
680
681         od = rc_overlay_find(rc, id);
682         if (!od) return;
683
684         if (pixbuf)
685                 {
686                 g_object_ref(G_OBJECT(pixbuf));
687                 g_object_unref(G_OBJECT(od->pixbuf));
688                 od->pixbuf = pixbuf;
689
690                 od->x = x;
691                 od->y = y;
692
693                 if (od->actor) gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE(od->actor), pixbuf, NULL);
694                 rc_overlay_update_position(rc, od);
695                 }
696         else
697                 {
698                 rc_overlay_free(rc, od);
699                 }
700 }
701
702 static gboolean rc_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
703 {
704         RendererClutter *rc = (RendererClutter *)renderer;
705
706         PixbufRenderer *pr = rc->pr;
707         OverlayData *od;
708
709         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
710
711         od = rc_overlay_find(rc, id);
712         if (!od) return FALSE;
713
714         if (pixbuf) *pixbuf = od->pixbuf;
715         if (x) *x = od->x;
716         if (y) *y = od->y;
717
718         return TRUE;
719 }
720
721
722 static void rc_update_viewport(void *renderer)
723 {
724         RendererClutter *rc = (RendererClutter *)renderer;
725         ClutterColor stage_color = { 0x00, 0x00, 0x00, 0xff }; 
726
727         rc->stereo_off_x = 0;
728         rc->stereo_off_y = 0;
729         
730         if (rc->stereo_mode & PR_STEREO_RIGHT) 
731                 {
732                 if (rc->stereo_mode & PR_STEREO_HORIZ) 
733                         {
734                         rc->stereo_off_x = rc->pr->viewport_width;
735                         }
736                 else if (rc->stereo_mode & PR_STEREO_VERT) 
737                         {
738                         rc->stereo_off_y = rc->pr->viewport_height;
739                         }
740                 else if (rc->stereo_mode & PR_STEREO_FIXED) 
741                         {
742                         rc->stereo_off_x = rc->pr->stereo_fixed_x_right;
743                         rc->stereo_off_y = rc->pr->stereo_fixed_y_right;
744                         }
745                 }
746         else
747                 {
748                 if (rc->stereo_mode & PR_STEREO_FIXED) 
749                         {
750                         rc->stereo_off_x = rc->pr->stereo_fixed_x_left;
751                         rc->stereo_off_y = rc->pr->stereo_fixed_y_left;
752                         }
753                 }
754         DEBUG_0("rc_update_viewport  scale %d %d", rc->pr->width, rc->pr->height);
755
756         clutter_stage_set_color(CLUTTER_STAGE(rc->stage), &stage_color);
757
758
759         clutter_actor_set_size(rc->group, rc->pr->viewport_width, rc->pr->viewport_height);
760         clutter_actor_set_position(rc->group, rc->stereo_off_x, rc->stereo_off_y);
761         
762         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->group),
763                                                 CLUTTER_Y_AXIS,
764                                                 (rc->stereo_mode & PR_STEREO_MIRROR) ? 180 : 0, 
765                                                 rc->pr->viewport_width / 2.0, 0, 0);
766
767         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->group),
768                                                 CLUTTER_X_AXIS,
769                                                 (rc->stereo_mode & PR_STEREO_FLIP) ? 180 : 0,
770                                                 0, rc->pr->viewport_height / 2.0, 0);
771
772         rc_sync_actor(rc);
773         rc_overlay_update_positions(rc);
774 }
775
776 static void rc_scroll(void *renderer, gint x_off, gint y_off)
777 {
778         DEBUG_0("rc_scroll");
779         RendererClutter *rc = (RendererClutter *)renderer;
780
781         rc_sync_actor(rc);
782 }
783
784 static void rc_stereo_set(void *renderer, gint stereo_mode)
785 {
786         RendererClutter *rc = (RendererClutter *)renderer;
787
788         rc->stereo_mode = stereo_mode;
789 }
790
791 static void rc_free(void *renderer)
792 {
793         RendererClutter *rc = (RendererClutter *)renderer;
794         GtkWidget *widget = gtk_bin_get_child(GTK_BIN(rc->pr));
795
796         rc_remove_pending_updates(rc);
797
798         rc_overlay_free_all(rc);
799         
800         if (widget)
801                 {
802                 /* widget still exists */
803                 clutter_actor_destroy(rc->group);
804                 if (clutter_group_get_n_children(CLUTTER_GROUP(rc->stage)) == 0)
805                         {
806                         DEBUG_1("destroy %p", rc->widget);
807                         /* this was the last user */
808                         gtk_widget_destroy(rc->widget);
809                         }
810                 else
811                         {
812                         DEBUG_1("keep %p", rc->widget);
813                         g_object_unref(G_OBJECT(rc->widget));
814                         }
815                 }
816         g_free(rc);
817 }
818
819 RendererFuncs *renderer_clutter_new(PixbufRenderer *pr)
820 {
821         RendererClutter *rc = g_new0(RendererClutter, 1);
822         
823         rc->pr = pr;
824         
825         rc->f.area_changed = rc_area_changed;
826         rc->f.update_pixbuf = rc_update_pixbuf;
827         rc->f.free = rc_free;
828         rc->f.update_zoom = rc_update_zoom;
829         rc->f.invalidate_region = rc_invalidate_region;
830         rc->f.scroll = rc_scroll;
831         rc->f.update_viewport = rc_update_viewport;
832
833
834         rc->f.overlay_add = rc_overlay_add;
835         rc->f.overlay_set = rc_overlay_set;
836         rc->f.overlay_get = rc_overlay_get;
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: */