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