… | |
… | |
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 width, int height) |
7 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height) |
8 | : s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false) |
8 | : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(RepeatNormal), |
|
|
9 | pm(0), ref(0) |
9 | { |
10 | { |
10 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
|
|
11 | } |
11 | } |
12 | |
12 | |
|
|
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), ref(img.ref) |
|
|
15 | { |
|
|
16 | ++ref->cnt; |
|
|
17 | } |
|
|
18 | |
|
|
19 | #if 0 |
13 | 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) |
14 | : 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) |
15 | { |
22 | { |
16 | } |
23 | } |
|
|
24 | #endif |
17 | |
25 | |
18 | rxvt_img * |
26 | rxvt_img * |
19 | rxvt_img::new_from_root (rxvt_screen *s) |
27 | rxvt_img::new_from_root (rxvt_screen *s) |
20 | { |
28 | { |
21 | Display *dpy = s->display->dpy; |
29 | Display *dpy = s->display->dpy; |
… | |
… | |
35 | return 0; |
43 | return 0; |
36 | |
44 | |
37 | rxvt_img *img = new rxvt_img ( |
45 | rxvt_img *img = new rxvt_img ( |
38 | s, |
46 | s, |
39 | XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
47 | XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), |
|
|
48 | 0, |
|
|
49 | 0, |
40 | root_pm_w, |
50 | root_pm_w, |
41 | root_pm_h, |
51 | root_pm_h |
42 | root_pixmap |
|
|
43 | ); |
52 | ); |
44 | |
53 | |
45 | img->shared = true; |
54 | img->pm = root_pixmap; |
|
|
55 | img->ref = new pixref (root_pm_w, root_pm_h); |
|
|
56 | img->ref->ours = false; |
46 | |
57 | |
47 | return img; |
58 | return img; |
48 | } |
59 | } |
49 | |
60 | |
50 | rxvt_img * |
61 | rxvt_img * |
… | |
… | |
57 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
68 | rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); |
58 | |
69 | |
59 | rxvt_img *img = new rxvt_img ( |
70 | rxvt_img *img = new rxvt_img ( |
60 | s, |
71 | s, |
61 | 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, |
62 | gdk_pixbuf_get_width (pb), |
75 | gdk_pixbuf_get_width (pb), |
63 | gdk_pixbuf_get_height (pb) |
76 | gdk_pixbuf_get_height (pb) |
64 | ); |
77 | ); |
65 | |
78 | img->alloc (); |
66 | 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); |
67 | |
80 | |
68 | g_object_unref (pb); |
81 | g_object_unref (pb); |
69 | |
82 | |
70 | return img; |
83 | return img; |
71 | } |
84 | } |
72 | |
85 | |
|
|
86 | void |
|
|
87 | rxvt_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 | |
73 | rxvt_img::~rxvt_img () |
98 | rxvt_img::~rxvt_img () |
74 | { |
99 | { |
75 | if (!shared) |
100 | destroy (); |
76 | XFreePixmap (s->display->dpy, pm); |
101 | } |
|
|
102 | |
|
|
103 | void |
|
|
104 | rxvt_img::alloc () |
|
|
105 | { |
|
|
106 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
|
|
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 | XRectangle clip = { -x, -y, min (w, ref->w), min (h, ref->h) }; |
|
|
120 | XRenderSetPictureClipRectangles (dpy, pic, 0, 0, &clip, 1); |
|
|
121 | |
|
|
122 | return pic; |
77 | } |
123 | } |
78 | |
124 | |
79 | void |
125 | void |
80 | rxvt_img::unshare () |
126 | rxvt_img::unshare () |
81 | { |
127 | { |
82 | if (!shared) |
128 | if (ref->cnt == 1 && ref->ours) |
83 | return; |
129 | return; |
84 | |
130 | |
85 | 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); |
86 | |
136 | |
87 | ::swap (pm , img->pm); |
137 | destroy (); |
88 | ::swap (shared, img->shared); |
|
|
89 | |
138 | |
90 | delete img; |
139 | pm = pm2; |
|
|
140 | ref = new pixref (ref->w, ref->h); |
91 | } |
141 | } |
92 | |
142 | |
93 | void |
143 | void |
94 | rxvt_img::fill (const rxvt_color &c) |
144 | rxvt_img::fill (const rxvt_color &c) |
95 | { |
145 | { |
… | |
… | |
129 | |
179 | |
130 | Display *dpy = s->display->dpy; |
180 | Display *dpy = s->display->dpy; |
131 | int size = max (rh, rv) * 2 + 1; |
181 | int size = max (rh, rv) * 2 + 1; |
132 | double *kernel = (double *)malloc (size * sizeof (double)); |
182 | double *kernel = (double *)malloc (size * sizeof (double)); |
133 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
183 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
134 | 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 (); |
135 | |
188 | |
136 | XRenderPictureAttributes pa; |
189 | XRenderPictureAttributes pa; |
137 | |
|
|
138 | pa.repeat = RepeatPad; |
190 | pa.repeat = RepeatPad; |
139 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
|
|
140 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); |
191 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); |
141 | |
192 | |
142 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
193 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
143 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
194 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
144 | XFreePixmap (dpy, tmp_pm); |
195 | XFreePixmap (dpy, tmp_pm); |
… | |
… | |
340 | } |
391 | } |
341 | |
392 | |
342 | rxvt_img * |
393 | rxvt_img * |
343 | rxvt_img::clone () |
394 | rxvt_img::clone () |
344 | { |
395 | { |
|
|
396 | return new rxvt_img (*this); |
|
|
397 | } |
|
|
398 | |
|
|
399 | rxvt_img * |
|
|
400 | rxvt_img::reify () |
|
|
401 | { |
345 | 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 (); |
346 | |
404 | |
347 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
405 | // todo, if x==0 and y==0 and w==real width we could clone |
348 | XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); |
406 | // but that involves an rtt to find pixmap width. |
349 | XFreeGC (s->display->dpy, gc); |
407 | |
|
|
408 | Display *dpy = s->display->dpy; |
|
|
409 | |
|
|
410 | Picture src = src_picture (); |
|
|
411 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
412 | |
|
|
413 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
|
|
414 | |
|
|
415 | XRenderFreePicture (dpy, src); |
|
|
416 | XRenderFreePicture (dpy, dst); |
350 | |
417 | |
351 | return img; |
418 | return img; |
352 | } |
419 | } |
353 | |
420 | |
354 | rxvt_img * |
421 | rxvt_img * |
355 | rxvt_img::sub_rect (int x, int y, int width, int height) |
422 | rxvt_img::sub_rect (int x, int y, int width, int height) |
356 | { |
423 | { |
357 | rxvt_img *img = new rxvt_img (s, format, width, height); |
424 | rxvt_img *img = clone (); |
358 | |
425 | |
359 | Display *dpy = s->display->dpy; |
426 | //TODO: width > w, must reify |
360 | XRenderPictureAttributes pa; |
|
|
361 | pa.repeat = repeat; |
|
|
362 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
363 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
364 | |
427 | |
365 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); |
428 | img->x += x; |
366 | |
429 | img->y += y; |
367 | XRenderFreePicture (dpy, src); |
430 | img->w = width; |
368 | XRenderFreePicture (dpy, dst); |
431 | img->h = height; |
369 | |
432 | |
370 | return img; |
433 | return img; |
371 | } |
434 | } |
372 | |
435 | |
373 | rxvt_img * |
436 | rxvt_img * |
374 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
437 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
375 | { |
438 | { |
376 | 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 (); |
377 | |
441 | |
378 | Display *dpy = s->display->dpy; |
442 | Display *dpy = s->display->dpy; |
379 | XRenderPictureAttributes pa; |
443 | Picture src = src_picture (); |
380 | pa.repeat = repeat; |
|
|
381 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
382 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
444 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
383 | |
445 | |
384 | XTransform xfrm; |
446 | XTransform xfrm; |
385 | |
447 | |
386 | for (int i = 0; i < 3; ++i) |
448 | for (int i = 0; i < 3; ++i) |
387 | for (int j = 0; j < 3; ++j) |
449 | for (int j = 0; j < 3; ++j) |
388 | 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); |
389 | |
454 | |
390 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
455 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
391 | XRenderSetPictureTransform (dpy, src, &xfrm); |
456 | XRenderSetPictureTransform (dpy, src, &xfrm); |
392 | 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); |
393 | |
458 | |
… | |
… | |
425 | } |
490 | } |
426 | |
491 | |
427 | rxvt_img * |
492 | rxvt_img * |
428 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
493 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
429 | { |
494 | { |
|
|
495 | if (new_format == format) |
|
|
496 | return clone (); |
|
|
497 | |
430 | 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 (); |
431 | |
500 | |
432 | Display *dpy = s->display->dpy; |
501 | Display *dpy = s->display->dpy; |
433 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
502 | Picture src = src_picture (); |
434 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
503 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
435 | int op = PictOpSrc; |
504 | int op = PictOpSrc; |
436 | |
505 | |
437 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
506 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
438 | { |
507 | { |
… | |
… | |
457 | rxvt_img * |
526 | rxvt_img * |
458 | rxvt_img::blend (rxvt_img *img, double factor) |
527 | rxvt_img::blend (rxvt_img *img, double factor) |
459 | { |
528 | { |
460 | rxvt_img *img2 = clone (); |
529 | rxvt_img *img2 = clone (); |
461 | Display *dpy = s->display->dpy; |
530 | Display *dpy = s->display->dpy; |
462 | Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
531 | Picture src = src_picture (); |
463 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
532 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
464 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
533 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
465 | |
534 | |
466 | XRenderColor mask_c; |
535 | XRenderColor mask_c; |
467 | |
536 | |