… | |
… | |
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), ref(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) |
… | |
… | |
114 | |
114 | |
115 | XRenderPictureAttributes pa; |
115 | XRenderPictureAttributes pa; |
116 | pa.repeat = repeat; |
116 | pa.repeat = repeat; |
117 | Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
117 | Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
118 | |
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; |
119 | return pic; |
123 | } |
120 | } |
124 | |
121 | |
125 | void |
122 | void |
126 | rxvt_img::unshare () |
123 | rxvt_img::unshare () |
… | |
… | |
179 | |
176 | |
180 | Display *dpy = s->display->dpy; |
177 | Display *dpy = s->display->dpy; |
181 | int size = max (rh, rv) * 2 + 1; |
178 | int size = max (rh, rv) * 2 + 1; |
182 | double *kernel = (double *)malloc (size * sizeof (double)); |
179 | double *kernel = (double *)malloc (size * sizeof (double)); |
183 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
180 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
184 | 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); |
185 | img->alloc (); |
182 | img->alloc (); |
186 | |
183 | |
187 | Picture src = src_picture (); |
184 | Picture src = src_picture (); |
188 | |
185 | |
189 | XRenderPictureAttributes pa; |
186 | XRenderPictureAttributes pa; |
… | |
… | |
394 | rxvt_img::clone () |
391 | rxvt_img::clone () |
395 | { |
392 | { |
396 | return new rxvt_img (*this); |
393 | return new rxvt_img (*this); |
397 | } |
394 | } |
398 | |
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 | |
399 | rxvt_img * |
417 | rxvt_img * |
400 | rxvt_img::reify () |
418 | rxvt_img::reify () |
401 | { |
419 | { |
402 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
420 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
403 | return clone (); |
421 | return clone (); |
404 | |
422 | |
405 | rxvt_img *img = new rxvt_img (s, format, 0, 0, w, h); |
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); |
406 | img->alloc (); |
430 | img->alloc (); |
407 | |
|
|
408 | Display *dpy = s->display->dpy; |
|
|
409 | |
431 | |
410 | Picture src = src_picture (); |
432 | Picture src = src_picture (); |
411 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
433 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
412 | |
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 |
413 | 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); |
414 | |
443 | |
415 | XRenderFreePicture (dpy, src); |
444 | XRenderFreePicture (dpy, src); |
416 | XRenderFreePicture (dpy, dst); |
445 | XRenderFreePicture (dpy, dst); |
417 | |
446 | |
418 | return img; |
447 | return img; |
419 | } |
448 | } |
… | |
… | |
421 | rxvt_img * |
450 | rxvt_img * |
422 | rxvt_img::sub_rect (int x, int y, int width, int height) |
451 | rxvt_img::sub_rect (int x, int y, int width, int height) |
423 | { |
452 | { |
424 | rxvt_img *img = clone (); |
453 | rxvt_img *img = clone (); |
425 | |
454 | |
426 | //TODO: width > w, must reify |
|
|
427 | |
|
|
428 | img->x += x; |
455 | img->x += x; |
429 | img->y += y; |
456 | img->y += y; |
|
|
457 | |
|
|
458 | if (w != width || h != height) |
|
|
459 | { |
430 | img->w = width; |
460 | img->w = width; |
431 | img->h = height; |
461 | img->h = height; |
|
|
462 | |
|
|
463 | rxvt_img *img2 = img->reify (); |
|
|
464 | delete img; |
|
|
465 | img = img2; |
|
|
466 | } |
432 | |
467 | |
433 | return img; |
468 | return img; |
434 | } |
469 | } |
435 | |
470 | |
436 | rxvt_img * |
471 | rxvt_img * |
437 | 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]) |
438 | { |
473 | { |
439 | 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); |
440 | img->alloc (); |
475 | img->alloc (); |
441 | |
476 | |
442 | Display *dpy = s->display->dpy; |
477 | Display *dpy = s->display->dpy; |
443 | Picture src = src_picture (); |
478 | Picture src = src_picture (); |
444 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
479 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
… | |
… | |
447 | |
482 | |
448 | for (int i = 0; i < 3; ++i) |
483 | for (int i = 0; i < 3; ++i) |
449 | for (int j = 0; j < 3; ++j) |
484 | for (int j = 0; j < 3; ++j) |
450 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
485 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
451 | |
486 | |
|
|
487 | #if 0 |
452 | xfrm.matrix [0][2] += XDoubleToFixed (x);//TODO |
488 | xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO |
453 | xfrm.matrix [0][3] += XDoubleToFixed (y); |
489 | xfrm.matrix [1][2] -= XDoubleToFixed (y); |
|
|
490 | #endif |
454 | |
491 | |
455 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
492 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
456 | XRenderSetPictureTransform (dpy, src, &xfrm); |
493 | XRenderSetPictureTransform (dpy, src, &xfrm); |
457 | 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); |
458 | |
495 | |
… | |
… | |
463 | } |
500 | } |
464 | |
501 | |
465 | rxvt_img * |
502 | rxvt_img * |
466 | rxvt_img::scale (int new_width, int new_height) |
503 | rxvt_img::scale (int new_width, int new_height) |
467 | { |
504 | { |
|
|
505 | if (w == new_width && h == new_height) |
|
|
506 | return clone (); |
|
|
507 | |
468 | double matrix[9] = { |
508 | double matrix[9] = { |
469 | w / (double)new_width, 0, 0, |
509 | w / (double)new_width, 0, 0, |
470 | 0, h / (double)new_height, 0, |
510 | 0, h / (double)new_height, 0, |
471 | 0, 0, 1 |
511 | 0, 0, 1 |
472 | }; |
512 | }; |
473 | |
513 | |
|
|
514 | int old_repeat_mode = repeat; |
|
|
515 | repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems |
|
|
516 | |
474 | 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; |
475 | } |
523 | } |
476 | |
524 | |
477 | rxvt_img * |
525 | rxvt_img * |
478 | 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) |
479 | { |
527 | { |
… | |
… | |
488 | |
536 | |
489 | return transform (new_width, new_height, matrix); |
537 | return transform (new_width, new_height, matrix); |
490 | } |
538 | } |
491 | |
539 | |
492 | rxvt_img * |
540 | rxvt_img * |
493 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
541 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) |
494 | { |
542 | { |
495 | if (new_format == format) |
543 | if (new_format == format) |
496 | return clone (); |
544 | return clone (); |
497 | |
545 | |
498 | rxvt_img *img = new rxvt_img (s, new_format, 0, 0, w, h); |
546 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
499 | img->alloc (); |
547 | img->alloc (); |
500 | |
548 | |
501 | Display *dpy = s->display->dpy; |
549 | Display *dpy = s->display->dpy; |
502 | Picture src = src_picture (); |
550 | Picture src = src_picture (); |
503 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
551 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
… | |
… | |
526 | rxvt_img * |
574 | rxvt_img * |
527 | rxvt_img::blend (rxvt_img *img, double factor) |
575 | rxvt_img::blend (rxvt_img *img, double factor) |
528 | { |
576 | { |
529 | rxvt_img *img2 = clone (); |
577 | rxvt_img *img2 = clone (); |
530 | Display *dpy = s->display->dpy; |
578 | Display *dpy = s->display->dpy; |
531 | Picture src = src_picture (); |
579 | Picture src = img->src_picture (); |
532 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
580 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
533 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
581 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
534 | |
582 | |
535 | XRenderColor mask_c; |
583 | XRenderColor mask_c; |
536 | |
584 | |