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