gdk: calculate pixel color shift and precision lazily

This gets rid of the decompose_mask utility entirely and simplifies
backend code.
This commit is contained in:
Paolo Borelli 2016-02-27 11:43:18 +01:00
parent f73e7680ed
commit 70d689cddd
7 changed files with 73 additions and 146 deletions

View File

@ -84,15 +84,6 @@ _gdk_broadway_screen_init_visuals (GdkScreen *screen)
visuals[0]->blue_mask = 0xff;
visuals[0]->colormap_size = 256;
visuals[0]->bits_per_rgb = 8;
gdk_visual_decompose_mask (visuals[0]->red_mask,
&visuals[0]->red_shift,
&visuals[0]->red_prec);
gdk_visual_decompose_mask (visuals[0]->green_mask,
&visuals[0]->green_shift,
&visuals[0]->green_prec);
gdk_visual_decompose_mask (visuals[0]->blue_mask,
&visuals[0]->blue_shift,
&visuals[0]->blue_prec);
visuals[1] = g_object_new (GDK_TYPE_BROADWAY_VISUAL, NULL);
visuals[1]->screen = screen;
@ -104,15 +95,6 @@ _gdk_broadway_screen_init_visuals (GdkScreen *screen)
visuals[1]->blue_mask = 0xff;
visuals[1]->colormap_size = 256;
visuals[1]->bits_per_rgb = 8;
gdk_visual_decompose_mask (visuals[1]->red_mask,
&visuals[1]->red_shift,
&visuals[1]->red_prec);
gdk_visual_decompose_mask (visuals[1]->green_mask,
&visuals[1]->green_shift,
&visuals[1]->green_prec);
gdk_visual_decompose_mask (visuals[1]->blue_mask,
&visuals[1]->blue_shift,
&visuals[1]->blue_prec);
broadway_screen->system_visual = visuals[1];
broadway_screen->rgba_visual = visuals[0];

View File

@ -351,6 +351,44 @@ gdk_visual_get_bits_per_rgb (GdkVisual *visual)
return visual->bits_per_rgb;
}
static void
gdk_visual_get_pixel_details (GdkVisual *visual,
gulong pixel_mask,
guint32 *mask,
gint *shift,
gint *precision)
{
gulong m = 0;
gint s = 0;
gint p = 0;
if (pixel_mask != 0)
{
m = pixel_mask;
while (!(m & 0x1))
{
s++;
m >>= 1;
}
m = pixel_mask;
while (m & 0x1)
{
p++;
m >>= 1;
}
}
if (mask)
*mask = pixel_mask;
if (shift)
*shift = s;
if (precision)
*precision = p;
}
/**
* gdk_visual_get_red_pixel_details:
* @visual: A #GdkVisual
@ -374,14 +412,7 @@ gdk_visual_get_red_pixel_details (GdkVisual *visual,
{
g_return_if_fail (GDK_IS_VISUAL (visual));
if (mask)
*mask = visual->red_mask;
if (shift)
*shift = visual->red_shift;
if (precision)
*precision = visual->red_prec;
gdk_visual_get_pixel_details (visual, visual->red_mask, mask, shift, precision);
}
/**
@ -407,14 +438,7 @@ gdk_visual_get_green_pixel_details (GdkVisual *visual,
{
g_return_if_fail (GDK_IS_VISUAL (visual));
if (mask)
*mask = visual->green_mask;
if (shift)
*shift = visual->green_shift;
if (precision)
*precision = visual->green_prec;
gdk_visual_get_pixel_details (visual, visual->green_mask, mask, shift, precision);
}
/**
@ -440,14 +464,7 @@ gdk_visual_get_blue_pixel_details (GdkVisual *visual,
{
g_return_if_fail (GDK_IS_VISUAL (visual));
if (mask)
*mask = visual->blue_mask;
if (shift)
*shift = visual->blue_shift;
if (precision)
*precision = visual->blue_prec;
gdk_visual_get_pixel_details (visual, visual->blue_mask, mask, shift, precision);
}
/**
@ -467,30 +484,3 @@ gdk_visual_get_screen (GdkVisual *visual)
return visual->screen;
}
void
gdk_visual_decompose_mask (gulong mask,
gint *shift,
gint *prec)
{
*shift = 0;
*prec = 0;
if (mask == 0)
{
g_warning ("Mask is 0 in visual. Server bug ?");
return;
}
while (!(mask & 0x1))
{
(*shift)++;
mask >>= 1;
}
while (mask & 0x1)
{
(*prec)++;
mask >>= 1;
}
}

View File

@ -40,16 +40,8 @@ struct _GdkVisual
gint bits_per_rgb;
guint32 red_mask;
gint red_shift;
gint red_prec;
guint32 green_mask;
gint green_shift;
gint green_prec;
guint32 blue_mask;
gint blue_shift;
gint blue_prec;
GdkScreen *screen;
};
@ -59,10 +51,6 @@ struct _GdkVisualClass
GObjectClass parent_class;
};
void gdk_visual_decompose_mask (gulong mask,
gint *shift,
gint *prec);
G_END_DECLS
#endif

View File

@ -55,16 +55,6 @@ create_standard_visual (GdkScreen *screen,
visual->red_mask = 0xff0000;
visual->green_mask = 0xff00;
visual->blue_mask = 0xff;
gdk_visual_decompose_mask (visual->red_mask,
&visual->red_shift,
&visual->red_prec);
gdk_visual_decompose_mask (visual->green_mask,
&visual->green_shift,
&visual->green_prec);
gdk_visual_decompose_mask (visual->blue_mask,
&visual->blue_shift,
&visual->blue_prec);
return visual;
}

View File

@ -38,6 +38,20 @@ static gint available_depths[1];
static GdkVisualType available_types[1];
static gint
get_color_precision (gulong mask)
{
gint p = 0;
while (mask & 0x1)
{
p++;
mask >>= 1;
}
return p;
}
static void
_gdk_visual_init_internal (GdkScreen *screen, gboolean is_rgba)
{
@ -206,38 +220,18 @@ _gdk_visual_init_internal (GdkScreen *screen, gboolean is_rgba)
visual->byte_order = GDK_LSB_FIRST;
visual->bits_per_rgb = 42; /* Not used? */
if ((visual->type == GDK_VISUAL_TRUE_COLOR) ||
(visual->type == GDK_VISUAL_DIRECT_COLOR))
{
gdk_visual_decompose_mask (visual->red_mask,
&visual->red_shift,
&visual->red_prec);
gdk_visual_decompose_mask (visual->green_mask,
&visual->green_shift,
&visual->green_prec);
gdk_visual_decompose_mask (visual->blue_mask,
&visual->blue_shift,
&visual->blue_prec);
map_entries = 1 << (MAX (visual->red_prec,
MAX (visual->green_prec,
visual->blue_prec)));
}
else
if ((visual->type != GDK_VISUAL_TRUE_COLOR) &&
(visual->type != GDK_VISUAL_DIRECT_COLOR))
{
visual->red_mask = 0;
visual->red_shift = 0;
visual->red_prec = 0;
visual->green_mask = 0;
visual->green_shift = 0;
visual->green_prec = 0;
visual->blue_mask = 0;
visual->blue_shift = 0;
visual->blue_prec = 0;
}
else
map_entries = 1 << (MAX (get_color_precision (visual->red_mask),
MAX (get_color_precision (visual->green_mask),
get_color_precision (visual->blue_mask))));
visual->colormap_size = map_entries;
available_depths[0] = visual->depth;

