--- rxvt-unicode/src/rxvtimg.C 2012/06/07 07:53:12 1.32 +++ rxvt-unicode/src/rxvtimg.C 2012/06/09 11:13:14 1.67 @@ -4,26 +4,18 @@ #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), - pm(0), refcnt(0) +rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) +: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), + pm(0), ref(0) { } 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) +: 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) { - if (refcnt) - ++*refcnt; + ++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) { @@ -46,11 +38,117 @@ rxvt_img *img = new rxvt_img ( s, XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), + 0, + 0, root_pm_w, root_pm_h ); img->pm = root_pixmap; + img->ref = new pixref (root_pm_w, root_pm_h); + img->ref->ours = false; + + return img; +} + +rxvt_img * +rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) +{ + Display *dpy = s->display->dpy; + + int width = gdk_pixbuf_get_width (pb); + int height = gdk_pixbuf_get_height (pb); + + if (width > 32767 || height > 32767) // well, we *could* upload in chunks + rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); + + // since we require rgb24/argb32 formats from xrender we assume + // that both 24 and 32 bpp MUST be supported by any screen that supports xrender + int depth = gdk_pixbuf_get_has_alpha (pb) ? 32 : 24; + + int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; + + XImage xi; + + xi.width = width; + xi.height = height; + xi.xoffset = 0; + xi.format = ZPixmap; + xi.byte_order = ImageByteOrder (dpy); + xi.bitmap_unit = 0; //XY only, unused + xi.bitmap_bit_order = 0; //XY only, unused + xi.bitmap_pad = BitmapPad (dpy); + xi.depth = depth; + xi.bytes_per_line = 0; + xi.bits_per_pixel = 32; //Z only + xi.red_mask = 0x00000000; //Z only, unused + xi.green_mask = 0x00000000; //Z only, unused + xi.blue_mask = 0x00000000; //Z only, unused + xi.obdata = 0; // probably unused + + bool byte_order_mismatch = byte_order != xi.byte_order; + + if (!XInitImage (&xi)) + rxvt_fatal ("unable to initialise ximage, please report.\n"); + + if (height > INT_MAX / xi.bytes_per_line) + rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); + + xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); + + int rowstride = gdk_pixbuf_get_rowstride (pb); + + assert (3 + (depth == 32) == gdk_pixbuf_get_n_channels (pb)); + unsigned char *row = gdk_pixbuf_get_pixels (pb); + char *line = xi.data; + + for (int y = 0; y < height; y++) + { + unsigned char *src = row; + uint32_t *dst = (uint32_t *)line; + + if (depth == 24) + for (int x = 0; x < width; x++) + { + uint8_t r = *src++; + uint8_t g = *src++; + uint8_t b = *src++; + + uint32_t v = (r << 16) | (g << 8) | b; + + if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) + v = ecb_bswap32 (v); + + *dst++ = v; + } + else + for (int x = 0; x < width; x++) + { + uint32_t v = *(uint32_t *)src; src += 4; + + if (ecb_big_endian ()) + v = ecb_bswap32 (v); + + v = ecb_rotl32 (v, 8); // abgr to bgra + + if (!byte_order_mismatch) + v = ecb_bswap32 (v); + + *dst++ = v; + } + + row += rowstride; + line += xi.bytes_per_line; + } + + rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, depth == 24 ? PictStandardRGB24 : PictStandardARGB32), 0, 0, width, height); + img->alloc (); + + GC gc = XCreateGC (dpy, img->pm, 0, 0); + XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); + XFreeGC (dpy, gc); + + free (xi.data); return img; } @@ -64,14 +162,7 @@ if (!pb) rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); - rxvt_img *img = new rxvt_img ( - s, - XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), - gdk_pixbuf_get_width (pb), - gdk_pixbuf_get_height (pb) - ); - img->alloc (); - img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); + rxvt_img *img = new_from_pixbuf (s, pb); g_object_unref (pb); @@ -81,13 +172,13 @@ void rxvt_img::destroy () { - if (!refcnt || --*refcnt) + if (--ref->cnt) return; - if (pm) + if (pm && ref->ours) XFreePixmap (s->display->dpy, pm); - delete refcnt; + delete ref; } rxvt_img::~rxvt_img () @@ -99,24 +190,36 @@ rxvt_img::alloc () { pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); - refcnt = new int (1); + 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); + + return pic; } void rxvt_img::unshare () { - if (refcnt && *refcnt == 1) + if (ref->cnt == 1 && ref->ours) return; - Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); + 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, w, h, 0, 0); + XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); XFreeGC (s->display->dpy, gc); destroy (); pm = pm2; - refcnt = new int (1); + ref = new pixref (ref->w, ref->h); } void @@ -160,14 +263,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, repeat); img->alloc (); XRenderPictureAttributes pa; - pa.repeat = RepeatPad; - Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); - Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); + Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); + Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); @@ -193,7 +295,7 @@ get_gaussian_kernel (rv, size, kernel, params); ::swap (params[0], params[1]); - XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); + XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2); XRenderComposite (dpy, PictOpSrc, tmp, @@ -229,31 +331,64 @@ return mask; } +static void +extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc) +{ + int32_t x = clamp (c, cl0, cl1); + c -= x; + xc = x; +} + +static bool +extract (int32_t cl0, int32_t cl1, int32_t &r, int32_t &g, int32_t &b, int32_t &a, unsigned short &xr, unsigned short &xg, unsigned short &xb, unsigned short &xa) +{ + extract (cl0, cl1, r, xr); + extract (cl0, cl1, g, xg); + extract (cl0, cl1, b, xb); + extract (cl0, cl1, a, xa); + + return xr | xg | xb | xa; +} + void -rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) +rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a) { + unshare (); + 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 = r; - mask_c.green = g; - mask_c.blue = b; - mask_c.alpha = a; - XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); + while (r | g | b | a) + { + unsigned short xr, xg, xb, xa; + XRenderColor mask_c; + + if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) + XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); + + if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) + { + XRenderColor mask_w = { 65535, 65535, 65535, 65535 }; + XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); + XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); + XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); + } + } - XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); - XRenderFreePicture (dpy, src); XRenderFreePicture (dpy, dst); } void -rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) +rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a) { if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) - return; + { + rxvt_warn ("rxvt_img::contrast operation not supported on this display, RENDER extension too old.\n"); + return; + } + + unshare (); Display *dpy = s->display->dpy; Picture src = create_xrender_mask (dpy, pm, True); @@ -272,125 +407,83 @@ XRenderFreePicture (dpy, dst); } -bool -rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) +rxvt_img * +rxvt_img::clone () { - Display *dpy = s->display->dpy; + return new rxvt_img (*this); +} - if (s->visual->c_class != TrueColor) - return false; +static XRenderPictFormat * +find_alpha_format_for (Display *dpy, XRenderPictFormat *format) +{ + if (format->direct.alphaMask) + return format; // already has alpha - uint32_t red_mask, green_mask, blue_mask, alpha_mask; + // try to find a suitable alpha format, one bit alpha is enough for our purposes + if (format->type == PictTypeDirect) + for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) + if (f->direct.alphaMask + && f->type == PictTypeDirect + && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) + && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) + && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) + return f; - red_mask = (uint32_t)format->direct.redMask << format->direct.red; - green_mask = (uint32_t)format->direct.greenMask << format->direct.green; - blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue; - alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha; - - int width_r = ecb_popcount32 (red_mask); - int width_g = ecb_popcount32 (green_mask); - int width_b = ecb_popcount32 (blue_mask); - int width_a = ecb_popcount32 (alpha_mask); - - if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8) - return false; - - int sh_r = ecb_ctz32 (red_mask); - int sh_g = ecb_ctz32 (green_mask); - int sh_b = ecb_ctz32 (blue_mask); - int sh_a = ecb_ctz32 (alpha_mask); - - if (width > 32767 || height > 32767) - return false; - - XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0, - width, height, 32, 0); - if (!ximage) - return false; + // should be a very good fallback + return XRenderFindStandardFormat (dpy, PictStandardARGB32); +} - if (height > INT_MAX / ximage->bytes_per_line - || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line))) - { - XDestroyImage (ximage); - return false; - } +rxvt_img * +rxvt_img::reify () +{ + if (x == 0 && y == 0 && w == ref->w && h == ref->h) + return clone (); - GC gc = XCreateGC (dpy, pm, 0, 0); + Display *dpy = s->display->dpy; - ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; + // add an alpha channel if... + bool alpha = !format->direct.alphaMask // pixmap has none yet + && (x || y) // we need one because of non-zero offset + && repeat == RepeatNone; // and we have no good pixels to fill with - int rowstride = gdk_pixbuf_get_rowstride (pixbuf); - int channels = gdk_pixbuf_get_n_channels (pixbuf); - unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; - char *line = ximage->data; + rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); + img->alloc (); - for (int y = 0; y < height; y++) + Picture src = src_picture (); + Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); + + if (alpha) { - for (int x = 0; x < width; x++) - { - unsigned char *pixel = row + x * channels; - uint32_t value; - unsigned char r, g, b, a; - - if (channels == 4) - { - a = pixel[3]; - r = pixel[0] * a / 0xff; - g = pixel[1] * a / 0xff; - b = pixel[2] * a / 0xff; - } - else - { - a = 0xff; - r = pixel[0]; - g = pixel[1]; - b = pixel[2]; - } - - value = ((r >> (8 - width_r)) << sh_r) - | ((g >> (8 - width_g)) << sh_g) - | ((b >> (8 - width_b)) << sh_b) - | ((a >> (8 - width_a)) << sh_a); - - if (ximage->bits_per_pixel == 32) - ((uint32_t *)line)[x] = value; - else - XPutPixel (ximage, x, y, value); - } - - row += rowstride; - line += ximage->bytes_per_line; + XRenderColor rc = { 0, 0, 0, 0 }; + XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles + XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); } + else + XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); - XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height); - XDestroyImage (ximage); - XFreeGC (dpy, gc); - - return true; -} + XRenderFreePicture (dpy, src); + XRenderFreePicture (dpy, dst); -rxvt_img * -rxvt_img::clone () -{ - return new rxvt_img (*this); + return img; } rxvt_img * rxvt_img::sub_rect (int x, int y, int width, int height) { - rxvt_img *img = new rxvt_img (s, format, width, height); - img->alloc (); + rxvt_img *img = clone (); - 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); + img->x += x; + img->y += y; - XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); + if (w != width || h != height) + { + img->w = width; + img->h = height; - XRenderFreePicture (dpy, src); - XRenderFreePicture (dpy, dst); + rxvt_img *img2 = img->reify (); + delete img; + img = img2; + } return img; } @@ -398,14 +491,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, repeat); 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; @@ -413,6 +504,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 [1][2] -= 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); @@ -426,13 +520,24 @@ rxvt_img * rxvt_img::scale (int new_width, int new_height) { + if (w == new_width && h == new_height) + return clone (); + double matrix[9] = { w / (double)new_width, 0, 0, 0, h / (double)new_height, 0, 0, 0, 1 }; - return transform (new_width, new_height, matrix); + int old_repeat_mode = repeat; + repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems + + rxvt_img *img = transform (new_width, new_height, matrix); + + repeat = old_repeat_mode; + img->repeat = repeat; + + return img; } rxvt_img * @@ -451,16 +556,16 @@ } rxvt_img * -rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) +rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) { if (new_format == format) return clone (); - rxvt_img *img = new rxvt_img (s, new_format, w, h); + rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); 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; @@ -489,7 +594,7 @@ { rxvt_img *img2 = clone (); Display *dpy = s->display->dpy; - Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); + Picture src = img->src_picture (); Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); Picture mask = create_xrender_mask (dpy, img->pm, False);