--- rxvt-unicode/src/rxvtimg.C 2012/06/07 06:08:09 1.31 +++ rxvt-unicode/src/rxvtimg.C 2012/06/07 08:36:09 1.34 @@ -4,16 +4,24 @@ #if HAVE_IMG -rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) -: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false) +rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height) +: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(RepeatNormal), + pm(0), ref(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), ref(img.ref) +{ + ++ref->cnt; +} + +#if 0 rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) : 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) @@ -37,12 +45,15 @@ rxvt_img *img = new rxvt_img ( s, XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), + 0, + 0, root_pm_w, - root_pm_h, - root_pixmap + root_pm_h ); - img->shared = true; + img->pm = root_pixmap; + img->ref = new pixref (root_pm_w, root_pm_h); + img->ref->ours = false; return img; } @@ -59,10 +70,12 @@ rxvt_img *img = new rxvt_img ( s, XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), + 0, + 0, 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); @@ -70,24 +83,61 @@ return img; } -rxvt_img::~rxvt_img () +void +rxvt_img::destroy () { - if (!shared) + if (--ref->cnt) + return; + + if (pm && ref->ours) XFreePixmap (s->display->dpy, pm); + + delete ref; +} + +rxvt_img::~rxvt_img () +{ + destroy (); +} + +void +rxvt_img::alloc () +{ + pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); + ref = new pixref (w, h); +} + +Picture +rxvt_img::src_picture () +{ + Display *dpy = s->display->dpy; + + XRenderPictureAttributes pa; + pa.repeat = repeat; + Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); + + XRectangle clip = { -x, -y, min (w, ref->w), min (h, ref->h) }; + XRenderSetPictureClipRectangles (dpy, pic, 0, 0, &clip, 1); + + return pic; } void rxvt_img::unshare () { - if (!shared) + if (ref->cnt == 1 && ref->ours) return; - rxvt_img *img = clone (); + //TODO: maybe should reify instead + Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth); + GC gc = XCreateGC (s->display->dpy, pm, 0, 0); + XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); + XFreeGC (s->display->dpy, gc); - ::swap (pm , img->pm); - ::swap (shared, img->shared); + destroy (); - delete img; + pm = pm2; + ref = new pixref (ref->w, ref->h); } void @@ -131,12 +181,13 @@ int size = max (rh, rv) * 2 + 1; 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); + rxvt_img *img = new rxvt_img (s, format, x, y, w, h); + img->alloc (); - XRenderPictureAttributes pa; + Picture src = src_picture (); + XRenderPictureAttributes pa; pa.repeat = RepeatPad; - Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); @@ -342,11 +393,27 @@ rxvt_img * rxvt_img::clone () { - rxvt_img *img = new rxvt_img (s, format, w, h); + return new rxvt_img (*this); +} - 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); +rxvt_img * +rxvt_img::reify () +{ + rxvt_img *img = new rxvt_img (s, format, 0, 0, w, h); + img->alloc (); + + // todo, if x==0 and y==0 and w==real width we could clone + // but that involves an rtt to find pixmap width. + + Display *dpy = s->display->dpy; + + Picture src = src_picture (); + Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); + + XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); + + XRenderFreePicture (dpy, src); + XRenderFreePicture (dpy, dst); return img; } @@ -354,18 +421,14 @@ rxvt_img * rxvt_img::sub_rect (int x, int y, int width, int height) { - rxvt_img *img = new rxvt_img (s, format, width, height); - - Display *dpy = s->display->dpy; - XRenderPictureAttributes pa; - pa.repeat = repeat; - Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); - Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); + rxvt_img *img = clone (); - XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); + //TODO: width > w, must reify - XRenderFreePicture (dpy, src); - XRenderFreePicture (dpy, dst); + img->x += x; + img->y += y; + img->w = width; + img->h = height; return img; } @@ -373,13 +436,12 @@ rxvt_img * rxvt_img::transform (int new_width, int new_height, double matrix[9]) { - rxvt_img *img = new rxvt_img (s, format, new_width, new_height); + rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height); + img->alloc (); Display *dpy = s->display->dpy; - XRenderPictureAttributes pa; - pa.repeat = repeat; - Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); - Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); + Picture src = src_picture (); + Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); XTransform xfrm; @@ -387,6 +449,9 @@ for (int j = 0; j < 3; ++j) xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); + xfrm.matrix [0][2] += XDoubleToFixed (x);//TODO + xfrm.matrix [0][3] += XDoubleToFixed (y); + XRenderSetPictureFilter (dpy, src, "good", 0, 0); XRenderSetPictureTransform (dpy, src, &xfrm); XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); @@ -427,10 +492,14 @@ rxvt_img * rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) { - rxvt_img *img = new rxvt_img (s, new_format, w, h); + if (new_format == format) + return clone (); + + rxvt_img *img = new rxvt_img (s, new_format, 0, 0, w, h); + img->alloc (); Display *dpy = s->display->dpy; - Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); + Picture src = src_picture (); Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); int op = PictOpSrc; @@ -459,7 +528,7 @@ { rxvt_img *img2 = clone (); Display *dpy = s->display->dpy; - Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); + Picture src = src_picture (); Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); Picture mask = create_xrender_mask (dpy, img->pm, False);