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.29 by root, Wed Jun 6 20:55:36 2012 UTC vs.
Revision 1.34 by root, Thu Jun 7 08:36:09 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
7#define float_to_component(d) ((d) * 65535.99)
8
9rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) 7rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height)
10: s(screen), w(width), h(height), format(format), shared(false) 8: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(RepeatNormal),
9 pm(0), ref(0)
11{ 10{
12 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
13} 11}
14 12
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), ref(img.ref)
15{
16 ++ref->cnt;
17}
18
19#if 0
15rxvt_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)
16: s(screen), pm(pixmap), w(width), h(height), format(format), shared(false) 21: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap)
17{ 22{
18} 23}
24#endif
19 25
20rxvt_img * 26rxvt_img *
21rxvt_img::new_from_root (rxvt_screen *s) 27rxvt_img::new_from_root (rxvt_screen *s)
22{ 28{
23 Display *dpy = s->display->dpy; 29 Display *dpy = s->display->dpy;
37 return 0; 43 return 0;
38 44
39 rxvt_img *img = new rxvt_img ( 45 rxvt_img *img = new rxvt_img (
40 s, 46 s,
41 XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), 47 XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)),
48 0,
49 0,
42 root_pm_w, 50 root_pm_w,
43 root_pm_h, 51 root_pm_h
44 root_pixmap
45 ); 52 );
46 53
47 img->shared = true; 54 img->pm = root_pixmap;
55 img->ref = new pixref (root_pm_w, root_pm_h);
56 img->ref->ours = false;
48 57
49 return img; 58 return img;
50} 59}
51 60
52rxvt_img * 61rxvt_img *
59 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); 68 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message);
60 69
61 rxvt_img *img = new rxvt_img ( 70 rxvt_img *img = new rxvt_img (
62 s, 71 s,
63 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), 72 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24),
73 0,
74 0,
64 gdk_pixbuf_get_width (pb), 75 gdk_pixbuf_get_width (pb),
65 gdk_pixbuf_get_height (pb) 76 gdk_pixbuf_get_height (pb)
66 ); 77 );
67 78 img->alloc ();
68 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); 79 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0);
69 80
70 g_object_unref (pb); 81 g_object_unref (pb);
71 82
72 return img; 83 return img;
73} 84}
74 85
86void
87rxvt_img::destroy ()
88{
89 if (--ref->cnt)
90 return;
91
92 if (pm && ref->ours)
93 XFreePixmap (s->display->dpy, pm);
94
95 delete ref;
96}
97
75rxvt_img::~rxvt_img () 98rxvt_img::~rxvt_img ()
76{ 99{
77 if (!shared) 100 destroy ();
78 XFreePixmap (s->display->dpy, pm); 101}
102
103void
104rxvt_img::alloc ()
105{
106 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
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 XRectangle clip = { -x, -y, min (w, ref->w), min (h, ref->h) };
120 XRenderSetPictureClipRectangles (dpy, pic, 0, 0, &clip, 1);
121
122 return pic;
79} 123}
80 124
81void 125void
82rxvt_img::unshare () 126rxvt_img::unshare ()
83{ 127{
84 if (!shared) 128 if (ref->cnt == 1 && ref->ours)
85 return; 129 return;
86 130
87 rxvt_img *img = clone (); 131 //TODO: maybe should reify instead
132 Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth);
133 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
134 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0);
135 XFreeGC (s->display->dpy, gc);
88 136
89 ::swap (pm , img->pm); 137 destroy ();
90 ::swap (shared, img->shared);
91 138
92 delete img; 139 pm = pm2;
140 ref = new pixref (ref->w, ref->h);
93} 141}
94 142
95void 143void
96rxvt_img::fill (const rxvt_color &c) 144rxvt_img::fill (const rxvt_color &c)
97{ 145{
131 179
132 Display *dpy = s->display->dpy; 180 Display *dpy = s->display->dpy;
133 int size = max (rh, rv) * 2 + 1; 181 int size = max (rh, rv) * 2 + 1;
134 double *kernel = (double *)malloc (size * sizeof (double)); 182 double *kernel = (double *)malloc (size * sizeof (double));
135 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 183 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
136 rxvt_img *img = new rxvt_img (s, format, w, h); 184 rxvt_img *img = new rxvt_img (s, format, x, y, w, h);
185 img->alloc ();
186
187 Picture src = src_picture ();
137 188
138 XRenderPictureAttributes pa; 189 XRenderPictureAttributes pa;
139
140 pa.repeat = RepeatPad; 190 pa.repeat = RepeatPad;
141 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa);
142 Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); 191 Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa);
143 192
144 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); 193 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
145 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); 194 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
146 XFreePixmap (dpy, tmp_pm); 195 XFreePixmap (dpy, tmp_pm);
200 249
201 return mask; 250 return mask;
202} 251}
203 252
204void 253void
205rxvt_img::brightness (double r, double g, double b, double a) 254rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a)
206{ 255{
207 Display *dpy = s->display->dpy; 256 Display *dpy = s->display->dpy;
208 Picture src = create_xrender_mask (dpy, pm, True); 257 Picture src = create_xrender_mask (dpy, pm, True);
209 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 258 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
210 259
211 XRenderColor mask_c; 260 XRenderColor mask_c;
212 mask_c.red = float_to_component (r); 261 mask_c.red = r;
213 mask_c.green = float_to_component (g); 262 mask_c.green = g;
214 mask_c.blue = float_to_component (b); 263 mask_c.blue = b;
215 mask_c.alpha = float_to_component (a); 264 mask_c.alpha = a;
216 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 265 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
217 266
218 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 267 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
219 268
220 XRenderFreePicture (dpy, src); 269 XRenderFreePicture (dpy, src);
221 XRenderFreePicture (dpy, dst); 270 XRenderFreePicture (dpy, dst);
222} 271}
223 272
224void 273void
225rxvt_img::contrast (double r, double g, double b, double a) 274rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a)
226{ 275{
227 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) 276 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
228 return; 277 return;
229 278
230 Display *dpy = s->display->dpy; 279 Display *dpy = s->display->dpy;
231 Picture src = create_xrender_mask (dpy, pm, True); 280 Picture src = create_xrender_mask (dpy, pm, True);
232 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 281 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
233 282
234 XRenderColor mask_c; 283 XRenderColor mask_c;
235 mask_c.red = float_to_component (r); 284 mask_c.red = r;
236 mask_c.green = float_to_component (g); 285 mask_c.green = g;
237 mask_c.blue = float_to_component (b); 286 mask_c.blue = b;
238 mask_c.alpha = float_to_component (a); 287 mask_c.alpha = a;
239 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 288 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
240 289
241 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 290 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
242 291
243 XRenderFreePicture (dpy, src); 292 XRenderFreePicture (dpy, src);
342} 391}
343 392
344rxvt_img * 393rxvt_img *
345rxvt_img::clone () 394rxvt_img::clone ()
346{ 395{
396 return new rxvt_img (*this);
397}
398
399rxvt_img *
400rxvt_img::reify ()
401{
347 rxvt_img *img = new rxvt_img (s, format, w, h); 402 rxvt_img *img = new rxvt_img (s, format, 0, 0, w, h);
403 img->alloc ();
348 404
349 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 405 // todo, if x==0 and y==0 and w==real width we could clone
350 XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); 406 // but that involves an rtt to find pixmap width.
351 XFreeGC (s->display->dpy, gc);
352 407
353 return img;
354}
355
356rxvt_img *
357rxvt_img::sub_rect (int x, int y, int width, int height, int repeat)
358{
359 rxvt_img *img = new rxvt_img (s, format, width, height);
360
361 Display *dpy = s->display->dpy; 408 Display *dpy = s->display->dpy;
362 XRenderPictureAttributes pa; 409
363 pa.repeat = repeat; 410 Picture src = src_picture ();
364 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
365 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 411 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
366 412
367 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); 413 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h);
368 414
369 XRenderFreePicture (dpy, src);
370 XRenderFreePicture (dpy, dst); 415 XRenderFreePicture (dpy, src);
416 XRenderFreePicture (dpy, dst);
371 417
372 return img; 418 return img;
373} 419}
374 420
375rxvt_img * 421rxvt_img *
422rxvt_img::sub_rect (int x, int y, int width, int height)
423{
424 rxvt_img *img = clone ();
425
426 //TODO: width > w, must reify
427
428 img->x += x;
429 img->y += y;
430 img->w = width;
431 img->h = height;
432
433 return img;
434}
435
436rxvt_img *
376rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat) 437rxvt_img::transform (int new_width, int new_height, double matrix[9])
377{ 438{
378 rxvt_img *img = new rxvt_img (s, format, new_width, new_height); 439 rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height);
440 img->alloc ();
379 441
380 Display *dpy = s->display->dpy; 442 Display *dpy = s->display->dpy;
381 XRenderPictureAttributes pa; 443 Picture src = src_picture ();
382 pa.repeat = repeat;
383 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
384 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 444 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
385 445
386 XTransform xfrm; 446 XTransform xfrm;
387 447
388 for (int i = 0; i < 3; ++i) 448 for (int i = 0; i < 3; ++i)
389 for (int j = 0; j < 3; ++j) 449 for (int j = 0; j < 3; ++j)
390 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 450 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]);
451
452 xfrm.matrix [0][2] += XDoubleToFixed (x);//TODO
453 xfrm.matrix [0][3] += XDoubleToFixed (y);
391 454
392 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 455 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
393 XRenderSetPictureTransform (dpy, src, &xfrm); 456 XRenderSetPictureTransform (dpy, src, &xfrm);
394 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); 457 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height);
395 458
410 473
411 return transform (new_width, new_height, matrix); 474 return transform (new_width, new_height, matrix);
412} 475}
413 476
414rxvt_img * 477rxvt_img *
415rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat) 478rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi)
416{ 479{
417 double s = sin (phi); 480 double s = sin (phi);
418 double c = cos (phi); 481 double c = cos (phi);
419 482
420 double matrix[9] = { 483 double matrix[9] = {
421 c, -s, -c * x + s * y + x, 484 c, -s, -c * x + s * y + x,
422 s, c, -s * x - c * y + y, 485 s, c, -s * x - c * y + y,
423 0, 0, 1 486 0, 0, 1
424 }; 487 };
425 488
426 return transform (new_width, new_height, matrix, repeat); 489 return transform (new_width, new_height, matrix);
427} 490}
428 491
429rxvt_img * 492rxvt_img *
430rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) 493rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg)
431{ 494{
495 if (new_format == format)
496 return clone ();
497
432 rxvt_img *img = new rxvt_img (s, new_format, w, h); 498 rxvt_img *img = new rxvt_img (s, new_format, 0, 0, w, h);
499 img->alloc ();
433 500
434 Display *dpy = s->display->dpy; 501 Display *dpy = s->display->dpy;
435 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); 502 Picture src = src_picture ();
436 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); 503 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
437 int op = PictOpSrc; 504 int op = PictOpSrc;
438 505
439 if (format->direct.alphaMask && !new_format->direct.alphaMask) 506 if (format->direct.alphaMask && !new_format->direct.alphaMask)
440 { 507 {
459rxvt_img * 526rxvt_img *
460rxvt_img::blend (rxvt_img *img, double factor) 527rxvt_img::blend (rxvt_img *img, double factor)
461{ 528{
462 rxvt_img *img2 = clone (); 529 rxvt_img *img2 = clone ();
463 Display *dpy = s->display->dpy; 530 Display *dpy = s->display->dpy;
464 Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 531 Picture src = src_picture ();
465 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 532 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
466 Picture mask = create_xrender_mask (dpy, img->pm, False); 533 Picture mask = create_xrender_mask (dpy, img->pm, False);
467 534
468 XRenderColor mask_c; 535 XRenderColor mask_c;
469 536

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines