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