… | |
… | |
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 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height) |
7 | rxvt_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 | |
13 | rxvt_img::rxvt_img (const rxvt_img &img) |
13 | rxvt_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 |
21 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) |
20 | rxvt_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 | |
60 | rxvt_img * |
61 | rxvt_img * |
… | |
… | |
83 | } |
84 | } |
84 | |
85 | |
85 | void |
86 | void |
86 | rxvt_img::destroy () |
87 | rxvt_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 | |
97 | rxvt_img::~rxvt_img () |
98 | rxvt_img::~rxvt_img () |
98 | { |
99 | { |
99 | destroy (); |
100 | destroy (); |
… | |
… | |
101 | |
102 | |
102 | void |
103 | void |
103 | rxvt_img::alloc () |
104 | rxvt_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 | |
|
|
110 | Picture |
|
|
111 | rxvt_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 | |
109 | void |
122 | void |
110 | rxvt_img::unshare () |
123 | rxvt_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 | |
126 | void |
140 | void |
127 | rxvt_img::fill (const rxvt_color &c) |
141 | rxvt_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); |
… | |
… | |
377 | rxvt_img::clone () |
391 | rxvt_img::clone () |
378 | { |
392 | { |
379 | return new rxvt_img (*this); |
393 | return new rxvt_img (*this); |
380 | } |
394 | } |
381 | |
395 | |
|
|
396 | |
|
|
397 | static XRenderPictFormat * |
|
|
398 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
399 | { |
|
|
400 | if (format->direct.alphaMask) |
|
|
401 | return format; // already has alpha |
|
|
402 | |
|
|
403 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
404 | if (format->type == PictTypeDirect) |
|
|
405 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
406 | if (f->direct.alphaMask |
|
|
407 | && f->type == PictTypeDirect |
|
|
408 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
409 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
410 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
|
|
411 | return f; |
|
|
412 | |
|
|
413 | // should be a very good fallback |
|
|
414 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
415 | } |
|
|
416 | |
382 | rxvt_img * |
417 | rxvt_img * |
383 | rxvt_img::reify () |
418 | rxvt_img::reify () |
384 | { |
419 | { |
385 | rxvt_img *img = new rxvt_img (s, format, 0, 0, w, h); |
420 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
|
|
421 | return clone (); |
|
|
422 | |
|
|
423 | Display *dpy = s->display->dpy; |
|
|
424 | |
|
|
425 | bool alpha = !format->direct.alphaMask |
|
|
426 | && (x || y) |
|
|
427 | && repeat == RepeatNone; |
|
|
428 | |
|
|
429 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
386 | img->alloc (); |
430 | img->alloc (); |
387 | |
431 | |
388 | // todo, if x==0 and y==0 and w==real width we could clone |
432 | 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); |
433 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
396 | |
434 | |
|
|
435 | if (alpha) |
|
|
436 | { |
|
|
437 | XRenderColor rc = { 0, 0, 0, 0 }; |
|
|
438 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
|
|
439 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); |
|
|
440 | } |
|
|
441 | else |
397 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
442 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
398 | |
443 | |
399 | XRenderFreePicture (dpy, src); |
444 | XRenderFreePicture (dpy, src); |
400 | XRenderFreePicture (dpy, dst); |
445 | XRenderFreePicture (dpy, dst); |
401 | |
446 | |
402 | return img; |
447 | return img; |
403 | } |
448 | } |
… | |
… | |
407 | { |
452 | { |
408 | rxvt_img *img = clone (); |
453 | rxvt_img *img = clone (); |
409 | |
454 | |
410 | img->x += x; |
455 | img->x += x; |
411 | img->y += y; |
456 | img->y += y; |
|
|
457 | |
|
|
458 | if (w != width || h != height) |
|
|
459 | { |
412 | img->w = width; |
460 | img->w = width; |
413 | img->h = height; |
461 | img->h = height; |
|
|
462 | |
|
|
463 | rxvt_img *img2 = img->reify (); |
|
|
464 | delete img; |
|
|
465 | img = img2; |
|
|
466 | } |
414 | |
467 | |
415 | return img; |
468 | return img; |
416 | } |
469 | } |
417 | |
470 | |
418 | rxvt_img * |
471 | rxvt_img * |
419 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
472 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
420 | { |
473 | { |
421 | rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height); |
474 | rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); |
422 | img->alloc (); |
475 | img->alloc (); |
423 | |
476 | |
424 | Display *dpy = s->display->dpy; |
477 | Display *dpy = s->display->dpy; |
425 | XRenderPictureAttributes pa; |
478 | 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); |
479 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
429 | |
480 | |
430 | XTransform xfrm; |
481 | XTransform xfrm; |
431 | |
482 | |
432 | for (int i = 0; i < 3; ++i) |
483 | for (int i = 0; i < 3; ++i) |
433 | for (int j = 0; j < 3; ++j) |
484 | for (int j = 0; j < 3; ++j) |
434 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
485 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
435 | |
486 | |
|
|
487 | #if 0 |
436 | xfrm.matrix [0][2] += XDoubleToFixed (x);//TODO |
488 | xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO |
437 | xfrm.matrix [0][3] += XDoubleToFixed (y); |
489 | xfrm.matrix [1][2] -= XDoubleToFixed (y); |
|
|
490 | #endif |
438 | |
491 | |
439 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
492 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
440 | XRenderSetPictureTransform (dpy, src, &xfrm); |
493 | XRenderSetPictureTransform (dpy, src, &xfrm); |
441 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
494 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
442 | |
495 | |
… | |
… | |
447 | } |
500 | } |
448 | |
501 | |
449 | rxvt_img * |
502 | rxvt_img * |
450 | rxvt_img::scale (int new_width, int new_height) |
503 | rxvt_img::scale (int new_width, int new_height) |
451 | { |
504 | { |
|
|
505 | if (w == new_width && h == new_height) |
|
|
506 | return clone (); |
|
|
507 | |
452 | double matrix[9] = { |
508 | double matrix[9] = { |
453 | w / (double)new_width, 0, 0, |
509 | w / (double)new_width, 0, 0, |
454 | 0, h / (double)new_height, 0, |
510 | 0, h / (double)new_height, 0, |
455 | 0, 0, 1 |
511 | 0, 0, 1 |
456 | }; |
512 | }; |
457 | |
513 | |
|
|
514 | int old_repeat_mode = repeat; |
|
|
515 | repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems |
|
|
516 | |
458 | return transform (new_width, new_height, matrix); |
517 | rxvt_img *img = transform (new_width, new_height, matrix); |
|
|
518 | |
|
|
519 | repeat = old_repeat_mode; |
|
|
520 | img->repeat = repeat; |
|
|
521 | |
|
|
522 | return img; |
459 | } |
523 | } |
460 | |
524 | |
461 | rxvt_img * |
525 | rxvt_img * |
462 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
526 | rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) |
463 | { |
527 | { |
… | |
… | |
472 | |
536 | |
473 | return transform (new_width, new_height, matrix); |
537 | return transform (new_width, new_height, matrix); |
474 | } |
538 | } |
475 | |
539 | |
476 | rxvt_img * |
540 | rxvt_img * |
477 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
541 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) |
478 | { |
542 | { |
479 | if (new_format == format) |
543 | if (new_format == format) |
480 | return clone (); |
544 | return clone (); |
481 | |
545 | |
482 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h); |
546 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
483 | img->alloc (); |
547 | img->alloc (); |
484 | |
548 | |
485 | Display *dpy = s->display->dpy; |
549 | Display *dpy = s->display->dpy; |
486 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
550 | Picture src = src_picture (); |
487 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
551 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
488 | int op = PictOpSrc; |
552 | int op = PictOpSrc; |
489 | |
553 | |
490 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
554 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
491 | { |
555 | { |
… | |
… | |
510 | rxvt_img * |
574 | rxvt_img * |
511 | rxvt_img::blend (rxvt_img *img, double factor) |
575 | rxvt_img::blend (rxvt_img *img, double factor) |
512 | { |
576 | { |
513 | rxvt_img *img2 = clone (); |
577 | rxvt_img *img2 = clone (); |
514 | Display *dpy = s->display->dpy; |
578 | Display *dpy = s->display->dpy; |
515 | Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
579 | Picture src = img->src_picture (); |
516 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
580 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
517 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
581 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
518 | |
582 | |
519 | XRenderColor mask_c; |
583 | XRenderColor mask_c; |
520 | |
584 | |