dynamic change of texture upload priority
[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   printf("%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(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         printf("scale %d %d\n", rc->pr->width, rc->pr->height);
243         printf("pos   %d %d\n", 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 renderer_area_clip_add(RendererClutter *rc, gfloat x, gfloat y, gfloat w, gfloat h)
350 {
351         PixbufRenderer *pr = rc->pr;
352         gfloat x2, y2;
353         gfloat clip_x, clip_y, clip_w, clip_h, clip_x2, clip_y2;
354         
355         x2 = x + w;
356         y2 = y + h;
357         
358         clutter_actor_get_clip(rc->texture, &clip_x, &clip_y, &clip_w, &clip_h);
359         
360         clip_x2 = clip_x + clip_w;
361         clip_y2 = clip_y + clip_h;
362         
363         if (clip_x > x) clip_x = x;
364         if (clip_x2 < x2) clip_x2 = x2;
365         if (clip_y > y) clip_y = y;
366         if (clip_y2 < y2) clip_y2 = y2;
367         
368         clip_w = clip_x2 - clip_x;
369         clip_h = clip_y2 - clip_y;
370         
371         printf("clip %f %f %f %f\n", clip_x, clip_y, clip_w, clip_h);
372         clutter_actor_set_clip(rc->texture, clip_x, clip_y, clip_w, clip_h);
373 }
374
375 #define MAX_REGION_AREA (32768 * 1024)
376
377 static gboolean renderer_area_changed_cb(gpointer data);
378
379 static void rc_schedule_texture_upload(RendererClutter *rc)
380 {
381         if (g_get_monotonic_time() - rc->last_pixbuf_change < 50000)
382                 {
383                 /* delay clutter redraw until the texture has some data 
384                    set priority between gtk redraw and clutter redraw */
385                 rc->idle_update = g_idle_add_full(CLUTTER_PRIORITY_REDRAW - 10, renderer_area_changed_cb, rc, NULL);
386                 }
387         else
388                 {
389                 /* higher prio than histogram */
390                 rc->idle_update = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 5, renderer_area_changed_cb, rc, NULL);
391                 }
392 }
393
394 static gboolean renderer_area_changed_cb(gpointer data)
395 {
396         RendererClutter *rc = (RendererClutter *)data;
397         PixbufRenderer *pr = rc->pr;
398         
399         RendererClutterAreaParam *par = rc->pending_updates->data;
400         
401         gint h = MAX_REGION_AREA / par->w;
402         if (h == 0) h = 1;
403         if (h > par->h) h = par->h;
404         
405         
406         printf("renderer_area_changed_cb %d %d %d %d  (%d)\n", par->x, par->y, par->w, h, par->h);
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_RGBA_8888 : COGL_PIXEL_FORMAT_RGB_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         renderer_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 renderer_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 renderer_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 renderer_update_pixbuf(void *renderer, gboolean lazy)
498 {
499         RendererClutter *rc = (RendererClutter *)renderer;
500         PixbufRenderer *pr = rc->pr;
501         
502         renderer_remove_pending_updates(rc);
503         
504         if (pr->pixbuf)
505                 {
506                 gint width = gdk_pixbuf_get_width(pr->pixbuf);
507                 gint height = gdk_pixbuf_get_height(pr->pixbuf);
508                 
509                 gint prev_width, prev_height;
510                 
511                 if (pr->stereo_data == STEREO_PIXBUF_SBS || pr->stereo_data == STEREO_PIXBUF_CROSS) 
512                         {
513                         width /= 2;
514                         }
515
516                 
517                 printf("renderer_update_pixbuf\n");
518                 clutter_texture_get_base_size(CLUTTER_TEXTURE(rc->texture), &prev_width, &prev_height);
519                 printf("change from %d %d to %d %d\n", prev_width, prev_height, width, height);
520                 
521                 if (width != prev_width || height != prev_height)
522                         {
523                         /* FIXME use CoglMaterial with multiple textures for background, color management, anaglyph, ... */
524                         CoglHandle texture = cogl_texture_new_with_size(width,
525                                                                         height,
526                                                                         COGL_TEXTURE_NO_AUTO_MIPMAP,
527                                                                         gdk_pixbuf_get_has_alpha(pr->pixbuf) ? COGL_PIXEL_FORMAT_RGBA_8888 : COGL_PIXEL_FORMAT_RGB_888);
528
529                         if (texture != COGL_INVALID_HANDLE)
530                                 {
531                                 clutter_texture_set_cogl_texture(CLUTTER_TEXTURE(rc->texture), texture);
532                                 cogl_handle_unref(texture);
533                                 }
534                         }
535                 clutter_actor_set_clip(rc->texture, 0, 0, 0, 0); /* visible area is extended as area_changed events arrive */
536                 if (!lazy)
537                         {
538                         renderer_area_changed(renderer, GET_RIGHT_PIXBUF_OFFSET(rc), 0, width, height);
539                         }
540                 }
541
542         rc->clut_updated = FALSE;
543         rc->last_pixbuf_change = g_get_monotonic_time();
544         printf("renderer_update_pixbuf\n");
545         rc_sync_actor(rc);
546 }
547
548
549
550 static void renderer_update_zoom(void *renderer, gboolean lazy)
551 {
552         RendererClutter *rc = (RendererClutter *)renderer;
553         PixbufRenderer *pr = rc->pr;
554
555         printf("renderer_update_zoom\n");
556         rc_sync_actor(rc);
557 }
558
559 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
560 {
561 }
562
563 static OverlayData *rc_overlay_find(RendererClutter *rc, gint id)
564 {
565         GList *work;
566
567         work = rc->overlay_list;
568         while (work)
569                 {
570                 OverlayData *od = work->data;
571                 work = work->next;
572
573                 if (od->id == id) return od;
574                 }
575
576         return NULL;
577 }
578
579 static void rc_overlay_actor_destroy_cb(ClutterActor *actor, gpointer user_data)
580 {
581         OverlayData *od = user_data;
582         od->actor = NULL;
583 }
584
585 static void rc_overlay_free(RendererClutter *rc, OverlayData *od)
586 {
587         rc->overlay_list = g_list_remove(rc->overlay_list, od);
588
589         if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
590         if (od->actor) clutter_actor_destroy(od->actor);
591         g_free(od);
592 }
593
594 static void rc_overlay_update_position(RendererClutter *rc, OverlayData *od)
595 {
596         gint px, py, pw, ph;
597
598         pw = gdk_pixbuf_get_width(od->pixbuf);
599         ph = gdk_pixbuf_get_height(od->pixbuf);
600         px = od->x;
601         py = od->y;
602
603         if (od->flags & OVL_RELATIVE)
604                 {
605                 if (px < 0) px = rc->pr->viewport_width - pw + px;
606                 if (py < 0) py = rc->pr->viewport_height - ph + py;
607                 }
608         if (od->actor) clutter_actor_set_position(od->actor, px, py);
609 }
610
611 static void rc_overlay_update_positions(RendererClutter *rc)
612 {
613         GList *work;
614
615         work = rc->overlay_list;
616         while (work)
617                 {
618                 OverlayData *od = work->data;
619                 work = work->next;
620
621                 rc_overlay_update_position(rc, od);
622                 }
623 }
624
625 static void rc_overlay_free_all(RendererClutter *rc)
626 {
627         GList *work;
628
629         work = rc->overlay_list;
630         while (work)
631                 {
632                 OverlayData *od = work->data;
633                 work = work->next;
634
635                 rc_overlay_free(rc, od);
636                 }
637 }
638
639
640 static void renderer_overlay_draw(void *renderer, gint x, gint y, gint w, gint h)
641 {
642 }
643
644 static gint renderer_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y, OverlayRendererFlags flags)
645 {
646         RendererClutter *rc = (RendererClutter *)renderer;
647         PixbufRenderer *pr = rc->pr;
648         OverlayData *od;
649         gint id;
650
651         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
652         g_return_val_if_fail(pixbuf != NULL, -1);
653
654         id = 1;
655         while (rc_overlay_find(rc, id)) id++;
656
657         od = g_new0(OverlayData, 1);
658         od->id = id;
659         od->pixbuf = pixbuf;
660         g_object_ref(G_OBJECT(od->pixbuf));
661         od->x = x;
662         od->y = y;
663         od->flags = flags;
664         
665         od->actor = gtk_clutter_texture_new();
666         g_signal_connect (od->actor, "destroy", G_CALLBACK(rc_overlay_actor_destroy_cb), od);
667         
668         gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE (od->actor), pixbuf, NULL);
669         clutter_container_add_actor(CLUTTER_CONTAINER(rc->group), od->actor);
670
671         rc->overlay_list = g_list_append(rc->overlay_list, od);
672         rc_overlay_update_position(rc, od);
673
674         return od->id;
675 }
676
677 static void renderer_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
678 {
679         RendererClutter *rc = (RendererClutter *)renderer;
680         PixbufRenderer *pr = rc->pr;
681         OverlayData *od;
682
683         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
684
685         od = rc_overlay_find(rc, id);
686         if (!od) return;
687
688         if (pixbuf)
689                 {
690                 g_object_ref(G_OBJECT(pixbuf));
691                 g_object_unref(G_OBJECT(od->pixbuf));
692                 od->pixbuf = pixbuf;
693
694                 od->x = x;
695                 od->y = y;
696
697                 if (od->actor) gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE(od->actor), pixbuf, NULL);
698                 rc_overlay_update_position(rc, od);
699                 }
700         else
701                 {
702                 rc_overlay_free(rc, od);
703                 }
704 }
705
706 static gboolean renderer_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
707 {
708         RendererClutter *rc = (RendererClutter *)renderer;
709
710         PixbufRenderer *pr = rc->pr;
711         OverlayData *od;
712
713         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
714
715         od = rc_overlay_find(rc, id);
716         if (!od) return FALSE;
717
718         if (pixbuf) *pixbuf = od->pixbuf;
719         if (x) *x = od->x;
720         if (y) *y = od->y;
721
722         return TRUE;
723 }
724
725
726 static void renderer_update_sizes(void *renderer)
727 {
728         RendererClutter *rc = (RendererClutter *)renderer;
729         ClutterColor stage_color = { 0x00, 0x00, 0x00, 0xff }; 
730
731         rc->stereo_off_x = 0;
732         rc->stereo_off_y = 0;
733         
734         if (rc->stereo_mode & PR_STEREO_RIGHT) 
735                 {
736                 if (rc->stereo_mode & PR_STEREO_HORIZ) 
737                         {
738                         rc->stereo_off_x = rc->pr->viewport_width;
739                         }
740                 else if (rc->stereo_mode & PR_STEREO_VERT) 
741                         {
742                         rc->stereo_off_y = rc->pr->viewport_height;
743                         }
744                 else if (rc->stereo_mode & PR_STEREO_FIXED) 
745                         {
746                         rc->stereo_off_x = rc->pr->stereo_fixed_x_right;
747                         rc->stereo_off_y = rc->pr->stereo_fixed_y_right;
748                         }
749                 }
750         else
751                 {
752                 if (rc->stereo_mode & PR_STEREO_FIXED) 
753                         {
754                         rc->stereo_off_x = rc->pr->stereo_fixed_x_left;
755                         rc->stereo_off_y = rc->pr->stereo_fixed_y_left;
756                         }
757                 }
758         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);
759
760         printf("renderer_update_sizes  scale %d %d\n", rc->pr->width, rc->pr->height);
761
762         clutter_stage_set_color(CLUTTER_STAGE(rc->stage), &stage_color);
763
764
765         clutter_actor_set_size(rc->group, rc->pr->viewport_width, rc->pr->viewport_height);
766         clutter_actor_set_position(rc->group, rc->stereo_off_x, rc->stereo_off_y);
767         
768         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->group),
769                                                 CLUTTER_Y_AXIS,
770                                                 (rc->stereo_mode & PR_STEREO_MIRROR) ? 180 : 0, 
771                                                 rc->pr->viewport_width / 2.0, 0, 0);
772
773         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->group),
774                                                 CLUTTER_X_AXIS,
775                                                 (rc->stereo_mode & PR_STEREO_FLIP) ? 180 : 0,
776                                                 0, rc->pr->viewport_height / 2.0, 0);
777
778         rc_sync_actor(rc);
779         rc_overlay_update_positions(rc);
780 }
781
782 static void renderer_scroll(void *renderer, gint x_off, gint y_off)
783 {
784         printf("renderer_scroll\n");
785         RendererClutter *rc = (RendererClutter *)renderer;
786         PixbufRenderer *pr = rc->pr;
787
788         rc_sync_actor(rc);
789 }
790
791 static void renderer_stereo_set(void *renderer, gint stereo_mode)
792 {
793         RendererClutter *rc = (RendererClutter *)renderer;
794
795         rc->stereo_mode = stereo_mode;
796 }
797
798 static void renderer_free(void *renderer)
799 {
800         RendererClutter *rc = (RendererClutter *)renderer;
801         GtkWidget *widget = gtk_bin_get_child(GTK_BIN(rc->pr));
802
803         renderer_remove_pending_updates(rc);
804
805         rc_overlay_free_all(rc);
806         
807         if (widget)
808                 {
809                 /* widget still exists */
810                 clutter_actor_destroy(rc->group);
811                 if (clutter_group_get_n_children(CLUTTER_GROUP(rc->stage)) == 0)
812                         {
813                         printf("destroy %p\n", rc->widget);
814                         /* this was the last user */
815                         gtk_widget_destroy(rc->widget);
816                         }
817                 else
818                         {
819                         printf("keep %p\n", rc->widget);
820                         g_object_unref(G_OBJECT(rc->widget));
821                         }
822                 }
823         g_free(rc);
824 }
825
826 RendererFuncs *renderer_clutter_new(PixbufRenderer *pr)
827 {
828         RendererClutter *rc = g_new0(RendererClutter, 1);
829         
830         rc->pr = pr;
831         
832         rc->f.area_changed = renderer_area_changed;
833         rc->f.update_pixbuf = renderer_update_pixbuf;
834         rc->f.free = renderer_free;
835         rc->f.update_zoom = renderer_update_zoom;
836         rc->f.invalidate_region = renderer_invalidate_region;
837         rc->f.scroll = renderer_scroll;
838         rc->f.update_sizes = renderer_update_sizes;
839
840
841         rc->f.overlay_add = renderer_overlay_add;
842         rc->f.overlay_set = renderer_overlay_set;
843         rc->f.overlay_get = renderer_overlay_get;
844         rc->f.overlay_draw = renderer_overlay_draw;
845
846         rc->f.stereo_set = renderer_stereo_set;
847         
848         
849         rc->stereo_mode = 0;
850         rc->stereo_off_x = 0;
851         rc->stereo_off_y = 0;
852
853         rc->idle_update = 0;
854         rc->pending_updates = NULL;
855
856         rc->widget = gtk_bin_get_child(GTK_BIN(rc->pr));
857         
858         if (rc->widget)
859                 {
860                 if (!GTK_CLUTTER_IS_EMBED(rc->widget))
861                         {
862                         g_free(rc);
863                         DEBUG_0("pixbuf renderer has a child of other type than gtk_clutter_embed");
864                         return NULL;
865                         }
866                 }
867         else 
868                 {
869                 rc->widget = gtk_clutter_embed_new();
870                 gtk_container_add(GTK_CONTAINER(rc->pr), rc->widget);
871                 }
872                 
873         gtk_event_box_set_above_child (GTK_EVENT_BOX(rc->pr), TRUE);
874         rc->stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (rc->widget));
875         
876         rc->group = clutter_group_new();
877         clutter_container_add_actor(CLUTTER_CONTAINER(rc->stage), rc->group);
878         clutter_actor_set_clip_to_allocation(CLUTTER_ACTOR(rc->group), TRUE);
879   
880         rc->texture = clutter_texture_new ();
881         clutter_container_add_actor(CLUTTER_CONTAINER(rc->group), rc->texture);
882         rc_set_shader(clutter_texture_get_cogl_material(rc->texture));
883         g_object_ref(G_OBJECT(rc->widget));
884   
885         gtk_widget_show(rc->widget);
886         return (RendererFuncs *) rc;
887 }
888
889 #endif 
890 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */