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