* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#ifdef HAVE_CONFIG_H
#include <config.h>
+#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <assert.h>
-#include "pixman.h"
#include "pixman-private.h"
-static void
-init_source_image (source_image_t *image)
-{
- image->class = SOURCE_IMAGE_CLASS_UNKNOWN;
-}
+#define Alpha(x) ((x) >> 24)
-static pixman_bool_t
-init_gradient (gradient_t *gradient,
- const pixman_gradient_stop_t *stops,
- int n_stops)
+pixman_bool_t
+_pixman_init_gradient (gradient_t *gradient,
+ const pixman_gradient_stop_t *stops,
+ int n_stops)
{
return_val_if_fail (n_stops > 0, FALSE);
- init_source_image (&gradient->common);
-
- gradient->stops = malloc (n_stops * sizeof (pixman_gradient_stop_t));
+ gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
if (!gradient->stops)
return FALSE;
memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
-
+
gradient->n_stops = n_stops;
gradient->stop_range = 0xffff;
gradient->color_table = NULL;
gradient->color_table_size = 0;
+ gradient->common.class = SOURCE_IMAGE_CLASS_UNKNOWN;
return TRUE;
}
-static uint32_t
-color_to_uint32 (const pixman_color_t *color)
+/*
+ * By default, just evaluate the image at 32bpp and expand. Individual image
+ * types can plug in a better scanline getter if they want to. For example
+ * we could produce smoother gradients by evaluating them at higher color depth, but
+ * that's a project for the future.
+ */
+void
+_pixman_image_get_scanline_64_generic (pixman_image_t * pict, int x, int y, int width,
+ uint64_t *buffer, uint64_t *mask, uint32_t maskBits)
{
- return
- (color->alpha >> 8 << 24) |
- (color->red >> 8 << 16) |
- (color->green & 0xff00) |
- (color->blue >> 8);
-}
-
-static pixman_image_t *image_cache;
+ uint32_t *mask8 = NULL;
+
+ // Contract the mask image, if one exists, so that the 32-bit fetch function
+ // can use it.
+ if (mask) {
+ mask8 = pixman_malloc_ab(width, sizeof(uint32_t));
+ if (!mask8)
+ return;
+
+ pixman_contract(mask8, mask, width);
+ }
-static pixman_image_t *
-new_image (void)
-{
- pixman_image_t *image;
+ // Fetch the source image into the first half of buffer.
+ _pixman_image_get_scanline_32 (pict, x, y, width, (uint32_t*)buffer, mask8,
+ maskBits);
- if (image_cache)
- {
- image = image_cache;
- image_cache = image->next;
- }
- else
- {
- image = malloc (sizeof (pixman_image_t));
- }
+ // Expand from 32bpp to 64bpp in place.
+ pixman_expand(buffer, (uint32_t*)buffer, PIXMAN_a8r8g8b8, width);
- return image;
+ free(mask8);
}
-static void
-delete_image (pixman_image_t *image)
+pixman_image_t *
+_pixman_image_allocate (void)
{
- image->next = image_cache;
- image_cache = image;
-}
+ pixman_image_t *image = malloc (sizeof (pixman_image_t));
-static pixman_image_t *
-allocate_image (void)
-{
- pixman_image_t *image = new_image();
-
if (image)
{
image_common_t *common = &image->common;
- pixman_region_init (&common->full_region);
- pixman_region_init (&common->clip_region);
- common->src_clip = &common->full_region;
- common->has_client_clip = FALSE;
+ pixman_region32_init (&common->clip_region);
+
+ common->have_clip_region = FALSE;
+ common->clip_sources = FALSE;
common->transform = NULL;
common->repeat = PIXMAN_REPEAT_NONE;
common->filter = PIXMAN_FILTER_NEAREST;
common->ref_count = 1;
common->read_func = NULL;
common->write_func = NULL;
+ common->classify = NULL;
+ common->client_clip = FALSE;
+ common->destroy_func = NULL;
+ common->destroy_data = NULL;
}
return image;
}
+source_pict_class_t
+_pixman_image_classify (pixman_image_t *image,
+ int x,
+ int y,
+ int width,
+ int height)
+{
+ if (image->common.classify)
+ return image->common.classify (image, x, y, width, height);
+ else
+ return SOURCE_IMAGE_CLASS_UNKNOWN;
+}
+
+void
+_pixman_image_get_scanline_32 (pixman_image_t *image, int x, int y, int width,
+ uint32_t *buffer, uint32_t *mask, uint32_t mask_bits)
+{
+ image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
+}
+
+void
+_pixman_image_get_scanline_64 (pixman_image_t *image, int x, int y, int width,
+ uint32_t *buffer, uint32_t *unused, uint32_t unused2)
+{
+ image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
+}
+
+/* Even thought the type of buffer is uint32_t *, the function actually expects
+ * a uint64_t *buffer.
+ */
+
+scanFetchProc
+_pixman_image_get_fetcher (pixman_image_t *image,
+ int wide)
+{
+ assert (image->common.get_scanline_64);
+ assert (image->common.get_scanline_32);
+
+ if (wide)
+ return image->common.get_scanline_64;
+ else
+ return image->common.get_scanline_32;
+}
+
+static void
+image_property_changed (pixman_image_t *image)
+{
+ image->common.property_changed (image);
+}
+
/* Ref Counting */
-pixman_image_t *
+PIXMAN_EXPORT pixman_image_t *
pixman_image_ref (pixman_image_t *image)
{
image->common.ref_count++;
return image;
}
-void
+/* returns TRUE when the image is freed */
+PIXMAN_EXPORT pixman_bool_t
pixman_image_unref (pixman_image_t *image)
{
image_common_t *common = (image_common_t *)image;
if (common->ref_count == 0)
{
- pixman_region_fini (&common->clip_region);
- pixman_region_fini (&common->full_region);
+ if (image->common.destroy_func)
+ image->common.destroy_func (image, image->common.destroy_data);
+
+ pixman_region32_fini (&common->clip_region);
if (common->transform)
free (common->transform);
if (image->type == BITS && image->bits.indexed)
free (image->bits.indexed);
#endif
-
+
#if 0
memset (image, 0xaa, sizeof (pixman_image_t));
#endif
free (image->gradient.stops);
}
-
+
if (image->type == BITS && image->bits.free_me)
free (image->bits.free_me);
-
- delete_image (image);
- }
-}
-
-/* Constructors */
-pixman_image_t *
-pixman_image_create_solid_fill (pixman_color_t *color)
-{
- pixman_image_t *img = allocate_image();
- if (!img)
- return NULL;
-
- init_source_image (&img->solid.common);
-
- img->type = SOLID;
- img->solid.color = color_to_uint32 (color);
-
- return img;
-}
-
-pixman_image_t *
-pixman_image_create_linear_gradient (pixman_point_fixed_t *p1,
- pixman_point_fixed_t *p2,
- const pixman_gradient_stop_t *stops,
- int n_stops)
-{
- pixman_image_t *image;
- linear_gradient_t *linear;
- return_val_if_fail (n_stops >= 2, NULL);
-
- image = allocate_image();
-
- if (!image)
- return NULL;
-
- linear = &image->linear;
-
- if (!init_gradient (&linear->common, stops, n_stops))
- {
free (image);
- return NULL;
- }
- linear->p1 = *p1;
- linear->p2 = *p2;
-
- image->type = LINEAR;
-
- return image;
-}
-
-
-pixman_image_t *
-pixman_image_create_radial_gradient (pixman_point_fixed_t *inner,
- pixman_point_fixed_t *outer,
- pixman_fixed_t inner_radius,
- pixman_fixed_t outer_radius,
- const pixman_gradient_stop_t *stops,
- int n_stops)
-{
- pixman_image_t *image;
- radial_gradient_t *radial;
-
- return_val_if_fail (n_stops >= 2, NULL);
-
- image = allocate_image();
-
- if (!image)
- return NULL;
-
- radial = &image->radial;
-
- if (!init_gradient (&radial->common, stops, n_stops))
- {
- free (image);
- return NULL;
+ return TRUE;
}
- image->type = RADIAL;
-
- radial->c1.x = inner->x;
- radial->c1.y = inner->y;
- radial->c1.radius = inner_radius;
- radial->c2.x = outer->x;
- radial->c2.y = outer->y;
- radial->c2.radius = outer_radius;
- radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
- radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
- radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
- radial->A = (radial->cdx * radial->cdx
- + radial->cdy * radial->cdy
- - radial->dr * radial->dr);
-
- return image;
+ return FALSE;
}
-pixman_image_t *
-pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
- pixman_fixed_t angle,
- const pixman_gradient_stop_t *stops,
- int n_stops)
+PIXMAN_EXPORT void
+pixman_image_set_destroy_function (pixman_image_t *image,
+ pixman_image_destroy_func_t func,
+ void *data)
{
- pixman_image_t *image = allocate_image();
- conical_gradient_t *conical;
-
- if (!image)
- return NULL;
-
- conical = &image->conical;
-
- if (!init_gradient (&conical->common, stops, n_stops))
- {
- free (image);
- return NULL;
- }
-
- image->type = CONICAL;
- conical->center = *center;
- conical->angle = angle;
-
- return image;
+ image->common.destroy_func = func;
+ image->common.destroy_data = data;
}
+
-static uint32_t *
-create_bits (pixman_format_code_t format,
- int width,
- int height,
- int *rowstride_bytes)
-{
- int stride;
- int buf_size;
- int bpp;
-
- bpp = PIXMAN_FORMAT_BPP (format);
- stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
- buf_size = height * stride;
-
- if (rowstride_bytes)
- *rowstride_bytes = stride;
+/* Constructors */
- return calloc (buf_size, 1);
+void
+_pixman_image_reset_clip_region (pixman_image_t *image)
+{
+ image->common.have_clip_region = FALSE;
}
-static void
-reset_clip_region (pixman_image_t *image)
+PIXMAN_EXPORT pixman_bool_t
+pixman_image_set_clip_region32 (pixman_image_t *image,
+ pixman_region32_t *region)
{
- pixman_region_fini (&image->common.clip_region);
-
- if (image->type == BITS)
+ image_common_t *common = (image_common_t *)image;
+ pixman_bool_t result;
+
+ if (region)
{
- pixman_region_init_rect (&image->common.clip_region, 0, 0,
- image->bits.width, image->bits.height);
+ if ((result = pixman_region32_copy (&common->clip_region, region)))
+ image->common.have_clip_region = TRUE;
}
else
{
- pixman_region_init (&image->common.clip_region);
- }
-}
-
-pixman_image_t *
-pixman_image_create_bits (pixman_format_code_t format,
- int width,
- int height,
- uint32_t *bits,
- int rowstride_bytes)
-{
- pixman_image_t *image;
- uint32_t *free_me = NULL;
+ _pixman_image_reset_clip_region (image);
- /* must be a whole number of uint32_t's
- */
- return_val_if_fail (bits == NULL ||
- (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
-
- if (!bits)
- {
- free_me = bits = create_bits (format, width, height, &rowstride_bytes);
- if (!bits)
- return NULL;
+ result = TRUE;
}
-
- image = allocate_image();
-
- if (!image)
- return NULL;
-
- image->type = BITS;
- image->bits.format = format;
- image->bits.width = width;
- image->bits.height = height;
- image->bits.bits = bits;
- image->bits.free_me = free_me;
-
- image->bits.rowstride = rowstride_bytes / sizeof (uint32_t); /* we store it in number
- * of uint32_t's
- */
- image->bits.indexed = NULL;
- pixman_region_fini (&image->common.full_region);
- pixman_region_init_rect (&image->common.full_region, 0, 0,
- image->bits.width, image->bits.height);
+ image_property_changed (image);
- reset_clip_region (image);
- return image;
+ return result;
}
-pixman_bool_t
+
+PIXMAN_EXPORT pixman_bool_t
pixman_image_set_clip_region (pixman_image_t *image,
pixman_region16_t *region)
{
image_common_t *common = (image_common_t *)image;
+ pixman_bool_t result;
if (region)
{
- return pixman_region_copy (&common->clip_region, region);
+ if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
+ image->common.have_clip_region = TRUE;
}
else
{
- reset_clip_region (image);
-
- return TRUE;
+ _pixman_image_reset_clip_region (image);
+
+ result = TRUE;
}
+
+ image_property_changed (image);
+
+ return result;
}
-/* Sets whether the clip region includes a clip region set by the client
- */
-void
+PIXMAN_EXPORT void
pixman_image_set_has_client_clip (pixman_image_t *image,
pixman_bool_t client_clip)
{
- image->common.has_client_clip = client_clip;
+ image->common.client_clip = client_clip;
}
-pixman_bool_t
+PIXMAN_EXPORT pixman_bool_t
pixman_image_set_transform (pixman_image_t *image,
const pixman_transform_t *transform)
{
{ 0, 0, pixman_fixed_1 }
}
};
-
+
image_common_t *common = (image_common_t *)image;
+ pixman_bool_t result;
if (common->transform == transform)
return TRUE;
if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
{
- transform = NULL;
- return TRUE;
+ free(common->transform);
+ common->transform = NULL;
+ result = TRUE;
+ goto out;
}
-
- if (common->transform)
- free (common->transform);
- if (transform)
- {
+ if (common->transform == NULL)
common->transform = malloc (sizeof (pixman_transform_t));
- if (!common->transform)
- return FALSE;
- *common->transform = *transform;
- }
- else
+ if (common->transform == NULL)
{
- common->transform = NULL;
+ result = FALSE;
+ goto out;
}
+ memcpy(common->transform, transform, sizeof(pixman_transform_t));
+
+out:
+ image_property_changed (image);
+
return TRUE;
}
-void
+PIXMAN_EXPORT void
pixman_image_set_repeat (pixman_image_t *image,
pixman_repeat_t repeat)
{
image->common.repeat = repeat;
+
+ image_property_changed (image);
}
-pixman_bool_t
+PIXMAN_EXPORT pixman_bool_t
pixman_image_set_filter (pixman_image_t *image,
pixman_filter_t filter,
const pixman_fixed_t *params,
new_params = NULL;
if (params)
{
- new_params = malloc (n_params * sizeof (pixman_fixed_t));
+ new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
if (!new_params)
return FALSE;
}
common->filter = filter;
-
+
if (common->filter_params)
free (common->filter_params);
common->filter_params = new_params;
common->n_filter_params = n_params;
+
+ image_property_changed (image);
return TRUE;
}
+PIXMAN_EXPORT void
+pixman_image_set_source_clipping (pixman_image_t *image,
+ pixman_bool_t clip_sources)
+{
+ image->common.clip_sources = clip_sources;
+}
+
/* Unlike all the other property setters, this function does not
* copy the content of indexed. Doing this copying is simply
* way, way too expensive.
*/
-void
+PIXMAN_EXPORT void
pixman_image_set_indexed (pixman_image_t *image,
const pixman_indexed_t *indexed)
{
bits_image_t *bits = (bits_image_t *)image;
bits->indexed = indexed;
+
+ image_property_changed (image);
}
-void
+PIXMAN_EXPORT void
pixman_image_set_alpha_map (pixman_image_t *image,
pixman_image_t *alpha_map,
int16_t x,
int16_t y)
{
image_common_t *common = (image_common_t *)image;
-
+
return_if_fail (!alpha_map || alpha_map->type == BITS);
if (common->alpha_map != (bits_image_t *)alpha_map)
common->alpha_map = NULL;
}
- common->alpha_origin.x = x;
- common->alpha_origin.y = y;
+ common->alpha_origin_x = x;
+ common->alpha_origin_y = y;
+
+ image_property_changed (image);
}
-void
+PIXMAN_EXPORT void
pixman_image_set_component_alpha (pixman_image_t *image,
pixman_bool_t component_alpha)
{
image->common.component_alpha = component_alpha;
+
+ image_property_changed (image);
}
-void
+PIXMAN_EXPORT void
pixman_image_set_accessors (pixman_image_t *image,
pixman_read_memory_func_t read_func,
pixman_write_memory_func_t write_func)
image->common.read_func = read_func;
image->common.write_func = write_func;
+
+ image_property_changed (image);
}
-uint32_t *
+PIXMAN_EXPORT uint32_t *
pixman_image_get_data (pixman_image_t *image)
{
if (image->type == BITS)
return NULL;
}
-int
+PIXMAN_EXPORT int
pixman_image_get_width (pixman_image_t *image)
{
if (image->type == BITS)
return 0;
}
-int
+PIXMAN_EXPORT int
pixman_image_get_height (pixman_image_t *image)
{
if (image->type == BITS)
return 0;
}
-int
+PIXMAN_EXPORT int
pixman_image_get_stride (pixman_image_t *image)
{
if (image->type == BITS)
- return image->bits.rowstride * sizeof (uint32_t);
+ return image->bits.rowstride * (int) sizeof (uint32_t);
return 0;
}
-int
+PIXMAN_EXPORT int
pixman_image_get_depth (pixman_image_t *image)
{
if (image->type == BITS)
}
pixman_bool_t
-color_to_pixel (pixman_color_t *color,
- uint32_t *pixel,
- pixman_format_code_t format)
+_pixman_image_is_solid (pixman_image_t *image)
{
- uint32_t c = color_to_uint32 (color);
-
- if (!(format == PIXMAN_a8r8g8b8 ||
- format == PIXMAN_x8r8g8b8 ||
- format == PIXMAN_a8b8g8r8 ||
- format == PIXMAN_x8b8g8r8 ||
- format == PIXMAN_r5g6b5 ||
- format == PIXMAN_b5g6r5 ||
- format == PIXMAN_a8))
+ if (image->type == SOLID)
+ return TRUE;
+
+ if (image->type != BITS ||
+ image->bits.width != 1 ||
+ image->bits.height != 1)
{
return FALSE;
}
-
- if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
- {
- c = ((c & 0xff000000) >> 0) |
- ((c & 0x00ff0000) >> 16) |
- ((c & 0x0000ff00) >> 0) |
- ((c & 0x000000ff) << 16);
- }
- if (format == PIXMAN_a8)
- c = c >> 24;
- else if (format == PIXMAN_r5g6b5 ||
- format == PIXMAN_b5g6r5)
- c = cvt8888to0565 (c);
+ if (image->common.repeat == PIXMAN_REPEAT_NONE)
+ return FALSE;
-#if 0
- printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
- printf ("pixel: %x\n", c);
-#endif
-
- *pixel = c;
return TRUE;
}
-pixman_bool_t
-pixman_image_fill_rectangles (pixman_op_t op,
- pixman_image_t *dest,
- pixman_color_t *color,
- int n_rects,
- const pixman_rectangle16_t *rects)
+uint32_t
+_pixman_image_get_solid (pixman_image_t *image, pixman_format_code_t format)
{
- pixman_image_t *solid;
- pixman_color_t c;
- int i;
+ uint32_t result;
+
+ _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL, 0);
- if (color->alpha == 0xffff)
+ /* If necessary, convert RGB <--> BGR. */
+ if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
{
- if (op == PIXMAN_OP_OVER)
- op = PIXMAN_OP_SRC;
- }
+ result = (((result & 0xff000000) >> 0) |
+ ((result & 0x00ff0000) >> 16) |
+ ((result & 0x0000ff00) >> 0) |
+ ((result & 0x000000ff) << 16));
+ }
+
+ return result;
+}
- if (op == PIXMAN_OP_CLEAR)
- {
- c.red = 0;
- c.green = 0;
- c.blue = 0;
- c.alpha = 0;
+pixman_bool_t
+_pixman_image_is_opaque (pixman_image_t *image)
+{
+ int i;
- color = &c;
-
- op = PIXMAN_OP_SRC;
- }
+ if (image->common.alpha_map)
+ return FALSE;
- if (op == PIXMAN_OP_SRC)
+ switch (image->type)
{
- uint32_t pixel;
+ case BITS:
+ if (image->common.repeat == PIXMAN_REPEAT_NONE)
+ return FALSE;
- if (color_to_pixel (color, &pixel, dest->bits.format))
+ if (PIXMAN_FORMAT_A (image->bits.format))
+ return FALSE;
+ break;
+
+ case LINEAR:
+ case RADIAL:
+ if (image->common.repeat == PIXMAN_REPEAT_NONE)
+ return FALSE;
+
+ for (i = 0; i < image->gradient.n_stops; ++i)
{
- for (i = 0; i < n_rects; ++i)
- {
- pixman_region16_t fill_region;
- int n_boxes, j;
- pixman_box16_t *boxes;
-
- pixman_region_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
- pixman_region_intersect (&fill_region, &fill_region, &dest->common.clip_region);
-
- boxes = pixman_region_rectangles (&fill_region, &n_boxes);
- for (j = 0; j < n_boxes; ++j)
- {
- const pixman_box16_t *box = &(boxes[j]);
- pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
- box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
- pixel);
- }
-
- pixman_region_fini (&fill_region);
- }
- return TRUE;
- }
- }
-
- solid = pixman_image_create_solid_fill (color);
- if (!solid)
- return FALSE;
+ if (image->gradient.stops[i].color.alpha != 0xffff)
+ return FALSE;
+ }
+ break;
- for (i = 0; i < n_rects; ++i)
- {
- const pixman_rectangle16_t *rect = &(rects[i]);
+ case CONICAL:
+ /* Conical gradients always have a transparent border */
+ return FALSE;
+ break;
- pixman_image_composite (op, solid, NULL, dest,
- 0, 0, 0, 0,
- rect->x, rect->y,
- rect->width, rect->height);
+ case SOLID:
+ if (Alpha (image->solid.color) != 0xff)
+ return FALSE;
+ break;
}
-
- pixman_image_unref (solid);
- return TRUE;
+ /* Convolution filters can introduce translucency if the sum of the
+ * weights is lower than 1.
+ */
+ if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
+ return FALSE;
+
+ return TRUE;
}