From c2bd2a56d126cefc5bcfdd885866bca64068c0c3 Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Wed, 4 Jul 2012 11:42:29 -0400 Subject: [PATCH] Bug 683243. Dither 16 bit gradients. r=BenWa This does a 2x2 ordered dither in the same way that Skia does. One of the things I'm currently unhappy with is that it duplicates the gradient walker code for 16 bits. We could turn it into a large macro that does the appropriate things for 16 bit and 32 bit versions, but that's not particularly appealing. --HG-- extra : rebase_source : ef89cd53d68166db825bb993e1262db342ba9b5d --- gfx/cairo/README | 2 + gfx/cairo/libpixman/src/pixman-dither.h | 51 +++ .../libpixman/src/pixman-linear-gradient.c | 37 ++- .../libpixman/src/pixman-radial-gradient.c | 15 +- gfx/cairo/pixman-dither.patch | 310 ++++++++++++++++++ 5 files changed, 403 insertions(+), 12 deletions(-) create mode 100644 gfx/cairo/libpixman/src/pixman-dither.h create mode 100644 gfx/cairo/pixman-dither.patch diff --git a/gfx/cairo/README b/gfx/cairo/README index 54ad9f13c0cc..f68778c18c59 100644 --- a/gfx/cairo/README +++ b/gfx/cairo/README @@ -200,6 +200,8 @@ pixman-bilinear-fastpath.patch: Bilinear fast paths for non-neon pixman-16-bit-pipeline.patch: 16 bit pipeline for dithering +pixman-dither.patch: Add dithering of 16 bit gradients + ==== disable printing patch ==== disable-printing.patch: allows us to use NS_PRINTING to disable printing. diff --git a/gfx/cairo/libpixman/src/pixman-dither.h b/gfx/cairo/libpixman/src/pixman-dither.h new file mode 100644 index 000000000000..83c7c8d380a0 --- /dev/null +++ b/gfx/cairo/libpixman/src/pixman-dither.h @@ -0,0 +1,51 @@ +#define R16_BITS 5 +#define G16_BITS 6 +#define B16_BITS 5 + +#define R16_SHIFT (B16_BITS + G16_BITS) +#define G16_SHIFT (B16_BITS) +#define B16_SHIFT 0 + +#define MASK 0xff +#define ONE_HALF 0x80 + +#define A_SHIFT 8 * 3 +#define R_SHIFT 8 * 2 +#define G_SHIFT 8 +#define A_MASK 0xff000000 +#define R_MASK 0xff0000 +#define G_MASK 0xff00 + +#define RB_MASK 0xff00ff +#define AG_MASK 0xff00ff00 +#define RB_ONE_HALF 0x800080 +#define RB_MASK_PLUS_ONE 0x10000100 + +#define ALPHA_8(x) ((x) >> A_SHIFT) +#define RED_8(x) (((x) >> R_SHIFT) & MASK) +#define GREEN_8(x) (((x) >> G_SHIFT) & MASK) +#define BLUE_8(x) ((x) & MASK) + +// This uses the same dithering technique that Skia does. +// It is essentially preturbing the lower bit based on the +// high bit +static inline uint16_t dither_32_to_16(uint32_t c) +{ + uint8_t b = BLUE_8(c); + uint8_t g = GREEN_8(c); + uint8_t r = RED_8(c); + r = ((r << 1) - ((r >> (8 - R16_BITS) << (8 - R16_BITS)) | (r >> R16_BITS))) >> (8 - R16_BITS); + g = ((g << 1) - ((g >> (8 - G16_BITS) << (8 - G16_BITS)) | (g >> G16_BITS))) >> (8 - G16_BITS); + b = ((b << 1) - ((b >> (8 - B16_BITS) << (8 - B16_BITS)) | (b >> B16_BITS))) >> (8 - B16_BITS); + return ((r << R16_SHIFT) | (g << G16_SHIFT) | (b << B16_SHIFT)); +} + +static inline uint16_t dither_8888_to_0565(uint32_t color, pixman_bool_t toggle) +{ + // alternate between a preturbed truncation and a regular truncation + if (toggle) { + return dither_32_to_16(color); + } else { + return CONVERT_8888_TO_0565(color); + } +} diff --git a/gfx/cairo/libpixman/src/pixman-linear-gradient.c b/gfx/cairo/libpixman/src/pixman-linear-gradient.c index a49f7294cc11..20f94f7934c6 100644 --- a/gfx/cairo/libpixman/src/pixman-linear-gradient.c +++ b/gfx/cairo/libpixman/src/pixman-linear-gradient.c @@ -31,6 +31,8 @@ #include #include "pixman-private.h" +#include "pixman-dither.h" + static pixman_bool_t linear_gradient_is_horizontal (pixman_image_t *image, int x, @@ -227,6 +229,8 @@ static uint16_t convert_8888_to_0565(uint32_t color) return CONVERT_8888_TO_0565(color); } + + static uint32_t * linear_get_scanline_16 (pixman_iter_t *iter, const uint32_t *mask) @@ -236,6 +240,7 @@ linear_get_scanline_16 (pixman_iter_t *iter, int y = iter->y; int width = iter->width; uint16_t * buffer = (uint16_t*)iter->buffer; + pixman_bool_t toggle = ((x ^ y) & 1); pixman_vector_t v, unit; pixman_fixed_32_32_t l; @@ -299,11 +304,22 @@ linear_get_scanline_16 (pixman_iter_t *iter, if (((pixman_fixed_32_32_t )(inc * width)) == 0) { - register uint16_t color; + register uint32_t color; + uint16_t dither_diff; + uint16_t color16; + uint16_t color16b; - color = convert_8888_to_0565(_pixman_gradient_walker_pixel (&walker, t)); - while (buffer < end) - *buffer++ = color; + color = _pixman_gradient_walker_pixel (&walker, t); + color16 = dither_8888_to_0565(color, toggle); + color16b = dither_8888_to_0565(color, toggle^1); + // compute the difference + dither_diff = color16 ^ color16b; + while (buffer < end) { + *buffer++ = color16; + // use dither_diff to toggle between color16 and color16b + color16 ^= dither_diff; + toggle ^= 1; + } } else { @@ -314,9 +330,11 @@ linear_get_scanline_16 (pixman_iter_t *iter, { if (!mask || *mask++) { - *buffer = convert_8888_to_0565(_pixman_gradient_walker_pixel (&walker, - t + next_inc)); + *buffer = dither_8888_to_0565(_pixman_gradient_walker_pixel (&walker, + t + next_inc), + toggle); } + toggle ^= 1; i++; next_inc = inc * i; buffer++; @@ -345,8 +363,10 @@ linear_get_scanline_16 (pixman_iter_t *iter, (dx * linear->p1.x + dy * linear->p1.y) * v2) * invden; } - *buffer = convert_8888_to_0565(_pixman_gradient_walker_pixel (&walker, t)); + *buffer = dither_8888_to_0565(_pixman_gradient_walker_pixel (&walker, t), + toggle); } + toggle ^= 1; ++buffer; @@ -374,7 +394,8 @@ linear_get_scanline_wide (pixman_iter_t *iter, const uint32_t *mask) void _pixman_linear_gradient_iter_init (pixman_image_t *image, pixman_iter_t *iter) { - if (linear_gradient_is_horizontal ( + // XXX: we can't use this optimization when dithering + if (0 && linear_gradient_is_horizontal ( iter->image, iter->x, iter->y, iter->width, iter->height)) { if (iter->flags & ITER_16) diff --git a/gfx/cairo/libpixman/src/pixman-radial-gradient.c b/gfx/cairo/libpixman/src/pixman-radial-gradient.c index 96cb0fd70f4e..d31aa14d29f6 100644 --- a/gfx/cairo/libpixman/src/pixman-radial-gradient.c +++ b/gfx/cairo/libpixman/src/pixman-radial-gradient.c @@ -34,6 +34,8 @@ #include #include "pixman-private.h" +#include "pixman-dither.h" + static inline pixman_fixed_32_32_t dot (pixman_fixed_48_16_t x1, pixman_fixed_48_16_t y1, @@ -494,6 +496,7 @@ radial_get_scanline_16 (pixman_iter_t *iter, const uint32_t *mask) int y = iter->y; int width = iter->width; uint16_t *buffer = iter->buffer; + pixman_bool_t toggle = ((x ^ y) & 1); gradient_t *gradient = (gradient_t *)image; radial_gradient_t *radial = (radial_gradient_t *)image; @@ -580,15 +583,17 @@ radial_get_scanline_16 (pixman_iter_t *iter, const uint32_t *mask) { if (!mask || *mask++) { - *buffer = convert_8888_to_0565( + *buffer = dither_8888_to_0565( radial_compute_color (radial->a, b, c, radial->inva, radial->delta.radius, radial->mindr, &walker, - image->common.repeat)); + image->common.repeat), + toggle); } + toggle ^= 1; b += db; c += dc; dc += ddc; @@ -626,13 +631,14 @@ radial_get_scanline_16 (pixman_iter_t *iter, const uint32_t *mask) pdx, pdy, radial->c1.radius); /* / pixman_fixed_1 / pixman_fixed_1 */ - *buffer = convert_8888_to_0565 ( + *buffer = dither_8888_to_0565 ( radial_compute_color (radial->a, b, c, radial->inva, radial->delta.radius, radial->mindr, &walker, - image->common.repeat)); + image->common.repeat), + toggle); } else { @@ -641,6 +647,7 @@ radial_get_scanline_16 (pixman_iter_t *iter, const uint32_t *mask) } ++buffer; + toggle ^= 1; v.vector[0] += unit.vector[0]; v.vector[1] += unit.vector[1]; diff --git a/gfx/cairo/pixman-dither.patch b/gfx/cairo/pixman-dither.patch new file mode 100644 index 000000000000..633a8d7282d5 --- /dev/null +++ b/gfx/cairo/pixman-dither.patch @@ -0,0 +1,310 @@ +diff --git a/gfx/cairo/libpixman/src/pixman-dither.h b/gfx/cairo/libpixman/src/pixman-dither.h +new file mode 100644 +--- /dev/null ++++ b/gfx/cairo/libpixman/src/pixman-dither.h +@@ -0,0 +1,51 @@ ++#define R16_BITS 5 ++#define G16_BITS 6 ++#define B16_BITS 5 ++ ++#define R16_SHIFT (B16_BITS + G16_BITS) ++#define G16_SHIFT (B16_BITS) ++#define B16_SHIFT 0 ++ ++#define MASK 0xff ++#define ONE_HALF 0x80 ++ ++#define A_SHIFT 8 * 3 ++#define R_SHIFT 8 * 2 ++#define G_SHIFT 8 ++#define A_MASK 0xff000000 ++#define R_MASK 0xff0000 ++#define G_MASK 0xff00 ++ ++#define RB_MASK 0xff00ff ++#define AG_MASK 0xff00ff00 ++#define RB_ONE_HALF 0x800080 ++#define RB_MASK_PLUS_ONE 0x10000100 ++ ++#define ALPHA_8(x) ((x) >> A_SHIFT) ++#define RED_8(x) (((x) >> R_SHIFT) & MASK) ++#define GREEN_8(x) (((x) >> G_SHIFT) & MASK) ++#define BLUE_8(x) ((x) & MASK) ++ ++// This uses the same dithering technique that Skia does. ++// It is essentially preturbing the lower bit based on the ++// high bit ++static inline uint16_t dither_32_to_16(uint32_t c) ++{ ++ uint8_t b = BLUE_8(c); ++ uint8_t g = GREEN_8(c); ++ uint8_t r = RED_8(c); ++ r = ((r << 1) - ((r >> (8 - R16_BITS) << (8 - R16_BITS)) | (r >> R16_BITS))) >> (8 - R16_BITS); ++ g = ((g << 1) - ((g >> (8 - G16_BITS) << (8 - G16_BITS)) | (g >> G16_BITS))) >> (8 - G16_BITS); ++ b = ((b << 1) - ((b >> (8 - B16_BITS) << (8 - B16_BITS)) | (b >> B16_BITS))) >> (8 - B16_BITS); ++ return ((r << R16_SHIFT) | (g << G16_SHIFT) | (b << B16_SHIFT)); ++} ++ ++static inline uint16_t dither_8888_to_0565(uint32_t color, pixman_bool_t toggle) ++{ ++ // alternate between a preturbed truncation and a regular truncation ++ if (toggle) { ++ return dither_32_to_16(color); ++ } else { ++ return CONVERT_8888_TO_0565(color); ++ } ++} +diff --git a/gfx/cairo/libpixman/src/pixman-linear-gradient.c b/gfx/cairo/libpixman/src/pixman-linear-gradient.c +--- a/gfx/cairo/libpixman/src/pixman-linear-gradient.c ++++ b/gfx/cairo/libpixman/src/pixman-linear-gradient.c +@@ -26,16 +26,18 @@ + */ + + #ifdef HAVE_CONFIG_H + #include + #endif + #include + #include "pixman-private.h" + ++#include "pixman-dither.h" ++ + static pixman_bool_t + linear_gradient_is_horizontal (pixman_image_t *image, + int x, + int y, + int width, + int height) + { + linear_gradient_t *linear = (linear_gradient_t *)image; +@@ -222,25 +224,28 @@ linear_get_scanline_narrow (pixman_iter_ + return iter->buffer; + } + + static uint16_t convert_8888_to_0565(uint32_t color) + { + return CONVERT_8888_TO_0565(color); + } + ++ ++ + static uint32_t * + linear_get_scanline_16 (pixman_iter_t *iter, + const uint32_t *mask) + { + pixman_image_t *image = iter->image; + int x = iter->x; + int y = iter->y; + int width = iter->width; + uint16_t * buffer = (uint16_t*)iter->buffer; ++ pixman_bool_t toggle = ((x ^ y) & 1); + + pixman_vector_t v, unit; + pixman_fixed_32_32_t l; + pixman_fixed_48_16_t dx, dy; + gradient_t *gradient = (gradient_t *)image; + linear_gradient_t *linear = (linear_gradient_t *)image; + uint16_t *end = buffer + width; + pixman_gradient_walker_t walker; +@@ -294,34 +299,47 @@ linear_get_scanline_16 (pixman_iter_t * + t = ((dx * v.vector[0] + dy * v.vector[1]) - + (dx * linear->p1.x + dy * linear->p1.y) * v2) * invden; + inc = (dx * unit.vector[0] + dy * unit.vector[1]) * invden; + } + next_inc = 0; + + if (((pixman_fixed_32_32_t )(inc * width)) == 0) + { +- register uint16_t color; ++ register uint32_t color; ++ uint16_t dither_diff; ++ uint16_t color16; ++ uint16_t color16b; + +- color = convert_8888_to_0565(_pixman_gradient_walker_pixel (&walker, t)); +- while (buffer < end) +- *buffer++ = color; ++ color = _pixman_gradient_walker_pixel (&walker, t); ++ color16 = dither_8888_to_0565(color, toggle); ++ color16b = dither_8888_to_0565(color, toggle^1); ++ // compute the difference ++ dither_diff = color16 ^ color16b; ++ while (buffer < end) { ++ *buffer++ = color16; ++ // use dither_diff to toggle between color16 and color16b ++ color16 ^= dither_diff; ++ toggle ^= 1; ++ } + } + else + { + int i; + + i = 0; + while (buffer < end) + { + if (!mask || *mask++) + { +- *buffer = convert_8888_to_0565(_pixman_gradient_walker_pixel (&walker, +- t + next_inc)); ++ *buffer = dither_8888_to_0565(_pixman_gradient_walker_pixel (&walker, ++ t + next_inc), ++ toggle); + } ++ toggle ^= 1; + i++; + next_inc = inc * i; + buffer++; + } + } + } + else + { +@@ -340,18 +358,20 @@ linear_get_scanline_16 (pixman_iter_t * + + invden = pixman_fixed_1 * (double) pixman_fixed_1 / + (l * (double) v.vector[2]); + v2 = v.vector[2] * (1. / pixman_fixed_1); + t = ((dx * v.vector[0] + dy * v.vector[1]) - + (dx * linear->p1.x + dy * linear->p1.y) * v2) * invden; + } + +- *buffer = convert_8888_to_0565(_pixman_gradient_walker_pixel (&walker, t)); ++ *buffer = dither_8888_to_0565(_pixman_gradient_walker_pixel (&walker, t), ++ toggle); + } ++ toggle ^= 1; + + ++buffer; + + v.vector[0] += unit.vector[0]; + v.vector[1] += unit.vector[1]; + v.vector[2] += unit.vector[2]; + } + } +@@ -369,17 +389,18 @@ linear_get_scanline_wide (pixman_iter_t + pixman_expand ((uint64_t *)buffer, buffer, PIXMAN_a8r8g8b8, iter->width); + + return buffer; + } + + void + _pixman_linear_gradient_iter_init (pixman_image_t *image, pixman_iter_t *iter) + { +- if (linear_gradient_is_horizontal ( ++ // XXX: we can't use this optimization when dithering ++ if (0 && linear_gradient_is_horizontal ( + iter->image, iter->x, iter->y, iter->width, iter->height)) + { + if (iter->flags & ITER_16) + linear_get_scanline_16 (iter, NULL); + else if (iter->flags & ITER_NARROW) + linear_get_scanline_narrow (iter, NULL); + else + linear_get_scanline_wide (iter, NULL); +diff --git a/gfx/cairo/libpixman/src/pixman-radial-gradient.c b/gfx/cairo/libpixman/src/pixman-radial-gradient.c +--- a/gfx/cairo/libpixman/src/pixman-radial-gradient.c ++++ b/gfx/cairo/libpixman/src/pixman-radial-gradient.c +@@ -29,16 +29,18 @@ + + #ifdef HAVE_CONFIG_H + #include + #endif + #include + #include + #include "pixman-private.h" + ++#include "pixman-dither.h" ++ + static inline pixman_fixed_32_32_t + dot (pixman_fixed_48_16_t x1, + pixman_fixed_48_16_t y1, + pixman_fixed_48_16_t z1, + pixman_fixed_48_16_t x2, + pixman_fixed_48_16_t y2, + pixman_fixed_48_16_t z2) + { +@@ -489,16 +491,17 @@ radial_get_scanline_16 (pixman_iter_t *i + * <=> for every p, the radiuses associated with the two t solutions + * have opposite sign + */ + pixman_image_t *image = iter->image; + int x = iter->x; + int y = iter->y; + int width = iter->width; + uint16_t *buffer = iter->buffer; ++ pixman_bool_t toggle = ((x ^ y) & 1); + + gradient_t *gradient = (gradient_t *)image; + radial_gradient_t *radial = (radial_gradient_t *)image; + uint16_t *end = buffer + width; + pixman_gradient_walker_t walker; + pixman_vector_t v, unit; + + /* reference point is the center of the pixel */ +@@ -575,25 +578,27 @@ radial_get_scanline_16 (pixman_iter_t *i + unit.vector[0], unit.vector[1], 0); + ddc = 2 * dot (unit.vector[0], unit.vector[1], 0, + unit.vector[0], unit.vector[1], 0); + + while (buffer < end) + { + if (!mask || *mask++) + { +- *buffer = convert_8888_to_0565( ++ *buffer = dither_8888_to_0565( + radial_compute_color (radial->a, b, c, + radial->inva, + radial->delta.radius, + radial->mindr, + &walker, +- image->common.repeat)); ++ image->common.repeat), ++ toggle); + } + ++ toggle ^= 1; + b += db; + c += dc; + dc += ddc; + ++buffer; + } + } + else + { +@@ -621,31 +626,33 @@ radial_get_scanline_16 (pixman_iter_t *i + radial->delta.x, radial->delta.y, + radial->delta.radius); + /* / pixman_fixed_1 / pixman_fixed_1 */ + + c = fdot (pdx, pdy, -radial->c1.radius, + pdx, pdy, radial->c1.radius); + /* / pixman_fixed_1 / pixman_fixed_1 */ + +- *buffer = convert_8888_to_0565 ( ++ *buffer = dither_8888_to_0565 ( + radial_compute_color (radial->a, b, c, + radial->inva, + radial->delta.radius, + radial->mindr, + &walker, +- image->common.repeat)); ++ image->common.repeat), ++ toggle); + } + else + { + *buffer = 0; + } + } + + ++buffer; ++ toggle ^= 1; + + v.vector[0] += unit.vector[0]; + v.vector[1] += unit.vector[1]; + v.vector[2] += unit.vector[2]; + } + } + + iter->y++; +