f2022f7a312678533c1cb764d76b57f9930bc16a
[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         gint rot_z = 0;
255         gint rot_y = 0;
256
257         clutter_actor_set_anchor_point(CLUTTER_ACTOR(rc->texture), 0, 0);
258
259         DEBUG_0("scale %d %d", rc->pr->width, rc->pr->height);
260         DEBUG_0("pos   %d %d", rc->pr->x_offset, rc->pr->y_offset);
261
262         clutter_actor_set_scale(CLUTTER_ACTOR(rc->texture),
263                                 (gfloat)pr->width / pr->image_width,
264                                 (gfloat)pr->height / pr->image_height);
265
266         switch (pr->orientation)
267                 {
268                 case EXIF_ORIENTATION_TOP_LEFT:
269                         /* 1 - Horizontal (normal)  */
270                         break;
271                 case EXIF_ORIENTATION_TOP_RIGHT:
272                         /* 2 - Mirror horizontal */
273                         rot_y = 180;
274                         anchor_x = pr->width;
275                         break;
276                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
277                         /* 3 - Rotate 180 */
278                         rot_z = 180;
279                         anchor_x = pr->width;
280                         anchor_y = pr->height;
281                         break;
282                 case EXIF_ORIENTATION_BOTTOM_LEFT:
283                         /* 4 - Mirror vertical */
284                         rot_z = 180;
285                         rot_y = 180;
286                         anchor_y = pr->height;
287                         break;
288                 case EXIF_ORIENTATION_LEFT_TOP:
289                         /* 5 - Mirror horizontal and rotate 270 CW */
290                         rot_z = 270;
291                         rot_y = 180;
292                         break;
293                 case EXIF_ORIENTATION_RIGHT_TOP:
294                         /* 6 - Rotate 90 CW */
295                         rot_z = 90;
296                         anchor_x = pr->width;
297                         break;
298                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
299                         /* 7 - Mirror horizontal and rotate 90 CW */
300                         rot_z = 90;
301                         rot_y = 180;
302                         anchor_x = pr->width;
303                         anchor_y = pr->height;
304                         break;
305                 case EXIF_ORIENTATION_LEFT_BOTTOM:
306                         /* 8 - Rotate 270 CW */
307                         rot_z = 270;
308                         anchor_y = pr->height;
309                         break;
310                 default:
311                         /* The other values are out of range */
312                         break;
313                 }
314
315         clutter_actor_set_rotation(     CLUTTER_ACTOR(rc->texture),
316                                                                 CLUTTER_Z_AXIS,
317                                                                 rot_z, 0, 0, 0);
318         clutter_actor_set_rotation(     CLUTTER_ACTOR(rc->texture),
319                                                                 CLUTTER_Y_AXIS,
320                                                                 rot_y, 0, 0, 0);
321
322         clutter_actor_set_position(CLUTTER_ACTOR(rc->texture),
323                                 pr->x_offset - pr->x_scroll + anchor_x,
324                                 pr->y_offset - pr->y_scroll + anchor_y);
325
326 }
327
328
329 static void rc_area_clip_add(RendererClutter *rc, gfloat x, gfloat y, gfloat w, gfloat h)
330 {
331         gfloat x2, y2;
332         gfloat clip_x, clip_y, clip_w, clip_h, clip_x2, clip_y2;
333
334         x2 = x + w;
335         y2 = y + h;
336
337         clutter_actor_get_clip(rc->texture, &clip_x, &clip_y, &clip_w, &clip_h);
338
339         clip_x2 = clip_x + clip_w;
340         clip_y2 = clip_y + clip_h;
341
342         if (clip_x > x) clip_x = x;
343         if (clip_x2 < x2) clip_x2 = x2;
344         if (clip_y > y) clip_y = y;
345         if (clip_y2 < y2) clip_y2 = y2;
346
347         clip_w = clip_x2 - clip_x;
348         clip_h = clip_y2 - clip_y;
349
350         clutter_actor_set_clip(rc->texture, clip_x, clip_y, clip_w, clip_h);
351 }
352
353 #define MAX_REGION_AREA (32768 * 1024)
354
355 static gboolean rc_area_changed_cb(gpointer data);
356
357 static void rc_schedule_texture_upload(RendererClutter *rc)
358 {
359         if (g_get_monotonic_time() - rc->last_pixbuf_change < 50000)
360                 {
361                 /* delay clutter redraw until the texture has some data
362                    set priority between gtk redraw and clutter redraw */
363                 DEBUG_0("%s tex upload high prio", get_exec_time());
364                 rc->idle_update = g_idle_add_full(CLUTTER_PRIORITY_REDRAW - 10, rc_area_changed_cb, rc, NULL);
365                 }
366         else
367                 {
368                 /* higher prio than histogram */
369                 DEBUG_0("%s tex upload low prio", get_exec_time());
370
371                 rc->idle_update = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 5, rc_area_changed_cb, rc, NULL);
372                 }
373 }
374
375 static gboolean rc_area_changed_cb(gpointer data)
376 {
377         RendererClutter *rc = (RendererClutter *)data;
378         PixbufRenderer *pr = rc->pr;
379
380         RendererClutterAreaParam *par = rc->pending_updates->data;
381
382         gint h = MAX_REGION_AREA / par->w;
383         if (h == 0) h = 1;
384         if (h > par->h) h = par->h;
385
386
387         DEBUG_0("%s upload start", get_exec_time());
388         if (pr->pixbuf)
389                 {
390                 CoglHandle texture = clutter_texture_get_cogl_texture(CLUTTER_TEXTURE(rc->texture));
391
392                 cogl_texture_set_region(texture,
393                                         par->x + GET_RIGHT_PIXBUF_OFFSET(rc),
394                                         par->y,
395                                         par->x,
396                                         par->y,
397                                         par->w,
398                                         h,
399                                         par->w,
400                                         h,
401                                         gdk_pixbuf_get_has_alpha(pr->pixbuf) ? COGL_PIXEL_FORMAT_BGRA_8888 : COGL_PIXEL_FORMAT_BGR_888,
402                                         gdk_pixbuf_get_rowstride(pr->pixbuf),
403                                         gdk_pixbuf_get_pixels(pr->pixbuf));
404                 }
405         DEBUG_0("%s upload end", get_exec_time());
406         rc_area_clip_add(rc, par->x, par->y, par->w, h);
407
408
409         par->y += h;
410         par->h -= h;
411
412         if (par->h == 0)
413                 {
414                 rc->pending_updates = g_list_remove(rc->pending_updates, par);
415                 g_free(par);
416                 }
417         if (!rc->pending_updates)
418                 {
419                 clutter_actor_queue_redraw(CLUTTER_ACTOR(rc->texture));
420                 rc->idle_update = 0;
421
422                 /* FIXME: find a better place for this */
423                 if (!rc->clut_updated) rc_prepare_post_process_lut(rc);
424
425                 return FALSE;
426                 }
427
428         rc_schedule_texture_upload(rc);
429
430         return FALSE; /* it was rescheduled, possibly with different prio */
431 }
432
433
434 static void rc_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
435 {
436         RendererClutter *rc = (RendererClutter *)renderer;
437         PixbufRenderer *pr = rc->pr;
438         RendererClutterAreaParam *par;
439
440         gint width = gdk_pixbuf_get_width(pr->pixbuf);
441         gint height = gdk_pixbuf_get_height(pr->pixbuf);
442
443         if (pr->stereo_data == STEREO_PIXBUF_SBS || pr->stereo_data == STEREO_PIXBUF_CROSS)
444                         {
445                         width /= 2;
446                         }
447
448         if (!pr_clip_region(src_x, src_y, src_w, src_h,
449                             GET_RIGHT_PIXBUF_OFFSET(rc), 0, width, height,
450                             &src_x, &src_y, &src_w, &src_h)) return;
451
452         par = g_new0(RendererClutterAreaParam, 1);
453         par->rc = rc;
454         par->x = src_x - GET_RIGHT_PIXBUF_OFFSET(rc);
455         par->y = src_y;
456         par->w = src_w;
457         par->h = src_h;
458         rc->pending_updates = g_list_append(rc->pending_updates, par);
459         if (!rc->idle_update)
460                 {
461                 rc_schedule_texture_upload(rc);
462                 }
463 }
464
465 static void rc_remove_pending_updates(RendererClutter *rc)
466 {
467         if (rc->idle_update) g_idle_remove_by_data(rc);
468         rc->idle_update = 0;
469         while (rc->pending_updates)
470                 {
471                 RendererClutterAreaParam *par = rc->pending_updates->data;
472                 rc->pending_updates = g_list_remove(rc->pending_updates, par);
473                 g_free(par);
474                 }
475 }
476
477 static void rc_update_pixbuf(void *renderer, gboolean lazy)
478 {
479         RendererClutter *rc = (RendererClutter *)renderer;
480         PixbufRenderer *pr = rc->pr;
481
482         DEBUG_0("rc_update_pixbuf");
483
484         rc_remove_pending_updates(rc);
485
486         rc->last_pixbuf_change = g_get_monotonic_time();
487         DEBUG_0("%s change time reset", get_exec_time());
488
489         if (pr->pixbuf)
490                 {
491                 gint width = gdk_pixbuf_get_width(pr->pixbuf);
492                 gint height = gdk_pixbuf_get_height(pr->pixbuf);
493
494                 DEBUG_0("pixbuf size %d x %d (%d)", width, height, gdk_pixbuf_get_has_alpha(pr->pixbuf) ? 32 : 24);
495
496                 gint prev_width, prev_height;
497
498                 if (pr->stereo_data == STEREO_PIXBUF_SBS || pr->stereo_data == STEREO_PIXBUF_CROSS)
499                         {
500                         width /= 2;
501                         }
502
503
504                 clutter_texture_get_base_size(CLUTTER_TEXTURE(rc->texture), &prev_width, &prev_height);
505
506                 if (width != prev_width || height != prev_height)
507                         {
508                         /* FIXME use CoglMaterial with multiple textures for background, color management, anaglyph, ... */
509                         CoglHandle texture = cogl_texture_new_with_size(width,
510                                                                         height,
511                                                                         COGL_TEXTURE_NO_AUTO_MIPMAP | COGL_TEXTURE_NO_SLICING,
512                                                                         gdk_pixbuf_get_has_alpha(pr->pixbuf) ? COGL_PIXEL_FORMAT_BGRA_8888 : COGL_PIXEL_FORMAT_BGR_888);
513
514                         if (texture != COGL_INVALID_HANDLE)
515                                 {
516                                 clutter_texture_set_cogl_texture(CLUTTER_TEXTURE(rc->texture), texture);
517                                 cogl_handle_unref(texture);
518                                 }
519                         }
520                 clutter_actor_set_clip(rc->texture, 0, 0, 0, 0); /* visible area is extended as area_changed events arrive */
521                 if (!lazy)
522                         {
523                         rc_area_changed(renderer, GET_RIGHT_PIXBUF_OFFSET(rc), 0, width, height);
524                         }
525                 }
526
527         rc->clut_updated = FALSE;
528 }
529
530
531
532 static void rc_update_zoom(void *renderer, gboolean lazy)
533 {
534         RendererClutter *rc = (RendererClutter *)renderer;
535
536         DEBUG_0("rc_update_zoom");
537         rc_sync_actor(rc);
538 }
539
540 static void rc_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
541 {
542 }
543
544 static OverlayData *rc_overlay_find(RendererClutter *rc, gint id)
545 {
546         GList *work;
547
548         work = rc->overlay_list;
549         while (work)
550                 {
551                 OverlayData *od = work->data;
552                 work = work->next;
553
554                 if (od->id == id) return od;
555                 }
556
557         return NULL;
558 }
559
560 static void rc_overlay_actor_destroy_cb(ClutterActor *actor, gpointer user_data)
561 {
562         OverlayData *od = user_data;
563         od->actor = NULL;
564 }
565
566 static void rc_overlay_free(RendererClutter *rc, OverlayData *od)
567 {
568         rc->overlay_list = g_list_remove(rc->overlay_list, od);
569
570         if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
571         if (od->actor) clutter_actor_destroy(od->actor);
572         g_free(od);
573 }
574
575 static void rc_overlay_update_position(RendererClutter *rc, OverlayData *od)
576 {
577         gint px, py, pw, ph;
578
579         pw = gdk_pixbuf_get_width(od->pixbuf);
580         ph = gdk_pixbuf_get_height(od->pixbuf);
581         px = od->x;
582         py = od->y;
583
584         if (od->flags & OVL_RELATIVE)
585                 {
586                 if (px < 0) px = rc->pr->viewport_width - pw + px;
587                 if (py < 0) py = rc->pr->viewport_height - ph + py;
588                 }
589         if (od->actor) clutter_actor_set_position(od->actor, px, py);
590 }
591
592 static void rc_overlay_update_positions(RendererClutter *rc)
593 {
594         GList *work;
595
596         work = rc->overlay_list;
597         while (work)
598                 {
599                 OverlayData *od = work->data;
600                 work = work->next;
601
602                 rc_overlay_update_position(rc, od);
603                 }
604 }
605
606 static void rc_overlay_free_all(RendererClutter *rc)
607 {
608         GList *work;
609
610         work = rc->overlay_list;
611         while (work)
612                 {
613                 OverlayData *od = work->data;
614                 work = work->next;
615
616                 rc_overlay_free(rc, od);
617                 }
618 }
619
620 static gint rc_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y, OverlayRendererFlags flags)
621 {
622         RendererClutter *rc = (RendererClutter *)renderer;
623         PixbufRenderer *pr = rc->pr;
624         OverlayData *od;
625         gint id;
626
627         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
628         g_return_val_if_fail(pixbuf != NULL, -1);
629
630         id = 1;
631         while (rc_overlay_find(rc, id)) id++;
632
633         od = g_new0(OverlayData, 1);
634         od->id = id;
635         od->pixbuf = pixbuf;
636         g_object_ref(G_OBJECT(od->pixbuf));
637         od->x = x;
638         od->y = y;
639         od->flags = flags;
640
641         od->actor = gtk_clutter_texture_new();
642         g_signal_connect (od->actor, "destroy", G_CALLBACK(rc_overlay_actor_destroy_cb), od);
643
644         gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE (od->actor), pixbuf, NULL);
645         clutter_container_add_actor(CLUTTER_CONTAINER(rc->group), od->actor);
646
647         rc->overlay_list = g_list_append(rc->overlay_list, od);
648         rc_overlay_update_position(rc, od);
649
650         return od->id;
651 }
652
653 static void rc_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
654 {
655         RendererClutter *rc = (RendererClutter *)renderer;
656         PixbufRenderer *pr = rc->pr;
657         OverlayData *od;
658
659         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
660
661         od = rc_overlay_find(rc, id);
662         if (!od) return;
663
664         if (pixbuf)
665                 {
666                 g_object_ref(G_OBJECT(pixbuf));
667                 g_object_unref(G_OBJECT(od->pixbuf));
668                 od->pixbuf = pixbuf;
669
670                 od->x = x;
671                 od->y = y;
672
673                 if (od->actor) gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE(od->actor), pixbuf, NULL);
674                 rc_overlay_update_position(rc, od);
675                 }
676         else
677                 {
678                 rc_overlay_free(rc, od);
679                 }
680 }
681
682 static gboolean rc_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
683 {
684         RendererClutter *rc = (RendererClutter *)renderer;
685
686         PixbufRenderer *pr = rc->pr;
687         OverlayData *od;
688
689         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
690
691         od = rc_overlay_find(rc, id);
692         if (!od) return FALSE;
693
694         if (pixbuf) *pixbuf = od->pixbuf;
695         if (x) *x = od->x;
696         if (y) *y = od->y;
697
698         return TRUE;
699 }
700
701
702 static void rc_update_viewport(void *renderer)
703 {
704         RendererClutter *rc = (RendererClutter *)renderer;
705         ClutterColor stage_color = { 0x00, 0x00, 0x00, 0xff };
706
707         rc->stereo_off_x = 0;
708         rc->stereo_off_y = 0;
709
710         if (rc->stereo_mode & PR_STEREO_RIGHT)
711                 {
712                 if (rc->stereo_mode & PR_STEREO_HORIZ)
713                         {
714                         rc->stereo_off_x = rc->pr->viewport_width;
715                         }
716                 else if (rc->stereo_mode & PR_STEREO_VERT)
717                         {
718                         rc->stereo_off_y = rc->pr->viewport_height;
719                         }
720                 else if (rc->stereo_mode & PR_STEREO_FIXED)
721                         {
722                         rc->stereo_off_x = rc->pr->stereo_fixed_x_right;
723                         rc->stereo_off_y = rc->pr->stereo_fixed_y_right;
724                         }
725                 }
726         else
727                 {
728                 if (rc->stereo_mode & PR_STEREO_FIXED)
729                         {
730                         rc->stereo_off_x = rc->pr->stereo_fixed_x_left;
731                         rc->stereo_off_y = rc->pr->stereo_fixed_y_left;
732                         }
733                 }
734         DEBUG_0("rc_update_viewport  scale %d %d", rc->pr->width, rc->pr->height);
735
736         clutter_stage_set_color(CLUTTER_STAGE(rc->stage), &stage_color);
737
738
739         clutter_actor_set_size(rc->group, rc->pr->viewport_width, rc->pr->viewport_height);
740         clutter_actor_set_position(rc->group, rc->stereo_off_x, rc->stereo_off_y);
741
742         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->group),
743                                                 CLUTTER_Y_AXIS,
744                                                 (rc->stereo_mode & PR_STEREO_MIRROR) ? 180 : 0,
745                                                 rc->pr->viewport_width / 2.0, 0, 0);
746
747         clutter_actor_set_rotation(CLUTTER_ACTOR(rc->group),
748                                                 CLUTTER_X_AXIS,
749                                                 (rc->stereo_mode & PR_STEREO_FLIP) ? 180 : 0,
750                                                 0, rc->pr->viewport_height / 2.0, 0);
751
752         rc_sync_actor(rc);
753         rc_overlay_update_positions(rc);
754 }
755
756 static void rc_scroll(void *renderer, gint x_off, gint y_off)
757 {
758         DEBUG_0("rc_scroll");
759         RendererClutter *rc = (RendererClutter *)renderer;
760
761         rc_sync_actor(rc);
762 }
763
764 static void rc_stereo_set(void *renderer, gint stereo_mode)
765 {
766         RendererClutter *rc = (RendererClutter *)renderer;
767
768         rc->stereo_mode = stereo_mode;
769 }
770
771 static void rc_free(void *renderer)
772 {
773         RendererClutter *rc = (RendererClutter *)renderer;
774         GtkWidget *widget = gtk_bin_get_child(GTK_BIN(rc->pr));
775
776         rc_remove_pending_updates(rc);
777
778         rc_overlay_free_all(rc);
779
780         if (widget)
781                 {
782                 /* widget still exists */
783                 clutter_actor_destroy(rc->group);
784                 if (clutter_group_get_n_children(CLUTTER_GROUP(rc->stage)) == 0)
785                         {
786                         DEBUG_1("destroy %p", rc->widget);
787                         /* this was the last user */
788                         gtk_widget_destroy(rc->widget);
789                         }
790                 else
791                         {
792                         DEBUG_1("keep %p", rc->widget);
793                         g_object_unref(G_OBJECT(rc->widget));
794                         }
795                 }
796         g_free(rc);
797 }
798
799 RendererFuncs *renderer_clutter_new(PixbufRenderer *pr)
800 {
801         RendererClutter *rc = g_new0(RendererClutter, 1);
802
803         rc->pr = pr;
804
805         rc->f.area_changed = rc_area_changed;
806         rc->f.update_pixbuf = rc_update_pixbuf;
807         rc->f.free = rc_free;
808         rc->f.update_zoom = rc_update_zoom;
809         rc->f.invalidate_region = rc_invalidate_region;
810         rc->f.scroll = rc_scroll;
811         rc->f.update_viewport = rc_update_viewport;
812
813
814         rc->f.overlay_add = rc_overlay_add;
815         rc->f.overlay_set = rc_overlay_set;
816         rc->f.overlay_get = rc_overlay_get;
817
818         rc->f.stereo_set = rc_stereo_set;
819
820
821         rc->stereo_mode = 0;
822         rc->stereo_off_x = 0;
823         rc->stereo_off_y = 0;
824
825         rc->idle_update = 0;
826         rc->pending_updates = NULL;
827
828         rc->widget = gtk_bin_get_child(GTK_BIN(rc->pr));
829
830         if (rc->widget)
831                 {
832                 if (!GTK_CLUTTER_IS_EMBED(rc->widget))
833                         {
834                         g_free(rc);
835                         DEBUG_0("pixbuf renderer has a child of other type than gtk_clutter_embed");
836                         return NULL;
837                         }
838                 }
839         else
840                 {
841                 rc->widget = gtk_clutter_embed_new();
842                 gtk_container_add(GTK_CONTAINER(rc->pr), rc->widget);
843                 }
844
845         gtk_event_box_set_above_child (GTK_EVENT_BOX(rc->pr), TRUE);
846         rc->stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (rc->widget));
847
848         rc->group = clutter_group_new();
849         clutter_container_add_actor(CLUTTER_CONTAINER(rc->stage), rc->group);
850         clutter_actor_set_clip_to_allocation(CLUTTER_ACTOR(rc->group), TRUE);
851
852         rc->texture = clutter_texture_new ();
853         clutter_container_add_actor(CLUTTER_CONTAINER(rc->group), rc->texture);
854         rc_set_shader(clutter_texture_get_cogl_material(CLUTTER_TEXTURE(rc->texture)));
855         g_object_ref(G_OBJECT(rc->widget));
856
857         gtk_widget_show(rc->widget);
858         return (RendererFuncs *) rc;
859 }
860
861 #endif
862 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */