ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/background.C
(Generate patch)

Comparing rxvt-unicode/src/background.C (file contents):
Revision 1.235 by sf-exg, Thu Jun 7 09:34:51 2012 UTC vs.
Revision 1.254 by sf-exg, Sun Dec 30 20:28:31 2012 UTC

1/*----------------------------------------------------------------------* 1/*----------------------------------------------------------------------*
2 * File: background.C - former xpm.C 2 * File: background.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * 4 *
5 * All portions of code are copyright by their respective author/s. 5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2005-2008 Marc Lehmann <schmorp@schmorp.de> 6 * Copyright (c) 2005-2008 Marc Lehmann <schmorp@schmorp.de>
7 * Copyright (c) 2007 Sasha Vasko <sasha@aftercode.net> 7 * Copyright (c) 2007 Sasha Vasko <sasha@aftercode.net>
20 * You should have received a copy of the GNU General Public License 20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software 21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *---------------------------------------------------------------------*/ 23 *---------------------------------------------------------------------*/
24 24
25#include <math.h>
26#include "../config.h" /* NECESSARY */ 25#include "../config.h" /* NECESSARY */
27#include "rxvt.h" /* NECESSARY */ 26#include "rxvt.h" /* NECESSARY */
28 27
29#if XRENDER
30# include <X11/extensions/Xrender.h>
31#endif
32
33#ifndef FilterConvolution
34#define FilterConvolution "convolution"
35#endif
36
37#ifndef RepeatPad
38#define RepeatPad True
39#endif
40
41#ifdef HAVE_BG_PIXMAP 28#ifdef HAVE_BG_PIXMAP
42# if XRENDER
43static Picture
44create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha)
45{
46 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
47
48 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
49 XRenderPictureAttributes pa;
50 pa.repeat = True;
51 pa.component_alpha = component_alpha;
52 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa);
53
54 XFreePixmap (dpy, pixmap);
55
56 return mask;
57}
58# endif
59 29
60void 30void
61rxvt_term::bg_destroy () 31rxvt_term::bg_destroy ()
62{ 32{
33# if BG_IMAGE_FROM_ROOT
34 delete root_img;
35 root_img = 0;
36# endif
37
63# if BG_IMAGE_FROM_FILE 38# if BG_IMAGE_FROM_FILE
64 fimage.destroy (); 39 fimage.destroy ();
65# endif 40# endif
66
67 if (bg_pixmap)
68 XFreePixmap (dpy, bg_pixmap);
69} 41}
70 42
71bool 43bool
72rxvt_term::bg_set_position (int x, int y) 44rxvt_term::bg_window_size_sensitive ()
73{ 45{
46# if BG_IMAGE_FROM_ROOT
47 if (root_img)
48 return true;
49# endif
74 50
75 if (target_x != x 51# if BG_IMAGE_FROM_FILE
76 || target_y != y) 52 if (fimage.img)
77 { 53 {
78 target_x = x; 54 if ((fimage.flags & IM_IS_SIZE_SENSITIVE)
79 target_y = y; 55 || fimage.img->w > szHint.width
56 || fimage.img->h > szHint.height)
80 return true; 57 return true;
81 } 58 }
59# endif
60
82 return false; 61 return false;
83} 62}
84 63
85bool 64bool
86rxvt_term::bg_window_size_sensitive () 65rxvt_term::bg_window_position_sensitive ()
87{ 66{
88# if ENABLE_TRANSPARENCY 67# if BG_IMAGE_FROM_ROOT
89 if (bg_flags & BG_IS_TRANSPARENT) 68 if (root_img)
90 return true; 69 return true;
91# endif 70# endif
92 71
93# if BG_IMAGE_FROM_FILE 72# if BG_IMAGE_FROM_FILE
94 if (fimage.flags & IM_IS_SET) 73 if (fimage.img)
95 {
96 if ((fimage.flags & IM_IS_SIZE_SENSITIVE)
97 || fimage.width () > szHint.width
98 || fimage.height () > szHint.height)
99 return true;
100 }
101# endif
102
103 return false;
104}
105
106bool
107rxvt_term::bg_window_position_sensitive ()
108{
109# if ENABLE_TRANSPARENCY
110 if (bg_flags & BG_IS_TRANSPARENT)
111 return true;
112# endif
113
114# if BG_IMAGE_FROM_FILE
115 if (fimage.flags & IM_IS_SET)
116 { 74 {
117 if (fimage.flags & IM_ROOT_ALIGN) 75 if (fimage.flags & IM_ROOT_ALIGN)
118 return true; 76 return true;
119 } 77 }
120# endif 78# endif
130 return lerp (0, window_size - image_size, align); 88 return lerp (0, window_size - image_size, align);
131 else if (align > 100) 89 else if (align > 100)
132 return lerp (window_size - image_size, window_size, align - 100); 90 return lerp (window_size - image_size, window_size, align - 100);
133 else 91 else
134 return lerp (-image_size, 0, align + 100); 92 return lerp (-image_size, 0, align + 100);
135}
136
137static inline int
138make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size)
139{
140 int src_pos = 0;
141 dst_pos = pos;
142 dst_size = size;
143 if (pos < 0)
144 {
145 src_pos = -pos;
146 dst_pos = 0;
147 dst_size += pos;
148 }
149
150 min_it (dst_size, target_size - dst_pos);
151 return src_pos;
152} 93}
153 94
154static void 95static void
155parse_style (const char *style, int &x, int &y, unsigned int &w, unsigned int &h, uint8_t &flags) 96parse_style (const char *style, int &x, int &y, unsigned int &w, unsigned int &h, uint8_t &flags)
156{ 97{
313} 254}
314 255
315void 256void
316rxvt_term::get_image_geometry (rxvt_image &image, int &w, int &h, int &x, int &y) 257rxvt_term::get_image_geometry (rxvt_image &image, int &w, int &h, int &x, int &y)
317{ 258{
318 int image_width = image.width (); 259 int image_width = image.img->w;
319 int image_height = image.height (); 260 int image_height = image.img->h;
320 int target_width = szHint.width; 261 int parent_width = szHint.width;
321 int target_height = szHint.height; 262 int parent_height = szHint.height;
322 int h_scale = min (image.h_scale, 32767 * 100 / target_width); 263 int h_scale = min (image.h_scale, 32767 * 100 / parent_width);
323 int v_scale = min (image.v_scale, 32767 * 100 / target_height); 264 int v_scale = min (image.v_scale, 32767 * 100 / parent_height);
324 265
325 w = h_scale * target_width / 100; 266 w = h_scale * parent_width / 100;
326 h = v_scale * target_height / 100; 267 h = v_scale * parent_height / 100;
327 268
328 if (image.flags & IM_KEEP_ASPECT) 269 if (image.flags & IM_KEEP_ASPECT)
329 { 270 {
330 float scale = (float)w / image_width; 271 float scale = (float)w / image_width;
331 min_it (scale, (float)h / image_height); 272 min_it (scale, (float)h / image_height);
336 if (!w) w = image_width; 277 if (!w) w = image_width;
337 if (!h) h = image_height; 278 if (!h) h = image_height;
338 279
339 if (image.flags & IM_ROOT_ALIGN) 280 if (image.flags & IM_ROOT_ALIGN)
340 { 281 {
341 x = -target_x; 282 x = -parent_x;
342 y = -target_y; 283 y = -parent_y;
343 } 284 }
344 else 285 else
345 { 286 {
346 x = make_align_position (image.h_align, target_width, w); 287 x = make_align_position (image.h_align, parent_width, w);
347 y = make_align_position (image.v_align, target_height, h); 288 y = make_align_position (image.v_align, parent_height, h);
348 }
349}
350
351# if HAVE_PIXBUF
352bool
353rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc,
354 int src_x, int src_y, int dst_x, int dst_y,
355 unsigned int width, unsigned int height, bool argb)
356{
357 XImage *ximage;
358 char *line;
359 int width_r, width_g, width_b, width_a;
360 int sh_r, sh_g, sh_b, sh_a;
361 uint32_t red_mask, green_mask, blue_mask, alpha_mask;
362 int rowstride;
363 int channels;
364 unsigned char *row;
365
366 if (visual->c_class != TrueColor)
367 return false;
368
369 if (argb)
370 { 289 }
371 red_mask = 0xff << 16;
372 green_mask = 0xff << 8;
373 blue_mask = 0xff;
374 alpha_mask = 0xff << 24;
375 }
376 else
377 {
378 red_mask = visual->red_mask;
379 green_mask = visual->green_mask;
380 blue_mask = visual->blue_mask;
381#if XRENDER
382 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
383 if (format)
384 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
385 else
386#endif
387 alpha_mask = 0;
388 }
389
390 width_r = ecb_popcount32 (red_mask);
391 width_g = ecb_popcount32 (green_mask);
392 width_b = ecb_popcount32 (blue_mask);
393 width_a = ecb_popcount32 (alpha_mask);
394
395 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
396 return false;
397
398 sh_r = ecb_ctz32 (red_mask);
399 sh_g = ecb_ctz32 (green_mask);
400 sh_b = ecb_ctz32 (blue_mask);
401 sh_a = ecb_ctz32 (alpha_mask);
402
403 if (width > 32767 || height > 32767)
404 return false;
405
406 ximage = XCreateImage (dpy, visual, argb ? 32 : depth, ZPixmap, 0, 0,
407 width, height, 32, 0);
408 if (!ximage)
409 return false;
410
411 if (height > INT_MAX / ximage->bytes_per_line
412 || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
413 {
414 XDestroyImage (ximage);
415 return false;
416 }
417
418 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
419
420 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
421 channels = gdk_pixbuf_get_n_channels (pixbuf);
422 row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
423 line = ximage->data;
424
425 rgba c (0, 0, 0);
426
427 if (channels == 4 && alpha_mask == 0)
428 {
429 pix_colors[Color_bg].get (c);
430 c.r >>= 8;
431 c.g >>= 8;
432 c.b >>= 8;
433 }
434
435 for (int y = 0; y < height; y++)
436 {
437 for (int x = 0; x < width; x++)
438 {
439 unsigned char *pixel = row + x * channels;
440 uint32_t value;
441 unsigned char r, g, b, a;
442
443 if (channels == 4)
444 {
445 a = pixel[3];
446 r = (pixel[0] * a + c.r * (0xff - a)) / 0xff;
447 g = (pixel[1] * a + c.g * (0xff - a)) / 0xff;
448 b = (pixel[2] * a + c.b * (0xff - a)) / 0xff;
449 }
450 else
451 {
452 a = 0xff;
453 r = pixel[0];
454 g = pixel[1];
455 b = pixel[2];
456 }
457
458 value = ((r >> (8 - width_r)) << sh_r)
459 | ((g >> (8 - width_g)) << sh_g)
460 | ((b >> (8 - width_b)) << sh_b)
461 | ((a >> (8 - width_a)) << sh_a);
462
463 if (ximage->bits_per_pixel == 32)
464 ((uint32_t *)line)[x] = value;
465 else
466 XPutPixel (ximage, x, y, value);
467 }
468
469 row += rowstride;
470 line += ximage->bytes_per_line;
471 }
472
473 XPutImage (dpy, pixmap, gc, ximage, 0, 0, dst_x, dst_y, width, height);
474 XDestroyImage (ximage);
475 return true;
476} 290}
477 291
478bool 292bool
479rxvt_term::render_image (rxvt_image &image) 293rxvt_term::render_image (rxvt_image &image)
480{ 294{
481 GdkPixbuf *pixbuf = image.pixbuf;
482 if (!pixbuf)
483 return false;
484
485 bool need_blend = bg_flags & BG_IS_VALID;
486
487 if (need_blend
488 && !(display->flags & DISPLAY_HAS_RENDER))
489 return false;
490
491 GdkPixbuf *result;
492
493 int image_width = gdk_pixbuf_get_width (pixbuf);
494 int image_height = gdk_pixbuf_get_height (pixbuf);
495
496 int target_width = szHint.width; 295 int parent_width = szHint.width;
497 int target_height = szHint.height; 296 int parent_height = szHint.height;
498 int new_pmap_width = target_width;
499 int new_pmap_height = target_height;
500 297
501 int x = 0; 298 int x = 0;
502 int y = 0; 299 int y = 0;
503 int w = 0; 300 int w = 0;
504 int h = 0; 301 int h = 0;
505 302
506 get_image_geometry (image, w, h, x, y); 303 get_image_geometry (image, w, h, x, y);
507 304
508 if (!(image.flags & IM_ROOT_ALIGN) 305 if (!(image.flags & IM_ROOT_ALIGN)
509 && (x >= target_width 306 && (x >= parent_width
510 || y >= target_height 307 || y >= parent_height
511 || x + w <= 0 308 || x + w <= 0
512 || y + h <= 0)) 309 || y + h <= 0))
513 return false; 310 return false;
514 311
515 result = pixbuf; 312 rxvt_img *img = image.img->scale (w, h);
516 313
517 if (w != image_width 314 if (image.flags & IM_TILE)
518 || h != image_height) 315 img->repeat_mode (RepeatNormal);
519 {
520 result = gdk_pixbuf_scale_simple (pixbuf,
521 w, h,
522 GDK_INTERP_BILINEAR);
523 }
524
525 if (!result)
526 return false;
527
528 bool ret = false;
529
530 XGCValues gcv;
531 GC gc;
532 Pixmap tmp_pixmap;
533
534 image_width = gdk_pixbuf_get_width (result);
535 image_height = gdk_pixbuf_get_height (result);
536
537 if (need_blend)
538 tmp_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, 32);
539 else 316 else
540 { 317 img->repeat_mode (RepeatNone);
541 if (image.flags & IM_TILE) 318 img->sub_rect (-x, -y, parent_width, parent_height)->replace (img);
542 {
543 new_pmap_width = min (image_width, target_width);
544 new_pmap_height = min (image_height, target_height);
545 }
546 319
547 if (bg_pixmap == None 320 if (bg_img)
548 || bg_pmap_width != new_pmap_width 321 img->draw (bg_img, PictOpOver, image.alpha * 1. / 0xffff);
549 || bg_pmap_height != new_pmap_height)
550 {
551 if (bg_pixmap)
552 XFreePixmap (dpy, bg_pixmap);
553 bg_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, depth);
554 bg_pmap_width = new_pmap_width;
555 bg_pmap_height = new_pmap_height;
556 }
557 322
558 tmp_pixmap = bg_pixmap;
559 }
560
561 gcv.foreground = pix_colors[Color_bg];
562 gc = XCreateGC (dpy, tmp_pixmap, GCForeground, &gcv);
563
564 if (gc)
565 {
566 if (image.flags & IM_TILE)
567 {
568 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, need_blend ? 32 : depth);
569 pixbuf_to_pixmap (result, tile, gc,
570 0, 0,
571 0, 0,
572 image_width, image_height, need_blend);
573
574 gcv.tile = tile;
575 gcv.fill_style = FillTiled;
576 gcv.ts_x_origin = x;
577 gcv.ts_y_origin = y;
578 XChangeGC (dpy, gc, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
579
580 XFillRectangle (dpy, tmp_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
581 XFreePixmap (dpy, tile);
582 }
583 else
584 {
585 int src_x, src_y, dst_x, dst_y;
586 int dst_width, dst_height;
587
588 src_x = make_clip_rectangle (x, image_width , new_pmap_width , dst_x, dst_width );
589 src_y = make_clip_rectangle (y, image_height, new_pmap_height, dst_y, dst_height);
590
591 if (dst_x > 0 || dst_y > 0
592 || dst_x + dst_width < new_pmap_width
593 || dst_y + dst_height < new_pmap_height)
594 XFillRectangle (dpy, tmp_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
595
596 if (dst_x < new_pmap_width && dst_y < new_pmap_height)
597 pixbuf_to_pixmap (result, tmp_pixmap, gc,
598 src_x, src_y,
599 dst_x, dst_y,
600 dst_width, dst_height, need_blend);
601 }
602
603 if (image.need_blur ())
604 blur_pixmap (tmp_pixmap, new_pmap_width, new_pmap_height, need_blend, image.h_blurRadius, image.v_blurRadius);
605 if (image.need_tint ())
606 tint_pixmap (tmp_pixmap, new_pmap_width, new_pmap_height, need_blend, image.tint, image.tint_set, image.shade);
607
608#if XRENDER
609 if (need_blend)
610 {
611 XRenderPictFormat *argb_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
612 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); 323 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
324 img->convert_format (format, pix_colors [Color_bg])->replace (img);
613 325
614 Picture src = XRenderCreatePicture (dpy, tmp_pixmap, argb_format, 0, 0); 326 delete bg_img;
327 bg_img = img;
615 328
616 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0);
617
618 Picture mask = create_xrender_mask (dpy, vt, False, False);
619
620 XRenderColor mask_c;
621
622 mask_c.alpha = image.alpha;
623 mask_c.red =
624 mask_c.green =
625 mask_c.blue = 0;
626 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
627
628 XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, target_width, target_height);
629
630 XRenderFreePicture (dpy, src);
631 XRenderFreePicture (dpy, dst);
632 XRenderFreePicture (dpy, mask);
633 }
634#endif
635
636 XFreeGC (dpy, gc);
637
638 ret = true;
639 }
640
641 if (result != pixbuf)
642 g_object_unref (result);
643
644 if (need_blend)
645 XFreePixmap (dpy, tmp_pixmap);
646
647 return ret; 329 return true;
648} 330}
649# endif /* HAVE_PIXBUF */
650 331
651rxvt_image::rxvt_image () 332rxvt_image::rxvt_image ()
652{ 333{
653 alpha = 0xffff; 334 alpha = 0xffff;
654 flags = 0; 335 flags = 0;
655 h_scale = 336 h_scale =
656 v_scale = defaultScale; 337 v_scale = defaultScale;
657 h_align = 338 h_align =
658 v_align = defaultAlign; 339 v_align = defaultAlign;
659 340
660# if HAVE_PIXBUF 341 img = 0;
661 pixbuf = 0;
662# endif
663} 342}
664 343
665bool 344void
666rxvt_image::set_file_geometry (const char *file) 345rxvt_image::set_file_geometry (rxvt_screen *s, const char *file)
667{ 346{
668 if (!file || !*file) 347 if (!file || !*file)
669 return false; 348 return;
670 349
671 const char *p = strchr (file, ';'); 350 const char *p = strchr (file, ';');
672 351
673 if (p) 352 if (p)
674 { 353 {
677 memcpy (f, file, len); 356 memcpy (f, file, len);
678 f[len] = '\0'; 357 f[len] = '\0';
679 file = f; 358 file = f;
680 } 359 }
681 360
682 bool ret = set_file (file); 361 set_file (s, file);
683 alpha = 0x8000; 362 alpha = 0x8000;
684 if (ret)
685 set_geometry (p ? p + 1 : ""); 363 set_geometry (p ? p + 1 : "");
686 return ret;
687} 364}
688 365
689bool 366void
690rxvt_image::set_file (const char *file) 367rxvt_image::set_file (rxvt_screen *s, const char *file)
691{ 368{
692 bool ret = false; 369 rxvt_img *img2 = rxvt_img::new_from_file (s, file);
693 370 delete img;
694# if HAVE_PIXBUF 371 img = img2;
695 GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL);
696 if (image)
697 {
698 if (pixbuf)
699 g_object_unref (pixbuf);
700 pixbuf = image;
701 ret = true;
702 }
703# endif
704
705 if (ret)
706 flags |= IM_IS_SET;
707
708 return ret;
709} 372}
710 373
711# endif /* BG_IMAGE_FROM_FILE */ 374# endif /* BG_IMAGE_FROM_FILE */
712 375
713bool 376bool
771 } 434 }
772 435
773 return false; 436 return false;
774} 437}
775 438
776#if XRENDER 439# if BG_IMAGE_FROM_ROOT
777static void
778get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
779{
780 double sigma = radius / 2.0;
781 double scale = sqrt (2.0 * M_PI) * sigma;
782 double sum = 0.0;
783
784 for (int i = 0; i < width; i++)
785 {
786 double x = i - width / 2;
787 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
788 sum += kernel[i];
789 }
790
791 params[0] = XDoubleToFixed (width);
792 params[1] = XDoubleToFixed (1);
793
794 for (int i = 0; i < width; i++)
795 params[i+2] = XDoubleToFixed (kernel[i] / sum);
796}
797#endif
798
799bool
800rxvt_term::blur_pixmap (Pixmap pixmap, int width, int height, bool argb, int h_blurRadius, int v_blurRadius)
801{
802 bool ret = false;
803#if XRENDER
804 if (!(display->flags & DISPLAY_HAS_RENDER_CONV))
805 return false;
806
807 int size = max (h_blurRadius, v_blurRadius) * 2 + 1;
808 double *kernel = (double *)malloc (size * sizeof (double));
809 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
810
811 XRenderPictureAttributes pa;
812 XRenderPictFormat *format = argb ? XRenderFindStandardFormat (dpy, PictStandardARGB32)
813 : XRenderFindVisualFormat (dpy, visual);
814
815 pa.repeat = RepeatPad;
816 Picture src = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa);
817 Pixmap tmp = XCreatePixmap (dpy, pixmap, width, height, depth);
818 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa);
819 XFreePixmap (dpy, tmp);
820
821 if (kernel && params)
822 {
823 size = h_blurRadius * 2 + 1;
824 get_gaussian_kernel (h_blurRadius, size, kernel, params);
825
826 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
827 XRenderComposite (dpy,
828 PictOpSrc,
829 src,
830 None,
831 dst,
832 0, 0,
833 0, 0,
834 0, 0,
835 width, height);
836
837 ::swap (src, dst);
838
839 size = v_blurRadius * 2 + 1;
840 get_gaussian_kernel (v_blurRadius, size, kernel, params);
841 ::swap (params[0], params[1]);
842
843 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
844 XRenderComposite (dpy,
845 PictOpSrc,
846 src,
847 None,
848 dst,
849 0, 0,
850 0, 0,
851 0, 0,
852 width, height);
853
854 ret = true;
855 }
856
857 free (kernel);
858 free (params);
859 XRenderFreePicture (dpy, src);
860 XRenderFreePicture (dpy, dst);
861#endif
862 return ret;
863}
864
865bool
866rxvt_term::tint_pixmap (Pixmap pixmap, int width, int height, bool argb, rxvt_color &tint, bool tint_set, int shade)
867{
868 bool ret = false;
869
870 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC);
871
872 if (tint_set)
873 tint.get (c);
874
875 if (shade == 100
876 && (c.r <= 0x00ff || c.r >= 0xff00)
877 && (c.g <= 0x00ff || c.g >= 0xff00)
878 && (c.b <= 0x00ff || c.b >= 0xff00))
879 {
880 XGCValues gcv;
881 GC gc;
882
883 /* In this case we can tint image server-side getting significant
884 * performance improvements, as we eliminate XImage transfer
885 */
886 gcv.foreground = Pixel (tint);
887 gcv.function = GXand;
888 gcv.fill_style = FillSolid;
889 gc = XCreateGC (dpy, pixmap, GCFillStyle | GCForeground | GCFunction, &gcv);
890 if (gc)
891 {
892 XFillRectangle (dpy, pixmap, gc, 0, 0, width, height);
893 ret = true;
894 XFreeGC (dpy, gc);
895 }
896 }
897# if XRENDER
898 else if (display->flags & DISPLAY_HAS_RENDER)
899 {
900 if (shade <= 100)
901 {
902 c.r = c.r * shade / 100;
903 c.g = c.g * shade / 100;
904 c.b = c.b * shade / 100;
905 }
906 else
907 {
908 c.r = c.r * (200 - shade) / 100;
909 c.g = c.g * (200 - shade) / 100;
910 c.b = c.b * (200 - shade) / 100;
911 }
912
913 XRenderPictFormat *format = argb ? XRenderFindStandardFormat (dpy, PictStandardARGB32)
914 : XRenderFindVisualFormat (dpy, visual);
915
916 Picture back_pic = XRenderCreatePicture (dpy, pixmap, format, 0, 0);
917
918 Picture overlay_pic = create_xrender_mask (dpy, pixmap, True, False);
919
920 Picture mask_pic = create_xrender_mask (dpy, pixmap, True, True);
921
922 XRenderColor mask_c;
923
924 mask_c.alpha = 0xffff;
925 mask_c.red =
926 mask_c.green =
927 mask_c.blue = 0;
928 XRenderFillRectangle (dpy, PictOpSrc, overlay_pic, &mask_c, 0, 0, 1, 1);
929
930 mask_c.alpha = 0;
931 mask_c.red = 0xffff - c.r;
932 mask_c.green = 0xffff - c.g;
933 mask_c.blue = 0xffff - c.b;
934 XRenderFillRectangle (dpy, PictOpSrc, mask_pic, &mask_c, 0, 0, 1, 1);
935
936 XRenderComposite (dpy, PictOpOver, overlay_pic, mask_pic, back_pic, 0, 0, 0, 0, 0, 0, width, height);
937
938 if (shade > 100)
939 {
940 mask_c.alpha = 0;
941 mask_c.red =
942 mask_c.green =
943 mask_c.blue = 0xffff * (shade - 100) / 100;
944 XRenderFillRectangle (dpy, PictOpSrc, overlay_pic, &mask_c, 0, 0, 1, 1);
945
946 XRenderComposite (dpy, PictOpOver, overlay_pic, None, back_pic, 0, 0, 0, 0, 0, 0, width, height);
947 }
948
949 ret = true;
950
951 XRenderFreePicture (dpy, mask_pic);
952 XRenderFreePicture (dpy, overlay_pic);
953 XRenderFreePicture (dpy, back_pic);
954 }
955# endif
956
957 return ret;
958}
959
960# if ENABLE_TRANSPARENCY
961/* 440/*
962 * Builds a pixmap of the same size as the terminal window that contains 441 * Builds a pixmap of the same size as the terminal window that contains
963 * the tiled portion of the root pixmap that is supposed to be covered by 442 * the tiled portion of the root pixmap that is supposed to be covered by
964 * our window. 443 * our window.
965 */ 444 */
966bool 445bool
967rxvt_term::render_root_image () 446rxvt_term::render_root_image ()
968{ 447{
969 bool ret = false;
970
971 /* root dimensions may change from call to call - but Display structure should 448 /* root dimensions may change from call to call - but Display structure should
972 * be always up-to-date, so let's use it : 449 * be always up-to-date, so let's use it :
973 */ 450 */
974 int screen = display->screen; 451 int screen = display->screen;
975 int root_depth = DefaultDepth (dpy, screen);
976 int root_width = DisplayWidth (dpy, screen); 452 int root_width = DisplayWidth (dpy, screen);
977 int root_height = DisplayHeight (dpy, screen); 453 int root_height = DisplayHeight (dpy, screen);
978 unsigned int root_pmap_width, root_pmap_height;
979 int window_width = szHint.width; 454 int parent_width = szHint.width;
980 int window_height = szHint.height; 455 int parent_height = szHint.height;
981 int sx, sy; 456 int sx, sy;
982 XGCValues gcv;
983 GC gc;
984 457
985 sx = target_x; 458 sx = parent_x;
986 sy = target_y; 459 sy = parent_y;
987 460
988 /* check if we are outside of the visible part of the virtual screen : */ 461 /* check if we are outside of the visible part of the virtual screen : */
989 if (sx + window_width <= 0 || sy + window_height <= 0 462 if (sx + parent_width <= 0 || sy + parent_height <= 0
990 || sx >= root_width || sy >= root_height) 463 || sx >= root_width || sy >= root_height)
991 return 0; 464 return 0;
992 465
993 // validate root pixmap and get its size
994 if (root_pixmap != None)
995 {
996 Window wdummy;
997 int idummy;
998 unsigned int udummy;
999
1000 allowedxerror = -1;
1001
1002 if (!XGetGeometry (dpy, root_pixmap, &wdummy, &idummy, &idummy, &root_pmap_width, &root_pmap_height, &udummy, &udummy))
1003 root_pixmap = None;
1004
1005 allowedxerror = 0;
1006 }
1007
1008 Pixmap recoded_root_pmap = root_pixmap;
1009
1010 if (root_pixmap != None && root_depth != depth)
1011 {
1012#if XRENDER
1013 if (display->flags & DISPLAY_HAS_RENDER)
1014 {
1015 recoded_root_pmap = XCreatePixmap (dpy, vt, root_pmap_width, root_pmap_height, depth);
1016
1017 XRenderPictFormat *src_format = XRenderFindVisualFormat (dpy, DefaultVisual (dpy, screen));
1018 Picture src = XRenderCreatePicture (dpy, root_pixmap, src_format, 0, 0);
1019
1020 XRenderPictFormat *dst_format = XRenderFindVisualFormat (dpy, visual);
1021 Picture dst = XRenderCreatePicture (dpy, recoded_root_pmap, dst_format, 0, 0);
1022
1023 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, root_pmap_width, root_pmap_height);
1024
1025 XRenderFreePicture (dpy, src);
1026 XRenderFreePicture (dpy, dst);
1027 }
1028 else
1029#endif
1030 recoded_root_pmap = None;
1031 }
1032
1033 if (recoded_root_pmap == None)
1034 return 0;
1035
1036 if (bg_pixmap == None
1037 || bg_pmap_width != window_width
1038 || bg_pmap_height != window_height)
1039 {
1040 if (bg_pixmap)
1041 XFreePixmap (dpy, bg_pixmap);
1042 bg_pixmap = XCreatePixmap (dpy, vt, window_width, window_height, depth);
1043 bg_pmap_width = window_width;
1044 bg_pmap_height = window_height;
1045 }
1046
1047 /* straightforward pixmap copy */
1048 while (sx < 0) sx += root_pmap_width; 466 while (sx < 0) sx += root_img->w;
1049 while (sy < 0) sy += root_pmap_height; 467 while (sy < 0) sy += root_img->h;
1050 468
1051 gcv.tile = recoded_root_pmap; 469 rxvt_img *img = root_img->sub_rect (sx, sy, parent_width, parent_height);
1052 gcv.fill_style = FillTiled;
1053 gcv.ts_x_origin = -sx;
1054 gcv.ts_y_origin = -sy;
1055 gc = XCreateGC (dpy, vt, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
1056 470
1057 if (gc) 471 if (root_effects.need_blur ())
1058 { 472 img->blur (root_effects.h_blurRadius, root_effects.v_blurRadius)->replace (img);
1059 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height);
1060 ret = true;
1061 bool need_blur = root_effects.need_blur ();
1062 bool need_tint = root_effects.need_tint ();
1063 473
1064 if (need_blur) 474 if (root_effects.need_tint ())
1065 {
1066 if (blur_pixmap (bg_pixmap, window_width, window_height, false,
1067 root_effects.h_blurRadius, root_effects.v_blurRadius))
1068 need_blur = false;
1069 }
1070 if (need_tint)
1071 {
1072 if (tint_pixmap (bg_pixmap, window_width, window_height, false,
1073 root_effects.tint, root_effects.tint_set, root_effects.shade))
1074 need_tint = false;
1075 }
1076 if (need_tint)
1077 {
1078 XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap);
1079 if (ximage)
1080 {
1081 /* our own client-side tinting */
1082 tint_ximage (ximage, root_effects.tint, root_effects.tint_set, root_effects.shade); 475 tint_image (img, root_effects.tint, root_effects.tint_set, root_effects.shade);
1083 476
1084 XPutImage (dpy, bg_pixmap, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height); 477 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
1085 XDestroyImage (ximage); 478 img->convert_format (format, pix_colors [Color_bg])->replace (img);
1086 }
1087 }
1088 479
1089 XFreeGC (dpy, gc); 480 delete bg_img;
1090 } 481 bg_img = img;
1091 482
1092 if (recoded_root_pmap != root_pixmap)
1093 XFreePixmap (dpy, recoded_root_pmap);
1094
1095 return ret; 483 return true;
1096} 484}
1097 485
1098void 486void
1099rxvt_term::bg_set_root_pixmap () 487rxvt_term::bg_set_root_pixmap ()
1100{ 488{
1101 Pixmap new_root_pixmap = display->get_pixmap_property (xa[XA_XROOTPMAP_ID]); 489 delete root_img;
1102 if (new_root_pixmap == None) 490 root_img = rxvt_img::new_from_root (this);
1103 new_root_pixmap = display->get_pixmap_property (xa[XA_ESETROOT_PMAP_ID]);
1104
1105 root_pixmap = new_root_pixmap;
1106} 491}
1107# endif /* ENABLE_TRANSPARENCY */ 492# endif /* BG_IMAGE_FROM_ROOT */
1108 493
1109void 494void
1110rxvt_term::bg_render () 495rxvt_term::bg_render ()
1111{ 496{
1112 if (bg_flags & BG_INHIBIT_RENDER) 497 if (bg_flags & BG_INHIBIT_RENDER)
1113 return; 498 return;
1114 499
1115 bg_invalidate (); 500 delete bg_img;
1116# if ENABLE_TRANSPARENCY 501 bg_img = 0;
1117 if (bg_flags & BG_IS_TRANSPARENT) 502 bg_flags = 0;
1118 { 503
1119 /* we need to re-generate transparency pixmap in that case ! */ 504 if (!mapped)
505 return;
506
507# if BG_IMAGE_FROM_ROOT
508 if (root_img)
1120 if (render_root_image ()) 509 if (render_root_image ())
1121 bg_flags |= BG_IS_VALID; 510 bg_flags |= BG_IS_TRANSPARENT;
1122 }
1123# endif 511# endif
1124 512
1125# if BG_IMAGE_FROM_FILE 513# if BG_IMAGE_FROM_FILE
1126 if (fimage.flags & IM_IS_SET) 514 if (fimage.img)
1127 {
1128 if (render_image (fimage)) 515 render_image (fimage);
1129 bg_flags |= BG_IS_VALID;
1130 }
1131# endif 516# endif
1132
1133 if (!(bg_flags & BG_IS_VALID))
1134 {
1135 if (bg_pixmap != None)
1136 {
1137 XFreePixmap (dpy, bg_pixmap);
1138 bg_pixmap = None;
1139 }
1140 }
1141 517
1142 scr_recolour (false); 518 scr_recolour (false);
1143 bg_flags |= BG_NEEDS_REFRESH; 519 bg_flags |= BG_NEEDS_REFRESH;
1144 520
1145 bg_valid_since = ev::now (); 521 bg_valid_since = ev::now ();
1146} 522}
1147 523
1148void 524void
1149rxvt_term::bg_init () 525rxvt_term::bg_init ()
1150{ 526{
527#if BG_IMAGE_FROM_ROOT
528 if (option (Opt_transparent))
529 {
530 if (rs [Rs_blurradius])
531 root_effects.set_blur (rs [Rs_blurradius]);
532
533 if (ISSET_PIXCOLOR (Color_tint))
534 root_effects.set_tint (pix_colors_focused [Color_tint]);
535
536 if (rs [Rs_shade])
537 root_effects.set_shade (rs [Rs_shade]);
538
539 bg_set_root_pixmap ();
540 XSelectInput (dpy, display->root, PropertyChangeMask);
541 rootwin_ev.start (display, display->root);
542 }
543#endif
544
1151#if BG_IMAGE_FROM_FILE 545#if BG_IMAGE_FROM_FILE
1152 if (rs[Rs_backgroundPixmap]) 546 if (rs[Rs_backgroundPixmap])
1153 { 547 {
1154 if (fimage.set_file_geometry (rs[Rs_backgroundPixmap]) 548 fimage.set_file_geometry (this, rs[Rs_backgroundPixmap]);
1155 && !bg_window_position_sensitive ()) 549 if (!bg_window_position_sensitive ())
1156 update_background (); 550 update_background ();
1157 } 551 }
1158#endif 552#endif
1159} 553}
1160 554
1161/* based on code from aterm-0.4.2 */
1162
1163static inline void
1164fill_lut (uint32_t *lookup, uint32_t mask, int sh, unsigned short low, unsigned short high)
1165{
1166 for (int i = 0; i <= mask >> sh; i++)
1167 {
1168 uint32_t tmp;
1169 tmp = i * high;
1170 tmp += (mask >> sh) * low;
1171 lookup[i] = (tmp / 0xffff) << sh;
1172 }
1173}
1174
1175void 555void
1176rxvt_term::tint_ximage (XImage *ximage, rxvt_color &tint, bool tint_set, int shade) 556rxvt_term::tint_image (rxvt_img *img, rxvt_color &tint, bool tint_set, int shade)
1177{ 557{
1178 unsigned int size_r, size_g, size_b;
1179 int sh_r, sh_g, sh_b;
1180 uint32_t mask_r, mask_g, mask_b;
1181 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b;
1182 unsigned short low;
1183 int host_byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
1184
1185 if (visual->c_class != TrueColor || ximage->format != ZPixmap) return;
1186
1187 /* for convenience */
1188 mask_r = visual->red_mask;
1189 mask_g = visual->green_mask;
1190 mask_b = visual->blue_mask;
1191
1192 /* boring lookup table pre-initialization */
1193 sh_r = ecb_ctz32 (mask_r);
1194 sh_g = ecb_ctz32 (mask_g);
1195 sh_b = ecb_ctz32 (mask_b);
1196
1197 size_r = mask_r >> sh_r;
1198 size_g = mask_g >> sh_g;
1199 size_b = mask_b >> sh_b;
1200
1201 if (size_r++ > 255 || size_g++ > 255 || size_b++ > 255)
1202 return;
1203
1204 lookup = (uint32_t *)malloc (sizeof (uint32_t) * (size_r + size_g + size_b));
1205 lookup_r = lookup;
1206 lookup_g = lookup + size_r;
1207 lookup_b = lookup + size_r + size_g;
1208
1209 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC); 558 rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC);
1210 559
1211 if (tint_set) 560 if (tint_set)
1212 tint.get (c); 561 tint.get (c);
1213 562
1214 /* prepare limits for color transformation (each channel is handled separately) */
1215 if (shade > 100) 563 if (shade > 100)
1216 { 564 {
1217 c.r = c.r * (200 - shade) / 100; 565 c.r = c.r * (200 - shade) / 100;
1218 c.g = c.g * (200 - shade) / 100; 566 c.g = c.g * (200 - shade) / 100;
1219 c.b = c.b * (200 - shade) / 100; 567 c.b = c.b * (200 - shade) / 100;
1220
1221 low = 0xffff * (shade - 100) / 100;
1222 } 568 }
1223 else 569 else
1224 { 570 {
1225 c.r = c.r * shade / 100; 571 c.r = c.r * shade / 100;
1226 c.g = c.g * shade / 100; 572 c.g = c.g * shade / 100;
1227 c.b = c.b * shade / 100; 573 c.b = c.b * shade / 100;
1228
1229 low = 0;
1230 }
1231
1232 /* fill our lookup tables */
1233 fill_lut (lookup_r, mask_r, sh_r, low, c.r);
1234 fill_lut (lookup_g, mask_g, sh_g, low, c.g);
1235 fill_lut (lookup_b, mask_b, sh_b, low, c.b);
1236
1237 /* apply table to input image (replacing colors by newly calculated ones) */
1238 if (ximage->bits_per_pixel == 32
1239 && ximage->byte_order == host_byte_order)
1240 { 574 }
1241 char *line = ximage->data;
1242 575
1243 for (int y = 0; y < ximage->height; y++) 576 img->contrast (c.r, c.g, c.b, c.a);
1244 { 577
1245 uint32_t *p = (uint32_t *)line; 578 if (shade > 100)
1246 for (int x = 0; x < ximage->width; x++)
1247 {
1248 *p = lookup_r[(*p & mask_r) >> sh_r] |
1249 lookup_g[(*p & mask_g) >> sh_g] |
1250 lookup_b[(*p & mask_b) >> sh_b];
1251 p++;
1252 }
1253 line += ximage->bytes_per_line;
1254 }
1255 } 579 {
1256 else 580 c.a = 0xffff;
581 c.r =
582 c.g =
583 c.b = 0xffff * (shade - 100) / 100;
584 img->brightness (c.r, c.g, c.b, c.a);
1257 { 585 }
1258 for (int y = 0; y < ximage->height; y++)
1259 for (int x = 0; x < ximage->width; x++)
1260 {
1261 unsigned long pixel = XGetPixel (ximage, x, y);
1262 pixel = lookup_r[(pixel & mask_r) >> sh_r] |
1263 lookup_g[(pixel & mask_g) >> sh_g] |
1264 lookup_b[(pixel & mask_b) >> sh_b];
1265 XPutPixel (ximage, x, y, pixel);
1266 }
1267 }
1268
1269 free (lookup);
1270} 586}
1271 587
1272#endif /* HAVE_BG_PIXMAP */ 588#endif /* HAVE_BG_PIXMAP */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines