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

Comparing rxvt-unicode/src/rxvtimg.C (file contents):
Revision 1.33 by root, Thu Jun 7 08:12:55 2012 UTC vs.
Revision 1.45 by root, Thu Jun 7 17:21:56 2012 UTC

2#include "../config.h" 2#include "../config.h"
3#include "rxvt.h" 3#include "rxvt.h"
4 4
5#if HAVE_IMG 5#if HAVE_IMG
6 6
7rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height) 7rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat)
8: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(RepeatNormal), 8: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat),
9 pm(0), refcnt(0) 9 pm(0), ref(0)
10{ 10{
11} 11}
12 12
13rxvt_img::rxvt_img (const rxvt_img &img) 13rxvt_img::rxvt_img (const rxvt_img &img)
14: s(img.s), x(img.x), y(img.y), w(img.w), h(img.h), format(img.format), repeat(img.repeat), pm(img.pm), refcnt(img.refcnt) 14: s(img.s), x(img.x), y(img.y), w(img.w), h(img.h), format(img.format), repeat(img.repeat), pm(img.pm), ref(img.ref)
15{ 15{
16 if (refcnt)
17 ++*refcnt; 16 ++ref->cnt;
18} 17}
19 18
20#if 0 19#if 0
21rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) 20rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap)
22: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap) 21: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap)
51 root_pm_w, 50 root_pm_w,
52 root_pm_h 51 root_pm_h
53 ); 52 );
54 53
55 img->pm = root_pixmap; 54 img->pm = root_pixmap;
55 img->ref = new pixref (root_pm_w, root_pm_h);
56 img->ref->ours = false;
56 57
57 return img; 58 return img;
58} 59}
59 60
60rxvt_img * 61rxvt_img *
83} 84}
84 85
85void 86void
86rxvt_img::destroy () 87rxvt_img::destroy ()
87{ 88{
88 if (!refcnt || --*refcnt) 89 if (--ref->cnt)
89 return; 90 return;
90 91
91 if (pm) 92 if (pm && ref->ours)
92 XFreePixmap (s->display->dpy, pm); 93 XFreePixmap (s->display->dpy, pm);
93 94
94 delete refcnt; 95 delete ref;
95} 96}
96 97
97rxvt_img::~rxvt_img () 98rxvt_img::~rxvt_img ()
98{ 99{
99 destroy (); 100 destroy ();
101 102
102void 103void
103rxvt_img::alloc () 104rxvt_img::alloc ()
104{ 105{
105 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); 106 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
106 refcnt = new int (1); 107 ref = new pixref (w, h);
108}
109
110Picture
111rxvt_img::src_picture ()
112{
113 Display *dpy = s->display->dpy;
114
115 XRenderPictureAttributes pa;
116 pa.repeat = repeat;
117 Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
118
119 return pic;
107} 120}
108 121
109void 122void
110rxvt_img::unshare () 123rxvt_img::unshare ()
111{ 124{
112 if (refcnt && *refcnt == 1) 125 if (ref->cnt == 1 && ref->ours)
113 return; 126 return;
114 127
128 //TODO: maybe should reify instead
115 Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); 129 Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth);
116 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 130 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
117 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); 131 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0);
118 XFreeGC (s->display->dpy, gc); 132 XFreeGC (s->display->dpy, gc);
119 133
120 destroy (); 134 destroy ();
121 135
122 pm = pm2; 136 pm = pm2;
123 refcnt = new int (1); 137 ref = new pixref (ref->w, ref->h);
124} 138}
125 139
126void 140void
127rxvt_img::fill (const rxvt_color &c) 141rxvt_img::fill (const rxvt_color &c)
128{ 142{
162 176
163 Display *dpy = s->display->dpy; 177 Display *dpy = s->display->dpy;
164 int size = max (rh, rv) * 2 + 1; 178 int size = max (rh, rv) * 2 + 1;
165 double *kernel = (double *)malloc (size * sizeof (double)); 179 double *kernel = (double *)malloc (size * sizeof (double));
166 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 180 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
167 rxvt_img *img = new rxvt_img (s, format, x, y, w, h); 181 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
168 img->alloc (); 182 img->alloc ();
169 183
184 Picture src = src_picture ();
185
170 XRenderPictureAttributes pa; 186 XRenderPictureAttributes pa;
171
172 pa.repeat = RepeatPad; 187 pa.repeat = RepeatPad;
173 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa);
174 Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); 188 Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa);
175 189
176 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); 190 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
177 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); 191 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
178 XFreePixmap (dpy, tmp_pm); 192 XFreePixmap (dpy, tmp_pm);
380} 394}
381 395
382rxvt_img * 396rxvt_img *
383rxvt_img::reify () 397rxvt_img::reify ()
384{ 398{
385 rxvt_img *img = new rxvt_img (s, format, 0, 0, w, h); 399 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
400 return clone ();
401
402 Display *dpy = s->display->dpy;
403
404 bool alpha = !format->direct.alphaMask
405 && (x || y)
406 && repeat == RepeatNone;
407
408 rxvt_img *img = new rxvt_img (s, alpha ? XRenderFindStandardFormat (dpy, PictStandardARGB32) : format, 0, 0, w, h, repeat);
386 img->alloc (); 409 img->alloc ();
387 410
388 // todo, if x==0 and y==0 and w==real width we could clone 411 Picture src = src_picture ();
389 // but that involves an rtt to find pixmap width.
390
391 Display *dpy = s->display->dpy;
392 XRenderPictureAttributes pa;
393 pa.repeat = repeat;
394 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
395 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 412 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
396 413
414 if (alpha)
415 {
416 XRenderColor rc = { 0, 0, 0, 0 };
417 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles
418 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h);
419 }
420 else
397 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); 421 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h);
398 422
399 XRenderFreePicture (dpy, src); 423 XRenderFreePicture (dpy, src);
400 XRenderFreePicture (dpy, dst); 424 XRenderFreePicture (dpy, dst);
401 425
402 return img; 426 return img;
403} 427}
407{ 431{
408 rxvt_img *img = clone (); 432 rxvt_img *img = clone ();
409 433
410 img->x += x; 434 img->x += x;
411 img->y += y; 435 img->y += y;
436
437 if (w != width || h != height)
438 {
412 img->w = width; 439 img->w = width;
413 img->h = height; 440 img->h = height;
441
442 rxvt_img *img2 = img->reify ();
443 delete img;
444 img = img2;
445 }
414 446
415 return img; 447 return img;
416} 448}
417 449
418rxvt_img * 450rxvt_img *
419rxvt_img::transform (int new_width, int new_height, double matrix[9]) 451rxvt_img::transform (int new_width, int new_height, double matrix[9])
420{ 452{
421 rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height); 453 rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat);
422 img->alloc (); 454 img->alloc ();
423 455
424 Display *dpy = s->display->dpy; 456 Display *dpy = s->display->dpy;
425 XRenderPictureAttributes pa; 457 Picture src = src_picture ();
426 pa.repeat = repeat;
427 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
428 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 458 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
429 459
430 XTransform xfrm; 460 XTransform xfrm;
431 461
432 for (int i = 0; i < 3; ++i) 462 for (int i = 0; i < 3; ++i)
433 for (int j = 0; j < 3; ++j) 463 for (int j = 0; j < 3; ++j)
434 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 464 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]);
435 465
466#if 0
436 xfrm.matrix [0][2] += XDoubleToFixed (x);//TODO 467 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO
437 xfrm.matrix [0][3] += XDoubleToFixed (y); 468 xfrm.matrix [1][2] -= XDoubleToFixed (y);
469#endif
438 470
439 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 471 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
440 XRenderSetPictureTransform (dpy, src, &xfrm); 472 XRenderSetPictureTransform (dpy, src, &xfrm);
441 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); 473 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height);
442 474
447} 479}
448 480
449rxvt_img * 481rxvt_img *
450rxvt_img::scale (int new_width, int new_height) 482rxvt_img::scale (int new_width, int new_height)
451{ 483{
484 if (w == new_width && h == new_height)
485 return clone ();
486
452 double matrix[9] = { 487 double matrix[9] = {
453 w / (double)new_width, 0, 0, 488 w / (double)new_width, 0, 0,
454 0, h / (double)new_height, 0, 489 0, h / (double)new_height, 0,
455 0, 0, 1 490 0, 0, 1
456 }; 491 };
457 492
493 int old_repeat_mode = repeat;
494 repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems
495
458 return transform (new_width, new_height, matrix); 496 rxvt_img *img = transform (new_width, new_height, matrix);
497
498 repeat = old_repeat_mode;
499 img->repeat = repeat;
500
501 return img;
459} 502}
460 503
461rxvt_img * 504rxvt_img *
462rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) 505rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi)
463{ 506{
472 515
473 return transform (new_width, new_height, matrix); 516 return transform (new_width, new_height, matrix);
474} 517}
475 518
476rxvt_img * 519rxvt_img *
477rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) 520rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg)
478{ 521{
479 if (new_format == format) 522 if (new_format == format)
480 return clone (); 523 return clone ();
481 524
482 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h); 525 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat);
483 img->alloc (); 526 img->alloc ();
484 527
485 Display *dpy = s->display->dpy; 528 Display *dpy = s->display->dpy;
486 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); 529 Picture src = src_picture ();
487 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); 530 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
488 int op = PictOpSrc; 531 int op = PictOpSrc;
489 532
490 if (format->direct.alphaMask && !new_format->direct.alphaMask) 533 if (format->direct.alphaMask && !new_format->direct.alphaMask)
491 { 534 {
510rxvt_img * 553rxvt_img *
511rxvt_img::blend (rxvt_img *img, double factor) 554rxvt_img::blend (rxvt_img *img, double factor)
512{ 555{
513 rxvt_img *img2 = clone (); 556 rxvt_img *img2 = clone ();
514 Display *dpy = s->display->dpy; 557 Display *dpy = s->display->dpy;
515 Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 558 Picture src = img->src_picture ();
516 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 559 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
517 Picture mask = create_xrender_mask (dpy, img->pm, False); 560 Picture mask = create_xrender_mask (dpy, img->pm, False);
518 561
519 XRenderColor mask_c; 562 XRenderColor mask_c;
520 563

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines