From 64c42025195233e6130f166ba807eff291c647bf Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 17 Jun 2008 09:21:26 +0200 Subject: [PATCH] draw: remove draw_image_from_file() This should speed up tasklist image drawing. Signed-off-by: Julien Danjou --- common/draw.c | 68 ++++++++++++++++++---------------------------- common/draw.h | 1 - widgets/tasklist.c | 17 ++++-------- 3 files changed, 33 insertions(+), 53 deletions(-) diff --git a/common/draw.c b/common/draw.c index 06142070..535b4e14 100644 --- a/common/draw.c +++ b/common/draw.c @@ -721,47 +721,32 @@ draw_image_from_argb_data(draw_context_t *ctx, int x, int y, int w, int h, cairo_surface_destroy(source); } -/** Draw an image (PNG format only) from a file to a draw context - * \param ctx Draw context to draw to - * \param x x coordinate - * \param y y coordinate - * \param wanted_h wanted height: if > 0, image will be resized - * \param filename file name to draw - */ -void -draw_image_from_file(draw_context_t *ctx, int x, int y, int wanted_h, const char *filename) -{ - draw_image_t *image = draw_image_new(filename); - - if(image) - { - draw_image(ctx, x, y, wanted_h, image); - draw_image_delete(&image); - } -} - #ifndef WITH_IMLIB2 -/** Load an image (PNG Format only) from filename - * \param filename the image file to load - * \return a new image +/** Load an image from filename. + * \param filename The image file to load. + * \return A new image. */ -draw_image_t *draw_image_new(const char *filename) +draw_image_t +*draw_image_new(const char *filename) { - draw_image_t *image; + draw_image_t *image = NULL; GdkPixbuf *pixbuf; - GError *error=NULL; + GError *error = NULL; - if(!(pixbuf = gdk_pixbuf_new_from_file(filename,&error))) { - warn("cannot load image %s: %s", filename, error->message); - return NULL; + if(filename) + { + if(!(pixbuf = gdk_pixbuf_new_from_file(filename,&error))) + warn("cannot load image %s: %s", filename, error->message); + else + { + image = p_new(draw_image_t, 1); + image->data = pixbuf; + image->width = gdk_pixbuf_get_width(pixbuf); + image->height = gdk_pixbuf_get_height(pixbuf); + } } - image = p_new(draw_image_t, 1); - image->data = pixbuf; - image->width = gdk_pixbuf_get_width(pixbuf); - image->height = gdk_pixbuf_get_height(pixbuf); - return image; } @@ -770,21 +755,22 @@ draw_image_t *draw_image_new(const char *filename) */ void draw_image_delete(draw_image_t **image) { - if (*image) + if(*image) { gdk_pixbuf_unref((*image)->data); p_delete(image); } } -/** Draw an image to a draw context - * \param ctx Draw context to draw to - * \param x x coordinate - * \param y y coordinate - * \param wanted_h wanted height: if > 0, image will be resized - * \param image the image to draw +/** Draw an image to a draw context. + * \param ctx Draw context to draw to. + * \param x x coordinate. + * \param y y coordinate. + * \param wanted_h Wanted height: if > 0, image will be resized. + * \param image The image to draw. */ -void draw_image(draw_context_t *ctx, int x, int y, int wanted_h, draw_image_t *image) +void +draw_image(draw_context_t *ctx, int x, int y, int wanted_h, draw_image_t *image) { cairo_t *cr; diff --git a/common/draw.h b/common/draw.h index ff3d3ca9..55f873d5 100644 --- a/common/draw.h +++ b/common/draw.h @@ -165,7 +165,6 @@ void draw_circle(draw_context_t *, int, int, int, bool, xcolor_t); draw_image_t *draw_image_new(const char *); void draw_image_delete(draw_image_t **); void draw_image(draw_context_t *, int, int, int, draw_image_t *); -void draw_image_from_file(draw_context_t *, int, int, int, const char *); void draw_image_from_argb_data(draw_context_t *, int, int, int, int, int, unsigned char *); area_t draw_get_image_size(const char *filename); void draw_rotate(draw_context_t *, xcb_drawable_t, xcb_drawable_t, int, int, int, int, double, int, int); diff --git a/widgets/tasklist.c b/widgets/tasklist.c index ca8b2101..d1754c2d 100644 --- a/widgets/tasklist.c +++ b/widgets/tasklist.c @@ -77,6 +77,7 @@ tasklist_draw(draw_context_t *ctx, int screen, markup_parser_data_t *p; const char *elements[] = { "bg", NULL }; xcolor_t bg_color; + draw_image_t *image; if(used >= ctx->width) return (w->area.width = 0); @@ -133,18 +134,12 @@ tasklist_draw(draw_context_t *ctx, int screen, p->text = NULL; markup_parser_data_delete(&p); - if(c->icon_path) + if((image = draw_image_new(c->icon_path))) { - area = draw_get_image_size(c->icon_path); - if(area.width > 0 && area.height > 0) - { - icon_width = ((double) ctx->height / (double) area.height) * area.width; - draw_image_from_file(ctx, - w->area.x + box_width * i, - w->area.y, - ctx->height, - c->icon_path); - } + icon_width = ((double) ctx->height / (double) image->height) * image->width; + draw_image(ctx, w->area.x + box_width * i, + w->area.y, ctx->height, image); + draw_image_delete(&image); } if(!icon_width && (icon = ewmh_get_window_icon(c->win)))