From 25486d7b3dfe7226553a2af74e15db4005a6a201 Mon Sep 17 00:00:00 2001 From: Rodger Combs Date: Thu, 23 Jun 2016 23:39:06 -0500 Subject: bitmap: use calloc instead of malloc/memset This can improve perf somewhat with large bitmaps --- libass/ass_bitmap.c | 32 ++++++++++++-------------------- libass/ass_bitmap.h | 2 +- libass/ass_blur.c | 2 +- libass/ass_render.c | 11 ++++++----- libass/ass_utils.c | 5 +++-- libass/ass_utils.h | 3 ++- 6 files changed, 25 insertions(+), 30 deletions(-) (limited to 'libass') diff --git a/libass/ass_bitmap.c b/libass/ass_bitmap.c index 2c354b3..e46ef0a 100644 --- a/libass/ass_bitmap.c +++ b/libass/ass_bitmap.c @@ -82,7 +82,7 @@ void ass_synth_blur(const BitmapEngine *engine, int opaque_box, int be, if (blur_g) size_g = sizeof(uint16_t) * bm_g->stride * 2; size_t size = FFMAX(size_o, size_g); - uint16_t *tmp = size ? ass_aligned_alloc(32, size) : NULL; + uint16_t *tmp = size ? ass_aligned_alloc(32, size, false) : NULL; if (!tmp) return; if (bm_o) { @@ -127,14 +127,15 @@ void ass_synth_blur(const BitmapEngine *engine, int opaque_box, int be, } } -static bool alloc_bitmap_buffer(const BitmapEngine *engine, Bitmap *bm, int w, int h) +static bool alloc_bitmap_buffer(const BitmapEngine *engine, Bitmap *bm, int w, int h, + bool zero) { unsigned align = 1 << engine->align_order; size_t s = ass_align(align, w); // Too often we use ints as offset for bitmaps => use INT_MAX. if (s > (INT_MAX - 32) / FFMAX(h, 1)) return false; - uint8_t *buf = ass_aligned_alloc(align, s * h + 32); + uint8_t *buf = ass_aligned_alloc(align, s * h + 32, zero); if (!buf) return false; bm->w = w; @@ -144,24 +145,15 @@ static bool alloc_bitmap_buffer(const BitmapEngine *engine, Bitmap *bm, int w, i return true; } -static Bitmap *alloc_bitmap_raw(const BitmapEngine *engine, int w, int h) +Bitmap *alloc_bitmap(const BitmapEngine *engine, int w, int h, bool zero) { Bitmap *bm = malloc(sizeof(Bitmap)); if (!bm) return NULL; - if (!alloc_bitmap_buffer(engine, bm, w, h)) { + if (!alloc_bitmap_buffer(engine, bm, w, h, zero)) { free(bm); return NULL; } - return bm; -} - -Bitmap *alloc_bitmap(const BitmapEngine *engine, int w, int h) -{ - Bitmap *bm = alloc_bitmap_raw(engine, w, h); - if(!bm) - return NULL; - memset(bm->buffer, 0, bm->stride * bm->h + 32); bm->left = bm->top = 0; return bm; } @@ -169,7 +161,7 @@ Bitmap *alloc_bitmap(const BitmapEngine *engine, int w, int h) bool realloc_bitmap(const BitmapEngine *engine, Bitmap *bm, int w, int h) { uint8_t *old = bm->buffer; - if (!alloc_bitmap_buffer(engine, bm, w, h)) + if (!alloc_bitmap_buffer(engine, bm, w, h, false)) return false; ass_aligned_free(old); return true; @@ -184,7 +176,7 @@ void ass_free_bitmap(Bitmap *bm) Bitmap *copy_bitmap(const BitmapEngine *engine, const Bitmap *src) { - Bitmap *dst = alloc_bitmap_raw(engine, src->w, src->h); + Bitmap *dst = alloc_bitmap(engine, src->w, src->h, false); if (!dst) return NULL; dst->left = src->left; @@ -208,7 +200,7 @@ Bitmap *outline_to_bitmap(ASS_Renderer *render_priv, return NULL; if (rst->x_min >= rst->x_max || rst->y_min >= rst->y_max) { - Bitmap *bm = alloc_bitmap(render_priv->engine, 2 * bord, 2 * bord); + Bitmap *bm = alloc_bitmap(render_priv->engine, 2 * bord, 2 * bord, true); if (!bm) return NULL; bm->left = bm->top = -bord; @@ -236,7 +228,7 @@ Bitmap *outline_to_bitmap(ASS_Renderer *render_priv, int tile_w = (w + 2 * bord + mask) & ~mask; int tile_h = (h + 2 * bord + mask) & ~mask; - Bitmap *bm = alloc_bitmap_raw(render_priv->engine, tile_w, tile_h); + Bitmap *bm = alloc_bitmap(render_priv->engine, tile_w, tile_h, false); if (!bm) return NULL; bm->left = x_min - bord; @@ -266,7 +258,7 @@ static Bitmap *outline_to_bitmap_ft(ASS_Renderer *render_priv, FT_Outline_Get_CBox(outline, &bbox); if (bbox.xMin >= bbox.xMax || bbox.yMin >= bbox.yMax) { - bm = alloc_bitmap(render_priv->engine, 2 * bord, 2 * bord); + bm = alloc_bitmap(render_priv->engine, 2 * bord, 2 * bord, true); if (!bm) return NULL; bm->left = bm->top = -bord; @@ -296,7 +288,7 @@ static Bitmap *outline_to_bitmap_ft(ASS_Renderer *render_priv, } // allocate and set up bitmap - bm = alloc_bitmap(render_priv->engine, w + 2 * bord, h + 2 * bord); + bm = alloc_bitmap(render_priv->engine, w + 2 * bord, h + 2 * bord, true); if (!bm) return NULL; bm->left = bbox.xMin - bord; diff --git a/libass/ass_bitmap.h b/libass/ass_bitmap.h index 36df816..ada3228 100644 --- a/libass/ass_bitmap.h +++ b/libass/ass_bitmap.h @@ -107,7 +107,7 @@ typedef struct { unsigned char *buffer; // h * stride buffer } Bitmap; -Bitmap *alloc_bitmap(const BitmapEngine *engine, int w, int h); +Bitmap *alloc_bitmap(const BitmapEngine *engine, int w, int h, bool zero); bool realloc_bitmap(const BitmapEngine *engine, Bitmap *bm, int w, int h); Bitmap *copy_bitmap(const BitmapEngine *engine, const Bitmap *src); void ass_free_bitmap(Bitmap *bm); diff --git a/libass/ass_blur.c b/libass/ass_blur.c index 870992e..636ab1a 100644 --- a/libass/ass_blur.c +++ b/libass/ass_blur.c @@ -851,7 +851,7 @@ bool ass_gaussian_blur(const BitmapEngine *engine, Bitmap *bm, double r2) const int stripe_width = 1 << (engine->align_order - 1); int size = end_h * ((end_w + stripe_width - 1) & ~(stripe_width - 1)); - int16_t *tmp = ass_aligned_alloc(2 * stripe_width, 4 * size); + int16_t *tmp = ass_aligned_alloc(2 * stripe_width, 4 * size, false); if (!tmp) return false; diff --git a/libass/ass_render.c b/libass/ass_render.c index 9bbaaa3..8790408 100644 --- a/libass/ass_render.c +++ b/libass/ass_render.c @@ -558,7 +558,7 @@ static void blend_vector_clip(ASS_Renderer *render_priv, } // Allocate new buffer and add to free list - nbuffer = ass_aligned_alloc(32, as * ah); + nbuffer = ass_aligned_alloc(32, as * ah, false); if (!nbuffer) break; @@ -578,7 +578,7 @@ static void blend_vector_clip(ASS_Renderer *render_priv, // Allocate new buffer and add to free list unsigned align = (w >= 16) ? 16 : ((w >= 8) ? 8 : 1); unsigned ns = ass_align(align, w); - nbuffer = ass_aligned_alloc(align, ns * h); + nbuffer = ass_aligned_alloc(align, ns * h, false); if (!nbuffer) break; @@ -2302,7 +2302,7 @@ static void render_and_combine_glyphs(ASS_Renderer *render_priv, } else if (info->n_bm) { info->bm = alloc_bitmap(render_priv->engine, info->rect.x_max - info->rect.x_min + 2 * bord, - info->rect.y_max - info->rect.y_min + 2 * bord); + info->rect.y_max - info->rect.y_min + 2 * bord, true); Bitmap *dst = info->bm; if (dst) { dst->left = info->rect.x_min - info->x - bord; @@ -2336,7 +2336,8 @@ static void render_and_combine_glyphs(ASS_Renderer *render_priv, } else if (info->n_bm_o) { info->bm_o = alloc_bitmap(render_priv->engine, info->rect_o.x_max - info->rect_o.x_min + 2 * bord, - info->rect_o.y_max - info->rect_o.y_min + 2 * bord); + info->rect_o.y_max - info->rect_o.y_min + 2 * bord, + true); Bitmap *dst = info->bm_o; if (dst) { dst->left = info->rect_o.x_min - info->x - bord; @@ -2378,7 +2379,7 @@ static void render_and_combine_glyphs(ASS_Renderer *render_priv, static void add_background(ASS_Renderer *render_priv, EventImages *event_images) { - void *nbuffer = ass_aligned_alloc(1, event_images->width * event_images->height); + void *nbuffer = ass_aligned_alloc(1, event_images->width * event_images->height, false); if (!nbuffer) return; memset(nbuffer, 0xFF, event_images->width * event_images->height); diff --git a/libass/ass_utils.c b/libass/ass_utils.c index bcaeec1..9dc2358 100644 --- a/libass/ass_utils.c +++ b/libass/ass_utils.c @@ -79,12 +79,13 @@ char *ass_strndup(const char *s, size_t n) } #endif -void *ass_aligned_alloc(size_t alignment, size_t size) +void *ass_aligned_alloc(size_t alignment, size_t size, bool zero) { assert(!(alignment & (alignment - 1))); // alignment must be power of 2 if (size >= SIZE_MAX - alignment - sizeof(void *)) return NULL; - char *allocation = malloc(size + sizeof(void *) + alignment - 1); + char *allocation = zero ? calloc(size + sizeof(void *) + alignment - 1, 1) + : malloc(size + sizeof(void *) + alignment - 1); if (!allocation) return NULL; char *ptr = allocation + sizeof(void *); diff --git a/libass/ass_utils.h b/libass/ass_utils.h index 5c3f65f..6c9f385 100644 --- a/libass/ass_utils.h +++ b/libass/ass_utils.h @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -56,7 +57,7 @@ char *ass_strndup(const char *s, size_t n); #define strndup ass_strndup #endif -void *ass_aligned_alloc(size_t alignment, size_t size); +void *ass_aligned_alloc(size_t alignment, size_t size, bool zero); void ass_aligned_free(void *ptr); void *ass_realloc_array(void *ptr, size_t nmemb, size_t size); -- cgit v1.2.3