… | |
… | |
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 | return pic; |
77 | } |
120 | } |
78 | |
121 | |
79 | void |
122 | void |
80 | rxvt_img::unshare () |
123 | rxvt_img::unshare () |
81 | { |
124 | { |
82 | if (!shared) |
125 | if (ref->cnt == 1 && ref->ours) |
83 | return; |
126 | return; |
84 | |
127 | |
85 | rxvt_img *img = clone (); |
128 | //TODO: maybe should reify instead |
|
|
129 | Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth); |
|
|
130 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
|
|
131 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); |
|
|
132 | XFreeGC (s->display->dpy, gc); |
86 | |
133 | |
87 | ::swap (pm , img->pm); |
134 | destroy (); |
88 | ::swap (shared, img->shared); |
|
|
89 | |
135 | |
90 | delete img; |
136 | pm = pm2; |
|
|
137 | ref = new pixref (ref->w, ref->h); |
91 | } |
138 | } |
92 | |
139 | |
93 | void |
140 | void |
94 | rxvt_img::fill (const rxvt_color &c) |
141 | rxvt_img::fill (const rxvt_color &c) |
95 | { |
142 | { |
… | |
… | |
129 | |
176 | |
130 | Display *dpy = s->display->dpy; |
177 | Display *dpy = s->display->dpy; |
131 | int size = max (rh, rv) * 2 + 1; |
178 | int size = max (rh, rv) * 2 + 1; |
132 | double *kernel = (double *)malloc (size * sizeof (double)); |
179 | double *kernel = (double *)malloc (size * sizeof (double)); |
133 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
180 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
134 | rxvt_img *img = new rxvt_img (s, format, w, h); |
181 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h); |
|
|
182 | img->alloc (); |
|
|
183 | |
|
|
184 | Picture src = src_picture (); |
135 | |
185 | |
136 | XRenderPictureAttributes pa; |
186 | XRenderPictureAttributes pa; |
137 | |
|
|
138 | pa.repeat = RepeatPad; |
187 | pa.repeat = RepeatPad; |
139 | Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); |
|
|
140 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); |
188 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); |
141 | |
189 | |
142 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
190 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
143 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
191 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
144 | XFreePixmap (dpy, tmp_pm); |
192 | XFreePixmap (dpy, tmp_pm); |
… | |
… | |
340 | } |
388 | } |
341 | |
389 | |
342 | rxvt_img * |
390 | rxvt_img * |
343 | rxvt_img::clone () |
391 | rxvt_img::clone () |
344 | { |
392 | { |
|
|
393 | return new rxvt_img (*this); |
|
|
394 | } |
|
|
395 | |
|
|
396 | rxvt_img * |
|
|
397 | rxvt_img::reify () |
|
|
398 | { |
|
|
399 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
|
|
400 | return clone (); |
|
|
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 | Display *dpy = s->display->dpy; |
348 | XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); |
406 | |
349 | XFreeGC (s->display->dpy, gc); |
407 | Picture src = src_picture (); |
|
|
408 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
409 | |
|
|
410 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
|
|
411 | |
|
|
412 | XRenderFreePicture (dpy, src); |
|
|
413 | XRenderFreePicture (dpy, dst); |
350 | |
414 | |
351 | return img; |
415 | return img; |
352 | } |
416 | } |
353 | |
417 | |
354 | rxvt_img * |
418 | rxvt_img * |
355 | rxvt_img::sub_rect (int x, int y, int width, int height) |
419 | rxvt_img::sub_rect (int x, int y, int width, int height) |
356 | { |
420 | { |
357 | rxvt_img *img = new rxvt_img (s, format, width, height); |
421 | rxvt_img *img = clone (); |
358 | |
422 | |
359 | Display *dpy = s->display->dpy; |
423 | img->x += x; |
360 | XRenderPictureAttributes pa; |
424 | img->y += y; |
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 | |
425 | |
365 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); |
426 | if (w != width || h != height) |
|
|
427 | { |
|
|
428 | img->w = width; |
|
|
429 | img->h = height; |
366 | |
430 | |
367 | XRenderFreePicture (dpy, src); |
431 | img->reify (); |
368 | XRenderFreePicture (dpy, dst); |
432 | } |
369 | |
433 | |
370 | return img; |
434 | return img; |
371 | } |
435 | } |
372 | |
436 | |
373 | rxvt_img * |
437 | rxvt_img * |
374 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
438 | rxvt_img::transform (int new_width, int new_height, double matrix[9]) |
375 | { |
439 | { |
376 | rxvt_img *img = new rxvt_img (s, format, new_width, new_height); |
440 | rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height); |
|
|
441 | img->alloc (); |
377 | |
442 | |
378 | Display *dpy = s->display->dpy; |
443 | Display *dpy = s->display->dpy; |
379 | XRenderPictureAttributes pa; |
444 | 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); |
445 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
383 | |
446 | |
384 | XTransform xfrm; |
447 | XTransform xfrm; |
385 | |
448 | |
386 | for (int i = 0; i < 3; ++i) |
449 | for (int i = 0; i < 3; ++i) |
387 | for (int j = 0; j < 3; ++j) |
450 | for (int j = 0; j < 3; ++j) |
388 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
451 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
|
|
452 | |
|
|
453 | xfrm.matrix [0][2] += XDoubleToFixed (x);//TODO |
|
|
454 | xfrm.matrix [0][3] += XDoubleToFixed (y); |
389 | |
455 | |
390 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
456 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
391 | XRenderSetPictureTransform (dpy, src, &xfrm); |
457 | XRenderSetPictureTransform (dpy, src, &xfrm); |
392 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
458 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
393 | |
459 | |
… | |
… | |
425 | } |
491 | } |
426 | |
492 | |
427 | rxvt_img * |
493 | rxvt_img * |
428 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
494 | rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) |
429 | { |
495 | { |
|
|
496 | if (new_format == format) |
|
|
497 | return clone (); |
|
|
498 | |
430 | rxvt_img *img = new rxvt_img (s, new_format, w, h); |
499 | rxvt_img *img = new rxvt_img (s, new_format, 0, 0, w, h); |
|
|
500 | img->alloc (); |
431 | |
501 | |
432 | Display *dpy = s->display->dpy; |
502 | Display *dpy = s->display->dpy; |
433 | Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); |
503 | Picture src = src_picture (); |
434 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
504 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
435 | int op = PictOpSrc; |
505 | int op = PictOpSrc; |
436 | |
506 | |
437 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
507 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
438 | { |
508 | { |
… | |
… | |
457 | rxvt_img * |
527 | rxvt_img * |
458 | rxvt_img::blend (rxvt_img *img, double factor) |
528 | rxvt_img::blend (rxvt_img *img, double factor) |
459 | { |
529 | { |
460 | rxvt_img *img2 = clone (); |
530 | rxvt_img *img2 = clone (); |
461 | Display *dpy = s->display->dpy; |
531 | Display *dpy = s->display->dpy; |
462 | Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
532 | Picture src = img->src_picture (); |
463 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
533 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
464 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
534 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
465 | |
535 | |
466 | XRenderColor mask_c; |
536 | XRenderColor mask_c; |
467 | |
537 | |