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