diff options
-rw-r--r-- | common/Makefile.am | 2 | ||||
-rw-r--r-- | common/backtrace.c | 3 | ||||
-rw-r--r-- | common/canvas_base.c | 214 | ||||
-rw-r--r-- | common/canvas_utils.c | 40 | ||||
-rw-r--r-- | common/canvas_utils.h | 2 | ||||
-rw-r--r-- | common/gdi_canvas.c | 106 | ||||
-rw-r--r-- | common/gl_canvas.c | 33 | ||||
-rw-r--r-- | common/gl_utils.h | 29 | ||||
-rw-r--r-- | common/glc.c | 98 | ||||
-rw-r--r-- | common/log.c | 113 | ||||
-rw-r--r-- | common/log.h | 144 | ||||
-rw-r--r-- | common/lz.c | 37 | ||||
-rw-r--r-- | common/lz.h | 7 | ||||
-rw-r--r-- | common/lz_decompress_tmpl.c | 18 | ||||
-rw-r--r-- | common/macros.h | 30 | ||||
-rw-r--r-- | common/mem.c | 23 | ||||
-rw-r--r-- | common/ogl_ctx.c | 2 | ||||
-rw-r--r-- | common/pixman_utils.c | 165 | ||||
-rw-r--r-- | common/quic.c | 104 | ||||
-rw-r--r-- | common/quic.h | 7 | ||||
-rw-r--r-- | common/quic_family_tmpl.c | 2 | ||||
-rw-r--r-- | common/quic_rgb_tmpl.c | 32 | ||||
-rw-r--r-- | common/quic_tmpl.c | 32 | ||||
-rw-r--r-- | common/region.c | 2 | ||||
-rw-r--r-- | common/ring.h | 26 | ||||
-rw-r--r-- | common/rop3.c | 10 | ||||
-rw-r--r-- | common/spice_common.h | 58 | ||||
-rw-r--r-- | common/sw_canvas.c | 34 |
28 files changed, 820 insertions, 553 deletions
diff --git a/common/Makefile.am b/common/Makefile.am index 4189dc1..e264db5 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -12,6 +12,8 @@ libspice_common_la_SOURCES = \ draw.h \ lines.c \ lines.h \ + log.c \ + log.h \ lz.c \ lz.h \ lz_common.h \ diff --git a/common/backtrace.c b/common/backtrace.c index c8f4626..305bbad 100644 --- a/common/backtrace.c +++ b/common/backtrace.c @@ -21,7 +21,10 @@ * Copyright (C) 2008 Red Hat, Inc. */ +#ifdef HAVE_CONFIG_H #include <config.h> +#endif + #include <unistd.h> #include <stdlib.h> #include <stdio.h> diff --git a/common/canvas_base.c b/common/canvas_base.c index 4b5d7d4..257cbc3 100644 --- a/common/canvas_base.c +++ b/common/canvas_base.c @@ -31,6 +31,7 @@ #include <math.h> #include <spice/macros.h> +#include "log.h" #include "quic.h" #include "lz.h" #include "canvas_base.h" @@ -43,21 +44,6 @@ #include "mutex.h" -#ifndef CANVAS_ERROR -#define CANVAS_ERROR(format, ...) { \ - printf("%s: " format "\n", __FUNCTION__, ## __VA_ARGS__); \ - abort(); \ -} -#endif - -#ifndef WARN -#define WARN(x) printf("warning: %s\n", x) -#endif - -#ifndef DBG -#define DBG(level, format, ...) printf("%s: debug: " format "\n", __FUNCTION__, ## __VA_ARGS__); -#endif - #define ROUND(_x) ((int)floor((_x) + 0.5)) #define IS_IMAGE_LOSSY(descriptor) \ @@ -115,9 +101,9 @@ static inline uint32_t canvas_16bpp_to_32bpp(uint32_t color) static HDC create_compatible_dc() { HDC dc = CreateCompatibleDC(NULL); - if (!dc) { - CANVAS_ERROR("create compatible DC failed"); - } + + spice_return_val_if_fail(dc != NULL, NULL); + return dc; } @@ -403,7 +389,8 @@ static pixman_image_t *canvas_get_quic(CanvasBase *canvas, SpiceImage *image, if (setjmp(quic_data->jmp_env)) { pixman_image_unref(surface); - CANVAS_ERROR("quic error, %s", quic_data->message_buf); + spice_warning("%s", quic_data->message_buf); + return NULL; } quic_data->chunks = image->u.quic.data; @@ -413,10 +400,11 @@ static pixman_image_t *canvas_get_quic(CanvasBase *canvas, SpiceImage *image, (uint32_t *)image->u.quic.data->chunk[0].data, image->u.quic.data->chunk[0].len >> 2, &type, &width, &height) == QUIC_ERROR) { - CANVAS_ERROR("quic decode begin failed"); + spice_warning("quic decode begin failed"); + return NULL; } - switch (type) { + switch (type) { case QUIC_IMAGE_TYPE_RGBA: as_type = QUIC_IMAGE_TYPE_RGBA; pixman_format = PIXMAN_a8r8g8b8; @@ -440,11 +428,12 @@ static pixman_image_t *canvas_get_quic(CanvasBase *canvas, SpiceImage *image, case QUIC_IMAGE_TYPE_INVALID: case QUIC_IMAGE_TYPE_GRAY: default: - CANVAS_ERROR("unexpected image type"); + spice_warn_if_reached(); + return NULL; } - ASSERT((uint32_t)width == image->descriptor.width); - ASSERT((uint32_t)height == image->descriptor.height); + spice_return_val_if_fail((uint32_t)width == image->descriptor.width, NULL); + spice_return_val_if_fail((uint32_t)height == image->descriptor.height, NULL); surface = surface_create( #ifdef WIN32 @@ -453,16 +442,15 @@ static pixman_image_t *canvas_get_quic(CanvasBase *canvas, SpiceImage *image, pixman_format, width, height, FALSE); - if (surface == NULL) { - CANVAS_ERROR("create surface failed"); - } + spice_return_val_if_fail(surface != NULL, NULL); dest = (uint8_t *)pixman_image_get_data(surface); stride = pixman_image_get_stride(surface); if (quic_decode(quic_data->quic, as_type, dest, stride) == QUIC_ERROR) { pixman_image_unref(surface); - CANVAS_ERROR("quic decode failed"); + spice_warning("quic decode failed"); + return NULL; } if (invers) { @@ -518,11 +506,11 @@ static pixman_image_t *canvas_get_jpeg(CanvasBase *canvas, SpiceImage *image, in int height; uint8_t *dest; - ASSERT(image->u.jpeg.data->num_chunks == 1); /* TODO: Handle chunks */ + spice_return_val_if_fail(image->u.jpeg.data->num_chunks == 1, NULL); canvas->jpeg->ops->begin_decode(canvas->jpeg, image->u.jpeg.data->chunk[0].data, image->u.jpeg.data->chunk[0].len, &width, &height); - ASSERT((uint32_t)width == image->descriptor.width); - ASSERT((uint32_t)height == image->descriptor.height); + spice_return_val_if_fail((uint32_t)width == image->descriptor.width, NULL); + spice_return_val_if_fail((uint32_t)height == image->descriptor.height, NULL); surface = surface_create( #ifdef WIN32 @@ -531,7 +519,8 @@ static pixman_image_t *canvas_get_jpeg(CanvasBase *canvas, SpiceImage *image, in PIXMAN_x8r8g8b8, width, height, FALSE); if (surface == NULL) { - CANVAS_ERROR("create surface failed"); + spice_warning("create surface failed"); + return NULL; } dest = (uint8_t *)pixman_image_get_data(surface); @@ -574,13 +563,13 @@ static pixman_image_t *canvas_get_jpeg_alpha(CanvasBase *canvas, int alpha_size; int lz_alpha_width, lz_alpha_height, n_comp_pixels, lz_alpha_top_down; - ASSERT(image->u.jpeg_alpha.data->num_chunks == 1); + spice_return_val_if_fail(image->u.jpeg_alpha.data->num_chunks == 1, NULL); canvas->jpeg->ops->begin_decode(canvas->jpeg, image->u.jpeg_alpha.data->chunk[0].data, image->u.jpeg_alpha.jpeg_size, &width, &height); - ASSERT((uint32_t)width == image->descriptor.width); - ASSERT((uint32_t)height == image->descriptor.height); + spice_return_val_if_fail((uint32_t)width == image->descriptor.width, NULL); + spice_return_val_if_fail((uint32_t)height == image->descriptor.height, NULL); if (image->u.jpeg_alpha.flags & SPICE_JPEG_ALPHA_FLAGS_TOP_DOWN) { alpha_top_down = TRUE; @@ -593,7 +582,8 @@ static pixman_image_t *canvas_get_jpeg_alpha(CanvasBase *canvas, width, height, width*height, alpha_top_down); if (surface == NULL) { - CANVAS_ERROR("create surface failed"); + spice_warning("create surface failed"); + return NULL; } dest = (uint8_t *)pixman_image_get_data(surface); @@ -607,11 +597,11 @@ static pixman_image_t *canvas_get_jpeg_alpha(CanvasBase *canvas, lz_decode_begin(lz_data->lz, comp_alpha_buf, alpha_size, &lz_alpha_type, &lz_alpha_width, &lz_alpha_height, &n_comp_pixels, &lz_alpha_top_down, NULL); - ASSERT(lz_alpha_type == LZ_IMAGE_TYPE_XXXA); - ASSERT(!!lz_alpha_top_down == !!alpha_top_down); - ASSERT(lz_alpha_width == width); - ASSERT(lz_alpha_height == height); - ASSERT(n_comp_pixels == width * height); + spice_return_val_if_fail(lz_alpha_type == LZ_IMAGE_TYPE_XXXA, NULL); + spice_return_val_if_fail(!!lz_alpha_top_down == !!alpha_top_down, NULL); + spice_return_val_if_fail(lz_alpha_width == width, NULL); + spice_return_val_if_fail(lz_alpha_height == height, NULL); + spice_return_val_if_fail(n_comp_pixels == width * height, NULL); if (!alpha_top_down) { decomp_alpha_buf = dest + stride * (height - 1); @@ -664,7 +654,8 @@ static pixman_image_t *canvas_bitmap_to_surface(CanvasBase *canvas, SpiceBitmap* format, bitmap->x, bitmap->y, FALSE); if (image == NULL) { - CANVAS_ERROR("create surface failed"); + spice_warning("create surface failed"); + return NULL; } spice_bitmap_convert_to_pixman(format, image, @@ -724,7 +715,8 @@ static inline SpicePalette *canvas_get_localized_palette(CanvasBase *canvas, Spi break; case SPICE_SURFACE_FMT_16_565: default: - PANIC("Unsupported palette depth"); + spice_warn_if_reached(); + return NULL; } *free_palette = TRUE; return copy; @@ -750,22 +742,24 @@ static pixman_image_t *canvas_get_lz(CanvasBase *canvas, SpiceImage *image, int if (setjmp(lz_data->jmp_env)) { free(decomp_buf); - CANVAS_ERROR("lz error, %s", lz_data->message_buf); + spice_warning("%s", lz_data->message_buf); + return NULL; } free_palette = FALSE; if (image->descriptor.type == SPICE_IMAGE_TYPE_LZ_RGB) { - ASSERT(image->u.lz_rgb.data->num_chunks == 1); /* TODO: Handle chunks */ + spice_return_val_if_fail(image->u.lz_rgb.data->num_chunks == 1, NULL); /* TODO: Handle chunks */ comp_buf = image->u.lz_rgb.data->chunk[0].data; comp_size = image->u.lz_rgb.data->chunk[0].len; palette = NULL; } else if (image->descriptor.type == SPICE_IMAGE_TYPE_LZ_PLT) { - ASSERT(image->u.lz_plt.data->num_chunks == 1); /* TODO: Handle chunks */ + spice_return_val_if_fail(image->u.lz_plt.data->num_chunks == 1, NULL); /* TODO: Handle chunks */ comp_buf = image->u.lz_plt.data->chunk[0].data; comp_size = image->u.lz_plt.data->chunk[0].len; palette = canvas_get_localized_palette(canvas, image->u.lz_plt.palette, image->u.lz_plt.palette_id, image->u.lz_plt.flags, &free_palette); } else { - CANVAS_ERROR("unexpected image type"); + spice_warn_if_reached(); + return NULL; } lz_decode_begin(lz_data->lz, comp_buf, comp_size, &type, @@ -798,13 +792,14 @@ static pixman_image_t *canvas_get_lz(CanvasBase *canvas, SpiceImage *image, int } break; default: - CANVAS_ERROR("unexpected LZ image type"); + spice_warn_if_reached(); + return NULL; } - ASSERT((unsigned)width == image->descriptor.width); - ASSERT((unsigned)height == image->descriptor.height); + spice_return_val_if_fail((unsigned)width == image->descriptor.width, NULL); + spice_return_val_if_fail((unsigned)height == image->descriptor.height, NULL); - ASSERT((image->descriptor.type == SPICE_IMAGE_TYPE_LZ_PLT) || (n_comp_pixels == width * height)); + spice_return_val_if_fail((image->descriptor.type == SPICE_IMAGE_TYPE_LZ_PLT) || (n_comp_pixels == width * height), NULL); #ifdef WIN32 lz_data->decode_data.dc = canvas->dc; #endif @@ -850,9 +845,7 @@ static pixman_image_t *canvas_get_lz(CanvasBase *canvas, SpiceImage *image, int static pixman_image_t *canvas_get_glz_rgb_common(CanvasBase *canvas, uint8_t *data, int want_original) { - if (canvas->glz_data.decoder == NULL) { - CANVAS_ERROR("glz not supported"); - } + spice_return_val_if_fail(canvas->glz_data.decoder != NULL, NULL); canvas->glz_data.decoder->ops->decode(canvas->glz_data.decoder, data, NULL, @@ -867,12 +860,12 @@ static pixman_image_t *canvas_get_glz_rgb_common(CanvasBase *canvas, uint8_t *da static pixman_image_t *canvas_get_glz(CanvasBase *canvas, SpiceImage *image, int want_original) { - ASSERT(image->descriptor.type == SPICE_IMAGE_TYPE_GLZ_RGB); + spice_return_val_if_fail(image->descriptor.type == SPICE_IMAGE_TYPE_GLZ_RGB, NULL); #ifdef WIN32 canvas->glz_data.decode_data.dc = canvas->dc; #endif - ASSERT(image->u.lz_rgb.data->num_chunks == 1); /* TODO: Handle chunks */ + spice_return_val_if_fail(image->u.lz_rgb.data->num_chunks == 1, NULL); /* TODO: Handle chunks */ return canvas_get_glz_rgb_common(canvas, image->u.lz_rgb.data->chunk[0].data, want_original); } @@ -882,11 +875,9 @@ static pixman_image_t *canvas_get_zlib_glz_rgb(CanvasBase *canvas, SpiceImage *i uint8_t *glz_data; pixman_image_t *surface; - if (canvas->zlib == NULL) { - CANVAS_ERROR("zlib not supported"); - } + spice_return_val_if_fail(canvas->zlib != NULL, NULL); - ASSERT(image->u.zlib_glz.data->num_chunks == 1); /* TODO: Handle chunks */ + spice_return_val_if_fail(image->u.zlib_glz.data->num_chunks == 1, NULL); /* TODO: Handle chunks */ glz_data = (uint8_t*)spice_malloc(image->u.zlib_glz.glz_data_size); canvas->zlib->ops->decode(canvas->zlib, image->u.zlib_glz.data->chunk[0].data, image->u.zlib_glz.data->chunk[0].len, @@ -1134,10 +1125,12 @@ static pixman_image_t *canvas_get_image_internal(CanvasBase *canvas, SpiceImage break; } default: - CANVAS_ERROR("invalid image type"); + spice_warn_if_reached(); + return NULL; } - surface_format = spice_pixman_image_get_format(surface); + spice_return_val_if_fail(surface != NULL, NULL); + spice_return_val_if_fail(spice_pixman_image_get_format(surface, &surface_format), NULL); if (descriptor->flags & SPICE_IMAGE_FLAGS_HIGH_BITS_SET && descriptor->type != SPICE_IMAGE_TYPE_FROM_CACHE && @@ -1172,7 +1165,8 @@ static pixman_image_t *canvas_get_image_internal(CanvasBase *canvas, SpiceImage #ifdef SW_CANVAS_CACHE } else if (descriptor->flags & SPICE_IMAGE_FLAGS_CACHE_REPLACE_ME) { if (IS_IMAGE_LOSSY(descriptor)) { - CANVAS_ERROR("invalid cache replace request: the image is lossy"); + spice_warning("invalid cache replace request: the image is lossy"); + return NULL; } canvas->bits_cache->ops->replace_lossy(canvas->bits_cache, descriptor->id, surface); #ifdef DEBUG_DUMP_SURFACE @@ -1249,8 +1243,11 @@ static pixman_image_t *canvas_get_image_internal(CanvasBase *canvas, SpiceImage return canvas_get_bits(canvas, &image->u.bitmap, want_original, &format); } default: - CANVAS_ERROR("invalid image type"); + spice_warn_if_reached(); + return NULL; } + + return NULL; } #endif @@ -1288,9 +1285,7 @@ static pixman_image_t* canvas_get_image_from_self(SpiceCanvas *canvas, surface = pixman_image_create_bits(spice_surface_format_to_pixman (canvas_base->format), width, height, NULL, 0); - if (surface == NULL) { - CANVAS_ERROR("create surface failed"); - } + spice_return_val_if_fail(surface != NULL, NULL); dest = (uint8_t *)pixman_image_get_data(surface); dest_stride = pixman_image_get_stride(surface); @@ -1333,9 +1328,7 @@ static pixman_image_t *canvas_get_bitmap_mask(CanvasBase *canvas, SpiceBitmap* b canvas->dc, #endif PIXMAN_a1, bitmap->x, bitmap->y, TRUE); - if (surface == NULL) { - CANVAS_ERROR("create surface failed"); - } + spice_return_val_if_fail(surface != NULL, NULL); spice_chunks_linearize(bitmap->data); src_line = bitmap->data->chunk[0].data; @@ -1350,7 +1343,7 @@ static pixman_image_t *canvas_get_bitmap_mask(CanvasBase *canvas, SpiceBitmap* b #else if (!(bitmap->flags & SPICE_BITMAP_FLAGS_TOP_DOWN)) { #endif - ASSERT(bitmap->y > 0); + spice_return_val_if_fail(bitmap->y > 0, NULL); dest_line += dest_stride * ((int)bitmap->y - 1); dest_stride = -dest_stride; } @@ -1389,7 +1382,8 @@ static pixman_image_t *canvas_get_bitmap_mask(CanvasBase *canvas, SpiceBitmap* b default: pixman_image_unref(surface); surface = NULL; - CANVAS_ERROR("invalid bitmap format"); + spice_warn_if_reached(); + return NULL; } } else { switch (bitmap->format) { @@ -1420,7 +1414,8 @@ static pixman_image_t *canvas_get_bitmap_mask(CanvasBase *canvas, SpiceBitmap* b default: pixman_image_unref(surface); surface = NULL; - CANVAS_ERROR("invalid bitmap format"); + spice_warn_if_reached(); + return NULL; } } return surface; @@ -1434,12 +1429,10 @@ static inline pixman_image_t *canvas_A1_invers(pixman_image_t *src_surf) uint8_t *src_line, *end_line, *dest_line; int src_stride, line_size, dest_stride; - ASSERT(pixman_image_get_depth(src_surf) == 1); + spice_return_val_if_fail(pixman_image_get_depth(src_surf) == 1, NULL); invers = pixman_image_create_bits(PIXMAN_a1, width, height, NULL, 0); - if (invers == NULL) { - CANVAS_ERROR("create surface failed"); - } + spice_return_val_if_fail(invers != NULL, NULL); src_line = (uint8_t *)pixman_image_get_data(src_surf); src_stride = pixman_image_get_stride(src_surf); @@ -1499,7 +1492,8 @@ static pixman_image_t *canvas_get_mask(CanvasBase *canvas, SpiceQMask *mask, int break; #endif default: - CANVAS_ERROR("invalid image type"); + spice_warn_if_reached(); + return NULL; } #if defined(SW_CANVAS_CACHE) || defined(SW_CANVAS_IMAGE_CACHE) @@ -1523,7 +1517,8 @@ static pixman_image_t *canvas_get_mask(CanvasBase *canvas, SpiceQMask *mask, int static inline void canvas_raster_glyph_box(const SpiceRasterGlyph *glyph, SpiceRect *r) { - ASSERT(r); + spice_return_if_fail(r != NULL); + r->top = glyph->render_pos.y + glyph->glyph_origin.y; r->bottom = r->top + glyph->height; r->left = glyph->render_pos.x + glyph->glyph_origin.x; @@ -1598,8 +1593,8 @@ static void canvas_put_glyph_bits(SpiceRasterGlyph *glyph, int bpp, uint8_t *des //todo: support SPICE_STRING_FLAGS_RASTER_TOP_DOWN canvas_raster_glyph_box(glyph, &glyph_box); - ASSERT(glyph_box.top >= bounds->top && glyph_box.bottom <= bounds->bottom); - ASSERT(glyph_box.left >= bounds->left && glyph_box.right <= bounds->right); + spice_return_if_fail(glyph_box.top >= bounds->top && glyph_box.bottom <= bounds->bottom); + spice_return_if_fail(glyph_box.left >= bounds->left && glyph_box.right <= bounds->right); rect_offset(&glyph_box, -bounds->left, -bounds->top); dest += glyph_box.top * dest_stride; @@ -1660,7 +1655,8 @@ static void canvas_put_glyph_bits(SpiceRasterGlyph *glyph, int bpp, uint8_t *des break; } default: - CANVAS_ERROR("invalid bpp"); + spice_warn_if_reached(); + return; } } @@ -1673,7 +1669,7 @@ static pixman_image_t *canvas_get_str_mask(CanvasBase *canvas, SpiceString *str, int dest_stride; int i; - ASSERT(str->length > 0); + spice_return_val_if_fail(str->length > 0, NULL); glyph = str->glyphs[0]; canvas_raster_glyph_box(glyph, &bounds); @@ -1688,9 +1684,8 @@ static pixman_image_t *canvas_get_str_mask(CanvasBase *canvas, SpiceString *str, str_mask = pixman_image_create_bits((bpp == 1) ? PIXMAN_a1 : PIXMAN_a8, bounds.right - bounds.left, bounds.bottom - bounds.top, NULL, 0); - if (str_mask == NULL) { - CANVAS_ERROR("create surface failed"); - } + spice_return_val_if_fail(str_mask != NULL, NULL); + dest = (uint8_t *)pixman_image_get_data(str_mask); dest_stride = pixman_image_get_stride(str_mask); for (i = 0; i < str->length; i++) { @@ -1713,13 +1708,13 @@ static pixman_image_t *canvas_scale_surface(pixman_image_t *src, const SpiceRect { pixman_image_t *surface; pixman_transform_t transform; + pixman_format_code_t format; double sx, sy; - surface = pixman_image_create_bits(spice_pixman_image_get_format (src), - width, height, NULL, 0); - if (surface == NULL) { - CANVAS_ERROR("create surface failed"); - } + spice_return_val_if_fail(spice_pixman_image_get_format (src, &format), NULL); + + surface = pixman_image_create_bits(format, width, height, NULL, 0); + spice_return_val_if_fail(surface != NULL, NULL); sx = (double)(src_area->right - src_area->left) / width; sy = (double)(src_area->bottom - src_area->top) / height; @@ -1728,7 +1723,7 @@ static pixman_image_t *canvas_scale_surface(pixman_image_t *src, const SpiceRect pixman_image_set_transform (src, &transform); pixman_image_set_repeat(src, PIXMAN_REPEAT_NONE); - ASSERT(scale_mode == SPICE_IMAGE_SCALE_MODE_INTERPOLATE || scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST); + spice_return_val_if_fail(scale_mode == SPICE_IMAGE_SCALE_MODE_INTERPOLATE || scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST, NULL); pixman_image_set_filter(src, (scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST) ?PIXMAN_FILTER_NEAREST : PIXMAN_FILTER_GOOD, NULL, 0); @@ -1746,7 +1741,7 @@ static pixman_image_t *canvas_scale_surface(pixman_image_t *src, const SpiceRect return surface; } -static void quic_usr_error(QuicUsrContext *usr, const char *fmt, ...) +ATTR_PRINTF(2, 3) static void quic_usr_error(QuicUsrContext *usr, const char *fmt, ...) { QuicData *usr_data = (QuicData *)usr; va_list ap; @@ -1758,7 +1753,7 @@ static void quic_usr_error(QuicUsrContext *usr, const char *fmt, ...) longjmp(usr_data->jmp_env, 1); } -static void quic_usr_warn(QuicUsrContext *usr, const char *fmt, ...) +ATTR_PRINTF(2, 3) static void quic_usr_warn(QuicUsrContext *usr, const char *fmt, ...) { QuicData *usr_data = (QuicData *)usr; va_list ap; @@ -1778,7 +1773,7 @@ static void quic_usr_free(QuicUsrContext *usr, void *ptr) free(ptr); } -static void lz_usr_warn(LzUsrContext *usr, const char *fmt, ...) +ATTR_PRINTF(2, 3) static void lz_usr_warn(LzUsrContext *usr, const char *fmt, ...) { LzData *usr_data = (LzData *)usr; va_list ap; @@ -1788,7 +1783,7 @@ static void lz_usr_warn(LzUsrContext *usr, const char *fmt, ...) va_end(ap); } -static void lz_usr_error(LzUsrContext *usr, const char *fmt, ...) +ATTR_PRINTF(2, 3) static void lz_usr_error(LzUsrContext *usr, const char *fmt, ...) { LzData *usr_data = (LzData *)usr; va_list ap; @@ -1901,7 +1896,8 @@ static void canvas_clip_pixman(CanvasBase *canvas, break; } default: - CANVAS_ERROR("invalid clip type"); + spice_warn_if_reached(); + return; } } @@ -2049,6 +2045,8 @@ static void draw_brush(SpiceCanvas *canvas, } } else { tile = canvas_get_image(canvas_base, pattern->pat, FALSE); + spice_return_if_fail(tile != NULL); + if (rop == SPICE_ROP_COPY) { canvas->ops->fill_tiled_rects(canvas, rects, n_rects, tile, offset_x, offset_y); } else { @@ -2064,7 +2062,8 @@ static void draw_brush(SpiceCanvas *canvas, canvas->ops->fill_solid_rects_rop(canvas, rects, n_rects, 0, rop); break; default: - CANVAS_ERROR("invalid brush type"); + spice_warn_if_reached(); + return; } } @@ -2183,6 +2182,8 @@ static void canvas_draw_copy(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceCl } } else { src_image = canvas_get_image(canvas, copy->src_bitmap, FALSE); + spice_return_if_fail(src_image != NULL); + if (rect_is_same_size(bbox, ©->src_area)) { if (rop == SPICE_ROP_COPY) { spice_canvas->ops->blit_image(spice_canvas, &dest_region, @@ -2288,6 +2289,8 @@ static void canvas_draw_transparent(SpiceCanvas *spice_canvas, SpiceRect *bbox, } } else { src_image = canvas_get_image(canvas, transparent->src_bitmap, FALSE); + spice_return_if_fail(src_image != NULL); + if (rect_is_same_size(bbox, &transparent->src_area)) { spice_canvas->ops->colorkey_image(spice_canvas, &dest_region, src_image, @@ -2365,6 +2368,8 @@ static void canvas_draw_alpha_blend(SpiceCanvas *spice_canvas, SpiceRect *bbox, } } else { src_image = canvas_get_image(canvas, alpha_blend->src_bitmap, TRUE); + spice_return_if_fail(src_image != NULL); + if (rect_is_same_size(bbox, &alpha_blend->src_area)) { spice_canvas->ops->blend_image(spice_canvas, &dest_region, alpha_blend->alpha_flags & SPICE_ALPHA_FLAGS_DEST_HAS_ALPHA, @@ -2448,6 +2453,7 @@ static void canvas_draw_opaque(SpiceCanvas *spice_canvas, SpiceRect *bbox, Spice } } else { src_image = canvas_get_image(canvas, opaque->src_bitmap, FALSE); + spice_return_if_fail(src_image != NULL); if (rect_is_same_size(bbox, &opaque->src_area)) { spice_canvas->ops->blit_image(spice_canvas, &dest_region, @@ -2545,6 +2551,8 @@ static void canvas_draw_blend(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceC } } else { src_image = canvas_get_image(canvas, blend->src_bitmap, FALSE); + spice_return_if_fail(src_image != NULL); + if (rect_is_same_size(bbox, &blend->src_area)) { if (rop == SPICE_ROP_COPY) spice_canvas->ops->blit_image(spice_canvas, &dest_region, @@ -3086,7 +3094,8 @@ static void canvas_draw_stroke(SpiceCanvas *spice_canvas, SpiceRect *bbox, gc.tile_offset_y = stroke->brush.u.pattern.pos.y; break; default: - CANVAS_ERROR("invalid brush type"); + spice_warn_if_reached(); + return; } stroke_lines_init(&lines); @@ -3105,7 +3114,7 @@ static void canvas_draw_stroke(SpiceCanvas *spice_canvas, SpiceRect *bbox, } if (seg->flags & SPICE_PATH_BEZIER) { - ASSERT((point - end_point) % 3 == 0); + spice_return_if_fail((point - end_point) % 3 == 0); for (; point + 2 < end_point; point += 3) { stroke_lines_append_bezier(&lines, &point[0], @@ -3186,7 +3195,8 @@ static void canvas_draw_rop3(SpiceCanvas *spice_canvas, SpiceRect *bbox, } if (pixman_image_get_width(s) - src_pos.x < width || pixman_image_get_height(s) - src_pos.y < heigth) { - CANVAS_ERROR("bad src bitmap size"); + spice_critical("bad src bitmap size"); + return; } if (rop3->brush.type == SPICE_BRUSH_TYPE_PATTERN) { SpiceCanvas *_surface_canvas; @@ -3280,7 +3290,7 @@ static void canvas_base_group_end(SpiceCanvas *spice_canvas) static void unimplemented_op(SpiceCanvas *canvas) { - PANIC("unimplemented canvas operation"); + spice_critical("unimplemented canvas operation"); } inline static void canvas_base_init_ops(SpiceCanvasOps *ops) diff --git a/common/canvas_utils.c b/common/canvas_utils.c index 6632942..f61da6e 100644 --- a/common/canvas_utils.c +++ b/common/canvas_utils.c @@ -19,10 +19,9 @@ #include <config.h> #endif +#include "spice_common.h" #include "canvas_utils.h" -#include <spice/macros.h> - #ifdef __GNUC__ #include <stdlib.h> #include <stdio.h> @@ -33,13 +32,6 @@ static int gdi_handlers = 0; #endif -#ifndef CANVAS_ERROR -#define CANVAS_ERROR(format, ...) { \ - printf("%s: " format "\n", __FUNCTION__, ## __VA_ARGS__); \ - abort(); \ -} -#endif - static void release_data(pixman_image_t *image, void *release_data) { PixmanData *data = (PixmanData *)release_data; @@ -65,7 +57,7 @@ pixman_image_add_data(pixman_image_t *image) if (data == NULL) { data = (PixmanData *)calloc(1, sizeof(PixmanData)); if (data == NULL) { - CANVAS_ERROR("out of memory"); + spice_error("out of memory"); } pixman_image_set_destroy_function(image, release_data, @@ -85,17 +77,21 @@ spice_pixman_image_set_format(pixman_image_t *image, data->format = format; } -pixman_format_code_t -spice_pixman_image_get_format(pixman_image_t *image) + +int spice_pixman_image_get_format(pixman_image_t *image, pixman_format_code_t *format) { PixmanData *data; + spice_return_val_if_fail(format != NULL, 0); + data = (PixmanData *)pixman_image_get_destroy_data(image); - if (data != NULL && - data->format != 0) - return data->format; + if (data != NULL && data->format != 0) { + *format = data->format; + return 1; + } - CANVAS_ERROR("Unknown pixman image type"); + spice_warn_if_reached(); + return 0; } static INLINE pixman_image_t *__surface_create_stride(pixman_format_code_t format, int width, int height, @@ -117,7 +113,7 @@ static INLINE pixman_image_t *__surface_create_stride(pixman_format_code_t forma if (surface == NULL) { free(data); - CANVAS_ERROR("create surface failed, out of memory"); + spice_error("create surface failed, out of memory"); } pixman_data = pixman_image_add_data(surface); @@ -182,20 +178,20 @@ pixman_image_t * surface_create(pixman_format_code_t format, int width, int heig nstride = SPICE_ALIGN(width, 32) / 8; break; default: - CANVAS_ERROR("invalid format"); + spice_error("invalid format"); } bitmap_info.inf.bmiHeader.biCompression = BI_RGB; mutex = CreateMutex(NULL, 0, NULL); if (!mutex) { - CANVAS_ERROR("Unable to CreateMutex"); + spice_error("Unable to CreateMutex"); } bitmap = CreateDIBSection(dc, &bitmap_info.inf, 0, (VOID **)&data, NULL, 0); if (!bitmap) { CloseHandle(mutex); - CANVAS_ERROR("Unable to CreateDIBSection"); + spice_error("Unable to CreateDIBSection"); } if (top_down) { @@ -209,7 +205,7 @@ pixman_image_t * surface_create(pixman_format_code_t format, int width, int heig if (surface == NULL) { CloseHandle(mutex); DeleteObject(bitmap); - CANVAS_ERROR("create surface failed, out of memory"); + spice_error("create surface failed, out of memory"); } pixman_data = pixman_image_add_data(surface); pixman_data->format = format; @@ -246,7 +242,7 @@ pixman_image_t * surface_create(pixman_format_code_t format, int width, int heig stride = SPICE_ALIGN(width, 32) / 8; break; default: - CANVAS_ERROR("invalid format"); + spice_error("invalid format"); } stride = -stride; return __surface_create_stride(format, width, height, stride); diff --git a/common/canvas_utils.h b/common/canvas_utils.h index 16ada45..da38e08 100644 --- a/common/canvas_utils.h +++ b/common/canvas_utils.h @@ -43,7 +43,7 @@ typedef struct PixmanData { void spice_pixman_image_set_format(pixman_image_t *image, pixman_format_code_t format); -pixman_format_code_t spice_pixman_image_get_format(pixman_image_t *image); +int spice_pixman_image_get_format(pixman_image_t *image, pixman_format_code_t *format); #ifdef WIN32 diff --git a/common/gdi_canvas.c b/common/gdi_canvas.c index d3e9c7f..e1af10b 100644 --- a/common/gdi_canvas.c +++ b/common/gdi_canvas.c @@ -331,14 +331,14 @@ static void set_path(GdiCanvas *canvas, SpicePath *s) BeginPath(canvas->dc); if (!MoveToEx(canvas->dc, (int)fix_to_double(point->x), (int)fix_to_double(point->y), NULL)) { - CANVAS_ERROR("MoveToEx failed"); + spice_critical("MoveToEx failed"); return; } point++; } if (seg->flags & SPICE_PATH_BEZIER) { - ASSERT((point - end_point) % 3 == 0); + spice_return_if_fail((point - end_point) % 3 == 0); for (; point + 2 < end_point; point += 3) { POINT points[3]; @@ -349,7 +349,7 @@ static void set_path(GdiCanvas *canvas, SpicePath *s) points[2].x = (int)fix_to_double(point[2].x); points[2].y = (int)fix_to_double(point[2].y); if (!PolyBezierTo(canvas->dc, points, 3)) { - CANVAS_ERROR("PolyBezierTo failed"); + spice_critical("PolyBezierTo failed"); return; } } @@ -357,7 +357,7 @@ static void set_path(GdiCanvas *canvas, SpicePath *s) for (; point < end_point; point++) { if (!LineTo(canvas->dc, (int)fix_to_double(point->x), (int)fix_to_double(point->y))) { - CANVAS_ERROR("LineTo failed"); + spice_critical("LineTo failed"); } } } @@ -366,12 +366,12 @@ static void set_path(GdiCanvas *canvas, SpicePath *s) if (seg->flags & SPICE_PATH_CLOSE) { if (!CloseFigure(canvas->dc)) { - CANVAS_ERROR("CloseFigure failed"); + spice_critical("CloseFigure failed"); } } if (!EndPath(canvas->dc)) { - CANVAS_ERROR("EndPath failed"); + spice_critical("EndPath failed"); } } @@ -385,7 +385,7 @@ static void set_scale_mode(GdiCanvas *canvas, uint8_t scale_mode) } else if (scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST) { SetStretchBltMode(canvas->dc, COLORONCOLOR); } else { - CANVAS_ERROR("Unknown ScaleMode"); + spice_critical("Unknown ScaleMode"); } } @@ -394,7 +394,7 @@ static void set_clip(GdiCanvas *canvas, SpiceClip *clip) switch (clip->type) { case SPICE_CLIP_TYPE_NONE: if (SelectClipRgn(canvas->dc, NULL) == ERROR) { - CANVAS_ERROR("SelectClipRgn failed"); + spice_critical("SelectClipRgn failed"); } break; case SPICE_CLIP_TYPE_RECTS: { @@ -416,26 +416,27 @@ static void set_clip(GdiCanvas *canvas, SpiceClip *clip) combaine_hrgn = CreateRectRgn(now->left, now->top, now->right, now->bottom); if (!combaine_hrgn) { - CANVAS_ERROR("Unable to CreateRectRgn"); + spice_critical("Unable to CreateRectRgn"); DeleteObject(main_hrgn); return; } if (CombineRgn(main_hrgn, main_hrgn, combaine_hrgn, RGN_OR) == ERROR) { - CANVAS_ERROR("Unable to CombineRgn"); + spice_critical("Unable to CombineRgn"); DeleteObject(combaine_hrgn); return; } DeleteObject(combaine_hrgn); } if (SelectClipRgn(canvas->dc, main_hrgn) == ERROR) { - CANVAS_ERROR("Unable to SelectClipRgn"); + spice_critical("Unable to SelectClipRgn"); } DeleteObject(main_hrgn); } break; } default: - CANVAS_ERROR("invalid clip type"); + spice_warn_if_reached(); + return; } } @@ -445,7 +446,8 @@ static void copy_bitmap(const uint8_t *src_image, int height, int src_stride, int copy_width = MIN(dest_stride, src_stride); int y = 0; - ASSERT(dest_stride >= 0 && src_stride >= 0); + spice_return_if_fail(dest_stride >= 0 && src_stride >= 0); + while (y < height) { memcpy(dest_bitmap, src_image, copy_width); src_image += src_stride; @@ -533,13 +535,13 @@ static uint8_t *create_bitmap(HBITMAP *bitmap, HBITMAP *prev_bitmap, HDC *dc, *dc = create_compatible_dc(); if (!*dc) { - CANVAS_ERROR("create_compatible_dc() failed"); + spice_critical("create_compatible_dc() failed"); return NULL; } *bitmap = CreateDIBSection(*dc, &bitmap_info.inf, 0, (VOID **)&data, NULL, 0); if (!*bitmap) { - CANVAS_ERROR("Unable to CreateDIBSection"); + spice_critical("Unable to CreateDIBSection"); DeleteDC(*dc); return NULL; } @@ -565,7 +567,8 @@ static uint8_t *create_bitmap(HBITMAP *bitmap, HBITMAP *prev_bitmap, HDC *dc, nstride = width * 4; break; default: - CANVAS_ERROR("invalid bitmap bits size"); + spice_warn_if_reached(); + return; } if (bitmap_data) { @@ -638,7 +641,8 @@ static HBRUSH get_brush(GdiCanvas *canvas, SpiceBrush *brush, RecurciveMutex **b switch (brush->type) { case SPICE_BRUSH_TYPE_SOLID: if (!(hbrush = CreateSolidBrush(get_color_ref(canvas, brush->u.color)))) { - CANVAS_ERROR("CreateSolidBrush failed"); + spice_critical("CreateSolidBrush failed"); + return NULL; } return hbrush; case SPICE_BRUSH_TYPE_PATTERN: { @@ -653,19 +657,21 @@ static HBRUSH get_brush(GdiCanvas *canvas, SpiceBrush *brush, RecurciveMutex **b if (gdi_surface) { bitmap = (HBITMAP)GetCurrentObject(gdi_surface->dc, OBJ_BITMAP); if (!bitmap) { - CANVAS_ERROR("GetCurrentObject failed"); + spice_critical("GetCurrentObject failed"); + return NULL; } *brush_lock = gdi_surface->lock; } else { surface = canvas_get_image(&canvas->base, brush->u.pattern.pat, FALSE); if (!create_bitmap_from_pixman(&bitmap, &prev_bitmap, &dc, surface, 0)) { - CANVAS_ERROR("create_bitmap failed"); + spice_critical("create_bitmap failed"); return NULL; } } if (!(hbrush = CreatePatternBrush(bitmap))) { - CANVAS_ERROR("CreatePatternBrush failed"); + spice_critical("CreatePatternBrush failed"); + return NULL; } if (!gdi_surface) { @@ -677,7 +683,7 @@ static HBRUSH get_brush(GdiCanvas *canvas, SpiceBrush *brush, RecurciveMutex **b case SPICE_BRUSH_TYPE_NONE: return NULL; default: - CANVAS_ERROR("invalid brush type"); + spice_warn_if_reached(); return NULL; } } @@ -692,12 +698,13 @@ static HBRUSH set_brush(HDC dc, HBRUSH hbrush, SpiceBrush *brush) HBRUSH prev_hbrush; prev_hbrush = (HBRUSH)SelectObject(dc, hbrush); if (!SetBrushOrgEx(dc, brush->u.pattern.pos.x, brush->u.pattern.pos.y, NULL)) { - CANVAS_ERROR("SetBrushOrgEx failed"); + spice_critical("SetBrushOrgEx failed"); + return NULL; } return prev_hbrush; } default: - CANVAS_ERROR("invalid brush type"); + spice_warn_if_reached(); return NULL; } } @@ -753,7 +760,8 @@ uint8_t calc_rop3(uint16_t rop3_bits, int brush) if (rop3_bits & SPICE_ROPD_OP_BLACKNESS || rop3_bits & SPICE_ROPD_OP_WHITENESS || rop3_bits & SPICE_ROPD_OP_INVERS) { - CANVAS_ERROR("invalid rop3 type"); + spice_warn_if_reached("invalid rop3 type"); + return 0; } return rop3; } @@ -802,7 +810,8 @@ static struct BitmapData get_mask_bitmap(struct GdiCanvas *canvas, struct SpiceQ _bitmap = (HBITMAP)GetCurrentObject(gdi_surface->dc, OBJ_BITMAP); if (!_bitmap) { - CANVAS_ERROR ("GetCurrentObject failed"); + spice_critical("GetCurrentObject failed"); + return bitmap; } bitmap.dc = gdi_surface->dc; bitmap.hbitmap = _bitmap; @@ -850,13 +859,15 @@ static void gdi_draw_bitmap(HDC dest_dc, const SpiceRect *src, const SpiceRect * (dest->bottom - dest->top) == (src->bottom - src->top)) { if (!BitBlt(dest_dc, dest->left, dest->top, dest->right - dest->left, dest->bottom - dest->top, src_dc, src->left, src->top, rast_oper)) { - CANVAS_ERROR("BitBlt failed"); + spice_critical("BitBlt failed"); + return; } } else { if (!StretchBlt(dest_dc, dest->left, dest->top, dest->right - dest->left, dest->bottom - dest->top, src_dc, src->left, src->top, src->right - src->left, src->bottom - src->top, rast_oper)) { - CANVAS_ERROR("StretchBlt failed"); + spice_critical("StretchBlt failed"); + return; } } } else { @@ -866,7 +877,8 @@ static void gdi_draw_bitmap(HDC dest_dc, const SpiceRect *src, const SpiceRect * dest->bottom - dest->top, src_dc, src->left, src->top, bitmapmask->hbitmap, bitmapmask->pos.x, bitmapmask->pos.y, rast_oper)) { - CANVAS_ERROR("MaskBlt failed"); + spice_critical("MaskBlt failed"); + return; } } } @@ -905,7 +917,7 @@ static void draw_str_mask_bitmap(struct GdiCanvas *canvas, bitmap.hbitmap = (HBITMAP)1; if (!(surface = canvas_get_str_mask(&canvas->base, str, n, &pos))) { - CANVAS_ERROR("unable to canvas_get_str_mask"); + spice_critical("unable to canvas_get_str_mask"); return; } @@ -946,7 +958,8 @@ static void draw_str_mask_bitmap(struct GdiCanvas *canvas, dest_stride = dest_stride * 4; break; default: - CANVAS_ERROR("unsupported bitmap bits size"); + spice_warn_if_reached(); + return; } dest_stride = dest_stride + 3; dest_stride = dest_stride & ~3; @@ -1018,10 +1031,8 @@ static void gdi_canvas_draw_fill(SpiceCanvas *spice_canvas, SpiceRect *bbox, Spi RecurciveLock lock(*canvas->lock); - if (!(brush = get_brush(canvas, &fill->brush, &brush_lock))) { - CANVAS_ERROR("no braash"); - return; - } + brush = get_brush(canvas, &fill->brush, &brush_lock); + spice_return_if_fail(brush != NULL); bitmapmask = get_mask_bitmap(canvas, &fill->mask); @@ -1123,18 +1134,18 @@ static void gdi_canvas_put_image(SpiceCanvas *spice_canvas, HDC dc, const SpiceR rects[i].x2, rects[i].y2); if (!combaine_hrgn) { - CANVAS_ERROR("CreateRectRgn failed"); + spice_critical("CreateRectRgn failed"); DeleteObject(main_hrgn); return; } if (!CombineRgn(main_hrgn, main_hrgn, combaine_hrgn, RGN_OR)) { - CANVAS_ERROR("CombineRgn failed in put_image"); + spice_critical("CombineRgn failed in put_image"); return; } DeleteObject(combaine_hrgn); } if (SelectClipRgn(canvas->dc, main_hrgn) == ERROR) { - CANVAS_ERROR("SelectClipRgn failed in put_image"); + spice_critical("SelectClipRgn failed in put_image"); DeleteObject(main_hrgn); return; } @@ -1238,7 +1249,8 @@ static void gdi_draw_bitmap_alpha(HDC dest_dc, const SpiceRect *src, const Spice if (!AlphaBlend(dest_dc, dest->left, dest->top, dest->right - dest->left, dest->bottom - dest->top, src_dc, src->left, src->top, src->right - src->left, src->bottom - src->top, bf)) { - CANVAS_ERROR("AlphaBlend failed"); + spice_critical("AlphaBlend failed"); + return; } } @@ -1582,7 +1594,7 @@ static void gdi_canvas_draw_text(SpiceCanvas *spice_canvas, SpiceRect *bbox, Spi draw_str_mask_bitmap(canvas, str, 4, &dest, &src, &text->fore_brush); } else if (str->flags & SPICE_STRING_FLAGS_RASTER_A8) { - WARN("untested path A8 glyphs, doing nothing"); + spice_warning("untested path A8 glyphs, doing nothing"); if (0) { SpiceRect dest; SpiceRect src; @@ -1590,7 +1602,7 @@ static void gdi_canvas_draw_text(SpiceCanvas *spice_canvas, SpiceRect *bbox, Spi draw_str_mask_bitmap(canvas, str, 8, &dest, &src, &text->fore_brush); } } else { - WARN("untested path vector glyphs, doing nothing"); + spice_warning("untested path vector glyphs, doing nothing"); if (0) { } } @@ -1601,9 +1613,8 @@ static uint32_t *gdi_get_userstyle(GdiCanvas *canvas, uint8_t nseg, SPICE_FIXED2 uint32_t *local_style; int i; - if (nseg == 0) { - CANVAS_ERROR("bad nseg"); - } + spice_return_val_if_fail(nseg != 0, NULL); + local_style = spice_new(uint32_t , nseg); if (start_is_gap) { @@ -1701,9 +1712,8 @@ static void gdi_canvas_draw_stroke(SpiceCanvas *spice_canvas, SpiceRect *bbox, S } bitmap_info; GdiImage image; #endif - //CANVAS_ERROR("untested path stroke brush with pattern"); #if 0 - ASSERT(surface) + spice_return_if_fail(surface != NULL) surface_to_image(surface, &image); memset(&bitmap_info, 0, sizeof(bitmap_info)); @@ -1722,7 +1732,8 @@ static void gdi_canvas_draw_stroke(SpiceCanvas *spice_canvas, SpiceRect *bbox, S logbrush.lbHatch = (LONG)GlobalAlloc(GMEM_MOVEABLE, image.height * -image.stride + sizeof(BITMAPINFO)); if (!logbrush.lbHatch) { - CANVAS_ERROR("GlobalAlloc failed"); + spice_critical("GlobalAlloc failed"); + return; } copy_bitmap(image.pixels - (image.height - 1) * -image.stride, image.height, -image.stride, @@ -1731,7 +1742,8 @@ static void gdi_canvas_draw_stroke(SpiceCanvas *spice_canvas, SpiceRect *bbox, S logbrush.lbHatch = (LONG)GlobalAlloc(GMEM_MOVEABLE, image.height * image.stride + sizeof(BITMAPINFO)); if (!logbrush.lbHatch) { - CANVAS_ERROR("GlobalAlloc failed"); + spice_critical("GlobalAlloc failed"); + return; } copy_bitmap(image.pixels, image.height, image.stride, (uint8_t *)logbrush.lbHatch, image.width); diff --git a/common/gl_canvas.c b/common/gl_canvas.c index ffc6b52..d30bf1b 100644 --- a/common/gl_canvas.c +++ b/common/gl_canvas.c @@ -94,7 +94,8 @@ static pixman_image_t *canvas_surf_to_trans_surf(GLCImage *image, ret = pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height, NULL, 0); if (ret == NULL) { - CANVAS_ERROR("create surface failed"); + spice_critical("create surface failed"); + return NULL; } src_line = image->pixels; @@ -133,7 +134,7 @@ static GLCPath get_path(GLCanvas *canvas, SpicePath *s) } if (seg->flags & SPICE_PATH_BEZIER) { - ASSERT((point - end_point) % 3 == 0); + spice_return_val_if_fail((point - end_point) % 3 == 0, path); for (; point + 2 < end_point; point += 3) { glc_path_curve_to(path, fix_to_double(point[0].x), fix_to_double(point[0].y), @@ -199,7 +200,8 @@ static void set_clip(GLCanvas *canvas, SpiceRect *bbox, SpiceClip *clip) break; } default: - CANVAS_ERROR("invalid clip type"); + spice_warn_if_reached(); + return; } } @@ -225,7 +227,7 @@ static inline void surface_to_image(GLCanvas *canvas, pixman_image_t *surface, G { int depth = pixman_image_get_depth(surface); - ASSERT(depth == 32 || depth == 24); + spice_return_if_fail(depth == 32 || depth == 24); image->format = (depth == 24) ? GLC_IMAGE_RGB32 : GLC_IMAGE_ARGB32; image->width = pixman_image_get_width(surface); image->height = pixman_image_get_height(surface); @@ -276,7 +278,8 @@ static void set_brush(GLCanvas *canvas, SpiceBrush *brush) case SPICE_BRUSH_TYPE_NONE: return; default: - CANVAS_ERROR("invalid brush type"); + spice_warn_if_reached(); + return; } } @@ -334,7 +337,7 @@ static void set_op(GLCanvas *canvas, uint16_t rop_decriptor) op = GLC_OP_OR_INVERTED; break; default: - WARN("GLC_OP_NOOP"); + spice_warning("GLC_OP_NOOP"); op = GLC_OP_NOOP; } glc_set_op(canvas->glc, op); @@ -530,7 +533,8 @@ static void gl_canvas_draw_rop3(SpiceCanvas *spice_canvas, SpiceRect *bbox, Spic d = pixman_image_create_bits(PIXMAN_x8r8g8b8, image.width, image.height, NULL, 0); if (d == NULL) { - CANVAS_ERROR("create surface failed"); + spice_critical("create surface failed"); + return; } image.pixels = (uint8_t *)pixman_image_get_data(d); image.stride = pixman_image_get_stride(d); @@ -565,7 +569,8 @@ static void gl_canvas_draw_rop3(SpiceCanvas *spice_canvas, SpiceRect *bbox, Spic if (pixman_image_get_width(s) - src_pos.x < image.width || pixman_image_get_height(s) - src_pos.y < image.height) { - CANVAS_ERROR("bad src bitmap size"); + spice_critical("bad src bitmap size"); + return; } if (rop3->brush.type == SPICE_BRUSH_TYPE_PATTERN) { @@ -619,7 +624,7 @@ static void gl_canvas_draw_stroke(SpiceCanvas *spice_canvas, SpiceRect *bbox, Sp set_brush(canvas, &stroke->brush); if (stroke->attr.flags & SPICE_LINE_FLAGS_STYLED) { - WARN("SPICE_LINE_FLAGS_STYLED"); + spice_warning("SPICE_LINE_FLAGS_STYLED"); } glc_set_line_width(canvas->glc, 1.0); @@ -667,7 +672,7 @@ static void gl_canvas_draw_text(SpiceCanvas *spice_canvas, SpiceRect *bbox, Spic pixman_image_unref(mask); } else if (str->flags & SPICE_STRING_FLAGS_RASTER_A8) { - WARN("untested path A8 glyphs, doing nothing"); + spice_warning("untested path A8 glyphs, doing nothing"); if (0) { SpicePoint pos; pixman_image_t *mask = canvas_get_str_mask(&canvas->base, str, 8, &pos); @@ -679,7 +684,7 @@ static void gl_canvas_draw_text(SpiceCanvas *spice_canvas, SpiceRect *bbox, Spic pixman_image_unref(mask); } } else { - WARN("untested path vector glyphs, doing nothing"); + spice_warning("untested path vector glyphs, doing nothing"); if (0) { //draw_vector_str(canvas, str, &text->fore_brush, text->fore_mode); } @@ -709,7 +714,8 @@ static void gl_canvas_read_bits(SpiceCanvas *spice_canvas, uint8_t *dest, int de GLCanvas *canvas = (GLCanvas *)spice_canvas; GLCImage image; - ASSERT(dest_stride > 0); + spice_return_if_fail(dest_stride > 0); + image.format = GLC_IMAGE_RGB32; image.height = area->bottom - area->top; image.width = area->right - area->left; @@ -752,7 +758,8 @@ static void gl_canvas_put_image(SpiceCanvas *spice_canvas, const SpiceRect *dest GLCImage image; uint32_t i; - ASSERT(src_stride <= 0) + spice_return_if_fail(src_stride <= 0); + glc_clip_reset(canvas->glc); if (clip) { diff --git a/common/gl_utils.h b/common/gl_utils.h index c30be16..c576255 100644 --- a/common/gl_utils.h +++ b/common/gl_utils.h @@ -20,28 +20,29 @@ #ifndef GL_UTILS_H #define GL_UTILS_H +#include "spice_common.h" #ifdef __cplusplus extern "C" { #endif #ifdef RED_DEBUG -#define GLC_ERROR_TEST_FLUSH { \ - GLenum gl_err; glFlush(); \ - if ((gl_err = glGetError()) != GL_NO_ERROR) { \ - printf("%s[%d]: opengl error: %s\n", __FUNCTION__, __LINE__, \ - gluErrorString(gl_err)); \ - abort(); \ - } \ +#define GLC_ERROR_TEST_FLUSH { \ + GLenum gl_err; glFlush(); \ + if ((gl_err = glGetError()) != GL_NO_ERROR) { \ + printf("%s[%d]: opengl error: %s\n", __FUNCTION__, __LINE__, \ + gluErrorString(gl_err)); \ + spice_abort(); \ + } \ } -#define GLC_ERROR_TEST_FINISH { \ - GLenum gl_err; glFinish(); \ - if ((gl_err = glGetError()) != GL_NO_ERROR) { \ - printf("%s[%d]: opengl error: %s\n", __FUNCTION__, __LINE__, \ - gluErrorString(gl_err)); \ - abort(); \ - } \ +#define GLC_ERROR_TEST_FINISH { \ + GLenum gl_err; glFinish(); \ + if ((gl_err = glGetError()) != GL_NO_ERROR) { \ + printf("%s[%d]: opengl error: %s\n", __FUNCTION__, __LINE__, \ + gluErrorString(gl_err)); \ + spice_abort(); \ + } \ } #else #define GLC_ERROR_TEST_FLUSH ; diff --git a/common/glc.c b/common/glc.c index 1414bcf..91b295c 100644 --- a/common/glc.c +++ b/common/glc.c @@ -305,7 +305,7 @@ void glc_path_move_to(GLCPath path, double x, double y) { InternalPath *internal = (InternalPath *)path; - ASSERT(internal); + spice_assert(internal); if (internal->current_segment) { internal->current_segment = NULL; @@ -361,7 +361,7 @@ void glc_path_line_to(GLCPath path, double x, double y) { InternalPath *internal = (InternalPath *)path; - ASSERT(internal); + spice_assert(internal); add_segment_common(internal, GLC_PATH_SEG_LINES, 1); put_point(internal, x, y); @@ -372,7 +372,7 @@ void glc_path_curve_to(GLCPath path, double p1_x, double p1_y, double p2_x, doub { InternalPath *internal = (InternalPath *)path; - ASSERT(internal); + spice_assert(internal); add_segment_common(internal, GLC_PATH_SEG_BEIZER, 3); put_point(internal, p1_x, p1_y); @@ -384,7 +384,7 @@ void glc_path_close(GLCPath path) { InternalPath *internal = (InternalPath *)path; - ASSERT(internal); + spice_assert(internal); if (!internal->current_path) { return; } @@ -397,7 +397,7 @@ void glc_path_cleare(GLCPath path) { InternalPath *internal = (InternalPath *)path; - ASSERT(internal); + spice_assert(internal); internal->paths_pos = internal->segments_pos = 0; internal->current_segment = NULL; internal->current_path = NULL; @@ -412,7 +412,7 @@ GLCPath glc_path_create(GLCCtx glc) InternaCtx *ctx = (InternaCtx *)glc; InternalPath *path; - ASSERT(ctx); + spice_assert(ctx); path = spice_new0(InternalPath, 1); path->paths_size = 2; path->paths = spice_new(Path, path->paths_size); @@ -447,7 +447,7 @@ static inline void unref_pat(InternalPat *pat) if (!pat) { return; } - ASSERT(pat->refs > 0); + spice_assert(pat->refs > 0); if (--pat->refs == 0) { glFinish(); glDeleteTextures(1, &pat->texture); @@ -498,16 +498,16 @@ static inline void init_pattern(InternalPat *pat, int x_orign, int y_orign, cons const int pix_bytes = 4; - ASSERT(image->format == GLC_IMAGE_RGB32); //for now + spice_assert(image->format == GLC_IMAGE_RGB32); //for now width = image->width; height = image->height; width2 = gl_get_to_power_two(width); height2 = gl_get_to_power_two(height); - ASSERT(width > 0 && height > 0); - ASSERT(width > 0 && width <= pat->owner->max_texture_size); - ASSERT(height > 0 && height <= pat->owner->max_texture_size); + spice_assert(width > 0 && height > 0); + spice_assert(width > 0 && width <= pat->owner->max_texture_size); + spice_assert(height > 0 && height <= pat->owner->max_texture_size); if (width2 != width || height2 != height) { tmp_pixmap = (uint32_t *)spice_malloc(width2 * height2 * sizeof(uint32_t)); @@ -529,7 +529,7 @@ static inline void init_pattern(InternalPat *pat, int x_orign, int y_orign, cons tmp_pixmap); free(tmp_pixmap); } else { - ASSERT(image->stride % pix_bytes == 0); + spice_assert(image->stride % pix_bytes == 0); glPixelStorei(GL_UNPACK_ROW_LENGTH, image->stride / pix_bytes); glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, image->pixels); @@ -553,7 +553,7 @@ GLCPattern glc_pattern_create(GLCCtx glc, int x_orign, int y_orign, const GLCIma InternaCtx *ctx = (InternaCtx *)glc; InternalPat *pat; - ASSERT(ctx && image); + spice_assert(ctx && image); pat = spice_new0(InternalPat, 1); pat->refs = 1; @@ -566,7 +566,7 @@ GLCPattern glc_pattern_create(GLCCtx glc, int x_orign, int y_orign, const GLCIma void glc_pattern_set(GLCPattern pattern, int x_orign, int y_orign, const GLCImage *image) { InternalPat *pat = (InternalPat *)pattern; - ASSERT(pat && pat->owner); + spice_assert(pat && pat->owner); glFinish(); init_pattern(pat, x_orign, y_orign, image); @@ -603,7 +603,7 @@ void glc_set_pattern(GLCCtx glc, GLCPattern pattern) InternaCtx *ctx = (InternaCtx *)glc; InternalPat *pat = (InternalPat *)pattern; - ASSERT(ctx && pat && pat->owner == ctx); + spice_assert(ctx && pat && pat->owner == ctx); set_pat(ctx, pat); } @@ -611,7 +611,7 @@ void glc_set_rgb(GLCCtx glc, double red, double green, double blue) { InternaCtx *ctx = (InternaCtx *)glc; - ASSERT(ctx); + spice_assert(ctx); glDisable(GL_TEXTURE_2D); unref_pat(ctx->pat); @@ -634,7 +634,7 @@ void glc_set_line_width(GLCCtx glc, double width) { InternaCtx *ctx = (InternaCtx *)glc; - ASSERT(ctx); + spice_assert(ctx); ctx->line_width = (GLfloat)width; if (ctx->line_width > 0) { glLineWidth(ctx->line_width); @@ -648,7 +648,7 @@ void glc_set_line_dash(GLCCtx glc, const double *dashes, int num_dashes, double { InternaCtx *ctx = (InternaCtx *)glc; - ASSERT(ctx); + spice_assert(ctx); if (dashes && num_dashes >= 0 && offset >= 0) { ctx->line_dash.dashes = spice_new(double, num_dashes); memcpy(ctx->line_dash.dashes, dashes, sizeof(double) * num_dashes); @@ -666,7 +666,7 @@ void glc_set_fill_mode(GLCCtx glc, GLCFillMode fill_mode) { InternaCtx *ctx = (InternaCtx *)glc; - ASSERT(ctx); + spice_assert(ctx); int mode; switch (fill_mode) { case GLC_FILL_MODE_WINDING_ODD: @@ -703,8 +703,8 @@ void glc_set_mask(GLCCtx glc, int x_dest, int y_dest, int width, int height, { InternaCtx *ctx = (InternaCtx *)glc; uint32_t mask = (id == GLC_MASK_A) ? 0x04 : 0x08; - ASSERT(ctx && bitmap); - ASSERT(id == GLC_MASK_A || id == GLC_MASK_B); + spice_assert(ctx && bitmap); + spice_assert(id == GLC_MASK_A || id == GLC_MASK_B); if (ctx->pat) { glDisable(GL_TEXTURE_2D); @@ -732,8 +732,8 @@ void glc_mask_rects(GLCCtx glc, int num_rect, GLCRect *rects, GLCMaskID id) InternaCtx *ctx = (InternaCtx *)glc; uint32_t mask = (id == GLC_MASK_A) ? 0x04 : 0x08; GLCRect *end; - ASSERT(ctx && rects); - ASSERT(id == GLC_MASK_A || id == GLC_MASK_B); + spice_assert(ctx && rects); + spice_assert(id == GLC_MASK_A || id == GLC_MASK_B); if (ctx->pat) { glDisable(GL_TEXTURE_2D); @@ -763,8 +763,8 @@ void glc_clear_mask(GLCCtx glc, GLCMaskID id) { InternaCtx *ctx = (InternaCtx *)glc; uint32_t mask = (id == GLC_MASK_A) ? 0x04 : 0x08; - ASSERT(ctx); - ASSERT(id == GLC_MASK_A || id == GLC_MASK_B); + spice_assert(ctx); + spice_assert(id == GLC_MASK_A || id == GLC_MASK_B); if ((ctx->stencil_mask & mask)) { ctx->stencil_mask &= ~mask; @@ -855,7 +855,7 @@ void glc_clip_rect(GLCCtx glc, const GLCRect *rect, GLCClipOp op) { InternaCtx *ctx = (InternaCtx *)glc; - ASSERT(ctx && rect); + spice_assert(ctx && rect); clip_common(ctx, op, fill_rect, (void *)rect); } @@ -863,7 +863,7 @@ void glc_clip_path(GLCCtx glc, GLCPath path, GLCClipOp op) { InternaCtx *ctx = (InternaCtx *)glc; - ASSERT(ctx && path); + spice_assert(ctx && path); clip_common(ctx, op, fill_path, path); } @@ -889,7 +889,7 @@ void glc_clip_mask(GLCCtx glc, int x_dest, int y_dest, int width, int height, InternaCtx *ctx = (InternaCtx *)glc; FillMaskInfo mask_info; - ASSERT(ctx && bitmap); + spice_assert(ctx && bitmap); mask_info.x_dest = x_dest; mask_info.y_dest = y_dest; mask_info.width = width; @@ -934,7 +934,7 @@ void glc_fill_rect(GLCCtx glc, const GLCRect *rect) InternaCtx *ctx = (InternaCtx *)glc; GLCRect *r = (GLCRect *)rect; // to avoid bugs in gcc older than 4.3 - ASSERT(ctx); + spice_assert(ctx); start_draw(ctx); fill_rect(ctx, (void *)r); GLC_ERROR_TEST_FLUSH; @@ -970,7 +970,7 @@ static void fill_path(InternaCtx *ctx, void *p) (GLdouble *)¤t_point[2]); } } else { - ASSERT(current_segment->type == GLC_PATH_SEG_LINES); + spice_assert(current_segment->type == GLC_PATH_SEG_LINES); end_point = current_point + current_segment->count; for (; current_point < end_point; current_point++) { gluTessVertex(ctx->tesselator, (GLdouble *)current_point, @@ -987,7 +987,7 @@ void glc_fill_path(GLCCtx glc, GLCPath path_ref) { InternaCtx *ctx = (InternaCtx *)glc; - ASSERT(ctx && path_ref); + spice_assert(ctx && path_ref); start_draw(ctx); fill_path(ctx, path_ref); } @@ -1005,10 +1005,10 @@ void _glc_fill_mask(GLCCtx glc, int x_dest, int y_dest, int width, int height, i { InternaCtx *ctx = (InternaCtx *)glc; - ASSERT(ctx && bitmap); + spice_assert(ctx && bitmap); start_draw(ctx); if (ctx->pat) { - WARN_ONCE("%s: unimplemented fill mask with pattern\n", __FUNCTION__); + spice_critical("unimplemented fill mask with pattern"); } fill_mask(ctx, x_dest, y_dest, width, height, stride, bitmap); } @@ -1019,7 +1019,7 @@ void glc_fill_alpha(GLCCtx glc, int x_dest, int y_dest, int width, int height, i InternaCtx *ctx = (InternaCtx *)glc; GLCRect r; - ASSERT(ctx); + spice_assert(ctx); start_draw(ctx); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE); @@ -1045,7 +1045,7 @@ void glc_stroke_rect(GLCCtx glc, const GLCRect *rect) { InternaCtx *ctx = (InternaCtx *)glc; - ASSERT(ctx); + spice_assert(ctx); if (ctx->line_width == 0) { return; } @@ -1151,7 +1151,7 @@ static void glc_vertex2d(InternaCtx *ctx, double x, double y) ctx->path_stroke.y = y; ctx->path_stroke.state = GLC_STROKE_ACTIVE; } else { - ASSERT(ctx->path_stroke.state == GLC_STROKE_NONACTIVE); + spice_assert(ctx->path_stroke.state == GLC_STROKE_NONACTIVE); //error } } @@ -1173,7 +1173,7 @@ void glc_stroke_path(GLCCtx glc, GLCPath path_ref) InternaCtx *ctx = (InternaCtx *)glc; InternalPath *path = (InternalPath *)path_ref; - ASSERT(ctx && path); + spice_assert(ctx && path); if (ctx->line_width == 0) { return; } @@ -1202,7 +1202,7 @@ void glc_stroke_path(GLCCtx glc, GLCPath path_ref) glc_vertex2d(ctx, current_point[2].x, current_point[2].y); } } else { - ASSERT(current_segment->type == GLC_PATH_SEG_LINES); + spice_assert(current_segment->type == GLC_PATH_SEG_LINES); end_point = current_point + current_segment->count; for (; current_point < end_point; current_point++) { glc_vertex2d(ctx, current_point->x, current_point->y); @@ -1220,10 +1220,10 @@ void glc_draw_image(GLCCtx glc, const GLCRecti *dest, const GLCRecti *src, const uint8_t *pixels; const int pix_bytes = 4; - ASSERT(ctx && image); - ASSERT(src->width > 0 && src->height > 0); + spice_assert(ctx && image); + spice_assert(src->width > 0 && src->height > 0); - ASSERT(image->format == GLC_IMAGE_RGB32 || image->format == GLC_IMAGE_ARGB32); //for now + spice_assert(image->format == GLC_IMAGE_RGB32 || image->format == GLC_IMAGE_ARGB32); //for now start_draw(ctx); if (ctx->pat) { glDisable(GL_TEXTURE_2D); @@ -1242,7 +1242,7 @@ void glc_draw_image(GLCCtx glc, const GLCRecti *dest, const GLCRecti *src, const glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); } - ASSERT(image->stride % pix_bytes == 0); + spice_assert(image->stride % pix_bytes == 0); glPixelStorei(GL_UNPACK_ROW_LENGTH, image->stride / pix_bytes); glDrawPixels(src->width, src->height, GL_BGRA, GL_UNSIGNED_BYTE, pixels); @@ -1262,7 +1262,7 @@ void glc_copy_pixels(GLCCtx glc, int x_dest, int y_dest, int x_src, int y_src, i InternaCtx *ctx = (InternaCtx *)glc; int recreate = 0; - ASSERT(ctx); + spice_assert(ctx); #ifdef USE_COPY_PIXELS start_draw(ctx); if (ctx->pat) { @@ -1303,7 +1303,7 @@ void glc_copy_pixels(GLCCtx glc, int x_dest, int y_dest, int x_src, int y_src, i glTexImage2D(GL_TEXTURE_2D, 0, 4, ctx->private_tex_width, ctx->private_tex_height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL); } - ASSERT(ctx->private_tex); + spice_assert(ctx->private_tex); glBindTexture(GL_TEXTURE_2D, ctx->private_tex); glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, x_src, ctx->height - (y_src + height), width2, height2, 0); @@ -1332,9 +1332,9 @@ void glc_read_pixels(GLCCtx glc, int x, int y, GLCImage *image) { InternaCtx *ctx = (InternaCtx *)glc; - ASSERT(ctx && image); - ASSERT(image->format == GLC_IMAGE_RGB32); //for now - ASSERT((image->stride % 4) == 0); //for now + spice_assert(ctx && image); + spice_assert(image->format == GLC_IMAGE_RGB32); //for now + spice_assert((image->stride % 4) == 0); //for now glPixelStorei(GL_PACK_ROW_LENGTH, image->stride / 4); glReadPixels(x, ctx->height - (y + image->height), image->width, image->height, GL_BGRA, GL_UNSIGNED_BYTE, image->pixels); @@ -1344,7 +1344,7 @@ void glc_clear(GLCCtx glc) { InternaCtx *ctx = (InternaCtx *)glc; - ASSERT(ctx); + spice_assert(ctx); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glClear(GL_COLOR_BUFFER_BIT); } @@ -1458,7 +1458,7 @@ GLCCtx glc_create(int width, int height) { InternaCtx *ctx; - ASSERT(sizeof(PathPoint) == sizeof(Vertex)); + spice_static_assert(sizeof(PathPoint) == sizeof(Vertex)); ctx = spice_new0(InternaCtx, 1); if (!init(ctx, width, height)) { diff --git a/common/log.c b/common/log.c new file mode 100644 index 0000000..ef4da06 --- /dev/null +++ b/common/log.c @@ -0,0 +1,113 @@ +/* + Copyright (C) 2012 Red Hat, Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdlib.h> +#include <stdio.h> +#include <sys/types.h> +#include <unistd.h> + +#include "log.h" +#include "backtrace.h" + +static int debug_level = -1; +static int abort_level = -1; + +static const char * spice_log_level_to_string(SpiceLogLevel level) +{ + static const char *to_string[] = { + [ SPICE_LOG_LEVEL_ERROR ] = "ERROR", + [ SPICE_LOG_LEVEL_CRITICAL ] = "CRITICAL", + [ SPICE_LOG_LEVEL_WARNING ] = "Warning", + [ SPICE_LOG_LEVEL_INFO ] = "Info", + [ SPICE_LOG_LEVEL_DEBUG ] = "Debug", + }; + const char *str = NULL; + + if (level < SPICE_N_ELEMENTS(to_string)) { + str = to_string[level]; + } + + return str; +} + +#ifndef SPICE_ABORT_LEVEL_DEFAULT +#ifdef SPICE_DISABLE_ABORT +#define SPICE_ABORT_LEVEL_DEFAULT -1 +#else +#define SPICE_ABORT_LEVEL_DEFAULT SPICE_LOG_LEVEL_CRITICAL +#endif +#endif + +void spice_logv(const char *log_domain, + SpiceLogLevel log_level, + const char *strloc, + const char *function, + const char *format, + va_list args) +{ + const char *level = spice_log_level_to_string(log_level); + + if (debug_level == -1) { + debug_level = getenv("SPICE_DEBUG_LEVEL") ? atoi(getenv("SPICE_DEBUG_LEVEL")) : SPICE_LOG_LEVEL_WARNING; + } + if (abort_level == -1) { + abort_level = getenv("SPICE_ABORT_LEVEL") ? atoi(getenv("SPICE_ABORT_LEVEL")) : SPICE_ABORT_LEVEL_DEFAULT; + } + + if (debug_level < log_level) + return; + + fprintf(stderr, "(%s:%d): ", getenv("_"), getpid()); + + if (log_domain) { + fprintf(stderr, "%s-", log_domain); + } + if (level) { + fprintf(stderr, "%s **: ", level); + } + if (strloc && function) { + fprintf(stderr, "%s:%s: ", strloc, function); + } + if (format) { + vfprintf(stderr, format, args); + } + + fprintf(stderr, "\n"); + + if (abort_level != -1 && abort_level >= log_level) { + spice_backtrace(); + abort(); + } +} + +void spice_log(const char *log_domain, + SpiceLogLevel log_level, + const char *strloc, + const char *function, + const char *format, + ...) +{ + va_list args; + + va_start (args, format); + spice_logv (log_domain, log_level, strloc, function, format, args); + va_end (args); +} diff --git a/common/log.h b/common/log.h new file mode 100644 index 0000000..949879e --- /dev/null +++ b/common/log.h @@ -0,0 +1,144 @@ +/* + Copyright (C) 2012 Red Hat, Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef H_SPICE_LOG +#define H_SPICE_LOG + +#include <spice/macros.h> +#include <stdarg.h> + +SPICE_BEGIN_DECLS + +#ifndef SPICE_LOG_DOMAIN +#define SPICE_LOG_DOMAIN "Spice" +#endif + +#define SPICE_STRINGIFY(x) SPICE_STRINGIFY_ARG (x) +#define SPICE_STRINGIFY_ARG(x) #x + +#define SPICE_STRLOC __FILE__ ":" SPICE_STRINGIFY (__LINE__) + +typedef enum { + SPICE_LOG_LEVEL_ERROR, + SPICE_LOG_LEVEL_CRITICAL, + SPICE_LOG_LEVEL_WARNING, + SPICE_LOG_LEVEL_INFO, + SPICE_LOG_LEVEL_DEBUG, +} SpiceLogLevel; + +void spice_logv(const char *log_domain, + SpiceLogLevel log_level, + const char *strloc, + const char *function, + const char *format, + va_list args); + +void spice_log(const char *log_domain, + SpiceLogLevel log_level, + const char *strloc, + const char *function, + const char *format, + ...); + +#ifndef spice_return_if_fail +#define spice_return_if_fail(x) SPICE_STMT_START { \ + if SPICE_LIKELY(x) { } else { \ + spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_CRITICAL, SPICE_STRLOC, __FUNCTION__, "condition `%s' failed", #x); \ + return; \ + } \ +} SPICE_STMT_END +#endif + +#ifndef spice_return_val_if_fail +#define spice_return_val_if_fail(x, val) SPICE_STMT_START { \ + if SPICE_LIKELY(x) { } else { \ + spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_CRITICAL, SPICE_STRLOC, __FUNCTION__, "condition `%s' failed", #x); \ + return (val); \ + } \ +} SPICE_STMT_END +#endif + +#ifndef spice_warn_if_reached +#define spice_warn_if_reached() SPICE_STMT_START { \ + spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_WARNING, SPICE_STRLOC, __FUNCTION__, "should not be reached"); \ +} SPICE_STMT_END +#endif + +#ifndef spice_printerr +#define spice_printerr(format, ...) SPICE_STMT_START { \ + fprintf(stderr, "%s: " format "\n", __FUNCTION__, ## __VA_ARGS__); \ +} SPICE_STMT_END +#endif + +#ifndef spice_debug +#define spice_debug(format, ...) SPICE_STMT_START { \ + spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_DEBUG, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \ +} SPICE_STMT_END +#endif + +#ifndef spice_warning +#define spice_warning(format, ...) SPICE_STMT_START { \ + spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_WARNING, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \ +} SPICE_STMT_END +#endif + +#ifndef spice_critical +#define spice_critical(format, ...) SPICE_STMT_START { \ + spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_CRITICAL, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \ +} SPICE_STMT_END +#endif + +#ifndef spice_error +#define spice_error(format, ...) SPICE_STMT_START { \ + spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_ERROR, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \ +} SPICE_STMT_END +#endif + +#ifndef spice_warn_if_fail +#define spice_warn_if_fail(x) SPICE_STMT_START { \ + if SPICE_LIKELY(x) { } else { \ + spice_warning("condition `%s' failed", #x); \ + } \ +} SPICE_STMT_END +#endif + +#ifndef spice_warn_if +#define spice_warn_if(x) SPICE_STMT_START { \ + if SPICE_UNLIKELY(x) { \ + spice_warning("condition `%s' reached", #x); \ + } \ +} SPICE_STMT_END +#endif + +#ifndef spice_assert +#define spice_assert(x) SPICE_STMT_START { \ + if SPICE_LIKELY(x) { } else { \ + spice_error("assertion `%s' failed", #x); \ + } \ +} SPICE_STMT_END +#endif + +/* FIXME: improve that some day.. */ +#ifndef spice_static_assert +#define spice_static_assert(x) SPICE_STMT_START { \ + spice_assert(x); \ +} SPICE_STMT_END +#endif + +SPICE_END_DECLS + +#endif /* H_SPICE_LOG */ diff --git a/common/lz.c b/common/lz.c index 5f51794..568aae7 100644 --- a/common/lz.c +++ b/common/lz.c @@ -47,21 +47,9 @@ #include <config.h> #endif +#include "spice_common.h" #include "lz.h" -#define DEBUG - -#ifdef DEBUG - -#define ASSERT(usr, x) \ - if (!(x)) (usr)->error(usr, "%s: ASSERT %s failed\n", __FUNCTION__, #x); - -#else - -#define ASSERT(usr, x) - -#endif - #define HASH_LOG 13 #define HASH_SIZE (1 << HASH_LOG) #define HASH_MASK (HASH_SIZE - 1) @@ -184,7 +172,7 @@ static int lz_read_image_segments(Encoder *encoder, uint8_t *first_lines, uint8_t* lines = first_lines; int row; - ASSERT(encoder->usr, !encoder->head_image_segs); + spice_return_val_if_fail(!encoder->head_image_segs, FALSE); image_seg = lz_alloc_image_seg(encoder); if (!image_seg) { @@ -240,10 +228,10 @@ static INLINE void encode(Encoder *encoder, uint8_t byte) if (more_io_bytes(encoder) <= 0) { encoder->usr->error(encoder->usr, "%s: no more bytes\n", __FUNCTION__); } - ASSERT(encoder->usr, encoder->io_now); + spice_return_if_fail(encoder->io_now); } - ASSERT(encoder->usr, encoder->io_now < encoder->io_end); + spice_return_if_fail(encoder->io_now < encoder->io_end); *(encoder->io_now++) = byte; } @@ -263,7 +251,7 @@ static INLINE void encode_copy_count(Encoder *encoder, uint8_t copy_count) static INLINE void update_copy_count(Encoder *encoder, uint8_t copy_count) { - ASSERT(encoder->usr, encoder->io_last_copy); + spice_return_if_fail(encoder->io_last_copy); *(encoder->io_last_copy) = copy_count; } @@ -278,12 +266,13 @@ static INLINE void compress_output_prev(Encoder *encoder) // io_now cannot be the first byte of the buffer encoder->io_now--; // the function should be called only when copy count is written unnecessarily by lz_compress - ASSERT(encoder->usr, encoder->io_now == encoder->io_last_copy) + spice_return_if_fail(encoder->io_now == encoder->io_last_copy); } static int encoder_reset(Encoder *encoder, uint8_t *io_ptr, uint8_t *io_ptr_end) { - ASSERT(encoder->usr, io_ptr <= io_ptr_end); + spice_return_val_if_fail(io_ptr <= io_ptr_end, FALSE); + encoder->io_bytes_count = io_ptr_end - io_ptr; encoder->io_start = io_ptr; encoder->io_now = io_ptr; @@ -300,9 +289,9 @@ static INLINE uint8_t decode(Encoder *encoder) if (num_io_bytes <= 0) { encoder->usr->error(encoder->usr, "%s: no more bytes\n", __FUNCTION__); } - ASSERT(encoder->usr, encoder->io_now); + spice_assert(encoder->io_now); } - ASSERT(encoder->usr, encoder->io_now < encoder->io_end); + spice_assert(encoder->io_now < encoder->io_end); return *(encoder->io_now++); } @@ -707,7 +696,7 @@ void lz_decode(LzContext *lz, LzImageType to_type, uint8_t *buf) if (encoder->type == to_type) { out_size = lz_rgb32_decompress(encoder, (rgb32_pixel_t *)buf, size); alpha_size = lz_rgb_alpha_decompress(encoder, (rgb32_pixel_t *)buf, size); - ASSERT(encoder->usr, alpha_size == size); + spice_assert(alpha_size == size); } else { encoder->usr->error(encoder->usr, "unsupported output format\n"); } @@ -731,8 +720,8 @@ void lz_decode(LzContext *lz, LzImageType to_type, uint8_t *buf) } } - ASSERT(encoder->usr, is_io_to_decode_end(encoder)); - ASSERT(encoder->usr, out_size == size); + spice_assert(is_io_to_decode_end(encoder)); + spice_assert(out_size == size); if (out_size != size) { encoder->usr->error(encoder->usr, "bad decode size\n"); diff --git a/common/lz.h b/common/lz.h index 472e34d..248ab2a 100644 --- a/common/lz.h +++ b/common/lz.h @@ -9,6 +9,7 @@ #include "lz_common.h" #include "lz_config.h" #include "draw.h" +#include "macros.h" #ifdef __cplusplus extern "C" { @@ -18,9 +19,9 @@ typedef void *LzContext; typedef struct LzUsrContext LzUsrContext; struct LzUsrContext { - void (*error)(LzUsrContext *usr, const char *fmt, ...); - void (*warn)(LzUsrContext *usr, const char *fmt, ...); - void (*info)(LzUsrContext *usr, const char *fmt, ...); + ATTR_PRINTF(2, 3) void (*error)(LzUsrContext *usr, const char *fmt, ...); + ATTR_PRINTF(2, 3) void (*warn)(LzUsrContext *usr, const char *fmt, ...); + ATTR_PRINTF(2, 3) void (*info)(LzUsrContext *usr, const char *fmt, ...); void *(*malloc)(LzUsrContext *usr, int size); void (*free)(LzUsrContext *usr, void *ptr); int (*more_space)(LzUsrContext *usr, uint8_t **io_ptr); // get the next chunk of the diff --git a/common/lz_decompress_tmpl.c b/common/lz_decompress_tmpl.c index b0cbb2a..fb41e77 100644 --- a/common/lz_decompress_tmpl.c +++ b/common/lz_decompress_tmpl.c @@ -254,9 +254,9 @@ static size_t FNAME(decompress)(Encoder *encoder, OUT_PIXEL *out_buf, int size) #endif ref -= ofs; - ASSERT(encoder->usr, op + len <= op_limit); - ASSERT(encoder->usr, ref + len <= op_limit); - ASSERT(encoder->usr, ref >= out_buf); + spice_assert(op + len <= op_limit); + spice_assert(ref + len <= op_limit); + spice_assert(ref >= out_buf); // TODO: optimize by not calling loop at least 3 times when not PLT_TO_RGB32 (len is // always >=3). in PLT_TO_RGB32 len >= 3*number_of_pixels_per_byte @@ -270,29 +270,29 @@ static size_t FNAME(decompress)(Encoder *encoder, OUT_PIXEL *out_buf, int size) OUT_PIXEL b = *ref; for (; len; --len) { COPY_PIXEL(b, op); - ASSERT(encoder->usr, op <= op_limit); + spice_assert(op <= op_limit); } } else { for (; len; --len) { COPY_REF_PIXEL(ref, op); - ASSERT(encoder->usr, op <= op_limit); + spice_assert(op <= op_limit); } } } else { // copy ctrl++; // copy count is biased by 1 #if defined(TO_RGB32) && (defined(PLT4_BE) || defined(PLT4_LE) || defined(PLT1_BE) || \ defined(PLT1_LE)) - ASSERT(encoder->usr, op + CAST_PLT_DISTANCE(ctrl) <= op_limit); + spice_assert(op + CAST_PLT_DISTANCE(ctrl) <= op_limit); #else - ASSERT(encoder->usr, op + ctrl <= op_limit); + spice_assert(op + ctrl <= op_limit); #endif COPY_COMP_PIXEL(encoder, op); - ASSERT(encoder->usr, op <= op_limit); + spice_assert(op <= op_limit); for (--ctrl; ctrl; ctrl--) { COPY_COMP_PIXEL(encoder, op); - ASSERT(encoder->usr, op <= op_limit); + spice_assert(op <= op_limit); } } diff --git a/common/macros.h b/common/macros.h new file mode 100644 index 0000000..44a37e4 --- /dev/null +++ b/common/macros.h @@ -0,0 +1,30 @@ +/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */ +/* + Copyright (C) 2009 Red Hat, Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef __MACROS_H +#define __MACROS_H + +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) +#define ATTR_PRINTF(a,b) \ + __attribute__((format(printf,a,b))) +#else +#define ATTR_PRINTF(a,b) +#endif /* __GNUC__ */ + + +#endif /* __MACROS_H */ diff --git a/common/mem.c b/common/mem.c index 5298e37..5f1dbe4 100644 --- a/common/mem.c +++ b/common/mem.c @@ -19,16 +19,18 @@ #include <config.h> #endif +#include "spice_common.h" #include "mem.h" + #include <stdlib.h> #include <stdio.h> #include <string.h> #ifndef MALLOC_ERROR -#define MALLOC_ERROR(format, ...) { \ - printf(format "\n", ## __VA_ARGS__); \ - abort(); \ -} +#define MALLOC_ERROR(format, ...) SPICE_STMT_START { \ + spice_error(format, ## __VA_ARGS__); \ + abort(); \ +} SPICE_STMT_END #endif size_t spice_strnlen(const char *str, size_t max_len) @@ -94,8 +96,7 @@ void *spice_malloc(size_t n_bytes) return mem; } - MALLOC_ERROR("spice_malloc: panic: unable to allocate %lu bytes\n", - (unsigned long)n_bytes); + MALLOC_ERROR("unable to allocate %lu bytes", (unsigned long)n_bytes); } return NULL; } @@ -111,8 +112,7 @@ void *spice_malloc0(size_t n_bytes) return mem; } - MALLOC_ERROR("spice_malloc0: panic: unable to allocate %lu bytes\n", - (unsigned long)n_bytes); + MALLOC_ERROR("unable to allocate %lu bytes", (unsigned long)n_bytes); } return NULL; } @@ -126,8 +126,7 @@ void *spice_realloc(void *mem, size_t n_bytes) return mem; } - MALLOC_ERROR("spice_realloc: panic: unable to allocate %lu bytes\n", - (unsigned long)n_bytes); + MALLOC_ERROR("unable to allocate %lu bytes", (unsigned long)n_bytes); } free(mem); @@ -140,7 +139,7 @@ void *spice_realloc(void *mem, size_t n_bytes) void *spice_malloc_n(size_t n_blocks, size_t n_block_bytes) { if (SIZE_OVERFLOWS (n_blocks, n_block_bytes)) { - MALLOC_ERROR("spice_malloc_n: overflow allocating %lu*%lu bytes", + MALLOC_ERROR("overflow allocating %lu*%lu bytes", (unsigned long)n_blocks, (unsigned long)n_block_bytes); } @@ -179,7 +178,7 @@ void *spice_realloc_n(void *mem, size_t n_blocks, size_t n_block_bytes) if (SIZE_OVERFLOWS (n_blocks, n_block_bytes)) { MALLOC_ERROR("spice_realloc_n: overflow allocating %lu*%lu bytes", (unsigned long)n_blocks, (unsigned long)n_block_bytes); - } + } return spice_realloc(mem, n_blocks * n_block_bytes); } diff --git a/common/ogl_ctx.c b/common/ogl_ctx.c index 41c0591..b4e60fb 100644 --- a/common/ogl_ctx.c +++ b/common/ogl_ctx.c @@ -243,7 +243,7 @@ void oglctx_destroy(OGLCtx *ctx) XFreePixmap(ctx->x_display, ((OGLPixmapCtx *)ctx)->pixmap); break; default: - PANIC("invalid ogl ctx type"); + spice_error("invalid ogl ctx type"); } XCloseDisplay(ctx->x_display); diff --git a/common/pixman_utils.c b/common/pixman_utils.c index 1136aa7..f113cf5 100644 --- a/common/pixman_utils.c +++ b/common/pixman_utils.c @@ -206,12 +206,12 @@ void spice_pixman_fill_rect(pixman_image_t *dest, depth = spice_pixman_image_get_bpp(dest); /* stride is in bytes, depth in bits */ - ASSERT(x >= 0); - ASSERT(y >= 0); - ASSERT(width > 0); - ASSERT(height > 0); - ASSERT(x + width <= pixman_image_get_width(dest)); - ASSERT(y + height <= pixman_image_get_height(dest)); + spice_assert(x >= 0); + spice_assert(y >= 0); + spice_assert(width > 0); + spice_assert(height > 0); + spice_assert(x + width <= pixman_image_get_width(dest)); + spice_assert(y + height <= pixman_image_get_height(dest)); if (pixman_fill(bits, stride / 4, @@ -231,7 +231,7 @@ void spice_pixman_fill_rect(pixman_image_t *dest, byte_width = 2 * width; value = (value & 0xffff) * 0x00010001; } else { - ASSERT (depth == 32) + spice_assert (depth == 32); byte_line = ((uint8_t *)bits) + stride * y + x * 4; byte_width = 4 * width; } @@ -298,13 +298,13 @@ void spice_pixman_fill_rect_rop(pixman_image_t *dest, depth = spice_pixman_image_get_bpp(dest); /* stride is in bytes, depth in bits */ - ASSERT(x >= 0); - ASSERT(y >= 0); - ASSERT(width > 0); - ASSERT(height > 0); - ASSERT(x + width <= pixman_image_get_width(dest)); - ASSERT(y + height <= pixman_image_get_height(dest)); - ASSERT(rop >= 0 && rop < 16); + spice_assert(x >= 0); + spice_assert(y >= 0); + spice_assert(width > 0); + spice_assert(height > 0); + spice_assert(x + width <= pixman_image_get_width(dest)); + spice_assert(y + height <= pixman_image_get_height(dest)); + spice_assert(rop >= 0 && rop < 16); if (depth == 8) { solid_rop_8_func_t rop_func = solid_rops_8[rop]; @@ -358,13 +358,13 @@ void spice_pixman_tile_rect(pixman_image_t *dest, tile_width = pixman_image_get_width(tile); tile_height = pixman_image_get_height(tile); - ASSERT(x >= 0); - ASSERT(y >= 0); - ASSERT(width > 0); - ASSERT(height > 0); - ASSERT(x + width <= pixman_image_get_width(dest)); - ASSERT(y + height <= pixman_image_get_height(dest)); - ASSERT(depth == spice_pixman_image_get_bpp(tile)); + spice_assert(x >= 0); + spice_assert(y >= 0); + spice_assert(width > 0); + spice_assert(height > 0); + spice_assert(x + width <= pixman_image_get_width(dest)); + spice_assert(y + height <= pixman_image_get_height(dest)); + spice_assert(depth == spice_pixman_image_get_bpp(tile)); tile_start_x = (x - offset_x) % tile_width; if (tile_start_x < 0) { @@ -406,7 +406,7 @@ void spice_pixman_tile_rect(pixman_image_t *dest, } } } else { - ASSERT (depth == 32); + spice_assert (depth == 32); byte_line = ((uint8_t *)bits) + stride * y + x * 4; tile_line = ((uint8_t *)tile_bits) + tile_stride * tile_start_y + tile_start_x * 4; @@ -449,14 +449,14 @@ void spice_pixman_tile_rect_rop(pixman_image_t *dest, tile_width = pixman_image_get_width(tile); tile_height = pixman_image_get_height(tile); - ASSERT(x >= 0); - ASSERT(y >= 0); - ASSERT(width > 0); - ASSERT(height > 0); - ASSERT(x + width <= pixman_image_get_width(dest)); - ASSERT(y + height <= pixman_image_get_height(dest)); - ASSERT(rop >= 0 && rop < 16); - ASSERT(depth == spice_pixman_image_get_bpp(tile)); + spice_assert(x >= 0); + spice_assert(y >= 0); + spice_assert(width > 0); + spice_assert(height > 0); + spice_assert(x + width <= pixman_image_get_width(dest)); + spice_assert(y + height <= pixman_image_get_height(dest)); + spice_assert(rop >= 0 && rop < 16); + spice_assert(depth == spice_pixman_image_get_bpp(tile)); tile_start_x = (x - offset_x) % tile_width; if (tile_start_x < 0) { @@ -504,7 +504,7 @@ void spice_pixman_tile_rect_rop(pixman_image_t *dest, } else { tiled_rop_32_func_t rop_func = tiled_rops_32[rop]; - ASSERT (depth == 32); + spice_assert (depth == 32); byte_line = ((uint8_t *)bits) + stride * y + x * 4; tile_line = ((uint8_t *)tile_bits) + tile_stride * tile_start_y + tile_start_x * 4; @@ -536,6 +536,11 @@ void spice_pixman_blit(pixman_image_t *dest, uint8_t *src_line; int byte_width; + if (!src) { + fprintf(stderr, "missing src!"); + return; + } + bits = pixman_image_get_data(dest); stride = pixman_image_get_stride(dest); depth = spice_pixman_image_get_bpp(dest); @@ -569,17 +574,17 @@ void spice_pixman_blit(pixman_image_t *dest, return; } - ASSERT(src_x >= 0); - ASSERT(src_y >= 0); - ASSERT(dest_x >= 0); - ASSERT(dest_y >= 0); - ASSERT(width > 0); - ASSERT(height > 0); - ASSERT(dest_x + width <= pixman_image_get_width(dest)); - ASSERT(dest_y + height <= pixman_image_get_height(dest)); - ASSERT(src_x + width <= pixman_image_get_width(src)); - ASSERT(src_y + height <= pixman_image_get_height(src)); - ASSERT(depth == src_depth); + spice_assert(src_x >= 0); + spice_assert(src_y >= 0); + spice_assert(dest_x >= 0); + spice_assert(dest_y >= 0); + spice_assert(width > 0); + spice_assert(height > 0); + spice_assert(dest_x + width <= pixman_image_get_width(dest)); + spice_assert(dest_y + height <= pixman_image_get_height(dest)); + spice_assert(src_x + width <= pixman_image_get_width(src)); + spice_assert(src_y + height <= pixman_image_get_height(src)); + spice_assert(depth == src_depth); if (pixman_blt(src_bits, bits, @@ -601,7 +606,7 @@ void spice_pixman_blit(pixman_image_t *dest, byte_width = width * 2; src_line = ((uint8_t *)src_bits) + src_stride * src_y + src_x * 2; } else { - ASSERT (depth == 32); + spice_assert (depth == 32); byte_line = ((uint8_t *)bits) + stride * dest_y + dest_x * 4; byte_width = width * 4; src_line = ((uint8_t *)src_bits) + src_stride * src_y + src_x * 4; @@ -660,17 +665,17 @@ void spice_pixman_blit_rop (pixman_image_t *dest, return; } - ASSERT(src_x >= 0); - ASSERT(src_y >= 0); - ASSERT(dest_x >= 0); - ASSERT(dest_y >= 0); - ASSERT(width > 0); - ASSERT(height > 0); - ASSERT(dest_x + width <= pixman_image_get_width(dest)); - ASSERT(dest_y + height <= pixman_image_get_height(dest)); - ASSERT(src_x + width <= pixman_image_get_width(src)); - ASSERT(src_y + height <= pixman_image_get_height(src)); - ASSERT(depth == src_depth); + spice_assert(src_x >= 0); + spice_assert(src_y >= 0); + spice_assert(dest_x >= 0); + spice_assert(dest_y >= 0); + spice_assert(width > 0); + spice_assert(height > 0); + spice_assert(dest_x + width <= pixman_image_get_width(dest)); + spice_assert(dest_y + height <= pixman_image_get_height(dest)); + spice_assert(src_x + width <= pixman_image_get_width(src)); + spice_assert(src_y + height <= pixman_image_get_height(src)); + spice_assert(depth == src_depth); if (depth == 8) { copy_rop_8_func_t rop_func = copy_rops_8[rop]; @@ -697,7 +702,7 @@ void spice_pixman_blit_rop (pixman_image_t *dest, } else { copy_rop_32_func_t rop_func = copy_rops_32[rop]; - ASSERT (depth == 32); + spice_assert (depth == 32); byte_line = ((uint8_t *)bits) + stride * dest_y + dest_x * 4; src_line = ((uint8_t *)src_bits) + src_stride * src_y + src_x * 4; @@ -756,17 +761,17 @@ void spice_pixman_blit_colorkey (pixman_image_t *dest, return; } - ASSERT(src_x >= 0); - ASSERT(src_y >= 0); - ASSERT(dest_x >= 0); - ASSERT(dest_y >= 0); - ASSERT(width > 0); - ASSERT(height > 0); - ASSERT(dest_x + width <= pixman_image_get_width(dest)); - ASSERT(dest_y + height <= pixman_image_get_height(dest)); - ASSERT(src_x + width <= pixman_image_get_width(src)); - ASSERT(src_y + height <= pixman_image_get_height(src)); - ASSERT(depth == spice_pixman_image_get_bpp(src)); + spice_assert(src_x >= 0); + spice_assert(src_y >= 0); + spice_assert(dest_x >= 0); + spice_assert(dest_y >= 0); + spice_assert(width > 0); + spice_assert(height > 0); + spice_assert(dest_x + width <= pixman_image_get_width(dest)); + spice_assert(dest_y + height <= pixman_image_get_height(dest)); + spice_assert(src_x + width <= pixman_image_get_width(src)); + spice_assert(src_y + height <= pixman_image_get_height(src)); + spice_assert(depth == spice_pixman_image_get_bpp(src)); if (depth == 8) { byte_line = ((uint8_t *)bits) + stride * dest_y + dest_x; @@ -806,7 +811,7 @@ void spice_pixman_blit_colorkey (pixman_image_t *dest, src_line += src_stride; } } else { - ASSERT (depth == 32); + spice_assert (depth == 32); byte_line = ((uint8_t *)bits) + stride * dest_y + dest_x * 4; src_line = ((uint8_t *)src_bits) + src_stride * src_y + src_x * 4; @@ -925,7 +930,7 @@ pixman_format_code_t spice_surface_format_to_pixman(uint32_t surface_format) return PIXMAN_a8r8g8b8; default: printf("Unknown surface format %d\n", surface_format); - abort(); + spice_abort(); break; } return (pixman_format_code_t)0; /* Not reached */ @@ -961,7 +966,7 @@ pixman_format_code_t spice_bitmap_format_to_pixman(int bitmap_format, case SPICE_BITMAP_FMT_INVALID: default: printf("Unknown bitmap format %d\n", bitmap_format); - abort(); + spice_abort(); return PIXMAN_a8r8g8b8; } } @@ -1131,7 +1136,7 @@ static void bitmap_8_32_to_32(uint8_t *dest, int dest_stride, #endif if (!palette) { - PANIC("No palette"); + spice_error("No palette"); return; } @@ -1177,7 +1182,7 @@ static void bitmap_8_16_to_16_555(uint8_t *dest, int dest_stride, #endif if (!palette) { - PANIC("No palette"); + spice_error("No palette"); return; } @@ -1223,7 +1228,7 @@ static void bitmap_4be_32_to_32(uint8_t* dest, int dest_stride, #endif if (!palette) { - PANIC("No palette"); + spice_error("No palette"); return; } @@ -1273,7 +1278,7 @@ static void bitmap_4be_16_to_16_555(uint8_t* dest, int dest_stride, #endif if (!palette) { - PANIC("No palette"); + spice_error("No palette"); return; } @@ -1323,7 +1328,7 @@ static void bitmap_1be_32_to_32(uint8_t* dest, int dest_stride, uint32_t fore_color; uint32_t back_color; - ASSERT(palette != NULL); + spice_assert(palette != NULL); if (!palette) { return; @@ -1355,7 +1360,7 @@ static void bitmap_1be_16_to_16_555(uint8_t* dest, int dest_stride, uint16_t fore_color; uint16_t back_color; - ASSERT(palette != NULL); + spice_assert(palette != NULL); if (!palette) { return; @@ -1461,7 +1466,7 @@ pixman_image_t *spice_bitmap_to_pixman(pixman_image_t *dest_image, dest = (uint8_t *)pixman_image_get_data(dest_image); dest_stride = pixman_image_get_stride(dest_image); if (!(flags & SPICE_BITMAP_FLAGS_TOP_DOWN)) { - ASSERT(height > 0); + spice_assert(height > 0); dest += dest_stride * (height - 1); dest_stride = -dest_stride; } @@ -1485,7 +1490,7 @@ pixman_image_t *spice_bitmap_to_pixman(pixman_image_t *dest_image, } else if (palette_surface_format == SPICE_SURFACE_FMT_16_555) { bitmap_8_16_to_16_555(dest, dest_stride, src, src_stride, width, end, palette); } else { - PANIC("Unsupported palette format"); + spice_error("Unsupported palette format"); } break; case SPICE_BITMAP_FMT_4BIT_BE: @@ -1495,7 +1500,7 @@ pixman_image_t *spice_bitmap_to_pixman(pixman_image_t *dest_image, } else if (palette_surface_format == SPICE_SURFACE_FMT_16_555) { bitmap_4be_16_to_16_555(dest, dest_stride, src, src_stride, width, end, palette); } else { - PANIC("Unsupported palette format"); + spice_error("Unsupported palette format"); } break; case SPICE_BITMAP_FMT_1BIT_BE: @@ -1505,11 +1510,11 @@ pixman_image_t *spice_bitmap_to_pixman(pixman_image_t *dest_image, } else if (palette_surface_format == SPICE_SURFACE_FMT_16_555) { bitmap_1be_16_to_16_555(dest, dest_stride, src, src_stride, width, end, palette); } else { - PANIC("Unsupported palette format"); + spice_error("Unsupported palette format"); } break; default: - PANIC("Unsupported bitmap format"); + spice_error("Unsupported bitmap format"); break; } diff --git a/common/quic.c b/common/quic.c index 707724a..27f637b 100644 --- a/common/quic.c +++ b/common/quic.c @@ -24,11 +24,9 @@ #endif #include "quic.h" -#include <spice/macros.h> +#include "spice_common.h" #include "bitops.h" -//#define DEBUG - #define RLE #define RLE_STAT #define PRED_1 @@ -42,17 +40,6 @@ #define QUIC_VERSION_MINOR 1U #define QUIC_VERSION ((QUIC_VERSION_MAJOR << 16) | (QUIC_VERSION_MAJOR & 0xffff)) -#ifdef DEBUG - -#define ASSERT(usr, x) \ - if (!(x)) (usr)->error(usr, "%s: ASSERT %s failed\n", __FUNCTION__, #x); - -#else - -#define ASSERT(usr, x) - -#endif - typedef uint8_t BYTE; /* maximum number of codes in family */ @@ -253,8 +240,8 @@ static const unsigned int tabrand_chaos[TABRAND_TABSIZE] = { static unsigned int stabrand(void) { - //ASSERT( !(TABRAND_SEEDMASK & TABRAND_TABSIZE)); - //ASSERT( TABRAND_SEEDMASK + 1 == TABRAND_TABSIZE ); + //spice_assert( !(TABRAND_SEEDMASK & TABRAND_TABSIZE)); + //spice_assert( TABRAND_SEEDMASK + 1 == TABRAND_TABSIZE ); return TABRAND_SEEDMASK; } @@ -279,19 +266,19 @@ static void set_wm_trigger(CommonState *state) wm = 10; } - ASSERT(state->encoder->usr, evol < 6); + spice_assert(evol < 6); state->wm_trigger = besttrigtab[evol / 2][wm]; - ASSERT(state->encoder->usr, state->wm_trigger <= 2000); - ASSERT(state->encoder->usr, state->wm_trigger >= 1); + spice_assert(state->wm_trigger <= 2000); + spice_assert(state->wm_trigger >= 1); } static int ceil_log_2(int val) /* ceil(log_2(val)) */ { int result; - //ASSERT(val>0); + //spice_assert(val>0); if (val == 1) { return 0; @@ -346,7 +333,7 @@ static void decorelate_init(QuicFamily *family, int bpc) const unsigned int pixelbitmaskshr = pixelbitmask >> 1; unsigned int s; - //ASSERT(bpc <= 8); + //spice_assert(bpc <= 8); for (s = 0; s <= pixelbitmask; s++) { if (s <= pixelbitmaskshr) { @@ -362,7 +349,7 @@ static void corelate_init(QuicFamily *family, int bpc) const unsigned long int pixelbitmask = bppmask[bpc]; unsigned long int s; - //ASSERT(bpc <= 8); + //spice_assert(bpc <= 8); for (s = 0; s <= pixelbitmask; s++) { if (s & 0x01) { @@ -404,7 +391,7 @@ static void more_io_words(Encoder *encoder) if (num_io_words <= 0) { encoder->usr->error(encoder->usr, "%s: no more words\n", __FUNCTION__); } - ASSERT(encoder->usr, io_ptr); + spice_assert(io_ptr); encoder->io_words_count += num_io_words; encoder->io_now = io_ptr; encoder->io_end = encoder->io_now + num_io_words; @@ -431,8 +418,8 @@ static INLINE void encode(Encoder *encoder, unsigned int word, unsigned int len) { int delta; - ASSERT(encoder->usr, len > 0 && len < 32); - ASSERT(encoder->usr, !(word & ~bppmask[len])); + spice_assert(len > 0 && len < 32); + spice_assert(!(word & ~bppmask[len])); if ((delta = ((int)encoder->io_available_bits - len)) >= 0) { encoder->io_available_bits = delta; encoder->io_word |= word << encoder->io_available_bits; @@ -444,8 +431,8 @@ static INLINE void encode(Encoder *encoder, unsigned int word, unsigned int len) encoder->io_available_bits = 32 - delta; encoder->io_word = word << encoder->io_available_bits; - ASSERT(encoder->usr, encoder->io_available_bits < 32); - ASSERT(encoder->usr, (encoder->io_word & bppmask[encoder->io_available_bits]) == 0); + spice_assert(encoder->io_available_bits < 32); + spice_assert((encoder->io_word & bppmask[encoder->io_available_bits]) == 0); } static INLINE void encode_32(Encoder *encoder, unsigned int word) @@ -478,7 +465,7 @@ static INLINE void read_io_word(Encoder *encoder) __read_io_word_ptr(encoder); //disable inline optimizations return; } - ASSERT(encoder->usr, encoder->io_now < encoder->io_end); + spice_assert(encoder->io_now < encoder->io_end); encoder->io_next_word = *(encoder->io_now++); } @@ -486,7 +473,7 @@ static INLINE void decode_eatbits(Encoder *encoder, int len) { int delta; - ASSERT(encoder->usr, len > 0 && len < 32); + spice_assert(len > 0 && len < 32); encoder->io_word <<= len; if ((delta = ((int)encoder->io_available_bits - len)) >= 0) { @@ -717,7 +704,7 @@ static INLINE void encode_run(Encoder *encoder, unsigned int len) while ((msb = spice_bit_find_msb(len))) { len &= ~(1 << (msb - 1)); - ASSERT(encoder->usr, msb < 32); + spice_assert(msb < 32); encode(encoder, (1 << (msb)) - 1, msb); encode(encoder, 0, 1); } @@ -739,7 +726,7 @@ static INLINE unsigned int decode_run(Encoder *encoder) while (encoder->io_word & (1U << 31)) { decode_eatbits(encoder, 1); count++; - ASSERT(encoder->usr, count < 32); + spice_assert(count < 32); } decode_eatbits(encoder, 1); len += (1U << count) >> 1; @@ -871,10 +858,10 @@ static void fill_model_structures(Encoder *encoder, FamilyStat *family_stat, family_stat->buckets_buf[bnumber].pcounters = free_counter; free_counter += ncounters; - ASSERT(encoder->usr, bstart < n_buckets_ptrs); + spice_assert(bstart < n_buckets_ptrs); { unsigned int i; - ASSERT(encoder->usr, bend < n_buckets_ptrs); + spice_assert(bend < n_buckets_ptrs); for (i = bstart; i <= bend; i++) { family_stat->buckets_ptrs[i] = family_stat->buckets_buf + bnumber; } @@ -883,7 +870,7 @@ static void fill_model_structures(Encoder *encoder, FamilyStat *family_stat, bnumber++; } while (bend < levels - 1); - ASSERT(encoder->usr, free_counter - family_stat->counters == nbuckets * ncounters); + spice_assert(free_counter - family_stat->counters == nbuckets * ncounters); } static void find_model_params(Encoder *encoder, @@ -901,7 +888,7 @@ static void find_model_params(Encoder *encoder, unsigned int bstart, bend = 0; /* bucket start and end, range : 0 to levels-1*/ unsigned int repcntr; /* helper */ - ASSERT(encoder->usr, bpc <= 8 && bpc > 0); + spice_assert(bpc <= 8 && bpc > 0); *ncounters = 8; @@ -965,7 +952,7 @@ static void find_model_params(Encoder *encoder, *n_buckets_ptrs = *levels; } else if (bsize >= 256) { /* this bucket is allowed to reside in the 2nd table */ b_lo_ptrs = bstart; - assert(bstart); /* previous bucket exists */ + spice_assert(bstart); /* previous bucket exists */ } #endif } @@ -1089,8 +1076,8 @@ static int init_encoder(Encoder *encoder, QuicUsrContext *usr) static int encoder_reste(Encoder *encoder, uint32_t *io_ptr, uint32_t *io_ptr_end) { - ASSERT(encoder->usr, ((unsigned long)io_ptr % 4) == ((unsigned long)io_ptr_end % 4)); - ASSERT(encoder->usr, io_ptr <= io_ptr_end); + spice_assert(((unsigned long)io_ptr % 4) == ((unsigned long)io_ptr_end % 4)); + spice_assert(io_ptr <= io_ptr_end); encoder->rgb_state.waitcnt = 0; encoder->rgb_state.tabrand_seed = stabrand(); @@ -1171,7 +1158,7 @@ static int encoder_reste_channels(Encoder *encoder, int channels, int width, int static void quic_image_params(Encoder *encoder, QuicImageType type, int *channels, int *bpc) { - ASSERT(encoder->usr, channels && bpc); + spice_assert(channels && bpc); switch (type) { case QUIC_IMAGE_TYPE_GRAY: *channels = 1; @@ -1251,7 +1238,10 @@ int quic_encode(QuicContext *quic, QuicImageType type, int width, int height, int i; #endif - ASSERT(encoder->usr, line); + if (line == NULL) { + spice_warn_if_reached(); + return QUIC_ERROR; + } lines_end = line + num_lines * stride; quic_image_params(encoder, type, &channels, &bpc); @@ -1275,19 +1265,19 @@ int quic_encode(QuicContext *quic, QuicImageType type, int width, int height, switch (type) { #ifdef QUIC_RGB case QUIC_IMAGE_TYPE_RGB32: - ASSERT(encoder->usr, ABS(stride) >= width * 4); + spice_assert(ABS(stride) >= width * 4); QUIC_COMPRESS_RGB(32); break; case QUIC_IMAGE_TYPE_RGB24: - ASSERT(encoder->usr, ABS(stride) >= width * 3); + spice_assert(ABS(stride) >= width * 3); QUIC_COMPRESS_RGB(24); break; case QUIC_IMAGE_TYPE_RGB16: - ASSERT(encoder->usr, ABS(stride) >= width * 2); + spice_assert(ABS(stride) >= width * 2); QUIC_COMPRESS_RGB(16); break; case QUIC_IMAGE_TYPE_RGBA: - ASSERT(encoder->usr, ABS(stride) >= width * 4); + spice_assert(ABS(stride) >= width * 4); encoder->channels[0].correlate_row[-1] = 0; encoder->channels[1].correlate_row[-1] = 0; @@ -1315,7 +1305,7 @@ int quic_encode(QuicContext *quic, QuicImageType type, int width, int height, break; #else case QUIC_IMAGE_TYPE_RGB24: - ASSERT(encoder->usr, ABS(stride) >= width * 3); + spice_assert(ABS(stride) >= width * 3); for (i = 0; i < 3; i++) { encoder->channels[i].correlate_row[-1] = 0; quic_three_compress_row0(encoder, &encoder->channels[i], (three_bytes_t *)(line + i), @@ -1335,7 +1325,7 @@ int quic_encode(QuicContext *quic, QuicImageType type, int width, int height, break; case QUIC_IMAGE_TYPE_RGB32: case QUIC_IMAGE_TYPE_RGBA: - ASSERT(encoder->usr, ABS(stride) >= width * 4); + spice_assert(ABS(stride) >= width * 4); for (i = 0; i < channels; i++) { encoder->channels[i].correlate_row[-1] = 0; quic_four_compress_row0(encoder, &encoder->channels[i], (four_bytes_t *)(line + i), @@ -1355,7 +1345,7 @@ int quic_encode(QuicContext *quic, QuicImageType type, int width, int height, break; #endif case QUIC_IMAGE_TYPE_GRAY: - ASSERT(encoder->usr, ABS(stride) >= width); + spice_assert(ABS(stride) >= width); encoder->channels[0].correlate_row[-1] = 0; quic_one_compress_row0(encoder, &encoder->channels[0], (one_byte_t *)line, width); encoder->rows_completed++; @@ -1525,18 +1515,18 @@ int quic_decode(QuicContext *quic, QuicImageType type, uint8_t *buf, int stride) int i; #endif - ASSERT(encoder->usr, buf); + spice_assert(buf); switch (encoder->type) { #ifdef QUIC_RGB case QUIC_IMAGE_TYPE_RGB32: case QUIC_IMAGE_TYPE_RGB24: if (type == QUIC_IMAGE_TYPE_RGB32) { - ASSERT(encoder->usr, ABS(stride) >= (int)encoder->width * 4); + spice_assert(ABS(stride) >= (int)encoder->width * 4); QUIC_UNCOMPRESS_RGB(32, rgb32_pixel_t); break; } else if (type == QUIC_IMAGE_TYPE_RGB24) { - ASSERT(encoder->usr, ABS(stride) >= (int)encoder->width * 3); + spice_assert(ABS(stride) >= (int)encoder->width * 3); QUIC_UNCOMPRESS_RGB(24, rgb24_pixel_t); break; } @@ -1544,10 +1534,10 @@ int quic_decode(QuicContext *quic, QuicImageType type, uint8_t *buf, int stride) return QUIC_ERROR; case QUIC_IMAGE_TYPE_RGB16: if (type == QUIC_IMAGE_TYPE_RGB16) { - ASSERT(encoder->usr, ABS(stride) >= (int)encoder->width * 2); + spice_assert(ABS(stride) >= (int)encoder->width * 2); QUIC_UNCOMPRESS_RGB(16, rgb16_pixel_t); } else if (type == QUIC_IMAGE_TYPE_RGB32) { - ASSERT(encoder->usr, ABS(stride) >= (int)encoder->width * 4); + spice_assert(ABS(stride) >= (int)encoder->width * 4); QUIC_UNCOMPRESS_RGB(16_to_32, rgb32_pixel_t); } else { encoder->usr->warn(encoder->usr, "unsupported output format\n"); @@ -1561,12 +1551,12 @@ int quic_decode(QuicContext *quic, QuicImageType type, uint8_t *buf, int stride) encoder->usr->warn(encoder->usr, "unsupported output format\n"); return QUIC_ERROR; } - ASSERT(encoder->usr, ABS(stride) >= (int)encoder->width * 4); + spice_assert(ABS(stride) >= (int)encoder->width * 4); uncompress_rgba(encoder, buf, stride); break; #else case QUIC_IMAGE_TYPE_RGB24: - ASSERT(encoder->usr, ABS(stride) >= (int)encoder->width * 3); + spice_assert(ABS(stride) >= (int)encoder->width * 3); for (i = 0; i < 3; i++) { encoder->channels[i].correlate_row[-1] = 0; quic_three_uncompress_row0(encoder, &encoder->channels[i], (three_bytes_t *)(buf + i), @@ -1587,7 +1577,7 @@ int quic_decode(QuicContext *quic, QuicImageType type, uint8_t *buf, int stride) } break; case QUIC_IMAGE_TYPE_RGB32: - ASSERT(encoder->usr, ABS(stride) >= encoder->width * 4); + spice_assert(ABS(stride) >= encoder->width * 4); for (i = 0; i < 3; i++) { encoder->channels[i].correlate_row[-1] = 0; quic_four_uncompress_row0(encoder, &encoder->channels[i], (four_bytes_t *)(buf + i), @@ -1610,7 +1600,7 @@ int quic_decode(QuicContext *quic, QuicImageType type, uint8_t *buf, int stride) } break; case QUIC_IMAGE_TYPE_RGBA: - ASSERT(encoder->usr, ABS(stride) >= encoder->width * 4); + spice_assert(ABS(stride) >= encoder->width * 4); for (i = 0; i < 4; i++) { encoder->channels[i].correlate_row[-1] = 0; quic_four_uncompress_row0(encoder, &encoder->channels[i], (four_bytes_t *)(buf + i), @@ -1637,7 +1627,7 @@ int quic_decode(QuicContext *quic, QuicImageType type, uint8_t *buf, int stride) encoder->usr->warn(encoder->usr, "unsupported output format\n"); return QUIC_ERROR; } - ASSERT(encoder->usr, ABS(stride) >= (int)encoder->width); + spice_assert(ABS(stride) >= (int)encoder->width); uncompress_gray(encoder, buf, stride); break; case QUIC_IMAGE_TYPE_INVALID: diff --git a/common/quic.h b/common/quic.h index 7ec94f5..58e68f7 100644 --- a/common/quic.h +++ b/common/quic.h @@ -20,6 +20,7 @@ #define __QUIC_H #include "quic_config.h" +#include "macros.h" #ifdef __cplusplus extern "C" { @@ -41,9 +42,9 @@ typedef void *QuicContext; typedef struct QuicUsrContext QuicUsrContext; struct QuicUsrContext { - void (*error)(QuicUsrContext *usr, const char *fmt, ...); - void (*warn)(QuicUsrContext *usr, const char *fmt, ...); - void (*info)(QuicUsrContext *usr, const char *fmt, ...); + ATTR_PRINTF(2, 3) void (*error)(QuicUsrContext *usr, const char *fmt, ...); + ATTR_PRINTF(2, 3) void (*warn)(QuicUsrContext *usr, const char *fmt, ...); + ATTR_PRINTF(2, 3) void (*info)(QuicUsrContext *usr, const char *fmt, ...); void *(*malloc)(QuicUsrContext *usr, int size); void (*free)(QuicUsrContext *usr, void *ptr); int (*more_space)(QuicUsrContext *usr, uint32_t **io_ptr, int rows_completed); diff --git a/common/quic_family_tmpl.c b/common/quic_family_tmpl.c index bfba0cc..cca2c05 100644 --- a/common/quic_family_tmpl.c +++ b/common/quic_family_tmpl.c @@ -107,7 +107,7 @@ static void FNAME(update_model)(CommonState *state, s_bucket * const bucket, static s_bucket *FNAME(find_bucket)(Channel *channel, const unsigned int val) { - ASSERT(channel->encoder->usr, val < (0x1U << BPC)); + spice_assert(val < (0x1U << BPC)); return channel->_buckets_ptrs[val]; } diff --git a/common/quic_rgb_tmpl.c b/common/quic_rgb_tmpl.c index 8f35679..37c908c 100644 --- a/common/quic_rgb_tmpl.c +++ b/common/quic_rgb_tmpl.c @@ -235,7 +235,7 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, int i, BYTE * const correlate_row_b = channel_b->correlate_row; int stopidx; - ASSERT(encoder->usr, end - i > 0); + spice_assert(end - i > 0); if (!i) { unsigned int codeword, codewordlen; @@ -305,9 +305,9 @@ static void FNAME(compress_row0)(Encoder *encoder, const PIXEL *cur_row, } } - ASSERT(encoder->usr, (int)encoder->rgb_state.wmidx <= wmimax); - ASSERT(encoder->usr, encoder->rgb_state.wmidx <= 32); - ASSERT(encoder->usr, wminext > 0); + spice_assert((int)encoder->rgb_state.wmidx <= wmimax); + spice_assert(encoder->rgb_state.wmidx <= 32); + spice_assert(wminext > 0); } #define COMPRESS_ONE_0(channel) \ @@ -347,7 +347,7 @@ static void FNAME(compress_row_seg)(Encoder *encoder, int i, int run_size; #endif - ASSERT(encoder->usr, end - i > 0); + spice_assert(end - i > 0); if (!i) { unsigned int codeword, codewordlen; @@ -451,9 +451,9 @@ static void FNAME(compress_row)(Encoder *encoder, } } - ASSERT(encoder->usr, (int)encoder->rgb_state.wmidx <= wmimax); - ASSERT(encoder->usr, encoder->rgb_state.wmidx <= 32); - ASSERT(encoder->usr, wminext > 0); + spice_assert((int)encoder->rgb_state.wmidx <= wmimax); + spice_assert(encoder->rgb_state.wmidx <= 32); + spice_assert(wminext > 0); } #endif @@ -490,7 +490,7 @@ static void FNAME(uncompress_row0_seg)(Encoder *encoder, int i, BYTE * const correlate_row_b = channel_b->correlate_row; int stopidx; - ASSERT(encoder->usr, end - i > 0); + spice_assert(end - i > 0); if (!i) { unsigned int codewordlen; @@ -567,9 +567,9 @@ static void FNAME(uncompress_row0)(Encoder *encoder, } } - ASSERT(encoder->usr, (int)encoder->rgb_state.wmidx <= wmimax); - ASSERT(encoder->usr, encoder->rgb_state.wmidx <= 32); - ASSERT(encoder->usr, wminext > 0); + spice_assert((int)encoder->rgb_state.wmidx <= wmimax); + spice_assert(encoder->rgb_state.wmidx <= 32); + spice_assert(wminext > 0); } #define UNCOMPRESS_ONE_0(channel) \ @@ -611,7 +611,7 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder, int run_end; #endif - ASSERT(encoder->usr, end - i > 0); + spice_assert(end - i > 0); if (!i) { unsigned int codewordlen; @@ -721,9 +721,9 @@ static void FNAME(uncompress_row)(Encoder *encoder, } } - ASSERT(encoder->usr, (int)encoder->rgb_state.wmidx <= wmimax); - ASSERT(encoder->usr, encoder->rgb_state.wmidx <= 32); - ASSERT(encoder->usr, wminext > 0); + spice_assert((int)encoder->rgb_state.wmidx <= wmimax); + spice_assert(encoder->rgb_state.wmidx <= 32); + spice_assert(wminext > 0); } #undef PIXEL diff --git a/common/quic_tmpl.c b/common/quic_tmpl.c index 6acd504..b625daf 100644 --- a/common/quic_tmpl.c +++ b/common/quic_tmpl.c @@ -158,7 +158,7 @@ static void FNAME(compress_row0_seg)(Encoder *encoder, Channel *channel, int i, BYTE * const decorelate_drow = channel->correlate_row; int stopidx; - ASSERT(encoder->usr, end - i > 0); + spice_assert(end - i > 0); if (i == 0) { unsigned int codeword, codewordlen; @@ -233,9 +233,9 @@ static void FNAME(compress_row0)(Encoder *encoder, Channel *channel, const PIXEL } } - ASSERT(encoder->usr, (int)channel->state.wmidx <= wmimax); - ASSERT(encoder->usr, channel->state.wmidx <= 32); - ASSERT(encoder->usr, wminext > 0); + spice_assert((int)channel->state.wmidx <= wmimax); + spice_assert(channel->state.wmidx <= 32); + spice_assert(wminext > 0); } static void FNAME(compress_row_seg)(Encoder *encoder, Channel *channel, int i, @@ -253,7 +253,7 @@ static void FNAME(compress_row_seg)(Encoder *encoder, Channel *channel, int i, int run_size; #endif - ASSERT(encoder->usr, end - i > 0); + spice_assert(end - i > 0); if (!i) { unsigned int codeword, codewordlen; @@ -375,9 +375,9 @@ static void FNAME(compress_row)(Encoder *encoder, Channel *channel, } } - ASSERT(encoder->usr, (int)channel->state.wmidx <= wmimax); - ASSERT(encoder->usr, channel->state.wmidx <= 32); - ASSERT(encoder->usr, wminext > 0); + spice_assert((int)channel->state.wmidx <= wmimax); + spice_assert(channel->state.wmidx <= 32); + spice_assert(wminext > 0); } static void FNAME(uncompress_row0_seg)(Encoder *encoder, Channel *channel, int i, @@ -390,7 +390,7 @@ static void FNAME(uncompress_row0_seg)(Encoder *encoder, Channel *channel, int i { int stopidx; - ASSERT(encoder->usr, end - i > 0); + spice_assert(end - i > 0); if (i == 0) { unsigned int codewordlen; @@ -475,9 +475,9 @@ static void FNAME(uncompress_row0)(Encoder *encoder, Channel *channel, } } - ASSERT(encoder->usr, (int)channel->state.wmidx <= wmimax); - ASSERT(encoder->usr, channel->state.wmidx <= 32); - ASSERT(encoder->usr, wminext > 0); + spice_assert((int)channel->state.wmidx <= wmimax); + spice_assert(channel->state.wmidx <= 32); + spice_assert(wminext > 0); } static void FNAME(uncompress_row_seg)(Encoder *encoder, Channel *channel, @@ -496,7 +496,7 @@ static void FNAME(uncompress_row_seg)(Encoder *encoder, Channel *channel, int run_end; #endif - ASSERT(encoder->usr, end - i > 0); + spice_assert(end - i > 0); if (i == 0) { unsigned int codewordlen; @@ -613,9 +613,9 @@ static void FNAME(uncompress_row)(Encoder *encoder, Channel *channel, } } - ASSERT(encoder->usr, (int)channel->state.wmidx <= wmimax); - ASSERT(encoder->usr, channel->state.wmidx <= 32); - ASSERT(encoder->usr, wminext > 0); + spice_assert((int)channel->state.wmidx <= wmimax); + spice_assert(channel->state.wmidx <= 32); + spice_assert(wminext > 0); } #undef PIXEL diff --git a/common/region.c b/common/region.c index 35935ea..30a11e3 100644 --- a/common/region.c +++ b/common/region.c @@ -561,7 +561,7 @@ static void rect_set(SpiceRect *r, int32_t top, int32_t left, int32_t bottom, in r->left = left; r->bottom = bottom; r->right = right; - ASSERT(rect_is_valid(r)); + spice_assert(rect_is_valid(r)); } static void random_region(QRegion *reg) diff --git a/common/ring.h b/common/ring.h index cb70696..36ad820 100644 --- a/common/ring.h +++ b/common/ring.h @@ -48,14 +48,14 @@ static inline int ring_item_is_linked(RingItem *item) static inline int ring_is_empty(Ring *ring) { - ASSERT(ring->next != NULL && ring->prev != NULL); + spice_assert(ring->next != NULL && ring->prev != NULL); return ring == ring->next; } static inline void ring_add(Ring *ring, RingItem *item) { - ASSERT(ring->next != NULL && ring->prev != NULL); - ASSERT(item->next == NULL && item->prev == NULL); + spice_assert(ring->next != NULL && ring->prev != NULL); + spice_assert(item->next == NULL && item->prev == NULL); item->next = ring->next; item->prev = ring; @@ -81,8 +81,8 @@ static inline void __ring_remove(RingItem *item) static inline void ring_remove(RingItem *item) { - ASSERT(item->next != NULL && item->prev != NULL); - ASSERT(item->next != item); + spice_assert(item->next != NULL && item->prev != NULL); + spice_assert(item->next != item); __ring_remove(item); } @@ -91,7 +91,7 @@ static inline RingItem *ring_get_head(Ring *ring) { RingItem *ret; - ASSERT(ring->next != NULL && ring->prev != NULL); + spice_assert(ring->next != NULL && ring->prev != NULL); if (ring_is_empty(ring)) { return NULL; @@ -104,7 +104,7 @@ static inline RingItem *ring_get_tail(Ring *ring) { RingItem *ret; - ASSERT(ring->next != NULL && ring->prev != NULL); + spice_assert(ring->next != NULL && ring->prev != NULL); if (ring_is_empty(ring)) { return NULL; @@ -117,9 +117,9 @@ static inline RingItem *ring_next(Ring *ring, RingItem *pos) { RingItem *ret; - ASSERT(ring->next != NULL && ring->prev != NULL); - ASSERT(pos); - ASSERT(pos->next != NULL && pos->prev != NULL); + spice_assert(ring->next != NULL && ring->prev != NULL); + spice_assert(pos); + spice_assert(pos->next != NULL && pos->prev != NULL); ret = pos->next; return (ret == ring) ? NULL : ret; } @@ -128,9 +128,9 @@ static inline RingItem *ring_prev(Ring *ring, RingItem *pos) { RingItem *ret; - ASSERT(ring->next != NULL && ring->prev != NULL); - ASSERT(pos); - ASSERT(pos->next != NULL && pos->prev != NULL); + spice_assert(ring->next != NULL && ring->prev != NULL); + spice_assert(pos); + spice_assert(pos->next != NULL && pos->prev != NULL); ret = pos->prev; return (ret == ring) ? NULL : ret; } diff --git a/common/rop3.c b/common/rop3.c index 12d9f11..d5b46db 100644 --- a/common/rop3.c +++ b/common/rop3.c @@ -47,13 +47,13 @@ static void default_rop3_with_pattern_handler(pixman_image_t *d, pixman_image_t SpicePoint *src_pos, pixman_image_t *p, SpicePoint *pat_pos) { - WARN("not implemented"); + spice_critical("not implemented"); } static void default_rop3_withe_color_handler(pixman_image_t *d, pixman_image_t *s, SpicePoint *src_pos, uint32_t rgb) { - WARN("not implemented"); + spice_critical("not implemented"); } static void default_rop3_test_handler(void) @@ -624,8 +624,8 @@ void do_rop3_with_pattern(uint8_t rop3, pixman_image_t *d, pixman_image_t *s, Sp int bpp; bpp = spice_pixman_image_get_bpp(d); - ASSERT (bpp == spice_pixman_image_get_bpp(s)); - ASSERT (bpp == spice_pixman_image_get_bpp(p)); + spice_assert(bpp == spice_pixman_image_get_bpp(s)); + spice_assert(bpp == spice_pixman_image_get_bpp(p)); if (bpp == 32) { rop3_with_pattern_handlers_32[rop3](d, s, src_pos, p, pat_pos); @@ -640,7 +640,7 @@ void do_rop3_with_color(uint8_t rop3, pixman_image_t *d, pixman_image_t *s, Spic int bpp; bpp = spice_pixman_image_get_bpp(d); - ASSERT (bpp == spice_pixman_image_get_bpp(s)); + spice_assert(bpp == spice_pixman_image_get_bpp(s)); if (bpp == 32) { rop3_with_color_handlers_32[rop3](d, s, src_pos, rgb); diff --git a/common/spice_common.h b/common/spice_common.h index e9f5ff5..5572326 100644 --- a/common/spice_common.h +++ b/common/spice_common.h @@ -23,57 +23,15 @@ #include <time.h> #include <stdlib.h> #include <stddef.h> -#include "backtrace.h" - -#define ASSERT(x) if (!(x)) { \ - printf("%s: ASSERT %s failed\n", __FUNCTION__, #x); \ - spice_backtrace(); \ - abort(); \ -} - -#define PANIC(format, ...) do { \ - printf("%s: panic: " format "\n", __FUNCTION__, ## __VA_ARGS__ ); \ - abort(); \ -} while (0) - -#define PANIC_ON(x) if ((x)) { \ - printf("%s: panic %s\n", __FUNCTION__, #x); \ - abort(); \ -} - -#define red_error(format, ...) do { \ - printf("%s: " format "\n", __FUNCTION__, ## __VA_ARGS__ ); \ - abort(); \ -} while (0) -#define red_printf(format, ...) \ - printf("%s: " format "\n", __FUNCTION__, ## __VA_ARGS__ ) -#define red_printf_once(format, ...) do { \ - static int do_print = TRUE; \ - if (do_print) { \ - do_print = FALSE; \ - printf("%s: " format "\n", __FUNCTION__, ## __VA_ARGS__ ); \ - } \ -} while (0) - -#define WARN(format, ...) red_printf("warning: "format, ##__VA_ARGS__ ); -#define WARN_ONCE red_printf_once - -#define red_printf_some(every, format, ...) do { \ - static int count = 0; \ - if (count++ % (every) == 0) { \ - printf("%s: " format "\n", __FUNCTION__, ## __VA_ARGS__ ); \ - } \ -} while (0) +#include <spice/macros.h> +#include "backtrace.h" +#include "log.h" -#define red_printf_debug(debug, prefix, format, ...) do { \ - static int debug_level = -1; \ - if (debug_level == -1) { \ - debug_level = getenv("SPICE_DEBUG_LEVEL") != NULL ? atoi(getenv("SPICE_DEBUG_LEVEL")) : 0; \ - } \ - if (debug <= debug_level) { \ - printf("%s: %s: " format "\n", prefix, __FUNCTION__, ## __VA_ARGS__ ); \ - } \ -} while(0) +#ifdef SPICE_DISABLE_ABORT +#define spice_abort() do { } while(0) +#else +#define spice_abort() abort() +#endif #endif diff --git a/common/sw_canvas.c b/common/sw_canvas.c index 8eabc89..c9c79c3 100644 --- a/common/sw_canvas.c +++ b/common/sw_canvas.c @@ -83,8 +83,10 @@ static pixman_image_t *canvas_get_pixman_brush(SwCanvas *canvas, case SPICE_BRUSH_TYPE_NONE: return NULL; default: - CANVAS_ERROR("invalid brush type"); + spice_warn_if_reached(); + return NULL; } + return NULL; } static pixman_image_t *get_image(SpiceCanvas *canvas) @@ -477,8 +479,8 @@ static void __scale_image(SpiceCanvas *spice_canvas, pixman_image_set_transform(src, &transform); pixman_image_set_repeat(src, PIXMAN_REPEAT_NONE); - ASSERT(scale_mode == SPICE_IMAGE_SCALE_MODE_INTERPOLATE || - scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST); + spice_return_if_fail(scale_mode == SPICE_IMAGE_SCALE_MODE_INTERPOLATE || + scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST); pixman_image_set_filter(src, (scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST) ? PIXMAN_FILTER_NEAREST : PIXMAN_FILTER_GOOD, @@ -539,11 +541,13 @@ static void __scale_image_rop(SpiceCanvas *spice_canvas, pixman_box32_t *rects; int n_rects, i; pixman_fixed_t fsx, fsy; + pixman_format_code_t format; fsx = ((pixman_fixed_48_16_t) src_width * 65536) / dest_width; fsy = ((pixman_fixed_48_16_t) src_height * 65536) / dest_height; - scaled = pixman_image_create_bits(spice_pixman_image_get_format(src), + spice_return_if_fail(spice_pixman_image_get_format(src, &format)); + scaled = pixman_image_create_bits(format, dest_width, dest_height, NULL, 0); @@ -558,8 +562,8 @@ static void __scale_image_rop(SpiceCanvas *spice_canvas, pixman_image_set_transform(src, &transform); pixman_image_set_repeat(src, PIXMAN_REPEAT_NONE); - ASSERT(scale_mode == SPICE_IMAGE_SCALE_MODE_INTERPOLATE || - scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST); + spice_return_if_fail(scale_mode == SPICE_IMAGE_SCALE_MODE_INTERPOLATE || + scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST); pixman_image_set_filter(src, (scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST) ? PIXMAN_FILTER_NEAREST : PIXMAN_FILTER_GOOD, @@ -759,8 +763,8 @@ static void __blend_scale_image(SpiceCanvas *spice_canvas, pixman_image_set_transform(src, &transform); pixman_image_set_repeat(src, PIXMAN_REPEAT_NONE); - ASSERT(scale_mode == SPICE_IMAGE_SCALE_MODE_INTERPOLATE || - scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST); + spice_return_if_fail(scale_mode == SPICE_IMAGE_SCALE_MODE_INTERPOLATE || + scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST); pixman_image_set_filter(src, (scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST) ? PIXMAN_FILTER_NEAREST : PIXMAN_FILTER_GOOD, @@ -895,11 +899,13 @@ static void __colorkey_scale_image(SpiceCanvas *spice_canvas, pixman_box32_t *rects; int n_rects, i; pixman_fixed_t fsx, fsy; + pixman_format_code_t format; fsx = ((pixman_fixed_48_16_t) src_width * 65536) / dest_width; fsy = ((pixman_fixed_48_16_t) src_height * 65536) / dest_height; - scaled = pixman_image_create_bits(spice_pixman_image_get_format (src), + spice_return_if_fail(spice_pixman_image_get_format(src, &format)); + scaled = pixman_image_create_bits(format, dest_width, dest_height, NULL, 0); @@ -1042,7 +1048,7 @@ static void canvas_draw_text(SpiceCanvas *spice_canvas, SpiceRect *bbox, pixman_region32_t dest_region; pixman_image_t *str_mask, *brush; SpiceString *str; - SpicePoint pos; + SpicePoint pos = { 0, }; int depth; pixman_region32_init_rect(&dest_region, @@ -1064,7 +1070,7 @@ static void canvas_draw_text(SpiceCanvas *spice_canvas, SpiceRect *bbox, /* Nothing else makes sense for text and we should deprecate it * and actually it means OVER really */ - ASSERT(text->fore_mode == SPICE_ROPD_OP_PUT); + spice_return_if_fail(text->fore_mode == SPICE_ROPD_OP_PUT); pixman_region32_init_rect(&back_region, text->back_area.left, @@ -1087,10 +1093,10 @@ static void canvas_draw_text(SpiceCanvas *spice_canvas, SpiceRect *bbox, } else if (str->flags & SPICE_STRING_FLAGS_RASTER_A4) { depth = 4; } else if (str->flags & SPICE_STRING_FLAGS_RASTER_A8) { - WARN("untested path A8 glyphs"); + spice_warning("untested path A8 glyphs"); depth = 8; } else { - WARN("unsupported path vector glyphs"); + spice_warning("unsupported path vector glyphs"); pixman_region32_fini (&dest_region); return; } @@ -1133,7 +1139,7 @@ static void canvas_read_bits(SpiceCanvas *spice_canvas, uint8_t *dest, uint8_t *dest_end; int bpp; - ASSERT(canvas && area); + spice_return_if_fail(canvas && area); surface = canvas->image; |