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