--- rxvt-unicode/src/rxvtimg.C 2012/06/06 20:22:03 1.28 +++ rxvt-unicode/src/rxvtimg.C 2012/06/07 07:53:12 1.32 @@ -4,18 +4,25 @@ #if HAVE_IMG -#define float_to_component(d) ((d) * 65535.99) - rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) -: s(screen), w(width), h(height), format(format), shared(false) +: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), + pm(0), refcnt(0) { - pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); } +rxvt_img::rxvt_img (const rxvt_img &img) +: 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) +{ + if (refcnt) + ++*refcnt; +} + +#if 0 rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) -: s(screen), pm(pixmap), w(width), h(height), format(format), shared(false) +: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap) { } +#endif rxvt_img * rxvt_img::new_from_root (rxvt_screen *s) @@ -40,11 +47,10 @@ s, XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), root_pm_w, - root_pm_h, - root_pixmap + root_pm_h ); - img->shared = true; + img->pm = root_pixmap; return img; } @@ -64,7 +70,7 @@ gdk_pixbuf_get_width (pb), gdk_pixbuf_get_height (pb) ); - + img->alloc (); img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); g_object_unref (pb); @@ -72,24 +78,45 @@ return img; } -rxvt_img::~rxvt_img () +void +rxvt_img::destroy () { - if (!shared) + if (!refcnt || --*refcnt) + return; + + if (pm) XFreePixmap (s->display->dpy, pm); + + delete refcnt; +} + +rxvt_img::~rxvt_img () +{ + destroy (); +} + +void +rxvt_img::alloc () +{ + pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); + refcnt = new int (1); } void rxvt_img::unshare () { - if (!shared) + if (refcnt && *refcnt == 1) return; - rxvt_img *img = clone (); + Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); + GC gc = XCreateGC (s->display->dpy, pm, 0, 0); + XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); + XFreeGC (s->display->dpy, gc); - ::swap (pm , img->pm); - ::swap (shared, img->shared); + destroy (); - delete img; + pm = pm2; + refcnt = new int (1); } void @@ -134,6 +161,7 @@ double *kernel = (double *)malloc (size * sizeof (double)); XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); rxvt_img *img = new rxvt_img (s, format, w, h); + img->alloc (); XRenderPictureAttributes pa; @@ -202,17 +230,17 @@ } void -rxvt_img::brightness (double r, double g, double b, double a) +rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) { Display *dpy = s->display->dpy; Picture src = create_xrender_mask (dpy, pm, True); Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); XRenderColor mask_c; - mask_c.red = float_to_component (r); - mask_c.green = float_to_component (g); - mask_c.blue = float_to_component (b); - mask_c.alpha = float_to_component (a); + mask_c.red = r; + mask_c.green = g; + mask_c.blue = b; + mask_c.alpha = a; XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); @@ -222,7 +250,7 @@ } void -rxvt_img::contrast (double r, double g, double b, double a) +rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) { if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) return; @@ -232,10 +260,10 @@ Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); XRenderColor mask_c; - mask_c.red = float_to_component (r); - mask_c.green = float_to_component (g); - mask_c.blue = float_to_component (b); - mask_c.alpha = float_to_component (a); + mask_c.red = r; + mask_c.green = g; + mask_c.blue = b; + mask_c.alpha = a; XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); @@ -344,19 +372,14 @@ rxvt_img * rxvt_img::clone () { - rxvt_img *img = new rxvt_img (s, format, w, h); - - GC gc = XCreateGC (s->display->dpy, pm, 0, 0); - XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); - XFreeGC (s->display->dpy, gc); - - return img; + return new rxvt_img (*this); } rxvt_img * -rxvt_img::sub_rect (int x, int y, int width, int height, int repeat) +rxvt_img::sub_rect (int x, int y, int width, int height) { rxvt_img *img = new rxvt_img (s, format, width, height); + img->alloc (); Display *dpy = s->display->dpy; XRenderPictureAttributes pa; @@ -373,9 +396,10 @@ } rxvt_img * -rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat) +rxvt_img::transform (int new_width, int new_height, double matrix[9]) { rxvt_img *img = new rxvt_img (s, format, new_width, new_height); + img->alloc (); Display *dpy = s->display->dpy; XRenderPictureAttributes pa; @@ -412,7 +436,7 @@ } rxvt_img * -rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat) +rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) { double s = sin (phi); double c = cos (phi); @@ -423,22 +447,24 @@ 0, 0, 1 }; - return transform (new_width, new_height, matrix, repeat); + return transform (new_width, new_height, matrix); } rxvt_img * rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) { + if (new_format == format) + return clone (); + rxvt_img *img = new rxvt_img (s, new_format, w, h); + img->alloc (); Display *dpy = s->display->dpy; Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); int op = PictOpSrc; - printf ("fa %x,%x fb %x,%x\n", format->direct.alpha, format->direct.alphaMask, new_format->direct.alpha, new_format->direct.alphaMask);//D - - if (format->direct.alpha && !new_format->direct.alpha) + if (format->direct.alphaMask && !new_format->direct.alphaMask) { // does it have to be that complicated rgba c;