View File

@ -175,34 +175,12 @@ _gdk_x11_screen_init_visuals (GdkScreen *screen)
visuals[nvisuals]->bits_per_rgb = visual_list[i].bits_per_rgb;
GDK_X11_VISUAL (visuals[nvisuals])->xvisual = visual_list[i].visual;
if ((visuals[nvisuals]->type == GDK_VISUAL_TRUE_COLOR) ||
(visuals[nvisuals]->type == GDK_VISUAL_DIRECT_COLOR))
{
gdk_visual_decompose_mask (visuals[nvisuals]->red_mask,
&visuals[nvisuals]->red_shift,
&visuals[nvisuals]->red_prec);
gdk_visual_decompose_mask (visuals[nvisuals]->green_mask,
&visuals[nvisuals]->green_shift,
&visuals[nvisuals]->green_prec);
gdk_visual_decompose_mask (visuals[nvisuals]->blue_mask,
&visuals[nvisuals]->blue_shift,
&visuals[nvisuals]->blue_prec);
}
else
if ((visuals[nvisuals]->type != GDK_VISUAL_TRUE_COLOR) &&
(visuals[nvisuals]->type != GDK_VISUAL_DIRECT_COLOR))
{
visuals[nvisuals]->red_mask = 0;
visuals[nvisuals]->red_shift = 0;
visuals[nvisuals]->red_prec = 0;
visuals[nvisuals]->green_mask = 0;
visuals[nvisuals]->green_shift = 0;
visuals[nvisuals]->green_prec = 0;
visuals[nvisuals]->blue_mask = 0;
visuals[nvisuals]->blue_shift = 0;
visuals[nvisuals]->blue_prec = 0;
}
nvisuals += 1;

View File

@ -2920,6 +2920,7 @@ gdk_window_x11_set_back_color (GdkWindow *window,
* it's likely alpha, so we set them to 1s.
*/
guint padding, pixel;
gint red_prec, red_shift, green_prec, green_shift, blue_prec, blue_shift;
/* Shifting by >= width-of-type isn't defined in C */
if (visual->depth >= 32)
@ -2928,10 +2929,14 @@ gdk_window_x11_set_back_color (GdkWindow *window,
padding = ((~(guint32)0)) << visual->depth;
pixel = ~ (visual->red_mask | visual->green_mask | visual->blue_mask | padding);
pixel += (((int) (red * ((1 << visual->red_prec ) - 1))) << visual->red_shift ) +
(((int) (green * ((1 << visual->green_prec) - 1))) << visual->green_shift) +
(((int) (blue * ((1 << visual->blue_prec ) - 1))) << visual->blue_shift );
gdk_visual_get_red_pixel_details (visual, NULL, &red_shift, &red_prec);
gdk_visual_get_green_pixel_details (visual, NULL, &green_shift, &green_prec);
gdk_visual_get_blue_pixel_details (visual, NULL, &blue_shift, &blue_prec);
pixel += (((int) (red * ((1 << red_prec ) - 1))) << red_shift ) +
(((int) (green * ((1 << green_prec) - 1))) << green_shift) +
(((int) (blue * ((1 << blue_prec ) - 1))) << blue_shift );
XSetWindowBackground (GDK_WINDOW_XDISPLAY (window),
GDK_WINDOW_XID (window), pixel);